Voice-to-text dictation app with local (Nvidia Parakeet/Whisper) and cloud models (BYOK). Privacy-first and available cross-platform.
JavaScript aianthropiccross-platformgeminigroqlinuxmacosnvidiaopen-sourceopenaiparakeetspeech-to-texttranscribewhisperwindows
fix(linux): keep the dictation pill hoverable after the first hover, and visible after Retry (#2161)
void685fceabdf90
diff --git a/preload.js b/preload.jsindex 86640d5e9..1ce36bc1f 100644--- a/preload.js+++ b/preload.js@@ -543,6 +543,11 @@ contextBridge.exposeInMainWorld("electronAPI", {ackMainWindowResizeMask: (token) => ipcRenderer.send("main-window-resize-mask-ready", token),setMainWindowInteractivity: (interactive) =>ipcRenderer.invoke("set-main-window-interactivity", interactive),+ setMainWindowInputRegion: (region) => ipcRenderer.invoke("set-main-window-input-region", region),+ onMainWindowVisibilityChanged: registerListener(+ "main-window-visibility-changed",+ (callback) => (_event, visible) => callback(visible)+ ),setNotificationInteractivity: (interactive) =>ipcRenderer.invoke("set-notification-interactivity", interactive),resizeMainWindow: (sizeKey) => ipcRenderer.invoke("resize-main-window", sizeKey),diff --git a/resources/linux-fast-paste.c b/resources/linux-fast-paste.cindex 7590768fb..8279899ed 100644--- a/resources/linux-fast-paste.c+++ b/resources/linux-fast-paste.c@@ -7,7 +7,9 @@#include <X11/Xatom.h>#include <X11/Xutil.h>#include <X11/extensions/XTest.h>+#include <X11/extensions/shape.h>#include <X11/keysym.h>+#include <math.h>#include <unistd.h>#ifdef HAVE_UINPUT@@ -784,6 +786,63 @@ static paste_mode_t resolve_paste_mode(int force_terminal, int force_shift_inserreturn is_term ? PASTE_MODE_CTRL_SHIFT_V : PASTE_MODE_CTRL_V;}+/* Shape only input, leaving transparent shadows/tooltips free to render.+ * Keeping the visible pill in the compositor's input region also works over+ * native Wayland apps, where XWayland cannot query a fresh global cursor. */+static int serve_input_region(Window window)+{+ if (window == None) return 1;+ Display *display = XOpenDisplay(NULL);+ if (!display) return 1;+ int event_base, error_base, major, minor;+ if (!XShapeQueryExtension(display, &event_base, &error_base) ||+ !XShapeQueryVersion(display, &major, &minor) ||+ (major == 1 && minor < 1)) {+ XCloseDisplay(display);+ return 1;+ }++ char request[512];+ while (fgets(request, sizeof(request), stdin)) {+ if (strcmp(request, "full\n") == 0) {+ XShapeCombineMask(display, window, ShapeInput, 0, 0, None, ShapeSet);+ } else {+ double viewport_width, viewport_height, x, y, width, height;+ if (sscanf(request, "%lf %lf %lf %lf %lf %lf", &viewport_width,+ &viewport_height, &x, &y, &width, &height) != 6 ||+ !isfinite(viewport_width) || !isfinite(viewport_height) ||+ !isfinite(x) || !isfinite(y) || !isfinite(width) || !isfinite(height) ||+ viewport_width <= 0 || viewport_height <= 0 || width < 0 || height < 0) {+ XCloseDisplay(display);+ return 1;+ }+ Window root;+ int window_x, window_y;+ unsigned int native_width, native_height, border, depth;+ if (!XGetGeometry(display, window, &root, &window_x, &window_y,+ &native_width, &native_height, &border, &depth)) {+ XCloseDisplay(display);+ return 1;+ }+ int left = (int)floor(fmax(0, fmin(native_width, x * native_width / viewport_width)));+ int top = (int)floor(fmax(0, fmin(native_height, y * native_height / viewport_height)));+ int right = (int)ceil(fmax(0, fmin(native_width, (x + width) * native_width / viewport_width)));+ int bottom = (int)ceil(fmax(0, fmin(native_height, (y + height) * native_height / viewport_height)));+ XRectangle rectangle = { left, top, right - left, bottom - top };+ int count = width > 0 && height > 0 && right > left && bottom > top ? 1 : 0;+ XShapeCombineRectangles(display, window, ShapeInput, 0, 0,+ &rectangle, count, ShapeSet, Unsorted);+ }+ /* Acknowledging after the server applies the shape orders panel capture+ * after any older pill-only requests from a replaced React effect. */+ XSync(display, False);+ printf("OK\n");+ fflush(stdout);+ }+ XCloseDisplay(display);+ return 0;+}+int main(int argc, char *argv[]) {int force_terminal = 0;int force_shift_insert = 0;@@ -792,6 +851,7 @@ int main(int argc, char *argv[]) {int media_play_pause = 0;int copy_mode = 0;int capabilities_only = 0;+ int input_region_server = 0;int atspi_target_only = 0;int atspi_selection = 0;const char *restore_token = NULL;@@ -812,6 +872,8 @@ int main(int argc, char *argv[]) {copy_mode = 1;} else if (strcmp(argv[i], "--capabilities") == 0) {capabilities_only = 1;+ } else if (strcmp(argv[i], "--input-region-server") == 0) {+ input_region_server = 1;} else if (strcmp(argv[i], "--atspi-target") == 0) {atspi_target_only = 1;} else if (strcmp(argv[i], "--atspi-selection") == 0) {@@ -823,6 +885,10 @@ int main(int argc, char *argv[]) {}}+ /* --capabilities makes older binaries exit harmlessly instead of pasting+ * when they encounter the new server flag. */+ if (input_region_server) return serve_input_region(target_window);+if (capabilities_only) {printf("paste-v1 selection-copy-v1 target-window-v1");#ifdef HAVE_GIOdiff --git a/scripts/build-linux-fast-paste.js b/scripts/build-linux-fast-paste.jsindex 9d960cf0c..a0e055d62 100755--- a/scripts/build-linux-fast-paste.js+++ b/scripts/build-linux-fast-paste.js@@ -164,7 +164,7 @@ function attemptCompile(command, args) {});}-const compileArgs = ["-O2", cSource, "-o", outputBinary, "-lX11", "-lXtst"];+const compileArgs = ["-O2", cSource, "-o", outputBinary, "-lX11", "-lXtst", "-lXext", "-lm"];if (uinputAvailable) {log("uinput headers found, enabling uinput support");diff --git a/src/App.jsx b/src/App.jsxindex 54ff9847b..82d0763fa 100644--- a/src/App.jsx+++ b/src/App.jsx@@ -5,6 +5,7 @@ import { useToast } from "./components/ui/useToast";import { useHotkey } from "./hooks/useHotkey";import { formatHotkeyListLabel } from "./utils/hotkeys";import { useWindowDrag } from "./hooks/useWindowDrag";+import { useLinuxPillInteractivity } from "./hooks/useLinuxPillInteractivity";import { useAudioRecording } from "./hooks/useAudioRecording";import { useAssistantPanel } from "./hooks/useAssistantPanel";import { useOnboardingAssistantDemo } from "./hooks/useOnboardingAssistantDemo";@@ -62,6 +63,7 @@ export default function App() {const [isHovered, setIsHovered] = useState(false);const [isCommandMenuOpen, setIsCommandMenuOpen] = useState(false);const buttonRef = useRef(null);+ const pillPresenceRef = useRef(null);const { toast, dismiss, toastCount, dictationErrorActionCount, dismissByPresentation } =useToast();const { t } = useTranslation();@@ -81,6 +83,9 @@ export default function App() {const [mainWindowHorizontalDirection, setMainWindowHorizontalDirection] = useState(null);const setWindowInteractivity = React.useCallback((shouldCapture) => {+ // Linux has one pointer-poll owner; native mouseleave must not undo its+ // drag/menu capture or strand the next hover in click-through mode.+ if (window.electronAPI?.getPlatform?.() === "linux") return;window.electronAPI?.setMainWindowInteractivity?.(shouldCapture);}, []);const dismissDictationError = React.useCallback(@@ -322,6 +327,7 @@ export default function App() {toastCount,isCommandMenuOpen,isCompactPill: windowFitsCompactPill,+ isDictationActive: isRecording || isVisuallyProcessing,assistantOpen: assistant.open,assistantMounted: assistant.mounted,assistantOpenRef,@@ -621,6 +627,12 @@ export default function App() {hasLiveActivity: pillHasLiveActivity,});+ useLinuxPillInteractivity({+ pillRef: pillPresenceRef,+ captureWindow: isCommandMenuOpen || toastCount > 0 || anyPanelMounted || isDragging,+ pillInteractive: pillIsInteractive && !pillVisuallySuppressed,+ });+return (<div className="dictation-window">{/* The panel footer can hide this pill, but never unmounts it. */}@@ -636,6 +648,7 @@ export default function App() {aria-hidden={pillVisuallySuppressed || undefined}><div+ ref={pillPresenceRef}className="assistant-pill-presence relative flex items-center"data-assistant-footer-phase={assistant.open ? assistant.footerPhase : undefined}data-horizontal-direction={voiceHorizontalDirection}diff --git a/src/helpers/ipcHandlers.js b/src/helpers/ipcHandlers.jsindex cdeaf31fb..833672d48 100644--- a/src/helpers/ipcHandlers.js+++ b/src/helpers/ipcHandlers.js@@ -1537,6 +1537,11 @@ class IPCHandlers {return { success: true };});+ ipcMain.handle("set-main-window-input-region", (event, region) => {+ if (event.sender !== this.windowManager.mainWindow?.webContents) return null;+ return this.windowManager.setMainWindowInputRegion(region);+ });+ipcMain.handle("get-main-window-horizontal-direction", () => {return this.windowManager.getMainWindowHorizontalDirection();});diff --git a/src/helpers/linuxWindowInputRegion.js b/src/helpers/linuxWindowInputRegion.jsnew file mode 100644index 000000000..2ef2aa806--- /dev/null+++ b/src/helpers/linuxWindowInputRegion.js@@ -0,0 +1,129 @@+const { spawn } = require("child_process");+const { app } = require("electron");+const { resolveBundledBinary } = require("./binaryResolver");+const debugLogger = require("./debugLogger");++const APPLY_TIMEOUT_MS = 1000;++function createLinuxWindowInputRegion(win) {+ let child = null;+ let failure = null;+ let buffer = "";+ const pending = [];+ let finishStopping;+ const stopped = new Promise((resolve) => {+ finishStopping = resolve;+ });++ const finish = () => {+ for (const request of pending.splice(0)) {+ clearTimeout(request.timer);+ request.reject(failure);+ }+ app.removeListener("before-quit", onClose);+ win.removeListener("closed", onClose);+ finishStopping();+ };+ const stop = (error = new Error("Input region writer stopped")) => {+ if (failure) return;+ failure = error;+ if (child) child.kill("SIGKILL");+ else finish();+ };+ const onClose = () => stop();+ const fail = (error) => {+ if (failure) return;+ debugLogger.warn("Linux input region unavailable", { error: error.message });+ stop(error);+ };++ const start = () => {+ const binary = resolveBundledBinary("linux-fast-paste", "linux-input-region");+ if (!binary) throw new Error("Linux input region helper not found");+ const windowId = win.getNativeWindowHandle().readUInt32LE();+ if (!windowId) throw new Error("X11 window handle unavailable");+ child = spawn(+ binary,+ ["--capabilities", "--input-region-server", "--window", String(windowId)],+ {+ stdio: ["pipe", "pipe", "ignore"],+ }+ );+ child.on("error", fail);+ child.on("close", () => {+ if (!failure) fail(new Error("Linux input region helper exited"));+ finish();+ });+ child.stdin.on("error", fail);+ child.stdout.on("error", fail);+ child.stdout.setEncoding("utf8");+ child.stdout.on("data", (chunk) => {+ if (failure) return;+ buffer += chunk;+ if (buffer.length > 1024) return fail(new Error("Invalid input region response"));+ let newline;+ while ((newline = buffer.indexOf("\n")) >= 0) {+ const line = buffer.slice(0, newline);+ buffer = buffer.slice(newline + 1);+ if (line !== "OK" || !pending.length)+ return fail(new Error("Invalid input region response"));+ const request = pending.shift();+ clearTimeout(request.timer);+ request.resolve();+ }+ });+ };++ const set = (region) => {+ if (failure)+ return stopped.then(() => {+ throw failure;+ });+ let line = "full\n";+ if (region !== null) {+ const values = [+ region?.viewportWidth,+ region?.viewportHeight,+ region?.x,+ region?.y,+ region?.width,+ region?.height,+ ];+ if (+ !values.every(Number.isFinite) ||+ values[0] <= 0 ||+ values[1] <= 0 ||+ values[4] < 0 ||+ values[5] < 0+ ) {+ fail(new Error("Invalid input region"));+ return stopped.then(() => {+ throw failure;+ });+ }+ line = `${values.join(" ")}\n`;+ }+ return new Promise((resolve, reject) => {+ pending.push({+ resolve,+ reject,+ timer: setTimeout(+ () => fail(new Error("Linux input region update timed out")),+ APPLY_TIMEOUT_MS+ ),+ });+ try {+ if (!child) start();+ child.stdin.write(line);+ } catch (error) {+ fail(error);+ }+ });+ };++ app.once("before-quit", onClose);+ win.once("closed", onClose);+ return { set, stop };+}++module.exports = { createLinuxWindowInputRegion };diff --git a/src/helpers/windowManager.js b/src/helpers/windowManager.jsindex 97ee04273..b7c1b2fc8 100644--- a/src/helpers/windowManager.js+++ b/src/helpers/windowManager.js@@ -1,5 +1,6 @@const { app, screen, BrowserWindow, dialog, ipcMain, Menu } = require("electron");const debugLogger = require("./debugLogger");+const { createLinuxWindowInputRegion } = require("./linuxWindowInputRegion");// Aliased: this class has an openExternalUrl method wrapping the helper.const { openExternalUrl: openUrlInExternalBrowser } = require("./externalUrlOpener");const HotkeyManager = require("./hotkeyManager");@@ -236,18 +237,38 @@ class WindowManager {if (process.platform === "win32") {// Windows click-through forwarding is unreliable for this floating panel.- // Keep the panel interactive so the mic button and cancel button are always clickable.this.mainWindow.setIgnoreMouseEvents(false);return;}- if (shouldCapture) {+ if (process.platform === "linux") {+ // Native capture is the fallback when the input-region helper is unavailable.+ this.mainWindow.setIgnoreMouseEvents(!shouldCapture);+ } else if (shouldCapture) {this.mainWindow.setIgnoreMouseEvents(false);} else {this.mainWindow.setIgnoreMouseEvents(true, { forward: true });}}+ async setMainWindowInputRegion(region) {+ const win = this.mainWindow;+ if (process.platform !== "linux" || !win || win.isDestroyed()) return false;+ if (this._linuxWindowInputRegion?.window !== win) {+ this._linuxWindowInputRegion?.stop();+ this._linuxWindowInputRegion = { window: win, ...createLinuxWindowInputRegion(win) };+ }+ try {+ await this._linuxWindowInputRegion.set(region);+ return !win.isDestroyed() && win.isVisible() && !win.isMinimized();+ } catch (error) {+ // The writer rejects after its process closes, so an old shape cannot+ // overwrite this fallback and leave native hover unreachable.+ if (!win.isDestroyed()) win.setIgnoreMouseEvents(false);+ throw error;+ }+ }+// Only the meeting prompt owns this: another overlay reporting its own hover// must not pause a countdown it cannot resume — it may be destroyed before// its pointer ever leaves.@@ -1946,6 +1967,20 @@ class WindowManager {return;}+ if (process.platform === "linux") {+ const win = this.mainWindow;+ // backgroundThrottling:false keeps document.visibilityState visible even+ // after hide(). Native visibility owns the Linux input-region updates.+ for (const event of ["show", "hide", "minimize", "restore"]) {+ win.on(event, () => {+ win.webContents.send(+ "main-window-visibility-changed",+ win.isVisible() && !win.isMinimized()+ );+ });+ }+ }+// Safety timeout: force show the window if ready-to-show doesn't fire within 10 secondsconst showTimeout = setTimeout(() => {if (diff --git a/src/hooks/useLinuxPillInteractivity.ts b/src/hooks/useLinuxPillInteractivity.tsnew file mode 100644index 000000000..4aeece3e5--- /dev/null+++ b/src/hooks/useLinuxPillInteractivity.ts@@ -0,0 +1,91 @@+import { useLayoutEffect, useRef, type RefObject } from "react";++interface LinuxPillInteractivityOptions {+ pillRef: RefObject<HTMLElement | null>;+ captureWindow: boolean;+ pillInteractive: boolean;+}++// Native input shaping leaves hover delivery to the compositor, including+// when the pointer crosses between XWayland and native Wayland applications.+export function useLinuxPillInteractivity({+ pillRef,+ captureWindow,+ pillInteractive,+}: LinuxPillInteractivityOptions): void {+ const nativeVisibleRef = useRef(true);+ useLayoutEffect(() => {+ const api = window.electronAPI;+ if (api?.getPlatform?.() !== "linux") return;+ let disposed = false;+ let generation = 0;+ let pending = false;+ let timer: ReturnType<typeof setInterval> | undefined;++ const stop = (): void => {+ generation += 1;+ clearInterval(timer);+ timer = undefined;+ };+ const applyRegion = async (+ region: Parameters<typeof api.setMainWindowInputRegion>[0]+ ): Promise<void> => {+ const requestGeneration = generation;+ try {+ const visible = await api.setMainWindowInputRegion(region);+ if (disposed || requestGeneration !== generation) return;+ if (!visible) {+ nativeVisibleRef.current = false;+ stop();+ }+ } catch {+ // The native writer restores full input when shaping is unavailable.+ // A failure from an older effect must not stop its replacement.+ if (!disposed && requestGeneration === generation) stop();+ }+ };+ const sample = async (): Promise<void> => {+ if (disposed || pending || !nativeVisibleRef.current || document.hidden) return;+ pending = true;+ try {+ const rect = pillInteractive ? pillRef.current?.getBoundingClientRect() : null;+ await applyRegion({+ x: rect?.x ?? 0,+ y: rect?.y ?? 0,+ width: rect?.width ?? 0,+ height: rect?.height ?? 0,+ viewportWidth: window.innerWidth,+ viewportHeight: window.innerHeight,+ });+ } finally {+ pending = false;+ }+ };+ const start = (): void => {+ stop();+ if (!nativeVisibleRef.current) return;+ if (captureWindow) {+ // Full input joins the same native FIFO immediately, even while an+ // older narrow-region update is still awaiting acknowledgement.+ void applyRegion(null);+ return;+ }+ // Reapply even unchanged geometry: native resizes can reset input shape.+ timer = setInterval(() => void sample(), 50);+ void sample();+ };+ const unsubscribe = api.onMainWindowVisibilityChanged((visible) => {+ nativeVisibleRef.current = visible;+ if (visible) start();+ else stop();+ });+ start();+ return () => {+ disposed = true;+ stop();+ unsubscribe();+ // The native writer orders this after any pending narrow-region update.+ void api.setMainWindowInputRegion(null).catch(() => {});+ };+ }, [captureWindow, pillInteractive, pillRef]);+}diff --git a/src/hooks/useMainWindowSizeOwner.js b/src/hooks/useMainWindowSizeOwner.jsindex 86230ee66..13b6d128a 100644--- a/src/hooks/useMainWindowSizeOwner.js+++ b/src/hooks/useMainWindowSizeOwner.js@@ -23,6 +23,7 @@ export function useMainWindowSizeOwner({toastCount,isCommandMenuOpen,isCompactPill,+ isDictationActive,assistantOpen,assistantMounted,assistantOpenRef,@@ -32,6 +33,7 @@ export function useMainWindowSizeOwner({}) {const [handoffActive, setHandoffActive] = useState(false);const actionCountRef = useRef(dictationErrorActionCount);+ const dictationActiveRef = useRef(isDictationActive);const handoffRef = useRef(null);// Same masking for the panel-return shrink: snapping the native window from// panel bounds back to the pill box paints one compositor frame of the old@@ -43,7 +45,11 @@ export function useMainWindowSizeOwner({useEffect(() => {const handoff = createPillVisibilityHandoff({onSuppressedChange: setHandoffActive,- shouldAutoHide: () => useSettingsStore.getState().floatingIconAutoHide,+ // Retry (and the hotkey) clear the error card by starting the next+ // dictation. Handing the window back to auto-hide then would leave that+ // recording running with no pill on screen (#2141).+ shouldAutoHide: () =>+ useSettingsStore.getState().floatingIconAutoHide && !dictationActiveRef.current,hideWindow: () => window.electronAPI?.hideWindow?.(),});handoffRef.current = handoff;@@ -65,6 +71,10 @@ export function useMainWindowSizeOwner({};}, []);+ useLayoutEffect(() => {+ dictationActiveRef.current = isDictationActive;+ }, [isDictationActive]);+useLayoutEffect(() => {actionCountRef.current = dictationErrorActionCount;if (dictationErrorActionCount > 0) {diff --git a/src/types/electron.ts b/src/types/electron.tsindex e5a497610..a407dbd0f 100644--- a/src/types/electron.ts+++ b/src/types/electron.ts@@ -10,6 +10,15 @@ import type { CalendarAvailabilityRequest, CalendarAvailabilityResult } from "./export type LocalTranscriptionProvider = "whisper" | "nvidia" | "cohere";+export interface MainWindowInputRegion {+ viewportWidth: number;+ viewportHeight: number;+ x: number;+ y: number;+ width: number;+ height: number;+}+export type ChineseScriptPreference = "simplified" | "traditional" | "as-transcribed";export type InferenceMode = "openwhispr" | "providers" | "local" | "self-hosted" | "enterprise";@@ -1924,6 +1933,8 @@ declare global {startControlPanelDrag: () => Promise<void>;stopControlPanelDrag: () => Promise<void>;setMainWindowInteractivity: (interactive: boolean) => Promise<void>;+ setMainWindowInputRegion: (region: MainWindowInputRegion | null) => Promise<boolean>;+ onMainWindowVisibilityChanged: (callback: (visible: boolean) => void) => () => void;setNotificationInteractivity: (interactive: boolean) => Promise<void>;resizeMainWindow: (sizeKey:diff --git a/test/helpers/agentDictationPillIpc.test.js b/test/helpers/agentDictationPillIpc.test.jsindex 3e345cbae..2de04af26 100644--- a/test/helpers/agentDictationPillIpc.test.js+++ b/test/helpers/agentDictationPillIpc.test.js@@ -170,6 +170,24 @@ test.before(() => {);});+test("only the main renderer can change its window's input region", async () => {+ const { stub, mainContents, pillContents } = makeWindowManagerStub();+ const regions = [];+ stub.setMainWindowInputRegion = async (region) => {+ regions.push(region);+ return true;+ };+ installWindowManager(stub);+ const handler = handlers.get("set-main-window-input-region");+ const region = { viewportWidth: 208, viewportHeight: 120, x: 156, y: 68, width: 40, height: 40 };+ assert.equal(await handler({ sender: mainContents }, region), true);+ assert.equal(handler({ sender: pillContents }, null), null);+ assert.equal(handler({ sender: {} }, null), null);+ stub.mainWindow = null;+ assert.equal(handler({ sender: mainContents }, null), null);+ assert.deepEqual(regions, [region]);+});+test.after(() => {Module._load = originalLoad;});diff --git a/test/helpers/linuxInputRegionNative.test.js b/test/helpers/linuxInputRegionNative.test.jsnew file mode 100644index 000000000..2d5e92068--- /dev/null+++ b/test/helpers/linuxInputRegionNative.test.js@@ -0,0 +1,84 @@+const test = require("node:test");+const assert = require("node:assert/strict");+const fs = require("node:fs");+const os = require("node:os");+const path = require("node:path");+const { spawn, spawnSync } = require("node:child_process");+const { once } = require("node:events");+const { createInterface } = require("node:readline");++test(+ "native input regions scale, clip and route clicks without changing the visible window",+ { skip: process.platform !== "linux", timeout: 30_000 },+ async (t) => {+ const temporaryDirectory = fs.mkdtempSync(path.join(os.tmpdir(), "openwhispr-input-region-"));+ let xvfb;+ t.after(async () => {+ if (xvfb && xvfb.exitCode === null && xvfb.signalCode === null) {+ const closed = once(xvfb, "close");+ xvfb.kill("SIGKILL");+ await closed;+ }+ fs.rmSync(temporaryDirectory, { recursive: true, force: true });+ });++ const xvfbCheck = spawnSync("Xvfb", ["-help"], { encoding: "utf8", timeout: 5000 });+ if (xvfbCheck.error?.code === "ENOENT") return t.skip("Xvfb is not installed");+ assert.ifError(xvfbCheck.error);+ const libraries = ["-lX11", "-lXtst", "-lXext", "-lm"];+ const prerequisites = spawnSync(+ "gcc",+ ["-x", "c", "-", "-o", path.join(temporaryDirectory, "prerequisites"), ...libraries],+ {+ input:+ "#include <X11/Xlib.h>\n#include <X11/extensions/XTest.h>\n#include <X11/extensions/shape.h>\nint main(void) { return 0; }\n",+ encoding: "utf8",+ timeout: 10_000,+ }+ );+ if (prerequisites.error?.code === "ENOENT") return t.skip("gcc is not installed");+ assert.ifError(prerequisites.error);+ if (prerequisites.status !== 0)+ return t.skip("X11, Xtst or Xext development libraries unavailable");++ const root = path.resolve(__dirname, "../..");+ const helper = path.join(temporaryDirectory, "linux-fast-paste");+ const fixture = path.join(temporaryDirectory, "input-region-fixture");+ for (const [source, output] of [+ [path.join(root, "resources/linux-fast-paste.c"), helper],+ [path.join(root, "test/native/linuxInputRegion.c"), fixture],+ ]) {+ const compiled = spawnSync("gcc", [source, "-o", output, ...libraries], {+ encoding: "utf8",+ timeout: 10_000,+ });+ assert.ifError(compiled.error);+ assert.equal(compiled.status, 0, compiled.stderr);+ }++ // Xvfb selects a free display itself; never send fixture input to the+ // developer's DISPLAY or assume that a hardcoded display number is unused.+ xvfb = spawn("Xvfb", ["-displayfd", "3", "-screen", "0", "1024x768x24", "-nolisten", "tcp"], {+ env: { ...process.env, DISPLAY: "" },+ stdio: ["ignore", "ignore", "pipe", "pipe"],+ });+ let displayErrors = "";+ xvfb.stderr.on("data", (chunk) => {+ displayErrors += chunk;+ });+ await once(xvfb, "spawn");+ const displayLines = createInterface({ input: xvfb.stdio[3] });+ t.after(() => displayLines.close());+ const [displayNumber] = await once(displayLines, "line", { signal: AbortSignal.timeout(5000) });+ assert.match(displayNumber, /^\d+$/, displayErrors);++ const result = spawnSync(fixture, [helper], {+ env: { ...process.env, DISPLAY: `:${displayNumber}` },+ encoding: "utf8",+ timeout: 10_000,+ });+ assert.ifError(result.error);+ assert.equal(result.status, 0, result.stderr || displayErrors);+ assert.match(result.stdout, /input region native checks passed/);+ }+);diff --git a/test/helpers/linuxWindowInputRegion.test.js b/test/helpers/linuxWindowInputRegion.test.jsnew file mode 100644index 000000000..7cf74d529--- /dev/null+++ b/test/helpers/linuxWindowInputRegion.test.js@@ -0,0 +1,328 @@+const test = require("node:test");+const assert = require("node:assert/strict");+const Module = require("node:module");+const { EventEmitter } = require("node:events");+const { PassThrough } = require("node:stream");++function loadInputRegion(t, { binary = "/app/linux-fast-paste", windowId = 0xfedcba98 } = {}) {+ const app = new EventEmitter();+ const win = new EventEmitter();+ win.getNativeWindowHandle = () => {+ const handle = Buffer.alloc(8);+ handle.writeUInt32LE(windowId);+ return handle;+ };+ const child = new EventEmitter();+ child.stdout = new PassThrough();+ child.stdin = new PassThrough();+ const killSignals = [];+ // Keep close under test control: requesting SIGKILL is not proof that the+ // old helper has stopped writing native input regions.+ child.kill = (signal) => {+ killSignals.push(signal);+ return true;+ };+ const writes = [];+ child.stdin.on("data", (chunk) => writes.push(chunk.toString()));+ const spawns = [];+ const warnings = [];+ const originalLoad = Module._load;+ const modulePath = require.resolve("../../src/helpers/linuxWindowInputRegion");+ const cachedModule = require.cache[modulePath];+ delete require.cache[modulePath];+ Module._load = function loadWithInputRegionDependencies(request, parent, isMain) {+ if (request === "electron") return { app };+ if (request === "./binaryResolver") return { resolveBundledBinary: () => binary };+ if (request === "./debugLogger") return { warn: (...args) => warnings.push(args) };+ if (request === "child_process") {+ return {+ spawn: (...args) => {+ spawns.push(args);+ return child;+ },+ };+ }+ return originalLoad.call(this, request, parent, isMain);+ };+ let createLinuxWindowInputRegion;+ try {+ ({ createLinuxWindowInputRegion } = require(modulePath));+ } finally {+ Module._load = originalLoad;+ if (cachedModule) require.cache[modulePath] = cachedModule;+ else delete require.cache[modulePath];+ }+ const writer = createLinuxWindowInputRegion(win);+ t.after(() => {+ writer.stop();+ child.emit("close", 0);+ child.stdin.destroy();+ child.stdout.destroy();+ });+ return { writer, child, app, win, writes, spawns, warnings, killSignals };+}++function trackSettlement(promise) {+ const result = { state: "pending" };+ void promise.then(+ () => {+ result.state = "fulfilled";+ },+ () => {+ result.state = "rejected";+ }+ );+ return result;+}++const REGION = { x: 104, y: 60, width: 84, height: 40, viewportWidth: 208, viewportHeight: 120 };++test("the helper starts lazily with the old-binary safeguard and waits for native acknowledgement", async (t) => {+ const { writer, child, writes, spawns } = loadInputRegion(t);+ assert.deepEqual(spawns, []);+ const applied = writer.set(REGION);+ const result = trackSettlement(applied);+ assert.deepEqual(writes, ["208 120 104 60 84 40\n"]);+ // An older helper recognizes --capabilities and exits instead of falling+ // through to its default paste action on the new, unrecognized server flag.+ assert.deepEqual(spawns, [+ [+ "/app/linux-fast-paste",+ ["--capabilities", "--input-region-server", "--window", "4275878552"],+ { stdio: ["pipe", "pipe", "ignore"] },+ ],+ ]);+ await Promise.resolve();+ assert.equal(result.state, "pending");+ child.stdout.write("OK\n");+ await applied;+});++test("overlapping narrow, full and narrow updates retain FIFO acknowledgement order", async (t) => {+ const { writer, child, writes, spawns } = loadInputRegion(t);+ const applied = [writer.set(REGION), writer.set(null), writer.set({ ...REGION, x: 12 })];+ const results = applied.map(trackSettlement);+ assert.deepEqual(writes, ["208 120 104 60 84 40\n", "full\n", "208 120 12 60 84 40\n"]);+ child.stdout.write("OK\n");+ await applied[0];+ assert.deepEqual(+ results.map((result) => result.state),+ ["fulfilled", "pending", "pending"]+ );+ child.stdout.write("OK\n");+ await applied[1];+ assert.deepEqual(+ results.map((result) => result.state),+ ["fulfilled", "fulfilled", "pending"]+ );+ child.stdout.write("OK\n");+ await Promise.all(applied);+ assert.equal(spawns.length, 1);+});++test("fragmented and combined acknowledgement chunks settle each queued update once", async (t) => {+ const { writer, child } = loadInputRegion(t);+ const applied = [writer.set(REGION), writer.set(null)];+ const results = applied.map(trackSettlement);+ child.stdout.write("O");+ await Promise.resolve();+ assert.deepEqual(+ results.map((result) => result.state),+ ["pending", "pending"]+ );+ child.stdout.write("K\nOK\n");+ await Promise.all(applied);+ assert.deepEqual(+ results.map((result) => result.state),+ ["fulfilled", "fulfilled"]+ );+});++test("fractional or clipped coordinates and an empty region are valid", async (t) => {+ const { writer, child, writes } = loadInputRegion(t);+ const applied = writer.set({ ...REGION, x: -12.5, y: 60.25, width: 0, height: 0 });+ assert.deepEqual(writes, ["208 120 -12.5 60.25 0 0\n"]);+ child.stdout.write("OK\n");+ await applied;+});++test("timeout blocks pending and later callers until the old native writer closes", async (t) => {+ t.mock.timers.enable({ apis: ["setTimeout"] });+ const { writer, child, writes, spawns, killSignals, warnings } = loadInputRegion(t);+ const applied = [writer.set(REGION), writer.set(null)];+ const results = applied.map(trackSettlement);+ t.mock.timers.tick(1000);+ const later = writer.set(REGION);+ applied.push(later);+ results.push(trackSettlement(later));+ child.stdout.write("OK\nOK\n");+ await Promise.resolve();+ assert.deepEqual(killSignals, ["SIGKILL"]);+ assert.deepEqual(+ results.map((result) => result.state),+ ["pending", "pending", "pending"]+ );+ assert.equal(writes.length, 2, "failed writers must not receive subsequent region commands");+ child.emit("close", null, "SIGKILL");+ await Promise.all(applied.map((promise) => assert.rejects(promise, /update timed out/)));+ await assert.rejects(writer.set(null), /update timed out/);+ assert.equal(spawns.length, 1);+ assert.equal(warnings.length, 1);+});++test("acknowledged updates clear their timeout and allow later region changes", async (t) => {+ t.mock.timers.enable({ apis: ["setTimeout"] });+ const { writer, child, killSignals } = loadInputRegion(t);+ const applied = writer.set(REGION);+ child.stdout.write("OK\n");+ await applied;+ t.mock.timers.tick(2000);+ assert.deepEqual(killSignals, []);+ const full = writer.set(null);+ child.stdout.write("OK\n");+ await full;+});++for (const stream of ["process", "stdin", "stdout"]) {+ test(`${stream} errors wait for close before rejecting queued updates`, async (t) => {+ const { writer, child, killSignals, app, win } = loadInputRegion(t);+ const applied = [writer.set(REGION), writer.set(null)];+ const results = applied.map(trackSettlement);+ const emitter = stream === "process" ? child : child[stream];+ emitter.emit("error", new Error("broken helper"));+ await Promise.resolve();+ assert.deepEqual(killSignals, ["SIGKILL"]);+ assert.deepEqual(+ results.map((result) => result.state),+ ["pending", "pending"]+ );+ child.emit("close", 1);+ await Promise.all(applied.map((promise) => assert.rejects(promise, /broken helper/)));+ assert.equal(app.listenerCount("before-quit"), 0);+ assert.equal(win.listenerCount("closed"), 0);+ });+}++test("process exit does not reject an update until its streams close", async (t) => {+ const { writer, child, app, win } = loadInputRegion(t);+ const applied = writer.set(REGION);+ const result = trackSettlement(applied);+ child.emit("exit", 1);+ await Promise.resolve();+ assert.equal(result.state, "pending");+ child.emit("close", 1);+ await assert.rejects(applied, /helper exited/);+ assert.equal(app.listenerCount("before-quit"), 0);+ assert.equal(win.listenerCount("closed"), 0);+});++test("a thrown stdin write also waits for process closure before enabling fallback", async (t) => {+ const { writer, child, killSignals } = loadInputRegion(t);+ t.mock.method(child.stdin, "write", () => {+ throw new Error("closed pipe");+ });+ const applied = writer.set(REGION);+ const result = trackSettlement(applied);+ await Promise.resolve();+ assert.equal(result.state, "pending");+ assert.deepEqual(killSignals, ["SIGKILL"]);+ child.emit("close", 1);+ await assert.rejects(applied, /closed pipe/);+});++for (const [name, options, message] of [+ ["missing binary", { binary: null }, /helper not found/],+ ["zero X11 window handle", { windowId: 0 }, /X11 window handle unavailable/],+]) {+ test(`${name} rejects without waiting for a nonexistent subprocess`, async (t) => {+ const { writer, spawns, writes, app, win } = loadInputRegion(t, options);+ await assert.rejects(writer.set(REGION), message);+ assert.deepEqual(spawns, []);+ assert.deepEqual(writes, []);+ assert.equal(app.listenerCount("before-quit"), 0);+ assert.equal(win.listenerCount("closed"), 0);+ });+}++for (const [name, response] of [+ ["old helper capabilities", "paste-v1 selection-copy-v1 target-window-v1\n"],+ ["unexpected acknowledgement", "ERROR\n"],+ ["oversized unterminated output", "x".repeat(1025)],+]) {+ test(`${name} stops the helper and waits for close`, async (t) => {+ const { writer, child, killSignals } = loadInputRegion(t);+ const applied = writer.set(REGION);+ const result = trackSettlement(applied);+ child.stdout.write(response);+ await Promise.resolve();+ assert.equal(result.state, "pending");+ assert.deepEqual(killSignals, ["SIGKILL"]);+ child.emit("close", 0);+ await assert.rejects(applied, /Invalid input region response/);+ });+}++for (const [name, region] of [+ ["missing region", undefined],+ ["missing coordinates", {}],+ ["string coordinate", { ...REGION, x: "104" }],+ ["infinite coordinate", { ...REGION, y: Infinity }],+ ["zero viewport width", { ...REGION, viewportWidth: 0 }],+ ["NaN viewport height", { ...REGION, viewportHeight: NaN }],+ ["negative width", { ...REGION, width: -1 }],+ ["negative height", { ...REGION, height: -1 }],+]) {+ test(`${name} never reaches native stdin`, async (t) => {+ const { writer, spawns, writes } = loadInputRegion(t);+ await assert.rejects(writer.set(region), /Invalid input region/);+ assert.deepEqual(spawns, []);+ assert.deepEqual(writes, []);+ });+}++test("an invalid payload waits for an existing native writer to close before rejection", async (t) => {+ const { writer, child, writes, killSignals } = loadInputRegion(t);+ const applied = writer.set(REGION);+ const invalid = writer.set({ ...REGION, width: -1 });+ const results = [applied, invalid].map(trackSettlement);+ await Promise.resolve();+ assert.deepEqual(+ results.map((result) => result.state),+ ["pending", "pending"]+ );+ assert.deepEqual(killSignals, ["SIGKILL"]);+ assert.deepEqual(writes, ["208 120 104 60 84 40\n"]);+ child.emit("close", null, "SIGKILL");+ await assert.rejects(applied, /Invalid input region/);+ await assert.rejects(invalid, /Invalid input region/);+});++for (const event of ["closed", "before-quit"]) {+ test(`${event} terminates the helper once and settles pending work after close`, async (t) => {+ const { writer, child, app, win, killSignals, warnings } = loadInputRegion(t);+ const applied = writer.set(REGION);+ const result = trackSettlement(applied);+ const owner = event === "closed" ? win : app;+ owner.emit(event);+ owner.emit(event);+ writer.stop();+ await Promise.resolve();+ assert.equal(result.state, "pending");+ assert.deepEqual(killSignals, ["SIGKILL"]);+ child.emit("close", 0);+ await assert.rejects(applied, /Input region writer stopped/);+ await assert.rejects(writer.set(null), /Input region writer stopped/);+ assert.deepEqual(warnings, []);+ assert.equal(app.listenerCount("before-quit"), 0);+ assert.equal(win.listenerCount("closed"), 0);+ });+}++test("stopping an unused writer prevents subsequent native spawning", async (t) => {+ const { writer, app, win, spawns } = loadInputRegion(t);+ writer.stop();+ await assert.rejects(writer.set(REGION), /Input region writer stopped/);+ assert.deepEqual(spawns, []);+ assert.equal(app.listenerCount("before-quit"), 0);+ assert.equal(win.listenerCount("closed"), 0);+});diff --git a/test/helpers/mainWindowInteractivity.test.js b/test/helpers/mainWindowInteractivity.test.jsnew file mode 100644index 000000000..d20e069aa--- /dev/null+++ b/test/helpers/mainWindowInteractivity.test.js@@ -0,0 +1,181 @@+const test = require("node:test");+const assert = require("node:assert/strict");+const Module = require("node:module");+const { EventEmitter } = require("node:events");+let applyInputRegion;++// Load the real WindowManager with the module surface it touches at require+// time. setMainWindowInteractivity only reads `this.mainWindow`, so the tests+// call it on a minimal receiver instead of constructing a whole manager.+const originalLoad = Module._load;+Module._load = function loadWindowManagerWithStubs(request, parent, isMain) {+ if (request === "electron") {+ return {+ app: { on: () => undefined },+ screen: {+ getPrimaryDisplay: () => ({}),+ on: () => undefined,+ },+ BrowserWindow: class {},+ shell: {},+ dialog: {},+ ipcMain: { on: () => undefined, handle: () => undefined },+ Menu: {},+ };+ }+ if (request === "./debugLogger") {+ return { warn: () => undefined, debug: () => undefined, info: () => undefined };+ }+ if (request === "./linuxWindowInputRegion") {+ return {+ createLinuxWindowInputRegion: () => ({+ set: (region) => applyInputRegion(region),+ stop: () => undefined,+ }),+ };+ }+ if (request === "./hotkeyManager") return class {};+ if (request === "./dragManager") return class {};+ if (request === "./menuManager") return {};+ if (request === "./devServerManager") {+ return {+ DEV_SERVER_PORT: 5173,+ DEV_SERVER_URL: "http://localhost:5173",+ getAppFilePath: () => ({ path: "/app/index.html", query: {} }),+ waitForDevServer: () => Promise.resolve(),+ };+ }+ if (request === "./dockManager") return {};+ if (request === "./i18nMain") return { i18nMain: { t: (key) => key } };+ if (request === "./windowConfig") {+ return {+ MAIN_WINDOW_CONFIG: {},+ CONTROL_PANEL_CONFIG: {},+ NOTIFICATION_WINDOW_CONFIG: {},+ WINDOW_SIZES: {},+ WindowPositionUtil: { setupAlwaysOnTop: () => undefined },+ };+ }+ return originalLoad.call(this, request, parent, isMain);+};+const WindowManager = require("../../src/helpers/windowManager");+Module._load = originalLoad;++const fakeWindow = () => {+ const calls = [];+ return {+ calls,+ isDestroyed: () => false,+ setIgnoreMouseEvents: (ignore, options) => calls.push({ ignore, options }),+ };+};++// Hover in, hover out, hover in again — the sequence App.jsx drives from the+// pill's mouseenter/mouseleave handlers.+const hoverCycle = (platform, win = fakeWindow()) => {+ const original = Object.getOwnPropertyDescriptor(process, "platform");+ Object.defineProperty(process, "platform", { value: platform, configurable: true });+ try {+ const setInteractivity = WindowManager.prototype.setMainWindowInteractivity.bind({+ mainWindow: win,+ });+ setInteractivity(true);+ setInteractivity(false);+ setInteractivity(true);+ } finally {+ Object.defineProperty(process, "platform", original);+ }+ return win.calls;+};++const INTERACTIVE = { ignore: false, options: undefined };++test("Windows keeps the existing always-interactive behavior", () => {+ assert.deepEqual(hoverCycle("win32"), [INTERACTIVE, INTERACTIVE, INTERACTIVE]);+});++test("Linux native capture fallback does not request unsupported forwarding", () => {+ assert.deepEqual(hoverCycle("linux"), [+ INTERACTIVE,+ { ignore: true, options: undefined },+ INTERACTIVE,+ ]);+});++test("macOS still returns the pill to click-through on mouse-leave", () => {+ assert.deepEqual(hoverCycle("darwin"), [+ INTERACTIVE,+ { ignore: true, options: { forward: true } },+ INTERACTIVE,+ ]);+});++test("a destroyed window is never touched", () => {+ const win = fakeWindow();+ win.isDestroyed = () => true;++ assert.deepEqual(hoverCycle("linux", win), []);+});++test("Linux reports visibility and restores capture only after a failed shape writer settles", async () => {+ const original = Object.getOwnPropertyDescriptor(process, "platform");+ Object.defineProperty(process, "platform", { value: "linux", configurable: true });+ try {+ let visible = false;+ const win = { ...fakeWindow(), isVisible: () => visible, isMinimized: () => false };+ const manager = Object.assign(Object.create(WindowManager.prototype), { mainWindow: win });+ applyInputRegion = async () => undefined;+ assert.equal(await manager.setMainWindowInputRegion(null), false);+ visible = true;+ assert.equal(await manager.setMainWindowInputRegion(null), true);+ let rejectAfterClose;+ applyInputRegion = () =>+ new Promise((_, reject) => {+ rejectAfterClose = reject;+ });+ const applying = manager.setMainWindowInputRegion(null);+ assert.deepEqual(win.calls, []);+ rejectAfterClose(new Error("helper closed"));+ await assert.rejects(applying, /helper closed/);+ assert.deepEqual(win.calls, [INTERACTIVE]);+ win.isDestroyed = () => true;+ assert.equal(await manager.setMainWindowInputRegion(null), false);+ } finally {+ Object.defineProperty(process, "platform", original);+ }+});++test("Linux native visibility reports hide, show, minimize and restore to the renderer", () => {+ const original = Object.getOwnPropertyDescriptor(process, "platform");+ Object.defineProperty(process, "platform", { value: "linux", configurable: true });+ const win = new EventEmitter();+ let visible = true;+ let minimized = false;+ const notifications = [];+ win.isVisible = () => visible;+ win.isMinimized = () => minimized;+ win.webContents = { send: (...args) => notifications.push(args) };+ try {+ WindowManager.prototype.registerMainWindowEvents.call({+ mainWindow: win,+ enforceMainWindowOnTop: () => {},+ });+ win.emit("ready-to-show");+ visible = false;+ win.emit("hide");+ visible = true;+ win.emit("show");+ minimized = true;+ win.emit("minimize");+ minimized = false;+ win.emit("restore");+ assert.deepEqual(notifications, [+ ["main-window-visibility-changed", false],+ ["main-window-visibility-changed", true],+ ["main-window-visibility-changed", false],+ ["main-window-visibility-changed", true],+ ]);+ } finally {+ Object.defineProperty(process, "platform", original);+ }+});diff --git a/test/hooks/dictationErrorAutoHide.test.js b/test/hooks/dictationErrorAutoHide.test.jsnew file mode 100644index 000000000..6ea4ead38--- /dev/null+++ b/test/hooks/dictationErrorAutoHide.test.js@@ -0,0 +1,102 @@+const test = require("node:test");+const assert = require("node:assert/strict");+const React = require("react");+const { createRoot } = require("react-dom/client");+const {+ createRendererServer,+ installBrowserGlobals,+ installHookDom,+} = require("../lib/rendererTestHarness");++const AUTO_HIDE_ON = `+ export const useSettingsStore = { getState: () => ({ floatingIconAutoHide: true }) };+`;++// Mount the real size owner with "Auto-hide when idle" enabled and an error+// card on screen, then dismiss the card the way Retry does.+async function mountWithErrorCard(t) {+ // Registered before the browser globals so React still has `window` when the+ // harness tears the root down.+ let root = null;+ t.after(async () => {+ if (root) await React.act(async () => root.unmount());+ });++ const hideCalls = [];+ installBrowserGlobals(t, {+ window: {+ electronAPI: {+ hideWindow: async () => {+ hideCalls.push("hide");+ },+ },+ },+ });+ const container = installHookDom(t);+ const vite = await createRendererServer(t, {+ cachePrefix: "openwhispr-dictation-error-auto-hide-",+ mockModules: { "stores/settingsStore": AUTO_HIDE_ON },+ });+ const { useMainWindowSizeOwner } = await vite.ssrLoadModule("/hooks/useMainWindowSizeOwner.js");++ const closed = { current: false };+ let props = {+ requestMainWindowSize: async () => ({ success: true }),+ dictationErrorActionCount: 1,+ toastCount: 0,+ isCommandMenuOpen: false,+ isCompactPill: false,+ isDictationActive: false,+ assistantOpen: false,+ assistantMounted: false,+ assistantOpenRef: closed,+ liveTranscriptOpen: false,+ liveTranscriptMounted: false,+ liveTranscriptOpenRef: closed,+ };+ let result;+ function Harness() {+ result = useMainWindowSizeOwner(props);+ return null;+ }++ root = createRoot(container);++ const render = async (next = {}) => {+ props = { ...props, ...next };+ await React.act(async () => {+ root.render(React.createElement(Harness));+ await Promise.resolve();+ });+ };+ // The reveal waits on the native resize and then on compositor frames, both+ // bounded by VISUAL_FRAME_TIMEOUT_MS.+ const settle = async () => {+ await React.act(async () => {+ await new Promise((resolve) => setTimeout(resolve, 900));+ });+ };++ await render();+ return { hideCalls, render, settle, state: () => result };+}++test("Retry keeps the pill window on screen while the new dictation runs", async (t) => {+ const { hideCalls, render, settle, state } = await mountWithErrorCard(t);++ // Retry starts recording, and that start dismisses the error card (#2141).+ await render({ dictationErrorActionCount: 0, isDictationActive: true });+ await settle();++ assert.deepEqual(hideCalls, [], "auto-hide must not hide a window that is still recording");+ assert.equal(state().dictationErrorPillHandoffActive, false, "the pill must be revealed again");+});++test("an error card dismissed while idle still auto-hides the window", async (t) => {+ const { hideCalls, render, settle } = await mountWithErrorCard(t);++ await render({ dictationErrorActionCount: 0, isDictationActive: false });+ await settle();++ assert.deepEqual(hideCalls, ["hide"]);+});diff --git a/test/hooks/linuxPillInteractivity.test.js b/test/hooks/linuxPillInteractivity.test.jsnew file mode 100644index 000000000..d681b7281--- /dev/null+++ b/test/hooks/linuxPillInteractivity.test.js@@ -0,0 +1,298 @@+const test = require("node:test");+const assert = require("node:assert/strict");+const React = require("react");+const { createRoot } = require("react-dom/client");+const {+ createRendererServer,+ installBrowserGlobals,+ installHookDom,+} = require("../lib/rendererTestHarness");++const PILL_RECT = { x: 156, y: 68, width: 40, height: 40 };+const VIEWPORT = { viewportWidth: 208, viewportHeight: 120 };+const EMPTY_REGION = { x: 0, y: 0, width: 0, height: 0, ...VIEWPORT };++async function mountPill(t, { platform = "linux", visible = true } = {}) {+ let root;+ t.after(async () => {+ if (root) await React.act(async () => root.unmount());+ });+ const regions = [];+ let measurements = 0;+ let visibilityListener;+ let writeRegion = async () => visible;+ let rect = PILL_RECT;+ const pill = {+ getBoundingClientRect: () => {+ measurements += 1;+ return rect;+ },+ };+ const pillRef = { current: pill };+ installBrowserGlobals(t, {+ window: {+ innerWidth: VIEWPORT.viewportWidth,+ innerHeight: VIEWPORT.viewportHeight,+ electronAPI: {+ getPlatform: () => platform,+ setMainWindowInputRegion: (region) => {+ regions.push(region);+ return writeRegion(region);+ },+ onMainWindowVisibilityChanged: (listener) => {+ visibilityListener = listener;+ return () => {+ visibilityListener = null;+ };+ },+ },+ },+ });+ const container = installHookDom(t);+ globalThis.document.hidden = false;+ const vite = await createRendererServer(t);+ const { useLinuxPillInteractivity } = await vite.ssrLoadModule(+ "/hooks/useLinuxPillInteractivity.ts"+ );+ t.mock.timers.enable({ apis: ["setInterval"] });+ let props = { pillRef, captureWindow: false, pillInteractive: true };+ function Harness() {+ useLinuxPillInteractivity(props);+ return null;+ }+ root = createRoot(container);+ const render = async (next = {}) => {+ props = { ...props, ...next };+ await React.act(async () => root.render(React.createElement(Harness)));+ };+ const tick = async () => {+ await React.act(async () => t.mock.timers.tick(50));+ };+ await render();+ return {+ regions,+ setRect: (next) => {+ rect = next;+ },+ setPillPresent: (present) => {+ pillRef.current = present ? pill : null;+ },+ setRegionWriter: (writer) => {+ writeRegion = writer;+ },+ measurements: () => measurements,+ setVisible: async (visible) => {+ await React.act(async () => visibilityListener(visible));+ },+ render,+ tick,+ unmount: async () => {+ await React.act(async () => root.unmount());+ root = null;+ },+ };+}++test("Linux measures the rendered pill and follows cancel emergence, docking and viewport changes", async (t) => {+ const mounted = await mountPill(t);+ assert.deepEqual(mounted.regions, [{ ...PILL_RECT, ...VIEWPORT }]);++ const expanded = { x: 62.5, y: 72, width: 133.5, height: 36 };+ mounted.setRect(expanded);+ await mounted.tick();+ assert.deepEqual(mounted.regions.at(-1), { ...expanded, ...VIEWPORT });++ const docked = { x: 12, y: 452.25, width: 98, height: 36 };+ mounted.setRect(docked);+ globalThis.window.innerWidth = 400;+ globalThis.window.innerHeight = 500;+ await mounted.tick();+ assert.deepEqual(mounted.regions.at(-1), {+ ...docked,+ viewportWidth: 400,+ viewportHeight: 500,+ });+});++test("unchanged geometry is reapplied because native resizes can reset the input region", async (t) => {+ const mounted = await mountPill(t);+ await mounted.tick();+ assert.deepEqual(mounted.regions, [+ { ...PILL_RECT, ...VIEWPORT },+ { ...PILL_RECT, ...VIEWPORT },+ ]);+});++test("controls and dragging request full input immediately despite an older pending region", async (t) => {+ const mounted = await mountPill(t);+ const pending = Promise.withResolvers();+ mounted.setRegionWriter((region) => (region ? pending.promise : Promise.resolve(true)));+ await mounted.tick();+ await mounted.render({ captureWindow: true });+ assert.equal(mounted.regions.at(-1), null);+ const writes = mounted.regions.length;+ const measurements = mounted.measurements();+ await mounted.tick();+ assert.equal(mounted.regions.length, writes);+ assert.equal(mounted.measurements(), measurements);++ await React.act(async () => pending.resolve(true));+ assert.equal(mounted.regions.at(-1), null);+ await mounted.render({ captureWindow: false });+ assert.deepEqual(mounted.regions.at(-1), { ...PILL_RECT, ...VIEWPORT });+});++test("suppressed and absent pills request an empty region while overlays keep full input", async (t) => {+ const mounted = await mountPill(t);+ await mounted.render({ pillInteractive: false });+ assert.deepEqual(mounted.regions.at(-1), EMPTY_REGION);+ await mounted.render({ captureWindow: true });+ assert.equal(mounted.regions.at(-1), null);++ mounted.setPillPresent(false);+ await mounted.render({ captureWindow: false, pillInteractive: true });+ assert.deepEqual(mounted.regions.at(-1), EMPTY_REGION);+ mounted.setPillPresent(true);+ await mounted.tick();+ assert.deepEqual(mounted.regions.at(-1), { ...PILL_RECT, ...VIEWPORT });+});++test("only one measurement update is pending and the next sample uses current geometry", async (t) => {+ const mounted = await mountPill(t);+ const pending = Promise.withResolvers();+ mounted.setRegionWriter(() => pending.promise);+ await mounted.tick();+ const writes = mounted.regions.length;+ const measurements = mounted.measurements();+ mounted.setRect({ x: 12, y: 72, width: 134, height: 36 });+ await mounted.tick();+ assert.equal(mounted.regions.length, writes);+ assert.equal(mounted.measurements(), measurements);++ await React.act(async () => pending.resolve(true));+ await mounted.tick();+ assert.deepEqual(mounted.regions.at(-1), {+ x: 12,+ y: 72,+ width: 134,+ height: 36,+ ...VIEWPORT,+ });+});++test("native hide stops sampling across prop changes and show applies the current region", async (t) => {+ const mounted = await mountPill(t);+ await mounted.setVisible(false);+ const writes = mounted.regions.length;+ await mounted.tick();+ assert.equal(mounted.regions.length, writes);+ assert.equal(globalThis.document.hidden, false);++ await mounted.render({ pillInteractive: false });+ assert.equal(mounted.regions.at(-1), null, "effect cleanup releases its previous region");+ const hiddenWrites = mounted.regions.length;+ await mounted.tick();+ assert.equal(mounted.regions.length, hiddenWrites);+ await mounted.setVisible(true);+ assert.deepEqual(mounted.regions.at(-1), EMPTY_REGION);+});++test("an initially hidden window stops after one update until native show", async (t) => {+ const mounted = await mountPill(t, { visible: false });+ assert.equal(mounted.regions.length, 1);+ await mounted.tick();+ assert.equal(mounted.regions.length, 1);++ await mounted.render({ pillInteractive: false });+ assert.equal(mounted.regions.at(-1), null);+ const writes = mounted.regions.length;+ await mounted.tick();+ assert.equal(mounted.regions.length, writes);++ mounted.setRegionWriter(async () => true);+ await mounted.setVisible(true);+ assert.deepEqual(mounted.regions.at(-1), EMPTY_REGION);+});++test("a region failure stops sampling and a later show starts a fresh update", async (t) => {+ const mounted = await mountPill(t);+ mounted.setRegionWriter(async () => {+ throw new Error("native input shaping unavailable");+ });+ await mounted.tick();+ const writes = mounted.regions.length;+ mounted.setRegionWriter(async () => true);+ await mounted.tick();+ assert.equal(mounted.regions.length, writes);+ await mounted.setVisible(true);+ assert.equal(mounted.regions.length, writes + 1);+ assert.deepEqual(mounted.regions.at(-1), { ...PILL_RECT, ...VIEWPORT });+});++test("an old rejected update cannot stop a new effect's sampling", async (t) => {+ const mounted = await mountPill(t);+ const pending = Promise.withResolvers();+ mounted.setRegionWriter(() => pending.promise);+ await mounted.tick();+ mounted.setRegionWriter(async () => true);+ await mounted.render({ pillInteractive: false });+ await React.act(async () => pending.reject(new Error("old update failed")));+ const writes = mounted.regions.length;+ await mounted.tick();+ assert.equal(mounted.regions.length, writes + 1);+ assert.deepEqual(mounted.regions.at(-1), EMPTY_REGION);+});++test("an old rejected update cannot stop sampling after native hide and show", async (t) => {+ const mounted = await mountPill(t);+ const pending = Promise.withResolvers();+ mounted.setRegionWriter(() => pending.promise);+ await mounted.tick();+ await mounted.setVisible(false);+ await mounted.setVisible(true);+ mounted.setRegionWriter(async () => true);+ await React.act(async () => pending.reject(new Error("hidden update failed")));+ const writes = mounted.regions.length;+ await mounted.tick();+ assert.equal(mounted.regions.length, writes + 1);+ assert.deepEqual(mounted.regions.at(-1), { ...PILL_RECT, ...VIEWPORT });+});++test("an old hidden response cannot suspend sampling after native show", async (t) => {+ const mounted = await mountPill(t);+ const pending = Promise.withResolvers();+ mounted.setRegionWriter(() => pending.promise);+ await mounted.tick();+ await mounted.setVisible(false);+ await mounted.setVisible(true);+ mounted.setRegionWriter(async () => true);+ await React.act(async () => pending.resolve(false));+ const writes = mounted.regions.length;+ await mounted.tick();+ assert.equal(mounted.regions.length, writes + 1);+ assert.deepEqual(mounted.regions.at(-1), { ...PILL_RECT, ...VIEWPORT });+});++test("unmount queues full input after a pending narrow region and silences later failures", async (t) => {+ const mounted = await mountPill(t);+ const pending = Promise.withResolvers();+ mounted.setRegionWriter((region) => (region ? pending.promise : Promise.resolve(true)));+ await mounted.tick();+ await mounted.unmount();+ assert.deepEqual(mounted.regions.slice(-2), [{ ...PILL_RECT, ...VIEWPORT }, null]);+ const writes = mounted.regions.length;+ await React.act(async () => pending.reject(new Error("unmounted update failed")));+ await mounted.tick();+ assert.equal(mounted.regions.length, writes);+});++for (const platform of ["darwin", "win32"]) {+ test(`${platform} keeps its native interactivity implementation`, async (t) => {+ const mounted = await mountPill(t, { platform });+ await mounted.render({ captureWindow: true });+ await mounted.tick();+ await mounted.unmount();+ assert.deepEqual(mounted.regions, []);+ assert.equal(mounted.measurements(), 0);+ });+}diff --git a/test/native/linuxInputRegion.c b/test/native/linuxInputRegion.cnew file mode 100644index 000000000..a39460852--- /dev/null+++ b/test/native/linuxInputRegion.c@@ -0,0 +1,148 @@+#include <X11/Xlib.h>+#include <X11/extensions/XTest.h>+#include <X11/extensions/shape.h>+#include <signal.h>+#include <stdio.h>+#include <stdlib.h>+#include <string.h>+#include <sys/wait.h>+#include <unistd.h>++static Display *display;+static pid_t helper_pid = -1;+static FILE *helper_input;+static FILE *helper_output;++static void cleanup(void)+{+ if (helper_pid > 0) {+ kill(helper_pid, SIGKILL);+ waitpid(helper_pid, NULL, 0);+ }+ if (display) XCloseDisplay(display);+}++static void require(int condition, const char *message)+{+ if (condition) return;+ fprintf(stderr, "%s\n", message);+ exit(1);+}++static void start_helper(const char *binary, Window window)+{+ int requests[2], responses[2];+ require(pipe(requests) == 0 && pipe(responses) == 0, "cannot create helper pipes");+ helper_pid = fork();+ require(helper_pid >= 0, "cannot fork helper");+ if (helper_pid == 0) {+ char window_id[32];+ snprintf(window_id, sizeof(window_id), "%lu", window);+ dup2(requests[0], STDIN_FILENO);+ dup2(responses[1], STDOUT_FILENO);+ close(requests[0]);+ close(requests[1]);+ close(responses[0]);+ close(responses[1]);+ close(ConnectionNumber(display));+ execl(binary, binary, "--capabilities", "--input-region-server", "--window", window_id, NULL);+ _exit(127);+ }+ close(requests[0]);+ close(responses[1]);+ helper_input = fdopen(requests[1], "w");+ helper_output = fdopen(responses[0], "r");+ require(helper_input && helper_output, "cannot open helper streams");+}++static void assert_rectangle(Window window, int kind, int expected_count,+ int x, int y, int width, int height)+{+ int count, ordering;+ XRectangle *rectangles = XShapeGetRectangles(display, window, kind, &count, &ordering);+ if (count != expected_count ||+ (count == 1 && (rectangles[0].x != x || rectangles[0].y != y ||+ rectangles[0].width != width || rectangles[0].height != height))) {+ fprintf(stderr, "shape %d: expected %d rectangle(s) at %d,%d %dx%d; got %d",+ kind, expected_count, x, y, width, height, count);+ if (count > 0) {+ fprintf(stderr, " at %d,%d %ux%u", rectangles[0].x, rectangles[0].y,+ rectangles[0].width, rectangles[0].height);+ }+ fprintf(stderr, "\n");+ exit(1);+ }+ if (rectangles) XFree(rectangles);+}++static void apply(Window window, const char *command, int native_width, int native_height,+ int count, int x, int y, int width, int height)+{+ char response[32];+ require(fputs(command, helper_input) >= 0 && fflush(helper_input) == 0, "helper write failed");+ require(fgets(response, sizeof(response), helper_output) != NULL, "helper exited before ACK");+ require(strcmp(response, "OK\n") == 0, "unexpected helper ACK");+ assert_rectangle(window, ShapeInput, count, x, y, width, height);+ assert_rectangle(window, ShapeBounding, 1, 0, 0, native_width, native_height);+}++static void assert_click_target(Window expected, int x, int y)+{+ XWarpPointer(display, None, DefaultRootWindow(display), 0, 0, 0, 0, 20 + x, 20 + y);+ XTestFakeButtonEvent(display, 1, True, CurrentTime);+ XTestFakeButtonEvent(display, 1, False, CurrentTime);+ XSync(display, False);+ int clicks = 0;+ while (XPending(display)) {+ XEvent event;+ XNextEvent(display, &event);+ if (event.type != ButtonPress) continue;+ require(event.xbutton.window == expected, "click delivered to the wrong input region");+ clicks += 1;+ }+ require(clicks == 1, "expected one native click");+}++int main(int argc, char **argv)+{+ require(argc == 2, "expected compiled helper path");+ atexit(cleanup);+ alarm(8);+ display = XOpenDisplay(NULL);+ require(display != NULL, "cannot open the isolated test display");+ Window root = DefaultRootWindow(display);+ Window background = XCreateSimpleWindow(display, root, 20, 20, 416, 240, 0, 0, 0);+ Window foreground = XCreateSimpleWindow(display, root, 20, 20, 208, 120, 0, 0, 0);+ XSelectInput(display, background, ButtonPressMask);+ XSelectInput(display, foreground, ButtonPressMask);+ XMapWindow(display, background);+ XMapRaised(display, foreground);+ XSync(display, False);+ start_helper(argv[1], foreground);++ apply(foreground, "208 120 156 68 40 40\n", 208, 120, 1, 156, 68, 40, 40);+ assert_click_target(foreground, 170, 80);+ assert_click_target(background, 10, 10);++ XResizeWindow(display, foreground, 416, 240);+ XSync(display, False);+ apply(foreground, "208 120 156 68 40 40\n", 416, 240, 1, 312, 136, 80, 80);+ assert_click_target(foreground, 340, 160);+ assert_click_target(background, 170, 80);++ apply(foreground, "208 120 -1.25 10.25 11.5 5.25\n", 416, 240, 1, 0, 20, 21, 11);+ apply(foreground, "208 120 200.25 115.25 20 20\n", 416, 240, 1, 400, 230, 16, 10);+ apply(foreground, "208 120 156.25 68.25 0 0\n", 416, 240, 0, 0, 0, 0, 0);+ assert_click_target(background, 340, 160);+ apply(foreground, "full\n", 416, 240, 1, 0, 0, 416, 240);+ assert_click_target(foreground, 10, 10);++ require(fclose(helper_input) == 0, "cannot close helper input");+ int status;+ require(waitpid(helper_pid, &status, 0) == helper_pid, "cannot wait for helper exit");+ helper_pid = -1;+ require(WIFEXITED(status) && WEXITSTATUS(status) == 0, "helper did not exit cleanly on EOF");+ fclose(helper_output);+ puts("input region native checks passed");+ return 0;+}