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): let the voice assistant panel take the keyboard (#2644)
voidd5ecb2d9d0bb
diff --git a/resources/linux-fast-paste.c b/resources/linux-fast-paste.cindex 1d8a9f840..c9e666413 100644--- a/resources/linux-fast-paste.c+++ b/resources/linux-fast-paste.c@@ -15,6 +15,7 @@#include <fcntl.h>#include <linux/input.h>#include <math.h>+#include <poll.h>#include <sys/ioctl.h>#include <time.h>#include <unistd.h>@@ -819,9 +820,44 @@ 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;}+#define WM_ROUND_TRIP_TIMEOUT_MS 100++/* Mutter only follows a client's focus change once it has made an X request+ * of its own since its last focus change. Converting the window manager's+ * WM_Sn selection makes it answer with requests, and waiting for the answer+ * orders them before ours. With no window manager, the X server answers. */+static void round_trip_window_manager(Display *display, Window requestor)+{+ char selection[16];+ snprintf(selection, sizeof(selection), "WM_S%d", DefaultScreen(display));+ Atom property = XInternAtom(display, "_OPENWHISPR_WM_ROUND_TRIP", False);+ XEvent event;+ /* Drop replies that came after an earlier round trip gave up, so they can't+ * stand in for this one (a reply later than this drain still can). */+ while (XCheckTypedWindowEvent(display, requestor, SelectionNotify, &event)) {}+ XConvertSelection(display, XInternAtom(display, selection, False),+ XInternAtom(display, "TARGETS", False), property, requestor,+ CurrentTime);+ XFlush(display);+ struct pollfd connection = { .fd = ConnectionNumber(display), .events = POLLIN };+ while (!XCheckTypedWindowEvent(display, requestor, SelectionNotify, &event)) {+ if (poll(&connection, 1, WM_ROUND_TRIP_TIMEOUT_MS) <= 0) return;+ }+ XDeleteProperty(display, requestor, property);+}++static int ignore_x_error(Display *display, XErrorEvent *error)+{+ (void)display;+ (void)error;+ return 0;+}+/* 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. */+ * native Wayland apps, where XWayland cannot query a fresh global cursor.+ * "focus" takes keyboard focus: the window is override-redirect, so no window+ * manager hands it focus on a click or an activation request. */static int serve_input_region(Window window){if (window == None) return 1;@@ -834,11 +870,29 @@ static int serve_input_region(Window window)XCloseDisplay(display);return 1;}+ Window requestor = XCreateSimpleWindow(display, DefaultRootWindow(display),+ 0, 0, 1, 1, 0, 0, 0);char request[512];while (fgets(request, sizeof(request), stdin)) {if (strcmp(request, "full\n") == 0) {XShapeCombineMask(display, window, ShapeInput, 0, 0, None, ShapeSet);+ } else if (strcmp(request, "focus\n") == 0) {+ round_trip_window_manager(display, requestor);+ XWindowAttributes attributes;+ XErrorHandler previous = XSetErrorHandler(ignore_x_error);+ if (XGetWindowAttributes(display, window, &attributes) &&+ attributes.map_state == IsViewable) {+ /* Leave first: a compositor can move keyboard focus to a native+ * Wayland app without moving X focus off this window, and+ * focusing it again then produces no FocusIn to follow. */+ XSetInputFocus(display, PointerRoot, RevertToPointerRoot, CurrentTime);+ XSetInputFocus(display, window, RevertToParent, CurrentTime);+ }+ /* An unmap racing the check must not end the server that owns the+ * window's input region. */+ XSync(display, False);+ XSetErrorHandler(previous);} else {double viewport_width, viewport_height, x, y, width, height;if (sscanf(request, "%lf %lf %lf %lf %lf %lf", &viewport_width,diff --git a/src/helpers/ipcHandlers.js b/src/helpers/ipcHandlers.jsindex a647ef971..e496b92c2 100644--- a/src/helpers/ipcHandlers.js+++ b/src/helpers/ipcHandlers.js@@ -3250,6 +3250,9 @@ class IPCHandlers {mainWindow.showInactive();} else {mainWindow.blur();+ // On Linux blur() leaves X focus on the pill, where the assistant panel+ // took it, so the paste would land in the panel.+ if (process.platform === "linux") this.windowManager.returnLinuxKeyboardFocus();await new Promise((resolve) => setTimeout(resolve, 80));}}diff --git a/src/helpers/linuxWindowInputRegion.js b/src/helpers/linuxWindowInputRegion.jsindex 2ef2aa806..5867302ac 100644--- a/src/helpers/linuxWindowInputRegion.js+++ b/src/helpers/linuxWindowInputRegion.js@@ -33,7 +33,7 @@ function createLinuxWindowInputRegion(win) {const onClose = () => stop();const fail = (error) => {if (failure) return;- debugLogger.warn("Linux input region unavailable", { error: error.message });+ debugLogger.warn("Linux input region helper unavailable", { error: error.message });stop(error);};@@ -74,11 +74,32 @@ function createLinuxWindowInputRegion(win) {});};- const set = (region) => {- if (failure)- return stopped.then(() => {- throw failure;+ const rejectWithFailure = () =>+ stopped.then(() => {+ throw failure;+ });++ const send = (line) => {+ if (failure) return rejectWithFailure();+ return new Promise((resolve, reject) => {+ pending.push({+ resolve,+ reject,+ timer: setTimeout(+ () => fail(new Error("Linux input region helper timed out")),+ APPLY_TIMEOUT_MS+ ),});+ try {+ if (!child) start();+ child.stdin.write(line);+ } catch (error) {+ fail(error);+ }+ });+ };++ const set = (region) => {let line = "full\n";if (region !== null) {const values = [@@ -97,33 +118,18 @@ function createLinuxWindowInputRegion(win) {values[5] < 0) {fail(new Error("Invalid input region"));- return stopped.then(() => {- throw failure;- });+ return rejectWithFailure();}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);- }- });+ return send(line);};+ const focus = () => send("focus\n");+app.once("before-quit", onClose);win.once("closed", onClose);- return { set, stop };+ return { set, focus, stop };}module.exports = { createLinuxWindowInputRegion };diff --git a/src/helpers/windowManager.js b/src/helpers/windowManager.jsindex 31610fa6f..a7460e145 100644--- a/src/helpers/windowManager.js+++ b/src/helpers/windowManager.js@@ -231,15 +231,34 @@ class WindowManager {if (process.platform !== "darwin") {this.mainWindow.focus();}+ // Linux: focusable:false made the pill an override-redirect X11 window,+ // which no window manager focuses, so focus() can't reach it there.+ if (process.platform === "linux") this._focusLinuxMainWindow();} else {- // On Windows/Linux the pill is a normal/toolbar window, so focus()- // activated OpenWhispr — blur before dropping focusability to hand- // the foreground back to the app the user was in. On macOS nothing- // was activated, and blur() would only churn key-window state.+ // Read before blur(): it clears isFocused() even where X focus stays+ // on the Linux pill.+ const hadFocus = this.mainWindow.isFocused();+ // On Windows the pill is a normal window, so focus() activated+ // OpenWhispr — blur before dropping focusability to hand the+ // foreground back to the app the user was in (Linux needs the remap+ // below). On macOS nothing was activated, and blur() would only churn+ // key-window state.if (process.platform !== "darwin") {this.mainWindow.blur();}this.mainWindow.setFocusable(false);+ if (process.platform === "linux") {+ // A click on Close sends its focus request before the panel closes,+ // and the helper can still be granting it; hand back after it lands.+ const focusRequest = this._linuxFocusRequest;+ if (focusRequest) {+ void focusRequest.then(() => {+ if (!this._assistantPanelOpen) this.returnLinuxKeyboardFocus();+ });+ } else if (hadFocus) {+ this.returnLinuxKeyboardFocus();+ }+ }}this.enforceMainWindowOnTop();}@@ -248,6 +267,16 @@ class WindowManager {this._updateMainContentProtection();}+ // blur() leaves X focus on the Linux pill, and GNOME hands focus back to the+ // previous app only when the focused override-redirect window unmaps. A hidden+ // pill holds no focus, and remapping it would undo whatever hid it.+ returnLinuxKeyboardFocus() {+ const win = this.mainWindow;+ if (!win || win.isDestroyed() || !win.isVisible()) return;+ win.hide();+ win.showInactive();+ }+setAssistantPanelBusy(busy) {this._assistantPanelBusy = Boolean(busy);}@@ -273,15 +302,31 @@ class WindowManager {}}- async setMainWindowInputRegion(region) {+ _mainWindowInputRegion() {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) };}+ return this._linuxWindowInputRegion;+ }++ // The writer logs its own failure.+ _focusLinuxMainWindow() {+ const request = this._mainWindowInputRegion()+ .focus()+ .catch(() => {});+ this._linuxFocusRequest = request;+ void request.then(() => {+ if (this._linuxFocusRequest === request) this._linuxFocusRequest = null;+ });+ }++ async setMainWindowInputRegion(region) {+ const win = this.mainWindow;+ if (process.platform !== "linux" || !win || win.isDestroyed()) return false;try {- await this._linuxWindowInputRegion.set(region);+ await this._mainWindowInputRegion().set(region);return !win.isDestroyed() && win.isVisible() && !win.isMinimized();} catch (error) {// The writer rejects after its process closes, so an old shape cannot@@ -2151,6 +2196,21 @@ class WindowManager {);});}+ // A click never focuses the pill, so returning to an open assistant+ // panel from another app has to take focus again. isFocused() follows X+ // focus, so this misses a compositor that hands the keyboard to a native+ // Wayland app without moving X focus off the pill. A request still in+ // flight already covers the click, and repeats would queue behind it.+ win.webContents.on("input-event", (_event, input) => {+ if (+ input.type === "mouseDown" &&+ this._assistantPanelOpen &&+ !win.isFocused() &&+ !this._linuxFocusRequest+ ) {+ this._focusLinuxMainWindow();+ }+ });}// Safety timeout: force show the window if ready-to-show doesn't fire within 10 secondsdiff --git a/src/hooks/useAudioRecording.js b/src/hooks/useAudioRecording.jsindex bcfa1878e..57886cb1e 100644--- a/src/hooks/useAudioRecording.js+++ b/src/hooks/useAudioRecording.js@@ -177,8 +177,9 @@ export const useAudioRecording = (toast, options = {}) => {// device still opens exactly once.void audioManagerRef.current.prepareMicCapture?.();- // The floating dictation panel is non-focusable, so the foreground app is- // still the user's actual editing target here. Refresh it for recordings+ // The floating dictation panel only takes focus while the assistant panel+ // is open, so the foreground app is otherwise still the user's actual+ // editing target here. Refresh it for recordings// started from the panel itself as well as from global hotkeys; otherwise// paste can reactivate a stale target from the preceding dictation.try {diff --git a/test/helpers/ipcPasteOutcome.test.js b/test/helpers/ipcPasteOutcome.test.jsindex ec6a8da17..c65a3ddf6 100644--- a/test/helpers/ipcPasteOutcome.test.js+++ b/test/helpers/ipcPasteOutcome.test.js@@ -126,6 +126,41 @@ test("paste-text preserves a clipboard-only fallback as not pasted", async () =>assert.deepEqual(result, { success: true, pasted: false });});+test("paste-text on Linux hands back the keyboard the assistant panel took before pasting", async () => {+ const original = Object.getOwnPropertyDescriptor(process, "platform");+ try {+ for (const [platform, expected] of [+ ["linux", ["blur", "return", "paste"]],+ ["win32", ["blur", "paste"]],+ ]) {+ Object.defineProperty(process, "platform", { value: platform, configurable: true });+ const order = [];+ target.windowManager = {+ isOnboardingDemoActive: () => false,+ mainWindow: {+ isDestroyed: () => false,+ isFocused: () => true,+ blur: () => order.push("blur"),+ },+ // blur() leaves X focus on the Linux pill, so the paste would land in+ // the panel instead of the user's app.+ returnLinuxKeyboardFocus: () => order.push("return"),+ };+ target.clipboardManager = {+ pasteText: async () => {+ order.push("paste");+ },+ };++ await handlers.get("paste-text")({ sender: { id: 1 } }, "dictated with the panel open");++ assert.deepEqual(order, expected, platform);+ }+ } finally {+ Object.defineProperty(process, "platform", original);+ }+});+test("paste-text tells the renderer why a paste was held back", async () => {target.windowManager = { isOnboardingDemoActive: () => false };target.clipboardManager = {diff --git a/test/helpers/linuxWindowInputRegion.test.js b/test/helpers/linuxWindowInputRegion.test.jsindex 7cf74d529..95e325f9c 100644--- a/test/helpers/linuxWindowInputRegion.test.js+++ b/test/helpers/linuxWindowInputRegion.test.js@@ -120,6 +120,22 @@ test("overlapping narrow, full and narrow updates retain FIFO acknowledgement orassert.equal(spawns.length, 1);});+test("keyboard focus requests share the acknowledged FIFO with region updates", async (t) => {+ const { writer, child, writes, spawns } = loadInputRegion(t);+ const applied = [writer.set(REGION), writer.focus()];+ const results = applied.map(trackSettlement);+ assert.deepEqual(writes, ["208 120 104 60 84 40\n", "focus\n"]);+ child.stdout.write("OK\n");+ await applied[0];+ assert.deepEqual(+ results.map((result) => result.state),+ ["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)];@@ -164,8 +180,8 @@ test("timeout blocks pending and later callers until the old native writer close);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/);+ await Promise.all(applied.map((promise) => assert.rejects(promise, /helper timed out/)));+ await assert.rejects(writer.set(null), /helper timed out/);assert.equal(spawns.length, 1);assert.equal(warnings.length, 1);});diff --git a/test/helpers/mainWindowInteractivity.test.js b/test/helpers/mainWindowInteractivity.test.jsindex d20e069aa..29e62dc25 100644--- a/test/helpers/mainWindowInteractivity.test.js+++ b/test/helpers/mainWindowInteractivity.test.js@@ -3,6 +3,7 @@ const assert = require("node:assert/strict");const Module = require("node:module");const { EventEmitter } = require("node:events");let applyInputRegion;+let focusInputRegion;// Load the real WindowManager with the module surface it touches at require// time. setMainWindowInteractivity only reads `this.mainWindow`, so the tests@@ -30,6 +31,7 @@ Module._load = function loadWindowManagerWithStubs(request, parent, isMain) {return {createLinuxWindowInputRegion: () => ({set: (region) => applyInputRegion(region),+ focus: () => focusInputRegion(),stop: () => undefined,}),};@@ -154,7 +156,9 @@ test("Linux native visibility reports hide, show, minimize and restore to the reconst notifications = [];win.isVisible = () => visible;win.isMinimized = () => minimized;- win.webContents = { send: (...args) => notifications.push(args) };+ win.webContents = Object.assign(new EventEmitter(), {+ send: (...args) => notifications.push(args),+ });try {WindowManager.prototype.registerMainWindowEvents.call({mainWindow: win,@@ -179,3 +183,116 @@ test("Linux native visibility reports hide, show, minimize and restore to the reObject.defineProperty(process, "platform", original);}});++test("a failed Linux keyboard focus request settles quietly", async () => {+ const win = { ...fakeWindow(), isVisible: () => true, isMinimized: () => false };+ const manager = Object.assign(Object.create(WindowManager.prototype), { mainWindow: win });+ let rejectFocus;+ focusInputRegion = () =>+ new Promise((_, reject) => {+ rejectFocus = reject;+ });+ manager._focusLinuxMainWindow();+ // The writer logs its own failure; a rejected request would skip a closing+ // panel's deferred hand-back.+ rejectFocus(new Error("helper closed"));+ await new Promise((resolve) => setImmediate(resolve));+ assert.equal(manager._linuxFocusRequest, null);+});++test("clicks on an unfocused panel share the focus request already in flight", async () => {+ const original = Object.getOwnPropertyDescriptor(process, "platform");+ Object.defineProperty(process, "platform", { value: "linux", configurable: true });+ const win = new EventEmitter();+ win.isFocused = () => false;+ win.isVisible = () => true;+ win.isMinimized = () => false;+ win.isDestroyed = () => false;+ win.webContents = Object.assign(new EventEmitter(), { send: () => undefined });+ const manager = Object.assign(Object.create(WindowManager.prototype), {+ mainWindow: win,+ _assistantPanelOpen: true,+ enforceMainWindowOnTop: () => {},+ });+ const lands = [];+ focusInputRegion = () => new Promise((resolve) => lands.push(resolve));+ const mouseDown = () => win.webContents.emit("input-event", {}, { type: "mouseDown" });+ try {+ WindowManager.prototype.registerMainWindowEvents.call(manager);+ win.emit("ready-to-show");+ mouseDown();+ mouseDown();+ // Each request can wait on the window manager, and queued repeats would+ // run into the helper's timeout, which ends the input region with it.+ assert.equal(lands.length, 1);+ lands[0]();+ await new Promise((resolve) => setImmediate(resolve));+ mouseDown();+ assert.equal(lands.length, 2, "a click after the request landed asks again");+ lands[1]();+ } finally {+ Object.defineProperty(process, "platform", original);+ }+});++test("only Linux listens for clicks that re-take keyboard focus", () => {+ const original = Object.getOwnPropertyDescriptor(process, "platform");+ try {+ for (const [platform, expected] of [+ ["linux", 1],+ ["win32", 0],+ ["darwin", 0],+ ]) {+ Object.defineProperty(process, "platform", { value: platform, configurable: true });+ const win = new EventEmitter();+ win.isVisible = () => true;+ win.isMinimized = () => false;+ win.webContents = Object.assign(new EventEmitter(), { send: () => undefined });+ WindowManager.prototype.registerMainWindowEvents.call({+ mainWindow: win,+ enforceMainWindowOnTop: () => {},+ });+ win.emit("ready-to-show");+ assert.equal(win.webContents.listenerCount("input-event"), expected, platform);+ }+ } finally {+ Object.defineProperty(process, "platform", original);+ }+});++test("Linux takes keyboard focus again when an unfocused open assistant panel is clicked", () => {+ const original = Object.getOwnPropertyDescriptor(process, "platform");+ Object.defineProperty(process, "platform", { value: "linux", configurable: true });+ const win = new EventEmitter();+ let focused = false;+ win.isFocused = () => focused;+ win.isVisible = () => true;+ win.isMinimized = () => false;+ win.webContents = Object.assign(new EventEmitter(), { send: () => undefined });+ const focusRequests = [];+ const manager = {+ mainWindow: win,+ _assistantPanelOpen: false,+ enforceMainWindowOnTop: () => {},+ _focusLinuxMainWindow: () => focusRequests.push("focus"),+ };+ const mouseDown = () => win.webContents.emit("input-event", {}, { type: "mouseDown" });+ try {+ WindowManager.prototype.registerMainWindowEvents.call(manager);+ win.emit("ready-to-show");+ mouseDown();+ assert.deepEqual(focusRequests, [], "a pill click must leave the user's app focused");+ manager._assistantPanelOpen = true;+ win.webContents.emit("input-event", {}, { type: "mouseMove" });+ focused = true;+ mouseDown();+ assert.deepEqual(focusRequests, [], "the panel already has the keyboard");+ // Back from another app: the window manager never refocuses the+ // override-redirect pill on a click.+ focused = false;+ mouseDown();+ assert.deepEqual(focusRequests, ["focus"]);+ } finally {+ Object.defineProperty(process, "platform", original);+ }+});diff --git a/test/helpers/windowManagerAssistantPanel.test.js b/test/helpers/windowManagerAssistantPanel.test.jsindex 17edf9b50..9c6b3c277 100644--- a/test/helpers/windowManagerAssistantPanel.test.js+++ b/test/helpers/windowManagerAssistantPanel.test.js@@ -5,6 +5,9 @@ const requestedMainWindowPositions = [];const createdBrowserWindows = [];const screenListeners = [];const builtMenus = [];+const inputRegionRequests = [];+const requestFocus = async () => inputRegionRequests.push("focus");+let focusInputRegion = requestFocus;// Same stub set as windowManagerMeetingNotification.test.js: WindowManager// pulls in electron + sibling managers at require time.@@ -123,6 +126,14 @@ Module._load = function loadWindowManagerWithStubs(request, parent, isMain) {};if (request === "./dockManager") return {};if (request === "./i18nMain") return { i18nMain: { t: (key) => key } };+ if (request === "./linuxWindowInputRegion") {+ return {+ createLinuxWindowInputRegion: () => ({+ focus: () => focusInputRegion(),+ stop: () => undefined,+ }),+ };+ }if (request === "./windowConfig") {return {MAIN_WINDOW_CONFIG: {},@@ -154,9 +165,10 @@ Module._load = function loadWindowManagerWithStubs(request, parent, isMain) {const WindowManager = require("../../src/helpers/windowManager");Module._load = originalLoad;-function fakeWindow({ visible }) {+function fakeWindow({ visible, focused = false }) {const calls = [];let isVisible = visible;+ let isFocused = focused;return {calls,window: {@@ -176,7 +188,12 @@ function fakeWindow({ visible }) {calls.push("hide");},focus: () => calls.push("focus"),- blur: () => calls.push("blur"),+ // Like Electron, blur() clears the flag even where X focus stays put.+ blur: () => {+ isFocused = false;+ calls.push("blur");+ },+ isFocused: () => isFocused,setFocusable: (value) => calls.push(`focusable:${value}`),setContentProtection: () => undefined,getBounds: () => ({ x: 0, y: 0, width: 96, height: 96 }),@@ -525,6 +542,149 @@ test("closing the assistant panel blurs only where opening focused", () => {}});+test("opening the assistant panel asks X for keyboard focus only on Linux", () => {+ for (const [platform, expected] of [+ ["linux", ["focus"]],+ ["win32", []],+ ["darwin", []],+ ]) {+ withPlatform(platform, () => {+ inputRegionRequests.length = 0;+ const { manager } = makeManager({ visible: true });+ manager.setAssistantPanelOpen(true);+ // focusable:false made the Linux pill override-redirect, so focus()+ // alone never reaches the window manager.+ assert.deepEqual(inputRegionRequests, expected, platform);+ });+ }+});++test("closing the assistant panel on Linux remaps a window that held the keyboard", () => {+ withPlatform("linux", () => {+ const { manager, calls } = makeManager({ visible: true, focused: true });+ manager.setAssistantPanelOpen(false);+ // GNOME returns focus to the previous app only when the focused+ // override-redirect window unmaps.+ assert.deepEqual(calls, ["blur", "focusable:false", "hide", "showInactive"]);+ });+ withPlatform("linux", () => {+ const { manager, calls } = makeManager({ visible: true, focused: false });+ manager.setAssistantPanelOpen(false);+ assert.deepEqual(calls, ["blur", "focusable:false"], "focus already left for another app");+ });+ for (const platform of ["win32", "darwin"]) {+ withPlatform(platform, () => {+ const { manager, calls } = makeManager({ visible: true, focused: true });+ manager.setAssistantPanelOpen(false);+ assert.ok(!calls.includes("hide"), `${platform} never remaps the pill`);+ });+ }+});++test("closing the panel during a click's focus request hands the keyboard back once it lands", async () => {+ let land;+ focusInputRegion = () =>+ new Promise((resolve) => {+ land = resolve;+ });+ try {+ const { manager, calls } = makeManager({ visible: true, focused: false });+ withPlatform("linux", () => {+ manager._assistantPanelOpen = true;+ // The user is in another app; their click on Close re-takes focus on+ // mouseDown, and the close arrives before the helper granted it.+ manager._focusLinuxMainWindow();+ manager.setAssistantPanelOpen(false);+ });+ assert.deepEqual(calls, ["blur", "focusable:false"]);+ land();+ await new Promise((resolve) => setImmediate(resolve));+ assert.deepEqual(calls, ["blur", "focusable:false", "hide", "showInactive"]);+ } finally {+ focusInputRegion = requestFocus;+ }+});++test("a panel reopened before the closing click's focus request lands keeps the keyboard", async () => {+ const lands = [];+ focusInputRegion = () => new Promise((resolve) => lands.push(resolve));+ try {+ const { manager, calls } = makeManager({ visible: true, focused: false });+ withPlatform("linux", () => {+ manager._assistantPanelOpen = true;+ manager._focusLinuxMainWindow();+ manager.setAssistantPanelOpen(false);+ manager.setAssistantPanelOpen(true);+ });+ for (const land of lands) land();+ await new Promise((resolve) => setImmediate(resolve));+ assert.deepEqual(+ calls,+ ["blur", "focusable:false", "focusable:true", "focus"],+ "the reopened panel must keep the keyboard"+ );+ } finally {+ focusInputRegion = requestFocus;+ }+});++test("a pill hidden before the closing click's focus request lands stays hidden", async () => {+ let land;+ focusInputRegion = () =>+ new Promise((resolve) => {+ land = resolve;+ });+ try {+ const { manager, calls } = makeManager({ visible: true, focused: false });+ withPlatform("linux", () => {+ manager._assistantPanelOpen = true;+ manager._focusLinuxMainWindow();+ manager.setAssistantPanelOpen(false);+ });+ // Tray Hide, a push-to-talk cancel or onboarding hides the pill meanwhile.+ manager.mainWindow.hide();+ land();+ await new Promise((resolve) => setImmediate(resolve));+ assert.deepEqual(calls, ["blur", "focusable:false", "hide"]);+ assert.equal(manager.mainWindow.isVisible(), false);+ } finally {+ focusInputRegion = requestFocus;+ }+});++test("a close waits for the latest focus request, not one an earlier request settled", async () => {+ const lands = [];+ focusInputRegion = () => new Promise((resolve) => lands.push(resolve));+ try {+ const { manager, calls } = makeManager({ visible: true, focused: false });+ withPlatform("linux", () => {+ // Opening asks for focus, then a click on Close asks again before the+ // first request has landed.+ manager.setAssistantPanelOpen(true);+ manager._focusLinuxMainWindow();+ });+ lands[0]();+ await new Promise((resolve) => setImmediate(resolve));+ withPlatform("linux", () => manager.setAssistantPanelOpen(false));+ assert.ok(!calls.includes("hide"), "the click's request is still pending");+ lands[1]();+ await new Promise((resolve) => setImmediate(resolve));+ assert.deepEqual(calls.slice(-2), ["hide", "showInactive"]);+ } finally {+ focusInputRegion = requestFocus;+ }+});++test("a focus request that already landed leaves a later close alone", async () => {+ const { manager, calls } = makeManager({ visible: true, focused: false });+ withPlatform("linux", () => manager.setAssistantPanelOpen(true));+ await new Promise((resolve) => setImmediate(resolve));+ // The user has since moved to another app, so the pill must not remap.+ withPlatform("linux", () => manager.setAssistantPanelOpen(false));+ await new Promise((resolve) => setImmediate(resolve));+ assert.ok(!calls.includes("hide"));+});+test("showDictationPanel still surfaces a hidden window while the panel is open", () => {const { manager, calls } = makeManager({ visible: false });// Panel open but the window got hidden afterwards (tray Hide raced the open).diff --git a/test/native/linuxInputRegion.c b/test/native/linuxInputRegion.cindex a39460852..5f6b8f6f7 100644--- a/test/native/linuxInputRegion.c+++ b/test/native/linuxInputRegion.c@@ -6,6 +6,7 @@#include <stdlib.h>#include <string.h>#include <sys/wait.h>+#include <time.h>#include <unistd.h>static Display *display;@@ -75,17 +76,63 @@ static void assert_rectangle(Window window, int kind, int expected_count,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)+static void send_command(const char *command){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");+}++static void apply(Window window, const char *command, int native_width, int native_height,+ int count, int x, int y, int width, int height)+{+ send_command(command);assert_rectangle(window, ShapeInput, count, x, y, width, height);assert_rectangle(window, ShapeBounding, 1, 0, 0, native_width, native_height);}+static Window focused_window(void)+{+ Window focus;+ int revert_to;+ XGetInputFocus(display, &focus, &revert_to);+ return focus;+}++static int focus_in_delivered(Window window)+{+ XEvent event;+ int delivered = 0;+ XSync(display, False);+ while (XCheckTypedWindowEvent(display, window, FocusIn, &event)) delivered = 1;+ return delivered;+}++/* Stands in for a window manager: answers every pending WM_Sn conversion. */+static void answer_selection_requests(Window owner)+{+ XEvent request;+ XSync(display, False);+ while (XCheckTypedWindowEvent(display, owner, SelectionRequest, &request)) {+ XEvent reply = { 0 };+ reply.xselection.type = SelectionNotify;+ reply.xselection.requestor = request.xselectionrequest.requestor;+ reply.xselection.selection = request.xselectionrequest.selection;+ reply.xselection.target = request.xselectionrequest.target;+ reply.xselection.property = None;+ XSendEvent(display, reply.xselection.requestor, False, 0, &reply);+ }+ XSync(display, False);+}++static long elapsed_ms(const struct timespec *since)+{+ struct timespec now;+ clock_gettime(CLOCK_MONOTONIC, &now);+ return (now.tv_sec - since->tv_sec) * 1000L + (now.tv_nsec - since->tv_nsec) / 1000000L;+}+static void assert_click_target(Window expected, int x, int y){XWarpPointer(display, None, DefaultRootWindow(display), 0, 0, 0, 0, 20 + x, 20 + y);@@ -114,7 +161,7 @@ int main(int argc, char **argv)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);+ XSelectInput(display, foreground, ButtonPressMask | FocusChangeMask);XMapWindow(display, background);XMapRaised(display, foreground);XSync(display, False);@@ -137,6 +184,39 @@ int main(int argc, char **argv)apply(foreground, "full\n", 416, 240, 1, 0, 0, 416, 240);assert_click_target(foreground, 10, 10);+ /* No window manager owns WM_S0 here, so the server answers the round trip. */+ send_command("focus\n");+ require(focused_window() == foreground, "focus did not reach the window");+ focus_in_delivered(foreground);+ send_command("focus\n");+ require(focused_window() == foreground, "focusing again lost the window");+ require(focus_in_delivered(foreground),+ "focusing a window that already has X focus sent no FocusIn");++ /* A window manager that answers after the helper gave up waiting must not+ * satisfy the next round trip with that late reply. */+ Window manager = XCreateSimpleWindow(display, root, 0, 0, 1, 1, 0, 0, 0);+ char selection_name[16];+ snprintf(selection_name, sizeof(selection_name), "WM_S%d", DefaultScreen(display));+ Atom selection = XInternAtom(display, selection_name, False);+ XSetSelectionOwner(display, selection, manager, CurrentTime);+ XSync(display, False);+ send_command("focus\n");+ answer_selection_requests(manager);+ struct timespec started;+ clock_gettime(CLOCK_MONOTONIC, &started);+ send_command("focus\n");+ require(elapsed_ms(&started) >= 50, "a late window manager reply ended a later round trip");+ answer_selection_requests(manager);+ XSetSelectionOwner(display, selection, None, CurrentTime);+ XUnmapWindow(display, foreground);+ XSync(display, False);+ send_command("focus\n");+ require(focused_window() != PointerRoot, "focusing an unmapped window dropped X focus");+ XMapRaised(display, foreground);+ XSync(display, False);+ apply(foreground, "full\n", 416, 240, 1, 0, 0, 416, 240);+require(fclose(helper_input) == 0, "cannot close helper input");int status;require(waitpid(helper_pid, &status, 0) == helper_pid, "cannot wait for helper exit");