Skip to content

Commit d356b9b

Browse files
authored
fix(desktop): reveal hidden windows on a bounded fallback and before recovery dialogs (#61743)
1 parent 5aeb55d commit d356b9b

4 files changed

Lines changed: 301 additions & 33 deletions

File tree

‎packages/desktop/src/main/windows/index.ts‎

Lines changed: 32 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,12 @@ import { registerRendererProtocol, setProtocolReporter } from "./protocol"
2828
import { loadWindow } from "./scheme"
2929
import { createWindowRegistry } from "./registry"
3030
import { windowRecovery } from "./recovery"
31+
import { createRevealDriver } from "./reveal-machine"
3132
import { takeEarlyWindow, type EarlyWindow } from "./early"
3233
import { manageWindowState, readWindowState, resolveWindowState, windowStateFile } from "./window-state"
3334
import { allowRendererPermissions, wireNavigationPolicy, wireRendererHeaders } from "./security"
3435

35-
const themeReady = new WeakMap<BrowserWindow, () => void>()
36+
const revealDrivers = new WeakMap<BrowserWindow, ReturnType<typeof createRevealDriver>>()
3637

3738
const displays = {
3839
all: () => screen.getAllDisplays().map((display) => display.bounds),
@@ -120,7 +121,7 @@ export function onMainWindow(listener: (win: BrowserWindow, id: string) => void)
120121
}
121122

122123
export function setWindowThemeReady(win: BrowserWindow) {
123-
themeReady.get(win)?.()
124+
revealDrivers.get(win)?.dispatch({ type: "theme.ready" })
124125
}
125126

126127
export const mainWindows = Effect.fn("Window.make")(function* () {
@@ -176,50 +177,50 @@ export const mainWindows = Effect.fn("Window.make")(function* () {
176177
manageWindowState(win, stateFile, state, displays)
177178
}
178179

179-
wireWindowRecovery(win, id, () => relaunchHandler())
180-
register(win, id)
181-
wireFullscreen(win)
182-
wireFocus(win)
183-
184-
if (!early) loadWindow(win, "index.html")
185-
wireZoom(win)
186-
let contentReady = false
187-
let appliedTheme = false
188-
let revealed = !!early
189-
190180
const focusForTests = () => {
191181
if (app.isPackaged || process.env.OPENCODE_TEST_ONBOARDING !== "1") return
192182

193183
if (process.platform === "darwin") app.focus({ steal: true })
194184
win.focus()
195185
}
196186

187+
const reveal = createRevealDriver({
188+
early: !!early,
189+
show: (reason) => {
190+
if (win.isDestroyed()) return
191+
win.show()
192+
focusForTests()
193+
runFork(Effect.logInfo("main window visible", reason === "ready" ? { window: id } : { window: id, reason }))
194+
},
195+
})
196+
197+
revealDrivers.set(win, reveal)
198+
199+
wireWindowRecovery(win, id, {
200+
relaunch: () => relaunchHandler(),
201+
reveal: () => reveal.dispatch({ type: "recovery.requested" }),
202+
})
203+
register(win, id)
204+
wireFullscreen(win)
205+
wireFocus(win)
206+
207+
if (!early) loadWindow(win, "index.html")
208+
wireZoom(win)
209+
197210
if (early) {
198211
focusForTests()
199212
runFork(Effect.logInfo("main window visible", { window: id, shownAt: early.shownAt }))
200213
}
201214

202-
const reveal = () => {
203-
if (!contentReady || !appliedTheme || revealed || win.isDestroyed()) return
204-
revealed = true
205-
win.show()
206-
focusForTests()
207-
runFork(Effect.logInfo("main window visible", { window: id }))
208-
}
215+
const contentReady = () => reveal.dispatch({ type: "content.ready" })
209216

210-
const ready = () => {
211-
contentReady = true
212-
reveal()
213-
}
217+
win.once("ready-to-show", contentReady)
214218

215-
themeReady.set(win, () => {
216-
appliedTheme = true
217-
reveal()
219+
if (process.platform === "linux") win.webContents.once("did-finish-load", contentReady)
220+
win.once("closed", () => {
221+
reveal.dispatch({ type: "window.closed" })
222+
revealDrivers.delete(win)
218223
})
219-
win.once("ready-to-show", ready)
220-
221-
if (process.platform === "linux") win.webContents.once("did-finish-load", ready)
222-
win.once("closed", () => themeReady.delete(win))
223224

224225
return win
225226
}

‎packages/desktop/src/main/windows/recovery.ts‎

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,15 @@ export const windowRecovery = Effect.gen(function* () {
1313
const logging = yield* DesktopLogging.Service
1414
const createUnresponsiveSampler = yield* unresponsiveSampler
1515

16-
function wireWindowRecovery(win: BrowserWindow, name: string, relaunch: () => void) {
16+
function wireWindowRecovery(
17+
win: BrowserWindow,
18+
name: string,
19+
options: {
20+
readonly relaunch: () => void
21+
/** Shows a window that is still hidden; the dialog is modal to it. */
22+
readonly reveal: () => void
23+
},
24+
) {
1725
let showing = false
1826
const sampler = createUnresponsiveSampler(win, name)
1927

@@ -33,7 +41,7 @@ export const windowRecovery = Effect.gen(function* () {
3341

3442
if (action === "relaunch") {
3543
sampler.stopAndFlush()
36-
relaunch()
44+
options.relaunch()
3745

3846
return false
3947
}
@@ -51,6 +59,8 @@ export const windowRecovery = Effect.gen(function* () {
5159
showing = true
5260

5361
try {
62+
options.reveal()
63+
5464
while (!win.isDestroyed()) {
5565
const actions: { id: RecoveryAction; label: string }[] = wait
5666
? [
Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
import { afterEach, beforeEach, describe, expect, jest, test } from "bun:test"
2+
import {
3+
createRevealDriver,
4+
initialRevealState,
5+
REVEAL_TIMEOUT_MS,
6+
transitionReveal,
7+
type RevealCommand,
8+
type RevealEvent,
9+
type RevealReason,
10+
type RevealState,
11+
} from "./reveal-machine"
12+
13+
// Folds events through the machine and keeps the commands of each step.
14+
function replay(events: readonly RevealEvent[], from = initialRevealState(false)) {
15+
return events.reduce<{ state: RevealState; commands: (readonly RevealCommand[])[] }>(
16+
(result, event) => {
17+
const next = transitionReveal(result.state, event)
18+
19+
return { state: next.state, commands: [...result.commands, next.commands] }
20+
},
21+
{ state: from, commands: [] },
22+
)
23+
}
24+
25+
const shown = (reason: RevealReason): RevealCommand[] => [{ type: "cancel-timeout" }, { type: "show", reason }]
26+
27+
const hidden = {
28+
loading: [],
29+
"content-ready": [{ type: "content.ready" }],
30+
"theme-ready": [{ type: "theme.ready" }],
31+
} satisfies Record<string, RevealEvent[]>
32+
33+
describe("window reveal", () => {
34+
test("shows a window whose theme never arrives once the fallback expires", () => {
35+
const result = replay([{ type: "content.ready" }, { type: "reveal.timed-out" }, { type: "theme.ready" }])
36+
37+
expect(result.state.status).toBe("shown")
38+
expect(result.commands).toEqual([[], shown("timeout"), []])
39+
})
40+
41+
test.each(Object.entries(hidden))("shows a %s window before its recovery dialog", (_status, events) => {
42+
const result = replay([
43+
...events,
44+
{ type: "recovery.requested" },
45+
{ type: "reveal.timed-out" },
46+
{ type: "theme.ready" },
47+
])
48+
49+
expect(result.state.status).toBe("shown")
50+
expect(result.commands.slice(events.length)).toEqual([shown("recovery"), [], []])
51+
})
52+
53+
test.each([
54+
["content first", [{ type: "content.ready" }, { type: "theme.ready" }]],
55+
["theme first", [{ type: "theme.ready" }, { type: "content.ready" }]],
56+
] satisfies [string, RevealEvent[]][])("waits for content and theme, %s", (_order, events) => {
57+
expect(replay(events).commands).toEqual([[], shown("ready")])
58+
})
59+
60+
test("ignores the second content readiness Linux reports", () => {
61+
const ready = transitionReveal(initialRevealState(false), { type: "content.ready" }).state
62+
const again = transitionReveal(ready, { type: "content.ready" })
63+
64+
expect(again.state).toBe(ready)
65+
expect(again.commands).toEqual([])
66+
expect(transitionReveal(again.state, { type: "theme.ready" }).commands).toEqual(shown("ready"))
67+
})
68+
69+
test("never shows the early window again, including after a dev reload", () => {
70+
const early = initialRevealState(true)
71+
72+
const result = replay(
73+
[
74+
{ type: "content.ready" },
75+
{ type: "theme.ready" },
76+
{ type: "reveal.timed-out" },
77+
{ type: "recovery.requested" },
78+
{ type: "theme.ready" },
79+
],
80+
early,
81+
)
82+
83+
expect(early.status).toBe("shown")
84+
expect(result.state).toBe(early)
85+
expect(result.commands.flat()).toEqual([])
86+
})
87+
88+
test.each(Object.entries(hidden))("cancels the fallback when a %s window closes", (_status, events) => {
89+
const result = replay([...events, { type: "window.closed" }])
90+
91+
expect(result.state.status).toBe("closed")
92+
expect(result.commands.at(-1)).toEqual([{ type: "cancel-timeout" }])
93+
})
94+
95+
test("ignores every event after the window closes", () => {
96+
const closed = replay([{ type: "content.ready" }, { type: "theme.ready" }, { type: "window.closed" }])
97+
98+
const after = replay(
99+
[
100+
{ type: "content.ready" },
101+
{ type: "theme.ready" },
102+
{ type: "reveal.timed-out" },
103+
{ type: "recovery.requested" },
104+
{ type: "window.closed" },
105+
],
106+
closed.state,
107+
)
108+
109+
expect(closed.commands.at(-1)).toEqual([])
110+
expect(after.state).toBe(closed.state)
111+
expect(after.commands.flat()).toEqual([])
112+
})
113+
})
114+
115+
describe("window reveal driver", () => {
116+
beforeEach(() => {
117+
jest.useFakeTimers()
118+
})
119+
120+
afterEach(() => {
121+
jest.useRealTimers()
122+
})
123+
124+
test("shows a hidden window once when the fallback expires", () => {
125+
const shows: RevealReason[] = []
126+
const reveal = createRevealDriver({ early: false, show: (reason) => shows.push(reason) })
127+
128+
reveal.dispatch({ type: "content.ready" })
129+
jest.advanceTimersByTime(REVEAL_TIMEOUT_MS - 1)
130+
expect(shows).toEqual([])
131+
132+
jest.advanceTimersByTime(1)
133+
reveal.dispatch({ type: "theme.ready" })
134+
expect(shows).toEqual(["timeout"])
135+
})
136+
137+
test("does not fire the fallback after a ready reveal or a close", () => {
138+
const shows: RevealReason[] = []
139+
const ready = createRevealDriver({ early: false, show: (reason) => shows.push(reason) })
140+
const closed = createRevealDriver({ early: false, show: (reason) => shows.push(reason) })
141+
142+
ready.dispatch({ type: "theme.ready" })
143+
ready.dispatch({ type: "content.ready" })
144+
closed.dispatch({ type: "window.closed" })
145+
jest.runOnlyPendingTimers()
146+
147+
expect(shows).toEqual(["ready"])
148+
expect(jest.getTimerCount()).toBe(0)
149+
})
150+
151+
test("arms no fallback for the early window", () => {
152+
const shows: RevealReason[] = []
153+
createRevealDriver({ early: true, show: (reason) => shows.push(reason) })
154+
155+
expect(jest.getTimerCount()).toBe(0)
156+
expect(shows).toEqual([])
157+
})
158+
})
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
/** How long a hidden window waits for its content and theme before it shows anyway. */
2+
export const REVEAL_TIMEOUT_MS = 3_000
3+
4+
export type RevealReason = "ready" | "timeout" | "recovery"
5+
6+
/**
7+
* A new window stays hidden until its content has loaded and the renderer has applied its theme, so it never
8+
* flashes unthemed. The early window was shown before adoption and starts `shown`.
9+
*/
10+
export type RevealState =
11+
| { readonly status: "loading" }
12+
| { readonly status: "content-ready" }
13+
| { readonly status: "theme-ready" }
14+
| { readonly status: "shown" }
15+
| { readonly status: "closed" }
16+
17+
export type RevealEvent =
18+
| { readonly type: "content.ready" }
19+
| { readonly type: "theme.ready" }
20+
| { readonly type: "reveal.timed-out" }
21+
| { readonly type: "recovery.requested" }
22+
| { readonly type: "window.closed" }
23+
24+
export type RevealCommand =
25+
| { readonly type: "show"; readonly reason: RevealReason }
26+
| { readonly type: "cancel-timeout" }
27+
28+
export type RevealTransition = {
29+
readonly state: RevealState
30+
readonly commands: readonly RevealCommand[]
31+
}
32+
33+
const LOADING: RevealState = Object.freeze({ status: "loading" })
34+
35+
const CONTENT_READY: RevealState = Object.freeze({ status: "content-ready" })
36+
37+
const THEME_READY: RevealState = Object.freeze({ status: "theme-ready" })
38+
39+
const SHOWN: RevealState = Object.freeze({ status: "shown" })
40+
41+
const CLOSED: RevealState = Object.freeze({ status: "closed" })
42+
43+
export function initialRevealState(early: boolean): RevealState {
44+
return early ? SHOWN : LOADING
45+
}
46+
47+
export function transitionReveal(state: RevealState, event: RevealEvent): RevealTransition {
48+
if (state.status === "closed") return unchanged(state)
49+
50+
if (event.type === "window.closed")
51+
return { state: CLOSED, commands: state.status === "shown" ? [] : [{ type: "cancel-timeout" }] }
52+
53+
if (state.status === "shown") return unchanged(state)
54+
55+
if (event.type === "reveal.timed-out") return show("timeout")
56+
57+
// A recovery dialog is modal to the window, so the window has to be visible for the user to see it.
58+
if (event.type === "recovery.requested") return show("recovery")
59+
60+
if (event.type === "content.ready") {
61+
if (state.status === "theme-ready") return show("ready")
62+
63+
// Linux reports content readiness twice (`ready-to-show` and `did-finish-load`).
64+
return state.status === "content-ready" ? unchanged(state) : { state: CONTENT_READY, commands: [] }
65+
}
66+
67+
if (state.status === "content-ready") return show("ready")
68+
69+
return state.status === "theme-ready" ? unchanged(state) : { state: THEME_READY, commands: [] }
70+
}
71+
72+
function show(reason: RevealReason): RevealTransition {
73+
return { state: SHOWN, commands: [{ type: "cancel-timeout" }, { type: "show", reason }] }
74+
}
75+
76+
function unchanged(state: RevealState): RevealTransition {
77+
return { state, commands: [] }
78+
}
79+
80+
/** Runs one window's reveal machine and owns its fallback timer. */
81+
export function createRevealDriver(input: { readonly early: boolean; readonly show: (reason: RevealReason) => void }) {
82+
const current = { state: initialRevealState(input.early) }
83+
84+
const dispatch = (event: RevealEvent) => {
85+
const next = transitionReveal(current.state, event)
86+
current.state = next.state
87+
next.commands.forEach(perform)
88+
}
89+
90+
const timer = input.early ? undefined : setTimeout(() => dispatch({ type: "reveal.timed-out" }), REVEAL_TIMEOUT_MS)
91+
92+
const perform = (command: RevealCommand) => {
93+
if (command.type === "cancel-timeout") return clearTimeout(timer)
94+
95+
input.show(command.reason)
96+
}
97+
98+
return { dispatch }
99+
}

0 commit comments

Comments
 (0)