-
-
Notifications
You must be signed in to change notification settings - Fork 163
/
Recorder.swift
166 lines (149 loc) · 4.78 KB
/
Recorder.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
import SwiftUI
@available(macOS 10.15, *)
extension KeyboardShortcuts {
private struct _Recorder: NSViewRepresentable { // swiftlint:disable:this type_name
typealias NSViewType = RecorderCocoa
let name: Name
let onChange: ((_ shortcut: Shortcut?) -> Void)?
func makeNSView(context: Context) -> NSViewType {
.init(for: name, onChange: onChange)
}
func updateNSView(_ nsView: NSViewType, context: Context) {
nsView.shortcutName = name
}
}
/**
A SwiftUI `View` that lets the user record a keyboard shortcut.
You would usually put this in your settings window.
It automatically prevents choosing a keyboard shortcut that is already taken by the system or by the app's main menu by showing a user-friendly alert to the user.
It takes care of storing the keyboard shortcut in `UserDefaults` for you.
```swift
import SwiftUI
import KeyboardShortcuts
struct SettingsScreen: View {
var body: some View {
Form {
KeyboardShortcuts.Recorder("Toggle Unicorn Mode:", name: .toggleUnicornMode)
}
}
}
```
*/
public struct Recorder<Label: View>: View { // swiftlint:disable:this type_name
private let name: Name
private let onChange: ((Shortcut?) -> Void)?
private let hasLabel: Bool
private let label: Label
init(
for name: Name,
onChange: ((Shortcut?) -> Void)? = nil,
hasLabel: Bool,
@ViewBuilder label: () -> Label
) {
self.name = name
self.onChange = onChange
self.hasLabel = hasLabel
self.label = label()
}
public var body: some View {
if hasLabel {
if #available(macOS 13, *) {
LabeledContent {
_Recorder(
name: name,
onChange: onChange
)
} label: {
label
}
} else {
_Recorder(
name: name,
onChange: onChange
)
.formLabel {
label
}
}
} else {
_Recorder(
name: name,
onChange: onChange
)
}
}
}
}
@available(macOS 10.15, *)
extension KeyboardShortcuts.Recorder<EmptyView> {
/**
- Parameter name: Strongly-typed keyboard shortcut name.
- 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.
*/
public init(
for name: KeyboardShortcuts.Name,
onChange: ((KeyboardShortcuts.Shortcut?) -> Void)? = nil
) {
self.init(
for: name,
onChange: onChange,
hasLabel: false
) {}
}
}
@available(macOS 10.15, *)
extension KeyboardShortcuts.Recorder<Text> {
/**
- Parameter title: The title of the keyboard shortcut recorder, describing its purpose.
- Parameter name: Strongly-typed keyboard shortcut name.
- 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.
*/
public init(
_ title: String,
name: KeyboardShortcuts.Name,
onChange: ((KeyboardShortcuts.Shortcut?) -> Void)? = nil
) {
self.init(
for: name,
onChange: onChange,
hasLabel: true
) {
Text(title)
}
}
}
@available(macOS 10.15, *)
extension KeyboardShortcuts.Recorder {
/**
- Parameter name: Strongly-typed keyboard shortcut name.
- 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.
- Parameter label: A view that describes the purpose of the keyboard shortcut recorder.
*/
public init(
for name: KeyboardShortcuts.Name,
onChange: ((KeyboardShortcuts.Shortcut?) -> Void)? = nil,
@ViewBuilder label: () -> Label
) {
self.init(
for: name,
onChange: onChange,
hasLabel: true,
label: label
)
}
}
@available(macOS 10.15, *)
#Preview {
KeyboardShortcuts.Recorder(for: .init("xcodePreview"))
.environment(\.locale, .init(identifier: "en"))
}
@available(macOS 10.15, *)
#Preview {
KeyboardShortcuts.Recorder(for: .init("xcodePreview"))
.environment(\.locale, .init(identifier: "zh-Hans"))
}
@available(macOS 10.15, *)
#Preview {
KeyboardShortcuts.Recorder(for: .init("xcodePreview"))
.environment(\.locale, .init(identifier: "ru"))
}