tauri-apps / tauri-apps/tauri

[feat] [macOS] App focus gain/loss handlers

Open
#11,670 4 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

platform: macOS type: feature request
Dominant language
Rust
Stars
111k
Forks
4k
Avg merge
2d 1h
Merged PRs (30d)
44

Description

Describe the problem

I am building a shortcut system for my desktop app, for triggering actions using hotkeys that aren't in any menu. From what I understand, there are two ways to go about this currently:

  1. Register keydown js listeners in the webview
  2. Register global key listeners on the Rust side using something like rdev.

Both solutions expose a problem on macOS: An application can have focus without having any windows open. For example: without any windows open, I would still like to be able to listen for Command + N, to open a new window.

  1. For JS, I'm registering key listeners directly on the window, but there's no way of listening to non-window key pressed afaik.
  2. For Rust, I can register global OS shortcuts (using rdev), but I would need to unregister these if the app loses focus. On macOS, this is not the same as checking if any window has focus.

In lieu of an actual key event handler solution on the Rust side (one that uses the app event loop itself), I'm inclined to go for solution 2, but I would need to know if my app has focus or not.

Describe the solution you'd like

Ideally, we could install an app focus loss/gain handler on the App builder, something along these lines:

tauri::Builder::default()
    .on_app_focus_gain(|app|{ }))
    .on_app_focus_loss(|app|{ }))
    .run()

These lambda's would directly map to NSApplicationDelegate::applicationDidBecomeActive() and its counterpart.

Alternatives considered

In this particular case I can put Command + N in my main application menu, and it will get triggered, but that is a happy coincidence. This isn't always the case.

Additional context
  • I would like to point out these handlers are useful outside of the scope of a shortcut system. Maybe someone wants to shutdown a thread while the app is inactive, or something like that.
  • Ideally I wouldn't even have to use rdev to listen for hotkeys and use a Tauri-native solution, but that's a different discussion.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start at the Tauri App builder and its macOS application delegate integration, then compare the requested callbacks with NSApplicationDelegate::applicationDidBecomeActive() and its counterpart. Done means app focus gain and loss handlers can be registered on the builder and are invoked when the application becomes active or inactive.

Written by the indexing model from the issue text.

Assessment

Tech stack
macos, rust
Domain
desktop
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.