Fix app behavior on launch (fix #1638)

This commit is contained in:
1024jp 2024-05-22 18:14:51 +09:00
parent 9805f196d2
commit 76e9cf586e
3 changed files with 45 additions and 25 deletions

View File

@ -13,6 +13,7 @@
### Fixes
- Fix an issue on CotEditor 4.8.3 that The Open dialog always shows up on launch despite the setting when the “Reopen windows from last session” option is disabled.
- Fix a typo in the Czech localization.

View File

@ -241,38 +241,27 @@ private enum BundleIdentifier {
func applicationShouldOpenUntitledFile(_ sender: NSApplication) -> Bool {
switch UserDefaults.standard[.noDocumentOnLaunchOption] {
case .untitledDocument:
return true
case .openPanel:
NSDocumentController.shared.openDocument(nil)
return false
case .none:
return false
}
// be called on the open event when iCloud Drive is disabled (2024-05, macOS 14).
// -> Otherwise, NSDocumentController.openDocument(_:) is directly called on launch.
(DocumentController.shared as? DocumentController)?.performOnLaunchAction()
return false
}
func applicationShouldHandleReopen(_ sender: NSApplication, hasVisibleWindows flag: Bool) -> Bool {
// not evaluated on the app launch (only on the *re*-open event)
// only on the *re*-open event (not called on the app launch)
// invoked only on the *re*-open event
switch UserDefaults.standard[.noDocumentOnLaunchOption] {
case .untitledDocument:
if !flag {
// show an untitled document before the app considers displaying the Open dialog
NSDocumentController.shared.newDocument(nil)
}
return false
case .openPanel:
// the original behavior depends on whether iCloud is enabled:
// -> On: the Open dialog
// -> Off: an untitled document
return true // entrust to `.applicationShouldOpenUntitledFile(_:)`
case .none:
return false
// Because the default reopen behavior varies depending on various conditions,
// such as NSQuitAlwaysKeepsWindows, the iCloud Drive availability, etc,
// execute the action directly by self (2024-05, macOS 14).
if !flag {
(DocumentController.shared as? DocumentController)?.performOnLaunchAction(isReopen: true)
}
return false
}

View File

@ -205,6 +205,19 @@ final class DocumentController: NSDocumentController {
}
override func openDocument(_ sender: Any?) {
// be called on the open event when iCloud Drive is enabled (2024-05, macOS 14).
// -> Otherwise, AppDelegate.applicationShouldOpenUntitledFile(_:) is called on launch.
if NSAppleEventManager.shared().isOpenEvent {
return self.performOnLaunchAction()
}
super.openDocument(sender)
}
override func validateUserInterfaceItem(_ item: any NSValidatedUserInterfaceItem) -> Bool {
switch item.action {
@ -251,6 +264,23 @@ final class DocumentController: NSDocumentController {
}
/// Performs the user-defined action on the open/reopen event.
///
/// - Parameter isReopen: Flag to tell whether the event is the reopen event (not affected to the behavior).
func performOnLaunchAction(isReopen: Bool = false) {
switch UserDefaults.standard[.noDocumentOnLaunchOption] {
case .untitledDocument:
self.newDocument(nil)
case .openPanel:
// invoke super to avoid infinite loop
super.openDocument(nil)
case .none:
break
}
}
// MARK: Action Messages