From c9b905ee8f912041a846ec9a844cd4f0a630b5da Mon Sep 17 00:00:00 2001 From: brianch Date: Fri, 2 Feb 2024 18:38:36 -0300 Subject: [PATCH] Update overlay code to compile with iced master The change in iced: https://github.com/iced-rs/iced/pull/2226 --- src/native/card.rs | 5 ++-- src/native/color_picker.rs | 5 ++-- src/native/context_menu.rs | 15 ++++++---- src/native/date_picker.rs | 5 ++-- src/native/floating_element.rs | 7 +++-- src/native/grid/widget.rs | 5 ++-- src/native/modal.rs | 8 ++---- src/native/overlay/color_picker.rs | 10 +++---- src/native/overlay/context_menu.rs | 14 ++++++---- src/native/overlay/date_picker.rs | 8 ++---- src/native/overlay/floating_element.rs | 38 ++++++++++++++------------ src/native/overlay/modal.rs | 6 ++-- src/native/overlay/time_picker.rs | 6 ++-- src/native/split.rs | 7 +++-- src/native/tabs.rs | 5 ++-- src/native/time_picker.rs | 5 ++-- src/native/wrap.rs | 5 ++-- 17 files changed, 81 insertions(+), 73 deletions(-) diff --git a/src/native/card.rs b/src/native/card.rs index 8eb1aa60..229611f3 100644 --- a/src/native/card.rs +++ b/src/native/card.rs @@ -14,7 +14,7 @@ use iced_widget::{ renderer, touch, widget::{Operation, Tree}, Alignment, Border, Clipboard, Color, Element, Event, Layout, Length, Padding, Pixels, - Point, Rectangle, Shadow, Shell, Size, Widget, + Point, Rectangle, Shadow, Shell, Size, Vector, Widget }, text::LineHeight, }; @@ -583,6 +583,7 @@ where tree: &'b mut Tree, layout: Layout<'_>, renderer: &Renderer, + translation: Vector, ) -> Option> { let mut children = vec![&mut self.head, &mut self.body]; if let Some(foot) = &mut self.foot { @@ -594,7 +595,7 @@ where .zip(layout.children()) .filter_map(|((child, state), layout)| { layout.children().next().and_then(|child_layout| { - child.as_widget_mut().overlay(state, child_layout, renderer) + child.as_widget_mut().overlay(state, child_layout, renderer, translation) }) }) .collect::>(); diff --git a/src/native/color_picker.rs b/src/native/color_picker.rs index 44915e33..b357ae5f 100644 --- a/src/native/color_picker.rs +++ b/src/native/color_picker.rs @@ -17,7 +17,7 @@ use iced_widget::{ self, tree::{self, Tag, Tree}, }, - Clipboard, Color, Element, Event, Layout, Length, Point, Rectangle, Shell, Widget, + Clipboard, Color, Element, Event, Layout, Length, Point, Rectangle, Shell, Vector, Widget }, renderer::Renderer, }; @@ -242,6 +242,7 @@ where state: &'b mut Tree, layout: Layout<'_>, renderer: &Renderer, + translation: Vector, ) -> Option> { let picker_state: &mut State = state.state.downcast_mut(); @@ -249,7 +250,7 @@ where return self .underlay .as_widget_mut() - .overlay(&mut state.children[0], layout, renderer); + .overlay(&mut state.children[0], layout, renderer, translation); } let bounds = layout.bounds(); diff --git a/src/native/context_menu.rs b/src/native/context_menu.rs index b91847c9..8d64183f 100644 --- a/src/native/context_menu.rs +++ b/src/native/context_menu.rs @@ -6,7 +6,7 @@ use iced_widget::core::{ mouse::{self, Button, Cursor}, overlay, renderer, widget::{tree, Operation, Tree}, - Clipboard, Element, Event, Layout, Length, Point, Rectangle, Shell, Widget, + Clipboard, Element, Event, Layout, Length, Point, Rectangle, Shell, Vector, Widget }; use crate::native::overlay::ContextMenuOverlay; @@ -218,6 +218,7 @@ where state: &'b mut Tree, layout: Layout<'_>, renderer: &Renderer, + translation: Vector, ) -> Option> { let s: &mut State = state.state.downcast_mut(); @@ -225,16 +226,20 @@ where return self .underlay .as_widget_mut() - .overlay(&mut state.children[0], layout, renderer); + .overlay(&mut state.children[0], layout, renderer, translation); } let position = s.cursor_position; let content = (self.overlay)(); content.as_widget().diff(&mut state.children[1]); - Some( - ContextMenuOverlay::new(&mut state.children[1], content, self.style.clone(), s) - .overlay(position), + ContextMenuOverlay::new( + position + translation, + &mut state.children[1], + content, + self.style.clone(), + s + ).overlay(), ) } } diff --git a/src/native/date_picker.rs b/src/native/date_picker.rs index dcb84dd9..dd4af053 100644 --- a/src/native/date_picker.rs +++ b/src/native/date_picker.rs @@ -16,7 +16,7 @@ use iced_widget::{ self, tree::{Tag, Tree}, }, - Clipboard, Element, Event, Layout, Length, Point, Rectangle, Shell, Widget, + Clipboard, Element, Event, Layout, Length, Point, Rectangle, Shell, Vector, Widget }, renderer::Renderer, text, @@ -249,6 +249,7 @@ where state: &'b mut Tree, layout: Layout<'_>, renderer: &Renderer, + translation: Vector, ) -> Option> { let picker_state: &mut State = state.state.downcast_mut(); @@ -256,7 +257,7 @@ where return self .underlay .as_widget_mut() - .overlay(&mut state.children[0], layout, renderer); + .overlay(&mut state.children[0], layout, renderer, translation); } let bounds = layout.bounds(); diff --git a/src/native/floating_element.rs b/src/native/floating_element.rs index 180e0e5e..89cc3851 100644 --- a/src/native/floating_element.rs +++ b/src/native/floating_element.rs @@ -10,7 +10,7 @@ use iced_widget::core::{ mouse::{self, Cursor}, overlay, renderer, widget::{Operation, Tree}, - Clipboard, Element, Event, Layout, Length, Rectangle, Shell, Widget, + Clipboard, Element, Event, Layout, Length, Rectangle, Shell, Vector, Widget }; pub mod anchor; @@ -211,20 +211,21 @@ where state: &'b mut Tree, layout: Layout<'_>, renderer: &Renderer, + translation: Vector, ) -> Option> { if self.hidden { return self .underlay .as_widget_mut() - .overlay(&mut state.children[0], layout, renderer); + .overlay(&mut state.children[0], layout, renderer, translation); } if state.children.len() == 2 { let bounds = layout.bounds(); Some(overlay::Element::new( - bounds.position(), Box::new(FloatingElementOverlay::new( + layout.position() + translation, &mut state.children[1], &mut self.element, &self.anchor, diff --git a/src/native/grid/widget.rs b/src/native/grid/widget.rs index ccccc9b9..71382fb3 100644 --- a/src/native/grid/widget.rs +++ b/src/native/grid/widget.rs @@ -5,7 +5,7 @@ use iced_widget::core::{ overlay::Group, renderer::Style, widget::{Operation, Tree}, - Clipboard, Element, Event, Layout, Length, Rectangle, Shell, Size, Widget, + Clipboard, Element, Event, Layout, Length, Rectangle, Shell, Size, Vector, Widget }; use super::{layout::layout, types::Grid}; @@ -159,13 +159,14 @@ where tree: &'b mut Tree, layout: Layout<'_>, renderer: &Renderer, + translation: Vector, ) -> Option> { let children = self .elements_iter_mut() .zip(&mut tree.children) .zip(layout.children()) .filter_map(|((child, state), layout)| { - child.as_widget_mut().overlay(state, layout, renderer) + child.as_widget_mut().overlay(state, layout, renderer, translation) }) .collect::>(); diff --git a/src/native/modal.rs b/src/native/modal.rs index 5f59222e..b235a1c4 100644 --- a/src/native/modal.rs +++ b/src/native/modal.rs @@ -10,7 +10,7 @@ use iced_widget::core::{ mouse::{self, Cursor}, overlay, renderer, widget::{Operation, Tree}, - Clipboard, Element, Event, Layout, Length, Point, Rectangle, Shell, Widget, + Clipboard, Element, Event, Layout, Length, Rectangle, Shell, Vector, Widget }; pub use crate::style::modal::StyleSheet; @@ -232,14 +232,12 @@ where state: &'b mut Tree, layout: Layout<'_>, renderer: &Renderer, + translation: Vector, ) -> Option> { if let Some(overlay) = &mut self.overlay { - let bounds = layout.bounds(); - let position = Point::new(bounds.x, bounds.y); overlay.as_widget().diff(&mut state.children[1]); Some(overlay::Element::new( - position, Box::new(ModalOverlay::new( &mut state.children[1], overlay, @@ -253,7 +251,7 @@ where } else { self.underlay .as_widget_mut() - .overlay(&mut state.children[0], layout, renderer) + .overlay(&mut state.children[0], layout, renderer, translation) } } diff --git a/src/native/overlay/color_picker.rs b/src/native/overlay/color_picker.rs index b451f550..8f213117 100644 --- a/src/native/overlay/color_picker.rs +++ b/src/native/overlay/color_picker.rs @@ -118,7 +118,7 @@ where /// Turn this [`ColorPickerOverlay`] into an overlay [`Element`](overlay::Element). #[must_use] pub fn overlay(self) -> overlay::Element<'a, Message, Theme, Renderer> { - overlay::Element::new(self.position, Box::new(self)) + overlay::Element::new(Box::new(self)) } /// The event handling for the HSV color area. @@ -556,8 +556,6 @@ where &mut self, renderer: &Renderer, bounds: Size, - position: Point, - _translation: Vector, ) -> Node { let (max_width, max_height) = if bounds.width > bounds.height { (600.0, 300.0) @@ -598,10 +596,10 @@ where .bounds(); // ----------- Block 1 ---------------------- - let block1_node = block1_layout(self, renderer, block1_bounds, position); + let block1_node = block1_layout(self, renderer, block1_bounds, self.position); // ----------- Block 2 ---------------------- - let block2_node = block2_layout(self, renderer, block2_bounds, position); + let block2_node = block2_layout(self, renderer, block2_bounds, self.position); let (width, height) = if bounds.width > bounds.height { ( @@ -618,7 +616,7 @@ where let mut node = Node::with_children(Size::new(width, height), vec![block1_node, block2_node]); - node.center_and_bounce(position, bounds); + node.center_and_bounce(self.position, bounds); node } diff --git a/src/native/overlay/context_menu.rs b/src/native/overlay/context_menu.rs index 4f064ce7..67420383 100644 --- a/src/native/overlay/context_menu.rs +++ b/src/native/overlay/context_menu.rs @@ -13,7 +13,7 @@ use iced_widget::core::{ overlay, renderer, touch, widget::tree::Tree, window, Border, Clipboard, Color, Element, Event, Layout, Point, Rectangle, Shadow, Shell, - Size, Vector, + Size, }; /// The overlay of the [`ContextMenu`](crate::native::ContextMenu). @@ -28,6 +28,8 @@ pub struct ContextMenuOverlay< Renderer: 'a + core::Renderer, Theme: StyleSheet, { + // The position of the element + position: Point, /// The state of the [`ContextMenuOverlay`]. tree: &'a mut Tree, /// The content of the [`ContextMenuOverlay`]. @@ -46,6 +48,7 @@ where { /// Creates a new [`ContextMenuOverlay`]. pub(crate) fn new( + position: Point, tree: &'a mut Tree, content: C, style: ::Style, @@ -55,6 +58,7 @@ where C: Into>, { ContextMenuOverlay { + position, tree, content: content.into(), style, @@ -63,8 +67,8 @@ where } /// Turn this [`ContextMenuOverlay`] into an overlay [`Element`](overlay::Element). - pub fn overlay(self, position: Point) -> overlay::Element<'a, Message, Theme, Renderer> { - overlay::Element::new(position, Box::new(self)) + pub fn overlay(self) -> overlay::Element<'a, Message, Theme, Renderer> { + overlay::Element::new(Box::new(self)) } } @@ -79,8 +83,6 @@ where &mut self, renderer: &Renderer, bounds: Size, - position: Point, - _translation: Vector, ) -> Node { let limits = Limits::new(Size::ZERO, bounds); let max_size = limits.max(); @@ -91,7 +93,7 @@ where .layout(self.tree, renderer, &limits); // Try to stay inside borders - let mut position = position; + let mut position = self.position; if position.x + content.size().width > bounds.width { position.x = f32::max(0.0, position.x - content.size().width); } diff --git a/src/native/overlay/date_picker.rs b/src/native/overlay/date_picker.rs index c7f20333..8013dbbb 100644 --- a/src/native/overlay/date_picker.rs +++ b/src/native/overlay/date_picker.rs @@ -29,7 +29,7 @@ use iced_widget::{ touch, widget::tree::Tree, Alignment, Border, Clipboard, Color, Element, Event, Layout, Length, Overlay, Padding, - Point, Rectangle, Renderer as _, Shadow, Shell, Size, Vector, Widget, + Point, Rectangle, Renderer as _, Shadow, Shell, Size, Widget, }, renderer::Renderer, text, Button, Column, Container, Row, Text, @@ -113,7 +113,7 @@ where /// Turn this [`DatePickerOverlay`] into an overlay [`Element`](overlay::Element). #[must_use] pub fn overlay(self) -> overlay::Element<'a, Message, Theme, Renderer> { - overlay::Element::new(self.position, Box::new(self)) + overlay::Element::new(Box::new(self)) } /// String representation of the current year. @@ -373,8 +373,6 @@ where &mut self, renderer: &Renderer, bounds: Size, - position: Point, - _translation: Vector, ) -> Node { let limits = Limits::new(Size::ZERO, bounds) .shrink(Padding::from(PADDING)) @@ -528,7 +526,7 @@ where ), vec![col, cancel_button, submit_button], ); - node.center_and_bounce(position, bounds); + node.center_and_bounce(self.position, bounds); node } diff --git a/src/native/overlay/floating_element.rs b/src/native/overlay/floating_element.rs index 09b21a91..6d31a049 100644 --- a/src/native/overlay/floating_element.rs +++ b/src/native/overlay/floating_element.rs @@ -22,6 +22,8 @@ pub struct FloatingElementOverlay< Theme = iced_widget::Theme, Renderer = iced_widget::Renderer, > { + // The position of the element + position: Point, /// The state of the element. state: &'b mut Tree, /// The floating element @@ -41,6 +43,7 @@ where /// Creates a new [`FloatingElementOverlay`] containing the given /// [`Element`](iced_widget::core::Element). pub fn new( + position: Point, state: &'b mut Tree, element: &'b mut Element<'a, Message, Theme, Renderer>, anchor: &'b Anchor, @@ -48,6 +51,7 @@ where underlay_bounds: Rectangle, ) -> Self { FloatingElementOverlay { + position, state, element, anchor, @@ -66,8 +70,6 @@ where &mut self, renderer: &Renderer, _bounds: Size, - position: Point, - _translation: Vector, ) -> layout::Node { // Constrain overlay to fit inside the underlay's bounds let limits = layout::Limits::new(Size::ZERO, self.underlay_bounds.size()) @@ -79,37 +81,37 @@ where .layout(self.state, renderer, &limits); let position = match self.anchor { - Anchor::NorthWest => Point::new(position.x + self.offset.x, position.y + self.offset.y), + Anchor::NorthWest => Point::new(self.position.x + self.offset.x, self.position.y + self.offset.y), Anchor::NorthEast => Point::new( - position.x + self.underlay_bounds.width - node.bounds().width - self.offset.x, - position.y + self.offset.y, + self.position.x + self.underlay_bounds.width - node.bounds().width - self.offset.x, + self.position.y + self.offset.y, ), Anchor::SouthWest => Point::new( - position.x + self.offset.x, - position.y + self.underlay_bounds.height - node.bounds().height - self.offset.y, + self.position.x + self.offset.x, + self.position.y + self.underlay_bounds.height - node.bounds().height - self.offset.y, ), Anchor::SouthEast => Point::new( - position.x + self.underlay_bounds.width - node.bounds().width - self.offset.x, - position.y + self.underlay_bounds.height - node.bounds().height - self.offset.y, + self.position.x + self.underlay_bounds.width - node.bounds().width - self.offset.x, + self.position.y + self.underlay_bounds.height - node.bounds().height - self.offset.y, ), Anchor::North => Point::new( - position.x + self.underlay_bounds.width / 2.0 - node.bounds().width / 2.0 + self.position.x + self.underlay_bounds.width / 2.0 - node.bounds().width / 2.0 + self.offset.x, - position.y + self.offset.y, + self.position.y + self.offset.y, ), Anchor::East => Point::new( - position.x + self.underlay_bounds.width - node.bounds().width - self.offset.x, - position.y + self.underlay_bounds.height / 2.0 - node.bounds().height / 2.0 + self.position.x + self.underlay_bounds.width - node.bounds().width - self.offset.x, + self.position.y + self.underlay_bounds.height / 2.0 - node.bounds().height / 2.0 + self.offset.y, ), Anchor::South => Point::new( - position.x + self.underlay_bounds.width / 2.0 - node.bounds().width / 2.0 + self.position.x + self.underlay_bounds.width / 2.0 - node.bounds().width / 2.0 + self.offset.x, - position.y + self.underlay_bounds.height - node.bounds().height - self.offset.y, + self.position.y + self.underlay_bounds.height - node.bounds().height - self.offset.y, ), Anchor::West => Point::new( - position.x + self.offset.x, - position.y + self.underlay_bounds.height / 2.0 - node.bounds().height / 2.0 + self.position.x + self.offset.x, + self.position.y + self.underlay_bounds.height / 2.0 - node.bounds().height / 2.0 + self.offset.y, ), }; @@ -171,6 +173,6 @@ where ) -> Option> { self.element .as_widget_mut() - .overlay(self.state, layout, renderer) + .overlay(self.state, layout, renderer, Vector::ZERO) } } diff --git a/src/native/overlay/modal.rs b/src/native/overlay/modal.rs index dc078e9e..9694530b 100644 --- a/src/native/overlay/modal.rs +++ b/src/native/overlay/modal.rs @@ -6,8 +6,8 @@ use iced_widget::core::{ mouse::{self, Cursor}, renderer, touch, widget::Tree, - Alignment, Border, Clipboard, Color, Element, Event, Layout, Overlay, Point, Rectangle, Shadow, - Shell, Size, Vector, + Alignment, Border, Clipboard, Color, Element, Event, Layout, Overlay, Rectangle, Shadow, + Shell, Size, }; use crate::style::modal::StyleSheet; @@ -73,8 +73,6 @@ where &mut self, renderer: &Renderer, bounds: Size, - _position: Point, - _translation: Vector, ) -> layout::Node { let limits = layout::Limits::new(Size::ZERO, bounds); let mut content = self diff --git a/src/native/overlay/time_picker.rs b/src/native/overlay/time_picker.rs index c73f328b..27011580 100644 --- a/src/native/overlay/time_picker.rs +++ b/src/native/overlay/time_picker.rs @@ -121,7 +121,7 @@ where /// Turn this [`TimePickerOverlay`] into an overlay [`Element`](overlay::Element). #[must_use] pub fn overlay(self) -> overlay::Element<'a, Message, Theme, Renderer> { - overlay::Element::new(self.position, Box::new(self)) + overlay::Element::new(Box::new(self)) } /// The event handling for the clock. @@ -513,8 +513,6 @@ where &mut self, renderer: &Renderer, bounds: Size, - position: Point, - _translation: Vector, ) -> Node { let limits = Limits::new(Size::ZERO, bounds) .shrink(Padding::from(PADDING)) @@ -603,7 +601,7 @@ where vec![clock, digital_clock, cancel_button, submit_button], ); - node.center_and_bounce(position, bounds); + node.center_and_bounce(self.position, bounds); node } diff --git a/src/native/split.rs b/src/native/split.rs index 0ede7cd8..6466fd90 100644 --- a/src/native/split.rs +++ b/src/native/split.rs @@ -14,7 +14,7 @@ use iced_widget::{ Operation, Tree, }, Border, Clipboard, Color, Element, Event, Layout, Length, Padding, Point, Rectangle, - Shadow, Shell, Size, Widget, + Shadow, Shell, Size, Vector, Widget }, Container, Row, }; @@ -498,6 +498,7 @@ where state: &'b mut Tree, layout: Layout<'_>, renderer: &Renderer, + translation: Vector, ) -> Option> { let mut children = layout.children(); let first_layout = children.next()?; @@ -513,11 +514,11 @@ where first .as_widget_mut() - .overlay(&mut first_state[0], first_layout, renderer) + .overlay(&mut first_state[0], first_layout, renderer, translation) .or_else(|| { second .as_widget_mut() - .overlay(&mut second_state[0], second_layout, renderer) + .overlay(&mut second_state[0], second_layout, renderer, translation) }) } } diff --git a/src/native/tabs.rs b/src/native/tabs.rs index b69ac6fa..52ff0d7e 100644 --- a/src/native/tabs.rs +++ b/src/native/tabs.rs @@ -20,7 +20,7 @@ use iced_widget::{ tree::{State, Tag}, Operation, Tree, }, - Clipboard, Element, Event, Layout, Length, Point, Rectangle, Shell, Size, Widget, + Clipboard, Element, Event, Layout, Length, Point, Rectangle, Shell, Size, Vector, Widget }, runtime::Font, text, Row, @@ -558,6 +558,7 @@ where state: &'b mut Tree, layout: Layout<'_>, renderer: &Renderer, + translation: Vector, ) -> Option> { let layout = match self.tab_bar_position { TabBarPosition::Top => layout.children().nth(1), @@ -569,7 +570,7 @@ where self.tabs .get_mut(idx) .map(Element::as_widget_mut) - .and_then(|w| w.overlay(&mut state.children[1].children[idx], layout, renderer)) + .and_then(|w| w.overlay(&mut state.children[1].children[idx], layout, renderer, translation)) }) } diff --git a/src/native/time_picker.rs b/src/native/time_picker.rs index eb25b2a1..38b20396 100644 --- a/src/native/time_picker.rs +++ b/src/native/time_picker.rs @@ -13,7 +13,7 @@ use iced_widget::{ mouse::{self, Cursor}, overlay, renderer, widget::tree::{self, Tag, Tree}, - Clipboard, Element, Event, Layout, Length, Point, Rectangle, Shell, Widget, + Clipboard, Element, Event, Layout, Length, Point, Rectangle, Shell, Vector, Widget }, renderer::Renderer, text, @@ -263,6 +263,7 @@ where state: &'b mut Tree, layout: Layout<'_>, renderer: &Renderer, + translation: Vector, ) -> Option> { let picker_state: &mut State = state.state.downcast_mut(); @@ -270,7 +271,7 @@ where return self .underlay .as_widget_mut() - .overlay(&mut state.children[0], layout, renderer); + .overlay(&mut state.children[0], layout, renderer, translation); } let bounds = layout.bounds(); diff --git a/src/native/wrap.rs b/src/native/wrap.rs index e82eb928..eceddd67 100644 --- a/src/native/wrap.rs +++ b/src/native/wrap.rs @@ -8,7 +8,7 @@ use iced_widget::core::{ renderer, widget::{Operation, Tree}, Alignment, Clipboard, Element, Event, Layout, Length, Padding, Point, Rectangle, Shell, Size, - Widget, + Vector, Widget }; use std::marker::PhantomData; @@ -217,13 +217,14 @@ where state: &'b mut Tree, layout: Layout<'_>, renderer: &Renderer, + translation: Vector, ) -> Option> { self.elements .iter_mut() .zip(&mut state.children) .zip(layout.children()) .find_map(|((child, state), layout)| { - child.as_widget_mut().overlay(state, layout, renderer) + child.as_widget_mut().overlay(state, layout, renderer, translation) }) }