Skip to content

Commit

Permalink
Relayout base layer when layout is invalidated from an overlay
Browse files Browse the repository at this point in the history
  • Loading branch information
hecrj committed May 13, 2022
1 parent 4c98de0 commit 296e157
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 22 deletions.
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

0 comments on commit 296e157

Please sign in to comment.