Batch event processing in `UserInterface::update`

This commit is contained in:
Héctor Ramón Jiménez 2020-11-12 02:51:26 +01:00
parent 69c50c8511
commit bf2d2561b8
4 changed files with 95 additions and 87 deletions

View File

@ -155,21 +155,32 @@ async fn run_instance<A, E, C>(
user_interface.draw(&mut renderer, state.cursor_position()); user_interface.draw(&mut renderer, state.cursor_position());
let mut mouse_interaction = mouse::Interaction::default(); let mut mouse_interaction = mouse::Interaction::default();
let mut events = Vec::new();
let mut messages = Vec::new(); let mut messages = Vec::new();
let mut is_clean = true;
debug.startup_finished(); debug.startup_finished();
while let Some(event) = receiver.next().await { while let Some(event) = receiver.next().await {
match event { match event {
event::Event::NewEvents(_) => {
debug.event_processing_started();
}
event::Event::MainEventsCleared => { event::Event::MainEventsCleared => {
if events.is_empty() && messages.is_empty() {
continue;
}
debug.event_processing_started();
let statuses = user_interface.update(
&events,
state.cursor_position(),
clipboard.as_ref().map(|c| c as _),
&mut renderer,
&mut messages,
);
debug.event_processing_finished(); debug.event_processing_finished();
if is_clean && messages.is_empty() { for event in events.drain(..).zip(statuses.into_iter()) {
continue; runtime.broadcast(event);
} }
if !messages.is_empty() { if !messages.is_empty() {
@ -203,7 +214,6 @@ async fn run_instance<A, E, C>(
debug.draw_finished(); debug.draw_finished();
context.window().request_redraw(); context.window().request_redraw();
is_clean = true;
} }
event::Event::UserEvent(message) => { event::Event::UserEvent(message) => {
messages.push(message); messages.push(message);
@ -277,17 +287,7 @@ async fn run_instance<A, E, C>(
state.scale_factor(), state.scale_factor(),
state.modifiers(), state.modifiers(),
) { ) {
let event_status = user_interface.update( events.push(event);
event.clone(),
state.cursor_position(),
clipboard.as_ref().map(|c| c as _),
&mut renderer,
&mut messages,
);
runtime.broadcast((event, event_status));
is_clean = false;
} }
} }
_ => {} _ => {}

View File

@ -122,17 +122,16 @@ where
debug.event_processing_started(); debug.event_processing_started();
let mut messages = Vec::new(); let mut messages = Vec::new();
for event in self.queued_events.drain(..) {
let _ = user_interface.update( let _ = user_interface.update(
event, &self.queued_events,
cursor_position, cursor_position,
clipboard, clipboard,
renderer, renderer,
&mut messages, &mut messages,
); );
}
messages.extend(self.queued_messages.drain(..)); messages.extend(self.queued_messages.drain(..));
self.queued_events.clear();
debug.event_processing_finished(); debug.event_processing_finished();
if messages.is_empty() { if messages.is_empty() {

View File

@ -182,16 +182,14 @@ where
/// &mut renderer, /// &mut renderer,
/// ); /// );
/// ///
/// for event in events.drain(..) {
/// // Update the user interface /// // Update the user interface
/// let _event_status = user_interface.update( /// let event_statuses = user_interface.update(
/// event, /// &events,
/// cursor_position, /// cursor_position,
/// None, /// None,
/// &renderer, /// &renderer,
/// &mut messages /// &mut messages
/// ); /// );
/// }
/// ///
/// cache = user_interface.into_cache(); /// cache = user_interface.into_cache();
/// ///
@ -203,13 +201,13 @@ where
/// ``` /// ```
pub fn update( pub fn update(
&mut self, &mut self,
event: Event, events: &[Event],
cursor_position: Point, cursor_position: Point,
clipboard: Option<&dyn Clipboard>, clipboard: Option<&dyn Clipboard>,
renderer: &Renderer, renderer: &Renderer,
messages: &mut Vec<Message>, messages: &mut Vec<Message>,
) -> event::Status { ) -> Vec<event::Status> {
let (base_cursor, overlay_status) = if let Some(mut overlay) = let (base_cursor, overlay_statuses) = if let Some(mut overlay) =
self.root.overlay(Layout::new(&self.base.layout)) self.root.overlay(Layout::new(&self.base.layout))
{ {
let layer = Self::overlay_layer( let layer = Self::overlay_layer(
@ -219,14 +217,20 @@ where
renderer, renderer,
); );
let event_status = overlay.on_event( let event_statuses = events
event.clone(), .iter()
.cloned()
.map(|event| {
overlay.on_event(
event,
Layout::new(&layer.layout), Layout::new(&layer.layout),
cursor_position, cursor_position,
messages, messages,
renderer, renderer,
clipboard, clipboard,
); )
})
.collect();
let base_cursor = if layer.layout.bounds().contains(cursor_position) let base_cursor = if layer.layout.bounds().contains(cursor_position)
{ {
@ -238,11 +242,16 @@ where
self.overlay = Some(layer); self.overlay = Some(layer);
(base_cursor, event_status) (base_cursor, event_statuses)
} else { } else {
(cursor_position, event::Status::Ignored) (cursor_position, vec![event::Status::Ignored; events.len()])
}; };
events
.iter()
.cloned()
.zip(overlay_statuses.into_iter())
.map(|(event, overlay_status)| {
let event_status = self.root.widget.on_event( let event_status = self.root.widget.on_event(
event, event,
Layout::new(&self.base.layout), Layout::new(&self.base.layout),
@ -253,6 +262,8 @@ where
); );
event_status.merge(overlay_status) event_status.merge(overlay_status)
})
.collect()
} }
/// Draws the [`UserInterface`] with the provided [`Renderer`]. /// Draws the [`UserInterface`] with the provided [`Renderer`].
@ -305,16 +316,14 @@ where
/// &mut renderer, /// &mut renderer,
/// ); /// );
/// ///
/// for event in events.drain(..) {
/// // Update the user interface /// // Update the user interface
/// let _event_status = user_interface.update( /// let event_statuses = user_interface.update(
/// event, /// &events,
/// cursor_position, /// cursor_position,
/// None, /// None,
/// &renderer, /// &renderer,
/// &mut messages /// &mut messages
/// ); /// );
/// }
/// ///
/// // Draw the user interface /// // Draw the user interface
/// let mouse_cursor = user_interface.draw(&mut renderer, cursor_position); /// let mouse_cursor = user_interface.draw(&mut renderer, cursor_position);

View File

@ -241,21 +241,32 @@ async fn run_instance<A, E, C>(
user_interface.draw(&mut renderer, state.cursor_position()); user_interface.draw(&mut renderer, state.cursor_position());
let mut mouse_interaction = mouse::Interaction::default(); let mut mouse_interaction = mouse::Interaction::default();
let mut events = Vec::new();
let mut messages = Vec::new(); let mut messages = Vec::new();
let mut is_clean = true;
debug.startup_finished(); debug.startup_finished();
while let Some(event) = receiver.next().await { while let Some(event) = receiver.next().await {
match event { match event {
event::Event::NewEvents(_) => {
debug.event_processing_started();
}
event::Event::MainEventsCleared => { event::Event::MainEventsCleared => {
if events.is_empty() && messages.is_empty() {
continue;
}
debug.event_processing_started();
let statuses = user_interface.update(
&events,
state.cursor_position(),
clipboard.as_ref().map(|c| c as _),
&mut renderer,
&mut messages,
);
debug.event_processing_finished(); debug.event_processing_finished();
if is_clean && messages.is_empty() { for event in events.drain(..).zip(statuses.into_iter()) {
continue; runtime.broadcast(event);
} }
if !messages.is_empty() { if !messages.is_empty() {
@ -288,7 +299,6 @@ async fn run_instance<A, E, C>(
debug.draw_finished(); debug.draw_finished();
window.request_redraw(); window.request_redraw();
is_clean = true;
} }
event::Event::UserEvent(message) => { event::Event::UserEvent(message) => {
messages.push(message); messages.push(message);
@ -359,17 +369,7 @@ async fn run_instance<A, E, C>(
state.scale_factor(), state.scale_factor(),
state.modifiers(), state.modifiers(),
) { ) {
let event_status = user_interface.update( events.push(event);
event.clone(),
state.cursor_position(),
clipboard.as_ref().map(|c| c as _),
&mut renderer,
&mut messages,
);
runtime.broadcast((event, event_status));
is_clean = false;
} }
} }
_ => {} _ => {}