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(mobile): 稳定 Android 模型选择浮窗键盘缩放 (#305)

qsoyqdaabc07c0ddc
diff --git a/apps/mobile/src/__tests__/modelPickerSheetResize.test.ts b/apps/mobile/src/__tests__/modelPickerSheetResize.test.ts
new file mode 100644
index 0000000000..7fc127ef3f
--- /dev/null
+++ b/apps/mobile/src/__tests__/modelPickerSheetResize.test.ts
@@ -0,0 +1,23 @@
+import { readFileSync } from 'node:fs';
+import { resolve } from 'node:path';
+import { describe, expect, it } from 'vitest';
+
+const readTextLf = (...args: Parameters<typeof readFileSync>): string =>
+ String(readFileSync(...args)).replace(/\r\n/g, '\n');
+
+describe('ModelPickerSheet window resize stability', () => {
+ it('does not reset the open sheet when Android adjustResize changes window height', () => {
+ const source = readTextLf(
+ resolve(process.cwd(), 'src/session/ModelPickerSheet.tsx'),
+ 'utf8',
+ );
+ const resetEffect = source.match(
+ /\/\/ 每次重新打开重置[\s\S]*?useEffect\(\(\) => \{[\s\S]*?\}\s*,\s*\[([^\]]*)\]\);/,
+ );
+
+ expect(resetEffect).not.toBeNull();
+ expect(resetEffect?.[1]).not.toMatch(/\bwindowHeight\b/);
+ expect(source).toContain('windowHeightRef.current = windowHeight;');
+ expect(source).toContain('secondaryTranslate.setValue(windowHeightRef.current);');
+ });
+});
diff --git a/apps/mobile/src/session/ModelPickerSheet.tsx b/apps/mobile/src/session/ModelPickerSheet.tsx
index c27bac4cfb..84d9146916 100644
--- a/apps/mobile/src/session/ModelPickerSheet.tsx
+++ b/apps/mobile/src/session/ModelPickerSheet.tsx
@@ -159,6 +159,14 @@ export function ModelPickerSheet({
const secondaryTranslate = useRef(new Animated.Value(windowHeight)).current;
const secondaryAnimatingRef = useRef(false);
+ // Android adjustResize 会在键盘开合时改变 windowHeight。重置 effect 只应由一次新的
+ // visible 周期触发;否则浮窗打开期间的缩窗会把二级视图、snap 和搜索状态全部重置,
+ // 并与 SheetSurface 对新高度的正常吸附动画叠加。最新高度只供下次打开时初始化位移。
+ const windowHeightRef = useRef(windowHeight);
+ useEffect(() => {
+ windowHeightRef.current = windowHeight;
+ }, [windowHeight]);
+
// 每次重新打开重置(view 回一级、snap 回 half、清搜索、允许再次自动滚到选中行)。
useEffect(() => {
if (!visible) return;
@@ -167,9 +175,9 @@ export function ModelPickerSheet({
setSecondarySnap('half');
setQuery('');
didAutoScrollRef.current = false;
- secondaryTranslate.setValue(windowHeight);
+ secondaryTranslate.setValue(windowHeightRef.current);
secondaryAnimatingRef.current = false;
- }, [visible, secondaryTranslate, windowHeight]);
+ }, [visible, secondaryTranslate]);
const heights = useMemo(
() => computeContextSheetSnapHeights({ safeAreaTopInset: insets.top, screenHeight: windowHeight }),