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;