1+ import Combine
12import Dependencies
23import Foundation
34import KeyboardShortcuts
45import Workspace
56import WorkspaceSuggestionService
7+ import XcodeInspector
68
79#if canImport(ProService)
810import ProService
@@ -27,6 +29,7 @@ public final class Service {
2729 public let guiController = GraphicalUserInterfaceController ( )
2830 public let realtimeSuggestionController = RealtimeSuggestionController ( )
2931 public let scheduledCleaner : ScheduledCleaner
32+ let globalShortcutManager : GlobalShortcutManager
3033
3134 #if canImport(ProService)
3235 let proService : ProService
@@ -38,6 +41,7 @@ public final class Service {
3841 scheduledCleaner = . init( workspacePool: workspacePool, guiController: guiController)
3942 workspacePool. registerPlugin { SuggestionServiceWorkspacePlugin ( workspace: $0) }
4043 self . workspacePool = workspacePool
44+ globalShortcutManager = . init( guiController: guiController)
4145
4246 #if canImport(ProService)
4347 proService = withDependencies { dependencyValues in
@@ -48,8 +52,6 @@ public final class Service {
4852 ProService ( )
4953 }
5054 #endif
51-
52- KeyboardShortcuts . userDefaults = . shared
5355 }
5456
5557 @MainActor
@@ -61,10 +63,51 @@ public final class Service {
6163 proService. start ( )
6264 #endif
6365 DependencyUpdater ( ) . update ( )
64-
66+ globalShortcutManager. start ( )
67+ }
68+ }
69+
70+ @MainActor
71+ final class GlobalShortcutManager {
72+ let guiController : GraphicalUserInterfaceController
73+ private var cancellable = Set < AnyCancellable > ( )
74+
75+ nonisolated init ( guiController: GraphicalUserInterfaceController ) {
76+ self . guiController = guiController
77+ }
78+
79+ func start( ) {
80+ KeyboardShortcuts . userDefaults = . shared
81+ setupShortcutIfNeeded ( )
82+
6583 KeyboardShortcuts . onKeyUp ( for: . showHideWidget) { [ guiController] in
6684 guiController. viewStore. send ( . suggestionWidget( . circularWidget( . widgetClicked) ) )
6785 }
86+
87+ XcodeInspector . shared. $activeApplication. sink { app in
88+ if !UserDefaults. shared. value ( for: \. showHideWidgetShortcutGlobally) {
89+ let shouldBeEnabled = if let app, app. isXcode || app. isExtensionService {
90+ true
91+ } else {
92+ false
93+ }
94+ if shouldBeEnabled {
95+ self . setupShortcutIfNeeded ( )
96+ } else {
97+ self . removeShortcutIfNeeded ( )
98+ }
99+ } else {
100+ self . setupShortcutIfNeeded ( )
101+ }
102+ } . store ( in: & cancellable)
103+ }
104+
105+ func setupShortcutIfNeeded( ) {
106+ KeyboardShortcuts . enable ( . showHideWidget)
107+ }
108+
109+ func removeShortcutIfNeeded( ) {
110+ KeyboardShortcuts . disable ( . showHideWidget)
68111 }
69112}
70113
0 commit comments