Skip to content

Commit

Permalink
Batch event processing in UserInterface::update
Browse files Browse the repository at this point in the history
  • Loading branch information
hecrj committed Nov 12, 2020
1 parent 69c50c8 commit bf2d256
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 88 deletions.
36 changes: 18 additions & 18 deletions glutin/src/application.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,21 +155,32 @@ async fn run_instance<A, E, C>(
user_interface.draw(&mut renderer, state.cursor_position());
let mut mouse_interaction = mouse::Interaction::default();

let mut events = Vec::new();
let mut messages = Vec::new();
let mut is_clean = true;

debug.startup_finished();

while let Some(event) = receiver.next().await {
match event {
event::Event::NewEvents(_) => {
debug.event_processing_started();
}
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();

if is_clean && messages.is_empty() {
continue;
for event in events.drain(..).zip(statuses.into_iter()) {
runtime.broadcast(event);
}

if !messages.is_empty() {
Expand Down Expand Up @@ -203,7 +214,6 @@ async fn run_instance<A, E, C>(
debug.draw_finished();

context.window().request_redraw();
is_clean = true;
}
event::Event::UserEvent(message) => {
messages.push(message);
Expand Down Expand Up @@ -277,17 +287,7 @@ async fn run_instance<A, E, C>(
state.scale_factor(),
state.modifiers(),
) {
let event_status = user_interface.update(
event.clone(),
state.cursor_position(),
clipboard.as_ref().map(|c| c as _),
&mut renderer,
&mut messages,
);

runtime.broadcast((event, event_status));

is_clean = false;
events.push(event);
}
}
_ => {}
Expand Down
17 changes: 8 additions & 9 deletions native/src/program/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,17 +122,16 @@ where
debug.event_processing_started();
let mut messages = Vec::new();

for event in self.queued_events.drain(..) {
let _ = user_interface.update(
event,
cursor_position,
clipboard,
renderer,
&mut messages,
);
}
let _ = user_interface.update(
&self.queued_events,
cursor_position,
clipboard,
renderer,
&mut messages,
);

messages.extend(self.queued_messages.drain(..));
self.queued_events.clear();
debug.event_processing_finished();

if messages.is_empty() {
Expand Down
95 changes: 52 additions & 43 deletions native/src/user_interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -182,16 +182,14 @@ where
/// &mut renderer,
/// );
///
/// for event in events.drain(..) {
/// // Update the user interface
/// let _event_status = user_interface.update(
/// event,
/// cursor_position,
/// None,
/// &renderer,
/// &mut messages
/// );
/// }
/// // Update the user interface
/// let event_statuses = user_interface.update(
/// &events,
/// cursor_position,
/// None,
/// &renderer,
/// &mut messages
/// );
///
/// cache = user_interface.into_cache();
///
Expand All @@ -203,13 +201,13 @@ where
/// ```
pub fn update(
&mut self,
event: Event,
events: &[Event],
cursor_position: Point,
clipboard: Option<&dyn Clipboard>,
renderer: &Renderer,
messages: &mut Vec<Message>,
) -> event::Status {
let (base_cursor, overlay_status) = if let Some(mut overlay) =
) -> Vec<event::Status> {
let (base_cursor, overlay_statuses) = if let Some(mut overlay) =
self.root.overlay(Layout::new(&self.base.layout))
{
let layer = Self::overlay_layer(
Expand All @@ -219,14 +217,20 @@ where
renderer,
);

let event_status = overlay.on_event(
event.clone(),
Layout::new(&layer.layout),
cursor_position,
messages,
renderer,
clipboard,
);
let event_statuses = events
.iter()
.cloned()
.map(|event| {
overlay.on_event(
event,
Layout::new(&layer.layout),
cursor_position,
messages,
renderer,
clipboard,
)
})
.collect();

let base_cursor = if layer.layout.bounds().contains(cursor_position)
{
Expand All @@ -238,21 +242,28 @@ where

self.overlay = Some(layer);

(base_cursor, event_status)
(base_cursor, event_statuses)
} else {
(cursor_position, event::Status::Ignored)
(cursor_position, vec![event::Status::Ignored; events.len()])
};

let event_status = self.root.widget.on_event(
event,
Layout::new(&self.base.layout),
base_cursor,
messages,
renderer,
clipboard,
);

event_status.merge(overlay_status)
events
.iter()
.cloned()
.zip(overlay_statuses.into_iter())
.map(|(event, overlay_status)| {
let event_status = self.root.widget.on_event(
event,
Layout::new(&self.base.layout),
base_cursor,
messages,
renderer,
clipboard,
);

event_status.merge(overlay_status)
})
.collect()
}

/// Draws the [`UserInterface`] with the provided [`Renderer`].
Expand Down Expand Up @@ -305,16 +316,14 @@ where
/// &mut renderer,
/// );
///
/// for event in events.drain(..) {
/// // Update the user interface
/// let _event_status = user_interface.update(
/// event,
/// cursor_position,
/// None,
/// &renderer,
/// &mut messages
/// );
/// }
/// // Update the user interface
/// let event_statuses = user_interface.update(
/// &events,
/// cursor_position,
/// None,
/// &renderer,
/// &mut messages
/// );
///
/// // Draw the user interface
/// let mouse_cursor = user_interface.draw(&mut renderer, cursor_position);
Expand Down
36 changes: 18 additions & 18 deletions winit/src/application.rs
Original file line number Diff line number Diff line change
Expand Up @@ -241,21 +241,32 @@ async fn run_instance<A, E, C>(
user_interface.draw(&mut renderer, state.cursor_position());
let mut mouse_interaction = mouse::Interaction::default();

let mut events = Vec::new();
let mut messages = Vec::new();
let mut is_clean = true;

debug.startup_finished();

while let Some(event) = receiver.next().await {
match event {
event::Event::NewEvents(_) => {
debug.event_processing_started();
}
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();

if is_clean && messages.is_empty() {
continue;
for event in events.drain(..).zip(statuses.into_iter()) {
runtime.broadcast(event);
}

if !messages.is_empty() {
Expand Down Expand Up @@ -288,7 +299,6 @@ async fn run_instance<A, E, C>(
debug.draw_finished();

window.request_redraw();
is_clean = true;
}
event::Event::UserEvent(message) => {
messages.push(message);
Expand Down Expand Up @@ -359,17 +369,7 @@ async fn run_instance<A, E, C>(
state.scale_factor(),
state.modifiers(),
) {
let event_status = user_interface.update(
event.clone(),
state.cursor_position(),
clipboard.as_ref().map(|c| c as _),
&mut renderer,
&mut messages,
);

runtime.broadcast((event, event_status));

is_clean = false;
events.push(event);
}
}
_ => {}
Expand Down

0 comments on commit bf2d256

Please sign in to comment.