diff options
author | 2022-05-04 14:25:04 +0200 | |
---|---|---|
committer | 2022-05-04 15:21:07 +0200 | |
commit | f1ec0af5070fe2752967cdc38ed66b8b70882366 (patch) | |
tree | 7bf1d4ce9c761b94a5dc7c40b265f62afb24fedf | |
parent | e24f26c28f09916f04f536a52368dcd1a4f34ed4 (diff) | |
download | iced-f1ec0af5070fe2752967cdc38ed66b8b70882366.tar.gz iced-f1ec0af5070fe2752967cdc38ed66b8b70882366.tar.bz2 iced-f1ec0af5070fe2752967cdc38ed66b8b70882366.zip |
Run `system::information` in a different thread
... since it seems it can block for a couple of seconds.
-rw-r--r-- | native/src/system/action.rs | 7 | ||||
-rw-r--r-- | winit/src/application.rs | 17 | ||||
-rw-r--r-- | winit/src/system.rs | 2 |
3 files changed, 17 insertions, 9 deletions
diff --git a/native/src/system/action.rs b/native/src/system/action.rs index 6dab20a6..dea9536f 100644 --- a/native/src/system/action.rs +++ b/native/src/system/action.rs @@ -1,15 +1,18 @@ use crate::system; use iced_futures::MaybeSend; - use std::fmt; /// An operation to be performed on the system. pub enum Action<T> { /// Query system information and produce `T` with the result. - QueryInformation(Box<dyn Fn(system::Information) -> T>), + QueryInformation(Box<dyn Closure<T>>), } +pub trait Closure<T>: Fn(system::Information) -> T + MaybeSend {} + +impl<T, O> Closure<O> for T where T: Fn(system::Information) -> O + MaybeSend {} + impl<T> Action<T> { /// Maps the output of a system [`Action`] using the provided closure. pub fn map<A>( diff --git a/winit/src/application.rs b/winit/src/application.rs index 421ae398..90b03d56 100644 --- a/winit/src/application.rs +++ b/winit/src/application.rs @@ -583,14 +583,19 @@ pub fn run_command<Message: 'static + std::fmt::Debug + Send, E: Executor>( system::Action::QueryInformation(_tag) => { #[cfg(feature = "system")] { - let information = - crate::system::information(_graphics_info()); + let graphics_info = _graphics_info(); + let proxy = proxy.clone(); - let message = _tag(information); + let _ = std::thread::spawn(move || { + let information = + crate::system::information(graphics_info); - proxy - .send_event(message) - .expect("Send message to event loop"); + let message = _tag(information); + + proxy + .send_event(message) + .expect("Send message to event loop") + }); } } }, diff --git a/winit/src/system.rs b/winit/src/system.rs index fa4ad1a3..0ed61dc9 100644 --- a/winit/src/system.rs +++ b/winit/src/system.rs @@ -6,7 +6,7 @@ use iced_graphics::compositor; /// Query for available system information. pub fn fetch_information<Message>( - f: impl Fn(Information) -> Message + 'static, + f: impl Fn(Information) -> Message + Send + 'static, ) -> Command<Message> { Command::single(command::Action::System(Action::QueryInformation( Box::new(f), |