Skip to content

Commit 2a0f7f0

Browse files
committed
refactor(ui): 优化 ThemeableStatic 组件重渲染逻辑
- 移除 ThemeableStatic 的 resetKey 属性,避免不必要的重新挂载 - 改用 welcomeNonce 仅触发 PromptInput 等组件重渲染 - 更新 colors-theme 中 codeBlock 和 table 的颜色样式 - MessageView 新增代码段背景渲染支持,提升代码块显示效果 - ThemeableStatic 内部优化渲染函数和 props 比较逻辑,提升性能表现
1 parent 5687fb8 commit 2a0f7f0

4 files changed

Lines changed: 16 additions & 17 deletions

File tree

src/ui/components/MessageView/index.tsx

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,13 @@ export function MessageView({ message, collapsed, width = 80 }: MessageViewProps
8282
</Box>
8383
);
8484
}
85+
if (seg.kind === "code") {
86+
return (
87+
<Box key={i} backgroundColor={theme.codeBlock.background} paddingLeft={1}>
88+
<Text>{seg.body}</Text>
89+
</Box>
90+
);
91+
}
8592
return <Text key={i}>{seg.body}</Text>;
8693
})
8794
: null}

src/ui/components/ThemeableStatic/index.tsx

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,23 +21,14 @@ type Props<T> = {
2121
const ThemeableStaticInner = function ThemeableStaticInner<T>({
2222
items,
2323
themeVersion: _themeVersion,
24-
resetKey,
2524
children: render,
2625
}: Props<T>): React.ReactElement {
27-
// resetKey 用于 /new 等需要重新挂载的场景
28-
// themeVersion 不参与 key,切换主题时历史消息保持原样,新消息用新主题
29-
const key = `${resetKey ?? 0}`;
30-
3126
const wrappedRender = useMemo(() => (item: T, index: number) => render(item, index), [render]);
32-
return (
33-
<Static key={key} items={items}>
34-
{wrappedRender}
35-
</Static>
36-
);
27+
return <Static items={items}>{wrappedRender}</Static>;
3728
};
3829

3930
function propsAreEqual(prev: Props<unknown>, next: Props<unknown>): boolean {
40-
return prev.items === next.items && prev.themeVersion === next.themeVersion && prev.resetKey === next.resetKey;
31+
return prev.items === next.items && prev.themeVersion === next.themeVersion;
4132
}
4233

4334
export default React.memo(ThemeableStaticInner, propsAreEqual) as typeof ThemeableStaticInner;

src/ui/theme/colors-theme.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -152,10 +152,10 @@ export function buildThemeTokens(
152152
border: dimHex(fg, 0.7),
153153
},
154154
codeBlock: {
155-
foreground: fg,
156-
background: mode === "dark" ? dimHex(c.Background, 0.15) : dimHex(c.Background, 0.05),
155+
foreground: inv,
156+
background: dimHex(fg, 0.7),
157157
border: dimHex(fg, 0.7),
158-
title: fg,
158+
title: inv,
159159
lineNumber: c.Gray,
160160
highlight: mode === "dark" ? "#2d333b" : "#fff8c5",
161161
},
@@ -187,7 +187,7 @@ export function buildThemeTokens(
187187
unchecked: dimHex(fg, 0.7),
188188
},
189189
table: {
190-
border: dimHex(fg, 0.7),
190+
border: dimHex(c.AccentYellowDim, 0.7),
191191
headerForeground: fg,
192192
headerBackground: dimHex(c.Background, 0.08),
193193
cellForeground: fg,

src/ui/views/App.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -630,7 +630,8 @@ function App({ projectRoot, initialPrompt, onRestart }: AppProps): React.ReactEl
630630

631631
// Don't clear the screen on resize — Ink handles re-layout naturally.
632632
// Clearing causes scroll-to-top and flash, especially on tab switch in iTerm2.
633-
// Just force ThemeableStatic to remount so Ink recalculates row heights.
633+
// Use welcomeNonce to force PromptInput and other components to re-render,
634+
// but don't pass it to ThemeableStatic (see layoutResetKey below).
634635
setWelcomeNonce((n) => n + 1);
635636
// eslint-disable-next-line react-hooks/exhaustive-deps
636637
}, [busy, mode, sessionManager, columns]);
@@ -736,7 +737,7 @@ function App({ projectRoot, initialPrompt, onRestart }: AppProps): React.ReactEl
736737

737738
return (
738739
<Box flexDirection="column" width={screenWidth} minWidth={80} overflowX={"visible"}>
739-
<ThemeableStatic items={staticItems} themeVersion={themeVersion} resetKey={welcomeNonce}>
740+
<ThemeableStatic items={staticItems} themeVersion={themeVersion}>
740741
{(item) => {
741742
if (item.id.startsWith("__welcome__")) {
742743
return (

0 commit comments

Comments
 (0)