Skip to content

armgit / cindy

Public Imported from GitHub
0

Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。

fix(terminal): preserve size while sidebar is hidden (#5201)

Tsai Yuanbb2bce608ad7
diff --git a/apps/desktop/src/renderer/features/right-sidebar/plugins/terminal/TerminalTabBody.tsx b/apps/desktop/src/renderer/features/right-sidebar/plugins/terminal/TerminalTabBody.tsx
index 8171077121..fb448f74be 100644
--- a/apps/desktop/src/renderer/features/right-sidebar/plugins/terminal/TerminalTabBody.tsx
+++ b/apps/desktop/src/renderer/features/right-sidebar/plugins/terminal/TerminalTabBody.tsx
@@ -35,6 +35,8 @@ interface Props {
state: TerminalState;
ctx: TabKindHostContext;
active?: boolean;
+ /** Keep the PTY at its last usable dimensions while the sidebar is collapsed. */
+ shellVisible?: boolean;
}
interface RuntimeError {
@@ -43,7 +45,7 @@ interface RuntimeError {
detail: string;
}
-export function TerminalTabBody({ state, ctx, active }: Props) {
+export function TerminalTabBody({ state, ctx, active, shellVisible = true }: Props) {
const { tabId, workdir, patchState } = ctx;
const { t } = useTranslation();
@@ -53,6 +55,8 @@ export function TerminalTabBody({ state, ctx, active }: Props) {
const onDataDisposerRef = useRef<{ dispose(): void } | null>(null);
/** 标记本组件实例是否还活着,异步 callback 里检查。 */
const aliveRef = useRef(true);
+ const shellVisibleRef = useRef(shellVisible);
+ shellVisibleRef.current = shellVisible;
const [runtimeError, setRuntimeError] = useState<RuntimeError | null>(null);
const [restarting, setRestarting] = useState(false);
@@ -96,8 +100,9 @@ export function TerminalTabBody({ state, ctx, active }: Props) {
patchState({ exited: ex.exit });
});
- // 首次 fit + 拿到尺寸
- fitNow(entry);
+ // 折叠侧栏时 FitAddon 会把 0 宽折成极窄尺寸;不要把它写进
+ // lastSize 或下发给 PTY,恢复可见后由 active effect 再同步。
+ if (shellVisibleRef.current) fitNow(entry);
return () => {
aliveRef.current = false;
@@ -163,7 +168,7 @@ export function TerminalTabBody({ state, ctx, active }: Props) {
if (!slot) return;
const ro = new ResizeObserver(() => {
const entry = entryRef.current;
- if (!entry || !aliveRef.current) return;
+ if (!entry || !aliveRef.current || !shellVisibleRef.current) return;
fitAndPushSize(entry, tabId);
});
ro.observe(slot);
@@ -172,16 +177,16 @@ export function TerminalTabBody({ state, ctx, active }: Props) {
// ─── 4. active 切换时 fit + focus(避免后台 layout 抖动)───
useEffect(() => {
- if (!active) return;
+ if (!active || !shellVisible) return;
const entry = entryRef.current;
if (!entry) return;
// 切到本 tab 时 fit 一下,因为后台 tab 的 layout 可能没跟上侧栏宽度变化
requestAnimationFrame(() => {
- if (!aliveRef.current || !entryRef.current) return;
+ if (!aliveRef.current || !entryRef.current || !shellVisibleRef.current) return;
fitAndPushSize(entryRef.current, tabId);
entryRef.current.terminal.focus();
});
- }, [active, tabId]);
+ }, [active, shellVisible, tabId]);
// ─── 5. Restart 按钮 handler ───
const onRestart = useCallback(async () => {
diff --git a/apps/desktop/src/renderer/features/right-sidebar/plugins/terminal/__tests__/TerminalTabBody.test.tsx b/apps/desktop/src/renderer/features/right-sidebar/plugins/terminal/__tests__/TerminalTabBody.test.tsx
new file mode 100644
index 0000000000..56471621ee
--- /dev/null
+++ b/apps/desktop/src/renderer/features/right-sidebar/plugins/terminal/__tests__/TerminalTabBody.test.tsx
@@ -0,0 +1,149 @@
+// @vitest-environment jsdom
+import { cleanup, render } from '@testing-library/react';
+import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
+
+import type { TabKindHostContext } from '../../../types';
+
+const fit = vi.fn();
+const resize = vi.fn(async () => undefined);
+const entry = {
+ terminal: {
+ cols: 80,
+ rows: 24,
+ element: undefined,
+ open: vi.fn(),
+ onData: vi.fn(() => ({ dispose: vi.fn() })),
+ write: vi.fn(),
+ focus: vi.fn(),
+ },
+ fitAddon: { fit },
+ lastSize: { cols: 80, rows: 24 },
+ ptyAttached: true,
+};
+
+vi.mock('react-i18next', () => ({
+ useTranslation: () => ({ t: (key: string) => key }),
+}));
+vi.mock('@/components/ui/spinner', () => ({ Spinner: () => null }));
+vi.mock('../lib/xtermPool', () => ({
+ getOrCreateXterm: () => entry,
+}));
+
+import { TerminalTabBody } from '../TerminalTabBody';
+
+class TestResizeObserver {
+ static callback: ResizeObserverCallback | null = null;
+ constructor(callback: ResizeObserverCallback) {
+ TestResizeObserver.callback = callback;
+ }
+ observe() {}
+ disconnect() {}
+ unobserve() {}
+}
+
+function makeContext(): TabKindHostContext {
+ return {
+ tabId: 'terminal-1',
+ sessionId: 'session-1',
+ workdir: '/workspace',
+ remoteHostId: null,
+ patchState: vi.fn(),
+ onVisibilityChange: vi.fn(),
+ setCloseInterceptor: vi.fn(() => vi.fn()),
+ };
+}
+
+beforeEach(() => {
+ fit.mockClear();
+ resize.mockClear();
+ entry.terminal.open.mockClear();
+ entry.terminal.onData.mockClear();
+ entry.terminal.cols = 80;
+ entry.terminal.rows = 24;
+ entry.lastSize = { cols: 80, rows: 24 };
+ TestResizeObserver.callback = null;
+ vi.stubGlobal('ResizeObserver', TestResizeObserver);
+ vi.stubGlobal('requestAnimationFrame', (callback: FrameRequestCallback) => {
+ callback(0);
+ return 1;
+ });
+ Object.defineProperty(window, 'electronAPI', {
+ configurable: true,
+ value: {
+ terminal: {
+ create: vi.fn(async () => ({ shellId: 'zsh', shellDisplayName: 'zsh' })),
+ resize,
+ restart: vi.fn(),
+ write: vi.fn(),
+ onData: vi.fn(() => vi.fn()),
+ onExit: vi.fn(() => vi.fn()),
+ },
+ },
+ });
+});
+
+afterEach(() => {
+ cleanup();
+ vi.unstubAllGlobals();
+});
+
+describe('TerminalTabBody sidebar visibility', () => {
+ it('does not fit or resize the PTY while the sidebar is collapsed, then refits when visible', () => {
+ const ctx = makeContext();
+ const view = render(
+ <TerminalTabBody
+ state={{ created: true, exited: null, title: '', shellId: '', shellDisplayName: '' }}
+ ctx={ctx}
+ active
+ shellVisible={false}
+ />,
+ );
+
+ TestResizeObserver.callback?.([], {} as ResizeObserver);
+ expect(fit).not.toHaveBeenCalled();
+ expect(resize).not.toHaveBeenCalled();
+
+ entry.terminal.cols = 100;
+ view.rerender(
+ <TerminalTabBody
+ state={{ created: true, exited: null, title: '', shellId: '', shellDisplayName: '' }}
+ ctx={ctx}
+ active
+ shellVisible
+ />,
+ );
+
+ expect(fit).toHaveBeenCalled();
+ expect(resize).toHaveBeenCalledWith('terminal-1', 100, 24);
+ });
+
+ it('drops an already queued fit when the sidebar collapses before its animation frame', () => {
+ let frame: FrameRequestCallback | undefined;
+ vi.stubGlobal('requestAnimationFrame', (callback: FrameRequestCallback) => {
+ frame = callback;
+ return 1;
+ });
+ const ctx = makeContext();
+ const view = render(
+ <TerminalTabBody
+ state={{ created: true, exited: null, title: '', shellId: '', shellDisplayName: '' }}
+ ctx={ctx}
+ active
+ shellVisible
+ />,
+ );
+
+ entry.terminal.cols = 2;
+ view.rerender(
+ <TerminalTabBody
+ state={{ created: true, exited: null, title: '', shellId: '', shellDisplayName: '' }}
+ ctx={ctx}
+ active
+ shellVisible={false}
+ />,
+ );
+ frame?.(0);
+
+ expect(resize).not.toHaveBeenCalled();
+ });
+});