@@ -3,6 +3,15 @@ import UniformTypeIdentifiers
33import Quartz
44import 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
615class 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