Skip to content

Commit

Permalink
all tauri handlers return result, make it easier to use ?
Browse files Browse the repository at this point in the history
  • Loading branch information
amrbashir committed Aug 2, 2023
1 parent a9c5244 commit 9d4a676
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 86 deletions.
30 changes: 19 additions & 11 deletions core/tauri/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,12 @@ use crate::{
#[cfg(target_os = "macos")]
use crate::ActivationPolicy;

pub(crate) type GlobalMenuEventListener<T> = Box<dyn Fn(&T, crate::menu::MenuEvent) + Send + Sync>;
pub(crate) type GlobalTrayIconEventListener<R> =
Box<dyn Fn(&AppHandle<R>, crate::tray::TrayIconEvent) + Send + Sync>;
pub(crate) type GlobalWindowEventListener<R> = Box<dyn Fn(GlobalWindowEvent<R>) + Send + Sync>;
pub(crate) type GlobalMenuEventListener<T> =
Box<dyn Fn(&T, crate::menu::MenuEvent) -> crate::Result<()> + Send + Sync>;
pub(crate) type GlobalTrayIconEventListener<T> =
Box<dyn Fn(&T, crate::tray::TrayIconEvent) -> crate::Result<()> + Send + Sync>;
pub(crate) type GlobalWindowEventListener<R> =
Box<dyn Fn(GlobalWindowEvent<R>) -> crate::Result<()> + Send + Sync>;

/// Api exposed on the `ExitRequested` event.
#[derive(Debug)]
Expand Down Expand Up @@ -471,7 +473,9 @@ macro_rules! shared_app_impl {
($app: ty) => {
impl<R: Runtime> $app {
/// Registers a global menu event listener.
pub fn on_menu_event<F: Fn(&AppHandle<R>, MenuEvent) + Send + Sync + 'static>(
pub fn on_menu_event<
F: Fn(&AppHandle<R>, MenuEvent) -> crate::Result<()> + Send + Sync + 'static,
>(
&self,
handler: F,
) {
Expand All @@ -485,7 +489,9 @@ macro_rules! shared_app_impl {
}

/// Registers a global tray icon menu event listener.
pub fn on_tray_icon_event<F: Fn(&AppHandle<R>, TrayIconEvent) + Send + Sync + 'static>(
pub fn on_tray_icon_event<
F: Fn(&AppHandle<R>, TrayIconEvent) -> crate::Result<()> + Send + Sync + 'static,
>(
&self,
handler: F,
) {
Expand Down Expand Up @@ -1317,7 +1323,9 @@ impl<R: Runtime> Builder<R> {
/// });
/// ```
#[must_use]
pub fn on_window_event<F: Fn(GlobalWindowEvent<R>) + Send + Sync + 'static>(
pub fn on_window_event<
F: Fn(GlobalWindowEvent<R>) -> crate::Result<()> + Send + Sync + 'static,
>(
mut self,
handler: F,
) -> Self {
Expand Down Expand Up @@ -1678,7 +1686,7 @@ fn on_event_loop_event<R: Runtime, F: FnMut(&AppHandle<R>, RunEvent) + 'static>(
.lock()
.unwrap()
{
listener(app_handle, e)
let _ = listener(app_handle, e);
}
for (label, listener) in &*app_handle
.manager
Expand All @@ -1688,7 +1696,7 @@ fn on_event_loop_event<R: Runtime, F: FnMut(&AppHandle<R>, RunEvent) + 'static>(
.unwrap()
{
if let Some(w) = app_handle.get_window(label) {
listener(&w, e)
let _ = listener(&w, e);
}
}
}
Expand All @@ -1700,7 +1708,7 @@ fn on_event_loop_event<R: Runtime, F: FnMut(&AppHandle<R>, RunEvent) + 'static>(
.lock()
.unwrap()
{
listener(app_handle, e)
let _ = listener(app_handle, e);
}

for (id, listener) in &*app_handle
Expand All @@ -1712,7 +1720,7 @@ fn on_event_loop_event<R: Runtime, F: FnMut(&AppHandle<R>, RunEvent) + 'static>(
{
if e.id == *id {
if let Some(tray) = app_handle.tray_by_id(*id) {
listener(&tray, e)
let _ = listener(&tray, e);
}
}
}
Expand Down
10 changes: 5 additions & 5 deletions core/tauri/src/manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,6 @@ const WINDOW_FILE_DROP_CANCELLED_EVENT: &str = "tauri://file-drop-cancelled";
pub(crate) const STRINGIFY_IPC_MESSAGE_FN: &str =
include_str!("../scripts/stringify-ipc-message-fn.js");

type TrayIconEventListener<R> = Box<dyn Fn(&TrayIcon<R>, crate::tray::TrayIconEvent) + Send + Sync>;

// we need to proxy the dev server on mobile because we can't use `localhost`, so we use the local IP address
// and we do not get a secure context without the custom protocol that proxies to the dev server
// additionally, we need the custom protocol to inject the initialization scripts on Android
Expand Down Expand Up @@ -241,9 +239,11 @@ pub struct InnerWindowManager<R: Runtime> {
/// Tray icons
pub(crate) tray_icons: Arc<Mutex<Vec<TrayIcon<R>>>>,
/// Global Tray icon event listeners.
pub(crate) global_tray_event_listeners: Arc<Mutex<Vec<GlobalTrayIconEventListener<R>>>>,
pub(crate) global_tray_event_listeners:
Arc<Mutex<Vec<GlobalTrayIconEventListener<AppHandle<R>>>>>,
/// Tray icon event listeners.
pub(crate) tray_event_listeners: Arc<Mutex<HashMap<u32, TrayIconEventListener<R>>>>,
pub(crate) tray_event_listeners:
Arc<Mutex<HashMap<u32, GlobalTrayIconEventListener<TrayIcon<R>>>>>,
/// Responder for invoke calls.
invoke_responder: Arc<InvokeResponder<R>>,
/// The script that initializes the invoke system.
Expand Down Expand Up @@ -1231,7 +1231,7 @@ impl<R: Runtime> WindowManager<R> {
window.on_window_event(move |event| {
let _ = on_window_event(&window_, &manager, event);
for handler in window_event_listeners.iter() {
handler(GlobalWindowEvent {
let _ = handler(GlobalWindowEvent {
window: window_.clone(),
event: event.clone(),
});
Expand Down
105 changes: 58 additions & 47 deletions core/tauri/src/tray.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

//! Tray icon types and utility functions

use crate::app::{GlobalMenuEventListener, GlobalTrayIconEventListener};
use crate::menu::MenuEvent;
use crate::{menu::ContextMenu, runtime::tray as tray_icon};
use crate::{run_main_thread, AppHandle, Icon, Manager, Runtime};
Expand All @@ -30,12 +31,10 @@ pub struct TrayIconAttributes<R: Runtime> {
menu: Option<Box<dyn crate::runtime::menu::ContextMenu>>,

/// Set a handler for menu events
#[allow(clippy::type_complexity)]
on_menu_event: Option<Box<dyn Fn(&AppHandle<R>, MenuEvent) + Send + Sync + 'static>>,
on_menu_event: Option<GlobalMenuEventListener<AppHandle<R>>>,

/// Set a handler for tray icon events
#[allow(clippy::type_complexity)]
on_tray_event: Option<Box<dyn Fn(&TrayIcon<R>, TrayIconEvent) + Send + Sync + 'static>>,
on_tray_event: Option<GlobalTrayIconEventListener<TrayIcon<R>>>,

/// Tray icon
///
Expand Down Expand Up @@ -88,10 +87,8 @@ impl<R: Runtime> From<TrayIconAttributes<R>> for tray_icon::TrayIconAttributes {
/// [`TrayIcon`] builder struct and associated methods.
#[derive(Default)]
pub struct TrayIconBuilder<R: Runtime> {
#[allow(clippy::type_complexity)]
on_menu_event: Option<Box<dyn Fn(&AppHandle<R>, MenuEvent) + Sync + Send + 'static>>,
#[allow(clippy::type_complexity)]
on_tray_event: Option<Box<dyn Fn(&TrayIcon<R>, TrayIconEvent) + Sync + Send + 'static>>,
on_menu_event: Option<GlobalMenuEventListener<AppHandle<R>>>,
on_tray_event: Option<GlobalTrayIconEventListener<TrayIcon<R>>>,
inner: tray_icon::TrayIconBuilder,
}

Expand Down Expand Up @@ -191,7 +188,9 @@ impl<R: Runtime> TrayIconBuilder<R> {
///
/// Note that this handler is called for any menu event,
/// whether it is coming from this window, another window or from the tray icon menu.
pub fn on_menu_event<F: Fn(&AppHandle<R>, MenuEvent) + Sync + Send + 'static>(
pub fn on_menu_event<
F: Fn(&AppHandle<R>, MenuEvent) -> crate::Result<()> + Sync + Send + 'static,
>(
mut self,
f: F,
) -> Self {
Expand All @@ -200,7 +199,9 @@ impl<R: Runtime> TrayIconBuilder<R> {
}

/// Set a handler for this tray icon events.
pub fn on_tray_event<F: Fn(&TrayIcon<R>, TrayIconEvent) + Sync + Send + 'static>(
pub fn on_tray_event<
F: Fn(&TrayIcon<R>, TrayIconEvent) -> crate::Result<()> + Sync + Send + 'static,
>(
mut self,
f: F,
) -> Self {
Expand Down Expand Up @@ -256,41 +257,6 @@ unsafe impl<R: Runtime> Sync for TrayIcon<R> {}
unsafe impl<R: Runtime> Send for TrayIcon<R> {}

impl<R: Runtime> TrayIcon<R> {
fn register(
&self,
app_handle: &AppHandle<R>,
on_menu_event: Option<Box<dyn Fn(&AppHandle<R>, MenuEvent) + Sync + Send + 'static>>,
on_tray_event: Option<Box<dyn Fn(&TrayIcon<R>, TrayIconEvent) + Sync + Send + 'static>>,
) {
if let Some(handler) = on_menu_event {
app_handle
.manager
.inner
.menu_event_listeners
.lock()
.unwrap()
.push(handler);
}

if let Some(handler) = on_tray_event {
app_handle
.manager
.inner
.tray_event_listeners
.lock()
.unwrap()
.insert(self.id, handler);
}

app_handle
.manager
.inner
.tray_icons
.lock()
.unwrap()
.push(self.clone());
}

/// Builds and adds a new tray icon to the system tray.
///
/// ## Platform-specific:
Expand Down Expand Up @@ -336,6 +302,41 @@ impl<R: Runtime> TrayIcon<R> {
Ok(icon)
}

fn register(
&self,
app_handle: &AppHandle<R>,
on_menu_event: Option<GlobalMenuEventListener<AppHandle<R>>>,
on_tray_event: Option<GlobalTrayIconEventListener<TrayIcon<R>>>,
) {
if let Some(handler) = on_menu_event {
app_handle
.manager
.inner
.menu_event_listeners
.lock()
.unwrap()
.push(handler);
}

if let Some(handler) = on_tray_event {
app_handle
.manager
.inner
.tray_event_listeners
.lock()
.unwrap()
.insert(self.id, handler);
}

app_handle
.manager
.inner
.tray_icons
.lock()
.unwrap()
.push(self.clone());
}

/// The application handle associated with this type.
pub fn app_handle(&self) -> AppHandle<R> {
self.app_handle.clone()
Expand All @@ -345,7 +346,12 @@ impl<R: Runtime> TrayIcon<R> {
///
/// Note that this handler is called for any menu event,
/// whether it is coming from this window, another window or from the tray icon menu.
pub fn on_menu_event<F: Fn(&AppHandle<R>, MenuEvent) + Sync + Send + 'static>(&self, f: F) {
pub fn on_menu_event<
F: Fn(&AppHandle<R>, MenuEvent) -> crate::Result<()> + Sync + Send + 'static,
>(
&self,
f: F,
) {
self
.app_handle
.manager
Expand All @@ -357,7 +363,12 @@ impl<R: Runtime> TrayIcon<R> {
}

/// Register a handler for this tray icon events.
pub fn on_tray_event<F: Fn(&TrayIcon<R>, TrayIconEvent) + Sync + Send + 'static>(&self, f: F) {
pub fn on_tray_event<
F: Fn(&TrayIcon<R>, TrayIconEvent) -> crate::Result<()> + Sync + Send + 'static,
>(
&self,
f: F,
) {
self
.app_handle
.manager
Expand Down
14 changes: 8 additions & 6 deletions core/tauri/src/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

//! The Tauri window types and functions.

use crate::menu::ContextMenu;
use crate::{app::GlobalMenuEventListener, menu::ContextMenu};
pub use tauri_utils::{config::Color, WindowEffect as Effect, WindowEffectState as EffectState};
use url::Url;

Expand Down Expand Up @@ -59,8 +59,6 @@ use std::{

pub(crate) type WebResourceRequestHandler = dyn Fn(&HttpRequest, &mut HttpResponse) + Send + Sync;
pub(crate) type NavigationHandler = dyn Fn(&Url) -> bool + Send;
pub(crate) type MenuEventHandler<R> =
Box<dyn Fn(&Window<R>, crate::menu::MenuEvent) + Send + Sync + 'static>;

#[derive(Clone, Serialize)]
struct WindowCreatedEvent {
Expand Down Expand Up @@ -122,7 +120,7 @@ pub struct WindowBuilder<'a, R: Runtime> {
pub(crate) webview_attributes: WebviewAttributes,
web_resource_request_handler: Option<Box<WebResourceRequestHandler>>,
navigation_handler: Option<Box<NavigationHandler>>,
on_menu_event: Option<MenuEventHandler<R>>,
on_menu_event: Option<GlobalMenuEventListener<Window<R>>>,
}

impl<'a, R: Runtime> fmt::Debug for WindowBuilder<'a, R> {
Expand Down Expand Up @@ -347,7 +345,9 @@ impl<'a, R: Runtime> WindowBuilder<'a, R> {
/// Ok(())
/// });
/// ```
pub fn on_menu_event<F: Fn(&Window<R>, crate::menu::MenuEvent) + Send + Sync + 'static>(
pub fn on_menu_event<
F: Fn(&Window<R>, crate::menu::MenuEvent) -> crate::Result<()> + Send + Sync + 'static,
>(
mut self,
f: F,
) -> Self {
Expand Down Expand Up @@ -1204,7 +1204,9 @@ impl<R: Runtime> Window<R> {
/// Ok(())
/// });
/// ```
pub fn on_menu_event<F: Fn(&Window<R>, crate::menu::MenuEvent) + Send + Sync + 'static>(
pub fn on_menu_event<
F: Fn(&Window<R>, crate::menu::MenuEvent) -> crate::Result<()> + Send + Sync + 'static,
>(
&self,
f: F,
) {
Expand Down
Loading

0 comments on commit 9d4a676

Please sign in to comment.