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(windows): keep the recording bar always on top after a popover or dialog closes

EtienneLescot923a2c42d14d
diff --git a/electron/keepOnTop.test.ts b/electron/keepOnTop.test.ts
new file mode 100644
index 000000000..05b35a771
--- /dev/null
+++ b/electron/keepOnTop.test.ts
@@ -0,0 +1,65 @@
+import { describe, expect, it, vi } from "vitest";
+import { keepAlwaysOnTop, reassertAlwaysOnTop } from "./keepOnTop";
+
+/** A window whose topmost bit Windows can take away, as `isAlwaysOnTop()` would then report. */
+function fakeWindow({ topmost = true, destroyed = false } = {}) {
+ const listeners = new Map<string, () => void>();
+ const win = {
+ topmost,
+ listeners,
+ isDestroyed: () => destroyed,
+ isAlwaysOnTop: () => win.topmost,
+ setAlwaysOnTop: vi.fn((flag: boolean) => {
+ win.topmost = flag;
+ }),
+ on: vi.fn((event: string, listener: () => void) => listeners.set(event, listener)),
+ };
+ return win;
+}
+
+describe("keepAlwaysOnTop on Windows (#1193)", () => {
+ it("uses a level Electron does not tuck under the taskbar", () => {
+ const win = fakeWindow();
+ keepAlwaysOnTop(win, "win32");
+ expect(win.setAlwaysOnTop).toHaveBeenCalledWith(true, "pop-up-menu");
+ });
+
+ it.each([
+ "focus",
+ "blur",
+ "show",
+ "restore",
+ ])("puts the topmost bit back on %s once Windows took it", (event) => {
+ const win = fakeWindow();
+ keepAlwaysOnTop(win, "win32");
+ win.setAlwaysOnTop.mockClear();
+
+ win.topmost = false;
+ win.listeners.get(event)?.();
+
+ expect(win.setAlwaysOnTop).toHaveBeenCalledWith(true, "pop-up-menu");
+ expect(win.topmost).toBe(true);
+ });
+});
+
+describe("reassertAlwaysOnTop", () => {
+ it("leaves a window that is still topmost alone, so it is never re-ordered", () => {
+ const win = fakeWindow();
+ reassertAlwaysOnTop(win, "win32");
+ expect(win.setAlwaysOnTop).not.toHaveBeenCalled();
+ });
+
+ it("skips a destroyed window", () => {
+ const win = fakeWindow({ topmost: false, destroyed: true });
+ reassertAlwaysOnTop(win, "win32");
+ expect(win.setAlwaysOnTop).not.toHaveBeenCalled();
+ });
+
+ it.each(["darwin", "linux"] as const)("does nothing on %s", (platform) => {
+ const win = fakeWindow({ topmost: false });
+ keepAlwaysOnTop(win, platform);
+ reassertAlwaysOnTop(win, platform);
+ expect(win.setAlwaysOnTop).not.toHaveBeenCalled();
+ expect(win.on).not.toHaveBeenCalled();
+ });
+});
diff --git a/electron/keepOnTop.ts b/electron/keepOnTop.ts
new file mode 100644
index 000000000..e77a4bdb2
--- /dev/null
+++ b/electron/keepOnTop.ts
@@ -0,0 +1,52 @@
+// Keeping an always-on-top window always on top on Windows.
+//
+// `alwaysOnTop: true` sets WS_EX_TOPMOST once, at creation, and nothing in Electron ever
+// checks it again. Windows takes it away behind the app's back:
+//
+// - Electron's default level, "floating", re-inserts the window right under the taskbar
+// (SetWindowPos with Shell_TrayWnd as insert-after) on every activation, so the window
+// inherits whatever band the taskbar is in at that instant.
+// - Windows moves owners and owned windows between the topmost and normal bands together,
+// and every Chromium popup of a window (tooltip, menu, bubble) is owned by it: one of
+// them made non-topmost takes the owner down with it (measured with bare Win32 windows).
+//
+// Measured on the HUD (#1193): WS_EX_TOPMOST gone after its language notice closed and
+// after the source picker closed, the bar sunk behind Notepad, and still not topmost once
+// Ctrl+Shift+O had brought it back.
+//
+// So the window gets a level Electron does not tuck under the taskbar ("pop-up-menu" is
+// plain WS_EX_TOPMOST on Windows; Chromium collapses every level to that), and the bit is
+// put back whenever the window changes state. `isAlwaysOnTop()` reads the live style, so
+// the check is free and a window that kept its bit is never re-ordered.
+
+const LEVEL = "pop-up-menu";
+
+/** The slice of `BrowserWindow` this needs, so the rule can be tested without Electron. */
+export type TopmostWindow = {
+ isDestroyed(): boolean;
+ isAlwaysOnTop(): boolean;
+ setAlwaysOnTop(flag: boolean, level?: typeof LEVEL): void;
+ on(event: "focus" | "blur" | "show" | "restore", listener: () => void): unknown;
+};
+
+/** Puts WS_EX_TOPMOST back if Windows took it. A no-op anywhere else. */
+export function reassertAlwaysOnTop(
+ win: TopmostWindow,
+ platform: NodeJS.Platform = process.platform,
+): void {
+ if (platform !== "win32" || win.isDestroyed() || win.isAlwaysOnTop()) return;
+ win.setAlwaysOnTop(true, LEVEL);
+}
+
+/** For a window created with `alwaysOnTop: true`. */
+export function keepAlwaysOnTop(
+ win: TopmostWindow,
+ platform: NodeJS.Platform = process.platform,
+): void {
+ if (platform !== "win32") return;
+ win.setAlwaysOnTop(true, LEVEL);
+ const reassert = () => reassertAlwaysOnTop(win, platform);
+ for (const event of ["focus", "blur", "show", "restore"] as const) {
+ win.on(event, reassert);
+ }
+}
diff --git a/electron/windows.contentProtection.test.ts b/electron/windows.contentProtection.test.ts
index 8c44d00b4..079561944 100644
--- a/electron/windows.contentProtection.test.ts
+++ b/electron/windows.contentProtection.test.ts
@@ -6,6 +6,7 @@ const electron = vi.hoisted(() => {
setContentProtection = vi.fn();
setAutoHideMenuBar = vi.fn();
setVisibleOnAllWorkspaces = vi.fn();
+ setAlwaysOnTop = vi.fn();
once = vi.fn();
on = vi.fn();
loadURL = vi.fn();
diff --git a/electron/windows.test.ts b/electron/windows.test.ts
index 916e58a72..28b73e8e4 100644
--- a/electron/windows.test.ts
+++ b/electron/windows.test.ts
@@ -24,6 +24,13 @@ const electron = vi.hoisted(() => {
pixels: Rect;
webContents = { on: vi.fn(), send: vi.fn() };
setContentProtection = vi.fn();
+ // WS_EX_TOPMOST, which Windows can take away behind Electron's back (#1193).
+ topmost = true;
+ isAlwaysOnTop = () => this.topmost;
+ setAlwaysOnTop = vi.fn((flag: boolean) => {
+ this.topmost = flag;
+ });
+ setIgnoreMouseEvents = vi.fn();
once = vi.fn();
on = vi.fn();
loadURL = vi.fn();
@@ -145,3 +152,19 @@ describe("HUD drag at 125 % scaling (#1004)", () => {
expect(Math.abs(win.pixels.height - height)).toBeLessThanOrEqual(1);
});
});
+
+describe("HUD topmost on Windows (#1193)", () => {
+ it.each([
+ [
+ "a popover closes",
+ () => send("hud-overlay-content", { x: 192, y: 621, width: 560, height: 64 }),
+ ],
+ ["the bar is resized", () => send("hud-overlay-set-size", 900, 700, null)],
+ ["the pointer crosses the bar", () => send("hud-overlay-ignore-mouse-events", true)],
+ ])("is put back once Windows took it and %s", (_name, step) => {
+ const win = openHud();
+ win.topmost = false;
+ step();
+ expect(win.topmost).toBe(true);
+ });
+});
diff --git a/electron/windows.ts b/electron/windows.ts
index b4fef84c1..a062e0110 100644
--- a/electron/windows.ts
+++ b/electron/windows.ts
@@ -19,6 +19,7 @@ import {
parseHudContentRect,
sameRect,
} from "./hudWindowBounds";
+import { keepAlwaysOnTop, reassertAlwaysOnTop } from "./keepOnTop";
import { followAcrossSpaces } from "./macSpaces";
import { markSheetless } from "./messageBox";
@@ -299,6 +300,9 @@ ipcMain.on("hud-overlay-ignore-mouse-events", (_event, ignore: boolean) => {
// No `forward`: the poll above replaces it, and leaving it on would keep the app
// depending on a hook it cannot check for a transition it no longer needs.
hudOverlayWindow.setIgnoreMouseEvents(ignore);
+ // The pointer crossing the bar is the one thing that follows every popover and dialog
+ // closing, whatever took the HUD's topmost bit with it (see keepOnTop.ts).
+ reassertAlwaysOnTop(hudOverlayWindow);
if (!ignore) {
// Input is live again; the document's own pointer events are cheaper and
@@ -416,6 +420,7 @@ ipcMain.on("hud-overlay-set-size", (_event, width: number, height: number, conte
if (!sameRect(next, bounds)) {
hudOverlayWindow.setBounds(next, false);
}
+ reassertAlwaysOnTop(hudOverlayWindow);
});
// The visible stack changed without a resize: the bar grew into its reserve, or a
@@ -425,6 +430,7 @@ ipcMain.on("hud-overlay-content", (_event, content: unknown) => {
if (!nextContent) return;
hudContentRect = nextContent;
reclampHud();
+ if (hudOverlayWindow) reassertAlwaysOnTop(hudOverlayWindow);
});
/**
@@ -494,6 +500,7 @@ export function createHudOverlayWindow(): BrowserWindow {
// Follow the user across macOS Spaces, else the HUD stays pinned to the Space
// it was first opened on.
followAcrossSpaces(win);
+ keepAlwaysOnTop(win);
// Show only once painted to avoid the black rectangle flash when a transparent
// window is shown before its first paint.
@@ -684,6 +691,7 @@ export function createSourceSelectorWindow(): BrowserWindow {
// Follow the user across macOS Spaces so the selector appears on the active
// desktop regardless of where the HUD was opened.
followAcrossSpaces(win);
+ keepAlwaysOnTop(win);
if (VITE_DEV_SERVER_URL) {
win.loadURL(VITE_DEV_SERVER_URL + "?windowType=source-selector");
@@ -736,6 +744,7 @@ export function createCountdownOverlayWindow(): BrowserWindow {
win.setIgnoreMouseEvents(true);
followAcrossSpaces(win);
+ keepAlwaysOnTop(win);
if (VITE_DEV_SERVER_URL) {
win.loadURL(VITE_DEV_SERVER_URL + "?windowType=countdown-overlay");
@@ -818,6 +827,7 @@ export function createNotesWindow(): BrowserWindow {
if (process.platform !== "darwin") {
win.setAutoHideMenuBar(true);
}
+ keepAlwaysOnTop(win);
applyContentProtection(win, "Notes");
win.once("ready-to-show", () => {