Skip to content

Commit 528cd5c

Browse files
committed
Add .resetAll() method
1 parent 16e7f8b commit 528cd5c

File tree

4 files changed

+75
-17
lines changed

4 files changed

+75
-17
lines changed

Example/KeyboardShortcutsExample/MainScreen.swift

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,11 @@ 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()
@@ -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

Sources/KeyboardShortcuts/Utilities.swift

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ extension String {
1414

1515

1616
extension Data {
17-
var toString: String? { String(data: self, encoding: .utf8) } // swiftlint:disable:this non_optional_string_data_conversion
17+
var toString: String? { String(data: self, encoding: .utf8) }
1818
}
1919

2020

@@ -497,3 +497,30 @@ extension Dictionary {
497497
}
498498
}
499499
#endif
500+
501+
502+
extension Sequence where Element: Hashable {
503+
/**
504+
Convert a `Sequence` with `Hashable` elements to a `Set`.
505+
*/
506+
func toSet() -> Set<Element> { Set(self) }
507+
}
508+
509+
510+
extension Set {
511+
/**
512+
Convert a `Set` to an `Array`.
513+
*/
514+
func toArray() -> [Element] { Array(self) }
515+
}
516+
517+
518+
extension StringProtocol {
519+
func replacingPrefix(_ prefix: String, with replacement: String) -> String {
520+
guard hasPrefix(prefix) else {
521+
return String(self)
522+
}
523+
524+
return replacement + dropFirst(prefix.count)
525+
}
526+
}

0 commit comments

Comments
 (0)