From a3525ef56e65b8bd198e5f1e837499673c3f6078 Mon Sep 17 00:00:00 2001 From: Ashley Wulber Date: Thu, 20 Mar 2025 10:10:02 -0400 Subject: [PATCH] refactor: track virtual offset in the layout --- iced | 2 +- src/widget/autosize.rs | 30 ++++++++++---- src/widget/button/widget.rs | 34 ++++++++++++---- src/widget/dropdown/widget.rs | 4 +- src/widget/flex_row/widget.rs | 41 ++++++++++++------- src/widget/grid/widget.rs | 59 +++++++++++++++++++--------- src/widget/id_container.rs | 30 ++++++++++---- src/widget/responsive_container.rs | 30 ++++++++++---- src/widget/wayland/tooltip/widget.rs | 48 +++++++++++++++------- 9 files changed, 200 insertions(+), 78 deletions(-) diff --git a/iced b/iced index 69da0aba..07f66698 160000 --- a/iced +++ b/iced @@ -1 +1 @@ -Subproject commit 69da0abad9b708f4191ac5c7a9fdc0b733c4ad2d +Subproject commit 07f666981d2d90cf7978f3fd90c0a9dfe22e1f0e diff --git a/src/widget/autosize.rs b/src/widget/autosize.rs index 6c15750d..3f608351 100644 --- a/src/widget/autosize.rs +++ b/src/widget/autosize.rs @@ -140,7 +140,11 @@ where operation.container(Some(&self.id), layout.bounds(), &mut |operation| { self.content.as_widget().operate( &mut tree.children[0], - layout.children().next().unwrap(), + layout + .children() + .next() + .unwrap() + .with_virtual_offset(layout.virtual_offset()), renderer, operation, ); @@ -171,7 +175,11 @@ where self.content.as_widget_mut().on_event( &mut tree.children[0], event.clone(), - layout.children().next().unwrap(), + layout + .children() + .next() + .unwrap() + .with_virtual_offset(layout.virtual_offset()), cursor_position, renderer, clipboard, @@ -191,7 +199,7 @@ where let content_layout = layout.children().next().unwrap(); self.content.as_widget().mouse_interaction( &tree.children[0], - content_layout, + content_layout.with_virtual_offset(layout.virtual_offset()), cursor_position, viewport, renderer, @@ -214,7 +222,7 @@ where renderer, theme, renderer_style, - content_layout, + content_layout.with_virtual_offset(layout.virtual_offset()), cursor_position, viewport, ); @@ -229,7 +237,11 @@ where ) -> Option> { self.content.as_widget_mut().overlay( &mut tree.children[0], - layout.children().next().unwrap(), + layout + .children() + .next() + .unwrap() + .with_virtual_offset(layout.virtual_offset()), renderer, translation, ) @@ -245,7 +257,7 @@ where let content_layout = layout.children().next().unwrap(); self.content.as_widget().drag_destinations( &state.children[0], - content_layout, + content_layout.with_virtual_offset(layout.virtual_offset()), renderer, dnd_rectangles, ); @@ -269,7 +281,11 @@ where ) -> iced_accessibility::A11yTree { let c_layout = layout.children().next().unwrap(); let c_state = &state.children[0]; - self.content.as_widget().a11y_nodes(c_layout, c_state, p) + self.content.as_widget().a11y_nodes( + c_layout.with_virtual_offset(layout.virtual_offset()), + c_state, + p, + ) } } diff --git a/src/widget/button/widget.rs b/src/widget/button/widget.rs index c3e51495..3b6d5577 100644 --- a/src/widget/button/widget.rs +++ b/src/widget/button/widget.rs @@ -271,7 +271,11 @@ impl<'a, Message: 'a + Clone> Widget operation.container(None, layout.bounds(), &mut |operation| { self.content.as_widget().operate( &mut tree.children[0], - layout.children().next().unwrap(), + layout + .children() + .next() + .unwrap() + .with_virtual_offset(layout.virtual_offset()), renderer, operation, ); @@ -315,7 +319,11 @@ impl<'a, Message: 'a + Clone> Widget if self.content.as_widget_mut().on_event( &mut tree.children[0], event.clone(), - layout.children().next().unwrap(), + layout + .children() + .next() + .unwrap() + .with_virtual_offset(layout.virtual_offset()), cursor, renderer, clipboard, @@ -416,7 +424,7 @@ impl<'a, Message: 'a + Clone> Widget text_color, scale_factor: renderer_style.scale_factor, }, - content_layout, + content_layout.with_virtual_offset(layout.virtual_offset()), cursor, &viewport.intersection(&bounds).unwrap_or_default(), ); @@ -533,7 +541,11 @@ impl<'a, Message: 'a + Clone> Widget _viewport: &Rectangle, _renderer: &crate::Renderer, ) -> mouse::Interaction { - mouse_interaction(layout, cursor, self.on_press.is_some()) + mouse_interaction( + layout.with_virtual_offset(layout.virtual_offset()), + cursor, + self.on_press.is_some(), + ) } fn overlay<'b>( @@ -548,7 +560,11 @@ impl<'a, Message: 'a + Clone> Widget translation.y += position.y; self.content.as_widget_mut().overlay( &mut tree.children[0], - layout.children().next().unwrap(), + layout + .children() + .next() + .unwrap() + .with_virtual_offset(layout.virtual_offset()), renderer, translation, ) @@ -614,9 +630,11 @@ impl<'a, Message: 'a + Clone> Widget node.set_default_action_verb(DefaultActionVerb::Click); if let Some(child_tree) = child_tree.map(|child_tree| { - self.content - .as_widget() - .a11y_nodes(child_layout, child_tree, p) + self.content.as_widget().a11y_nodes( + child_layout.with_virtual_offset(layout.virtual_offset()), + child_tree, + p, + ) }) { A11yTree::node_with_child_tree(A11yNode::new(node, self.id.clone()), child_tree) } else { diff --git a/src/widget/dropdown/widget.rs b/src/widget/dropdown/widget.rs index f3388976..f6cecea7 100644 --- a/src/widget/dropdown/widget.rs +++ b/src/widget/dropdown/widget.rs @@ -535,6 +535,8 @@ pub fn update< let state = state.clone(); let on_close = surface::action::destroy_popup(id); let on_surface_action_clone = on_surface_action.clone(); + let translation = layout.virtual_offset(); + dbg!(translation); let get_popup_action = surface::action::simple_popup::< AppMessage, Box< @@ -556,7 +558,7 @@ pub fn update< anchor: cctk::wayland_protocols::xdg::shell::client::xdg_positioner::Anchor::BottomLeft, gravity: cctk::wayland_protocols::xdg::shell::client::xdg_positioner::Gravity::BottomRight, reactive: true, - offset: (-padding.left as i32, 0), + offset: ((-padding.left - translation.x) as i32, -translation.y as i32), constraint_adjustment: 9, ..Default::default() }, diff --git a/src/widget/flex_row/widget.rs b/src/widget/flex_row/widget.rs index 852c18cc..e343757a 100644 --- a/src/widget/flex_row/widget.rs +++ b/src/widget/flex_row/widget.rs @@ -137,10 +137,13 @@ impl Widget for FlexR .iter() .zip(&mut tree.children) .zip(layout.children()) - .for_each(|((child, state), layout)| { - child - .as_widget() - .operate(state, layout, renderer, operation); + .for_each(|((child, state), c_layout)| { + child.as_widget().operate( + state, + c_layout.with_virtual_offset(layout.virtual_offset()), + renderer, + operation, + ); }); }); } @@ -160,11 +163,11 @@ impl Widget for FlexR .iter_mut() .zip(&mut tree.children) .zip(layout.children()) - .map(|((child, state), layout)| { + .map(|((child, state), c_layout)| { child.as_widget_mut().on_event( state, event.clone(), - layout, + c_layout.with_virtual_offset(layout.virtual_offset()), cursor, renderer, clipboard, @@ -187,10 +190,14 @@ impl Widget for FlexR .iter() .zip(&tree.children) .zip(layout.children()) - .map(|((child, state), layout)| { - child - .as_widget() - .mouse_interaction(state, layout, cursor, viewport, renderer) + .map(|((child, state), c_layout)| { + child.as_widget().mouse_interaction( + state, + c_layout.with_virtual_offset(layout.virtual_offset()), + cursor, + viewport, + renderer, + ) }) .max() .unwrap_or_default() @@ -206,15 +213,21 @@ impl Widget for FlexR cursor: mouse::Cursor, viewport: &Rectangle, ) { - for ((child, state), layout) in self + for ((child, state), c_layout) in self .children .iter() .zip(&tree.children) .zip(layout.children()) { - child - .as_widget() - .draw(state, renderer, theme, style, layout, cursor, viewport); + child.as_widget().draw( + state, + renderer, + theme, + style, + c_layout.with_virtual_offset(layout.virtual_offset()), + cursor, + viewport, + ); } } diff --git a/src/widget/grid/widget.rs b/src/widget/grid/widget.rs index fc91ba29..f09b3739 100644 --- a/src/widget/grid/widget.rs +++ b/src/widget/grid/widget.rs @@ -166,10 +166,13 @@ impl Widget for Grid< .iter() .zip(&mut tree.children) .zip(layout.children()) - .for_each(|((child, state), layout)| { - child - .as_widget() - .operate(state, layout, renderer, operation); + .for_each(|((child, state), c_layout)| { + child.as_widget().operate( + state, + c_layout.with_virtual_offset(layout.virtual_offset()), + renderer, + operation, + ); }); }); } @@ -189,11 +192,11 @@ impl Widget for Grid< .iter_mut() .zip(&mut tree.children) .zip(layout.children()) - .map(|((child, state), layout)| { + .map(|((child, state), c_layout)| { child.as_widget_mut().on_event( state, event.clone(), - layout, + c_layout.with_virtual_offset(layout.virtual_offset()), cursor, renderer, clipboard, @@ -216,10 +219,14 @@ impl Widget for Grid< .iter() .zip(&tree.children) .zip(layout.children()) - .map(|((child, state), layout)| { - child - .as_widget() - .mouse_interaction(state, layout, cursor, viewport, renderer) + .map(|((child, state), c_layout)| { + child.as_widget().mouse_interaction( + state, + c_layout.with_virtual_offset(layout.virtual_offset()), + cursor, + viewport, + renderer, + ) }) .max() .unwrap_or_default() @@ -235,15 +242,21 @@ impl Widget for Grid< cursor: mouse::Cursor, viewport: &Rectangle, ) { - for ((child, state), layout) in self + for ((child, state), c_layout) in self .children .iter() .zip(&tree.children) .zip(layout.children()) { - child - .as_widget() - .draw(state, renderer, theme, style, layout, cursor, viewport); + child.as_widget().draw( + state, + renderer, + theme, + style, + c_layout.with_virtual_offset(layout.virtual_offset()), + cursor, + viewport, + ); } } @@ -271,7 +284,13 @@ impl Widget for Grid< .iter() .zip(layout.children()) .zip(state.children.iter()) - .map(|((c, c_layout), state)| c.as_widget().a11y_nodes(c_layout, state, p)), + .map(|((c, c_layout), state)| { + c.as_widget().a11y_nodes( + c_layout.with_virtual_offset(layout.virtual_offset()), + state, + p, + ) + }), ) } @@ -282,14 +301,18 @@ impl Widget for Grid< renderer: &Renderer, dnd_rectangles: &mut iced_core::clipboard::DndDestinationRectangles, ) { - for ((e, layout), state) in self + for ((e, c_layout), state) in self .children .iter() .zip(layout.children()) .zip(state.children.iter()) { - e.as_widget() - .drag_destinations(state, layout, renderer, dnd_rectangles); + e.as_widget().drag_destinations( + state, + c_layout.with_virtual_offset(layout.virtual_offset()), + renderer, + dnd_rectangles, + ); } } } diff --git a/src/widget/id_container.rs b/src/widget/id_container.rs index 2070780b..7f6bc97e 100644 --- a/src/widget/id_container.rs +++ b/src/widget/id_container.rs @@ -88,7 +88,11 @@ where operation.container(Some(&self.id), layout.bounds(), &mut |operation| { self.content.as_widget().operate( &mut tree.children[0], - layout.children().next().unwrap(), + layout + .children() + .next() + .unwrap() + .with_virtual_offset(layout.virtual_offset()), renderer, operation, ); @@ -109,7 +113,11 @@ where self.content.as_widget_mut().on_event( &mut tree.children[0], event.clone(), - layout.children().next().unwrap(), + layout + .children() + .next() + .unwrap() + .with_virtual_offset(layout.virtual_offset()), cursor_position, renderer, clipboard, @@ -129,7 +137,7 @@ where let content_layout = layout.children().next().unwrap(); self.content.as_widget().mouse_interaction( &tree.children[0], - content_layout, + content_layout.with_virtual_offset(layout.virtual_offset()), cursor_position, viewport, renderer, @@ -152,7 +160,7 @@ where renderer, theme, renderer_style, - content_layout, + content_layout.with_virtual_offset(layout.virtual_offset()), cursor_position, viewport, ); @@ -167,7 +175,11 @@ where ) -> Option> { self.content.as_widget_mut().overlay( &mut tree.children[0], - layout.children().next().unwrap(), + layout + .children() + .next() + .unwrap() + .with_virtual_offset(layout.virtual_offset()), renderer, translation, ) @@ -183,7 +195,7 @@ where let content_layout = layout.children().next().unwrap(); self.content.as_widget().drag_destinations( &state.children[0], - content_layout, + content_layout.with_virtual_offset(layout.virtual_offset()), renderer, dnd_rectangles, ); @@ -207,7 +219,11 @@ where ) -> iced_accessibility::A11yTree { let c_layout = layout.children().next().unwrap(); let c_state = &state.children[0]; - self.content.as_widget().a11y_nodes(c_layout, c_state, p) + self.content.as_widget().a11y_nodes( + c_layout.with_virtual_offset(layout.virtual_offset()), + c_state, + p, + ) } } diff --git a/src/widget/responsive_container.rs b/src/widget/responsive_container.rs index 82b91824..2f15a889 100644 --- a/src/widget/responsive_container.rs +++ b/src/widget/responsive_container.rs @@ -131,7 +131,11 @@ where operation.container(Some(&self.id), layout.bounds(), &mut |operation| { self.content.as_widget().operate( &mut tree.children[0], - layout.children().next().unwrap(), + layout + .children() + .next() + .unwrap() + .with_virtual_offset(layout.virtual_offset()), renderer, operation, ); @@ -165,7 +169,11 @@ where self.content.as_widget_mut().on_event( &mut tree.children[0], event.clone(), - layout.children().next().unwrap(), + layout + .children() + .next() + .unwrap() + .with_virtual_offset(layout.virtual_offset()), cursor_position, renderer, clipboard, @@ -185,7 +193,7 @@ where let content_layout = layout.children().next().unwrap(); self.content.as_widget().mouse_interaction( &tree.children[0], - content_layout, + content_layout.with_virtual_offset(layout.virtual_offset()), cursor_position, viewport, renderer, @@ -208,7 +216,7 @@ where renderer, theme, renderer_style, - content_layout, + content_layout.with_virtual_offset(layout.virtual_offset()), cursor_position, viewport, ); @@ -223,7 +231,11 @@ where ) -> Option> { self.content.as_widget_mut().overlay( &mut tree.children[0], - layout.children().next().unwrap(), + layout + .children() + .next() + .unwrap() + .with_virtual_offset(layout.virtual_offset()), renderer, translation, ) @@ -239,7 +251,7 @@ where let content_layout = layout.children().next().unwrap(); self.content.as_widget().drag_destinations( &state.children[0], - content_layout, + content_layout.with_virtual_offset(layout.virtual_offset()), renderer, dnd_rectangles, ); @@ -263,7 +275,11 @@ where ) -> iced_accessibility::A11yTree { let c_layout = layout.children().next().unwrap(); let c_state = &state.children[0]; - self.content.as_widget().a11y_nodes(c_layout, c_state, p) + self.content.as_widget().a11y_nodes( + c_layout.with_virtual_offset(layout.virtual_offset()), + c_state, + p, + ) } } diff --git a/src/widget/wayland/tooltip/widget.rs b/src/widget/wayland/tooltip/widget.rs index 033ad0f3..35dd14de 100644 --- a/src/widget/wayland/tooltip/widget.rs +++ b/src/widget/wayland/tooltip/widget.rs @@ -240,7 +240,11 @@ impl<'a, Message: 'static + Clone, TopLevelMessage: 'static + Clone> operation.container(None, layout.bounds(), &mut |operation| { self.content.as_widget().operate( &mut tree.children[0], - layout.children().next().unwrap(), + layout + .children() + .next() + .unwrap() + .with_virtual_offset(layout.virtual_offset()), renderer, operation, ); @@ -271,16 +275,22 @@ impl<'a, Message: 'static + Clone, TopLevelMessage: 'static + Clone> &self.on_surface_action, || tree.state.downcast_mut::(), ); - status.merge(self.content.as_widget_mut().on_event( - &mut tree.children[0], - event, - layout.children().next().unwrap(), - cursor, - renderer, - clipboard, - shell, - viewport, - )) + status.merge( + self.content.as_widget_mut().on_event( + &mut tree.children[0], + event, + layout + .children() + .next() + .unwrap() + .with_virtual_offset(layout.virtual_offset()), + cursor, + renderer, + clipboard, + shell, + viewport, + ), + ) } #[allow(clippy::too_many_lines)] @@ -321,7 +331,7 @@ impl<'a, Message: 'static + Clone, TopLevelMessage: 'static + Clone> text_color: styling.text_color, scale_factor: renderer_style.scale_factor, }, - content_layout, + content_layout.with_virtual_offset(layout.virtual_offset()), cursor, &viewport.intersection(&bounds).unwrap_or_default(), ); @@ -339,7 +349,7 @@ impl<'a, Message: 'static + Clone, TopLevelMessage: 'static + Clone> ) -> mouse::Interaction { self.content.as_widget().mouse_interaction( &tree.children[0], - layout, + layout.children().next().unwrap(), cursor, viewport, renderer, @@ -358,7 +368,11 @@ impl<'a, Message: 'static + Clone, TopLevelMessage: 'static + Clone> translation.y += position.y; self.content.as_widget_mut().overlay( &mut tree.children[0], - layout.children().next().unwrap(), + layout + .children() + .next() + .unwrap() + .with_virtual_offset(layout.virtual_offset()), renderer, translation, ) @@ -374,7 +388,11 @@ impl<'a, Message: 'static + Clone, TopLevelMessage: 'static + Clone> ) -> iced_accessibility::A11yTree { let c_layout = layout.children().next().unwrap(); - self.content.as_widget().a11y_nodes(c_layout, state, p) + self.content.as_widget().a11y_nodes( + c_layout.with_virtual_offset(layout.virtual_offset()), + state, + p, + ) } fn id(&self) -> Option {