Skip to content

Commit c4011d0

Browse files
customisable wait for command key check
1 parent 8d4e007 commit c4011d0

File tree

2 files changed

+53
-33
lines changed

2 files changed

+53
-33
lines changed

Stapler.xcodeproj/project.pbxproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,7 @@
289289
"$(inherited)",
290290
"@executable_path/../Frameworks",
291291
);
292-
MARKETING_VERSION = 1.2.2;
292+
MARKETING_VERSION = 1.2.3;
293293
PRODUCT_BUNDLE_IDENTIFIER = com.gingerbeardman.Stapler;
294294
PRODUCT_NAME = "$(TARGET_NAME)";
295295
SWIFT_EMIT_LOC_STRINGS = YES;
@@ -321,7 +321,7 @@
321321
"$(inherited)",
322322
"@executable_path/../Frameworks",
323323
);
324-
MARKETING_VERSION = 1.2.2;
324+
MARKETING_VERSION = 1.2.3;
325325
PRODUCT_BUNDLE_IDENTIFIER = com.gingerbeardman.Stapler;
326326
PRODUCT_NAME = "$(TARGET_NAME)";
327327
SWIFT_EMIT_LOC_STRINGS = YES;

Stapler/StaplerApp.swift

Lines changed: 51 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@ import Quartz
44
import os
55

66
class AppDelegate: NSObject, NSApplicationDelegate {
7+
func setupDefaultCommandKeyDelay() {
8+
if UserDefaults.standard.object(forKey: "CommandKeyDelay") == nil {
9+
UserDefaults.standard.set(100, forKey: "CommandKeyDelay") // Default wait
10+
}
11+
}
12+
713
func applicationShouldTerminate(_ sender: NSApplication) -> NSApplication.TerminateReply {
814
let unsavedDocuments = NSDocumentController.shared.documents.filter { $0.isDocumentEdited }
915

@@ -518,44 +524,51 @@ struct StaplerApp: App {
518524
@StateObject private var appStateManager = AppStateManager()
519525
private let logger = Logger(subsystem: Bundle.main.bundleIdentifier!, category: "info")
520526

527+
// Add a computed property to get the delay from UserDefaults
528+
private var commandKeyDelay: Double {
529+
UserDefaults.standard.double(forKey: "CommandKeyDelay") / 1000.0 // Convert milliseconds to seconds
530+
}
531+
521532
func handleDocumentOpening(_ url: URL) {
522533
let currentEvent = NSApplication.shared.currentEvent
523534
let isOpenedFromFinder = currentEvent != nil && currentEvent?.type == .appKitDefined && currentEvent?.subtype.rawValue == NSEvent.EventSubtype.applicationActivated.rawValue
524535

525536
if isOpenedFromFinder && !appStateManager.wasJustLaunched {
526-
527-
let commandKeyPressed = NSEvent.modifierFlags.contains(.command)
528-
529-
if !commandKeyPressed {
530-
// Launch all items and close the document
531-
DispatchQueue.main.async {
532-
do {
533-
// Start accessing the security-scoped resource
534-
guard url.startAccessingSecurityScopedResource() else {
535-
logger.error("Failed to access security-scoped resource")
536-
return
537-
}
538-
defer {
539-
url.stopAccessingSecurityScopedResource()
540-
}
541-
542-
let document = try StaplerDocument(contentsOf: url)
543-
let viewModel = StaplerViewModel(document: document)
544-
viewModel.launchAliases(at: IndexSet(integersIn: 0..<document.aliases.count))
545-
546-
// Close the document
547-
if let windowController = NSDocumentController.shared.document(for: url)?.windowControllers.first {
548-
windowController.close()
549-
}
550-
551-
// If this was the only document and the app was just launched, quit the app
552-
if appStateManager.wasJustLaunched && !appStateManager.hasActiveDocument {
553-
DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
554-
NSApp.terminate(nil)
537+
// Delay the check for Command key to allow it to be released
538+
DispatchQueue.main.asyncAfter(deadline: .now() + commandKeyDelay) {
539+
let commandKeyPressed = NSEvent.modifierFlags.contains(.command)
540+
541+
if !commandKeyPressed {
542+
// Launch all items and close the document
543+
DispatchQueue.main.async {
544+
do {
545+
// Start accessing the security-scoped resource
546+
guard url.startAccessingSecurityScopedResource() else {
547+
logger.error("Failed to access security-scoped resource")
548+
return
549+
}
550+
defer {
551+
url.stopAccessingSecurityScopedResource()
552+
}
553+
554+
let document = try StaplerDocument(contentsOf: url)
555+
let viewModel = StaplerViewModel(document: document)
556+
viewModel.launchAliases(at: IndexSet(integersIn: 0..<document.aliases.count))
557+
558+
// Close the document
559+
if let windowController = NSDocumentController.shared.document(for: url)?.windowControllers.first {
560+
windowController.close()
561+
}
562+
563+
// If this was the only document and the app was just launched, quit the app
564+
if appStateManager.wasJustLaunched && !appStateManager.hasActiveDocument {
565+
DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
566+
NSApp.terminate(nil)
567+
}
555568
}
569+
} catch {
570+
logger.error("Error handling document opening: \(error.localizedDescription)")
556571
}
557-
} catch {
558-
logger.error("Error handling document opening: \(error.localizedDescription)")
559572
}
560573
}
561574
}
@@ -705,6 +718,13 @@ extension EnvironmentValues {
705718
}
706719
}
707720

721+
extension UserDefaults {
722+
@objc dynamic var commandKeyDelay: Int {
723+
get { integer(forKey: "CommandKeyDelay") }
724+
set { set(newValue, forKey: "CommandKeyDelay") }
725+
}
726+
}
727+
708728
//#Preview {
709729
// ContentView(document: .constant(StaplerDocument()))
710730
// .environmentObject(AppStateManager())

0 commit comments

Comments
 (0)