✨ add UserAttentionService to gate background pollers on window visibility#4406
Merged
Merged
Conversation
…ility Introduces an abstract UserAttentionService in commonMain that exposes per-surface visibility (main / search window) as StateFlows, plus two helpers: - attentionOn(vararg AttentionSurface) — OR-combines the requested surfaces into a single Flow<Boolean>. - CoroutineScope.launchWhileAttentive(attention, interval, runOnResume) — drives a polling loop only while attention is true; cancels cleanly on flip-false; re-fires immediately on resume so the user sees fresh state the moment they open the relevant window. Desktop binds DesktopUserAttentionService backed by DesktopAppWindowManager's mainWindowInfo / searchWindowInfo. Headless binds HeadlessUserAttentionService whose flows are permanently false, which naturally suppresses gated pollers in headless installs without explicit checks. DesktopNetworkProfileService is the first migration: its 60s Windows firewall scan now only runs while the main window is visible (the only surface that renders the warning dialog and the menu-bar warning entry). The 3s startup delay is preserved. Polling services that must keep running regardless of UI (sync, cleanup) intentionally do not subscribe — the abstraction is opt-in.
This was referenced May 16, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #4405
Summary
UserAttentionService(commonMain) exposes per-surface visibility asStateFlow<Boolean>— main window and search window. Comes with two helpers:attentionOn(vararg AttentionSurface)to combine surfaces, andCoroutineScope.launchWhileAttentive(...)to drive polling loops gated on attention.DesktopUserAttentionService(wrapsDesktopAppWindowManager); headless bindsHeadlessUserAttentionService(permanently false).DesktopNetworkProfileServiceis the first migration: the 60s Windows firewall scan now only runs while the main window is visible. On window-show the helper firesrunDetection()immediately, so the user sees fresh diagnosis the moment they open the window — not after waiting a poll interval.Design notes
SyncPollingManager/CleanSchedulerare not migrated — they need to run regardless of UI state. The abstraction is here for the pollers that only matter when a user can see the result.MAIN_WINDOWonly,SEARCH_WINDOWonly, or both viaattentionOn(MAIN_WINDOW, SEARCH_WINDOW). Network detection only renders warnings in the main window so it gates onMAIN_WINDOW.WindowInfo.showis the app's logical show state, so minimized windows still count as visible. If we later decide minimized should suspend polling, change the mapping inDesktopUserAttentionServiceonly — consumers don't need to change.runDetection()is never invoked in headless mode, without explicitif (headless)checks scattered through services.runOnResume = trueis the default so a user opening the main window sees a fresh diagnosis immediately, not after a 60s wait.Files
app/src/commonMain/kotlin/com/crosspaste/app/UserAttentionService.kt— newapp/src/desktopMain/kotlin/com/crosspaste/app/DesktopUserAttentionService.kt— newapp/src/desktopMain/kotlin/com/crosspaste/headless/HeadlessUserAttentionService.kt— newapp/src/desktopMain/kotlin/com/crosspaste/DesktopUiModule.kt— bindUserAttentionServiceto desktop implapp/src/desktopMain/kotlin/com/crosspaste/headless/HeadlessModule.kt— bind to headless implapp/src/desktopMain/kotlin/com/crosspaste/DesktopNetworkModule.kt— pass new dep intoDesktopNetworkProfileServiceapp/src/desktopMain/kotlin/com/crosspaste/net/DesktopNetworkProfileService.kt— migrate tolaunchWhileAttentiveTest plan
./gradlew app:desktopTest— all desktop tests pass.runDetectionlog lines until window reopens.