Skip to content

Commit 4d6b46a

Browse files
committed
Move GlobalShortcutManager to its own file
1 parent 257c025 commit 4d6b46a

File tree

2 files changed

+75
-72
lines changed

2 files changed

+75
-72
lines changed
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
import AppKit
2+
import Combine
3+
import Foundation
4+
import KeyboardShortcuts
5+
import XcodeInspector
6+
7+
extension KeyboardShortcuts.Name {
8+
static let showHideWidget = Self("ShowHideWidget")
9+
}
10+
11+
@MainActor
12+
final class GlobalShortcutManager {
13+
let guiController: GraphicalUserInterfaceController
14+
private var cancellable = Set<AnyCancellable>()
15+
16+
nonisolated init(guiController: GraphicalUserInterfaceController) {
17+
self.guiController = guiController
18+
}
19+
20+
func start() {
21+
KeyboardShortcuts.userDefaults = .shared
22+
setupShortcutIfNeeded()
23+
24+
KeyboardShortcuts.onKeyUp(for: .showHideWidget) { [guiController] in
25+
let isXCodeActive = XcodeInspector.shared.activeXcode != nil
26+
let isExtensionActive = NSApplication.shared.isActive
27+
28+
if !isXCodeActive,
29+
!guiController.viewStore.state.suggestionWidgetState.chatPanelState.isPanelDisplayed,
30+
UserDefaults.shared.value(for: \.showHideWidgetShortcutGlobally)
31+
{
32+
guiController.viewStore.send(.openChatPanel(forceDetach: true))
33+
} else {
34+
guiController.viewStore.send(.toggleWidgets)
35+
}
36+
37+
if !isExtensionActive {
38+
Task {
39+
try await Task.sleep(nanoseconds: 150_000_000)
40+
NSApplication.shared.activate(ignoringOtherApps: true)
41+
}
42+
} else if let previous = XcodeInspector.shared.previousActiveApplication,
43+
!previous.isActive
44+
{
45+
previous.runningApplication.activate()
46+
}
47+
}
48+
49+
XcodeInspector.shared.$activeApplication.sink { app in
50+
if !UserDefaults.shared.value(for: \.showHideWidgetShortcutGlobally) {
51+
let shouldBeEnabled = if let app, app.isXcode || app.isExtensionService {
52+
true
53+
} else {
54+
false
55+
}
56+
if shouldBeEnabled {
57+
self.setupShortcutIfNeeded()
58+
} else {
59+
self.removeShortcutIfNeeded()
60+
}
61+
} else {
62+
self.setupShortcutIfNeeded()
63+
}
64+
}.store(in: &cancellable)
65+
}
66+
67+
func setupShortcutIfNeeded() {
68+
KeyboardShortcuts.enable(.showHideWidget)
69+
}
70+
71+
func removeShortcutIfNeeded() {
72+
KeyboardShortcuts.disable(.showHideWidget)
73+
}
74+
}
75+

Core/Sources/Service/Service.swift

Lines changed: 0 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
1-
import AppKit
2-
import Combine
31
import Dependencies
42
import Foundation
5-
import KeyboardShortcuts
63
import Workspace
74
import WorkspaceSuggestionService
85
import XcodeInspector
@@ -16,10 +13,6 @@ import ProService
1613
public static let shared = TheActor()
1714
}
1815

19-
extension KeyboardShortcuts.Name {
20-
static let showHideWidget = Self("ShowHideWidget")
21-
}
22-
2316
/// The running extension service.
2417
public final class Service {
2518
public static let shared = Service()
@@ -70,68 +63,3 @@ public final class Service {
7063
}
7164
}
7265

73-
@MainActor
74-
final class GlobalShortcutManager {
75-
let guiController: GraphicalUserInterfaceController
76-
private var cancellable = Set<AnyCancellable>()
77-
78-
nonisolated init(guiController: GraphicalUserInterfaceController) {
79-
self.guiController = guiController
80-
}
81-
82-
func start() {
83-
KeyboardShortcuts.userDefaults = .shared
84-
setupShortcutIfNeeded()
85-
86-
KeyboardShortcuts.onKeyUp(for: .showHideWidget) { [guiController] in
87-
let isXCodeActive = XcodeInspector.shared.activeXcode != nil
88-
let isExtensionActive = NSApplication.shared.isActive
89-
90-
if !isXCodeActive,
91-
!guiController.viewStore.state.suggestionWidgetState.chatPanelState.isPanelDisplayed,
92-
UserDefaults.shared.value(for: \.showHideWidgetShortcutGlobally)
93-
{
94-
guiController.viewStore.send(.openChatPanel(forceDetach: true))
95-
} else {
96-
guiController.viewStore.send(.suggestionWidget(.circularWidget(.widgetClicked)))
97-
}
98-
99-
if !isExtensionActive {
100-
Task {
101-
try await Task.sleep(nanoseconds: 150_000_000)
102-
NSApplication.shared.activate(ignoringOtherApps: true)
103-
}
104-
} else if let previous = XcodeInspector.shared.previousActiveApplication,
105-
!previous.isActive
106-
{
107-
previous.runningApplication.activate()
108-
}
109-
}
110-
111-
XcodeInspector.shared.$activeApplication.sink { app in
112-
if !UserDefaults.shared.value(for: \.showHideWidgetShortcutGlobally) {
113-
let shouldBeEnabled = if let app, app.isXcode || app.isExtensionService {
114-
true
115-
} else {
116-
false
117-
}
118-
if shouldBeEnabled {
119-
self.setupShortcutIfNeeded()
120-
} else {
121-
self.removeShortcutIfNeeded()
122-
}
123-
} else {
124-
self.setupShortcutIfNeeded()
125-
}
126-
}.store(in: &cancellable)
127-
}
128-
129-
func setupShortcutIfNeeded() {
130-
KeyboardShortcuts.enable(.showHideWidget)
131-
}
132-
133-
func removeShortcutIfNeeded() {
134-
KeyboardShortcuts.disable(.showHideWidget)
135-
}
136-
}
137-

0 commit comments

Comments
 (0)