Skip to content

Commit 6a8bdad

Browse files
authored
Merge branch 'main' into main
2 parents 7d6ad8f + 7ecc38b commit 6a8bdad

File tree

9 files changed

+654
-154
lines changed

9 files changed

+654
-154
lines changed

Example/KeyboardShortcutsExample/MainScreen.swift

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,16 @@ private struct DynamicShortcut: View {
5757
Divider()
5858
DynamicShortcutRecorder(name: $shortcut.name, isPressed: $isPressed)
5959
}
60+
Divider()
61+
.padding(.vertical)
62+
Button("Reset All") {
63+
KeyboardShortcuts.resetAll()
64+
}
6065
}
6166
.frame(maxWidth: 300)
6267
.padding()
6368
.padding(.bottom, 20)
64-
.onChange(of: shortcut) { oldValue, newValue in
69+
.onChange(of: shortcut, initial: true) { oldValue, newValue in
6570
onShortcutChange(oldValue: oldValue, newValue: newValue)
6671
}
6772
}
@@ -98,9 +103,6 @@ private struct DoubleShortcut: View {
98103
.offset(x: 90)
99104
}
100105
Spacer()
101-
Button("Reset All") {
102-
KeyboardShortcuts.reset(.testShortcut1, .testShortcut2)
103-
}
104106
}
105107
.offset(x: -40)
106108
.frame(maxWidth: 300)

Example/KeyboardShortcutsExample/Utilities.swift

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import SwiftUI
22

3-
43
@MainActor
54
final class CallbackMenuItem: NSMenuItem {
65
private static var validateCallback: ((NSMenuItem) -> Bool)?
@@ -50,7 +49,6 @@ extension CallbackMenuItem: NSMenuItemValidation {
5049
}
5150
}
5251

53-
5452
extension NSMenuItem {
5553
convenience init(
5654
_ title: String,
@@ -81,7 +79,6 @@ extension NSMenuItem {
8179
}
8280
}
8381

84-
8582
extension NSMenu {
8683
@MainActor
8784
@discardableResult

Sources/KeyboardShortcuts/KeyboardShortcuts.swift

Lines changed: 42 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,19 @@ public enum KeyboardShortcuts {
5858
}
5959
}
6060

61+
static var allNames: Set<Name> {
62+
UserDefaults.standard.dictionaryRepresentation()
63+
.compactMap { key, _ in
64+
guard key.hasPrefix(userDefaultsPrefix) else {
65+
return nil
66+
}
67+
68+
let name = key.replacingPrefix(userDefaultsPrefix, with: "")
69+
return .init(name)
70+
}
71+
.toSet()
72+
}
73+
6174
/**
6275
Enable keyboard shortcuts to work even when an `NSMenu` is open by setting this property when the menu opens and closes.
6376

@@ -225,11 +238,8 @@ public enum KeyboardShortcuts {
225238
var body: some View {
226239
VStack {
227240
// …
228-
Button("Reset All") {
229-
KeyboardShortcuts.reset(
230-
.toggleUnicornMode,
231-
.showRainbow
232-
)
241+
Button("Reset") {
242+
KeyboardShortcuts.reset(.toggleUnicornMode)
233243
}
234244
}
235245
}
@@ -255,11 +265,8 @@ public enum KeyboardShortcuts {
255265
var body: some View {
256266
VStack {
257267
// …
258-
Button("Reset All") {
259-
KeyboardShortcuts.reset(
260-
.toggleUnicornMode,
261-
.showRainbow
262-
)
268+
Button("Reset") {
269+
KeyboardShortcuts.reset(.toggleUnicornMode)
263270
}
264271
}
265272
}
@@ -270,6 +277,31 @@ public enum KeyboardShortcuts {
270277
reset(names)
271278
}
272279

280+
/**
281+
Reset the keyboard shortcut for all the names.
282+
283+
Unlike `reset(…)`, this resets all the shortcuts to `nil`, not the `defaultValue`.
284+
285+
```swift
286+
import SwiftUI
287+
import KeyboardShortcuts
288+
289+
struct SettingsScreen: View {
290+
var body: some View {
291+
VStack {
292+
// …
293+
Button("Reset All") {
294+
KeyboardShortcuts.resetAll()
295+
}
296+
}
297+
}
298+
}
299+
```
300+
*/
301+
public static func resetAll() {
302+
reset(allNames.toArray())
303+
}
304+
273305
/**
274306
Set the keyboard shortcut for a name.
275307

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
"record_shortcut" = "设置快捷键";
22
"press_shortcut" = "按下快捷键";
3-
"keyboard_shortcut_used_by_menu_item" = "当前快捷键无法使用,因为它已被用作菜单项 “%@” 的快捷键。";
3+
"keyboard_shortcut_used_by_menu_item" = "当前快捷键无法使用,因为它已被用作菜单项“%@”的快捷键。";
44
"keyboard_shortcut_used_by_system" = "当前快捷键无法使用,因为它已被用作系统快捷键。";
5-
"keyboard_shortcuts_can_be_changed" = "可以在 “系统设置 › 键盘 › 键盘快捷键” 中更改大多数系统快捷键。";
6-
"keyboard_shortcut_disallowed" = "Option 修飾鍵必須與 Command 或 Control 組合使用。";
5+
"keyboard_shortcuts_can_be_changed" = "可以在“系统设置 › 键盘 › 键盘快捷键”中更改大多数系统快捷键。";
6+
"keyboard_shortcut_disallowed" = "Option键必须与Command键或Control键组合使用。";
77
"force_use_shortcut" = "强制使用";
88
"ok" = "好";
99
"space_key" = "空格";

Sources/KeyboardShortcuts/Localization/zh-TW.lproj/Localizable.strings

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"keyboard_shortcut_used_by_menu_item" = "此快速鍵無法使用,因為它已被選單項目「%@」使用。";
44
"keyboard_shortcut_used_by_system" = "此快速鍵無法使用,因為它已被系統使用。";
55
"keyboard_shortcuts_can_be_changed" = "可以在「系統設定 › 鍵盤 › 鍵盤快速鍵」中更改大多數的系統快速鍵。";
6-
"keyboard_shortcut_disallowed" = "Option 鍵必須與 Command 或 Control 鍵組合使用。";
6+
"keyboard_shortcut_disallowed" = "Option鍵必須與Command鍵或Control鍵組合使用。";
77
"force_use_shortcut" = "強制使用";
88
"ok" = "好";
99
"space_key" = "空格";

Sources/KeyboardShortcuts/NSMenuItem++.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ extension NSMenuItem {
9696
return
9797
}
9898

99-
keyEquivalent = shortcut.keyEquivalent
99+
keyEquivalent = shortcut.keyEquivalent ?? ""
100100
keyEquivalentModifierMask = shortcut.modifiers
101101

102102
if #available(macOS 12, *) {

Sources/KeyboardShortcuts/RecorderCocoa.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,7 @@ extension KeyboardShortcuts {
264264

265265
// The “shift” key is not allowed without other modifiers or a function key, since it doesn't actually work.
266266
guard
267-
!event.modifiers.subtracting(.shift).isEmpty
267+
!event.modifiers.subtracting([.shift, .function]).isEmpty
268268
|| event.specialKey?.isFunctionKey == true,
269269
let shortcut = Shortcut(event: event)
270270
else {

0 commit comments

Comments
 (0)