Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 28 additions & 4 deletions src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1013,6 +1013,7 @@ function MainApp() {
terminalState,
ensureTerminalWithTitle,
restartTerminalSession,
requestTerminalFocus,
} = useTerminalController({
activeWorkspaceId,
activeWorkspace,
Expand All @@ -1026,10 +1027,33 @@ function MainApp() {
[ensureTerminalWithTitle],
);

const openTerminalWithFocus = useCallback(() => {
if (!activeWorkspaceId) {
return;
}
requestTerminalFocus();
openTerminal();
}, [activeWorkspaceId, openTerminal, requestTerminalFocus]);

const handleToggleTerminalWithFocus = useCallback(() => {
if (!activeWorkspaceId) {
return;
}
if (!terminalOpen) {
requestTerminalFocus();
}
handleToggleTerminal();
}, [
activeWorkspaceId,
handleToggleTerminal,
requestTerminalFocus,
terminalOpen,
]);

const launchScriptState = useWorkspaceLaunchScript({
activeWorkspace,
updateWorkspaceSettings,
openTerminal,
openTerminal: openTerminalWithFocus,
ensureLaunchTerminal,
restartLaunchSession: restartTerminalSession,
terminalState,
Expand All @@ -1039,7 +1063,7 @@ function MainApp() {
const launchScriptsState = useWorkspaceLaunchScripts({
activeWorkspace,
updateWorkspaceSettings,
openTerminal,
openTerminal: openTerminalWithFocus,
ensureLaunchTerminal: (workspaceId, entry, title) => {
const label = entry.label?.trim() || entry.icon;
return ensureTerminalWithTitle(
Expand Down Expand Up @@ -1882,7 +1906,7 @@ function MainApp() {
onCycleAgent: handleCycleAgent,
onCycleWorkspace: handleCycleWorkspace,
onToggleDebug: handleDebugClick,
onToggleTerminal: handleToggleTerminal,
onToggleTerminal: handleToggleTerminalWithFocus,
sidebarCollapsed,
rightPanelCollapsed,
onExpandSidebar: expandSidebar,
Expand Down Expand Up @@ -2032,7 +2056,7 @@ function MainApp() {
handleCheckoutPullRequest(pullRequest.number),
onCreateBranch: handleCreateBranch,
onCopyThread: handleCopyThread,
onToggleTerminal: handleToggleTerminal,
onToggleTerminal: handleToggleTerminalWithFocus,
showTerminalButton: !isCompact,
showWorkspaceTools: !isCompact,
launchScript: launchScriptState.launchScript,
Expand Down
14 changes: 11 additions & 3 deletions src/features/terminal/hooks/useTerminalController.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useCallback, useEffect, useRef } from "react";
import { useCallback, useEffect, useRef, useState } from "react";
import type { DebugEntry, WorkspaceInfo } from "../../../types";
import { closeTerminalSession } from "../../../services/tauri";
import { buildErrorDebugEntry } from "../../../utils/debugEntries";
Expand All @@ -23,6 +23,10 @@ export function useTerminalController({
const cleanupTerminalRef = useRef<((workspaceId: string, terminalId: string) => void) | null>(
null,
);
const [focusRequestVersion, setFocusRequestVersion] = useState(0);
const requestTerminalFocus = useCallback(() => {
setFocusRequestVersion((prev) => prev + 1);
}, []);
const shouldIgnoreTerminalCloseError = useCallback((error: unknown) => {
const message = error instanceof Error ? error.message : String(error);
return message.includes("Terminal session not found");
Expand Down Expand Up @@ -66,6 +70,7 @@ export function useTerminalController({
activeWorkspace,
activeTerminalId,
isVisible: terminalOpen,
focusRequestVersion,
onDebug,
onSessionExit: (workspaceId, terminalId) => {
const shouldClosePanel =
Expand All @@ -88,17 +93,19 @@ export function useTerminalController({
if (!activeWorkspaceId) {
return;
}
requestTerminalFocus();
setActiveTerminal(activeWorkspaceId, terminalId);
},
[activeWorkspaceId, setActiveTerminal],
[activeWorkspaceId, requestTerminalFocus, setActiveTerminal],
);

const onNewTerminal = useCallback(() => {
if (!activeWorkspaceId) {
return;
}
requestTerminalFocus();
createTerminal(activeWorkspaceId);
}, [activeWorkspaceId, createTerminal]);
}, [activeWorkspaceId, createTerminal, requestTerminalFocus]);

const onCloseTerminal = useCallback(
(terminalId: string) => {
Expand Down Expand Up @@ -139,5 +146,6 @@ export function useTerminalController({
terminalState,
ensureTerminalWithTitle,
restartTerminalSession,
requestTerminalFocus,
};
}
23 changes: 21 additions & 2 deletions src/features/terminal/hooks/useTerminalSession.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ type UseTerminalSessionOptions = {
activeWorkspace: WorkspaceInfo | null;
activeTerminalId: string | null;
isVisible: boolean;
focusRequestVersion: number;
onDebug?: (entry: DebugEntry) => void;
onSessionExit?: (workspaceId: string, terminalId: string) => void;
};
Expand Down Expand Up @@ -114,6 +115,7 @@ export function useTerminalSession({
activeWorkspace,
activeTerminalId,
isVisible,
focusRequestVersion,
onDebug,
onSessionExit,
}: UseTerminalSessionOptions): TerminalSessionState {
Expand All @@ -127,6 +129,7 @@ export function useTerminalSession({
const renderedKeyRef = useRef<string | null>(null);
const activeWorkspaceRef = useRef<WorkspaceInfo | null>(null);
const activeTerminalIdRef = useRef<string | null>(null);
const pendingFocusRef = useRef(false);
const [status, setStatus] = useState<TerminalStatus>("idle");
const [message, setMessage] = useState("Open a terminal to start a session.");
const [hasSession, setHasSession] = useState(false);
Expand Down Expand Up @@ -165,15 +168,23 @@ export function useTerminalSession({
terminalRef.current?.write(data);
}, []);

const focusTerminalIfRequested = useCallback(() => {
if (!pendingFocusRef.current) {
return;
}
pendingFocusRef.current = false;
terminalRef.current?.focus();
}, []);

const refreshTerminal = useCallback(() => {
const terminal = terminalRef.current;
if (!terminal) {
return;
}
const lastRow = Math.max(0, terminal.rows - 1);
terminal.refresh(0, lastRow);
terminal.focus();
}, []);
focusTerminalIfRequested();
}, [focusTerminalIfRequested]);

const syncActiveBuffer = useCallback(
(key: string) => {
Expand Down Expand Up @@ -353,6 +364,14 @@ export function useTerminalSession({
sessionResetCounter,
]);

useEffect(() => {
if (!isVisible || focusRequestVersion === 0) {
return;
Comment on lines +368 to +369

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Request focus when opening terminal via keyboard shortcut

This new guard means terminal focus only happens after requestTerminalFocus() increments focusRequestVersion, but the keyboard shortcut path still toggles the panel through usePanelShortcuts with the raw handleToggleTerminal callback (src/features/app/hooks/useLayoutController.ts:64-69) and never sends that focus request. After this change, opening the terminal via the configured shortcut can leave input focus outside xterm (especially on Windows), so typing does nothing until the user manually clicks the terminal.

Useful? React with 👍 / 👎.

}
pendingFocusRef.current = true;
focusTerminalIfRequested();
}, [focusRequestVersion, focusTerminalIfRequested, isVisible]);

useEffect(() => {
if (!isVisible || !activeKey || !terminalRef.current || !fitAddonRef.current) {
return;
Expand Down