Skip to content

Record your screen, ship a demo. Free and open-source, GPU-accelerated, no watermarks, no subscriptions. Windows, macOS, Linux. Actively maintained.

fix(hud): keep a dragged HUD inside the display work area

im10furryfe5d9f7bee5e
diff --git a/electron/hudWindowBounds.test.ts b/electron/hudWindowBounds.test.ts
new file mode 100644
index 000000000..c79274312
--- /dev/null
+++ b/electron/hudWindowBounds.test.ts
@@ -0,0 +1,89 @@
+import { describe, expect, it } from "vitest";
+import { clampHudToWorkArea, HUD_WINDOW_MIN, hudDragDestination } from "./hudWindowBounds";
+
+// 1920x1080 with a 48px taskbar at the bottom: the work area stops above it, so a window
+// whose bottom edge reaches 1032 is sitting on the taskbar.
+const WORK_AREA = { x: 0, y: 0, width: 1920, height: 1032 };
+// A HUD with room to move: 560x100, well clear of the bottom of the work area.
+const HUD = { x: 680, y: 800, width: 560, height: 100 };
+
+/** The drag path as the IPC handler runs it: destination first, then the work-area clamp. */
+function dragTo(deltaX: number, deltaY: number, { bounds = HUD, workArea = WORK_AREA } = {}) {
+ return clampHudToWorkArea(
+ hudDragDestination({ bounds, origin: bounds, deltaX, deltaY }),
+ workArea,
+ );
+}
+
+describe("hud drag bounds", () => {
+ it("follows the pointer while the window stays on screen", () => {
+ expect(dragTo(40, -120)).toEqual({ x: 720, y: 680, width: 560, height: 100 });
+ });
+
+ it("stops the bar above the taskbar when the drag aims past the bottom of the screen", () => {
+ const asked = hudDragDestination({ bounds: HUD, origin: HUD, deltaX: 0, deltaY: 4000 });
+ // Without the clamp this is where the window would land: 3000px past the taskbar.
+ expect(asked.y).toBe(4800);
+
+ const clamped = dragTo(0, 4000);
+ expect(clamped.y + clamped.height).toBe(WORK_AREA.height);
+ expect(clamped.x).toBe(HUD.x);
+ });
+
+ it("keeps the bar off the left, top and right edges", () => {
+ expect(dragTo(-4000, 0)).toEqual({ ...HUD, x: 0 });
+ expect(dragTo(0, -4000)).toEqual({ ...HUD, y: 0 });
+ expect(dragTo(4000, 0)).toEqual({ ...HUD, x: WORK_AREA.width - HUD.width });
+ });
+
+ it("uses the work area of the display the drag lands on, not the one it started on", () => {
+ // Second display to the left of the primary one, work area 1280x984 starting at
+ // x = -1280. Dragging left across the boundary must be allowed to follow.
+ const left = { x: -1280, y: 0, width: 1280, height: 984 };
+ const destination = hudDragDestination({
+ bounds: HUD,
+ origin: HUD,
+ deltaX: -1500,
+ deltaY: 0,
+ });
+ expect(destination.x).toBe(-820);
+
+ const clamped = clampHudToWorkArea(destination, left);
+ expect(clamped).toEqual({ ...HUD, x: -820 });
+ expect(clamped.x).toBeLessThan(0);
+ });
+
+ it("shrinks a HUD that cannot fit the work area at all", () => {
+ const clamped = clampHudToWorkArea(
+ { x: 900, y: 900, width: 2600, height: 1400 },
+ { x: 0, y: 0, width: 1024, height: 768 },
+ );
+ expect(clamped).toEqual({ x: 0, y: 0, width: 1024, height: 768 });
+ });
+
+ it("never hands a fractional or negative-zero coordinate to the window", () => {
+ // A fractional, slightly negative delta is routine under fractional display
+ // scaling, and Math.round turns it into -0 — which the native setter rejects
+ // (see the comment in hudDragDestination).
+ const tiny = hudDragDestination({ bounds: HUD, origin: HUD, deltaX: -0.2, deltaY: -0.4 });
+ expect(Number.isInteger(tiny.x)).toBe(true);
+ expect(Number.isInteger(tiny.y)).toBe(true);
+
+ const clamped = clampHudToWorkArea(tiny, { ...WORK_AREA, x: 0 });
+ expect(Object.is(clamped.x, -0)).toBe(false);
+ expect(Object.is(clamped.y, -0)).toBe(false);
+ });
+
+ it("is idempotent, so a drag that re-clamps every frame cannot creep", () => {
+ const once = dragTo(0, 4000);
+ const twice = clampHudToWorkArea(once, WORK_AREA);
+ expect(twice).toEqual(once);
+ });
+});
+
+describe("hud window minimum", () => {
+ it("matches the window's own floor, not the editor's", () => {
+ // browserWindow minWidth/minHeight at construction (electron/windows.ts).
+ expect(HUD_WINDOW_MIN).toEqual({ width: 120, height: 80 });
+ });
+});
diff --git a/electron/hudWindowBounds.ts b/electron/hudWindowBounds.ts
new file mode 100644
index 000000000..47d773bb7
--- /dev/null
+++ b/electron/hudWindowBounds.ts
@@ -0,0 +1,53 @@
+// Where the HUD overlay window is allowed to sit.
+//
+// The HUD is transparent, always-on-top and `skipTaskbar`, and two different inputs move
+// it: the drag handle repositions it frame by frame, and a content change re-anchors it
+// around its bottom edge. The Windows taskbar and the macOS Dock live *outside* the
+// display work area, so a HUD that lands there is painted over them with no taskbar
+// entry to bring it back — the tray icon is the only way home.
+//
+// Pure geometry on purpose: `screen` and the window are the caller's job, so the rules
+// can be tested without an Electron runtime.
+
+import {
+ clampRectToWorkArea,
+ type DisplayWorkArea,
+ type EditorWindowRect,
+} from "./editorWindowState";
+
+/** The HUD's own floor, matching `minWidth`/`minHeight` at construction. */
+export const HUD_WINDOW_MIN = { width: 120, height: 80 };
+
+/** Puts HUD bounds inside the work area of the display they belong to. */
+export function clampHudToWorkArea(
+ bounds: EditorWindowRect,
+ workArea: DisplayWorkArea,
+): EditorWindowRect {
+ return clampRectToWorkArea(bounds, workArea, HUD_WINDOW_MIN);
+}
+
+/**
+ * Where the drag handle is asking for the window to go, before the work-area clamp.
+ *
+ * The caller resolves the display from *this* rect rather than from the window's current
+ * one, so dragging towards a second display follows the pointer instead of stopping at
+ * the edge of the display the drag started on.
+ */
+export function hudDragDestination(input: {
+ bounds: EditorWindowRect;
+ origin: { x: number; y: number };
+ deltaX: number;
+ deltaY: number;
+}): EditorWindowRect {
+ // `| 0` is load-bearing, not defensive noise. Math.round returns NEGATIVE ZERO for
+ // any delta in [-0.5, 0) — routine under fractional scaling, where screenY deltas
+ // are fractional. V8's IsInt32() rejects -0, so gin refuses to convert it and the
+ // main process dies with "Error processing argument at index 1, conversion failure
+ // from". `Number.isFinite(-0)` is true, so a finiteness check does NOT catch this;
+ // `| 0` collapses -0 to 0 and pins the value to int32. (`+ 0` would not: -0 + 0 is
+ // still -0, and Math.trunc preserves it too.)
+ const x = Math.round(input.origin.x + input.deltaX) | 0;
+ const y = Math.round(input.origin.y + input.deltaY) | 0;
+
+ return { ...input.bounds, x, y };
+}
diff --git a/electron/windows.ts b/electron/windows.ts
index b973af8f4..7deaa458b 100644
--- a/electron/windows.ts
+++ b/electron/windows.ts
@@ -8,6 +8,7 @@ import {
saveEditorWindowState,
shouldTrackEditorWindow,
} from "./editorWindowState";
+import { clampHudToWorkArea, hudDragDestination } from "./hudWindowBounds";
const __dirname = path.dirname(fileURLToPath(import.meta.url));
@@ -102,6 +103,41 @@ let hudOverlayWindow: BrowserWindow | null = null;
// self-corrects on the next one instead of leaving the window permanently offset.
let hudDragOrigin: { x: number; y: number } | null = null;
+// The work area can shrink under a HUD nobody is touching: the taskbar stops auto-hiding
+// (or is revealed), the Dock moves, the resolution or the scale changes. A bar parked at
+// the bottom edge would then be left sitting on the taskbar until the next drag — and the
+// drag handle is under the taskbar. Re-clamp whenever the displays change, so the
+// invariant the drag and resize paths both maintain holds without user input too.
+let isWatchingHudWorkArea = false;
+function watchHudWorkAreaChanges() {
+ if (isWatchingHudWorkArea) return;
+ isWatchingHudWorkArea = true;
+
+ const reclampHud = () => {
+ const win = hudOverlayWindow;
+ if (!win || win.isDestroyed()) return;
+ // A drag owns the position frame by frame; moving the window out from under it
+ // would fight the gesture. The renderer re-measures on release, and the next
+ // metric change re-clamps it anyway.
+ if (hudDragOrigin) return;
+
+ const bounds = win.getBounds();
+ const next = clampHudToWorkArea(bounds, screen.getDisplayMatching(bounds).workArea);
+ if (
+ next.x !== bounds.x ||
+ next.y !== bounds.y ||
+ next.width !== bounds.width ||
+ next.height !== bounds.height
+ ) {
+ win.setBounds(next, false);
+ }
+ };
+
+ screen.on("display-metrics-changed", reclampHud);
+ screen.on("display-added", reclampHud);
+ screen.on("display-removed", reclampHud);
+}
+
ipcMain.on("hud-overlay-hide", () => {
if (hudOverlayWindow && !hudOverlayWindow.isDestroyed()) {
hudOverlayWindow.minimize();
@@ -206,17 +242,20 @@ ipcMain.on("hud-overlay-drag-to", (_event, deltaX: number, deltaY: number) => {
return;
}
- // `| 0` is load-bearing, not defensive noise. Math.round returns NEGATIVE ZERO for
- // any delta in [-0.5, 0) — routine under fractional scaling, where screenY deltas
- // are fractional. V8's IsInt32() rejects -0, so gin refuses to convert it and the
- // main process dies with "Error processing argument at index 1, conversion failure
- // from". `Number.isFinite(-0)` is true, so a finiteness check does NOT catch this;
- // `| 0` collapses -0 to 0 and pins the value to int32. (`+ 0` would not: -0 + 0 is
- // still -0, and Math.trunc preserves it too.)
- const x = Math.round(hudDragOrigin.x + deltaX) | 0;
- const y = Math.round(hudDragOrigin.y + deltaY) | 0;
-
- hudOverlayWindow.setPosition(x, y, false);
+ // Clamp to the work area of the display the drag is heading *for* — resolved from the
+ // destination, not from where the window currently sits, so a drag towards a second
+ // display follows the pointer instead of stopping at the edge it started on.
+ //
+ // Without this the drag handle was a one-way trip off the screen: the taskbar and the
+ // Dock live outside the work area, and an always-on-top, `skipTaskbar` HUD parked
+ // there covers them, with no taskbar entry to click to get it back. The tray icon was
+ // the only way home. "hud-overlay-set-size" already clamped for exactly this reason;
+ // this path did not.
+ const bounds = hudOverlayWindow.getBounds();
+ const destination = hudDragDestination({ bounds, origin: hudDragOrigin, deltaX, deltaY });
+ const { workArea } = screen.getDisplayMatching(destination);
+
+ hudOverlayWindow.setBounds(clampHudToWorkArea(destination, workArea), false);
});
ipcMain.on("hud-overlay-drag-end", () => {
@@ -365,6 +404,7 @@ export function createHudOverlayWindow(): BrowserWindow {
});
hudOverlayWindow = win;
+ watchHudWorkAreaChanges();
win.on("closed", () => {
if (hudOverlayWindow === win) {