Add optional change callback to `Recorder` and `RecorderCocoa` (#12)

Co-authored-by: Sindre Sorhus <sindresorhus@gmail.com>
This commit is contained in:
Garth Poitras 2020-06-17 04:14:49 -04:00 committed by GitHub
parent e3784b1264
commit 5cb615fbca
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 39 additions and 5 deletions

View File

@ -30,13 +30,25 @@ extension KeyboardShortcuts {
public typealias NSViewType = RecorderCocoa
private let name: Name
private let onChange: ((_ shortcut: Shortcut?) -> Void)?
public init(for name: Name) {
/**
Creates a new Recorder view
- Parameter name: strongly typed `KeyboardShortcuts.Name`
- Parameter onChange: optional callback which will be called when the shortcut is successfully changed/removed. This could be useful if you would like to store the keyboard shortcut somewhere yourself instead of rely on the build-in `UserDefaults` storage.
```
KeyboardShortcuts.Recorder(for: .toggleUnicornMode, onChange: { (shortcut: KeyboardShortcuts.Shortcut?) in
print("Changed shortcut to:", shortcut)
})
```
**/
public init(for name: Name, onChange: ((_ shortcut: Shortcut?) -> Void)? = nil) {
self.name = name
self.onChange = onChange
}
/// :nodoc:
public func makeNSView(context: Context) -> NSViewType { .init(for: name) }
public func makeNSView(context: Context) -> NSViewType { .init(for: name, onChange: onChange) }
/// :nodoc:
public func updateNSView(_ nsView: NSViewType, context: Context) {}

View File

@ -29,6 +29,7 @@ extension KeyboardShortcuts {
private let minimumWidth: Double = 130
private var eventMonitor: LocalEventMonitor?
private let shortcutName: Name
private let onChange: ((_ shortcut: Shortcut?) -> Void)?
/// :nodoc:
override public var canBecomeKeyView: Bool { false }
@ -49,8 +50,19 @@ extension KeyboardShortcuts {
}
}
public required init(for name: Name) {
/**
Creates a new RecorderCocoa view
- Parameter name: strongly typed `KeyboardShortcuts.Name`
- Parameter onChange: optional callback which will be called when the shortcut is successfully changed/removed. This could be useful if you would like to store the keyboard shortcut somewhere yourself instead of rely on the build-in `UserDefaults` storage.
```
KeyboardShortcuts.RecorderCocoa(for: .toggleUnicornMode, onChange: { (shortcut: KeyboardShortcuts.Shortcut?) in
print("Changed shortcut to:", shortcut)
})
```
**/
public required init(for name: Name, onChange: ((_ shortcut: Shortcut?) -> Void)? = nil) {
self.shortcutName = name
self.onChange = onChange
super.init(frame: .zero)
self.delegate = self
@ -82,7 +94,7 @@ extension KeyboardShortcuts {
/// :nodoc:
public func controlTextDidChange(_ object: Notification) {
if stringValue.isEmpty {
userDefaultsRemove(name: shortcutName)
saveShortcut(nil)
}
showsCancelButton = !stringValue.isEmpty
@ -205,7 +217,7 @@ extension KeyboardShortcuts {
self.stringValue = "\(shortcut)"
self.showsCancelButton = true
userDefaultsSet(name: self.shortcutName, shortcut: shortcut)
self.saveShortcut(shortcut)
self.blur()
return nil
@ -213,5 +225,15 @@ extension KeyboardShortcuts {
return shouldBecomeFirstResponder
}
private func saveShortcut(_ shortcut: Shortcut?) {
if let shortcut = shortcut {
userDefaultsSet(name: shortcutName, shortcut: shortcut)
} else {
userDefaultsRemove(name: shortcutName)
}
onChange?(shortcut)
}
}
}