summaryrefslogtreecommitdiffstats
path: root/native/src/widget/radio.rs
diff options
context:
space:
mode:
authorLibravatar Héctor Ramón Jiménez <hector0193@gmail.com>2019-09-20 19:15:31 +0200
committerLibravatar Héctor Ramón Jiménez <hector0193@gmail.com>2019-09-20 19:15:31 +0200
commitb9e0f7494881ad7cdfbcbc16878ecc6ef717753f (patch)
treec8a7419b5cb4c0161306c479b93038f2f86498c2 /native/src/widget/radio.rs
parentb83a4b42dd912b5f59d40e7d4f7f7ccdabc43019 (diff)
downloadiced-b9e0f7494881ad7cdfbcbc16878ecc6ef717753f.tar.gz
iced-b9e0f7494881ad7cdfbcbc16878ecc6ef717753f.tar.bz2
iced-b9e0f7494881ad7cdfbcbc16878ecc6ef717753f.zip
Create `iced_core` and `iced_native`
Diffstat (limited to 'native/src/widget/radio.rs')
-rw-r--r--native/src/widget/radio.rs92
1 files changed, 92 insertions, 0 deletions
diff --git a/native/src/widget/radio.rs b/native/src/widget/radio.rs
new file mode 100644
index 00000000..33d42e61
--- /dev/null
+++ b/native/src/widget/radio.rs
@@ -0,0 +1,92 @@
+//! Create choices using radio buttons.
+use crate::input::{mouse, ButtonState};
+use crate::{Element, Event, Hasher, Layout, MouseCursor, Node, Point, Widget};
+
+use std::hash::Hash;
+
+pub use iced_core::Radio;
+
+impl<Message, Renderer> Widget<Message, Renderer> for Radio<Message>
+where
+ Renderer: self::Renderer,
+ Message: Copy + std::fmt::Debug,
+{
+ fn node(&self, renderer: &mut Renderer) -> Node {
+ renderer.node(&self)
+ }
+
+ fn on_event(
+ &mut self,
+ event: Event,
+ layout: Layout<'_>,
+ cursor_position: Point,
+ messages: &mut Vec<Message>,
+ ) {
+ match event {
+ Event::Mouse(mouse::Event::Input {
+ button: mouse::Button::Left,
+ state: ButtonState::Pressed,
+ }) => {
+ if layout.bounds().contains(cursor_position) {
+ messages.push(self.on_click);
+ }
+ }
+ _ => {}
+ }
+ }
+
+ fn draw(
+ &self,
+ renderer: &mut Renderer,
+ layout: Layout<'_>,
+ cursor_position: Point,
+ ) -> MouseCursor {
+ renderer.draw(&self, layout, cursor_position)
+ }
+
+ fn hash_layout(&self, state: &mut Hasher) {
+ self.label.hash(state);
+ }
+}
+
+/// The renderer of a [`Radio`] button.
+///
+/// Your [renderer] will need to implement this trait before being
+/// able to use a [`Radio`] button in your user interface.
+///
+/// [`Radio`]: struct.Radio.html
+/// [renderer]: ../../renderer/index.html
+pub trait Renderer {
+ /// Creates a [`Node`] for the provided [`Radio`].
+ ///
+ /// [`Node`]: ../../struct.Node.html
+ /// [`Radio`]: struct.Radio.html
+ fn node<Message>(&mut self, radio: &Radio<Message>) -> Node;
+
+ /// Draws a [`Radio`] button.
+ ///
+ /// It receives:
+ /// * the current cursor position
+ /// * the bounds of the [`Radio`]
+ /// * the bounds of the label of the [`Radio`]
+ /// * whether the [`Radio`] is selected or not
+ ///
+ /// [`Radio`]: struct.Radio.html
+ fn draw<Message>(
+ &mut self,
+ radio: &Radio<Message>,
+ layout: Layout<'_>,
+ cursor_position: Point,
+ ) -> MouseCursor;
+}
+
+impl<'a, Message, Renderer> From<Radio<Message>>
+ for Element<'a, Message, Renderer>
+where
+ Renderer: self::Renderer,
+ Message: 'static + Copy + std::fmt::Debug,
+{
+ fn from(checkbox: Radio<Message>) -> Element<'a, Message, Renderer> {
+ Element::new(checkbox)
+ }
+}