summaryrefslogtreecommitdiffstats
path: root/winit/src/application.rs
diff options
context:
space:
mode:
authorLibravatar Héctor Ramón <hector0193@gmail.com>2019-12-16 21:38:56 +0100
committerLibravatar GitHub <noreply@github.com>2019-12-16 21:38:56 +0100
commit0f2e20f5e5b1f0658ab4e6cbe6fdda9ca97f2b36 (patch)
tree6b4c601bfa0ced1e003f597d7f485be7c108e12c /winit/src/application.rs
parent3702b109977a249247a0f1be40e57bec2cbaa4e3 (diff)
parent430ab6e44432d044f8444575053d97651f0f7d20 (diff)
downloadiced-0f2e20f5e5b1f0658ab4e6cbe6fdda9ca97f2b36.tar.gz
iced-0f2e20f5e5b1f0658ab4e6cbe6fdda9ca97f2b36.tar.bz2
iced-0f2e20f5e5b1f0658ab4e6cbe6fdda9ca97f2b36.zip
Merge pull request #122 from hecrj/feature/event-subscriptions
Event subscriptions
Diffstat (limited to 'winit/src/application.rs')
-rw-r--r--winit/src/application.rs29
1 files changed, 26 insertions, 3 deletions
diff --git a/winit/src/application.rs b/winit/src/application.rs
index 85d06d9b..3b8ac16b 100644
--- a/winit/src/application.rs
+++ b/winit/src/application.rs
@@ -2,8 +2,8 @@ use crate::{
conversion,
input::{keyboard, mouse},
renderer::{Target, Windowed},
- Cache, Command, Container, Debug, Element, Event, Length, MouseCursor,
- Settings, UserInterface,
+ subscription, Cache, Command, Container, Debug, Element, Event, Length,
+ MouseCursor, Settings, Subscription, UserInterface,
};
/// An interactive, native cross-platform application.
@@ -57,6 +57,15 @@ pub trait Application: Sized {
/// [`Command`]: struct.Command.html
fn update(&mut self, message: Self::Message) -> Command<Self::Message>;
+ /// Returns the event `Subscription` for the current state of the
+ /// application.
+ ///
+ /// The messages produced by the `Subscription` will be handled by
+ /// [`update`](#tymethod.update).
+ ///
+ /// A `Subscription` will be kept alive as long as you keep returning it!
+ fn subscription(&self) -> Subscription<Self::Message>;
+
/// Returns the widgets to display in the [`Application`].
///
/// These widgets can produce __messages__ based on user interaction.
@@ -89,11 +98,15 @@ pub trait Application: Sized {
let proxy = event_loop.create_proxy();
let mut thread_pool =
futures::executor::ThreadPool::new().expect("Create thread pool");
+ let mut subscription_pool = subscription::Pool::new();
let mut external_messages = Vec::new();
let (mut application, init_command) = Self::new();
spawn(init_command, &mut thread_pool, &proxy);
+ let subscription = application.subscription();
+ subscription_pool.update(subscription, &mut thread_pool, &proxy);
+
let mut title = application.title();
let window = {
@@ -176,6 +189,10 @@ pub trait Application: Sized {
debug.layout_finished();
debug.event_processing_started();
+ events.iter().for_each(|event| {
+ subscription_pool.broadcast_event(*event)
+ });
+
let mut messages =
user_interface.update(&renderer, events.drain(..));
messages.extend(external_messages.drain(..));
@@ -199,11 +216,17 @@ pub trait Application: Sized {
debug.update_started();
let command = application.update(message);
-
spawn(command, &mut thread_pool, &proxy);
debug.update_finished();
}
+ let subscription = application.subscription();
+ subscription_pool.update(
+ subscription,
+ &mut thread_pool,
+ &proxy,
+ );
+
// Update window title
let new_title = application.title();