From 738dfebfdd5d6361b510f3def29d191202a7a368 Mon Sep 17 00:00:00 2001 From: Nico Burns Date: Fri, 17 Nov 2023 00:02:00 +0000 Subject: [PATCH] Make set_origin method take WidgetState rather than LayoutCx This allows it to be called from the event method --- src/app.rs | 2 +- src/widget/core.rs | 13 +++++++++---- src/widget/linear_layout.rs | 2 +- 3 files changed, 11 insertions(+), 6 deletions(-) diff --git a/src/app.rs b/src/app.rs index 1e9d55751..cec2163e9 100644 --- a/src/app.rs +++ b/src/app.rs @@ -250,7 +250,7 @@ where let mut layout_cx = LayoutCx::new(&mut cx_state, &mut self.root_state); let bc = BoxConstraints::tight(self.size); root_pod.layout(&mut layout_cx, &bc); - root_pod.set_origin(&mut layout_cx, Point::ORIGIN); + root_pod.set_origin(layout_cx.widget_state, Point::ORIGIN); } if root_pod .state diff --git a/src/widget/core.rs b/src/widget/core.rs index 16db18e72..c2544e61d 100644 --- a/src/widget/core.rs +++ b/src/widget/core.rs @@ -95,7 +95,7 @@ pub struct Pod { } #[derive(Debug)] -pub(crate) struct WidgetState { +pub struct WidgetState { pub(crate) id: Id, pub(crate) flags: PodFlags, /// The origin of the child in the parent's coordinate space. @@ -486,18 +486,23 @@ impl Pod { /// The widget container can also call `set_origin` from other context, but calling `set_origin` /// after the widget received [`LifeCycle::ViewContextChanged`] and before the next event results /// in an inconsistent state of the widget tree. - pub fn set_origin(&mut self, cx: &mut LayoutCx, origin: Point) { + pub fn set_origin(&mut self, parent_state: &mut WidgetState, origin: Point) { if origin != self.state.origin { self.state.origin = origin; // request paint is called on the parent instead of this widget, since this widget's // fragment does not change. - cx.view_context_changed(); - cx.request_paint(); + parent_state.flags |= PodFlags::REQUEST_PAINT; + parent_state.flags |= PodFlags::VIEW_CONTEXT_CHANGED; self.state.flags.insert(PodFlags::VIEW_CONTEXT_CHANGED); } } + /// Get the widgets size (as returned by the layout method) + pub fn get_size(&mut self) -> Size { + self.state.size + } + // Return true if hot state has changed fn set_hot_state( widget: &mut dyn AnyWidget, diff --git a/src/widget/linear_layout.rs b/src/widget/linear_layout.rs index 7057ecf9b..57adbb3be 100644 --- a/src/widget/linear_layout.rs +++ b/src/widget/linear_layout.rs @@ -70,7 +70,7 @@ impl Widget for LinearLayout { for (index, child) in self.children.iter_mut().enumerate() { let size = child.layout(cx, &child_bc); - child.set_origin(cx, self.axis.pack(major_used, 0.0)); + child.set_origin(cx.widget_state, self.axis.pack(major_used, 0.0)); major_used += self.axis.major(size); if index < child_count - 1 { major_used += self.spacing;