From 36dec7034070f2fd5ede8cad355de27d9028742b Mon Sep 17 00:00:00 2001 From: Archie Cho Date: Tue, 24 Nov 2026 15:48:45 +0900 Subject: [PATCH] fix(window): defer initial tab focus until the window is activated The active tab was focused while its host window was still hidden. On Windows this races window activation (WM_ACTIVATE) against Chromium assigning the text-input focus. When the race is lost, the TSF composition window is never moved from the screen origin, so typing Korean right after launch draws the composition string at the top-left corner in addition to the caret. Clicking outside the window and back re-created the input context, which is why it looked like a focus bug. Focus the active tab on the window's first native focus event instead, and give the hidden xterm textarea an in-bounds anchor so Windows has a usable caret rect before xterm positions the textarea on cursor movement. Evidence: enumerating Win32 windows during the first real composition showed a CiceroUIWndFrame owned by the Wave UI thread at rect (0,0,53,31), while the xterm preedit overlay was measured at the caret (138,114). A build with only the textarea anchor still reproduced the window; adding the focus ordering made it disappear. --- emain/emain-window.ts | 31 +++++++++++++++++++----------- frontend/app/view/term/termwrap.ts | 7 ++++++- frontend/app/view/term/xterm.css | 7 +++++++ 3 files changed, 33 insertions(+), 12 deletions(-) diff --git a/emain/emain-window.ts b/emain/emain-window.ts index c5620ae39d..8cc0765442 100644 --- a/emain/emain-window.ts +++ b/emain/emain-window.ts @@ -516,18 +516,27 @@ export class WaveBrowserWindow extends BaseWindow { this.finalizePositioning(); } - // something is causing the new tab to lose focus so it requires manual refocusing - tabView.webContents.focus(); - setTimeout(() => { - if (tabView.webContents && this.activeTabView == tabView && !tabView.webContents.isFocused()) { - tabView.webContents.focus(); - } - }, 10); - setTimeout(() => { - if (tabView.webContents && this.activeTabView == tabView && !tabView.webContents.isFocused()) { - tabView.webContents.focus(); + const focusTab = () => { + if (this.isDestroyed() || tabView.isDestroyed || this.activeTabView !== tabView) { + return; } - }, 30); + tabView.webContents.focus(); + // The new tab sometimes loses focus immediately after switching. + const retryFocus = () => { + if (!tabView.isDestroyed && this.activeTabView === tabView && !tabView.webContents.isFocused()) { + tabView.webContents.focus(); + } + }; + setTimeout(retryFocus, 10); + setTimeout(retryFocus, 30); + }; + // Let Windows activate the HWND before Chromium assigns the initial text-input focus. + // Focusing a hidden tab leaves the TSF composition window anchored at the screen origin. + if (process.platform === "win32" && !this.isVisible()) { + this.once("focus", focusTab); + } else { + focusTab(); + } } private finalizePositioning() { diff --git a/frontend/app/view/term/termwrap.ts b/frontend/app/view/term/termwrap.ts index 956d5fc9e2..cfaa1cd794 100644 --- a/frontend/app/view/term/termwrap.ts +++ b/frontend/app/view/term/termwrap.ts @@ -17,7 +17,7 @@ import { WOS, } from "@/store/global"; import * as services from "@/store/services"; -import { PLATFORM, PlatformMacOS } from "@/util/platformutil"; +import { isWindows, PLATFORM, PlatformMacOS } from "@/util/platformutil"; import { base64ToArray, fireAndForget } from "@/util/util"; import { FitAddon } from "@xterm/addon-fit"; import { SearchAddon } from "@xterm/addon-search"; @@ -272,6 +272,11 @@ export class TermWrap { this.heldData = []; this.handleResize_debounced = debounce(50, this.handleResize.bind(this)); this.terminal.open(this.connectElem); + if (isWindows()) { + // Give Windows IME a usable anchor inside the terminal before xterm positions the + // hidden textarea on the first cursor movement. + this.terminal.textarea?.classList.add("wave-windows-ime-anchor"); + } const dragoverHandler = (e: DragEvent) => { e.preventDefault(); diff --git a/frontend/app/view/term/xterm.css b/frontend/app/view/term/xterm.css index 819654e453..e3c2df35c7 100644 --- a/frontend/app/view/term/xterm.css +++ b/frontend/app/view/term/xterm.css @@ -76,6 +76,13 @@ resize: none; } +.xterm .xterm-helper-textarea.wave-windows-ime-anchor { + /* Give Windows IME a usable anchor before xterm positions the textarea on cursor movement. */ + left: 0; + min-width: 1px; + min-height: 1px; +} + .xterm .composition-view { /* TODO: Composition position got messed up somewhere */ background: #000;