reports icon indicating copy to clipboard operation
reports copied to clipboard

FB9754295: `applicationShouldHandleReopen` does not work with `@NSApplicationDelegateAdaptor` in a SwiftUI lifecycle app

Open sindresorhus opened this issue 3 years ago • 9 comments

  • 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

Sample project.zip

sindresorhus avatar Nov 12 '21 08:11 sindresorhus

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

markvanwijnen avatar Apr 17 '22 18:04 markvanwijnen

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)

thesauravmitra avatar May 18 '22 13:05 thesauravmitra

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!

xmollv avatar May 30 '22 10:05 xmollv

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
	}
}

Wouter01 avatar Jun 05 '22 12:06 Wouter01

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.

godbout avatar Jul 14 '22 07:07 godbout

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.

this is not working anymore, starting with at least Ventura public beta 2.

godbout avatar Aug 05 '22 13:08 godbout

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.

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.

godbout avatar Aug 06 '22 04:08 godbout

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.

godbout avatar Aug 14 '22 04:08 godbout

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.

godbout avatar Aug 14 '22 14:08 godbout