Skip to content
This repository was archived by the owner on Nov 17, 2025. It is now read-only.

Commit b69b8a0

Browse files
committed
Fix notification observer concurrency issues
- Use queue: .main for notification observers to ensure MainActor execution - Add @mainactor to CarbonKeyboardShortcuts.register method - Add @mainactor to handleOnKeyDown/Up methods - Remove unnecessary MainActor.assumeIsolated blocks
1 parent 442a1e0 commit b69b8a0

3 files changed

Lines changed: 23 additions & 26 deletions

File tree

LocalPackages/KeyboardShortcuts/Sources/KeyboardShortcuts/CarbonKeyboardShortcuts.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@ enum CarbonKeyboardShortcuts {
134134
}
135135
}
136136

137+
@MainActor
137138
static func register(
138139
_ shortcut: KeyboardShortcuts.Shortcut,
139140
onKeyDown: @escaping (KeyboardShortcuts.Shortcut) -> Void,

LocalPackages/KeyboardShortcuts/Sources/KeyboardShortcuts/KeyboardShortcuts.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -398,6 +398,7 @@ public enum KeyboardShortcuts {
398398
return decoded
399399
}
400400

401+
@MainActor
401402
private static func handleOnKeyDown(_ shortcut: Shortcut) {
402403
guard !isPaused else {
403404
return
@@ -424,6 +425,7 @@ public enum KeyboardShortcuts {
424425
}
425426
}
426427

428+
@MainActor
427429
private static func handleOnKeyUp(_ shortcut: Shortcut) {
428430
guard !isPaused else {
429431
return

LocalPackages/KeyboardShortcuts/Sources/KeyboardShortcuts/RecorderCocoa.swift

Lines changed: 20 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -120,18 +120,16 @@ extension KeyboardShortcuts {
120120
}
121121

122122
private func setUpEvents() {
123-
shortcutsNameChangeObserver = NotificationCenter.default.addObserver(forName: .shortcutByNameDidChange, object: nil, queue: nil) { [weak self] notification in
124-
MainActor.assumeIsolated { [weak self] in
125-
guard
126-
let self,
127-
let nameInNotification = notification.userInfo?["name"] as? KeyboardShortcuts.Name,
128-
nameInNotification == self.shortcutName
129-
else {
130-
return
131-
}
132-
133-
self.setStringValue(name: nameInNotification)
123+
shortcutsNameChangeObserver = NotificationCenter.default.addObserver(forName: .shortcutByNameDidChange, object: nil, queue: .main) { [weak self] notification in
124+
guard
125+
let self,
126+
let nameInNotification = notification.userInfo?["name"] as? KeyboardShortcuts.Name,
127+
nameInNotification == self.shortcutName
128+
else {
129+
return
134130
}
131+
132+
self.setStringValue(name: nameInNotification)
135133
}
136134
}
137135

@@ -183,25 +181,21 @@ extension KeyboardShortcuts {
183181

184182
// Ensures the recorder stops when the window is hidden.
185183
// This is especially important for Settings windows, which as of macOS 13.5, only hides instead of closes when you click the close button.
186-
windowDidResignKeyObserver = NotificationCenter.default.addObserver(forName: NSWindow.didResignKeyNotification, object: window, queue: nil) { [weak self] _ in
187-
MainActor.assumeIsolated { [weak self] in
188-
guard
189-
let self,
190-
let window = self.window
191-
else {
192-
return
193-
}
194-
195-
self.endRecording()
196-
window.makeFirstResponder(nil)
184+
windowDidResignKeyObserver = NotificationCenter.default.addObserver(forName: NSWindow.didResignKeyNotification, object: window, queue: .main) { [weak self] _ in
185+
guard
186+
let self,
187+
let window = self.window
188+
else {
189+
return
197190
}
191+
192+
self.endRecording()
193+
window.makeFirstResponder(nil)
198194
}
199195

200196
// Ensures the recorder does not receive initial focus when a hidden window becomes unhidden.
201-
windowDidBecomeKeyObserver = NotificationCenter.default.addObserver(forName: NSWindow.didBecomeKeyNotification, object: window, queue: nil) { [weak self] _ in
202-
MainActor.assumeIsolated {
203-
self?.preventBecomingKey()
204-
}
197+
windowDidBecomeKeyObserver = NotificationCenter.default.addObserver(forName: NSWindow.didBecomeKeyNotification, object: window, queue: .main) { [weak self] _ in
198+
self?.preventBecomingKey()
205199
}
206200

207201
preventBecomingKey()

0 commit comments

Comments
 (0)