From 53fdfe52bb30d52653c72ca9f42506c3863dcf4a Mon Sep 17 00:00:00 2001 From: Lucas Fernandes Nogueira Date: Mon, 4 Oct 2021 12:54:38 -0300 Subject: [PATCH] feat(core): expose `run_on_main_thread` API (#2711) --- .changes/run-on-main-thread.md | 5 +++++ .changes/runtime-handle-run-on-main-thread.md | 6 ++++++ core/tauri-runtime-wry/src/lib.rs | 4 ++++ core/tauri-runtime/src/lib.rs | 3 +++ core/tauri/src/app.rs | 9 ++++++++- core/tauri/src/window.rs | 4 ++-- 6 files changed, 28 insertions(+), 3 deletions(-) create mode 100644 .changes/run-on-main-thread.md create mode 100644 .changes/runtime-handle-run-on-main-thread.md diff --git a/.changes/run-on-main-thread.md b/.changes/run-on-main-thread.md new file mode 100644 index 000000000..5af644d68 --- /dev/null +++ b/.changes/run-on-main-thread.md @@ -0,0 +1,5 @@ +--- +"tauri": patch +--- + +Expose `run_on_main_thread` APIs on `Window` and `AppHandle`. diff --git a/.changes/runtime-handle-run-on-main-thread.md b/.changes/runtime-handle-run-on-main-thread.md new file mode 100644 index 000000000..06854acdc --- /dev/null +++ b/.changes/runtime-handle-run-on-main-thread.md @@ -0,0 +1,6 @@ +--- +"tauri-runtime": "patch" +"tauri-runtime-wry": patch +--- + +Added `run_on_main_thread` API on `RuntimeHandle`. diff --git a/core/tauri-runtime-wry/src/lib.rs b/core/tauri-runtime-wry/src/lib.rs index 30fd61205..83b08c64e 100644 --- a/core/tauri-runtime-wry/src/lib.rs +++ b/core/tauri-runtime-wry/src/lib.rs @@ -1528,6 +1528,10 @@ impl RuntimeHandle for WryHandle { Ok(DetachedWindow { label, dispatcher }) } + fn run_on_main_thread(&self, f: F) -> Result<()> { + send_user_message(&self.context, Message::Task(Box::new(f))) + } + #[cfg(all(windows, feature = "system-tray"))] /// Deprecated. (not needed anymore) fn remove_system_tray(&self) -> Result<()> { diff --git a/core/tauri-runtime/src/lib.rs b/core/tauri-runtime/src/lib.rs index e673b2103..cfcf4b406 100644 --- a/core/tauri-runtime/src/lib.rs +++ b/core/tauri-runtime/src/lib.rs @@ -255,6 +255,9 @@ pub trait RuntimeHandle: Debug + Send + Sized + Clone + 'static { pending: PendingWindow, ) -> crate::Result>; + /// Run a task on the main thread. + fn run_on_main_thread(&self, f: F) -> crate::Result<()>; + #[cfg(all(windows, feature = "system-tray"))] #[cfg_attr(doc_cfg, doc(cfg(all(windows, feature = "system-tray"))))] fn remove_system_tray(&self) -> crate::Result<()>; diff --git a/core/tauri/src/app.rs b/core/tauri/src/app.rs index 6d4b20a4a..c7a83a66f 100644 --- a/core/tauri/src/app.rs +++ b/core/tauri/src/app.rs @@ -33,7 +33,6 @@ use std::{ use crate::runtime::menu::{Menu, MenuId, MenuIdRef}; -#[cfg(all(windows, feature = "system-tray"))] use crate::runtime::RuntimeHandle; #[cfg(feature = "system-tray")] use crate::runtime::{Icon, SystemTrayEvent as RuntimeSystemTrayEvent}; @@ -225,6 +224,14 @@ impl<'de, R: Runtime> CommandArg<'de, R> for AppHandle { } impl AppHandle { + /// Runs the given closure on the main thread. + pub fn run_on_main_thread(&self, f: F) -> crate::Result<()> { + self + .runtime_handle + .run_on_main_thread(f) + .map_err(Into::into) + } + /// Removes the system tray. #[cfg(all(windows, feature = "system-tray"))] #[cfg_attr(doc_cfg, doc(cfg(all(windows, feature = "system-tray"))))] diff --git a/core/tauri/src/window.rs b/core/tauri/src/window.rs index bf767489d..8db8fa29d 100644 --- a/core/tauri/src/window.rs +++ b/core/tauri/src/window.rs @@ -206,8 +206,8 @@ impl Window { self.window.dispatcher.clone() } - #[allow(dead_code)] - pub(crate) fn run_on_main_thread(&self, f: F) -> crate::Result<()> { + /// Runs the given closure on the main thread. + pub fn run_on_main_thread(&self, f: F) -> crate::Result<()> { self .window .dispatcher