Skip to content

Commit

Permalink
Code style tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
sindresorhus committed Sep 23, 2024
1 parent 425ddf7 commit 9369a04
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 39 deletions.
4 changes: 1 addition & 3 deletions Example/KeyboardShortcutsExample/App.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,11 @@ import SwiftUI

@main
struct AppMain: App {
@StateObject private var state = AppState()

var body: some Scene {
WindowGroup {
MainScreen()
.task {
state.createMenus()
AppState.shared.createMenus()
}
}
}
Expand Down
14 changes: 9 additions & 5 deletions Example/KeyboardShortcutsExample/AppState.swift
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import SwiftUI

@MainActor
final class AppState: ObservableObject {
final class AppState {
static let shared = AppState()

private init() {}

func createMenus() {
let testMenuItem = NSMenuItem()
NSApp.mainMenu?.addItem(testMenuItem)
Expand All @@ -13,22 +17,22 @@ final class AppState: ObservableObject {
testMenu.addCallbackItem("Shortcut 1") { [weak self] in
self?.alert(1)
}
.setShortcut(for: .testShortcut1)
.setShortcut(for: .testShortcut1)

testMenu.addCallbackItem("Shortcut 2") { [weak self] in
self?.alert(2)
}
.setShortcut(for: .testShortcut2)
.setShortcut(for: .testShortcut2)

testMenu.addCallbackItem("Shortcut 3") { [weak self] in
self?.alert(3)
}
.setShortcut(for: .testShortcut3)
.setShortcut(for: .testShortcut3)

testMenu.addCallbackItem("Shortcut 4") { [weak self] in
self?.alert(4)
}
.setShortcut(for: .testShortcut4)
.setShortcut(for: .testShortcut4)
}

private func alert(_ number: Int) {
Expand Down
56 changes: 28 additions & 28 deletions Example/KeyboardShortcutsExample/MainScreen.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ private struct DynamicShortcutRecorder: View {
Text("Pressed? \(isPressed ? "👍" : "👎")")
.frame(width: 100, alignment: .leading)
}
.onChange(of: name) {
isFocused = true
}
.onChange(of: name) {
isFocused = true
}
}
}

Expand Down Expand Up @@ -58,12 +58,12 @@ private struct DynamicShortcut: View {
DynamicShortcutRecorder(name: $shortcut.name, isPressed: $isPressed)
}
}
.frame(maxWidth: 300)
.padding()
.padding(.bottom, 20)
.onChange(of: shortcut) { oldValue, newValue in
onShortcutChange(oldValue: oldValue, newValue: newValue)
}
.frame(maxWidth: 300)
.padding()
.padding(.bottom, 20)
.onChange(of: shortcut) { oldValue, newValue in
onShortcutChange(oldValue: oldValue, newValue: newValue)
}
}

private func onShortcutChange(oldValue: Shortcut, newValue: Shortcut) {
Expand Down Expand Up @@ -93,30 +93,30 @@ private struct DoubleShortcut: View {
KeyboardShortcuts.Recorder(for: .testShortcut2) {
Text("Shortcut 2:") // Intentionally using the verbose initializer for testing.
}
.overlay(alignment: .trailing) {
Text("Pressed? \(isPressed2 ? "👍" : "👎")")
.offset(x: 90)
}
.overlay(alignment: .trailing) {
Text("Pressed? \(isPressed2 ? "👍" : "👎")")
.offset(x: 90)
}
Spacer()
Button("Reset All") {
KeyboardShortcuts.reset(.testShortcut1, .testShortcut2)
}
}
.offset(x: -40)
.frame(maxWidth: 300)
.padding()
.padding()
.onGlobalKeyboardShortcut(.testShortcut1) {
isPressed1 = $0 == .keyDown
}
.onGlobalKeyboardShortcut(.testShortcut2, type: .keyDown) {
isPressed2 = true
}
.task {
KeyboardShortcuts.onKeyUp(for: .testShortcut2) {
isPressed2 = false
}
.offset(x: -40)
.frame(maxWidth: 300)
.padding()
.padding()
.onGlobalKeyboardShortcut(.testShortcut1) {
isPressed1 = $0 == .keyDown
}
.onGlobalKeyboardShortcut(.testShortcut2, type: .keyDown) {
isPressed2 = true
}
.task {
KeyboardShortcuts.onKeyUp(for: .testShortcut2) {
isPressed2 = false
}
}
}
}

Expand All @@ -127,7 +127,7 @@ struct MainScreen: View {
Divider()
DynamicShortcut()
}
.frame(width: 400, height: 320)
.frame(width: 400, height: 320)
}
}

Expand Down
2 changes: 2 additions & 0 deletions Sources/KeyboardShortcuts/Recorder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ extension KeyboardShortcuts {
}
}
```
- 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`.
*/
public struct Recorder<Label: View>: View { // swiftlint:disable:this type_name
private let name: Name
Expand Down
2 changes: 1 addition & 1 deletion Sources/KeyboardShortcuts/Utilities.swift
Original file line number Diff line number Diff line change
Expand Up @@ -477,7 +477,7 @@ extension View {
labelsHidden()
.alignmentGuide(.controlAlignment) { $0[.leading] }
}
.alignmentGuide(.leading) { $0[.controlAlignment] }
.alignmentGuide(.leading) { $0[.controlAlignment] }
}
}

Expand Down
5 changes: 3 additions & 2 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ import KeyboardShortcuts

@main
struct YourApp: App {
@StateObject private var appState = AppState()
@State private var appState = AppState()

var body: some Scene {
WindowGroup {
Expand All @@ -78,7 +78,8 @@ struct YourApp: App {
}

@MainActor
final class AppState: ObservableObject {
@Observable
final class AppState {
init() {
KeyboardShortcuts.onKeyUp(for: .toggleUnicornMode) { [self] in
isUnicornMode.toggle()
Expand Down

0 comments on commit 9369a04

Please sign in to comment.