Skip to content

Commit 9369a04

Browse files
committed
Code style tweaks
1 parent 425ddf7 commit 9369a04

File tree

6 files changed

+44
-39
lines changed

6 files changed

+44
-39
lines changed

Example/KeyboardShortcutsExample/App.swift

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,11 @@ import SwiftUI
22

33
@main
44
struct AppMain: App {
5-
@StateObject private var state = AppState()
6-
75
var body: some Scene {
86
WindowGroup {
97
MainScreen()
108
.task {
11-
state.createMenus()
9+
AppState.shared.createMenus()
1210
}
1311
}
1412
}

Example/KeyboardShortcutsExample/AppState.swift

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
import SwiftUI
22

33
@MainActor
4-
final class AppState: ObservableObject {
4+
final class AppState {
5+
static let shared = AppState()
6+
7+
private init() {}
8+
59
func createMenus() {
610
let testMenuItem = NSMenuItem()
711
NSApp.mainMenu?.addItem(testMenuItem)
@@ -13,22 +17,22 @@ final class AppState: ObservableObject {
1317
testMenu.addCallbackItem("Shortcut 1") { [weak self] in
1418
self?.alert(1)
1519
}
16-
.setShortcut(for: .testShortcut1)
20+
.setShortcut(for: .testShortcut1)
1721

1822
testMenu.addCallbackItem("Shortcut 2") { [weak self] in
1923
self?.alert(2)
2024
}
21-
.setShortcut(for: .testShortcut2)
25+
.setShortcut(for: .testShortcut2)
2226

2327
testMenu.addCallbackItem("Shortcut 3") { [weak self] in
2428
self?.alert(3)
2529
}
26-
.setShortcut(for: .testShortcut3)
30+
.setShortcut(for: .testShortcut3)
2731

2832
testMenu.addCallbackItem("Shortcut 4") { [weak self] in
2933
self?.alert(4)
3034
}
31-
.setShortcut(for: .testShortcut4)
35+
.setShortcut(for: .testShortcut4)
3236
}
3337

3438
private func alert(_ number: Int) {

Example/KeyboardShortcutsExample/MainScreen.swift

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@ private struct DynamicShortcutRecorder: View {
2222
Text("Pressed? \(isPressed ? "👍" : "👎")")
2323
.frame(width: 100, alignment: .leading)
2424
}
25-
.onChange(of: name) {
26-
isFocused = true
27-
}
25+
.onChange(of: name) {
26+
isFocused = true
27+
}
2828
}
2929
}
3030

@@ -58,12 +58,12 @@ private struct DynamicShortcut: View {
5858
DynamicShortcutRecorder(name: $shortcut.name, isPressed: $isPressed)
5959
}
6060
}
61-
.frame(maxWidth: 300)
62-
.padding()
63-
.padding(.bottom, 20)
64-
.onChange(of: shortcut) { oldValue, newValue in
65-
onShortcutChange(oldValue: oldValue, newValue: newValue)
66-
}
61+
.frame(maxWidth: 300)
62+
.padding()
63+
.padding(.bottom, 20)
64+
.onChange(of: shortcut) { oldValue, newValue in
65+
onShortcutChange(oldValue: oldValue, newValue: newValue)
66+
}
6767
}
6868

6969
private func onShortcutChange(oldValue: Shortcut, newValue: Shortcut) {
@@ -93,30 +93,30 @@ private struct DoubleShortcut: View {
9393
KeyboardShortcuts.Recorder(for: .testShortcut2) {
9494
Text("Shortcut 2:") // Intentionally using the verbose initializer for testing.
9595
}
96-
.overlay(alignment: .trailing) {
97-
Text("Pressed? \(isPressed2 ? "👍" : "👎")")
98-
.offset(x: 90)
99-
}
96+
.overlay(alignment: .trailing) {
97+
Text("Pressed? \(isPressed2 ? "👍" : "👎")")
98+
.offset(x: 90)
99+
}
100100
Spacer()
101101
Button("Reset All") {
102102
KeyboardShortcuts.reset(.testShortcut1, .testShortcut2)
103103
}
104104
}
105-
.offset(x: -40)
106-
.frame(maxWidth: 300)
107-
.padding()
108-
.padding()
109-
.onGlobalKeyboardShortcut(.testShortcut1) {
110-
isPressed1 = $0 == .keyDown
111-
}
112-
.onGlobalKeyboardShortcut(.testShortcut2, type: .keyDown) {
113-
isPressed2 = true
114-
}
115-
.task {
116-
KeyboardShortcuts.onKeyUp(for: .testShortcut2) {
117-
isPressed2 = false
118-
}
105+
.offset(x: -40)
106+
.frame(maxWidth: 300)
107+
.padding()
108+
.padding()
109+
.onGlobalKeyboardShortcut(.testShortcut1) {
110+
isPressed1 = $0 == .keyDown
111+
}
112+
.onGlobalKeyboardShortcut(.testShortcut2, type: .keyDown) {
113+
isPressed2 = true
114+
}
115+
.task {
116+
KeyboardShortcuts.onKeyUp(for: .testShortcut2) {
117+
isPressed2 = false
119118
}
119+
}
120120
}
121121
}
122122

@@ -127,7 +127,7 @@ struct MainScreen: View {
127127
Divider()
128128
DynamicShortcut()
129129
}
130-
.frame(width: 400, height: 320)
130+
.frame(width: 400, height: 320)
131131
}
132132
}
133133

Sources/KeyboardShortcuts/Recorder.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ extension KeyboardShortcuts {
3838
}
3939
}
4040
```
41+
42+
- Note: Since macOS 15, for sandboxed apps, it's [no longer possible](https://developer.apple.com/forums/thread/763878?answerId=804374022#804374022) to specify the `Option` key without also using `Command` or `Control`.
4143
*/
4244
public struct Recorder<Label: View>: View { // swiftlint:disable:this type_name
4345
private let name: Name

Sources/KeyboardShortcuts/Utilities.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -477,7 +477,7 @@ extension View {
477477
labelsHidden()
478478
.alignmentGuide(.controlAlignment) { $0[.leading] }
479479
}
480-
.alignmentGuide(.leading) { $0[.controlAlignment] }
480+
.alignmentGuide(.leading) { $0[.controlAlignment] }
481481
}
482482
}
483483

readme.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ import KeyboardShortcuts
6565

6666
@main
6767
struct YourApp: App {
68-
@StateObject private var appState = AppState()
68+
@State private var appState = AppState()
6969

7070
var body: some Scene {
7171
WindowGroup {
@@ -78,7 +78,8 @@ struct YourApp: App {
7878
}
7979

8080
@MainActor
81-
final class AppState: ObservableObject {
81+
@Observable
82+
final class AppState {
8283
init() {
8384
KeyboardShortcuts.onKeyUp(for: .toggleUnicornMode) { [self] in
8485
isUnicornMode.toggle()

0 commit comments

Comments
 (0)