Skip to content

Commit 7d6256e

Browse files
author
ComputelessComputer
committed
Avoid launch-time status menu hangs
Move status menu exclusion loading off the launch path and stop collecting visible text when resolving the current activity context.
1 parent 1b899ba commit 7d6256e

3 files changed

Lines changed: 34 additions & 16 deletions

File tree

Sources/OpenbirdApp/AppModel.swift

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ final class AppModel: ObservableObject {
3333
let pattern: String
3434
}
3535

36+
static let empty = Self(app: nil, domain: nil)
37+
3638
let app: Action?
3739
let domain: Action?
3840

@@ -722,21 +724,23 @@ final class AppModel: ObservableObject {
722724
}
723725
}
724726

725-
func statusMenuExclusionState() -> StatusMenuExclusionState {
726-
let context = currentActivityContextService.currentContext()
727+
func loadStatusMenuState() async -> StatusMenuState {
728+
let context = await currentActivityContextService.currentContext()
727729
let appAction = excludableAppAction(for: context)
728730
let domainAction = excludableDomainAction(for: context)
729731

730-
return StatusMenuExclusionState(
731-
app: appAction,
732-
domain: domainAction
732+
return statusMenuState(
733+
exclusionState: StatusMenuExclusionState(
734+
app: appAction,
735+
domain: domainAction
736+
)
733737
)
734738
}
735739

736-
func statusMenuState() -> StatusMenuState {
740+
func statusMenuState(exclusionState: StatusMenuExclusionState = .empty) -> StatusMenuState {
737741
StatusMenuState(
738742
isCapturePaused: isCapturePaused,
739-
exclusionState: statusMenuExclusionState(),
743+
exclusionState: exclusionState,
740744
versionText: menuVersionText,
741745
updateStatusText: menuUpdateStatusText
742746
)

Sources/OpenbirdApp/OpenbirdAppMain.swift

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,10 @@ private struct OpenbirdStatusMenu: View {
183183
}
184184
.onAppear {
185185
state = model.statusMenuState()
186-
Task { await model.refreshCollectorState() }
186+
Task {
187+
await model.refreshCollectorState()
188+
state = await model.loadStatusMenuState()
189+
}
187190
}
188191
}
189192

Sources/OpenbirdKit/Services/CurrentActivityContextService.swift

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,17 @@ public struct CurrentActivityContextService: Sendable {
1919

2020
public init() {}
2121

22-
@MainActor
23-
public func currentContext() -> CurrentActivityContext? {
24-
guard let application = FrontmostApplicationContext.current() else {
22+
public func currentContext() async -> CurrentActivityContext? {
23+
guard let application = await MainActor.run(body: { FrontmostApplicationContext.current() }) else {
2524
return nil
2625
}
2726

28-
var snapshot = snapshotter.snapshotFrontmostWindow(for: application)
27+
var snapshot = await Task.detached(
28+
priority: .utility,
29+
operation: { @Sendable in
30+
snapshotter.snapshotFrontmostWindow(for: application, includeVisibleText: false)
31+
}
32+
).value
2933
?? WindowSnapshot(
3034
bundleId: application.bundleID,
3135
appName: application.appName,
@@ -36,10 +40,17 @@ public struct CurrentActivityContextService: Sendable {
3640
)
3741

3842
if snapshot.url == nil {
39-
snapshot.url = browserURLResolver.currentURL(
40-
for: snapshot.bundleId,
41-
windowTitle: snapshot.windowTitle
42-
)
43+
let bundleID = snapshot.bundleId
44+
let windowTitle = snapshot.windowTitle
45+
snapshot.url = await Task.detached(
46+
priority: .utility,
47+
operation: { @Sendable in
48+
browserURLResolver.currentURL(
49+
for: bundleID,
50+
windowTitle: windowTitle
51+
)
52+
}
53+
).value
4354
}
4455

4556
return CurrentActivityContext(

0 commit comments

Comments
 (0)