From de40257cadda79babb0f890655beaec5c2d89cc6 Mon Sep 17 00:00:00 2001 From: Matthew Riley MacPherson Date: Wed, 15 Nov 2023 06:38:01 +0000 Subject: [PATCH] Allow overriding system-reserved shortcuts (#151) --- .../Localization/ar.lproj/Localizable.strings | 1 + .../Localization/cs.lproj/Localizable.strings | 1 + .../Localization/de.lproj/Localizable.strings | 1 + .../Localization/en.lproj/Localizable.strings | 2 ++ .../Localization/es.lproj/Localizable.strings | 1 + .../Localization/fr.lproj/Localizable.strings | 1 + .../Localization/hu.lproj/Localizable.strings | 1 + .../Localization/ja.lproj/Localizable.strings | 1 + .../Localization/ko.lproj/Localizable.strings | 1 + .../Localization/nl.lproj/Localizable.strings | 1 + .../Localization/pt-BR.lproj/Localizable.strings | 1 + .../Localization/ru.lproj/Localizable.strings | 1 + .../zh-Hans.lproj/Localizable.strings | 1 + .../Localization/zh-TW.lproj/Localizable.strings | 1 + Sources/KeyboardShortcuts/RecorderCocoa.swift | 15 +++++++++++---- Sources/KeyboardShortcuts/Utilities.swift | 13 ++++++++++--- 16 files changed, 36 insertions(+), 7 deletions(-) diff --git a/Sources/KeyboardShortcuts/Localization/ar.lproj/Localizable.strings b/Sources/KeyboardShortcuts/Localization/ar.lproj/Localizable.strings index d0fb2cf2..0624561f 100644 --- a/Sources/KeyboardShortcuts/Localization/ar.lproj/Localizable.strings +++ b/Sources/KeyboardShortcuts/Localization/ar.lproj/Localizable.strings @@ -3,3 +3,4 @@ "keyboard_shortcut_used_by_menu_item" = "لا يمكن استخدام اختصار لوحة المفاتيح هذا لأنه مستخدم بواسطة عنصر القائمة “%@”."; "keyboard_shortcut_used_by_system" = "لا يمكن استخدام اختصار لوحة المفاتيح هذا لأنه مستخدم مسبقاً على مستوى النظام."; "keyboard_shortcuts_can_be_changed" = "يمكن تغيير معظم اختصارات لوحة المفاتيح على مستوى النظام في “تفضيلات النظام > لوحة المفاتيح > الاختصارات ”."; +"ok" = "موافق"; diff --git a/Sources/KeyboardShortcuts/Localization/cs.lproj/Localizable.strings b/Sources/KeyboardShortcuts/Localization/cs.lproj/Localizable.strings index c324701f..56a3d39c 100644 --- a/Sources/KeyboardShortcuts/Localization/cs.lproj/Localizable.strings +++ b/Sources/KeyboardShortcuts/Localization/cs.lproj/Localizable.strings @@ -3,3 +3,4 @@ "keyboard_shortcut_used_by_menu_item" = "Tuto zkratku nelze použít, protože je již využívána položkou „%@“"; "keyboard_shortcut_used_by_system" = "Tuto zkratku nelze použít, protože už ji používá systém."; "keyboard_shortcuts_can_be_changed" = "Většinu systémových zkratek můžete změnit v „Nastavení systému › Klávesnice › Klávesové zkratky“."; +"ok" = "OK"; diff --git a/Sources/KeyboardShortcuts/Localization/de.lproj/Localizable.strings b/Sources/KeyboardShortcuts/Localization/de.lproj/Localizable.strings index e39f2f73..63a389ec 100644 --- a/Sources/KeyboardShortcuts/Localization/de.lproj/Localizable.strings +++ b/Sources/KeyboardShortcuts/Localization/de.lproj/Localizable.strings @@ -3,3 +3,4 @@ "keyboard_shortcut_used_by_menu_item" = "Dieses Tastaturkürzel kann nicht verwendet werden, da es bereits durch den Menüpunkt „%@” belegt ist."; "keyboard_shortcut_used_by_system" = "Dieses Tastaturkürzel kann nicht verwendet werden, da es bereits systemweit verwendet wird."; "keyboard_shortcuts_can_be_changed" = "Die meisten systemweiten Tastaturkürzel können unter „Systemeinstellungen › Tastatur › Kurzbefehle“ geändert werden."; +"ok" = "OK"; diff --git a/Sources/KeyboardShortcuts/Localization/en.lproj/Localizable.strings b/Sources/KeyboardShortcuts/Localization/en.lproj/Localizable.strings index af9437d3..15a15e1e 100644 --- a/Sources/KeyboardShortcuts/Localization/en.lproj/Localizable.strings +++ b/Sources/KeyboardShortcuts/Localization/en.lproj/Localizable.strings @@ -3,3 +3,5 @@ "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"; +"ok" = "OK"; diff --git a/Sources/KeyboardShortcuts/Localization/es.lproj/Localizable.strings b/Sources/KeyboardShortcuts/Localization/es.lproj/Localizable.strings index 07194533..e82f7efb 100644 --- a/Sources/KeyboardShortcuts/Localization/es.lproj/Localizable.strings +++ b/Sources/KeyboardShortcuts/Localization/es.lproj/Localizable.strings @@ -3,3 +3,4 @@ "keyboard_shortcut_used_by_menu_item" = "Este atajo de teclado no se puede utilizar ya que está siendo utilizado por el elemento de menú “%@”."; "keyboard_shortcut_used_by_system" = "Este atajo de teclado no se puede utilizar ya que está siendo utilizado por un atajo del sistema operativo."; "keyboard_shortcuts_can_be_changed" = "La mayoría de los atajos de teclado del sistema operativo pueden ser modificados en “Configuración del sistema › Teclado › Atajos de teclado“."; +"ok" = "Aceptar"; diff --git a/Sources/KeyboardShortcuts/Localization/fr.lproj/Localizable.strings b/Sources/KeyboardShortcuts/Localization/fr.lproj/Localizable.strings index f53fff58..ceee5878 100644 --- a/Sources/KeyboardShortcuts/Localization/fr.lproj/Localizable.strings +++ b/Sources/KeyboardShortcuts/Localization/fr.lproj/Localizable.strings @@ -3,3 +3,4 @@ "keyboard_shortcut_used_by_menu_item" = "Ce raccourci ne peut pas être utilisé, car il est déjà utilisé par le menu “%@”."; "keyboard_shortcut_used_by_system" = "Ce raccourci ne peut pas être utilisé car il s'agit d'un raccourci déjà présent dans le système."; "keyboard_shortcuts_can_be_changed" = "La plupart des raccourcis clavier de l'ensemble du système peuvent être modifiés en “Réglages du système… › Clavier › Raccourcis clavier…”."; +"ok" = "OK"; diff --git a/Sources/KeyboardShortcuts/Localization/hu.lproj/Localizable.strings b/Sources/KeyboardShortcuts/Localization/hu.lproj/Localizable.strings index 65e2a018..869345a6 100644 --- a/Sources/KeyboardShortcuts/Localization/hu.lproj/Localizable.strings +++ b/Sources/KeyboardShortcuts/Localization/hu.lproj/Localizable.strings @@ -3,3 +3,4 @@ "keyboard_shortcut_used_by_menu_item" = "Ez a billentyűparancs nem használható mert már a “%@” menü elem használja."; "keyboard_shortcut_used_by_system" = "Ez a billentyűparancs nem használható mert már egy rendszerszintü billentyűparancs."; "keyboard_shortcuts_can_be_changed" = "A legtöbb rendszerszintü billentyűparancsot a “Rendszerbeállítások › Billentyűzet › Billentyűparancsok“ menüben meg lehet változtatni"; +"ok" = "OK"; diff --git a/Sources/KeyboardShortcuts/Localization/ja.lproj/Localizable.strings b/Sources/KeyboardShortcuts/Localization/ja.lproj/Localizable.strings index 3e1212e1..780d6a99 100644 --- a/Sources/KeyboardShortcuts/Localization/ja.lproj/Localizable.strings +++ b/Sources/KeyboardShortcuts/Localization/ja.lproj/Localizable.strings @@ -3,3 +3,4 @@ "keyboard_shortcut_used_by_menu_item" = "このショートカットは既に“%@”で使われており使えません。"; "keyboard_shortcut_used_by_system" = "このショートカットキーは既にシステムで使われており使えません。"; "keyboard_shortcuts_can_be_changed" = "システムで設定されているショートカットキーは“システム設定 › キーボード › キーボードショートカット”で変更できます。"; +"ok" = "OK"; diff --git a/Sources/KeyboardShortcuts/Localization/ko.lproj/Localizable.strings b/Sources/KeyboardShortcuts/Localization/ko.lproj/Localizable.strings index 324a8014..84c970e4 100644 --- a/Sources/KeyboardShortcuts/Localization/ko.lproj/Localizable.strings +++ b/Sources/KeyboardShortcuts/Localization/ko.lproj/Localizable.strings @@ -3,3 +3,4 @@ "keyboard_shortcut_used_by_menu_item" = "이 키보드 단축키는 이미 “%@” 메뉴 항목에 사용되고 있으므로 등록할 수 없습니다."; "keyboard_shortcut_used_by_system" = "이 키보드 단축키는 이미 시스템상에서 사용되고 있으므로 등록할 수 없습니다."; "keyboard_shortcuts_can_be_changed" = "대부분의 시스템 키보드 단축키는 “시스템 설정 › 키보드 › 키보드 단축키”에서 변경 가능합니다."; +"ok" = "확인"; diff --git a/Sources/KeyboardShortcuts/Localization/nl.lproj/Localizable.strings b/Sources/KeyboardShortcuts/Localization/nl.lproj/Localizable.strings index bcfcd6a1..343e4aaa 100644 --- a/Sources/KeyboardShortcuts/Localization/nl.lproj/Localizable.strings +++ b/Sources/KeyboardShortcuts/Localization/nl.lproj/Localizable.strings @@ -3,3 +3,4 @@ "keyboard_shortcut_used_by_menu_item" = "Deze toetscombinatie kan niet worden gebruikt omdat hij al wordt gebruikt door het menu item “%@”."; "keyboard_shortcut_used_by_system" = "Deze toetscombinatie kan niet worden gebruikt omdat hij al door het systeem gebruikt wordt."; "keyboard_shortcuts_can_be_changed" = "De meeste systeem toetscombinaties kunnen onder “Systeeminstellingen… > Toetsenbord > Toetscombinaties…” veranderd worden."; +"ok" = "OK"; diff --git a/Sources/KeyboardShortcuts/Localization/pt-BR.lproj/Localizable.strings b/Sources/KeyboardShortcuts/Localization/pt-BR.lproj/Localizable.strings index 4e1ed4a6..0f1bbe6e 100644 --- a/Sources/KeyboardShortcuts/Localization/pt-BR.lproj/Localizable.strings +++ b/Sources/KeyboardShortcuts/Localization/pt-BR.lproj/Localizable.strings @@ -3,3 +3,4 @@ "keyboard_shortcut_used_by_menu_item" = "Este atalho não pode ser usado porque ele já é usado pelo item de menu “%@”."; "keyboard_shortcut_used_by_system" = "Este atalho não pode ser usado porque ele já é usado por um atalho do sistema."; "keyboard_shortcuts_can_be_changed" = "A maioria dos atalhos do sistema podem ser alterados em “Ajustes do Sistema › Teclado › Atalhos de Teclado”."; +"ok" = "OK"; diff --git a/Sources/KeyboardShortcuts/Localization/ru.lproj/Localizable.strings b/Sources/KeyboardShortcuts/Localization/ru.lproj/Localizable.strings index caa17252..f3b21236 100644 --- a/Sources/KeyboardShortcuts/Localization/ru.lproj/Localizable.strings +++ b/Sources/KeyboardShortcuts/Localization/ru.lproj/Localizable.strings @@ -3,3 +3,4 @@ "keyboard_shortcut_used_by_menu_item" = "Это сочетание клавиш нельзя использовать, так как оно уже используется в пункте меню «%@»."; "keyboard_shortcut_used_by_system" = "Это сочетание клавиш нельзя использовать, поскольку оно является системным."; "keyboard_shortcuts_can_be_changed" = "Большинство системных сочетаний клавиш можно изменить в «Системные настройки › Клавиатура › Сочетания клавиш»."; +"ok" = "OK"; diff --git a/Sources/KeyboardShortcuts/Localization/zh-Hans.lproj/Localizable.strings b/Sources/KeyboardShortcuts/Localization/zh-Hans.lproj/Localizable.strings index dc04b5a4..f71305b6 100644 --- a/Sources/KeyboardShortcuts/Localization/zh-Hans.lproj/Localizable.strings +++ b/Sources/KeyboardShortcuts/Localization/zh-Hans.lproj/Localizable.strings @@ -3,3 +3,4 @@ "keyboard_shortcut_used_by_menu_item" = "当前快捷键无法使用,因为它已被用作菜单项 “%@” 的快捷键。"; "keyboard_shortcut_used_by_system" = "当前快捷键无法使用,因为它已被用作系统快捷键。"; "keyboard_shortcuts_can_be_changed" = "可以在 “系统设置 › 键盘 › 键盘快捷键” 中更改大多数系统快捷键。"; +"ok" = "好"; diff --git a/Sources/KeyboardShortcuts/Localization/zh-TW.lproj/Localizable.strings b/Sources/KeyboardShortcuts/Localization/zh-TW.lproj/Localizable.strings index cf519633..88b4eb76 100644 --- a/Sources/KeyboardShortcuts/Localization/zh-TW.lproj/Localizable.strings +++ b/Sources/KeyboardShortcuts/Localization/zh-TW.lproj/Localizable.strings @@ -3,3 +3,4 @@ "keyboard_shortcut_used_by_menu_item" = "此快速鍵無法使用,因為它已被選單項目「%@」使用。"; "keyboard_shortcut_used_by_system" = "此快速鍵無法使用,因為它已被系統使用。"; "keyboard_shortcuts_can_be_changed" = "可以在「系統設定 › 鍵盤 › 鍵盤快速鍵」中更改大多數的系統快速鍵。"; +"ok" = "好"; diff --git a/Sources/KeyboardShortcuts/RecorderCocoa.swift b/Sources/KeyboardShortcuts/RecorderCocoa.swift index 3084658e..e3b75a23 100644 --- a/Sources/KeyboardShortcuts/RecorderCocoa.swift +++ b/Sources/KeyboardShortcuts/RecorderCocoa.swift @@ -283,19 +283,26 @@ 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: [ + "ok".localized, + "force_use_shortcut".localized + ] ) self.focus() - return nil + // If the user has selected "Use Anyway" in the dialog (the second option), we'll continue setting the keyboard shorcut even though it's reserved by the system. + guard modalResponse == .alertSecondButtonReturn else { + return nil + } } self.stringValue = "\(shortcut)" diff --git a/Sources/KeyboardShortcuts/Utilities.swift b/Sources/KeyboardShortcuts/Utilities.swift index 5ac686e1..4935f3ee 100644 --- a/Sources/KeyboardShortcuts/Utilities.swift +++ b/Sources/KeyboardShortcuts/Utilities.swift @@ -201,13 +201,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) } @@ -215,13 +217,18 @@ 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 }