feat(core): expose run_on_main_thread API (#2711)

This commit is contained in:
Lucas Fernandes Nogueira 2021-10-04 12:54:38 -03:00 committed by GitHub
parent 7ed3f3b7e4
commit 53fdfe52bb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 28 additions and 3 deletions

View File

@ -0,0 +1,5 @@
---
"tauri": patch
---
Expose `run_on_main_thread` APIs on `Window` and `AppHandle`.

View File

@ -0,0 +1,6 @@
---
"tauri-runtime": "patch"
"tauri-runtime-wry": patch
---
Added `run_on_main_thread` API on `RuntimeHandle`.

View File

@ -1528,6 +1528,10 @@ impl RuntimeHandle for WryHandle {
Ok(DetachedWindow { label, dispatcher }) Ok(DetachedWindow { label, dispatcher })
} }
fn run_on_main_thread<F: FnOnce() + Send + 'static>(&self, f: F) -> Result<()> {
send_user_message(&self.context, Message::Task(Box::new(f)))
}
#[cfg(all(windows, feature = "system-tray"))] #[cfg(all(windows, feature = "system-tray"))]
/// Deprecated. (not needed anymore) /// Deprecated. (not needed anymore)
fn remove_system_tray(&self) -> Result<()> { fn remove_system_tray(&self) -> Result<()> {

View File

@ -255,6 +255,9 @@ pub trait RuntimeHandle: Debug + Send + Sized + Clone + 'static {
pending: PendingWindow<Self::Runtime>, pending: PendingWindow<Self::Runtime>,
) -> crate::Result<DetachedWindow<Self::Runtime>>; ) -> crate::Result<DetachedWindow<Self::Runtime>>;
/// Run a task on the main thread.
fn run_on_main_thread<F: FnOnce() + Send + 'static>(&self, f: F) -> crate::Result<()>;
#[cfg(all(windows, feature = "system-tray"))] #[cfg(all(windows, feature = "system-tray"))]
#[cfg_attr(doc_cfg, doc(cfg(all(windows, feature = "system-tray"))))] #[cfg_attr(doc_cfg, doc(cfg(all(windows, feature = "system-tray"))))]
fn remove_system_tray(&self) -> crate::Result<()>; fn remove_system_tray(&self) -> crate::Result<()>;

View File

@ -33,7 +33,6 @@ use std::{
use crate::runtime::menu::{Menu, MenuId, MenuIdRef}; use crate::runtime::menu::{Menu, MenuId, MenuIdRef};
#[cfg(all(windows, feature = "system-tray"))]
use crate::runtime::RuntimeHandle; use crate::runtime::RuntimeHandle;
#[cfg(feature = "system-tray")] #[cfg(feature = "system-tray")]
use crate::runtime::{Icon, SystemTrayEvent as RuntimeSystemTrayEvent}; use crate::runtime::{Icon, SystemTrayEvent as RuntimeSystemTrayEvent};
@ -225,6 +224,14 @@ impl<'de, R: Runtime> CommandArg<'de, R> for AppHandle<R> {
} }
impl<R: Runtime> AppHandle<R> { impl<R: Runtime> AppHandle<R> {
/// Runs the given closure on the main thread.
pub fn run_on_main_thread<F: FnOnce() + Send + 'static>(&self, f: F) -> crate::Result<()> {
self
.runtime_handle
.run_on_main_thread(f)
.map_err(Into::into)
}
/// Removes the system tray. /// Removes the system tray.
#[cfg(all(windows, feature = "system-tray"))] #[cfg(all(windows, feature = "system-tray"))]
#[cfg_attr(doc_cfg, doc(cfg(all(windows, feature = "system-tray"))))] #[cfg_attr(doc_cfg, doc(cfg(all(windows, feature = "system-tray"))))]

View File

@ -206,8 +206,8 @@ impl<R: Runtime> Window<R> {
self.window.dispatcher.clone() self.window.dispatcher.clone()
} }
#[allow(dead_code)] /// Runs the given closure on the main thread.
pub(crate) fn run_on_main_thread<F: FnOnce() + Send + 'static>(&self, f: F) -> crate::Result<()> { pub fn run_on_main_thread<F: FnOnce() + Send + 'static>(&self, f: F) -> crate::Result<()> {
self self
.window .window
.dispatcher .dispatcher