Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 8 additions & 9 deletions electron/hudWindowBounds.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { describe, expect, it } from "vitest";
import { HUD_TOOLTIP_STACK_HEIGHT } from "../src/components/launch/hudGeometry";
import {
clampHudBoundsToWorkArea,
clampHudContentToWorkArea,
Expand All @@ -9,7 +10,6 @@ import {
NOTES_WINDOW_MIN_HEIGHT,
notesWindowHeight,
parseHudContentRect,
WAYLAND_CASCADE_STEP,
WAYLAND_HUD_BAR_TOP,
} from "./hudWindowBounds";

Expand Down Expand Up @@ -313,14 +313,13 @@ describe("Notes opening height (GNOME Wayland)", () => {
expect(notesWindowHeight(false)).toBe(NOTES_WINDOW_HEIGHT);
});

it("ends above the HUD bar when mutter cascades it one or two steps from the HUD", () => {
// rc.2: Notes opened 540px tall, cascaded from the HUD's corner, over the bar.
const height = notesWindowHeight(true);
for (const steps of [1, 2]) {
expect(steps * WAYLAND_CASCADE_STEP + height).toBeLessThanOrEqual(WAYLAND_HUD_BAR_TOP);
}
// What it replaces: one step down at 540px reached into the bar.
expect(WAYLAND_CASCADE_STEP + NOTES_WINDOW_HEIGHT).toBeGreaterThan(WAYLAND_HUD_BAR_TOP);
it("ends above the HUD bar and its tooltips when both start at the work area's top edge", () => {
// Notes opens at the top edge (notesPlacement.ts); the HUD's top is at that edge or below.
expect(notesWindowHeight(true) + HUD_TOOLTIP_STACK_HEIGHT).toBeLessThanOrEqual(
WAYLAND_HUD_BAR_TOP,
);
// What it replaces: at 540px Notes reached into the bar even from the same corner.
expect(NOTES_WINDOW_HEIGHT).toBeGreaterThan(WAYLAND_HUD_BAR_TOP);
});

it("never opens below its own minimum height", () => {
Expand Down
18 changes: 6 additions & 12 deletions electron/hudWindowBounds.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {
HUD_EDGE_SLACK,
HUD_MODAL_MAX_HEIGHT,
HUD_POPOVER_GAP,
HUD_TOOLTIP_STACK_HEIGHT,
} from "../src/components/launch/hudGeometry";
import {
clampRectToWorkArea,
Expand All @@ -33,12 +34,6 @@ import {
export const NOTES_WINDOW_HEIGHT = 540;
export const NOTES_WINDOW_MIN_HEIGHT = 400;

/**
* How far GNOME's mutter cascades a new window from the one whose top-left corner it lands
* on: a titlebar height, measured 50px on GNOME 46 (v2.3.0-rc.2, 1920×1080 at 100%).
*/
export const WAYLAND_CASCADE_STEP = 50;

/**
* Where the bar's top sits in the Wayland HUD window, from the window's top edge: the window
* keeps the tallest panel's room above the bar (`computeHudLinuxStack`), so this is that room
Expand All @@ -47,15 +42,14 @@ export const WAYLAND_CASCADE_STEP = 50;
export const WAYLAND_HUD_BAR_TOP = HUD_EDGE_SLACK + HUD_POPOVER_GAP + HUD_MODAL_MAX_HEIGHT;

/**
* The height Notes opens at. A Wayland client cannot place its window, and mutter opens Notes
* cascaded down and right of the HUD's top-left corner, one step per window already on that
* diagonal, so its left half lands on the bar below the HUD's empty reserve. At 540px it
* covered the controls from Microphone to Notes. Short enough to end above the bar two steps
* down, it covers only the reserve; the user can still make it taller.
* The height Notes settles at. On Wayland, Notes opens at the work area's top edge, which the
* HUD's window cannot rise above (notesPlacement.ts), so the bar is at least
* `WAYLAND_HUD_BAR_TOP` below Notes' top. Notes stops short of the room the bar's tooltips take
* above it, so it covers neither; the user can still make it taller.
*/
export function notesWindowHeight(wayland: boolean): number {
if (!wayland) return NOTES_WINDOW_HEIGHT;
const clearOfBar = WAYLAND_HUD_BAR_TOP - 2 * WAYLAND_CASCADE_STEP;
const clearOfBar = WAYLAND_HUD_BAR_TOP - HUD_TOOLTIP_STACK_HEIGHT;
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
return Math.max(NOTES_WINDOW_MIN_HEIGHT, Math.min(NOTES_WINDOW_HEIGHT, clearOfBar));
}

Expand Down
74 changes: 74 additions & 0 deletions electron/notesPlacement.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import {
PLACEMENT_FALLBACK_MS,
type PlacedWindow,
shrinkOncePlaced,
topEdgeOpeningHeight,
} from "./notesPlacement";

const SIZE = { width: 400, height: 417 };
const MAX = { width: 640, height: 720 };

function fakeWindow({ destroyed = false } = {}) {
let configure: (() => void) | undefined;
const win = {
configure: () => configure?.(),
isDestroyed: () => destroyed,
once: vi.fn((_event: "resize", listener: () => void) => {
configure = listener;
}),
setSize: vi.fn(),
setMaximumSize: vi.fn(),
};
return win satisfies PlacedWindow;
}

describe("topEdgeOpeningHeight", () => {
it("is taller than every display, so the compositor has to clamp it", () => {
expect(topEdgeOpeningHeight([1080, 1440])).toBe(1441);
expect(topEdgeOpeningHeight([])).toBe(1);
});
});

describe("shrinkOncePlaced (GNOME Wayland Notes)", () => {
beforeEach(() => vi.useFakeTimers());
afterEach(() => vi.useRealTimers());

it("waits for the compositor's first configure, then shrinks on the next turn", () => {
const win = fakeWindow();
shrinkOncePlaced(win, SIZE, MAX);
expect(win.setSize).not.toHaveBeenCalled();

win.configure();
// Not inside the configure itself: a resize sent before the compositor placed the
// window is overwritten (rc.3 probe: Notes stayed full height).
expect(win.setSize).not.toHaveBeenCalled();
vi.advanceTimersByTime(0);

expect(win.setSize).toHaveBeenCalledWith(400, 417);
expect(win.setMaximumSize).toHaveBeenCalledWith(640, 720);
});

it("shrinks anyway when no configure comes, and only once", () => {
const win = fakeWindow();
shrinkOncePlaced(win, SIZE, MAX);

vi.advanceTimersByTime(PLACEMENT_FALLBACK_MS - 1);
expect(win.setSize).not.toHaveBeenCalled();
vi.advanceTimersByTime(1);
expect(win.setSize).toHaveBeenCalledTimes(1);

win.configure();
vi.runAllTimers();
expect(win.setSize).toHaveBeenCalledTimes(1);
});

it("leaves a window closed before it was placed alone", () => {
const win = fakeWindow({ destroyed: true });
shrinkOncePlaced(win, SIZE, MAX);
win.configure();
vi.runAllTimers();
expect(win.setSize).not.toHaveBeenCalled();
expect(win.setMaximumSize).not.toHaveBeenCalled();
});
});
58 changes: 58 additions & 0 deletions electron/notesPlacement.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// Opening Notes clear of the HUD bar on GNOME Wayland.
//
// A Wayland client cannot place its windows. mutter opens a new one 50px down-right of each
// window whose top-left corner sits on its diagonal from the work area's corner, and the HUD
// sits on that diagonal from launch. How many steps Notes takes depends on the other windows on
// the desktop, so no opening height keeps it off the bar: in rc.3, at 402px, three steps put it
// 48px over the bar.
//
// What mutter does not vary is where a window as tall as the work area goes: its top edge. A
// cascade step would push it off the bottom, so mutter starts the next cascade along the top
// edge, and its first-fit search must fit the whole window too. A resize then keeps the window's
// top-left. So Notes opens that tall and shrinks once the compositor has placed it. The HUD's
// window cannot rise above that edge, and everything it draws sits at least
// WAYLAND_HUD_BAR_TOP below its own top, so a Notes window shorter than that ends above the bar
// wherever the HUD is. Measured on GNOME 46 (v2.3.0-rc.3): Notes at x 50–450, y 32–433 with the
// bar at y 535, where it had opened at y 183–583.

/** The slice of `BrowserWindow` this needs, so the rule can be tested without Electron. */
export type PlacedWindow = {
isDestroyed(): boolean;
once(event: "resize", listener: () => void): unknown;
setSize(width: number, height: number): void;
setMaximumSize(width: number, height: number): void;
};

type Size = { width: number; height: number };

/**
* How long to wait for the compositor's first configure before shrinking anyway, for a
* compositor that does not clamp the window to its work area and so never sends one.
*/
export const PLACEMENT_FALLBACK_MS = 1000;

/**
* Taller than any display, so the compositor has to clamp the window to its work area: that
* clamp is the configure `shrinkOncePlaced` waits for.
*/
export function topEdgeOpeningHeight(displayHeights: readonly number[]): number {
return Math.max(0, ...displayHeights) + 1;
}

/**
* For a window just shown at `topEdgeOpeningHeight`: brings it to `size`, and its maximum back
* to `max`, once the compositor has placed it. Its first configure, the `resize` that clamps it
* to the work area, tells when. A resize sent before it is overwritten (measured: 16ms after
* `show()`, Notes stayed full height), so the shrink waits for it and goes out on the next turn.
*/
export function shrinkOncePlaced(win: PlacedWindow, size: Size, max: Size): void {
let done = false;
const shrink = () => {
if (done || win.isDestroyed()) return;
done = true;
win.setSize(size.width, size.height);
win.setMaximumSize(max.width, max.height);
};
win.once("resize", () => setTimeout(shrink, 0));
setTimeout(shrink, PLACEMENT_FALLBACK_MS);
}
16 changes: 12 additions & 4 deletions electron/windows.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import {
import { keepAlwaysOnTop, reassertAlwaysOnTop } from "./keepOnTop";
import { followAcrossSpaces } from "./macSpaces";
import { markSheetless } from "./messageBox";
import { shrinkOncePlaced, topEdgeOpeningHeight } from "./notesPlacement";
import { usesWaylandBackend } from "./wayland";

const __dirname = path.dirname(fileURLToPath(import.meta.url));
Expand Down Expand Up @@ -969,13 +970,19 @@ export function parkCameraPreviewWindow(win: BrowserWindow): void {

// Frameless Notes Window for taking notes during a recording.
export function createNotesWindow(): BrowserWindow {
const wayland = usesWaylandBackend(process.platform, app.commandLine, process.env);
const size = { width: 400, height: notesWindowHeight(wayland) };
const max = { width: 640, height: 720 };
const win = new BrowserWindow({
width: 400,
height: notesWindowHeight(usesWaylandBackend(process.platform, app.commandLine, process.env)),
width: size.width,
// On Wayland it opens taller than the screen, so mutter can only put it at the top edge,
// clear of the HUD bar, and shrinks once placed (notesPlacement.ts).
height: wayland
? topEdgeOpeningHeight(screen.getAllDisplays().map((d) => d.bounds.height))
: size.height,
minWidth: 360,
minHeight: NOTES_WINDOW_MIN_HEIGHT,
maxWidth: 640,
maxHeight: 720,
...(wayland ? {} : { maxWidth: max.width, maxHeight: max.height }),
title: "OpenScreen - Notes",
backgroundColor: "#09090b",
resizable: true,
Expand All @@ -1001,6 +1008,7 @@ export function createNotesWindow(): BrowserWindow {
win.once("ready-to-show", () => {
applyContentProtection(win, "Notes");
win.show();
if (wayland) shrinkOncePlaced(win, size, max);
});

if (VITE_DEV_SERVER_URL) {
Expand Down
Loading