From 0234a201681ea2d792329d2f9a2074a8fc32c2a6 Mon Sep 17 00:00:00 2001 From: Amr Bashir Date: Wed, 2 Aug 2023 21:56:15 +0300 Subject: [PATCH] revert "adding result return to menu/tray/window event handlers" --- core/tauri/src/app.rs | 74 +++++++++---------------- core/tauri/src/menu/builders/menu.rs | 26 ++++----- core/tauri/src/menu/builders/submenu.rs | 24 ++++---- core/tauri/src/test/mod.rs | 1 - core/tauri/src/tray.rs | 22 ++------ core/tauri/src/window.rs | 12 +--- 6 files changed, 58 insertions(+), 101 deletions(-) diff --git a/core/tauri/src/app.rs b/core/tauri/src/app.rs index b9eaf73e58f..92324270041 100644 --- a/core/tauri/src/app.rs +++ b/core/tauri/src/app.rs @@ -53,12 +53,10 @@ use crate::{ #[cfg(target_os = "macos")] use crate::ActivationPolicy; -pub(crate) type GlobalMenuEventListener = - Box crate::Result<()> + Send + Sync>; +pub(crate) type GlobalMenuEventListener = Box; pub(crate) type GlobalTrayIconEventListener = - Box crate::Result<()> + Send + Sync>; -pub(crate) type GlobalWindowEventListener = - Box) -> crate::Result<()> + Send + Sync>; + Box; +pub(crate) type GlobalWindowEventListener = Box) + Send + Sync>; /// Api exposed on the `ExitRequested` event. #[derive(Debug)] @@ -473,9 +471,7 @@ macro_rules! shared_app_impl { ($app: ty) => { impl $app { /// Registers a global menu event listener. - pub fn on_menu_event< - F: Fn(&AppHandle, MenuEvent) -> crate::Result<()> + Send + Sync + 'static, - >( + pub fn on_menu_event, MenuEvent) + Send + Sync + 'static>( &self, handler: F, ) { @@ -489,9 +485,7 @@ macro_rules! shared_app_impl { } /// Registers a global tray icon menu event listener. - pub fn on_tray_icon_event< - F: Fn(&AppHandle, TrayIconEvent) -> crate::Result<()> + Send + Sync + 'static, - >( + pub fn on_tray_icon_event, TrayIconEvent) + Send + Sync + 'static>( &self, handler: F, ) { @@ -849,7 +843,7 @@ impl App { /// .expect("error while building tauri application"); /// #[cfg(target_os = "macos")] /// app.set_activation_policy(tauri::ActivationPolicy::Accessory); - /// app.run(|_app_handle, _event| Ok(())); + /// app.run(|_app_handle, _event| {}); /// ``` #[cfg(target_os = "macos")] #[cfg_attr(doc_cfg, doc(cfg(target_os = "macos")))] @@ -878,7 +872,7 @@ impl App { /// .build(tauri::generate_context!("test/fixture/src-tauri/tauri.conf.json")) /// .expect("error while building tauri application"); /// app.set_device_event_filter(tauri::DeviceEventFilter::Always); - /// app.run(|_app_handle, _event| Ok(())); + /// app.run(|_app_handle, _event| {}); /// ``` /// /// [`tao`]: https://crates.io/crates/tao @@ -898,20 +892,14 @@ impl App { /// // on an actual app, remove the string argument /// .build(tauri::generate_context!("test/fixture/src-tauri/tauri.conf.json")) /// .expect("error while building tauri application"); - /// app.run(|_app_handle, event| { - /// match event { - /// tauri::RunEvent::ExitRequested { api, .. } => { - /// api.prevent_exit(); - /// } - /// _ => {} + /// app.run(|_app_handle, event| match event { + /// tauri::RunEvent::ExitRequested { api, .. } => { + /// api.prevent_exit(); /// } - /// Ok(()) + /// _ => {} /// }); /// ``` - pub fn run, RunEvent) -> crate::Result<()> + 'static>( - mut self, - mut callback: F, - ) { + pub fn run, RunEvent) + 'static>(mut self, mut callback: F) { let app_handle = self.handle(); let manager = self.manager.clone(); self.runtime.take().unwrap().run(move |event| match event { @@ -969,7 +957,7 @@ impl App { &app_handle, event, &manager, - Option::<&mut Box, RunEvent) -> crate::Result<()>>>::None, + Option::<&mut Box, RunEvent)>>::None, ) }) } @@ -1322,23 +1310,18 @@ impl Builder { /// # Examples /// ``` /// tauri::Builder::default() - /// .on_window_event(|event| { - /// match event.event() { - /// tauri::WindowEvent::Focused(focused) => { - /// // hide window whenever it loses focus - /// if !focused { - /// event.window().hide().unwrap(); - /// } + /// .on_window_event(|event| match event.event() { + /// tauri::WindowEvent::Focused(focused) => { + /// // hide window whenever it loses focus + /// if !focused { + /// event.window().hide().unwrap(); /// } - /// _ => {} /// } - /// Ok(()) + /// _ => {} /// }); /// ``` #[must_use] - pub fn on_window_event< - F: Fn(GlobalWindowEvent) -> crate::Result<()> + Send + Sync + 'static, - >( + pub fn on_window_event) + Send + Sync + 'static>( mut self, handler: F, ) -> Self { @@ -1571,7 +1554,7 @@ impl Builder { /// Runs the configured Tauri application. pub fn run(self, context: Context) -> crate::Result<()> { - self.build(context)?.run(|_, _| Ok(())); + self.build(context)?.run(|_, _| {}); Ok(()) } } @@ -1641,10 +1624,7 @@ fn setup(app: &mut App) -> crate::Result<()> { Ok(()) } -fn on_event_loop_event< - R: Runtime, - F: FnMut(&AppHandle, RunEvent) -> crate::Result<()> + 'static, ->( +fn on_event_loop_event, RunEvent) + 'static>( app_handle: &AppHandle, event: RuntimeRunEvent, manager: &WindowManager, @@ -1702,7 +1682,7 @@ fn on_event_loop_event< .lock() .unwrap() { - let _ = listener(app_handle, e); + listener(app_handle, e); } for (label, listener) in &*app_handle .manager @@ -1712,7 +1692,7 @@ fn on_event_loop_event< .unwrap() { if let Some(w) = app_handle.get_window(label) { - let _ = listener(&w, e); + listener(&w, e); } } } @@ -1724,7 +1704,7 @@ fn on_event_loop_event< .lock() .unwrap() { - let _ = listener(app_handle, e); + listener(app_handle, e); } for (id, listener) in &*app_handle @@ -1736,7 +1716,7 @@ fn on_event_loop_event< { if e.id == *id { if let Some(tray) = app_handle.tray_by_id(*id) { - let _ = listener(&tray, e); + listener(&tray, e); } } } @@ -1758,7 +1738,7 @@ fn on_event_loop_event< .on_event(app_handle, &event); if let Some(c) = callback { - let _ = c(app_handle, event); + c(app_handle, event); } } diff --git a/core/tauri/src/menu/builders/menu.rs b/core/tauri/src/menu/builders/menu.rs index 60c1bbd38e8..d0935c0b87f 100644 --- a/core/tauri/src/menu/builders/menu.rs +++ b/core/tauri/src/menu/builders/menu.rs @@ -19,21 +19,21 @@ use crate::{menu::*, Icon, Manager, Runtime}; /// # height: 0, /// # }; /// # let icon2 = icon1.clone(); -/// let menu = MenuBuilder::new(&handle, "File") -/// .item(&MenuItem::new(&handle, "MenuItem 1", true, None))? +/// let menu = MenuBuilder::new(&handle) +/// .item(&MenuItem::new(&handle, "MenuItem 1", true, None)) /// .items(&[ /// &CheckMenuItem::new(&handle, "CheckMenuItem 1", true, true, None), /// &IconMenuItem::new(&handle, "IconMenuItem 1", true, Some(icon1), None), -/// ])? -/// .separator()? -/// .cut()? -/// .copy()? -/// .paste()? -/// .separator()? -/// .text("MenuItem 2")? -/// .check("CheckMenuItem 2")? -/// .icon("IconMenuItem 2", icon2)? -/// .build(); +/// ]) +/// .separator() +/// .cut() +/// .copy() +/// .paste() +/// .separator() +/// .text("MenuItem 2") +/// .check("CheckMenuItem 2") +/// .icon("IconMenuItem 2", icon2) +/// .build()?; /// app.set_menu(menu); /// Ok(()) /// }); @@ -45,7 +45,7 @@ pub struct MenuBuilder<'m, R: Runtime, M: Manager> { impl<'m, R: Runtime, M: Manager> MenuBuilder<'m, R, M> { /// Create a new menu builder. - pub fn new>(manager: &'m M) -> Self { + pub fn new(manager: &'m M) -> Self { Self { items: Vec::new(), manager, diff --git a/core/tauri/src/menu/builders/submenu.rs b/core/tauri/src/menu/builders/submenu.rs index 66ab1dd2221..2bace543420 100644 --- a/core/tauri/src/menu/builders/submenu.rs +++ b/core/tauri/src/menu/builders/submenu.rs @@ -21,21 +21,21 @@ use crate::{menu::*, Icon, Manager, Runtime}; /// # let icon2 = icon1.clone(); /// let menu = Menu::new(&handle); /// let submenu = SubmenuBuilder::new(&handle, "File") -/// .item(&MenuItem::new(&handle, "MenuItem 1", true, None))? +/// .item(&MenuItem::new(&handle, "MenuItem 1", true, None)) /// .items(&[ /// &CheckMenuItem::new(&handle, "CheckMenuItem 1", true, true, None), /// &IconMenuItem::new(&handle, "IconMenuItem 1", true, Some(icon1), None), -/// ])? -/// .separator()? -/// .cut()? -/// .copy()? -/// .paste()? -/// .separator()? -/// .text("MenuItem 2")? -/// .check("CheckMenuItem 2")? -/// .icon("IconMenuItem 2", icon2)? -/// .build(); -/// menu.append(&submenu); +/// ]) +/// .separator() +/// .cut() +/// .copy() +/// .paste() +/// .separator() +/// .text("MenuItem 2") +/// .check("CheckMenuItem 2") +/// .icon("IconMenuItem 2", icon2) +/// .build()?; +/// menu.append(&submenu)?; /// app.set_menu(menu); /// Ok(()) /// }); diff --git a/core/tauri/src/test/mod.rs b/core/tauri/src/test/mod.rs index 1a9fafa4c9b..d88af801ea8 100644 --- a/core/tauri/src/test/mod.rs +++ b/core/tauri/src/test/mod.rs @@ -279,7 +279,6 @@ mod tests { app.run(|_app, event| { println!("{:?}", event); - Ok(()) }); } } diff --git a/core/tauri/src/tray.rs b/core/tauri/src/tray.rs index 62ca821cf19..ae89602d497 100644 --- a/core/tauri/src/tray.rs +++ b/core/tauri/src/tray.rs @@ -120,9 +120,7 @@ impl TrayIconBuilder { /// /// 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, MenuEvent) -> crate::Result<()> + Sync + Send + 'static, - >( + pub fn on_menu_event, MenuEvent) + Sync + Send + 'static>( mut self, f: F, ) -> Self { @@ -131,9 +129,7 @@ impl TrayIconBuilder { } /// Set a handler for this tray icon events. - pub fn on_tray_event< - F: Fn(&TrayIcon, TrayIconEvent) -> crate::Result<()> + Sync + Send + 'static, - >( + pub fn on_tray_event, TrayIconEvent) + Sync + Send + 'static>( mut self, f: F, ) -> Self { @@ -235,12 +231,7 @@ impl TrayIcon { /// /// 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, MenuEvent) -> crate::Result<()> + Sync + Send + 'static, - >( - &self, - f: F, - ) { + pub fn on_menu_event, MenuEvent) + Sync + Send + 'static>(&self, f: F) { self .app_handle .manager @@ -252,12 +243,7 @@ impl TrayIcon { } /// Register a handler for this tray icon events. - pub fn on_tray_event< - F: Fn(&TrayIcon, TrayIconEvent) -> crate::Result<()> + Sync + Send + 'static, - >( - &self, - f: F, - ) { + pub fn on_tray_event, TrayIconEvent) + Sync + Send + 'static>(&self, f: F) { self .app_handle .manager diff --git a/core/tauri/src/window.rs b/core/tauri/src/window.rs index 6061c3a13d8..474db23b1ea 100644 --- a/core/tauri/src/window.rs +++ b/core/tauri/src/window.rs @@ -338,8 +338,6 @@ impl<'a, R: Runtime> WindowBuilder<'a, R> { /// if event.id == save_menu_item.id() { /// // save menu item /// } - /// - /// Ok(()) /// }) /// .build() /// .unwrap(); @@ -347,9 +345,7 @@ impl<'a, R: Runtime> WindowBuilder<'a, R> { /// Ok(()) /// }); /// ``` - pub fn on_menu_event< - F: Fn(&Window, crate::menu::MenuEvent) -> crate::Result<()> + Send + Sync + 'static, - >( + pub fn on_menu_event, crate::menu::MenuEvent) + Send + Sync + 'static>( mut self, f: F, ) -> Self { @@ -1197,16 +1193,12 @@ impl Window { /// if event.id == save_menu_item.id() { /// // save menu item /// } - /// - /// Ok(()) /// }); /// /// Ok(()) /// }); /// ``` - pub fn on_menu_event< - F: Fn(&Window, crate::menu::MenuEvent) -> crate::Result<()> + Send + Sync + 'static, - >( + pub fn on_menu_event, crate::menu::MenuEvent) + Send + Sync + 'static>( &self, f: F, ) {