tauri icon indicating copy to clipboard operation
tauri copied to clipboard

[feat][macOS] Provide a hook to capture the dock events / activate event

Open xuchaoqian opened this issue 3 years ago • 9 comments

Is your feature request related to a problem? Please describe. I use api.prevent_close() and window.hide() to prevent the app to exit. And then I want to show the window again by left-clicking the icon on the dock, but it does not work. The question is how to capture the left-click events?

Describe the solution you'd like Provide a hook to capture theses events, like this:

  app.run(|app_handle, event| match event {
    DockEvent::LeftClick => {}
    _ => {}
)

Describe alternatives you've considered None.

Additional context image

Cargo tauri info Operating System - Mac OS, version 11.6.1 X64

Node.js environment Node.js - 14.18.1 @tauri-apps/cli - 1.0.0-beta.10 @tauri-apps/api - 1.0.0-beta.8

Global packages npm - 6.14.15 yarn - 1.22.17

Rust environment rustc - 1.56.1 cargo - 1.56.0

App directory structure /bin /node_modules /webpack /public /src-web /src-tauri /change-logs /build /.git /src

App tauri.rs - 1.0.0-beta.8 build-type - bundle CSP - default-src blob: data: filesystem: ws: wss: http: https: tauri: 'unsafe-eval' 'unsafe-inline' 'self' img-src: 'self' distDir - ../build devPath - http://localhost:9091 framework - React bundler - Webpack

xuchaoqian avatar Dec 16 '21 16:12 xuchaoqian

related to tauri-apps/tao#218

amrbashir avatar Dec 16 '21 16:12 amrbashir

Maybe just DockEvent::LeftClick is not enough. I have an Electron app where I use electronApp.hide().

Once the app is hidden, I can bring the window back by clicking the dock icon, or by switching to the app with option+tab. I hope https://github.com/tauri-apps/tao/issues/218 will make the latter option possible.

Leksat avatar Aug 28 '22 09:08 Leksat

Also found https://github.com/tauri-apps/tao/pull/517 and https://github.com/tauri-apps/tauri/pull/4865

Leksat avatar Aug 29 '22 03:08 Leksat

Is there any plan for working on this one? This is a pretty common feature in MacOs: clicking app icon in the dock should open an app window.

alchaplinsky avatar Dec 03 '22 23:12 alchaplinsky

I use api.prevent_close() and window.hide() to prevent the app to exit. And then I want to show the window again by left-clicking the icon on the dock, but it does happen. The question is how to capture the left-click events?

This may not close the issue as it's not about capturing left click on dock events. So obviously this isn't the solution you've described as the one you'd like, but I think I have implemented the behaviour you've described.

I've got an app which needs to stay running when the window is closed as we have some long running processes. A bit like docker desktop. On MacOS we want the app to stay in the dock, and the window to reappear if the item in the dock is left clicked.

On Windows we don't want the application to be visible on the menu bar unless the window is open, as we have a system tray icon instead, so our logic differs slightly:

  tauri::Builder::default()
    .on_window_event(|event| match event.event() {
      tauri::WindowEvent::CloseRequested { api, .. } => {
        #[cfg(not(target_os = "macos"))] {
          event.window().hide().unwrap();
        }

        #[cfg(target_os = "macos")] {
          tauri::AppHandle::hide(&event.window().app_handle()).unwrap();
        }
        api.prevent_close();
      }
      _ => {}
    })

Excuse the 'unwrap's - It's easier to show them here for brevity, but longer term I'd suggest doing something like mapping them and handling the errors.

I've found that this hiding was enough, as the other events appear to be handled by default in a satisfactory manner. I don't try to manually 'show' the window (except if prompted via our system tray)

MGough avatar Mar 21 '23 11:03 MGough

@MGough thanks!! for some reason, indeed if I use tauri::AppHandle::hide it works as expected on MacOS.

ngalaiko avatar Apr 27 '23 11:04 ngalaiko

This basic function is not supported yet? That's unbelievable.

yaxiaoliu avatar Nov 23 '23 03:11 yaxiaoliu

Is there a way to get the NS'Application object? This makes it possible

ghost avatar Feb 08 '24 13:02 ghost

Two years have passed. Has there been any progress on this issue?

thothsun avatar Apr 03 '24 01:04 thothsun