Skip to content

Commit ad167e7

Browse files
committed
Adjust API of ActiveApplicationMonitor
1 parent f0634b9 commit ad167e7

File tree

9 files changed

+45
-46
lines changed

9 files changed

+45
-46
lines changed

Core/Sources/Service/GUI/PromptToCodeProvider+Service.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ extension PromptToCodeProvider {
5959
Task {
6060
let handler = PseudoCommandHandler()
6161
await handler.acceptSuggestion()
62-
if let app = ActiveApplicationMonitor.previousActiveApplication, app.isXcode {
62+
if let app = ActiveApplicationMonitor.shared.previousApp, app.isXcode {
6363
try await Task.sleep(nanoseconds: 200_000_000)
6464
app.activate()
6565
}

Core/Sources/Service/GUI/WidgetDataSource.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ final class WidgetDataSource {
6060
self?.removePromptToCode(for: url)
6161
let presenter = PresentInWindowSuggestionPresenter()
6262
presenter.closePromptToCode(fileURL: url)
63-
if let app = ActiveApplicationMonitor.previousActiveApplication, app.isXcode {
63+
if let app = ActiveApplicationMonitor.shared.previousApp, app.isXcode {
6464
Task { @MainActor in
6565
try await Task.sleep(nanoseconds: 200_000_000)
6666
app.activate()
@@ -113,7 +113,7 @@ extension WidgetDataSource: SuggestionWidgetDataSource {
113113
Task {
114114
let handler = PseudoCommandHandler()
115115
await handler.rejectSuggestions()
116-
if let app = ActiveApplicationMonitor.previousActiveApplication,
116+
if let app = ActiveApplicationMonitor.shared.previousApp,
117117
app.isXcode
118118
{
119119
try await Task.sleep(nanoseconds: 200_000_000)
@@ -125,7 +125,7 @@ extension WidgetDataSource: SuggestionWidgetDataSource {
125125
Task {
126126
let handler = PseudoCommandHandler()
127127
await handler.acceptSuggestion()
128-
if let app = ActiveApplicationMonitor.previousActiveApplication,
128+
if let app = ActiveApplicationMonitor.shared.previousApp,
129129
app.isXcode
130130
{
131131
try await Task.sleep(nanoseconds: 200_000_000)

Core/Sources/Service/RealtimeSuggestionController.swift

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,21 +26,21 @@ public class RealtimeSuggestionController {
2626

2727
func start() {
2828
Task { [weak self] in
29-
if let app = ActiveApplicationMonitor.activeXcode {
29+
if let app = ActiveApplicationMonitor.shared.activeXcode {
3030
self?.handleXcodeChanged(app)
3131
self?.startHIDObservation()
3232
}
33-
var previousApp = ActiveApplicationMonitor.activeXcode
34-
for await app in ActiveApplicationMonitor.createStream() {
33+
var previousApp = ActiveApplicationMonitor.shared.activeXcode
34+
for await app in ActiveApplicationMonitor.shared.createStream() {
3535
guard let self else { return }
3636
try Task.checkCancellation()
3737
defer { previousApp = app }
3838

39-
if let app = ActiveApplicationMonitor.activeXcode, app != previousApp {
39+
if let app = ActiveApplicationMonitor.shared.activeXcode, app != previousApp {
4040
self.handleXcodeChanged(app)
4141
}
4242

43-
if ActiveApplicationMonitor.activeXcode != nil {
43+
if ActiveApplicationMonitor.shared.activeXcode != nil {
4444
startHIDObservation()
4545
} else {
4646
stopHIDObservation()
@@ -91,7 +91,7 @@ public class RealtimeSuggestionController {
9191
}
9292

9393
private func handleFocusElementChange() {
94-
guard let activeXcode = ActiveApplicationMonitor.activeXcode else { return }
94+
guard let activeXcode = ActiveApplicationMonitor.shared.activeXcode else { return }
9595
let application = AXUIElementCreateApplication(activeXcode.processIdentifier)
9696
guard let focusElement = application.focusedElement else { return }
9797
let focusElementType = focusElement.description
@@ -226,7 +226,7 @@ public class RealtimeSuggestionController {
226226
/// Looks like the Xcode will keep the panel around until content is changed,
227227
/// not sure how to observe that it's hidden.
228228
func isCompletionPanelPresenting() -> Bool {
229-
guard let activeXcode = ActiveApplicationMonitor.activeXcode else { return false }
229+
guard let activeXcode = ActiveApplicationMonitor.shared.activeXcode else { return false }
230230
let application = AXUIElementCreateApplication(activeXcode.processIdentifier)
231231
return application.focusedWindow?.child(identifier: "_XC_COMPLETION_TABLE_") != nil
232232
}

Core/Sources/Service/ScheduledCleaner.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public final class ScheduledCleaner {
2929

3030
// cleanup when Xcode becomes inactive
3131
Task { @ServiceActor in
32-
for await app in ActiveApplicationMonitor.createStream() {
32+
for await app in ActiveApplicationMonitor.shared.createStream() {
3333
try Task.checkCancellation()
3434
if let app, !app.isXcode {
3535
await cleanUp()

Core/Sources/Service/SuggestionCommandHandler/PseudoCommandHandler.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -168,8 +168,8 @@ struct PseudoCommandHandler {
168168
}
169169
try await Environment.triggerAction("Accept Suggestion")
170170
} catch {
171-
guard let xcode = ActiveApplicationMonitor.activeXcode ?? ActiveApplicationMonitor
172-
.latestXcode else { return }
171+
guard let xcode = ActiveApplicationMonitor.shared.activeXcode
172+
?? ActiveApplicationMonitor.shared.latestXcode else { return }
173173
let application = AXUIElementCreateApplication(xcode.processIdentifier)
174174
guard let focusElement = application.focusedElement,
175175
focusElement.description == "Source Editor"
@@ -260,8 +260,8 @@ extension PseudoCommandHandler {
260260
cursorPosition: CursorPosition
261261
)?
262262
{
263-
guard let xcode = ActiveApplicationMonitor.activeXcode
264-
?? ActiveApplicationMonitor.latestXcode else { return nil }
263+
guard let xcode = ActiveApplicationMonitor.shared.activeXcode
264+
?? ActiveApplicationMonitor.shared.latestXcode else { return nil }
265265
let application = AXUIElementCreateApplication(xcode.processIdentifier)
266266
guard let focusElement = sourceEditor ?? application.focusedElement,
267267
focusElement.description == "Source Editor"
@@ -282,7 +282,7 @@ extension PseudoCommandHandler {
282282
guard
283283
let fileURL = await getFileURL(),
284284
let (_, filespace) = try? await Service.shared.workspacePool
285-
.fetchOrCreateWorkspaceAndFilespace(fileURL: fileURL)
285+
.fetchOrCreateWorkspaceAndFilespace(fileURL: fileURL)
286286
else { return nil }
287287
return filespace
288288
}

Core/Sources/SuggestionWidget/FeatureReducers/WidgetFeature.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ public struct WidgetFeature: ReducerProtocol {
149149
}
150150
return .run { _ in
151151
guard isDisplayingContent else { return }
152-
if let app = activeApplicationMonitor.previousActiveApplication, app.isXcode {
152+
if let app = activeApplicationMonitor.previousApp, app.isXcode {
153153
try await Task.sleep(nanoseconds: 200_000_000)
154154
app.activate()
155155
}

Core/Sources/SuggestionWidget/ModuleDependency.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ struct XcodeInspectorKey: DependencyKey {
7171
}
7272

7373
struct ActiveApplicationMonitorKey: DependencyKey {
74-
static let liveValue = ActiveApplicationMonitor.self
74+
static let liveValue = ActiveApplicationMonitor.shared
7575
}
7676

7777
struct ChatTabBuilderCollectionKey: DependencyKey {
@@ -83,7 +83,7 @@ struct ChatTabBuilderCollectionKey: DependencyKey {
8383
struct ActivatePreviouslyActiveXcodeKey: DependencyKey {
8484
static let liveValue = { @MainActor in
8585
@Dependency(\.activeApplicationMonitor) var activeApplicationMonitor
86-
if let app = activeApplicationMonitor.previousActiveApplication, app.isXcode {
86+
if let app = activeApplicationMonitor.previousApp, app.isXcode {
8787
try? await Task.sleep(nanoseconds: 200_000_000)
8888
app.activate()
8989
}
@@ -120,7 +120,7 @@ extension DependencyValues {
120120
set { self[XcodeInspectorKey.self] = newValue }
121121
}
122122

123-
var activeApplicationMonitor: ActiveApplicationMonitor.Type {
123+
var activeApplicationMonitor: ActiveApplicationMonitor {
124124
get { self[ActiveApplicationMonitorKey.self] }
125125
set { self[ActiveApplicationMonitorKey.self] = newValue }
126126
}

Tool/Sources/ActiveApplicationMonitor/ActiveApplicationMonitor.swift

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
import AppKit
22

33
public final class ActiveApplicationMonitor {
4-
static let shared = ActiveApplicationMonitor()
5-
var latestXcode: NSRunningApplication? = NSWorkspace.shared.runningApplications
4+
public static let shared = ActiveApplicationMonitor()
5+
public private(set) var latestXcode: NSRunningApplication? = NSWorkspace.shared
6+
.runningApplications
67
.first(where: \.isXcode)
7-
var previousApp: NSRunningApplication?
8-
var activeApplication = NSWorkspace.shared.runningApplications.first(where: \.isActive) {
8+
public private(set) var previousApp: NSRunningApplication?
9+
public private(set) var activeApplication = NSWorkspace.shared.runningApplications
10+
.first(where: \.isActive)
11+
{
912
didSet {
1013
if activeApplication?.isXcode ?? false {
1114
latestXcode = activeApplication
@@ -36,27 +39,23 @@ public final class ActiveApplicationMonitor {
3639
}
3740
}
3841

39-
public static var activeApplication: NSRunningApplication? { shared.activeApplication }
40-
41-
public static var previousActiveApplication: NSRunningApplication? { shared.previousApp }
42-
43-
public static var activeXcode: NSRunningApplication? {
42+
public var activeXcode: NSRunningApplication? {
4443
if activeApplication?.isXcode ?? false {
4544
return activeApplication
4645
}
4746
return nil
4847
}
4948

50-
public static var latestXcode: NSRunningApplication? { shared.latestXcode }
51-
52-
public static func createStream() -> AsyncStream<NSRunningApplication?> {
49+
public func createStream() -> AsyncStream<NSRunningApplication?> {
5350
.init { continuation in
5451
let id = UUID()
55-
ActiveApplicationMonitor.shared.addContinuation(continuation, id: id)
56-
continuation.onTermination = { _ in
57-
ActiveApplicationMonitor.shared.removeContinuation(id: id)
52+
Task { @MainActor in
53+
continuation.onTermination = { _ in
54+
self.removeContinuation(id: id)
55+
}
56+
addContinuation(continuation, id: id)
57+
continuation.yield(activeApplication)
5858
}
59-
continuation.yield(activeApplication)
6059
}
6160
}
6261

Tool/Sources/Environment/Environment.swift

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public enum Environment {
2525
public static var now = { Date() }
2626

2727
public static var isXcodeActive: () async -> Bool = {
28-
ActiveApplicationMonitor.activeXcode != nil
28+
ActiveApplicationMonitor.shared.activeXcode != nil
2929
}
3030

3131
public static var frontmostXcodeWindowIsEditor: () async -> Bool = {
@@ -43,8 +43,8 @@ public enum Environment {
4343
}
4444

4545
public static var fetchCurrentProjectRootURLFromXcode: () async throws -> URL? = {
46-
if let xcode = ActiveApplicationMonitor.activeXcode
47-
?? ActiveApplicationMonitor.latestXcode
46+
if let xcode = ActiveApplicationMonitor.shared.activeXcode
47+
?? ActiveApplicationMonitor.shared.latestXcode
4848
{
4949
let application = AXUIElementCreateApplication(xcode.processIdentifier)
5050
let focusedWindow = application.focusedWindow
@@ -84,8 +84,8 @@ public enum Environment {
8484
}
8585

8686
public static var fetchCurrentFileURL: () async throws -> URL = {
87-
guard let xcode = ActiveApplicationMonitor.activeXcode
88-
?? ActiveApplicationMonitor.latestXcode
87+
guard let xcode = ActiveApplicationMonitor.shared.activeXcode
88+
?? ActiveApplicationMonitor.shared.latestXcode
8989
else {
9090
throw FailedToFetchFileURLError()
9191
}
@@ -111,8 +111,8 @@ public enum Environment {
111111
}
112112

113113
public static var fetchFocusedElementURI: () async throws -> URL = {
114-
guard let xcode = ActiveApplicationMonitor.activeXcode
115-
?? ActiveApplicationMonitor.latestXcode
114+
guard let xcode = ActiveApplicationMonitor.shared.activeXcode
115+
?? ActiveApplicationMonitor.shared.latestXcode
116116
else { return URL(fileURLWithPath: "/global") }
117117

118118
let application = AXUIElementCreateApplication(xcode.processIdentifier)
@@ -134,8 +134,8 @@ public enum Environment {
134134
}
135135

136136
public static var triggerAction: (_ name: String) async throws -> Void = { name in
137-
guard let activeXcode = ActiveApplicationMonitor.activeXcode
138-
?? ActiveApplicationMonitor.latestXcode
137+
guard let activeXcode = ActiveApplicationMonitor.shared.activeXcode
138+
?? ActiveApplicationMonitor.shared.latestXcode
139139
else { return }
140140
let bundleName = Bundle.main
141141
.object(forInfoDictionaryKey: "EXTENSION_BUNDLE_NAME") as! String

0 commit comments

Comments
 (0)