Skip to content

Commit

Permalink
Fix responsive, component, and lazy always returning an overlay
Browse files Browse the repository at this point in the history
  • Loading branch information
hecrj committed Oct 4, 2024
1 parent fca5d80 commit d057b16
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 61 deletions.
57 changes: 34 additions & 23 deletions widget/src/lazy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -270,29 +270,40 @@ where
renderer: &Renderer,
translation: Vector,
) -> Option<overlay::Element<'_, Message, Theme, Renderer>> {

Check warning on line 272 in widget/src/lazy.rs

View workflow job for this annotation

GitHub Actions / all

elided lifetime has a name
let overlay = Overlay(Some(
InnerBuilder {
cell: self.element.borrow().as_ref().unwrap().clone(),
element: self
.element
.borrow()
.as_ref()
.unwrap()
.borrow_mut()
.take()
.unwrap(),
tree: &mut tree.children[0],
overlay_builder: |element, tree| {
element
.as_widget_mut()
.overlay(tree, layout, renderer, translation)
.map(|overlay| RefCell::new(Nested::new(overlay)))
},
}
.build(),
));

Some(overlay::Element::new(Box::new(overlay)))
let overlay = InnerBuilder {
cell: self.element.borrow().as_ref().unwrap().clone(),
element: self
.element
.borrow()
.as_ref()
.unwrap()
.borrow_mut()
.take()
.unwrap(),
tree: &mut tree.children[0],
overlay_builder: |element, tree| {
element
.as_widget_mut()
.overlay(tree, layout, renderer, translation)
.map(|overlay| RefCell::new(Nested::new(overlay)))
},
}
.build();

#[allow(clippy::redundant_closure_for_method_calls)]
if overlay.with_overlay(|overlay| overlay.is_some()) {
Some(overlay::Element::new(Box::new(Overlay(Some(overlay)))))
} else {
let heads = overlay.into_heads();

// - You may not like it, but this is what peak performance looks like
// - TODO: Get rid of ouroboros, for good
// - What?!
*self.element.borrow().as_ref().unwrap().borrow_mut() =
Some(heads.element);

None
}
}
}

Expand Down
78 changes: 41 additions & 37 deletions widget/src/lazy/component.rs
Original file line number Diff line number Diff line change
Expand Up @@ -446,44 +446,48 @@ where
) -> Option<overlay::Element<'b, Message, Theme, Renderer>> {
self.rebuild_element_if_necessary();

let tree = tree
.state
.downcast_mut::<Rc<RefCell<Option<Tree>>>>()
.borrow_mut()
.take()
.unwrap();

let overlay = Overlay(Some(
InnerBuilder {
instance: self,
tree,
types: PhantomData,
overlay_builder: |instance, tree| {
instance.state.get_mut().as_mut().unwrap().with_element_mut(
move |element| {
element
.as_mut()
.unwrap()
.as_widget_mut()
.overlay(
&mut tree.children[0],
layout,
renderer,
translation,
)
.map(|overlay| {
RefCell::new(Nested::new(overlay))
})
},
)
},
}
.build(),
));
let state = tree.state.downcast_mut::<Rc<RefCell<Option<Tree>>>>();
let tree = state.borrow_mut().take().unwrap();

let overlay = InnerBuilder {
instance: self,
tree,
types: PhantomData,
overlay_builder: |instance, tree| {
instance.state.get_mut().as_mut().unwrap().with_element_mut(
move |element| {
element
.as_mut()
.unwrap()
.as_widget_mut()
.overlay(
&mut tree.children[0],
layout,
renderer,
translation,
)
.map(|overlay| RefCell::new(Nested::new(overlay)))
},
)
},
}
.build();

#[allow(clippy::redundant_closure_for_method_calls)]
if overlay.with_overlay(|overlay| overlay.is_some()) {
Some(overlay::Element::new(Box::new(OverlayInstance {
overlay: Some(Overlay(Some(overlay))), // Beautiful, I know
})))
} else {
let heads = overlay.into_heads();

// - You may not like it, but this is what peak performance looks like
// - TODO: Get rid of ouroboros, for good
// - What?!
*state.borrow_mut() = Some(heads.tree);

Some(overlay::Element::new(Box::new(OverlayInstance {
overlay: Some(overlay),
})))
None
}
}
}

Expand Down
6 changes: 5 additions & 1 deletion widget/src/lazy/responsive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,11 @@ where
}
.build();

Some(overlay::Element::new(Box::new(overlay)))
if overlay.with_overlay(|(overlay, _layout)| overlay.is_some()) {
Some(overlay::Element::new(Box::new(overlay)))
} else {
None
}
}
}

Expand Down

0 comments on commit d057b16

Please sign in to comment.