Return `event::Status` in `UserInterface::update`

This commit is contained in:
Héctor Ramón Jiménez 2020-11-12 02:00:08 +01:00
parent 6e9bd0d9d1
commit 33d80b5a0b
4 changed files with 107 additions and 92 deletions

View File

@ -155,30 +155,23 @@ 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 external_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() && external_messages.is_empty() { debug.event_processing_finished();
if is_clean && messages.is_empty() {
continue; continue;
} }
debug.event_processing_started();
let mut messages = user_interface.update(
&events,
state.cursor_position(),
clipboard.as_ref().map(|c| c as _),
&mut renderer,
);
messages.extend(external_messages.drain(..));
events.clear();
debug.event_processing_finished();
if !messages.is_empty() { if !messages.is_empty() {
let cache = let cache =
ManuallyDrop::into_inner(user_interface).into_cache(); ManuallyDrop::into_inner(user_interface).into_cache();
@ -188,7 +181,7 @@ async fn run_instance<A, E, C>(
&mut application, &mut application,
&mut runtime, &mut runtime,
&mut debug, &mut debug,
messages, &mut messages,
); );
// Update window // Update window
@ -210,9 +203,10 @@ 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) => {
external_messages.push(message); messages.push(message);
} }
event::Event::RedrawRequested(_) => { event::Event::RedrawRequested(_) => {
debug.render_started(); debug.render_started();
@ -283,8 +277,17 @@ async fn run_instance<A, E, C>(
state.scale_factor(), state.scale_factor(),
state.modifiers(), state.modifiers(),
) { ) {
events.push(event.clone()); let _ = user_interface.update(
event.clone(),
state.cursor_position(),
clipboard.as_ref().map(|c| c as _),
&mut renderer,
&mut messages,
);
runtime.broadcast(event); runtime.broadcast(event);
is_clean = false;
} }
} }
_ => {} _ => {}

View File

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

View File

@ -1,6 +1,7 @@
use crate::event::{self, Event};
use crate::layout; use crate::layout;
use crate::overlay; use crate::overlay;
use crate::{Clipboard, Element, Event, Layout, Point, Rectangle, Size}; use crate::{Clipboard, Element, Layout, Point, Rectangle, Size};
use std::hash::Hasher; use std::hash::Hasher;
@ -169,9 +170,10 @@ where
/// ///
/// // Initialize our event storage /// // Initialize our event storage
/// let mut events = Vec::new(); /// let mut events = Vec::new();
/// let mut messages = Vec::new();
/// ///
/// loop { /// loop {
/// // Process system events... /// // Obtain system events...
/// ///
/// let mut user_interface = UserInterface::build( /// let mut user_interface = UserInterface::build(
/// counter.view(), /// counter.view(),
@ -180,32 +182,34 @@ where
/// &mut renderer, /// &mut renderer,
/// ); /// );
/// ///
/// // Update the user interface /// for event in events.drain(..) {
/// let messages = user_interface.update( /// // Update the user interface
/// &events, /// let _event_status = user_interface.update(
/// cursor_position, /// event,
/// None, /// cursor_position,
/// &renderer, /// None,
/// ); /// &renderer,
/// &mut messages
/// );
/// }
/// ///
/// cache = user_interface.into_cache(); /// cache = user_interface.into_cache();
/// ///
/// // Process the produced messages /// // Process the produced messages
/// for message in messages { /// for message in messages.drain(..) {
/// counter.update(message); /// counter.update(message);
/// } /// }
/// } /// }
/// ``` /// ```
pub fn update( pub fn update(
&mut self, &mut self,
events: &[Event], event: Event,
cursor_position: Point, cursor_position: Point,
clipboard: Option<&dyn Clipboard>, clipboard: Option<&dyn Clipboard>,
renderer: &Renderer, renderer: &Renderer,
) -> Vec<Message> { messages: &mut Vec<Message>,
let mut messages = Vec::new(); ) -> event::Status {
let (base_cursor, overlay_status) = if let Some(mut overlay) =
let base_cursor = 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(
@ -215,16 +219,14 @@ where
renderer, renderer,
); );
for event in events { let event_status = overlay.on_event(
let _ = overlay.on_event( event.clone(),
event.clone(), Layout::new(&layer.layout),
Layout::new(&layer.layout), cursor_position,
cursor_position, messages,
&mut messages, renderer,
renderer, clipboard,
clipboard, );
);
}
let base_cursor = if layer.layout.bounds().contains(cursor_position) let base_cursor = if layer.layout.bounds().contains(cursor_position)
{ {
@ -236,23 +238,21 @@ where
self.overlay = Some(layer); self.overlay = Some(layer);
base_cursor (base_cursor, event_status)
} else { } else {
cursor_position (cursor_position, event::Status::Ignored)
}; };
for event in events { let event_status = self.root.widget.on_event(
let _ = self.root.widget.on_event( event,
event.clone(), Layout::new(&self.base.layout),
Layout::new(&self.base.layout), base_cursor,
base_cursor, messages,
&mut messages, renderer,
renderer, clipboard,
clipboard, );
);
}
messages event_status.merge(overlay_status)
} }
/// Draws the [`UserInterface`] with the provided [`Renderer`]. /// Draws the [`UserInterface`] with the provided [`Renderer`].
@ -293,9 +293,10 @@ where
/// let mut window_size = Size::new(1024.0, 768.0); /// let mut window_size = Size::new(1024.0, 768.0);
/// let mut cursor_position = Point::default(); /// let mut cursor_position = Point::default();
/// let mut events = Vec::new(); /// let mut events = Vec::new();
/// let mut messages = Vec::new();
/// ///
/// loop { /// loop {
/// // Process system events... /// // Obtain system events...
/// ///
/// let mut user_interface = UserInterface::build( /// let mut user_interface = UserInterface::build(
/// counter.view(), /// counter.view(),
@ -304,19 +305,23 @@ where
/// &mut renderer, /// &mut renderer,
/// ); /// );
/// ///
/// let messages = user_interface.update( /// for event in events.drain(..) {
/// &events, /// // Update the user interface
/// cursor_position, /// let _event_status = user_interface.update(
/// None, /// event,
/// &renderer, /// cursor_position,
/// ); /// None,
/// &renderer,
/// &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);
/// ///
/// cache = user_interface.into_cache(); /// cache = user_interface.into_cache();
/// ///
/// for message in messages { /// for message in messages.drain(..) {
/// counter.update(message); /// counter.update(message);
/// } /// }
/// ///

