diff options
author | 2021-09-13 11:49:06 +0700 | |
---|---|---|
committer | 2021-09-13 11:49:06 +0700 | |
commit | 93fec8d273ef8305e1c2456abe0c8ecd7a9d9407 (patch) | |
tree | c0c2445703133293b13657ab4f9c1c936e9cd688 /winit | |
parent | 589f68df0f647d93f2b9dd7bf29cfacb0201351c (diff) | |
parent | 01b945b9814b9dc546e783a6dab66e4f7fe49786 (diff) | |
download | iced-93fec8d273ef8305e1c2456abe0c8ecd7a9d9407.tar.gz iced-93fec8d273ef8305e1c2456abe0c8ecd7a9d9407.tar.bz2 iced-93fec8d273ef8305e1c2456abe0c8ecd7a9d9407.zip |
Merge pull request #1019 from hecrj/command-actions
Platform-specific `Command` implementations
Diffstat (limited to 'winit')
-rw-r--r-- | winit/src/application.rs | 85 | ||||
-rw-r--r-- | winit/src/clipboard.rs | 17 | ||||
-rw-r--r-- | winit/src/lib.rs | 3 | ||||
-rw-r--r-- | winit/src/window.rs | 18 |
4 files changed, 111 insertions, 12 deletions
diff --git a/winit/src/application.rs b/winit/src/application.rs index b683e592..722b4757 100644 --- a/winit/src/application.rs +++ b/winit/src/application.rs @@ -3,11 +3,12 @@ mod state; pub use state::State; +use crate::clipboard::{self, Clipboard}; use crate::conversion; use crate::mouse; use crate::{ - Clipboard, Color, Command, Debug, Error, Executor, Mode, Proxy, Runtime, - Settings, Size, Subscription, + Color, Command, Debug, Error, Executor, Mode, Proxy, Runtime, Settings, + Size, Subscription, }; use iced_futures::futures; @@ -30,7 +31,7 @@ use std::mem::ManuallyDrop; /// /// When using an [`Application`] with the `debug` feature enabled, a debug view /// can be toggled by pressing `F12`. -pub trait Application: Program<Clipboard = Clipboard> { +pub trait Application: Program { /// The data needed to initialize your [`Application`]. type Flags; @@ -127,6 +128,7 @@ where debug.startup_started(); let event_loop = EventLoop::with_user_event(); + let mut proxy = event_loop.create_proxy(); let mut runtime = { let proxy = Proxy::new(event_loop.create_proxy()); @@ -143,9 +145,6 @@ where let subscription = application.subscription(); - runtime.spawn(init_command); - runtime.track(subscription); - let window = settings .window .into_builder( @@ -158,6 +157,17 @@ where .build(&event_loop) .map_err(Error::WindowCreationFailed)?; + let mut clipboard = Clipboard::connect(&window); + + run_command( + init_command, + &mut runtime, + &mut clipboard, + &mut proxy, + &window, + ); + runtime.track(subscription); + let (compositor, renderer) = C::new(compositor_settings, Some(&window))?; let (mut sender, receiver) = mpsc::unbounded(); @@ -167,6 +177,8 @@ where compositor, renderer, runtime, + clipboard, + proxy, debug, receiver, window, @@ -215,6 +227,8 @@ async fn run_instance<A, E, C>( mut compositor: C, mut renderer: A::Renderer, mut runtime: Runtime<E, Proxy<A::Message>, A::Message>, + mut clipboard: Clipboard, + mut proxy: winit::event_loop::EventLoopProxy<A::Message>, mut debug: Debug, mut receiver: mpsc::UnboundedReceiver<winit::event::Event<'_, A::Message>>, window: winit::window::Window, @@ -228,7 +242,6 @@ async fn run_instance<A, E, C>( use winit::event; let mut surface = compositor.create_surface(&window); - let mut clipboard = Clipboard::connect(&window); let mut state = State::new(&application, &window); let mut viewport_version = state.viewport_version(); @@ -289,9 +302,11 @@ async fn run_instance<A, E, C>( update( &mut application, &mut runtime, - &mut debug, &mut clipboard, + &mut proxy, + &mut debug, &mut messages, + &window, ); // Update window @@ -491,20 +506,68 @@ pub fn build_user_interface<'a, A: Application>( pub fn update<A: Application, E: Executor>( application: &mut A, runtime: &mut Runtime<E, Proxy<A::Message>, A::Message>, + clipboard: &mut Clipboard, + proxy: &mut winit::event_loop::EventLoopProxy<A::Message>, debug: &mut Debug, - clipboard: &mut A::Clipboard, messages: &mut Vec<A::Message>, + window: &winit::window::Window, ) { for message in messages.drain(..) { debug.log_message(&message); debug.update_started(); - let command = runtime.enter(|| application.update(message, clipboard)); + let command = runtime.enter(|| application.update(message)); debug.update_finished(); - runtime.spawn(command); + run_command(command, runtime, clipboard, proxy, window); } let subscription = application.subscription(); runtime.track(subscription); } + +/// Runs the actions of a [`Command`]. +pub fn run_command<Message: 'static + std::fmt::Debug + Send, E: Executor>( + command: Command<Message>, + runtime: &mut Runtime<E, Proxy<Message>, Message>, + clipboard: &mut Clipboard, + proxy: &mut winit::event_loop::EventLoopProxy<Message>, + window: &winit::window::Window, +) { + use iced_native::command; + use iced_native::window; + + for action in command.actions() { + match action { + command::Action::Future(future) => { + runtime.spawn(future); + } + command::Action::Clipboard(action) => match action { + clipboard::Action::Read(tag) => { + let message = tag(clipboard.read()); + + proxy + .send_event(message) + .expect("Send message to event loop"); + } + clipboard::Action::Write(contents) => { + clipboard.write(contents); + } + }, + command::Action::Window(action) => match action { + window::Action::Resize { width, height } => { + window.set_inner_size(winit::dpi::LogicalSize { + width, + height, + }); + } + window::Action::Move { x, y } => { + window.set_outer_position(winit::dpi::LogicalPosition { + x, + y, + }); + } + }, + } + } +} diff --git a/winit/src/clipboard.rs b/winit/src/clipboard.rs index ca25c065..1b92b28d 100644 --- a/winit/src/clipboard.rs +++ b/winit/src/clipboard.rs @@ -1,3 +1,8 @@ +//! Access the clipboard. +pub use iced_native::clipboard::Action; + +use crate::command::{self, Command}; + /// A buffer for short-term storage and transfer within and between /// applications. #[allow(missing_debug_implementations)] @@ -52,3 +57,15 @@ impl iced_native::Clipboard for Clipboard { self.write(contents) } } + +/// Read the current contents of the clipboard. +pub fn read<Message>( + f: impl Fn(Option<String>) -> Message + 'static, +) -> Command<Message> { + Command::single(command::Action::Clipboard(Action::Read(Box::new(f)))) +} + +/// Write the given contents to the clipboard. +pub fn write<Message>(contents: String) -> Command<Message> { + Command::single(command::Action::Clipboard(Action::Write(contents))) +} diff --git a/winit/src/lib.rs b/winit/src/lib.rs index 1707846a..30813152 100644 --- a/winit/src/lib.rs +++ b/winit/src/lib.rs @@ -25,10 +25,11 @@ pub use iced_native::*; pub use winit; pub mod application; +pub mod clipboard; pub mod conversion; pub mod settings; +pub mod window; -mod clipboard; mod error; mod mode; mod position; diff --git a/winit/src/window.rs b/winit/src/window.rs new file mode 100644 index 00000000..f3207e68 --- /dev/null +++ b/winit/src/window.rs @@ -0,0 +1,18 @@ +//! Interact with the window of your application. +use crate::command::{self, Command}; +use iced_native::window; + +pub use window::Event; + +/// Resizes the window to the given logical dimensions. +pub fn resize<Message>(width: u32, height: u32) -> Command<Message> { + Command::single(command::Action::Window(window::Action::Resize { + width, + height, + })) +} + +/// Moves a window to the given logical coordinates. +pub fn move_to<Message>(x: i32, y: i32) -> Command<Message> { + Command::single(command::Action::Window(window::Action::Move { x, y })) +} |