From 5919325d9b9ebf86f7358059201e6fc1af2412d8 Mon Sep 17 00:00:00 2001 From: Richard Date: Wed, 15 Jun 2022 20:01:50 -0300 Subject: Internally wrap `Message` with a `Event` enum --- winit/src/multi_window.rs | 43 ++++++++++++++++++++++++++++--------------- 1 file changed, 28 insertions(+), 15 deletions(-) (limited to 'winit') diff --git a/winit/src/multi_window.rs b/winit/src/multi_window.rs index 8a967207..61984b93 100644 --- a/winit/src/multi_window.rs +++ b/winit/src/multi_window.rs @@ -13,8 +13,8 @@ use crate::{ Settings, Size, Subscription, }; -use iced_futures::futures; use iced_futures::futures::channel::mpsc; +use iced_futures::futures::{self, FutureExt}; use iced_graphics::compositor; use iced_graphics::window; use iced_native::user_interface::{self, UserInterface}; @@ -24,6 +24,16 @@ pub use iced_native::application::{Appearance, StyleSheet}; use std::collections::HashMap; use std::mem::ManuallyDrop; +/// TODO(derezzedex) +// This is the an wrapper around the `Application::Message` associate type +// to allows the `shell` to create internal messages, while still having +// the current user specified custom messages. +#[derive(Debug, Clone)] +pub enum Event { + /// An [`Application`] generated message + Application(Message), +} + /// An interactive, native cross-platform application. /// /// This trait is the main entrypoint of Iced. Once implemented, you can run @@ -247,10 +257,12 @@ async fn run_instance( mut application: A, mut compositor: C, mut renderer: A::Renderer, - mut runtime: Runtime, A::Message>, - mut proxy: winit::event_loop::EventLoopProxy, + mut runtime: Runtime>, Event>, + mut proxy: winit::event_loop::EventLoopProxy>, mut debug: Debug, - mut receiver: mpsc::UnboundedReceiver>, + mut receiver: mpsc::UnboundedReceiver< + winit::event::Event<'_, Event>, + >, init_command: Command, windows: HashMap, exit_on_close_request: bool, @@ -292,7 +304,7 @@ async fn run_instance( &windows, || compositor.fetch_information(), ); - runtime.track(application.subscription()); + runtime.track(application.subscription().map(Event::Application)); let mut user_interface = ManuallyDrop::new(build_user_interface( &application, @@ -406,6 +418,7 @@ async fn run_instance( )); } event::Event::UserEvent(message) => { + let Event::Application(message) = message; messages.push(message); } event::Event::RedrawRequested(_) => { @@ -565,9 +578,9 @@ pub fn update( cache: &mut user_interface::Cache, state: &State, renderer: &mut A::Renderer, - runtime: &mut Runtime, A::Message>, + runtime: &mut Runtime>, Event>, clipboard: &mut Clipboard, - proxy: &mut winit::event_loop::EventLoopProxy, + proxy: &mut winit::event_loop::EventLoopProxy>, debug: &mut Debug, messages: &mut Vec, windows: &HashMap, @@ -597,7 +610,7 @@ pub fn update( ); } - let subscription = application.subscription(); + let subscription = application.subscription().map(Event::Application); runtime.track(subscription); } @@ -608,9 +621,9 @@ pub fn run_command( state: &State, renderer: &mut A::Renderer, command: Command, - runtime: &mut Runtime, A::Message>, + runtime: &mut Runtime>, Event>, clipboard: &mut Clipboard, - proxy: &mut winit::event_loop::EventLoopProxy, + proxy: &mut winit::event_loop::EventLoopProxy>, debug: &mut Debug, windows: &HashMap, _graphics_info: impl FnOnce() -> compositor::Information + Copy, @@ -628,14 +641,14 @@ pub fn run_command( for action in command.actions() { match action { command::Action::Future(future) => { - runtime.spawn(future); + runtime.spawn(Box::pin(future.map(Event::Application))); } command::Action::Clipboard(action) => match action { clipboard::Action::Read(tag) => { let message = tag(clipboard.read()); proxy - .send_event(message) + .send_event(Event::Application(message)) .expect("Send message to event loop"); } clipboard::Action::Write(contents) => { @@ -670,7 +683,7 @@ pub fn run_command( }; proxy - .send_event(tag(mode)) + .send_event(Event::Application(tag(mode))) .expect("Send message to event loop"); } }, @@ -688,7 +701,7 @@ pub fn run_command( let message = _tag(information); proxy - .send_event(message) + .send_event(Event::Application(message)) .expect("Send message to event loop") }); } @@ -713,7 +726,7 @@ pub fn run_command( operation::Outcome::None => {} operation::Outcome::Some(message) => { proxy - .send_event(message) + .send_event(Event::Application(message)) .expect("Send message to event loop"); } operation::Outcome::Chain(next) => { -- cgit