Skip to content

Commit

Permalink
Make set_origin method take WidgetState rather than LayoutCx
Browse files Browse the repository at this point in the history
This allows it to be called from the event method
  • Loading branch information
nicoburns committed Nov 28, 2023
1 parent c439885 commit 738dfeb
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 9 additions & 4 deletions src/widget/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion src/widget/linear_layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 738dfeb

Please sign in to comment.