Skip to content

Commit e33b6cb

Browse files
committed
Minor tweaks
1 parent 5cb615f commit e33b6cb

File tree

7 files changed

+34
-45
lines changed

7 files changed

+34
-45
lines changed

.swiftlint.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,6 @@ whitelist_rules:
8181
- operator_whitespace
8282
- orphaned_doc_comment
8383
- overridden_super_call
84-
- pattern_matching_keywords
8584
- prefer_self_type_over_type_of_self
8685
- private_action
8786
- private_outlet

KeyboardShortcutsExample/AppDelegate.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import KeyboardShortcuts
44

55
@NSApplicationMain
66
final class AppDelegate: NSObject, NSApplicationDelegate {
7-
var window: NSWindow!
7+
private var window: NSWindow!
88

99
func applicationDidFinishLaunching(_ notification: Notification) {
1010
let contentView = ContentView()

KeyboardShortcutsExample/ContentView.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ struct ContentView: View {
1010
@State private var isPressed1 = false
1111
@State private var isPressed2 = false
1212

13-
var body: some View {
13+
var body: some View {
1414
VStack {
1515
HStack {
1616
KeyboardShortcuts.Recorder(for: .testShortcut1)
@@ -44,11 +44,11 @@ struct ContentView: View {
4444
self.isPressed2 = false
4545
}
4646
}
47-
}
47+
}
4848
}
4949

5050
struct ContentView_Previews: PreviewProvider {
51-
static var previews: some View {
52-
ContentView()
53-
}
51+
static var previews: some View {
52+
ContentView()
53+
}
5454
}

Sources/KeyboardShortcuts/Key.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ extension KeyboardShortcuts {
229229
case kVK_RightShift:
230230
self = .rightShift
231231
case kVK_Return:
232-
self = .`return`
232+
self = .return
233233
case kVK_ANSI_Backslash:
234234
self = .backslash
235235
case kVK_ANSI_Grave:
@@ -464,7 +464,7 @@ extension KeyboardShortcuts {
464464
return kVK_RightControl
465465
case .rightShift:
466466
return kVK_RightShift
467-
case .`return`:
467+
case .return:
468468
return kVK_Return
469469
case .backslash:
470470
return kVK_ANSI_Backslash

Sources/KeyboardShortcuts/Recorder.swift

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -33,16 +33,13 @@ extension KeyboardShortcuts {
3333
private let onChange: ((_ shortcut: Shortcut?) -> Void)?
3434

3535
/**
36-
Creates a new Recorder view
37-
- Parameter name: strongly typed `KeyboardShortcuts.Name`
38-
- 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.
39-
```
40-
KeyboardShortcuts.Recorder(for: .toggleUnicornMode, onChange: { (shortcut: KeyboardShortcuts.Shortcut?) in
41-
print("Changed shortcut to:", shortcut)
42-
})
43-
```
44-
**/
45-
public init(for name: Name, onChange: ((_ shortcut: Shortcut?) -> Void)? = nil) {
36+
- Parameter name: Strongly-typed keyboard shortcut name.
37+
- Parameter onChange: Callback which will be called when the keyboard shortcut is changed/removed by the user. This can be useful when you need more control. For example, when migrating from a different keyboard shortcut solution and you need to store the keyboard shortcut somewhere yourself instead of relying on the built-in storage. However, it's strongly recommended to just rely on the built-in storage when possible.
38+
*/
39+
public init(
40+
for name: Name,
41+
onChange: ((_ shortcut: Shortcut?) -> Void)? = nil
42+
) {
4643
self.name = name
4744
self.onChange = onChange
4845
}
@@ -57,7 +54,7 @@ extension KeyboardShortcuts {
5754

5855
@available(macOS 10.15, *)
5956
struct SwiftUI_Previews: PreviewProvider {
60-
static var previews: some View {
57+
static var previews: some View {
6158
KeyboardShortcuts.Recorder(for: .Name("xcodePreview"))
62-
}
59+
}
6360
}

Sources/KeyboardShortcuts/RecorderCocoa.swift

Lines changed: 14 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -51,16 +51,13 @@ extension KeyboardShortcuts {
5151
}
5252

5353
/**
54-
Creates a new RecorderCocoa view
55-
- Parameter name: strongly typed `KeyboardShortcuts.Name`
56-
- 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.
57-
```
58-
KeyboardShortcuts.RecorderCocoa(for: .toggleUnicornMode, onChange: { (shortcut: KeyboardShortcuts.Shortcut?) in
59-
print("Changed shortcut to:", shortcut)
60-
})
61-
```
62-
**/
63-
public required init(for name: Name, onChange: ((_ shortcut: Shortcut?) -> Void)? = nil) {
54+
- Parameter name: Strongly-typed keyboard shortcut name.
55+
- Parameter onChange: Callback which will be called when the keyboard shortcut is changed/removed by the user. This can be useful when you need more control. For example, when migrating from a different keyboard shortcut solution and you need to store the keyboard shortcut somewhere yourself instead of relying on the built-in storage. However, it's strongly recommended to just rely on the built-in storage when possible.
56+
*/
57+
public required init(
58+
for name: Name,
59+
onChange: ((_ shortcut: Shortcut?) -> Void)? = nil
60+
) {
6461
self.shortcutName = name
6562
self.onChange = onChange
6663

@@ -133,7 +130,7 @@ extension KeyboardShortcuts {
133130
let clickMargin: CGFloat = 3
134131

135132
if
136-
(event.type == .leftMouseUp || event.type == .rightMouseUp),
133+
event.type == .leftMouseUp || event.type == .rightMouseUp,
137134
!self.frame.insetBy(dx: -clickMargin, dy: -clickMargin).contains(clickPoint)
138135
{
139136
self.blur()
@@ -163,22 +160,18 @@ extension KeyboardShortcuts {
163160
}
164161

165162
if
166-
event.modifiers.isEmpty &&
167-
(
168-
event.specialKey == .delete ||
169-
event.specialKey == .deleteForward ||
170-
event.specialKey == .backspace
171-
)
163+
event.modifiers.isEmpty,
164+
event.specialKey == .delete
165+
|| event.specialKey == .deleteForward
166+
|| event.specialKey == .backspace
172167
{
173168
self.clear()
174169
return nil
175170
}
176171

177172
guard
178-
(
179-
!event.modifiers.isEmpty ||
180-
event.specialKey?.isFunctionKey == true
181-
),
173+
!event.modifiers.isEmpty
174+
|| event.specialKey?.isFunctionKey == true,
182175
let shortcut = Shortcut(event: event)
183176
else {
184177
NSSound.beep()

Sources/KeyboardShortcuts/Shortcut.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -188,8 +188,8 @@ extension KeyboardShortcuts.Shortcut {
188188
return nil
189189
}
190190

191-
let dataRef = unsafeBitCast(layoutDataPointer, to: CFData.self)
192-
let keyLayout = unsafeBitCast(CFDataGetBytePtr(dataRef), to: UnsafePointer<CoreServices.UCKeyboardLayout>.self)
191+
let layoutData = unsafeBitCast(layoutDataPointer, to: CFData.self)
192+
let keyLayout = unsafeBitCast(CFDataGetBytePtr(layoutData), to: UnsafePointer<CoreServices.UCKeyboardLayout>.self)
193193
var deadKeyState: UInt32 = 0
194194
let maxLength = 4
195195
var length = 0
@@ -199,7 +199,7 @@ extension KeyboardShortcuts.Shortcut {
199199
keyLayout,
200200
UInt16(carbonKeyCode),
201201
UInt16(CoreServices.kUCKeyActionDisplay),
202-
UInt32(carbonModifiers),
202+
0, // No modifiers
203203
UInt32(LMGetKbdType()),
204204
OptionBits(CoreServices.kUCKeyTranslateNoDeadKeysBit),
205205
&deadKeyState,

0 commit comments

Comments
 (0)