Skip to content

Commit

Permalink
Add .resetAll() method
Browse files Browse the repository at this point in the history
  • Loading branch information
sindresorhus committed Oct 9, 2024
1 parent 16e7f8b commit 528cd5c
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 17 deletions.
8 changes: 5 additions & 3 deletions Example/KeyboardShortcutsExample/MainScreen.swift
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@ private struct DynamicShortcut: View {
Divider()
DynamicShortcutRecorder(name: $shortcut.name, isPressed: $isPressed)
}
Divider()
.padding(.vertical)
Button("Reset All") {
KeyboardShortcuts.resetAll()
}
}
.frame(maxWidth: 300)
.padding()
Expand Down Expand Up @@ -98,9 +103,6 @@ private struct DoubleShortcut: View {
.offset(x: 90)
}
Spacer()
Button("Reset All") {
KeyboardShortcuts.reset(.testShortcut1, .testShortcut2)
}
}
.offset(x: -40)
.frame(maxWidth: 300)
Expand Down
3 changes: 0 additions & 3 deletions Example/KeyboardShortcutsExample/Utilities.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import SwiftUI


@MainActor
final class CallbackMenuItem: NSMenuItem {
private static var validateCallback: ((NSMenuItem) -> Bool)?
Expand Down Expand Up @@ -50,7 +49,6 @@ extension CallbackMenuItem: NSMenuItemValidation {
}
}


extension NSMenuItem {
convenience init(
_ title: String,
Expand Down Expand Up @@ -81,7 +79,6 @@ extension NSMenuItem {
}
}


extension NSMenu {
@MainActor
@discardableResult
Expand Down
52 changes: 42 additions & 10 deletions Sources/KeyboardShortcuts/KeyboardShortcuts.swift
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,19 @@ public enum KeyboardShortcuts {
}
}

static var allNames: Set<Name> {
UserDefaults.standard.dictionaryRepresentation()
.compactMap { key, _ in
guard key.hasPrefix(userDefaultsPrefix) else {
return nil
}

let name = key.replacingPrefix(userDefaultsPrefix, with: "")
return .init(name)
}
.toSet()
}

/**
Enable keyboard shortcuts to work even when an `NSMenu` is open by setting this property when the menu opens and closes.
Expand Down Expand Up @@ -225,11 +238,8 @@ public enum KeyboardShortcuts {
var body: some View {
VStack {
// …
Button("Reset All") {
KeyboardShortcuts.reset(
.toggleUnicornMode,
.showRainbow
)
Button("Reset") {
KeyboardShortcuts.reset(.toggleUnicornMode)
}
}
}
Expand All @@ -255,11 +265,8 @@ public enum KeyboardShortcuts {
var body: some View {
VStack {
// …
Button("Reset All") {
KeyboardShortcuts.reset(
.toggleUnicornMode,
.showRainbow
)
Button("Reset") {
KeyboardShortcuts.reset(.toggleUnicornMode)
}
}
}
Expand All @@ -270,6 +277,31 @@ public enum KeyboardShortcuts {
reset(names)
}

/**
Reset the keyboard shortcut for all the names.
Unlike `reset(…)`, this resets all the shortcuts to `nil`, not the `defaultValue`.
```swift
import SwiftUI
import KeyboardShortcuts
struct SettingsScreen: View {
var body: some View {
VStack {
// …
Button("Reset All") {
KeyboardShortcuts.resetAll()
}
}
}
}
```
*/
public static func resetAll() {
reset(allNames.toArray())
}

/**
Set the keyboard shortcut for a name.
Expand Down
29 changes: 28 additions & 1 deletion Sources/KeyboardShortcuts/Utilities.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ extension String {


extension Data {
var toString: String? { String(data: self, encoding: .utf8) } // swiftlint:disable:this non_optional_string_data_conversion
var toString: String? { String(data: self, encoding: .utf8) }
}


Expand Down Expand Up @@ -497,3 +497,30 @@ extension Dictionary {
}
}
#endif


extension Sequence where Element: Hashable {
/**
Convert a `Sequence` with `Hashable` elements to a `Set`.
*/
func toSet() -> Set<Element> { Set(self) }
}


extension Set {
/**
Convert a `Set` to an `Array`.
*/
func toArray() -> [Element] { Array(self) }
}


extension StringProtocol {
func replacingPrefix(_ prefix: String, with replacement: String) -> String {
guard hasPrefix(prefix) else {
return String(self)
}

return replacement + dropFirst(prefix.count)
}
}

0 comments on commit 528cd5c

Please sign in to comment.