Skip to content

Commit 0a6b6bc

Browse files
improved document opening scenario handling
1 parent feaa002 commit 0a6b6bc

File tree

2 files changed

+79
-44
lines changed

2 files changed

+79
-44
lines changed

Stapler.xcodeproj/project.pbxproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,7 @@
273273
CODE_SIGN_ENTITLEMENTS = Stapler/Stapler.entitlements;
274274
CODE_SIGN_STYLE = Automatic;
275275
COMBINE_HIDPI_IMAGES = YES;
276-
CURRENT_PROJECT_VERSION = 240822;
276+
CURRENT_PROJECT_VERSION = 240823;
277277
DEVELOPMENT_ASSET_PATHS = "\"Stapler/Preview Content\"";
278278
DEVELOPMENT_TEAM = Q3Z639YB49;
279279
ENABLE_HARDENED_RUNTIME = YES;
@@ -305,7 +305,7 @@
305305
CODE_SIGN_ENTITLEMENTS = Stapler/Stapler.entitlements;
306306
CODE_SIGN_STYLE = Automatic;
307307
COMBINE_HIDPI_IMAGES = YES;
308-
CURRENT_PROJECT_VERSION = 240822;
308+
CURRENT_PROJECT_VERSION = 240823;
309309
DEVELOPMENT_ASSET_PATHS = "\"Stapler/Preview Content\"";
310310
DEVELOPMENT_TEAM = Q3Z639YB49;
311311
ENABLE_HARDENED_RUNTIME = YES;

Stapler/StaplerApp.swift

Lines changed: 77 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,15 @@ import UniformTypeIdentifiers
33
import Quartz
44
import os
55

6+
// Define an enum for the different document opening scenarios
7+
enum DocumentOpeningScenario {
8+
case launchedWithDocument
9+
case resumedBySystem
10+
case openedThroughFileMenu
11+
case unknown
12+
}
13+
14+
// Modify the AppDelegate to work with the new AppStateManager
615
class AppDelegate: NSObject, NSApplicationDelegate {
716
func setupDefaultCommandKeyDelay() {
817
if UserDefaults.standard.object(forKey: "CommandKeyDelay") == nil {
@@ -562,54 +571,80 @@ struct StaplerApp: App {
562571
}
563572

564573
func handleDocumentOpening(_ url: URL) {
565-
let currentEvent = NSApplication.shared.currentEvent
566-
let isOpenedFromFinder = currentEvent != nil && currentEvent?.type == .appKitDefined && currentEvent?.subtype.rawValue == NSEvent.EventSubtype.applicationActivated.rawValue
567-
568-
if isOpenedFromFinder {
569-
// Delay the check for Command key to allow it to be released
570-
DispatchQueue.main.asyncAfter(deadline: .now() + commandKeyDelay) {
571-
let commandKeyPressed = NSEvent.modifierFlags.contains(.command)
572-
573-
if !commandKeyPressed {
574-
// Launch all items and close the document
575-
DispatchQueue.main.async {
576-
do {
577-
// Start accessing the security-scoped resource
578-
guard url.startAccessingSecurityScopedResource() else {
579-
logger.error("Failed to access security-scoped resource")
580-
return
581-
}
582-
defer {
583-
url.stopAccessingSecurityScopedResource()
584-
}
585-
586-
let document = try StaplerDocument(contentsOf: url)
587-
let viewModel = StaplerViewModel(document: document)
588-
viewModel.launchAliases(at: IndexSet(integersIn: 0..<document.aliases.count))
589-
590-
// Close the document
591-
if let windowController = NSDocumentController.shared.document(for: url)?.windowControllers.first {
592-
windowController.close()
593-
}
594-
595-
// If this was the only document and the app was just launched, quit the app
596-
if appStateManager.wasJustLaunched && !appStateManager.hasActiveDocument {
597-
DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
598-
NSApp.terminate(nil)
599-
}
600-
}
601-
} catch {
602-
logger.error("Error handling document opening: \(error.localizedDescription)")
603-
}
604-
}
605-
}
606-
}
574+
let scenario = determineOpeningScenario()
575+
576+
switch scenario {
577+
case .launchedWithDocument:
578+
logger.info("Document Opening Scenario: launchedWithDocument")
579+
handleLaunchedWithDocument(url)
580+
case .resumedBySystem:
581+
logger.info("Document Opening Scenario: resumedBySystem")
582+
// Handle resumed by system scenario
583+
break
584+
case .openedThroughFileMenu:
585+
logger.info("Document Opening Scenario: openedThroughFileMenu")
586+
// Handle opened through file menu scenario
587+
break
588+
case .unknown:
589+
logger.info("Document Opening Scenario: unknown")
590+
// Handle unknown scenarios
591+
break
607592
}
608593

609594
// Reset the wasJustLaunched flag
610595
appStateManager.wasJustLaunched = false
611596
}
612597

598+
private func determineOpeningScenario() -> DocumentOpeningScenario {
599+
let currentEvent = NSApplication.shared.currentEvent
600+
let isOpenedFromFinder = currentEvent != nil && currentEvent?.type == .appKitDefined && currentEvent?.subtype.rawValue == NSEvent.EventSubtype.applicationActivated.rawValue
601+
602+
if appStateManager.wasJustLaunched && isOpenedFromFinder {
603+
return .launchedWithDocument
604+
} else if NSApp.isActive {
605+
return .openedThroughFileMenu
606+
} else if ProcessInfo.processInfo.isOperatingSystemAtLeast(OperatingSystemVersion(majorVersion: 10, minorVersion: 15, patchVersion: 0)) {
607+
// Check if the app was resumed by the system (macOS 10.15+)
608+
return .resumedBySystem
609+
} else {
610+
return .unknown
611+
}
612+
}
613+
614+
private func handleLaunchedWithDocument(_ url: URL) {
615+
DispatchQueue.main.asyncAfter(deadline: .now() + commandKeyDelay) {
616+
let commandKeyPressed = NSEvent.modifierFlags.contains(.command)
617+
618+
if !commandKeyPressed {
619+
do {
620+
guard url.startAccessingSecurityScopedResource() else {
621+
logger.error("Failed to access security-scoped resource")
622+
return
623+
}
624+
defer { url.stopAccessingSecurityScopedResource() }
625+
626+
let document = try StaplerDocument(contentsOf: url)
627+
let viewModel = StaplerViewModel(document: document)
628+
viewModel.launchAliases(at: IndexSet(integersIn: 0..<document.aliases.count))
629+
630+
// Close the document
631+
if let windowController = NSDocumentController.shared.document(for: url)?.windowControllers.first {
632+
windowController.close()
633+
}
634+
635+
// If this was the only document and the app was just launched, quit the app
636+
if NSDocumentController.shared.documents.count == 1 {
637+
DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
638+
NSApp.terminate(nil)
639+
}
640+
}
641+
} catch {
642+
logger.error("Error handling document opening: \(error.localizedDescription)")
643+
}
644+
}
645+
}
646+
}
647+
613648
var body: some Scene {
614649
DocumentGroup(newDocument: StaplerDocument()) { file in
615650
ContentView(document: file.$document, hasSelection: $hasSelection)

0 commit comments

Comments
 (0)