Skip to content

Commit

Permalink
Merge pull request #1799 from tarkah/fix/responsive-layout
Browse files Browse the repository at this point in the history
Invalidate responsive layout when shell layout is invalidated
  • Loading branch information
hecrj authored Apr 21, 2023
2 parents 99fc717 + ecc1a45 commit 501b6b8
Showing 1 changed file with 33 additions and 21 deletions.
54 changes: 33 additions & 21 deletions lazy/src/responsive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ where
view: Box::new(view),
content: RefCell::new(Content {
size: Size::ZERO,
layout: layout::Node::new(Size::ZERO),
layout: None,
element: Element::new(horizontal_space(0)),
}),
}
Expand All @@ -50,18 +50,27 @@ where

struct Content<'a, Message, Renderer> {
size: Size,
layout: layout::Node,
layout: Option<layout::Node>,
element: Element<'a, Message, Renderer>,
}

impl<'a, Message, Renderer> Content<'a, Message, Renderer>
where
Renderer: iced_native::Renderer,
{
fn layout(&mut self, renderer: &Renderer) {
if self.layout.is_none() {
self.layout =
Some(self.element.as_widget().layout(
renderer,
&layout::Limits::new(Size::ZERO, self.size),
));
}
}

fn update(
&mut self,
tree: &mut Tree,
renderer: &Renderer,
new_size: Size,
view: &dyn Fn(Size) -> Element<'a, Message, Renderer>,
) {
Expand All @@ -73,11 +82,6 @@ where
self.size = new_size;

tree.diff(&self.element);

self.layout = self
.element
.as_widget()
.layout(renderer, &layout::Limits::new(Size::ZERO, self.size));
}

fn resolve<R, T>(
Expand All @@ -96,11 +100,12 @@ where
where
R: Deref<Target = Renderer>,
{
self.update(tree, renderer.deref(), layout.bounds().size(), view);
self.update(tree, layout.bounds().size(), view);
self.layout(renderer.deref());

let content_layout = Layout::with_offset(
layout.position() - Point::ORIGIN,
&self.layout,
self.layout.as_ref().unwrap(),
);

f(tree, renderer, content_layout, &mut self.element)
Expand Down Expand Up @@ -178,7 +183,10 @@ where
let state = tree.state.downcast_mut::<State>();
let mut content = self.content.borrow_mut();

content.resolve(
let mut local_messages = vec![];
let mut local_shell = Shell::new(&mut local_messages);

let status = content.resolve(
&mut state.tree.borrow_mut(),
renderer,
layout,
Expand All @@ -191,10 +199,18 @@ where
cursor_position,
renderer,
clipboard,
shell,
&mut local_shell,
)
},
)
);

if local_shell.is_layout_invalid() {
content.layout = None;
}

shell.merge(local_shell, std::convert::identity);

status
}

fn draw(
Expand Down Expand Up @@ -272,22 +288,18 @@ where
tree: state.tree.borrow_mut(),
types: PhantomData,
overlay_builder: |content: &mut RefMut<Content<_, _>>, tree| {
content.update(
tree,
renderer,
layout.bounds().size(),
&self.view,
);
content.update(tree, layout.bounds().size(), &self.view);
content.layout(renderer);

let Content {
element,
layout: content_layout,
layout: content_layout_node,
..
} = content.deref_mut();

let content_layout = Layout::with_offset(
layout.bounds().position() - Point::ORIGIN,
content_layout,
content_layout_node.as_ref().unwrap(),
);

element
Expand Down

0 comments on commit 501b6b8

Please sign in to comment.