diff options
author | 2019-09-24 15:39:33 +0200 | |
---|---|---|
committer | 2019-09-24 15:39:33 +0200 | |
commit | 68c4752e998dca1d618380ce4e7d8ac52b710056 (patch) | |
tree | 35e386030b072c189509bb2ed3adeaec5b0fd4d1 /native/src/widget/checkbox.rs | |
parent | bb5cac49d028eb53c259ae58e3a007ebfb736fd4 (diff) | |
parent | 05c7c39ecb8910c75b82dc4052a7720fb2d42b4a (diff) | |
download | iced-68c4752e998dca1d618380ce4e7d8ac52b710056.tar.gz iced-68c4752e998dca1d618380ce4e7d8ac52b710056.tar.bz2 iced-68c4752e998dca1d618380ce4e7d8ac52b710056.zip |
Merge pull request #17 from hecrj/web
Basic web support (core, native, and web crates)
Diffstat (limited to '')
-rw-r--r-- | native/src/widget/checkbox.rs | 95 |
1 files changed, 95 insertions, 0 deletions
diff --git a/native/src/widget/checkbox.rs b/native/src/widget/checkbox.rs new file mode 100644 index 00000000..3e307f64 --- /dev/null +++ b/native/src/widget/checkbox.rs @@ -0,0 +1,95 @@ +//! Show toggle controls using checkboxes. +use std::hash::Hash; + +use crate::input::{mouse, ButtonState}; +use crate::{Element, Event, Hasher, Layout, MouseCursor, Node, Point, Widget}; + +pub use iced_core::Checkbox; + +impl<Message, Renderer> Widget<Message, Renderer> for Checkbox<Message> +where + Renderer: self::Renderer, +{ + 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, + }) => { + let mouse_over = layout + .children() + .any(|child| child.bounds().contains(cursor_position)); + + if mouse_over { + messages.push((self.on_toggle)(!self.is_checked)); + } + } + _ => {} + } + } + + 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 [`Checkbox`]. +/// +/// Your [renderer] will need to implement this trait before being +/// able to use a [`Checkbox`] in your user interface. +/// +/// [`Checkbox`]: struct.Checkbox.html +/// [renderer]: ../../renderer/index.html +pub trait Renderer { + /// Creates a [`Node`] for the provided [`Checkbox`]. + /// + /// [`Node`]: ../../struct.Node.html + /// [`Checkbox`]: struct.Checkbox.html + fn node<Message>(&mut self, checkbox: &Checkbox<Message>) -> Node; + + /// Draws a [`Checkbox`]. + /// + /// It receives: + /// * the current cursor position + /// * the bounds of the [`Checkbox`] + /// * the bounds of the label of the [`Checkbox`] + /// * whether the [`Checkbox`] is checked or not + /// + /// [`Checkbox`]: struct.Checkbox.html + fn draw<Message>( + &mut self, + checkbox: &Checkbox<Message>, + layout: Layout<'_>, + cursor_position: Point, + ) -> MouseCursor; +} + +impl<'a, Message, Renderer> From<Checkbox<Message>> + for Element<'a, Message, Renderer> +where + Renderer: self::Renderer, + Message: 'static, +{ + fn from(checkbox: Checkbox<Message>) -> Element<'a, Message, Renderer> { + Element::new(checkbox) + } +} |