Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
xuchaoqian opened this issue Dec 16, 2021 · 9 comments
Closed
Labels
platform: macOS status: upstream This issue is blocked by upstream dependencies and we need to wait or contribute upstream fixes type: feature request

Comments

@xuchaoqian
Copy link
Contributor

xuchaoqian commented Dec 16, 2021

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 xuchaoqian changed the title Provide a hook to capture the dock event Provide a hook to capture the dock events Dec 16, 2021
@amrbashir amrbashir added the status: upstream This issue is blocked by upstream dependencies and we need to wait or contribute upstream fixes label Dec 16, 2021
@amrbashir
Copy link
Member

related to tauri-apps/tao#218

@Leksat
Copy link

Leksat commented Aug 28, 2022

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 tauri-apps/tao#218 will make the latter option possible.

@Leksat
Copy link

Leksat commented Aug 29, 2022

Also found tauri-apps/tao#517 and #4865

@alchaplinsky
Copy link

alchaplinsky commented Dec 3, 2022

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.

@MGough
Copy link
Contributor

MGough commented Mar 21, 2023

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)

@ngalaiko
Copy link

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

@github-project-automation github-project-automation bot moved this to 📋 Backlog in Roadmap Apr 27, 2023
@wusyong wusyong moved this from 📋 Backlog to 📬Proposal in Roadmap Apr 29, 2023
limitedmage added a commit to sourcegraph/sourcegraph-public-snapshot that referenced this issue Jun 20, 2023
Closes https://github.com/sourcegraph/sourcegraph/issues/53756

This allows clicking the dock icon to show the main window if it is
closed but the app is still running.

This is a temporary solution that only works if the app has a single
window. If we need to add more windows in the future, we need to wait
until tauri-apps/tauri#3084 is fixed.

## Test plan

1. Open app
2. Close with red button on top-left of window
3. Click on Cody icon in dock

App main window should be shown again.
ErikaRS pushed a commit to sourcegraph/sourcegraph-public-snapshot that referenced this issue Jun 22, 2023
Closes https://github.com/sourcegraph/sourcegraph/issues/53756

This allows clicking the dock icon to show the main window if it is
closed but the app is still running.

This is a temporary solution that only works if the app has a single
window. If we need to add more windows in the future, we need to wait
until tauri-apps/tauri#3084 is fixed.

## Test plan

1. Open app
2. Close with red button on top-left of window
3. Click on Cody icon in dock

App main window should be shown again.
@yaxiaoliu
Copy link

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

@ghost
Copy link

ghost commented Feb 8, 2024

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

@thothsun
Copy link

thothsun commented Apr 3, 2024

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

@amrbashir amrbashir changed the title Provide a hook to capture the dock events [feat][macOS] Provide a hook to capture the dock events / activate event Apr 30, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
platform: macOS status: upstream This issue is blocked by upstream dependencies and we need to wait or contribute upstream fixes type: feature request
Projects
Status: 📬Proposal
Development

No branches or pull requests

8 participants