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

fix: prevent building component overlay with stale layout #1341

Merged
merged 4 commits into from
May 14, 2022
Merged
Show file tree
Hide file tree
Changes from 3 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
5 changes: 5 additions & 0 deletions native/src/shell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ impl<'a, Message> Shell<'a, Message> {
}
}

/// Returns whether the current layout is invalid or not.
pub fn is_layout_invalid(&self) -> bool {
self.is_layout_invalid
}

/// Publish the given `Message` for an application to process it.
pub fn publish(&mut self, message: Message) {
self.messages.push(message);
Expand Down
67 changes: 45 additions & 22 deletions native/src/user_interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -177,40 +177,61 @@ where
clipboard: &mut dyn Clipboard,
messages: &mut Vec<Message>,
) -> (State, Vec<event::Status>) {
use std::mem::ManuallyDrop;

let mut state = State::Updated;
let mut manual_overlay = ManuallyDrop::new(
self.root.overlay(Layout::new(&self.base), renderer),
);

let (base_cursor, overlay_statuses) = if let Some(mut overlay) =
self.root.overlay(Layout::new(&self.base), renderer)
{
let (base_cursor, overlay_statuses) = if manual_overlay.is_some() {
let bounds = self.bounds;

let mut overlay = manual_overlay.as_mut().unwrap();
let mut layout = overlay.layout(renderer, bounds);
let mut event_statuses = Vec::new();

let event_statuses = events
.iter()
.cloned()
.map(|event| {
let mut shell = Shell::new(messages);
for event in events.iter().cloned() {
let mut shell = Shell::new(messages);

let event_status = overlay.on_event(
event,
Layout::new(&layout),
cursor_position,
renderer,
clipboard,
&mut shell,
let event_status = overlay.on_event(
event,
Layout::new(&layout),
cursor_position,
renderer,
clipboard,
&mut shell,
);

event_statuses.push(event_status);

if shell.is_layout_invalid() {
let _ = ManuallyDrop::into_inner(manual_overlay);

self.base = renderer.layout(
&self.root,
&layout::Limits::new(Size::ZERO, self.bounds),
);

manual_overlay = ManuallyDrop::new(
self.root.overlay(Layout::new(&self.base), renderer),
);

if manual_overlay.is_none() {
break;
}

overlay = manual_overlay.as_mut().unwrap();

shell.revalidate_layout(|| {
layout = overlay.layout(renderer, bounds);
});
}

if shell.are_widgets_invalid() {
state = State::Outdated;
}

event_status
})
.collect();
if shell.are_widgets_invalid() {
state = State::Outdated;
}
}

let base_cursor = if layout.bounds().contains(cursor_position) {
// TODO: Type-safe cursor availability
Expand All @@ -226,6 +247,8 @@ where
(cursor_position, vec![event::Status::Ignored; events.len()])
};

let _ = ManuallyDrop::into_inner(manual_overlay);

let event_statuses = events
.iter()
.cloned()
Expand Down