Skip to content

Commit 53fdfe5

Browse files
authored
feat(core): expose run_on_main_thread API (#2711)
1 parent 7ed3f3b commit 53fdfe5

6 files changed

Lines changed: 28 additions & 3 deletions

File tree

.changes/run-on-main-thread.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"tauri": patch
3+
---
4+
5+
Expose `run_on_main_thread` APIs on `Window` and `AppHandle`.
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"tauri-runtime": "patch"
3+
"tauri-runtime-wry": patch
4+
---
5+
6+
Added `run_on_main_thread` API on `RuntimeHandle`.

core/tauri-runtime-wry/src/lib.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1528,6 +1528,10 @@ impl RuntimeHandle for WryHandle {
15281528
Ok(DetachedWindow { label, dispatcher })
15291529
}
15301530

1531+
fn run_on_main_thread<F: FnOnce() + Send + 'static>(&self, f: F) -> Result<()> {
1532+
send_user_message(&self.context, Message::Task(Box::new(f)))
1533+
}
1534+
15311535
#[cfg(all(windows, feature = "system-tray"))]
15321536
/// Deprecated. (not needed anymore)
15331537
fn remove_system_tray(&self) -> Result<()> {

core/tauri-runtime/src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,9 @@ pub trait RuntimeHandle: Debug + Send + Sized + Clone + 'static {
255255
pending: PendingWindow<Self::Runtime>,
256256
) -> crate::Result<DetachedWindow<Self::Runtime>>;
257257

258+
/// Run a task on the main thread.
259+
fn run_on_main_thread<F: FnOnce() + Send + 'static>(&self, f: F) -> crate::Result<()>;
260+
258261
#[cfg(all(windows, feature = "system-tray"))]
259262
#[cfg_attr(doc_cfg, doc(cfg(all(windows, feature = "system-tray"))))]
260263
fn remove_system_tray(&self) -> crate::Result<()>;

core/tauri/src/app.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ use std::{
3333

3434
use crate::runtime::menu::{Menu, MenuId, MenuIdRef};
3535

36-
#[cfg(all(windows, feature = "system-tray"))]
3736
use crate::runtime::RuntimeHandle;
3837
#[cfg(feature = "system-tray")]
3938
use crate::runtime::{Icon, SystemTrayEvent as RuntimeSystemTrayEvent};
@@ -225,6 +224,14 @@ impl<'de, R: Runtime> CommandArg<'de, R> for AppHandle<R> {
225224
}
226225

227226
impl<R: Runtime> AppHandle<R> {
227+
/// Runs the given closure on the main thread.
228+
pub fn run_on_main_thread<F: FnOnce() + Send + 'static>(&self, f: F) -> crate::Result<()> {
229+
self
230+
.runtime_handle
231+
.run_on_main_thread(f)
232+
.map_err(Into::into)
233+
}
234+
228235
/// Removes the system tray.
229236
#[cfg(all(windows, feature = "system-tray"))]
230237
#[cfg_attr(doc_cfg, doc(cfg(all(windows, feature = "system-tray"))))]

core/tauri/src/window.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -206,8 +206,8 @@ impl<R: Runtime> Window<R> {
206206
self.window.dispatcher.clone()
207207
}
208208

209-
#[allow(dead_code)]
210-
pub(crate) fn run_on_main_thread<F: FnOnce() + Send + 'static>(&self, f: F) -> crate::Result<()> {
209+
/// Runs the given closure on the main thread.
210+
pub fn run_on_main_thread<F: FnOnce() + Send + 'static>(&self, f: F) -> crate::Result<()> {
211211
self
212212
.window
213213
.dispatcher

0 commit comments

Comments
 (0)