tauri
tauri copied to clipboard
[feat][macOS] Provide a hook to capture the dock events / activate event
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
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
related to tauri-apps/tao#218
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.
Also found https://github.com/tauri-apps/tao/pull/517 and https://github.com/tauri-apps/tauri/pull/4865
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.
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 thanks!! for some reason, indeed if I use tauri::AppHandle::hide
it works as expected on MacOS.
This basic function is not supported yet? That's unbelievable.
Is there a way to get the NS'Application object? This makes it possible
Two years have passed. Has there been any progress on this issue?