Skip to content

Commit

Permalink
additional scenario
Browse files Browse the repository at this point in the history
  • Loading branch information
gingerbeardman committed Aug 23, 2024
1 parent 0a6b6bc commit 9fad23c
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 47 deletions.
16 changes: 8 additions & 8 deletions Stapler.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -281,10 +281,10 @@
GENERATE_INFOPLIST_FILE = YES;
INFOPLIST_FILE = Stapler/Info.plist;
INFOPLIST_KEY_LSApplicationCategoryType = "public.app-category.productivity";
INFOPLIST_KEY_NSDesktopFolderUsageDescription = "This app needs access to your Desktop folder to open and manage files.";
INFOPLIST_KEY_NSDocumentsFolderUsageDescription = "This app needs access to your Documents folder to open and manage files.";
INFOPLIST_KEY_NSDownloadsFolderUsageDescription = "This app needs access to your Downloads folder to open and manage files.";
INFOPLIST_KEY_NSHumanReadableCopyright = "Copyright © 2024 Matt Sephton.\nAll rights reserved.";
INFOPLIST_KEY_NSDesktopFolderUsageDescription = "Stapler needs access to your files to read aliases and create and manage its own files.";
INFOPLIST_KEY_NSDocumentsFolderUsageDescription = "Stapler needs access to your files to read aliases and create and manage its own files.";
INFOPLIST_KEY_NSDownloadsFolderUsageDescription = "Stapler needs access to your files to read aliases and create and manage its own files.";
INFOPLIST_KEY_NSHumanReadableCopyright = "Copyright © 2024 Matt Sephton";
LD_RUNPATH_SEARCH_PATHS = (
"$(inherited)",
"@executable_path/../Frameworks",
Expand Down Expand Up @@ -313,10 +313,10 @@
GENERATE_INFOPLIST_FILE = YES;
INFOPLIST_FILE = Stapler/Info.plist;
INFOPLIST_KEY_LSApplicationCategoryType = "public.app-category.productivity";
INFOPLIST_KEY_NSDesktopFolderUsageDescription = "This app needs access to your Desktop folder to open and manage files.";
INFOPLIST_KEY_NSDocumentsFolderUsageDescription = "This app needs access to your Documents folder to open and manage files.";
INFOPLIST_KEY_NSDownloadsFolderUsageDescription = "This app needs access to your Downloads folder to open and manage files.";
INFOPLIST_KEY_NSHumanReadableCopyright = "Copyright © 2024 Matt Sephton.\nAll rights reserved.";
INFOPLIST_KEY_NSDesktopFolderUsageDescription = "Stapler needs access to your files to read aliases and create and manage its own files.";
INFOPLIST_KEY_NSDocumentsFolderUsageDescription = "Stapler needs access to your files to read aliases and create and manage its own files.";
INFOPLIST_KEY_NSDownloadsFolderUsageDescription = "Stapler needs access to your files to read aliases and create and manage its own files.";
INFOPLIST_KEY_NSHumanReadableCopyright = "Copyright © 2024 Matt Sephton";
LD_RUNPATH_SEARCH_PATHS = (
"$(inherited)",
"@executable_path/../Frameworks",
Expand Down
6 changes: 0 additions & 6 deletions Stapler/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,6 @@
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>NSDesktopFolderUsageDescription</key>
<string>Stapler needs access to your files to read aliases and create and manage its own files.</string>
<key>NSDocumentsFolderUsageDescription</key>
<string>Stapler needs access to your files to read aliases and create and manage its own files.</string>
<key>NSDownloadsFolderUsageDescription</key>
<string>Stapler needs access to your files to read aliases and create and manage its own files.</string>
<key>CFBundleDocumentTypes</key>
<array>
<dict>
Expand Down
100 changes: 67 additions & 33 deletions Stapler/StaplerApp.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,15 @@ enum DocumentOpeningScenario {
case launchedWithDocument
case resumedBySystem
case openedThroughFileMenu
case openedFromFinderWhileRunning
case unknown
}

// Modify the AppDelegate to work with the new AppStateManager
class AppDelegate: NSObject, NSApplicationDelegate {
func setupDefaultCommandKeyDelay() {
if UserDefaults.standard.object(forKey: "CommandKeyDelay") == nil {
UserDefaults.standard.set(100, forKey: "CommandKeyDelay") // Default wait 100ms
UserDefaults.standard.set(0, forKey: "CommandKeyDelay") // Default wait 0ms
}
}

Expand Down Expand Up @@ -585,6 +586,10 @@ struct StaplerApp: App {
logger.info("Document Opening Scenario: openedThroughFileMenu")
// Handle opened through file menu scenario
break
case .openedFromFinderWhileRunning:
logger.info("Document Opening Scenario: openedFromFinderWhileRunning")
// Handle opened through Finder whilst running scenario
handleOpenedFromFinderWhileRunning(url)
case .unknown:
logger.info("Document Opening Scenario: unknown")
// Handle unknown scenarios
Expand All @@ -601,6 +606,8 @@ struct StaplerApp: App {

if appStateManager.wasJustLaunched && isOpenedFromFinder {
return .launchedWithDocument
} else if isOpenedFromFinder && NSApp.isActive {
return .openedFromFinderWhileRunning
} else if NSApp.isActive {
return .openedThroughFileMenu
} else if ProcessInfo.processInfo.isOperatingSystemAtLeast(OperatingSystemVersion(majorVersion: 10, minorVersion: 15, patchVersion: 0)) {
Expand All @@ -612,38 +619,65 @@ struct StaplerApp: App {
}

private func handleLaunchedWithDocument(_ url: URL) {
DispatchQueue.main.asyncAfter(deadline: .now() + commandKeyDelay) {
let commandKeyPressed = NSEvent.modifierFlags.contains(.command)

if !commandKeyPressed {
do {
guard url.startAccessingSecurityScopedResource() else {
logger.error("Failed to access security-scoped resource")
return
}
defer { url.stopAccessingSecurityScopedResource() }

let document = try StaplerDocument(contentsOf: url)
let viewModel = StaplerViewModel(document: document)
viewModel.launchAliases(at: IndexSet(integersIn: 0..<document.aliases.count))

// Close the document
if let windowController = NSDocumentController.shared.document(for: url)?.windowControllers.first {
windowController.close()
}

// If this was the only document and the app was just launched, quit the app
if NSDocumentController.shared.documents.count == 1 {
DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
NSApp.terminate(nil)
}
}
} catch {
logger.error("Error handling document opening: \(error.localizedDescription)")
}
}
}
}
DispatchQueue.main.asyncAfter(deadline: .now() + commandKeyDelay) {
let commandKeyPressed = NSEvent.modifierFlags.contains(.command)

if !commandKeyPressed {
do {
guard url.startAccessingSecurityScopedResource() else {
logger.error("Failed to access security-scoped resource")
return
}
defer { url.stopAccessingSecurityScopedResource() }

let document = try StaplerDocument(contentsOf: url)
let viewModel = StaplerViewModel(document: document)
viewModel.launchAliases(at: IndexSet(integersIn: 0..<document.aliases.count))

// Close the document
if let windowController = NSDocumentController.shared.document(for: url)?.windowControllers.first {
windowController.close()
}

// If this was the only document and the app was just launched, quit the app
if NSDocumentController.shared.documents.count == 1 {
DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
NSApp.terminate(nil)
}
}
} catch {
logger.error("Error handling document opening: \(error.localizedDescription)")
}
}
}
}

private func handleOpenedFromFinderWhileRunning(_ url: URL) {
DispatchQueue.main.asyncAfter(deadline: .now() + commandKeyDelay) {
let commandKeyPressed = NSEvent.modifierFlags.contains(.command)

if !commandKeyPressed {
do {
guard url.startAccessingSecurityScopedResource() else {
logger.error("Failed to access security-scoped resource")
return
}
defer { url.stopAccessingSecurityScopedResource() }

let document = try StaplerDocument(contentsOf: url)
let viewModel = StaplerViewModel(document: document)
viewModel.launchAliases(at: IndexSet(integersIn: 0..<document.aliases.count))

// Close the document
if let windowController = NSDocumentController.shared.document(for: url)?.windowControllers.first {
windowController.close()
}
} catch {
logger.error("Error handling document opening: \(error.localizedDescription)")
}
}
}
}

var body: some Scene {
DocumentGroup(newDocument: StaplerDocument()) { file in
Expand Down

0 comments on commit 9fad23c

Please sign in to comment.