Repository navigation
Description
What happened?
Global keyboard shortcuts (Toggle Recording, Select Content, Select Area) do nothing until the menu bar popover has been opened at least once after launch.
Root cause
registerKeyboardShortcuts() is called from a .task attached to the MenuBarExtra content in BetterCaptureApp.swift:
MenuBarExtra {
MenuBarView(viewModel: viewModel)
.task {
await viewModel.requestPermissionsOnLaunch()
registerKeyboardShortcuts()
}
} label: {
MenuBarLabel(viewModel: viewModel)
}
.menuBarExtraStyle(.window)MenuBarExtra content using .menuBarExtraStyle(.window) is instantiated lazily — the view is not built until the user first clicks the menu bar icon. So the .task never runs at launch, and KeyboardShortcuts.onKeyUp(for:) is never registered.
Evidence
Streaming the app's log from a cold launch, without opening the popover:
$ log stream --level debug --predicate 'subsystem == "com.sattlerjoshua.BetterCapture"'
[com.sattlerjoshua.BetterCapture:PermissionService] Permission states - Screen: ... Microphone: ... Camera: ...
$ rg -c "Requesting permissions" applog.txt
NOT FOUND
PermissionService.init() logs, which proves RecorderViewModel is constructed eagerly at launch. requestPermissionsOnLaunch() — the first line of the same .task — never logs, which proves the .task body did not run.
Steps to Reproduce
- Quit BetterCapture completely
- Launch it (or log in, if it starts at login)
- Without clicking the menu bar icon, press the Toggle Recording shortcut
Nothing happens. Click the menu bar icon once, dismiss the popover, press the shortcut again — it now works for the rest of the session.
What did you expect to happen?
Global shortcuts work from launch, without any interaction with the menu bar popover.
Suggested fix
Move registerKeyboardShortcuts() into AppDelegate.applicationDidFinishLaunching(_:), which runs at launch and does not depend on any view being built.
requestPermissionsOnLaunch() sits in the same .task and has the same problem — despite its name it does not run on launch. Moving it is a user-visible change though, since BetterCapture is LSUIElement: the TCC prompts would appear at login with no visible app window behind them. Worth deciding separately.
Related
The AppDelegate referenced above was added while fixing the same class of bug for the bettercapture:// URL scheme, which was dead for the same reason (onOpenURL attached to the lazily-created MenuBarExtra content).
Activity
fix(shortcuts): register global shortcuts at launch