reports
reports copied to clipboard
FB9754295: `applicationShouldHandleReopen` does not work with `@NSApplicationDelegateAdaptor` in a SwiftUI lifecycle app
- Date: 2021-11-12
- Resolution: Open
- Area: SwiftUI Framework
- OS: macOS 12.0.1
- Type: Incorrect/Unexpected Behavior
Description
applicationShouldHandleReopen
is never called in a SwiftUI lifecycle app.
Steps to reproduce:
- Open the attached sample project and run it.
- Find the app product in Finder, and double-click it (to reopen it while running).
- Notice how the logging in
applicationShouldHandleReopen
is never printed.
Files
I have the same problem. Has this been resolved or is there perhaps a workaround you know of?
Xcode Version 13.3.1 (13E500a) macOS Monterey Version 12.3.1 (21E258) Swift 5
Yeah, I've tried setting the app delegate to my own that calls the SwiftUI.AppDelegate for all its functions, but that isn't enough to behave the same as it somehow.
Any workarounds or solutions for this? (macOS 11.6)
I'm acing the same issue, I've ended up here trying to solve the issue when clicking the dock and the app not launching the minimized window: https://developer.apple.com/forums/thread/706772. Filed FB10028653
if any of our 🍎 is reading this!
Weirdly enough, it does get called when there's no WindowGroup
.
@main
struct TestApp: App {
@NSApplicationDelegateAdaptor(AppDelegate.self) var appDelegate
var body: some Scene {
Settings {
VStack {
Text("Test")
}
}
}
}
final class AppDelegate: NSObject, NSApplicationDelegate {
func applicationShouldHandleReopen(_ sender: NSApplication, hasVisibleWindows flag: Bool) -> Bool {
print("CALLED!")
return true
}
}
confirmed it's working fine without a WindowGroup
. some people just use the WindowGroup
to get keyboard shortcuts (select all, copy, paste etc.) in their background apps. you can do it without a WindowGroup
like this:
var body: some Scene {
Settings {
SettingsView()
}
.commands {
CommandMenu("File") {
Button("Close") {
NSApp.sendAction(#selector(NSWindow.orderOut(_:)), to: nil, from: self)
NSApp.hide(self)
}.keyboardShortcut("w")
}
CommandMenu("Edit") {
Button("Cut") {
NSApp.sendAction(#selector(NSText.cut(_:)), to: nil, from: self)
}.keyboardShortcut("x")
Button("Copy") {
NSApp.sendAction(#selector(NSText.copy(_:)), to: nil, from: self)
}.keyboardShortcut("c")
Button("Paste") {
NSApp.sendAction(#selector(NSText.paste(_:)), to: nil, from: self)
}.keyboardShortcut("v")
Button("Select All") {
NSApp.sendAction(#selector(NSText.selectAll(_:)), to: nil, from: self)
}.keyboardShortcut("a")
}
}
}
the shortcuts will be available everywhere, not just in the Preferences/Settings.
confirmed it's working fine without a
WindowGroup
. some people just use theWindowGroup
to get keyboard shortcuts (select all, copy, paste etc.) in their background apps. you can do it without aWindowGroup
like this:var body: some Scene { Settings { SettingsView() } .commands { CommandMenu("File") { Button("Close") { NSApp.sendAction(#selector(NSWindow.orderOut(_:)), to: nil, from: self) NSApp.hide(self) }.keyboardShortcut("w") } CommandMenu("Edit") { Button("Cut") { NSApp.sendAction(#selector(NSText.cut(_:)), to: nil, from: self) }.keyboardShortcut("x") Button("Copy") { NSApp.sendAction(#selector(NSText.copy(_:)), to: nil, from: self) }.keyboardShortcut("c") Button("Paste") { NSApp.sendAction(#selector(NSText.paste(_:)), to: nil, from: self) }.keyboardShortcut("v") Button("Select All") { NSApp.sendAction(#selector(NSText.selectAll(_:)), to: nil, from: self) }.keyboardShortcut("a") } } }
the shortcuts will be available everywhere, not just in the Preferences/Settings.
this is not working anymore, starting with at least Ventura public beta 2.
confirmed it's working fine without a
WindowGroup
. some people just use theWindowGroup
to get keyboard shortcuts (select all, copy, paste etc.) in their background apps. you can do it without aWindowGroup
like this:var body: some Scene { Settings { SettingsView() } .commands { CommandMenu("File") { Button("Close") { NSApp.sendAction(#selector(NSWindow.orderOut(_:)), to: nil, from: self) NSApp.hide(self) }.keyboardShortcut("w") } CommandMenu("Edit") { Button("Cut") { NSApp.sendAction(#selector(NSText.cut(_:)), to: nil, from: self) }.keyboardShortcut("x") Button("Copy") { NSApp.sendAction(#selector(NSText.copy(_:)), to: nil, from: self) }.keyboardShortcut("c") Button("Paste") { NSApp.sendAction(#selector(NSText.paste(_:)), to: nil, from: self) }.keyboardShortcut("v") Button("Select All") { NSApp.sendAction(#selector(NSText.selectAll(_:)), to: nil, from: self) }.keyboardShortcut("a") } } }
the shortcuts will be available everywhere, not just in the Preferences/Settings.
this is not working anymore, starting with at least Ventura public beta 2.
ok so from my last testing, this is not necessary anymore starting with Xcode Version 14.0 beta 3 (14A5270f). i'm surprised, i thought this would be an OS issue rather than a Swift/SwiftUI/Xcode issue. but it kinda makes sense now that i think about it.
so now only this is necessary:
var body: some Scene {
Settings {
SettingsView()
}
}
and the standard shortcuts will work. tested both for Ventura public beta 2, and Monterey 12.5. finally.
and the standard shortcuts will work. tested both for Ventura public beta 2, and Monterey 12.5. finally.
my bad. not working on 12.5.
the way i personally now handle it. i couldn't find a better way. was not even able to build a custom @SceneBuilder
, but i also haven't spent much time on it:
var body: some Scene {
Settings {
SettingsView()
}
.commands {
CommandMenu("Wooshy") {
if #available(macOS 13, *) {}
else {
Button("Settings...") {
guard AppCore.shared.statusBarController != nil else { return }
StatusBarController.showSettings()
}.keyboardShortcut(",")
}
}
CommandMenu("File") {
if #available(macOS 13, *) {}
else {
Button("Close") {
NSApp.sendAction(#selector(NSWindow.orderOut(_:)), to: nil, from: self)
NSApp.hide(self)
}.keyboardShortcut("w")
}
}
CommandMenu("Edit") {
if #available(macOS 13, *) {}
else {
Button("Cut") {
NSApp.sendAction(#selector(NSText.cut(_:)), to: nil, from: self)
}.keyboardShortcut("x")
Button("Copy") {
NSApp.sendAction(#selector(NSText.copy(_:)), to: nil, from: self)
}.keyboardShortcut("c")
Button("Paste") {
NSApp.sendAction(#selector(NSText.paste(_:)), to: nil, from: self)
}.keyboardShortcut("v")
Button("Select All") {
NSApp.sendAction(#selector(NSText.selectAll(_:)), to: nil, from: self)
}.keyboardShortcut("a")
}
}
}
}
all standard macOS keyboard shortcuts working for Ventura and (at least) Monterey 12.5.