View File

@ -241,30 +241,23 @@ 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 external_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() && external_messages.is_empty() { debug.event_processing_finished();
if is_clean && messages.is_empty() {
continue; continue;
} }
debug.event_processing_started();
let mut messages = user_interface.update(
&events,
state.cursor_position(),
clipboard.as_ref().map(|c| c as _),
&mut renderer,
);
messages.extend(external_messages.drain(..));
events.clear();
debug.event_processing_finished();
if !messages.is_empty() { if !messages.is_empty() {
let cache = let cache =
ManuallyDrop::into_inner(user_interface).into_cache(); ManuallyDrop::into_inner(user_interface).into_cache();
@ -274,7 +267,7 @@ async fn run_instance<A, E, C>(
&mut application, &mut application,
&mut runtime, &mut runtime,
&mut debug, &mut debug,
messages, &mut messages,
); );
// Update window // Update window
@ -295,9 +288,10 @@ 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) => {
external_messages.push(message); messages.push(message);
} }
event::Event::RedrawRequested(_) => { event::Event::RedrawRequested(_) => {
debug.render_started(); debug.render_started();
@ -365,8 +359,17 @@ async fn run_instance<A, E, C>(
state.scale_factor(), state.scale_factor(),
state.modifiers(), state.modifiers(),
) { ) {
events.push(event.clone()); let _ = user_interface.update(
event.clone(),
state.cursor_position(),
clipboard.as_ref().map(|c| c as _),
&mut renderer,
&mut messages,
);
runtime.broadcast(event); runtime.broadcast(event);
is_clean = false;
} }
} }
_ => {} _ => {}
@ -437,9 +440,9 @@ pub fn update<A: Application, E: Executor>(
application: &mut A, application: &mut A,
runtime: &mut Runtime<E, Proxy<A::Message>, A::Message>, runtime: &mut Runtime<E, Proxy<A::Message>, A::Message>,
debug: &mut Debug, debug: &mut Debug,
messages: Vec<A::Message>, messages: &mut Vec<A::Message>,
) { ) {
for message in messages { for message in messages.drain(..) {
debug.log_message(&message); debug.log_message(&message);
debug.update_started(); debug.update_started();