tauri-apps / tauri

Build smaller, faster, and more secure desktop applications with a web frontend.
https://tauri.app
Apache License 2.0
81.61k stars 2.44k forks source link

[bug] Hiding a window on macOS doesn't unfocus #7540

Open imNel opened 1 year ago

imNel commented 1 year ago

Describe the bug

Running appWindow.hide() doesn't un-focus the window. The default behaviour of cmd+h doesn't have this issue but using .hide() does.

I've tried using setFocus but that only brings the window in focus, not toggle.

Reproduction

This is the code I can use to reproduce the issue:

import { appWindow } from "@tauri-apps/api/window";
await register("CommandOrControl+Shift+Enter", async () => {
  if (await appWindow.isVisible()) {
    await appWindow.hide();
  } else {
    await appWindow.show();
    await appWindow.center();
    await appWindow.setFocus();
  }
});

Expected behavior

No response

Platform and versions

[✔] Environment
    - OS: Mac OS 13.0.1 X64
    ✔ Xcode Command Line Tools: installed
    ✔ rustc: 1.67.1 (d5a82bbd2 2023-02-07)
    ✔ Cargo: 1.67.1 (8ecd4f20a 2023-01-10)
    ✔ rustup: 1.25.2 (17db695f1 2023-02-01)
    ✔ Rust toolchain: stable-aarch64-apple-darwin (default)
    - node: 16.20.0
    - pnpm: 8.6.2
    - yarn: 1.22.19
    - npm: 8.19.4

[-] Packages
    - tauri [RUST]: 1.4.1
    - tauri-build [RUST]: 1.4.0
    - wry [RUST]: 0.24.3
    - tao [RUST]: 0.16.2
    - @tauri-apps/api [NPM]: 1.4.0
    - @tauri-apps/cli [NPM]: 1.4.0

[-] App
    - build-type: bundle
    - CSP: unset
    - distDir: ../dist
    - devPath: http://localhost:1420/
    - framework: SolidJS
    - bundler: Vite

Stack trace

No response

Additional context

No response

YongzeYao commented 7 months ago

Any updates on this issue?

zibo-chen commented 1 month ago

I have the same problem, does anyone know how to solve it?

zibo-chen commented 1 month ago

@imNel How did you solve this problem?

HongyuS commented 3 weeks ago

this issue still not fixed at latest 2.0 rc

amrbashir commented 3 weeks ago

This is not an issue to fix IMO, handing focus off to another window upon hiding is tricky. I don't know much about macOS but probably only the OS knows what last focused window was and even if we know that, I don't think macOS allows an app to focus a window from another app (I could be wrong).

In the past and in different frameworks not necessarily tauri, I've seen some people trick the OS into returning focus by minimizing the window before hiding (works well on Windows and Linux, not sure about macOS). If your app consists of only 1 window, I've also seen some people call app.hide()

I will still leave the issue open, in case someone finds a fix.

zibo-chen commented 3 weeks ago

I found a solution to the issue where appWindow.hide() doesn't un-focus the window. Create an NSPanel window to manage the focus without affecting the current window.

https://github.com/ahkohd/tauri-nspanel