From 3c866c15aa1db944a2056f01449a2fbdda2f5abb Mon Sep 17 00:00:00 2001 From: Cory Forsstrom Date: Tue, 17 Jan 2023 10:01:17 -0800 Subject: Add group overlay element --- native/src/overlay.rs | 2 + native/src/overlay/group.rs | 165 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 167 insertions(+) create mode 100644 native/src/overlay/group.rs (limited to 'native') diff --git a/native/src/overlay.rs b/native/src/overlay.rs index 22f8b6ec..0b1e8daf 100644 --- a/native/src/overlay.rs +++ b/native/src/overlay.rs @@ -1,9 +1,11 @@ //! Display interactive elements on top of other widgets. mod element; +mod group; pub mod menu; pub use element::Element; +pub use group::Group; pub use menu::Menu; use crate::event::{self, Event}; diff --git a/native/src/overlay/group.rs b/native/src/overlay/group.rs new file mode 100644 index 00000000..f894f911 --- /dev/null +++ b/native/src/overlay/group.rs @@ -0,0 +1,165 @@ +use iced_core::{Point, Rectangle, Size}; + +use crate::event; +use crate::layout; +use crate::mouse; +use crate::overlay; +use crate::renderer; +use crate::widget; +use crate::{Clipboard, Event, Layout, Overlay, Shell}; + +/// An [`Overlay`] container that displays multiple overlay +/// [`overlay::Element`] children +#[allow(missing_debug_implementations)] +pub struct Group<'a, Message, Renderer> { + children: Vec>, +} + +impl<'a, Message, Renderer> Group<'a, Message, Renderer> +where + Renderer: 'a + crate::Renderer, + Message: 'a, +{ + /// Creates an empty [`Group`]. + pub fn new() -> Self { + Self::default() + } + + /// Creates a [`Group`] with the given elements. + pub fn with_children( + children: Vec>, + ) -> Self { + Group { children } + } + + /// Adds an [`overlay::Element`] to the [`Group`]. + pub fn push( + mut self, + child: impl Into>, + ) -> Self { + self.children.push(child.into()); + self + } + + /// Turns the [`Group`] into an overlay [`overlay::Element`] + pub fn overlay(self) -> overlay::Element<'a, Message, Renderer> { + overlay::Element::new(Point::ORIGIN, Box::new(self)) + } +} + +impl<'a, Message, Renderer> Default for Group<'a, Message, Renderer> +where + Renderer: 'a + crate::Renderer, + Message: 'a, +{ + fn default() -> Self { + Self::with_children(Vec::new()) + } +} + +impl<'a, Message, Renderer> Overlay + for Group<'a, Message, Renderer> +where + Renderer: crate::Renderer, +{ + fn layout( + &self, + renderer: &Renderer, + bounds: Size, + _position: Point, + ) -> layout::Node { + layout::Node::with_children( + bounds, + self.children + .iter() + .map(|child| child.layout(renderer, bounds)) + .collect(), + ) + } + + fn on_event( + &mut self, + event: Event, + layout: Layout<'_>, + cursor_position: Point, + renderer: &Renderer, + clipboard: &mut dyn Clipboard, + shell: &mut Shell<'_, Message>, + ) -> event::Status { + self.children + .iter_mut() + .zip(layout.children()) + .map(|(child, layout)| { + child.on_event( + event.clone(), + layout, + cursor_position, + renderer, + clipboard, + shell, + ) + }) + .fold(event::Status::Ignored, event::Status::merge) + } + + fn draw( + &self, + renderer: &mut Renderer, + theme: &::Theme, + style: &renderer::Style, + layout: Layout<'_>, + cursor_position: Point, + ) { + for (child, layout) in self.children.iter().zip(layout.children()) { + child.draw(renderer, theme, style, layout, cursor_position); + } + } + + fn mouse_interaction( + &self, + layout: Layout<'_>, + cursor_position: Point, + viewport: &Rectangle, + renderer: &Renderer, + ) -> mouse::Interaction { + self.children + .iter() + .zip(layout.children()) + .map(|(child, layout)| { + child.mouse_interaction( + layout, + cursor_position, + viewport, + renderer, + ) + }) + .max() + .unwrap_or_default() + } + + fn operate( + &mut self, + layout: Layout<'_>, + renderer: &Renderer, + operation: &mut dyn widget::Operation, + ) { + operation.container(None, &mut |operation| { + self.children.iter_mut().zip(layout.children()).for_each( + |(child, layout)| { + child.operate(layout, renderer, operation); + }, + ) + }); + } +} + +impl<'a, Message, Renderer> From> + for overlay::Element<'a, Message, Renderer> +where + Renderer: 'a + crate::Renderer, + Message: 'a, +{ + fn from(group: Group<'a, Message, Renderer>) -> Self { + group.overlay() + } +} -- cgit From b2a3a85acb2a0722e90c46b70d574f1d676da9d1 Mon Sep 17 00:00:00 2001 From: Cory Forsstrom Date: Tue, 17 Jan 2023 10:12:51 -0800 Subject: Use group overlay for containers w/ children --- native/src/overlay.rs | 8 +++++--- native/src/widget/pane_grid.rs | 13 ++++++++----- 2 files changed, 13 insertions(+), 8 deletions(-) (limited to 'native') diff --git a/native/src/overlay.rs b/native/src/overlay.rs index 0b1e8daf..e7394494 100644 --- a/native/src/overlay.rs +++ b/native/src/overlay.rs @@ -91,7 +91,7 @@ where } } -/// Obtains the first overlay [`Element`] found in the given children. +/// Returns a [`Group`] of overlay [`Element`] children. /// /// This method will generally only be used by advanced users that are /// implementing the [`Widget`](crate::Widget) trait. @@ -104,12 +104,14 @@ pub fn from_children<'a, Message, Renderer>( where Renderer: crate::Renderer, { - children + let children = children .iter_mut() .zip(&mut tree.children) .zip(layout.children()) .filter_map(|((child, state), layout)| { child.as_widget_mut().overlay(state, layout, renderer) }) - .next() + .collect::>(); + + (!children.is_empty()).then(|| Group::with_children(children).overlay()) } diff --git a/native/src/widget/pane_grid.rs b/native/src/widget/pane_grid.rs index 8dbd1825..eb04c0ba 100644 --- a/native/src/widget/pane_grid.rs +++ b/native/src/widget/pane_grid.rs @@ -35,7 +35,7 @@ pub use iced_style::pane_grid::{Line, StyleSheet}; use crate::event::{self, Event}; use crate::layout; use crate::mouse; -use crate::overlay; +use crate::overlay::{self, Group}; use crate::renderer; use crate::touch; use crate::widget; @@ -450,14 +450,17 @@ where layout: Layout<'_>, renderer: &Renderer, ) -> Option> { - self.contents + let children = self + .contents .iter_mut() .zip(&mut tree.children) .zip(layout.children()) - .filter_map(|(((_, pane), tree), layout)| { - pane.overlay(tree, layout, renderer) + .filter_map(|(((_, content), state), layout)| { + content.overlay(state, layout, renderer) }) - .next() + .collect::>(); + + (!children.is_empty()).then(|| Group::with_children(children).overlay()) } } -- cgit From 3ab679725526bd095cc1a160705312b16c408b92 Mon Sep 17 00:00:00 2001 From: Cory Forsstrom Date: Tue, 17 Jan 2023 11:12:10 -0800 Subject: New method to determine if overlay contains cursor This is needed for "container" overlay's such as `Group` which should only consider it's childrens layouts and not it's own when determining if the cursor is captured by the overlay. --- native/src/overlay.rs | 9 +++++++++ native/src/overlay/element.rs | 17 +++++++++++++++++ native/src/overlay/group.rs | 13 +++++++++++++ native/src/user_interface.rs | 11 +++++++++-- 4 files changed, 48 insertions(+), 2 deletions(-) (limited to 'native') diff --git a/native/src/overlay.rs b/native/src/overlay.rs index e7394494..16d8bb31 100644 --- a/native/src/overlay.rs +++ b/native/src/overlay.rs @@ -89,6 +89,15 @@ where ) -> mouse::Interaction { mouse::Interaction::Idle } + + /// Whether the [`Overlay`] contains the cursor + fn contains_cursor( + &self, + layout: Layout<'_>, + cursor_position: Point, + ) -> bool { + layout.bounds().contains(cursor_position) + } } /// Returns a [`Group`] of overlay [`Element`] children. diff --git a/native/src/overlay/element.rs b/native/src/overlay/element.rs index 41a8a597..125258c5 100644 --- a/native/src/overlay/element.rs +++ b/native/src/overlay/element.rs @@ -115,6 +115,15 @@ where ) { self.overlay.operate(layout, renderer, operation); } + + /// Whether the [`Overlay`] contains the cursor + pub fn contains_cursor( + &self, + layout: Layout<'_>, + cursor_position: Point, + ) -> bool { + self.overlay.contains_cursor(layout, cursor_position) + } } struct Map<'a, A, B, Renderer> { @@ -252,4 +261,12 @@ where self.content .draw(renderer, theme, style, layout, cursor_position) } + + fn contains_cursor( + &self, + layout: Layout<'_>, + cursor_position: Point, + ) -> bool { + self.content.contains_cursor(layout, cursor_position) + } } diff --git a/native/src/overlay/group.rs b/native/src/overlay/group.rs index f894f911..96d10c19 100644 --- a/native/src/overlay/group.rs +++ b/native/src/overlay/group.rs @@ -151,6 +151,19 @@ where ) }); } + + fn contains_cursor( + &self, + layout: Layout<'_>, + cursor_position: Point, + ) -> bool { + self.children + .iter() + .zip(layout.children()) + .any(|(child, layout)| { + child.contains_cursor(layout, cursor_position) + }) + } } impl<'a, Message, Renderer> From> diff --git a/native/src/user_interface.rs b/native/src/user_interface.rs index 29cc3472..8659caa3 100644 --- a/native/src/user_interface.rs +++ b/native/src/user_interface.rs @@ -261,7 +261,11 @@ where } } - let base_cursor = if layout.bounds().contains(cursor_position) { + let base_cursor = if manual_overlay + .as_ref() + .unwrap() + .contains_cursor(Layout::new(&layout), cursor_position) + { // TODO: Type-safe cursor availability Point::new(-1.0, -1.0) } else { @@ -504,7 +508,10 @@ where ); }); - if overlay_bounds.contains(cursor_position) { + if overlay.contains_cursor( + Layout::new(layout), + cursor_position, + ) { overlay_interaction } else { base_interaction -- cgit From d470467718ecad0f37599a811bef846846dbb2b9 Mon Sep 17 00:00:00 2001 From: Cory Forsstrom Date: Tue, 17 Jan 2023 17:10:58 -0800 Subject: Add toast example --- native/src/shell.rs | 5 +++++ 1 file changed, 5 insertions(+) (limited to 'native') diff --git a/native/src/shell.rs b/native/src/shell.rs index f1ddb48e..74a5c616 100644 --- a/native/src/shell.rs +++ b/native/src/shell.rs @@ -25,6 +25,11 @@ impl<'a, Message> Shell<'a, Message> { } } + /// Returns true if the [`Shell`] contains no published messages + pub fn is_empty(&self) -> bool { + self.messages.is_empty() + } + /// Publish the given `Message` for an application to process it. pub fn publish(&mut self, message: Message) { self.messages.push(message); -- cgit From be860508a9deed1f4583e045790eb9ddd74d07d5 Mon Sep 17 00:00:00 2001 From: Cory Forsstrom Date: Tue, 17 Jan 2023 17:20:53 -0800 Subject: Rename method to is_over --- native/src/overlay.rs | 8 ++------ native/src/overlay/element.rs | 18 +++++------------- native/src/overlay/group.rs | 10 ++-------- native/src/user_interface.rs | 8 +++----- 4 files changed, 12 insertions(+), 32 deletions(-) (limited to 'native') diff --git a/native/src/overlay.rs b/native/src/overlay.rs index 16d8bb31..1c3d0fb9 100644 --- a/native/src/overlay.rs +++ b/native/src/overlay.rs @@ -90,12 +90,8 @@ where mouse::Interaction::Idle } - /// Whether the [`Overlay`] contains the cursor - fn contains_cursor( - &self, - layout: Layout<'_>, - cursor_position: Point, - ) -> bool { + /// Returns true if the cursor is over the [`Overlay`] + fn is_over(&self, layout: Layout<'_>, cursor_position: Point) -> bool { layout.bounds().contains(cursor_position) } } diff --git a/native/src/overlay/element.rs b/native/src/overlay/element.rs index 125258c5..edeb7dbf 100644 --- a/native/src/overlay/element.rs +++ b/native/src/overlay/element.rs @@ -116,13 +116,9 @@ where self.overlay.operate(layout, renderer, operation); } - /// Whether the [`Overlay`] contains the cursor - pub fn contains_cursor( - &self, - layout: Layout<'_>, - cursor_position: Point, - ) -> bool { - self.overlay.contains_cursor(layout, cursor_position) + /// Returns true if the cursor is over the [`Element`] + pub fn is_over(&self, layout: Layout<'_>, cursor_position: Point) -> bool { + self.overlay.is_over(layout, cursor_position) } } @@ -262,11 +258,7 @@ where .draw(renderer, theme, style, layout, cursor_position) } - fn contains_cursor( - &self, - layout: Layout<'_>, - cursor_position: Point, - ) -> bool { - self.content.contains_cursor(layout, cursor_position) + fn is_over(&self, layout: Layout<'_>, cursor_position: Point) -> bool { + self.content.is_over(layout, cursor_position) } } diff --git a/native/src/overlay/group.rs b/native/src/overlay/group.rs index 96d10c19..fa3c7396 100644 --- a/native/src/overlay/group.rs +++ b/native/src/overlay/group.rs @@ -152,17 +152,11 @@ where }); } - fn contains_cursor( - &self, - layout: Layout<'_>, - cursor_position: Point, - ) -> bool { + fn is_over(&self, layout: Layout<'_>, cursor_position: Point) -> bool { self.children .iter() .zip(layout.children()) - .any(|(child, layout)| { - child.contains_cursor(layout, cursor_position) - }) + .any(|(child, layout)| child.is_over(layout, cursor_position)) } } diff --git a/native/src/user_interface.rs b/native/src/user_interface.rs index 8659caa3..0def730c 100644 --- a/native/src/user_interface.rs +++ b/native/src/user_interface.rs @@ -264,7 +264,7 @@ where let base_cursor = if manual_overlay .as_ref() .unwrap() - .contains_cursor(Layout::new(&layout), cursor_position) + .is_over(Layout::new(&layout), cursor_position) { // TODO: Type-safe cursor availability Point::new(-1.0, -1.0) @@ -508,10 +508,8 @@ where ); }); - if overlay.contains_cursor( - Layout::new(layout), - cursor_position, - ) { + if overlay.is_over(Layout::new(layout), cursor_position) + { overlay_interaction } else { base_interaction -- cgit From 01c484245be54c1aeb6605659fb0f222856c28da Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Tue, 24 Jan 2023 01:59:34 +0100 Subject: Fix some minor documentation inconsistencies --- native/src/overlay.rs | 5 ++++- native/src/overlay/element.rs | 2 +- native/src/overlay/group.rs | 6 +++--- 3 files changed, 8 insertions(+), 5 deletions(-) (limited to 'native') diff --git a/native/src/overlay.rs b/native/src/overlay.rs index 1c3d0fb9..6cada416 100644 --- a/native/src/overlay.rs +++ b/native/src/overlay.rs @@ -90,7 +90,10 @@ where mouse::Interaction::Idle } - /// Returns true if the cursor is over the [`Overlay`] + /// Returns true if the cursor is over the [`Overlay`]. + /// + /// By default, it returns true if the bounds of the `layout` contain + /// the `cursor_position`. fn is_over(&self, layout: Layout<'_>, cursor_position: Point) -> bool { layout.bounds().contains(cursor_position) } diff --git a/native/src/overlay/element.rs b/native/src/overlay/element.rs index edeb7dbf..bdf7766e 100644 --- a/native/src/overlay/element.rs +++ b/native/src/overlay/element.rs @@ -116,7 +116,7 @@ where self.overlay.operate(layout, renderer, operation); } - /// Returns true if the cursor is over the [`Element`] + /// Returns true if the cursor is over the [`Element`]. pub fn is_over(&self, layout: Layout<'_>, cursor_position: Point) -> bool { self.overlay.is_over(layout, cursor_position) } diff --git a/native/src/overlay/group.rs b/native/src/overlay/group.rs index fa3c7396..5c9cf809 100644 --- a/native/src/overlay/group.rs +++ b/native/src/overlay/group.rs @@ -8,8 +8,8 @@ use crate::renderer; use crate::widget; use crate::{Clipboard, Event, Layout, Overlay, Shell}; -/// An [`Overlay`] container that displays multiple overlay -/// [`overlay::Element`] children +/// An [`Overlay`] container that displays multiple overlay [`overlay::Element`] +/// children. #[allow(missing_debug_implementations)] pub struct Group<'a, Message, Renderer> { children: Vec>, @@ -41,7 +41,7 @@ where self } - /// Turns the [`Group`] into an overlay [`overlay::Element`] + /// Turns the [`Group`] into an overlay [`overlay::Element`]. pub fn overlay(self) -> overlay::Element<'a, Message, Renderer> { overlay::Element::new(Point::ORIGIN, Box::new(self)) } -- cgit