diff options
author | 2019-10-30 03:31:07 +0100 | |
---|---|---|
committer | 2019-10-30 03:31:07 +0100 | |
commit | 63cd0fd8eb1eebae8de7d5141c846fc4ea55d702 (patch) | |
tree | e0a427a35435ad5fe5ce98a0b5050a1026a80207 /native | |
parent | 85916c9e8710ee90cbf37d384acbb6d208ff1da3 (diff) | |
download | iced-63cd0fd8eb1eebae8de7d5141c846fc4ea55d702.tar.gz iced-63cd0fd8eb1eebae8de7d5141c846fc4ea55d702.tar.bz2 iced-63cd0fd8eb1eebae8de7d5141c846fc4ea55d702.zip |
Draft `TextInput` widget structure
Also started a `todos` example to showcase it!
Diffstat (limited to 'native')
-rw-r--r-- | native/src/mouse_cursor.rs | 3 | ||||
-rw-r--r-- | native/src/widget.rs | 3 | ||||
-rw-r--r-- | native/src/widget/text_input.rs | 84 |
3 files changed, 90 insertions, 0 deletions
diff --git a/native/src/mouse_cursor.rs b/native/src/mouse_cursor.rs index 291e57a6..a4740a27 100644 --- a/native/src/mouse_cursor.rs +++ b/native/src/mouse_cursor.rs @@ -18,4 +18,7 @@ pub enum MouseCursor { /// The cursor is grabbing a widget. Grabbing, + + /// The cursor is over a text widget. + Text, } diff --git a/native/src/widget.rs b/native/src/widget.rs index c04f3377..01f5c92e 100644 --- a/native/src/widget.rs +++ b/native/src/widget.rs @@ -29,6 +29,7 @@ pub mod row; pub mod scrollable; pub mod slider; pub mod text; +pub mod text_input; #[doc(no_inline)] pub use button::Button; @@ -48,6 +49,8 @@ pub use scrollable::Scrollable; pub use slider::Slider; #[doc(no_inline)] pub use text::Text; +#[doc(no_inline)] +pub use text_input::TextInput; use crate::{Event, Hasher, Layout, Node, Point}; diff --git a/native/src/widget/text_input.rs b/native/src/widget/text_input.rs new file mode 100644 index 00000000..0cb949d1 --- /dev/null +++ b/native/src/widget/text_input.rs @@ -0,0 +1,84 @@ +use crate::{ + Element, Event, Hasher, Layout, Length, Node, Point, Rectangle, Style, + Widget, +}; + +pub use iced_core::{text_input::State, TextInput}; + +impl<'a, Message, Renderer> Widget<Message, Renderer> for TextInput<'a, Message> +where + Renderer: self::Renderer, + Message: Clone + std::fmt::Debug, +{ + fn node(&self, renderer: &Renderer) -> Node { + let text_bounds = + Node::new(Style::default().width(Length::Fill).height( + Length::Units(self.size.unwrap_or(renderer.default_size())), + )); + + Node::with_children( + Style::default() + .width(self.width) + .max_width(self.width) + .padding(self.padding), + vec![text_bounds], + ) + } + + fn on_event( + &mut self, + _event: Event, + _layout: Layout<'_>, + _cursor_position: Point, + _messages: &mut Vec<Message>, + _renderer: &Renderer, + ) { + } + + fn draw( + &self, + renderer: &mut Renderer, + layout: Layout<'_>, + cursor_position: Point, + ) -> Renderer::Output { + let bounds = layout.bounds(); + let text_bounds = layout.children().next().unwrap().bounds(); + + renderer.draw(&self, bounds, text_bounds, cursor_position) + } + + fn hash_layout(&self, state: &mut Hasher) { + use std::any::TypeId; + use std::hash::Hash; + + TypeId::of::<TextInput<'static, ()>>().hash(state); + + self.width.hash(state); + self.max_width.hash(state); + self.padding.hash(state); + self.size.hash(state); + } +} + +pub trait Renderer: crate::Renderer + Sized { + fn default_size(&self) -> u16; + + fn draw<Message>( + &mut self, + text_input: &TextInput<'_, Message>, + bounds: Rectangle, + text_bounds: Rectangle, + cursor_position: Point, + ) -> Self::Output; +} + +impl<'a, Message, Renderer> From<TextInput<'a, Message>> + for Element<'a, Message, Renderer> +where + Renderer: 'static + self::Renderer, + Message: 'static + Clone + std::fmt::Debug, +{ + fn from(button: TextInput<'a, Message>) -> Element<'a, Message, Renderer> { + Element::new(button) + } +} |