Skip to content

Commit

Permalink
revert "adding result return to menu/tray/window event handlers"
Browse files Browse the repository at this point in the history
  • Loading branch information
amrbashir committed Aug 2, 2023
1 parent c846285 commit 0234a20
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 101 deletions.
74 changes: 27 additions & 47 deletions core/tauri/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,10 @@ use crate::{
#[cfg(target_os = "macos")]
use crate::ActivationPolicy;

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

/// Api exposed on the `ExitRequested` event.
#[derive(Debug)]
Expand Down Expand Up @@ -473,9 +471,7 @@ 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) -> crate::Result<()> + Send + Sync + 'static,
>(
pub fn on_menu_event<F: Fn(&AppHandle<R>, MenuEvent) + Send + Sync + 'static>(
&self,
handler: F,
) {
Expand All @@ -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<R>, TrayIconEvent) -> crate::Result<()> + Send + Sync + 'static,
>(
pub fn on_tray_icon_event<F: Fn(&AppHandle<R>, TrayIconEvent) + Send + Sync + 'static>(
&self,
handler: F,
) {
Expand Down Expand Up @@ -849,7 +843,7 @@ impl<R: Runtime> App<R> {
/// .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")))]
Expand Down Expand Up @@ -878,7 +872,7 @@ impl<R: Runtime> App<R> {
/// .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
Expand All @@ -898,20 +892,14 @@ impl<R: Runtime> App<R> {
/// // 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<F: FnMut(&AppHandle<R>, RunEvent) -> crate::Result<()> + 'static>(
mut self,
mut callback: F,
) {
pub fn run<F: FnMut(&AppHandle<R>, 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 {
Expand Down Expand Up @@ -969,7 +957,7 @@ impl<R: Runtime> App<R> {
&app_handle,
event,
&manager,
Option::<&mut Box<dyn FnMut(&AppHandle<R>, RunEvent) -> crate::Result<()>>>::None,
Option::<&mut Box<dyn FnMut(&AppHandle<R>, RunEvent)>>::None,
)
})
}
Expand Down Expand Up @@ -1322,23 +1310,18 @@ impl<R: Runtime> Builder<R> {
/// # 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<R>) -> crate::Result<()> + Send + Sync + 'static,
>(
pub fn on_window_event<F: Fn(GlobalWindowEvent<R>) + Send + Sync + 'static>(
mut self,
handler: F,
) -> Self {
Expand Down Expand Up @@ -1571,7 +1554,7 @@ impl<R: Runtime> Builder<R> {

/// Runs the configured Tauri application.
pub fn run<A: Assets>(self, context: Context<A>) -> crate::Result<()> {
self.build(context)?.run(|_, _| Ok(()));
self.build(context)?.run(|_, _| {});
Ok(())
}
}
Expand Down Expand Up @@ -1641,10 +1624,7 @@ fn setup<R: Runtime>(app: &mut App<R>) -> crate::Result<()> {
Ok(())
}

fn on_event_loop_event<
R: Runtime,
F: FnMut(&AppHandle<R>, RunEvent) -> crate::Result<()> + 'static,
>(
fn on_event_loop_event<R: Runtime, F: FnMut(&AppHandle<R>, RunEvent) + 'static>(
app_handle: &AppHandle<R>,
event: RuntimeRunEvent<EventLoopMessage>,
manager: &WindowManager<R>,
Expand Down Expand Up @@ -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
Expand All @@ -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);
}
}
}
Expand All @@ -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
Expand All @@ -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);
}
}
}
Expand All @@ -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);
}
}

Expand Down
26 changes: 13 additions & 13 deletions core/tauri/src/menu/builders/menu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(())
/// });
Expand All @@ -45,7 +45,7 @@ pub struct MenuBuilder<'m, R: Runtime, M: Manager<R>> {

impl<'m, R: Runtime, M: Manager<R>> MenuBuilder<'m, R, M> {
/// Create a new menu builder.
pub fn new<S: AsRef<str>>(manager: &'m M) -> Self {
pub fn new(manager: &'m M) -> Self {
Self {
items: Vec::new(),
manager,
Expand Down
24 changes: 12 additions & 12 deletions core/tauri/src/menu/builders/submenu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(())
/// });
Expand Down
1 change: 0 additions & 1 deletion core/tauri/src/test/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,6 @@ mod tests {

app.run(|_app, event| {
println!("{:?}", event);
Ok(())
});
}
}
22 changes: 4 additions & 18 deletions core/tauri/src/tray.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,9 +120,7 @@ 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) -> crate::Result<()> + Sync + Send + 'static,
>(
pub fn on_menu_event<F: Fn(&AppHandle<R>, MenuEvent) + Sync + Send + 'static>(
mut self,
f: F,
) -> Self {
Expand All @@ -131,9 +129,7 @@ impl<R: Runtime> TrayIconBuilder<R> {
}

/// Set a handler for this tray icon events.
pub fn on_tray_event<
F: Fn(&TrayIcon<R>, TrayIconEvent) -> crate::Result<()> + Sync + Send + 'static,
>(
pub fn on_tray_event<F: Fn(&TrayIcon<R>, TrayIconEvent) + Sync + Send + 'static>(
mut self,
f: F,
) -> Self {
Expand Down Expand Up @@ -235,12 +231,7 @@ 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) -> crate::Result<()> + Sync + Send + 'static,
>(
&self,
f: F,
) {
pub fn on_menu_event<F: Fn(&AppHandle<R>, MenuEvent) + Sync + Send + 'static>(&self, f: F) {
self
.app_handle
.manager
Expand All @@ -252,12 +243,7 @@ impl<R: Runtime> TrayIcon<R> {
}

/// Register a handler for this tray icon events.
pub fn on_tray_event<
F: Fn(&TrayIcon<R>, TrayIconEvent) -> crate::Result<()> + Sync + Send + 'static,
>(
&self,
f: F,
) {
pub fn on_tray_event<F: Fn(&TrayIcon<R>, TrayIconEvent) + Sync + Send + 'static>(&self, f: F) {
self
.app_handle
.manager
Expand Down
12 changes: 2 additions & 10 deletions core/tauri/src/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -338,18 +338,14 @@ impl<'a, R: Runtime> WindowBuilder<'a, R> {
/// if event.id == save_menu_item.id() {
/// // save menu item
/// }
///
/// Ok(())
/// })
/// .build()
/// .unwrap();
///
/// Ok(())
/// });
/// ```
pub fn on_menu_event<
F: Fn(&Window<R>, crate::menu::MenuEvent) -> crate::Result<()> + Send + Sync + 'static,
>(
pub fn on_menu_event<F: Fn(&Window<R>, crate::menu::MenuEvent) + Send + Sync + 'static>(
mut self,
f: F,
) -> Self {
Expand Down Expand Up @@ -1197,16 +1193,12 @@ impl<R: Runtime> Window<R> {
/// if event.id == save_menu_item.id() {
/// // save menu item
/// }
///
/// Ok(())
/// });
///
/// Ok(())
/// });
/// ```
pub fn on_menu_event<
F: Fn(&Window<R>, crate::menu::MenuEvent) -> crate::Result<()> + Send + Sync + 'static,
>(
pub fn on_menu_event<F: Fn(&Window<R>, crate::menu::MenuEvent) + Send + Sync + 'static>(
&self,
f: F,
) {
Expand Down

0 comments on commit 0234a20

Please sign in to comment.