Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Invalidate responsive layout when shell layout is invalidated #1799

Merged
merged 4 commits into from
Apr 21, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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