From 671b73a7ab85cf5da98f9d00f2504f2374d94e3f Mon Sep 17 00:00:00 2001 From: Andy Park Date: Sat, 11 Mar 2023 15:07:07 +0900 Subject: [PATCH] when shortcut taken by system, allow user to use anyway --- .../Localization/en.lproj/Localizable.strings | 1 + Sources/KeyboardShortcuts/RecorderCocoa.swift | 16 ++++++++++++---- Sources/KeyboardShortcuts/Utilities.swift | 13 ++++++++++--- 3 files changed, 23 insertions(+), 7 deletions(-) diff --git a/Sources/KeyboardShortcuts/Localization/en.lproj/Localizable.strings b/Sources/KeyboardShortcuts/Localization/en.lproj/Localizable.strings index af9437d..dd4d49b 100644 --- a/Sources/KeyboardShortcuts/Localization/en.lproj/Localizable.strings +++ b/Sources/KeyboardShortcuts/Localization/en.lproj/Localizable.strings @@ -3,3 +3,4 @@ "keyboard_shortcut_used_by_menu_item" = "This keyboard shortcut cannot be used as it’s already used by the “%@” menu item."; "keyboard_shortcut_used_by_system" = "This keyboard shortcut cannot be used as it’s already a system-wide keyboard shortcut."; "keyboard_shortcuts_can_be_changed" = "Most system-wide keyboard shortcuts can be changed in “System Settings › Keyboard › Keyboard Shortcuts”."; +"force_use_shortcut" = "Use Anyway"; \ No newline at end of file diff --git a/Sources/KeyboardShortcuts/RecorderCocoa.swift b/Sources/KeyboardShortcuts/RecorderCocoa.swift index da8b458..c6e83ae 100644 --- a/Sources/KeyboardShortcuts/RecorderCocoa.swift +++ b/Sources/KeyboardShortcuts/RecorderCocoa.swift @@ -248,19 +248,27 @@ extension KeyboardShortcuts { return nil } - guard !shortcut.isTakenBySystem else { + if shortcut.isTakenBySystem { self.blur() - NSAlert.showModal( + let modalResponse = NSAlert.showModal( for: self.window, title: "keyboard_shortcut_used_by_system".localized, // TODO: Add button to offer to open the relevant system settings pane for the user. - message: "keyboard_shortcuts_can_be_changed".localized + message: "keyboard_shortcuts_can_be_changed".localized, + buttonTitles: [ + "force_use_shortcut".localized, + "Cancel" + ] ) self.focus() - return nil + // first button returned from alert means user wants to continue setting this + // shortcut despite it being used by system. + guard modalResponse == .alertFirstButtonReturn else { + return nil + } } self.stringValue = "\(shortcut)" diff --git a/Sources/KeyboardShortcuts/Utilities.swift b/Sources/KeyboardShortcuts/Utilities.swift index 7887a2d..471473b 100644 --- a/Sources/KeyboardShortcuts/Utilities.swift +++ b/Sources/KeyboardShortcuts/Utilities.swift @@ -138,13 +138,15 @@ extension NSAlert { title: String, message: String? = nil, style: Style = .warning, - icon: NSImage? = nil + icon: NSImage? = nil, + buttonTitles: [String] = [] ) -> NSApplication.ModalResponse { NSAlert( title: title, message: message, style: style, - icon: icon + icon: icon, + buttonTitles: buttonTitles ).runModal(for: window) } @@ -152,12 +154,17 @@ extension NSAlert { title: String, message: String? = nil, style: Style = .warning, - icon: NSImage? = nil + icon: NSImage? = nil, + buttonTitles: [String] = [] ) { self.init() self.messageText = title self.alertStyle = style self.icon = icon + + for buttonTitle in buttonTitles { + self.addButton(withTitle: buttonTitle) + } if let message { self.informativeText = message