diff --git a/native/src/shell.rs b/native/src/shell.rs index 4a0aa9c66b..b96d23e5aa 100644 --- a/native/src/shell.rs +++ b/native/src/shell.rs @@ -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); diff --git a/native/src/user_interface.rs b/native/src/user_interface.rs index f80786aa1e..785ee56a59 100644 --- a/native/src/user_interface.rs +++ b/native/src/user_interface.rs @@ -177,40 +177,61 @@ where clipboard: &mut dyn Clipboard, messages: &mut Vec, ) -> (State, Vec) { + 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 @@ -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()