@@ -23,30 +23,47 @@ final class XcodeKeyboardShortcutsManager: @unchecked Sendable {
2323
2424 private var cancellables : Set < AnyCancellable > = [ ]
2525
26+ private var enabledShortcutNames = Set < String > ( )
27+
2628 @Dependency ( \. appEventHandlerRegistry) private var appEventHandlerRegistry
2729
2830 /// This can be moved to the initializer once https://github.com/swiftlang/swift/issues/80050 is fixed.
2931 private func observeXcodeState( appsActivationState: AnyPublisher < AppsActivationState , Never > ) {
3032 let cancellable = appsActivationState
3133 . sink { @Sendable [ weak self] appsActivationState in
32- guard self != nil else { return }
34+ guard let self else { return }
3335 // Handle de-activations first, then activations.
3436 if !appsActivationState. isHostAppActive {
35- KeyboardShortcuts . disable ( KeyboardShortcuts . Name. hostAppShortcuts)
37+ disable ( KeyboardShortcuts . Name. hostAppShortcuts)
3638 }
3739 if !appsActivationState. isXcodeActive {
38- KeyboardShortcuts . disable ( KeyboardShortcuts . Name. xcodeShortcuts)
40+ disable ( KeyboardShortcuts . Name. xcodeShortcuts)
3941 }
4042 if appsActivationState. isHostAppActive {
41- KeyboardShortcuts . enable ( KeyboardShortcuts . Name. hostAppShortcuts)
43+ enable ( KeyboardShortcuts . Name. hostAppShortcuts)
4244 }
4345 if appsActivationState. isXcodeActive {
44- KeyboardShortcuts . enable ( KeyboardShortcuts . Name. xcodeShortcuts)
46+ enable ( KeyboardShortcuts . Name. xcodeShortcuts)
4547 }
4648 }
4749 safelyMutate { $0. cancellables. insert ( cancellable) }
4850 }
4951
52+ /// Manually tracks enabled shortcuts due to https://github.com/sindresorhus/KeyboardShortcuts/issues/217
53+ private func enable( _ shortcuts: [ KeyboardShortcuts . Name ] ) {
54+ KeyboardShortcuts . enable ( shortcuts)
55+ safelyMutate { state in
56+ for shortcut in shortcuts { state. enabledShortcutNames. insert ( shortcut. rawValue) }
57+ }
58+ }
59+
60+ private func disable( _ shortcuts: [ KeyboardShortcuts . Name ] ) {
61+ KeyboardShortcuts . disable ( shortcuts)
62+ safelyMutate { state in
63+ for shortcut in shortcuts { state. enabledShortcutNames. remove ( shortcut. rawValue) }
64+ }
65+ }
66+
5067 private func registerActions( ) {
5168 on ( . ask, trigger: AddCodeToChatEvent ( newThread: false , chatMode: . ask) )
5269 on ( . askInNewThread, trigger: AddCodeToChatEvent ( newThread: true , chatMode: . ask) )
@@ -56,14 +73,17 @@ final class XcodeKeyboardShortcutsManager: @unchecked Sendable {
5673 on ( . generate, trigger: GenerateEvent ( ) )
5774 on ( . hideChat, trigger: HideChatEvent ( ) )
5875 on ( . new, trigger: NewChatEvent ( ) )
59- on ( . switchToAskMode, trigger: AddCodeToChatEvent ( newThread : false , chatMode: . ask) )
60- on ( . switchToAgentMode, trigger: AddCodeToChatEvent ( newThread : false , chatMode: . agent) )
76+ on ( . switchToAskMode, trigger: ChangeChatModeEvent ( chatMode: . ask) )
77+ on ( . switchToAgentMode, trigger: ChangeChatModeEvent ( chatMode: . agent) )
6178 }
6279
6380 private func on( _ keyEvent: KeyboardShortcuts . Name , trigger event: AppEvent ) {
6481 KeyboardShortcuts . onKeyUp ( for: keyEvent) {
6582 Task { [ weak self] in
66- await self ? . appEventHandlerRegistry. handle ( event: event)
83+ guard let self else { return }
84+ if enabledShortcutNames. contains ( keyEvent. rawValue) {
85+ _ = await appEventHandlerRegistry. handle ( event: event)
86+ }
6787 }
6888 }
6989 }
0 commit comments