Skip to content

Commit ec8fc90

Browse files
committed
fix(linux): keep the Wayland camera preview across the editor and Notes off the bar
GNOME's mutter places every new window itself and cascades it 50px down-right of the window whose corner it lands on; a Wayland client cannot choose otherwise (parent, focus, type and always-on-top all measured to change nothing). - The camera preview window now outlives its HUD on Wayland, parked at 1x1 while the editor is open, and the next HUD adopts it, so it comes back where it was dragged. main.ts destroys it when it and the countdown are the last windows, so closing the editor still quits. - Notes opens 402px tall on Wayland instead of 540, so a Notes window cascaded one or two steps from the HUD's corner ends above the bar instead of covering it. (cherry picked from commit a859c0c)
1 parent 1cda7a0 commit ec8fc90

8 files changed

Lines changed: 160 additions & 21 deletions

‎electron/cameraPreview.test.ts‎

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -439,6 +439,51 @@ describe("floating camera preview parking (Wayland)", () => {
439439
expect(overlay.showInactive).toHaveBeenCalledTimes(2);
440440
});
441441

442+
it("keeps the preview parked for the next HUD across an editor round trip", () => {
443+
// The HUD closes when the editor opens. A preview destroyed with it was placed anew by
444+
// the compositor for the next HUD, top-left on GNOME, wherever the user had dragged it.
445+
const { preview, sender, owner, overlay, create, park, resize } = parkingSetup();
446+
preview.configure(sender, true, true);
447+
overlay.emit("ready-to-show");
448+
resize.mockClear();
449+
450+
owner.emit("destroyed");
451+
expect(overlay.destroy).not.toHaveBeenCalled();
452+
expect(park).toHaveBeenCalledOnce();
453+
preview.frame(sender, jpeg);
454+
expect(overlay.webContents.send).not.toHaveBeenCalled();
455+
456+
const nextHud = Object.assign(new EventEmitter(), { send: vi.fn() });
457+
const nextSender = nextHud as unknown as WebContents;
458+
expect(preview.ensure(nextSender)).toBe(overlay);
459+
expect(preview.ensure(sender)).toBeNull();
460+
preview.configure(nextSender, true, true);
461+
expect(create).toHaveBeenCalledOnce();
462+
expect(resize).toHaveBeenCalledOnce();
463+
expect(overlay.showInactive).toHaveBeenCalledTimes(2);
464+
preview.frame(nextSender, jpeg);
465+
expect(overlay.webContents.send).toHaveBeenCalledWith("camera-preview:frame", jpeg);
466+
467+
// The next HUD's own destruction is still heard.
468+
nextHud.emit("destroyed");
469+
expect(park).toHaveBeenCalledTimes(2);
470+
});
471+
472+
it("keeps the size the user picked across the round trip", () => {
473+
const { preview, sender, owner, overlay, overlaySender, resize } = parkingSetup();
474+
preview.configure(sender, true, true);
475+
overlay.emit("ready-to-show");
476+
preview.resize(overlaySender, CAMERA_PREVIEW_SIZES[0]);
477+
owner.emit("destroyed");
478+
resize.mockClear();
479+
480+
const nextSender = Object.assign(new EventEmitter(), {
481+
send: vi.fn(),
482+
}) as unknown as WebContents;
483+
preview.configure(nextSender, true, true);
484+
expect(resize).toHaveBeenCalledWith(overlay, CAMERA_PREVIEW_SIZES[0], expect.any(Number));
485+
});
486+
442487
it("hides a preview that was never shown: there is no position to keep", () => {
443488
const { preview, sender, overlay, park } = parkingSetup();
444489
preview.configure(sender, true, false);

‎electron/cameraPreview.ts‎

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,11 @@ export class CameraPreviewController {
6767
this.owner = owner;
6868
owner.send("camera-preview:availability", this.captureAllowed);
6969
this.onOwnerDestroyed = () => {
70-
if (this.owner === owner) this.dispose();
70+
if (this.owner !== owner) return;
71+
// Wayland keeps the window for the next HUD, so an editor round trip does not
72+
// hand it back to the compositor to place anew.
73+
if (this.parkWindow) this.release();
74+
else this.dispose();
7175
};
7276
owner.once("destroyed", this.onOwnerDestroyed);
7377
// Windows and macOS minimize the HUD, Linux hides it; the tray brings it back.
@@ -193,7 +197,7 @@ export class CameraPreviewController {
193197
this.window.webContents.send("camera-preview:frame", jpeg);
194198
}
195199

196-
dispose(): void {
200+
private detachOwner(): void {
197201
if (this.owner && this.onOwnerDestroyed) {
198202
this.owner.removeListener("destroyed", this.onOwnerDestroyed);
199203
}
@@ -203,16 +207,30 @@ export class CameraPreviewController {
203207
}
204208
this.hud = null;
205209
this.hudListeners = [];
206-
this.ready = false;
207-
this.parked = false;
208-
const previewWindow = this.window;
209-
this.window = null;
210210
this.owner = null;
211-
previewWindow?.destroy();
212211
this.visible = false;
213212
this.captureAllowed = true;
214213
this.ownerShown = true;
215214
this.captureGeneration++;
215+
}
216+
217+
/**
218+
* The HUD is gone (the editor opened) but its window stays, parked where the user left it,
219+
* with its size, for the next HUD to adopt: the window outlives HUDs only where it is parked.
220+
* main.ts destroys it if it is all that is left open, so it never holds the app up.
221+
*/
222+
private release(): void {
223+
if (this.window) this.conceal(this.window);
224+
this.detachOwner();
225+
}
226+
227+
dispose(): void {
228+
this.detachOwner();
229+
this.ready = false;
230+
this.parked = false;
231+
const previewWindow = this.window;
232+
this.window = null;
233+
previewWindow?.destroy();
216234
this.width = CAMERA_PREVIEW_DEFAULT_SIZE;
217235
this.aspectRatio = CAMERA_PREVIEW_DEFAULT_ASPECT_RATIO;
218236
}

‎electron/hudWindowBounds.test.ts‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,12 @@ import {
55
hudBarAnchor,
66
hudDragDestination,
77
hudResizeBounds,
8+
NOTES_WINDOW_HEIGHT,
9+
NOTES_WINDOW_MIN_HEIGHT,
10+
notesWindowHeight,
811
parseHudContentRect,
12+
WAYLAND_CASCADE_STEP,
13+
WAYLAND_HUD_BAR_TOP,
914
} from "./hudWindowBounds";
1015

1116
// 1920x1080 with a 48px taskbar at the bottom: the work area stops above it.
@@ -302,3 +307,23 @@ describe("hud content rect parsing", () => {
302307
expect(parseHudContentRect({ x: 0, y: 0, width: 560 })).toBeNull();
303308
});
304309
});
310+
311+
describe("Notes opening height (GNOME Wayland)", () => {
312+
it("keeps its height where the app places its own windows", () => {
313+
expect(notesWindowHeight(false)).toBe(NOTES_WINDOW_HEIGHT);
314+
});
315+
316+
it("ends above the HUD bar when mutter cascades it one or two steps from the HUD", () => {
317+
// rc.2: Notes opened 540px tall, cascaded from the HUD's corner, over the bar.
318+
const height = notesWindowHeight(true);
319+
for (const steps of [1, 2]) {
320+
expect(steps * WAYLAND_CASCADE_STEP + height).toBeLessThanOrEqual(WAYLAND_HUD_BAR_TOP);
321+
}
322+
// What it replaces: one step down at 540px reached into the bar.
323+
expect(WAYLAND_CASCADE_STEP + NOTES_WINDOW_HEIGHT).toBeGreaterThan(WAYLAND_HUD_BAR_TOP);
324+
});
325+
326+
it("never opens below its own minimum height", () => {
327+
expect(notesWindowHeight(true)).toBeGreaterThanOrEqual(NOTES_WINDOW_MIN_HEIGHT);
328+
});
329+
});

‎electron/hudWindowBounds.ts‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,47 @@
1818
// Pure geometry on purpose: `screen` and the window are the caller's job, so the
1919
// rules can be tested without an Electron runtime.
2020

21+
import {
22+
HUD_EDGE_SLACK,
23+
HUD_MODAL_MAX_HEIGHT,
24+
HUD_POPOVER_GAP,
25+
} from "../src/components/launch/hudGeometry";
2126
import {
2227
clampRectToWorkArea,
2328
type DisplayWorkArea,
2429
type EditorWindowRect,
2530
} from "./editorWindowState";
2631

32+
/** The Notes window's opening height, and the least it can be resized to. */
33+
export const NOTES_WINDOW_HEIGHT = 540;
34+
export const NOTES_WINDOW_MIN_HEIGHT = 400;
35+
36+
/**
37+
* How far GNOME's mutter cascades a new window from the one whose top-left corner it lands
38+
* on: a titlebar height, measured 50px on GNOME 46 (v2.3.0-rc.2, 1920×1080 at 100%).
39+
*/
40+
export const WAYLAND_CASCADE_STEP = 50;
41+
42+
/**
43+
* Where the bar's top sits in the Wayland HUD window, from the window's top edge: the window
44+
* keeps the tallest panel's room above the bar (`computeHudLinuxStack`), so this is that room
45+
* plus the edge slack. Measured 503px on a 1080p GNOME desktop.
46+
*/
47+
export const WAYLAND_HUD_BAR_TOP = HUD_EDGE_SLACK + HUD_POPOVER_GAP + HUD_MODAL_MAX_HEIGHT;
48+
49+
/**
50+
* The height Notes opens at. A Wayland client cannot place its window, and mutter opens Notes
51+
* cascaded down and right of the HUD's top-left corner, one step per window already on that
52+
* diagonal, so its left half lands on the bar below the HUD's empty reserve. At 540px it
53+
* covered the controls from Microphone to Notes. Short enough to end above the bar two steps
54+
* down, it covers only the reserve; the user can still make it taller.
55+
*/
56+
export function notesWindowHeight(wayland: boolean): number {
57+
if (!wayland) return NOTES_WINDOW_HEIGHT;
58+
const clearOfBar = WAYLAND_HUD_BAR_TOP - 2 * WAYLAND_CASCADE_STEP;
59+
return Math.max(NOTES_WINDOW_MIN_HEIGHT, Math.min(NOTES_WINDOW_HEIGHT, clearOfBar));
60+
}
61+
2762
/** The HUD's own floor, matching `minWidth`/`minHeight` at construction. */
2863
export const HUD_WINDOW_MIN = { width: 120, height: 80 };
2964

‎electron/lingeringOverlay.test.ts‎

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,26 @@ const window = (destroyed = false) => ({ isDestroyed: () => destroyed });
66
describe("isOnlyLingeringOverlay", () => {
77
it("is true when the hidden overlay is the last window open", () => {
88
const overlay = window();
9-
expect(isOnlyLingeringOverlay([overlay], overlay)).toBe(true);
10-
expect(isOnlyLingeringOverlay([overlay, window(true)], overlay)).toBe(true);
9+
expect(isOnlyLingeringOverlay([overlay], [overlay])).toBe(true);
10+
expect(isOnlyLingeringOverlay([overlay, window(true)], [overlay])).toBe(true);
1111
});
1212

1313
it("is false while the HUD or the editor is still open", () => {
1414
const overlay = window();
15-
expect(isOnlyLingeringOverlay([window(), overlay], overlay)).toBe(false);
15+
expect(isOnlyLingeringOverlay([window(), overlay], [overlay, null])).toBe(false);
16+
});
17+
18+
it("is true when the countdown and a parked camera preview are all that is left", () => {
19+
const countdown = window();
20+
const preview = window();
21+
expect(isOnlyLingeringOverlay([preview, countdown], [countdown, preview])).toBe(true);
22+
expect(isOnlyLingeringOverlay([preview], [null, preview])).toBe(true);
23+
expect(isOnlyLingeringOverlay([preview, window()], [countdown, preview])).toBe(false);
1624
});
1725

1826
it("is false without an overlay to close", () => {
19-
expect(isOnlyLingeringOverlay([], null)).toBe(false);
27+
expect(isOnlyLingeringOverlay([], [null])).toBe(false);
2028
const gone = window(true);
21-
expect(isOnlyLingeringOverlay([gone], gone)).toBe(false);
29+
expect(isOnlyLingeringOverlay([gone], [gone])).toBe(false);
2230
});
2331
});

‎electron/lingeringOverlay.ts‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
// The countdown overlay is created on the first take and only hidden between takes, so it is
22
// ready to paint the next countdown at once. A hidden window still counts for Electron, so
33
// once the HUD and the editor were gone `window-all-closed` never fired: OpenScreen stayed
4-
// up with no window and only the tray icon left (issue #961). `main.ts` closes the overlay
5-
// when it is the last window standing.
4+
// up with no window and only the tray icon left (issue #961). On Wayland the camera preview
5+
// outlives its HUD too, parked while the editor is open (see CameraPreviewController).
6+
// `main.ts` closes these overlays when they are the last windows standing.
67

78
/** The slice of `BrowserWindow` this needs, so the rule can be tested without Electron. */
89
export type LingeringWindow = { isDestroyed(): boolean };
910

10-
/** True when `overlay` is the only window still open, so it alone holds the app up. */
11+
/** True when only `overlays` are still open, so they alone hold the app up. */
1112
export function isOnlyLingeringOverlay(
1213
open: readonly LingeringWindow[],
13-
overlay: LingeringWindow | null,
14+
overlays: readonly (LingeringWindow | null)[],
1415
): boolean {
15-
if (!overlay || overlay.isDestroyed()) return false;
1616
const alive = open.filter((window) => !window.isDestroyed());
17-
return alive.length === 1 && alive[0] === overlay;
17+
return alive.length > 0 && alive.every((window) => overlays.includes(window));
1818
}

‎electron/main.ts‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ import {
8989
createHudOverlayWindow,
9090
createNotesWindow,
9191
createSourceSelectorWindow,
92+
getCameraPreviewWindow,
9293
} from "./windows";
9394
import { isWindowsUpdatePending } from "./windows-update";
9495

@@ -1226,8 +1227,13 @@ if (!cliCommand) {
12261227
// this only fires when the user really closed the last one.
12271228
app.on("browser-window-created", (_, win) => {
12281229
win.once("closed", () => {
1229-
if (isOnlyLingeringOverlay(BrowserWindow.getAllWindows(), countdownOverlayWindow)) {
1230+
const preview = getCameraPreviewWindow();
1231+
if (
1232+
isOnlyLingeringOverlay(BrowserWindow.getAllWindows(), [countdownOverlayWindow, preview])
1233+
) {
12301234
countdownOverlayWindow?.close();
1235+
// destroy(), not close(): the preview turns a close into a hide.
1236+
preview?.destroy();
12311237
}
12321238
});
12331239
});

‎electron/windows.ts‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ import {
1818
hudContentScreenRect,
1919
hudDragDestination,
2020
hudResizeBounds,
21+
NOTES_WINDOW_MIN_HEIGHT,
22+
notesWindowHeight,
2123
parseHudContentRect,
2224
sameRect,
2325
} from "./hudWindowBounds";
@@ -969,9 +971,9 @@ export function parkCameraPreviewWindow(win: BrowserWindow): void {
969971
export function createNotesWindow(): BrowserWindow {
970972
const win = new BrowserWindow({
971973
width: 400,
972-
height: 540,
974+
height: notesWindowHeight(usesWaylandBackend(process.platform, app.commandLine, process.env)),
973975
minWidth: 360,
974-
minHeight: 400,
976+
minHeight: NOTES_WINDOW_MIN_HEIGHT,
975977
maxWidth: 640,
976978
maxHeight: 720,
977979
title: "OpenScreen - Notes",

0 commit comments

Comments
 (0)