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();

Choose a reason for hiding this comment

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

P2 Badge Preserve shortcut-open focus behavior

refreshTerminal no longer focuses by default, so opening the panel now depends on an explicit requestTerminalFocus() call. The keyboard shortcut path still goes through usePanelShortcuts -> useLayoutController -> raw handleToggleTerminal (no focus request), so opening terminal via the configured shortcut leaves focus in the previous element and users cannot type until they click the terminal.

Useful? React with 👍 / 👎.

}, [focusTerminalIfRequested]);

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

useEffect(() => {
if (!isVisible || focusRequestVersion === 0) {
return;
}
pendingFocusRef.current = true;
focusTerminalIfRequested();
Comment on lines +368 to +372

Choose a reason for hiding this comment

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

P2 Badge Consume focus requests exactly once

This effect treats focusRequestVersion as a boolean (!= 0) instead of a monotonic request id, so once any focus request has ever happened, every later isVisible transition to true will set pendingFocusRef again and re-focus the terminal. That reintroduces unsolicited focus for non-focus open paths (for example programmatic opens that intentionally call plain openTerminal), because the old request is effectively replayed indefinitely rather than consumed once.

Useful? React with 👍 / 👎.

}, [focusRequestVersion, focusTerminalIfRequested, isVisible]);

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