From 897188317b5875cc00a0f1c797790df8ac13687f Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Fri, 11 Feb 2022 17:50:12 +0700 Subject: Rename `iced_virtual` to `iced_pure` `virtual` is a reserved keyword in Rust :grimacing: --- pure/src/widget/button.rs | 272 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 272 insertions(+) create mode 100644 pure/src/widget/button.rs (limited to 'pure/src/widget/button.rs') diff --git a/pure/src/widget/button.rs b/pure/src/widget/button.rs new file mode 100644 index 00000000..ece90811 --- /dev/null +++ b/pure/src/widget/button.rs @@ -0,0 +1,272 @@ +use crate::widget::{Element, Tree, Widget}; + +use iced_native::event::{self, Event}; +use iced_native::layout; +use iced_native::mouse; +use iced_native::renderer; +use iced_native::touch; +use iced_native::{ + Background, Clipboard, Color, Hasher, Layout, Length, Padding, Point, + Rectangle, Shell, Vector, +}; +use iced_style::button::StyleSheet; + +use std::any::Any; + +pub struct Button { + content: Element, + on_press: Option, + style_sheet: Box, + width: Length, + height: Length, + padding: Padding, +} + +impl Button { + pub fn new(content: impl Into>) -> Self { + Button { + content: content.into(), + on_press: None, + style_sheet: Default::default(), + width: Length::Shrink, + height: Length::Shrink, + padding: Padding::new(5), + } + } + + pub fn on_press(mut self, on_press: Message) -> Self { + self.on_press = Some(on_press); + self + } +} + +impl Widget for Button +where + Message: 'static + Clone, + Renderer: 'static + iced_native::Renderer, +{ + fn tag(&self) -> std::any::TypeId { + std::any::TypeId::of::() + } + + fn state(&self) -> Box { + Box::new(State { is_pressed: false }) + } + + fn children(&self) -> &[Element] { + std::slice::from_ref(&self.content) + } + + fn width(&self) -> Length { + self.width + } + + fn height(&self) -> Length { + self.height + } + + fn hash_layout(&self, state: &mut Hasher) { + use std::hash::Hash; + + self.tag().hash(state); + self.width.hash(state); + self.content.as_widget().hash_layout(state); + } + + fn layout( + &self, + renderer: &Renderer, + limits: &layout::Limits, + ) -> layout::Node { + let limits = limits + .width(self.width) + .height(self.height) + .pad(self.padding); + + let mut content = self.content.as_widget().layout(renderer, &limits); + content.move_to(Point::new( + self.padding.left.into(), + self.padding.top.into(), + )); + + let size = limits.resolve(content.size()).pad(self.padding); + + layout::Node::with_children(size, vec![content]) + } + + fn on_event( + &mut self, + tree: &mut Tree, + event: Event, + layout: Layout<'_>, + cursor_position: Point, + renderer: &Renderer, + clipboard: &mut dyn Clipboard, + shell: &mut Shell<'_, Message>, + ) -> event::Status { + let state = if let Some(state) = tree.state.downcast_mut::() { + state + } else { + return event::Status::Ignored; + }; + + if let event::Status::Captured = self.content.as_widget_mut().on_event( + &mut tree.children[0], + event.clone(), + layout.children().next().unwrap(), + cursor_position, + renderer, + clipboard, + shell, + ) { + return event::Status::Captured; + } + + match event { + Event::Mouse(mouse::Event::ButtonPressed(mouse::Button::Left)) + | Event::Touch(touch::Event::FingerPressed { .. }) => { + if self.on_press.is_some() { + let bounds = layout.bounds(); + + if bounds.contains(cursor_position) { + state.is_pressed = true; + + return event::Status::Captured; + } + } + } + Event::Mouse(mouse::Event::ButtonReleased(mouse::Button::Left)) + | Event::Touch(touch::Event::FingerLifted { .. }) => { + if let Some(on_press) = self.on_press.clone() { + let bounds = layout.bounds(); + + if state.is_pressed { + state.is_pressed = false; + + if bounds.contains(cursor_position) { + shell.publish(on_press); + } + + return event::Status::Captured; + } + } + } + Event::Touch(touch::Event::FingerLost { .. }) => { + state.is_pressed = false; + } + _ => {} + } + + event::Status::Ignored + } + + fn draw( + &self, + tree: &Tree, + renderer: &mut Renderer, + _style: &renderer::Style, + layout: Layout<'_>, + cursor_position: Point, + _viewport: &Rectangle, + ) { + let state = if let Some(state) = tree.state.downcast_ref::() { + state + } else { + return; + }; + + let bounds = layout.bounds(); + let content_layout = layout.children().next().unwrap(); + + let is_mouse_over = bounds.contains(cursor_position); + let is_disabled = self.on_press.is_none(); + + let styling = if is_disabled { + self.style_sheet.disabled() + } else if is_mouse_over { + if state.is_pressed { + self.style_sheet.pressed() + } else { + self.style_sheet.hovered() + } + } else { + self.style_sheet.active() + }; + + if styling.background.is_some() || styling.border_width > 0.0 { + if styling.shadow_offset != Vector::default() { + // TODO: Implement proper shadow support + renderer.fill_quad( + renderer::Quad { + bounds: Rectangle { + x: bounds.x + styling.shadow_offset.x, + y: bounds.y + styling.shadow_offset.y, + ..bounds + }, + border_radius: styling.border_radius, + border_width: 0.0, + border_color: Color::TRANSPARENT, + }, + Background::Color([0.0, 0.0, 0.0, 0.5].into()), + ); + } + + renderer.fill_quad( + renderer::Quad { + bounds, + border_radius: styling.border_radius, + border_width: styling.border_width, + border_color: styling.border_color, + }, + styling + .background + .unwrap_or(Background::Color(Color::TRANSPARENT)), + ); + } + + self.content.as_widget().draw( + &tree.children[0], + renderer, + &renderer::Style { + text_color: styling.text_color, + }, + content_layout, + cursor_position, + &bounds, + ); + } + + fn mouse_interaction( + &self, + _tree: &Tree, + layout: Layout<'_>, + cursor_position: Point, + _viewport: &Rectangle, + _renderer: &Renderer, + ) -> mouse::Interaction { + let is_mouse_over = layout.bounds().contains(cursor_position); + let is_disabled = self.on_press.is_none(); + + if is_mouse_over && !is_disabled { + mouse::Interaction::Pointer + } else { + mouse::Interaction::default() + } + } +} + +#[derive(Debug, Clone)] +struct State { + is_pressed: bool, +} + +impl Into> + for Button +where + Message: Clone + 'static, + Renderer: iced_native::Renderer + 'static, +{ + fn into(self) -> Element { + Element::new(self) + } +} -- cgit From 01c5004959c9b11f2580840f4553ad7d706f4564 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Fri, 11 Feb 2022 22:07:21 +0700 Subject: Allow pure widgets to borrow from `Application` data :tada: --- pure/src/widget/button.rs | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) (limited to 'pure/src/widget/button.rs') diff --git a/pure/src/widget/button.rs b/pure/src/widget/button.rs index ece90811..198a3af9 100644 --- a/pure/src/widget/button.rs +++ b/pure/src/widget/button.rs @@ -13,17 +13,17 @@ use iced_style::button::StyleSheet; use std::any::Any; -pub struct Button { - content: Element, +pub struct Button<'a, Message, Renderer> { + content: Element<'a, Message, Renderer>, on_press: Option, - style_sheet: Box, + style_sheet: Box, width: Length, height: Length, padding: Padding, } -impl Button { - pub fn new(content: impl Into>) -> Self { +impl<'a, Message, Renderer> Button<'a, Message, Renderer> { + pub fn new(content: impl Into>) -> Self { Button { content: content.into(), on_press: None, @@ -40,7 +40,8 @@ impl Button { } } -impl Widget for Button +impl<'a, Message, Renderer> Widget + for Button<'a, Message, Renderer> where Message: 'static + Clone, Renderer: 'static + iced_native::Renderer, @@ -96,7 +97,7 @@ where fn on_event( &mut self, - tree: &mut Tree, + tree: &mut Tree, event: Event, layout: Layout<'_>, cursor_position: Point, @@ -162,7 +163,7 @@ where fn draw( &self, - tree: &Tree, + tree: &Tree, renderer: &mut Renderer, _style: &renderer::Style, layout: Layout<'_>, @@ -238,7 +239,7 @@ where fn mouse_interaction( &self, - _tree: &Tree, + _tree: &Tree, layout: Layout<'_>, cursor_position: Point, _viewport: &Rectangle, @@ -260,13 +261,13 @@ struct State { is_pressed: bool, } -impl Into> - for Button +impl<'a, Message, Renderer> Into> + for Button<'a, Message, Renderer> where Message: Clone + 'static, Renderer: iced_native::Renderer + 'static, { - fn into(self) -> Element { + fn into(self) -> Element<'a, Message, Renderer> { Element::new(self) } } -- cgit From ecb3df8e018930c407e469ce2b8f4208a9d15426 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Fri, 11 Feb 2022 23:17:07 +0700 Subject: Expose reusable `Button` logic ... and reuse it in `iced_pure`! --- pure/src/widget/button.rs | 162 +++++++++++----------------------------------- 1 file changed, 38 insertions(+), 124 deletions(-) (limited to 'pure/src/widget/button.rs') diff --git a/pure/src/widget/button.rs b/pure/src/widget/button.rs index 198a3af9..89acb7f5 100644 --- a/pure/src/widget/button.rs +++ b/pure/src/widget/button.rs @@ -4,15 +4,16 @@ use iced_native::event::{self, Event}; use iced_native::layout; use iced_native::mouse; use iced_native::renderer; -use iced_native::touch; +use iced_native::widget::button; use iced_native::{ - Background, Clipboard, Color, Hasher, Layout, Length, Padding, Point, - Rectangle, Shell, Vector, + Clipboard, Hasher, Layout, Length, Padding, Point, Rectangle, Shell, }; use iced_style::button::StyleSheet; use std::any::Any; +pub use button::State; + pub struct Button<'a, Message, Renderer> { content: Element<'a, Message, Renderer>, on_press: Option, @@ -51,7 +52,7 @@ where } fn state(&self) -> Box { - Box::new(State { is_pressed: false }) + Box::new(State::new()) } fn children(&self) -> &[Element] { @@ -71,6 +72,8 @@ where self.tag().hash(state); self.width.hash(state); + self.height.hash(state); + self.padding.hash(state); self.content.as_widget().hash_layout(state); } @@ -79,20 +82,16 @@ where renderer: &Renderer, limits: &layout::Limits, ) -> layout::Node { - let limits = limits - .width(self.width) - .height(self.height) - .pad(self.padding); - - let mut content = self.content.as_widget().layout(renderer, &limits); - content.move_to(Point::new( - self.padding.left.into(), - self.padding.top.into(), - )); - - let size = limits.resolve(content.size()).pad(self.padding); - - layout::Node::with_children(size, vec![content]) + button::layout( + renderer, + limits, + self.width, + self.height, + self.padding, + |renderer, limits| { + self.content.as_widget().layout(renderer, &limits) + }, + ) } fn on_event( @@ -105,12 +104,6 @@ where clipboard: &mut dyn Clipboard, shell: &mut Shell<'_, Message>, ) -> event::Status { - let state = if let Some(state) = tree.state.downcast_mut::() { - state - } else { - return event::Status::Ignored; - }; - if let event::Status::Captured = self.content.as_widget_mut().on_event( &mut tree.children[0], event.clone(), @@ -123,42 +116,14 @@ where return event::Status::Captured; } - match event { - Event::Mouse(mouse::Event::ButtonPressed(mouse::Button::Left)) - | Event::Touch(touch::Event::FingerPressed { .. }) => { - if self.on_press.is_some() { - let bounds = layout.bounds(); - - if bounds.contains(cursor_position) { - state.is_pressed = true; - - return event::Status::Captured; - } - } - } - Event::Mouse(mouse::Event::ButtonReleased(mouse::Button::Left)) - | Event::Touch(touch::Event::FingerLifted { .. }) => { - if let Some(on_press) = self.on_press.clone() { - let bounds = layout.bounds(); - - if state.is_pressed { - state.is_pressed = false; - - if bounds.contains(cursor_position) { - shell.publish(on_press); - } - - return event::Status::Captured; - } - } - } - Event::Touch(touch::Event::FingerLost { .. }) => { - state.is_pressed = false; - } - _ => {} - } - - event::Status::Ignored + button::update( + event, + layout, + cursor_position, + shell, + &self.on_press, + || tree.state_mut::(), + ) } fn draw( @@ -170,60 +135,17 @@ where cursor_position: Point, _viewport: &Rectangle, ) { - let state = if let Some(state) = tree.state.downcast_ref::() { - state - } else { - return; - }; - let bounds = layout.bounds(); let content_layout = layout.children().next().unwrap(); - let is_mouse_over = bounds.contains(cursor_position); - let is_disabled = self.on_press.is_none(); - - let styling = if is_disabled { - self.style_sheet.disabled() - } else if is_mouse_over { - if state.is_pressed { - self.style_sheet.pressed() - } else { - self.style_sheet.hovered() - } - } else { - self.style_sheet.active() - }; - - if styling.background.is_some() || styling.border_width > 0.0 { - if styling.shadow_offset != Vector::default() { - // TODO: Implement proper shadow support - renderer.fill_quad( - renderer::Quad { - bounds: Rectangle { - x: bounds.x + styling.shadow_offset.x, - y: bounds.y + styling.shadow_offset.y, - ..bounds - }, - border_radius: styling.border_radius, - border_width: 0.0, - border_color: Color::TRANSPARENT, - }, - Background::Color([0.0, 0.0, 0.0, 0.5].into()), - ); - } - - renderer.fill_quad( - renderer::Quad { - bounds, - border_radius: styling.border_radius, - border_width: styling.border_width, - border_color: styling.border_color, - }, - styling - .background - .unwrap_or(Background::Color(Color::TRANSPARENT)), - ); - } + let styling = button::draw( + renderer, + bounds, + cursor_position, + self.on_press.is_some(), + self.style_sheet.as_ref(), + || tree.state::(), + ); self.content.as_widget().draw( &tree.children[0], @@ -245,22 +167,14 @@ where _viewport: &Rectangle, _renderer: &Renderer, ) -> mouse::Interaction { - let is_mouse_over = layout.bounds().contains(cursor_position); - let is_disabled = self.on_press.is_none(); - - if is_mouse_over && !is_disabled { - mouse::Interaction::Pointer - } else { - mouse::Interaction::default() - } + button::mouse_interaction( + layout, + cursor_position, + self.on_press.is_some(), + ) } } -#[derive(Debug, Clone)] -struct State { - is_pressed: bool, -} - impl<'a, Message, Renderer> Into> for Button<'a, Message, Renderer> where -- cgit From dd3e74e74de3a416d9d2dcfee051d78ba03dc540 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Fri, 11 Feb 2022 23:33:03 +0700 Subject: Complete `Button` in `iced_pure` --- pure/src/widget/button.rs | 34 ++++++++++++++++++++++++++++++++-- 1 file changed, 32 insertions(+), 2 deletions(-) (limited to 'pure/src/widget/button.rs') diff --git a/pure/src/widget/button.rs b/pure/src/widget/button.rs index 89acb7f5..b9561b09 100644 --- a/pure/src/widget/button.rs +++ b/pure/src/widget/button.rs @@ -35,8 +35,38 @@ impl<'a, Message, Renderer> Button<'a, Message, Renderer> { } } - pub fn on_press(mut self, on_press: Message) -> Self { - self.on_press = Some(on_press); + /// Sets the width of the [`Button`]. + pub fn width(mut self, width: Length) -> Self { + self.width = width; + self + } + + /// Sets the height of the [`Button`]. + pub fn height(mut self, height: Length) -> Self { + self.height = height; + self + } + + /// Sets the [`Padding`] of the [`Button`]. + pub fn padding>(mut self, padding: P) -> Self { + self.padding = padding.into(); + self + } + + /// Sets the message that will be produced when the [`Button`] is pressed. + /// + /// Unless `on_press` is called, the [`Button`] will be disabled. + pub fn on_press(mut self, msg: Message) -> Self { + self.on_press = Some(msg); + self + } + + /// Sets the style of the [`Button`]. + pub fn style( + mut self, + style_sheet: impl Into>, + ) -> Self { + self.style_sheet = style_sheet.into(); self } } -- cgit From b2670e8752eb96a4018f93b9cb8945da81a7ebff Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Sat, 12 Feb 2022 15:17:44 +0700 Subject: Implement `Scrollable` in `iced_pure` --- pure/src/widget/button.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'pure/src/widget/button.rs') diff --git a/pure/src/widget/button.rs b/pure/src/widget/button.rs index b9561b09..d0f9e53e 100644 --- a/pure/src/widget/button.rs +++ b/pure/src/widget/button.rs @@ -152,7 +152,7 @@ where cursor_position, shell, &self.on_press, - || tree.state_mut::(), + || tree.state.downcast_mut::(), ) } @@ -174,7 +174,7 @@ where cursor_position, self.on_press.is_some(), self.style_sheet.as_ref(), - || tree.state::(), + || tree.state.downcast_ref::(), ); self.content.as_widget().draw( -- cgit From bd22cc0bc0f7551d29cf2acd22520f4a906f253c Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Sat, 12 Feb 2022 17:21:28 +0700 Subject: Implement pure version of `todos` example :tada: The `Widget` trait in `iced_pure` needed to change a bit to make the implementation of `Element::map` possible. Specifically, the `children` method has been split into `diff` and `children_state`. --- pure/src/widget/button.rs | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) (limited to 'pure/src/widget/button.rs') diff --git a/pure/src/widget/button.rs b/pure/src/widget/button.rs index d0f9e53e..6dc1016c 100644 --- a/pure/src/widget/button.rs +++ b/pure/src/widget/button.rs @@ -85,8 +85,12 @@ where Box::new(State::new()) } - fn children(&self) -> &[Element] { - std::slice::from_ref(&self.content) + fn diff(&self, tree: &mut Tree) { + tree.diff_children(std::slice::from_ref(&self.content)) + } + + fn children_state(&self) -> Vec { + vec![Tree::new(&self.content)] } fn width(&self) -> Length { -- cgit From 35e9b75e415ef3b9124051696b60628ef56afe47 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Wed, 16 Feb 2022 15:44:50 +0700 Subject: Introduce `Tag` and `State` opaque types in `iced_pure::widget::tree` --- pure/src/widget/button.rs | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) (limited to 'pure/src/widget/button.rs') diff --git a/pure/src/widget/button.rs b/pure/src/widget/button.rs index 6dc1016c..55cbf8b4 100644 --- a/pure/src/widget/button.rs +++ b/pure/src/widget/button.rs @@ -1,4 +1,5 @@ -use crate::widget::{Element, Tree, Widget}; +use crate::widget::tree::{self, Tree}; +use crate::widget::{Element, Widget}; use iced_native::event::{self, Event}; use iced_native::layout; @@ -10,8 +11,6 @@ use iced_native::{ }; use iced_style::button::StyleSheet; -use std::any::Any; - pub use button::State; pub struct Button<'a, Message, Renderer> { @@ -77,20 +76,20 @@ where Message: 'static + Clone, Renderer: 'static + iced_native::Renderer, { - fn tag(&self) -> std::any::TypeId { - std::any::TypeId::of::() + fn tag(&self) -> tree::Tag { + tree::Tag::of::() } - fn state(&self) -> Box { - Box::new(State::new()) + fn state(&self) -> tree::State { + tree::State::new(State::new()) } - fn diff(&self, tree: &mut Tree) { - tree.diff_children(std::slice::from_ref(&self.content)) + fn children(&self) -> Vec { + vec![Tree::new(&self.content)] } - fn children_state(&self) -> Vec { - vec![Tree::new(&self.content)] + fn diff(&self, tree: &mut Tree) { + tree.diff_children(std::slice::from_ref(&self.content)) } fn width(&self) -> Length { -- cgit From 019af8ddbf96680ffcee2b3407819e90575760cb Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Wed, 16 Feb 2022 17:07:25 +0700 Subject: Add `overlay` support in `iced_pure` and port `PickList` :tada: --- pure/src/widget/button.rs | 14 ++++++++++++++ 1 file changed, 14 insertions(+) (limited to 'pure/src/widget/button.rs') diff --git a/pure/src/widget/button.rs b/pure/src/widget/button.rs index 55cbf8b4..f5e78933 100644 --- a/pure/src/widget/button.rs +++ b/pure/src/widget/button.rs @@ -1,3 +1,4 @@ +use crate::overlay; use crate::widget::tree::{self, Tree}; use crate::widget::{Element, Widget}; @@ -206,6 +207,19 @@ where self.on_press.is_some(), ) } + + fn overlay<'b>( + &'b mut self, + tree: &'b mut Tree, + layout: Layout<'_>, + renderer: &Renderer, + ) -> Option> { + self.content.as_widget_mut().overlay( + &mut tree.children[0], + layout.children().next().unwrap(), + renderer, + ) + } } impl<'a, Message, Renderer> Into> -- cgit From da45b6c1627935bff5334d213096c4e78972af46 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Thu, 17 Feb 2022 19:08:54 +0700 Subject: Implement `pure::Component` in `iced_lazy` --- pure/src/widget/button.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'pure/src/widget/button.rs') diff --git a/pure/src/widget/button.rs b/pure/src/widget/button.rs index f5e78933..2ed67a9c 100644 --- a/pure/src/widget/button.rs +++ b/pure/src/widget/button.rs @@ -209,12 +209,12 @@ where } fn overlay<'b>( - &'b mut self, + &'b self, tree: &'b mut Tree, layout: Layout<'_>, renderer: &Renderer, ) -> Option> { - self.content.as_widget_mut().overlay( + self.content.as_widget().overlay( &mut tree.children[0], layout.children().next().unwrap(), renderer, -- cgit From d7100fd2597da82d97eaf196d50573ea64f3f8ff Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Wed, 16 Mar 2022 17:37:19 +0700 Subject: Export widget modules in `iced_pure` ... and fix collisions with the new `helpers` --- pure/src/widget/button.rs | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) (limited to 'pure/src/widget/button.rs') diff --git a/pure/src/widget/button.rs b/pure/src/widget/button.rs index 4380b608..f99d3018 100644 --- a/pure/src/widget/button.rs +++ b/pure/src/widget/button.rs @@ -1,6 +1,6 @@ use crate::overlay; use crate::widget::tree::{self, Tree}; -use crate::widget::{Element, Widget}; +use crate::{Element, Widget}; use iced_native::event::{self, Event}; use iced_native::layout; @@ -10,9 +10,10 @@ use iced_native::widget::button; use iced_native::{ Clipboard, Layout, Length, Padding, Point, Rectangle, Shell, }; -use iced_style::button::StyleSheet; -pub use button::State; +pub use iced_style::button::{Style, StyleSheet}; + +use button::State; pub struct Button<'a, Message, Renderer> { content: Element<'a, Message, Renderer>, -- cgit