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): 展开输入卡时语音按钮回到工具排 (#3053)

Dash7ef58ade27b4
diff --git a/apps/mobile/src/__tests__/composerVoiceButtonAnchor.test.ts b/apps/mobile/src/__tests__/composerVoiceButtonAnchor.test.ts
new file mode 100644
index 0000000000..bff0b2f87d
--- /dev/null
+++ b/apps/mobile/src/__tests__/composerVoiceButtonAnchor.test.ts
@@ -0,0 +1,82 @@
+import { describe, expect, it } from 'vitest';
+import { spacing } from '@/theme/tokens';
+import {
+ MOBILE_COMPOSER_CONTROL_SIZE,
+ MOBILE_COMPOSER_TOOL_GAP,
+ MOBILE_COMPOSER_VOICE_ANCHOR_CARD_BOTTOM,
+ MOBILE_COMPOSER_VOICE_ANCHOR_RIGHT,
+ resolveMobileComposerVoiceButtonAnchorStyle,
+ resolveMobileComposerVoiceButtonPlacement,
+} from '@/session/composerVoiceButtonAnchor';
+
+describe('resolveMobileComposerVoiceButtonPlacement', () => {
+ it('行尾有 trailing 时向左让位,否则贴行尾', () => {
+ expect(resolveMobileComposerVoiceButtonPlacement({ hasTrailingAction: true })).toEqual({
+ floating: true,
+ inline: false,
+ });
+ expect(resolveMobileComposerVoiceButtonPlacement({ hasTrailingAction: false })).toEqual({
+ floating: false,
+ inline: true,
+ });
+ });
+});
+
+describe('resolveMobileComposerVoiceButtonAnchorStyle', () => {
+ it('收起态按行垂直居中,不写 bottom 数值', () => {
+ const style = resolveMobileComposerVoiceButtonAnchorStyle({
+ cardLayout: false,
+ floating: false,
+ });
+ expect(style).toEqual({
+ position: 'absolute',
+ right: MOBILE_COMPOSER_VOICE_ANCHOR_RIGHT,
+ top: '50%',
+ bottom: 'auto',
+ transform: [{ translateY: -MOBILE_COMPOSER_CONTROL_SIZE / 2 }],
+ zIndex: 2,
+ });
+ expect(style.bottom).toBe('auto');
+ expect(style.top).not.toBe('auto');
+ });
+
+ it('卡片态钉在工具排底边,显式清掉 top 与 translateY', () => {
+ const style = resolveMobileComposerVoiceButtonAnchorStyle({
+ cardLayout: true,
+ floating: false,
+ });
+ expect(style).toEqual({
+ position: 'absolute',
+ right: MOBILE_COMPOSER_VOICE_ANCHOR_RIGHT,
+ top: 'auto',
+ bottom: MOBILE_COMPOSER_VOICE_ANCHOR_CARD_BOTTOM,
+ transform: [],
+ zIndex: 2,
+ });
+ expect(style.top).toBe('auto');
+ expect(style.transform).toEqual([]);
+ expect(Object.prototype.hasOwnProperty.call(style, 'top')).toBe(true);
+ expect(Object.prototype.hasOwnProperty.call(style, 'bottom')).toBe(true);
+ expect(Object.prototype.hasOwnProperty.call(style, 'transform')).toBe(true);
+ });
+
+ it('有 trailing 时两态都向左让出发送键宽度', () => {
+ const inset = MOBILE_COMPOSER_VOICE_ANCHOR_RIGHT
+ + MOBILE_COMPOSER_CONTROL_SIZE
+ + MOBILE_COMPOSER_TOOL_GAP;
+ expect(resolveMobileComposerVoiceButtonAnchorStyle({
+ cardLayout: false,
+ floating: true,
+ }).right).toBe(inset);
+ expect(resolveMobileComposerVoiceButtonAnchorStyle({
+ cardLayout: true,
+ floating: true,
+ }).right).toBe(inset);
+ expect(inset).toBe(spacing.md + 34 + 6);
+ });
+
+ it('卡片底边与间距 token 同源,避免再写 8 与 paddingBottom 对不齐', () => {
+ expect(MOBILE_COMPOSER_VOICE_ANCHOR_CARD_BOTTOM).toBe(spacing.sm);
+ expect(MOBILE_COMPOSER_VOICE_ANCHOR_RIGHT).toBe(spacing.md);
+ });
+});
diff --git a/apps/mobile/src/__tests__/newSession.test.ts b/apps/mobile/src/__tests__/newSession.test.ts
index a2db09cdac..433277a46d 100644
--- a/apps/mobile/src/__tests__/newSession.test.ts
+++ b/apps/mobile/src/__tests__/newSession.test.ts
@@ -1786,10 +1786,11 @@ describe('new session composer surface', () => {
expect(newSource).toContain('floatingVoiceButton={voiceUiAvailable ? renderComposerVoiceButton : undefined}');
expect(sessionSource).toContain('voicePlacement={composerVoicePlacement}');
expect(sharedSource).toContain('export const MOBILE_COMPOSER_INPUT_MAX_VISIBLE_LINES = 12;');
- expect(sharedSource).toContain('export const MOBILE_COMPOSER_CONTROL_SIZE = 34;');
- expect(sharedSource).toContain('export function resolveMobileComposerVoiceButtonPlacement');
+ expect(sharedSource).toContain('MOBILE_COMPOSER_CONTROL_SIZE,');
+ expect(sharedSource).toContain('resolveMobileComposerVoiceButtonPlacement,');
+ expect(sharedSource).toContain('resolveMobileComposerVoiceButtonAnchorStyle({');
expect(sharedSource).toContain('voicePlacement?.inline || voicePlacement?.floating');
- expect(sharedSource).toContain('styles.voiceButtonAnchor,');
+ expect(sharedSource).not.toContain('styles.voiceButtonAnchor');
expect(newSource).not.toContain('messageInput: {');
expect(newSource).not.toContain('composerToolbar: {');
expect(newSource).not.toContain('permissionIcon: {');
diff --git a/apps/mobile/src/__tests__/sessionComposerDesktopFirst.test.ts b/apps/mobile/src/__tests__/sessionComposerDesktopFirst.test.ts
index 6b9a7b4bbd..a6f32fad4e 100644
--- a/apps/mobile/src/__tests__/sessionComposerDesktopFirst.test.ts
+++ b/apps/mobile/src/__tests__/sessionComposerDesktopFirst.test.ts
@@ -66,14 +66,11 @@ describe('mobile session composer desktop-first surface', () => {
const inlineButtonStart = source.indexOf('composerInlineToolButton: {');
const inlineButtonEnd = source.indexOf('composerToolButtonActive:', inlineButtonStart);
const inlineButtonStyle = source.slice(inlineButtonStart, inlineButtonEnd);
- const floatingButtonStart = sharedSource.indexOf('voiceButtonAnchor: {', sharedStyleStart);
- const floatingButtonEnd = sharedSource.indexOf('voiceButtonAnchorWithTrailing:', floatingButtonStart);
- const floatingButtonStyle = sharedSource.slice(floatingButtonStart, floatingButtonEnd);
const sendButtonStart = source.indexOf('sendButton: {');
const sendButtonEnd = source.indexOf('sendButtonInactive:', sendButtonStart);
const sendButtonStyle = source.slice(sendButtonStart, sendButtonEnd);
const inputStart = sharedSource.indexOf('input: {', sharedStyleStart);
- const inputEnd = sharedSource.indexOf('voiceButtonAnchor:', inputStart);
+ const inputEnd = sharedSource.indexOf('resizeGrabberTouch:', inputStart);
const inputStyle = sharedSource.slice(inputStart, inputEnd);
const composerStyleStart = source.indexOf('composer: {');
const composerStyleEnd = source.indexOf('composerScroll:', composerStyleStart);
@@ -240,8 +237,11 @@ describe('mobile session composer desktop-first surface', () => {
expect(inputStyle).toContain('paddingHorizontal: COMPOSER_TEXT_HORIZONTAL_PADDING');
expect(inputStyle).toContain('paddingTop: COMPOSER_TEXT_PADDING_TOP');
expect(inputStyle).toContain("textAlignVertical: 'top'");
- expect(floatingButtonStyle).toContain("top: '50%'");
- expect(floatingButtonStyle).toContain('translateY: -MOBILE_COMPOSER_CONTROL_SIZE / 2');
+ expect(sharedSource).toContain('resolveMobileComposerVoiceButtonAnchorStyle({');
+ expect(sharedSource).toContain('cardLayout,');
+ expect(sharedSource).toContain('floating: voicePlacement.floating,');
+ expect(sharedSource).not.toContain('styles.voiceButtonAnchor');
+ expect(sharedSource).not.toContain('voiceButtonAnchorCard');
// Composer input is a single stable, always-multiline, always-inline instance (no compact↔expanded
// swap that remounts the native input) so the first tap reliably opens the keyboard — guards the
// "two taps to focus" regression. Compact rest look kept via minHeight (no fixed height that clips).
@@ -473,9 +473,9 @@ describe('mobile session composer desktop-first surface', () => {
expect(composerInputSource).not.toContain('floatingVoiceButtonStyle=');
expect(composerInputSource).toContain('voicePlacement={composerVoicePlacement}');
expect(sharedSource).toContain('voicePlacement?.inline || voicePlacement?.floating');
- expect(sharedSource).toContain('styles.voiceButtonAnchor,');
- expect(sharedSource).toContain('voicePlacement.floating && styles.voiceButtonAnchorWithTrailing,');
- expect(sharedSource).toContain('cardLayout && styles.voiceButtonAnchorCard,');
+ expect(sharedSource).toContain('resolveMobileComposerVoiceButtonAnchorStyle({');
+ expect(sharedSource).toContain('floating: voicePlacement.floating,');
+ expect(sharedSource).not.toContain('cardLayout && styles.voiceButtonAnchorCard,');
// 录音中语音按钮以「红色停止方块」可见,禁止任何 opacity:0 隐藏样式回归
// (旧 gestureAnchor 设计曾把听写中的按钮隐藏,会让停止录音无可见控件)。
expect(source).not.toContain('composerInlineToolButtonGestureAnchor');
@@ -512,12 +512,8 @@ describe('mobile session composer desktop-first surface', () => {
expect(inlineButtonStyle).not.toContain('width: 36');
expect(inlineButtonStyle).not.toContain('height: 42');
expect(inlineButtonStyle).not.toContain('width: 42');
- expect(floatingButtonStyle).toContain("position: 'absolute'");
- expect(floatingButtonStyle).toContain('right: MOBILE_COMPOSER_VOICE_ANCHOR_RIGHT');
- expect(floatingButtonStyle).toContain("top: '50%'");
- expect(floatingButtonStyle).toContain('translateY: -MOBILE_COMPOSER_CONTROL_SIZE / 2');
- expect(floatingButtonStyle).toContain('zIndex: 2');
- expect(sharedSource).toContain('right: spacing.md + MOBILE_COMPOSER_CONTROL_SIZE + MOBILE_COMPOSER_TOOL_GAP');
+ expect(sharedSource).toContain("from '@/session/composerVoiceButtonAnchor'");
+ expect(sharedSource).toContain('MOBILE_COMPOSER_VOICE_ANCHOR_CARD_BOTTOM');
expect(sendButtonStyle).toContain('height: 34');
expect(sendButtonStyle).toContain('width: 34');
expect(sendButtonStyle).not.toContain('height: 36');
diff --git a/apps/mobile/src/session/MobileComposerInputRow.tsx b/apps/mobile/src/session/MobileComposerInputRow.tsx
index e329728c02..45326d9c42 100644
--- a/apps/mobile/src/session/MobileComposerInputRow.tsx
+++ b/apps/mobile/src/session/MobileComposerInputRow.tsx
@@ -1,7 +1,6 @@
import {
Animated,
Easing,
- Platform,
StyleSheet,
View,
type GestureResponderHandlers,
@@ -32,6 +31,23 @@ import {
COMPOSER_TEXT_PADDING_BOTTOM,
COMPOSER_TEXT_PADDING_TOP,
} from '@/session/composerTextPlatformMetrics';
+import {
+ MOBILE_COMPOSER_CONTROL_SIZE,
+ MOBILE_COMPOSER_TOOL_GAP,
+ MOBILE_COMPOSER_VOICE_ANCHOR_CARD_BOTTOM,
+ resolveMobileComposerVoiceButtonAnchorStyle,
+ type MobileComposerVoiceButtonPlacement,
+} from '@/session/composerVoiceButtonAnchor';
+
+export {
+ MOBILE_COMPOSER_CONTROL_SIZE,
+ MOBILE_COMPOSER_TOOL_GAP,
+ MOBILE_COMPOSER_VOICE_ANCHOR_CARD_BOTTOM,
+ MOBILE_COMPOSER_VOICE_ANCHOR_RIGHT,
+ resolveMobileComposerVoiceButtonAnchorStyle,
+ resolveMobileComposerVoiceButtonPlacement,
+ type MobileComposerVoiceButtonPlacement,
+} from '@/session/composerVoiceButtonAnchor';
/**
* Composer 草稿文本的排版档。正本在 `composerTextMetrics`——原生输入框、WebView 富文本
@@ -50,8 +66,6 @@ export const MOBILE_COMPOSER_INPUT_SINGLE_LINE_HEIGHT = COMPOSER_SINGLE_LINE_HEI
export const MOBILE_COMPOSER_INPUT_MAX_VISIBLE_LINES = 12;
export const MOBILE_COMPOSER_INPUT_MAX_HEIGHT = (MOBILE_COMPOSER_INPUT_LINE_HEIGHT * MOBILE_COMPOSER_INPUT_MAX_VISIBLE_LINES)
+ (MOBILE_COMPOSER_INPUT_VERTICAL_PADDING * 2);
-export const MOBILE_COMPOSER_CONTROL_SIZE = 34;
-export const MOBILE_COMPOSER_TOOL_GAP = 6;
/**
* 触控目标下限(mobile-design-guide「主操作命中区 ≥ 44×44」,iOS HIG 同值)。
* 语音听写期间「点输入区停止听写」的命中层用它撑起 inputFrame(见 inputFrameMinHeight)。
@@ -59,29 +73,6 @@ export const MOBILE_COMPOSER_TOOL_GAP = 6;
export const MOBILE_COMPOSER_MIN_TOUCH_TARGET = 44;
const isExpoGo =
Constants.executionEnvironment === ExecutionEnvironment.StoreClient;
-/** 语音按钮 absolute 锚点距 composer 内容区右缘的距离(voiceButtonAnchor.right);
- * 消息列表的「跳到底部」浮标按同一常量推导麦克风所在列,保持两者圆心同列。 */
-export const MOBILE_COMPOSER_VOICE_ANCHOR_RIGHT = spacing.md;
-
-export interface MobileComposerVoiceButtonPlacement {
- floating: boolean;
- inline: boolean;
-}
-
-/**
- * 语音按钮的位置分配:行尾有发送 / 创建 / 停止等 trailing 按钮时向左让位
- * (floating)、否则贴行尾(inline)。判定看「是否有 trailing 按钮」而非
- * 「是否有文字」——附件-only(无文字但发送按钮可见)时同样需要让位。
- * 听写中按钮不隐藏——对齐桌面版设计,同一颗按钮录音中变为「停止录音」形态。
- */
-export function resolveMobileComposerVoiceButtonPlacement(input: {
- hasTrailingAction: boolean;
-}): MobileComposerVoiceButtonPlacement {
- return {
- floating: input.hasTrailingAction,
- inline: !input.hasTrailingAction,
- };
-}
export interface MobileComposerInputRowProps {
accessibilityHint?: string;
@@ -104,9 +95,10 @@ export interface MobileComposerInputRowProps {
editable?: boolean;
/**
* 语音按钮 render。语音按钮是简洁态与卡片态都存在的常驻控件,
- * 由组件用 absolute 锚点渲染为同一实例:简洁态贴输入行右侧、
- * 卡片态落在底部工具排右二(工具排里放 ComposerToolbarVoiceSlot 占位),
- * 两态切换时 LayoutAnimation 对同一实例的位置做平滑插值,不闪不跳。
+ * 由组件用一份完整 absolute 样式渲染为同一实例:简洁态贴输入行右侧、
+ * 卡片态落在底部工具排右二(工具排里放 ComposerToolbarVoiceSlot 占位)。
+ * 定位走 resolveMobileComposerVoiceButtonAnchorStyle,两态都写全 top /
+ * bottom / transform,避免 RN 合并残留把麦克风停在卡片中部。
*/
floatingVoiceButton?: (style: StyleProp<ViewStyle>) => ReactNode;
floatingVoiceButtonStyle?: StyleProp<ViewStyle>;
@@ -331,9 +323,10 @@ export function MobileComposerInputRow({
) : null}
{voicePlacement?.inline || voicePlacement?.floating
? floatingVoiceButton?.([
- styles.voiceButtonAnchor,
- voicePlacement.floating && styles.voiceButtonAnchorWithTrailing,
- cardLayout && styles.voiceButtonAnchorCard,
+ resolveMobileComposerVoiceButtonAnchorStyle({
+ cardLayout,
+ floating: voicePlacement.floating,
+ }),
floatingVoiceButtonStyle,
])
: null}
@@ -534,7 +527,7 @@ const makeMobileComposerInputRowStyles = (colors: ThemeColors) => ({
// (grabber 横条距顶约 8pt、距输入内容约 14pt,参考 Cursor 移动端)。
rowCard: {
borderRadius: radius.control,
- paddingBottom: 8,
+ paddingBottom: MOBILE_COMPOSER_VOICE_ANCHOR_CARD_BOTTOM,
paddingTop: 26,
},
// 水平输入行:简洁态装 [输入][发送],card 态只剩全宽输入区;
@@ -607,26 +600,6 @@ const makeMobileComposerInputRowStyles = (colors: ThemeColors) => ({
paddingTop: COMPOSER_TEXT_GEOMETRIC_PADDING_TOP,
textAlignVertical: 'center',
},
- // 语音按钮的 absolute 锚点:简洁态贴输入行右侧垂直居中;
- // 有发送按钮时向左让位;卡片态下沉到底部工具排的占位上。
- voiceButtonAnchor: {
- // 收起态按行垂直居中,与 34pt 加号 / 文字共中线;
- // 不再钉在底部,避免行高变化后麦克风掉下去。
- bottom: undefined,
- position: 'absolute',
- right: MOBILE_COMPOSER_VOICE_ANCHOR_RIGHT,
- top: '50%',
- transform: [{ translateY: -MOBILE_COMPOSER_CONTROL_SIZE / 2 }],
- zIndex: 2,
- },
- voiceButtonAnchorWithTrailing: {
- right: spacing.md + MOBILE_COMPOSER_CONTROL_SIZE + MOBILE_COMPOSER_TOOL_GAP,
- },
- voiceButtonAnchorCard: {
- bottom: 8,
- top: undefined,
- transform: [],
- },
// 外层横跨全行但 box-none 穿透触摸,只有中间的窄命中条接手势,
// 避免与左右两侧按钮的 hitSlop 抢触摸。
// 命中区拉满卡片顶部整行(与 Context 面板拖动区同手感):高度对齐 rowCard 的
diff --git a/apps/mobile/src/session/composerVoiceButtonAnchor.ts b/apps/mobile/src/session/composerVoiceButtonAnchor.ts
new file mode 100644
index 0000000000..c661b2169a
--- /dev/null
+++ b/apps/mobile/src/session/composerVoiceButtonAnchor.ts
@@ -0,0 +1,73 @@
+import { spacing } from '@/theme/tokens';
+
+export const MOBILE_COMPOSER_CONTROL_SIZE = 34;
+export const MOBILE_COMPOSER_TOOL_GAP = 6;
+/** 语音按钮 absolute 锚点距 composer 内容区右缘的距离。
+ * 消息列表的「跳到底部」浮标按同一常量推导麦克风所在列,保持两者圆心同列。 */
+export const MOBILE_COMPOSER_VOICE_ANCHOR_RIGHT = spacing.md;
+/** 卡片态麦克风贴工具排底边的距离,与 rowCard.paddingBottom 同源。 */
+export const MOBILE_COMPOSER_VOICE_ANCHOR_CARD_BOTTOM = spacing.sm;
+
+export interface MobileComposerVoiceButtonPlacement {
+ floating: boolean;
+ inline: boolean;
+}
+
+/**
+ * 语音按钮的左右分配:行尾有发送 / 创建 / 停止等 trailing 按钮时向左让位
+ * (floating)、否则贴行尾(inline)。判定看「是否有 trailing 按钮」而非
+ * 「是否有文字」——附件-only(无文字但发送按钮可见)时同样需要让位。
+ * 听写中按钮不隐藏——对齐桌面版设计,同一颗按钮录音中变为「停止录音」形态。
+ */
+export function resolveMobileComposerVoiceButtonPlacement(input: {
+ hasTrailingAction: boolean;
+}): MobileComposerVoiceButtonPlacement {
+ return {
+ floating: input.hasTrailingAction,
+ inline: !input.hasTrailingAction,
+ };
+}
+
+export type MobileComposerVoiceButtonAnchorStyle = {
+ position: 'absolute';
+ right: number;
+ top: '50%' | 'auto';
+ bottom: number | 'auto';
+ transform: Array<{ translateY: number }>;
+ zIndex: number;
+};
+
+/**
+ * 语音按钮的完整定位。必须一次性写出 top / bottom / transform 三套互斥键,
+ * 不能靠 StyleSheet 数组后项写 `undefined` 去覆盖前项:
+ * RN 扁平化会跳过 undefined,卡片态就会残留收起态的 `top: '50%'`,麦克风停在
+ * 卡片中部(工具排占位空着)。漏写的键在原生侧也可能继续沿用上一帧的 inset。
+ */
+export function resolveMobileComposerVoiceButtonAnchorStyle(input: {
+ cardLayout: boolean;
+ floating: boolean;
+}): MobileComposerVoiceButtonAnchorStyle {
+ const right = input.floating
+ ? MOBILE_COMPOSER_VOICE_ANCHOR_RIGHT + MOBILE_COMPOSER_CONTROL_SIZE + MOBILE_COMPOSER_TOOL_GAP
+ : MOBILE_COMPOSER_VOICE_ANCHOR_RIGHT;
+
+ if (input.cardLayout) {
+ return {
+ position: 'absolute',
+ right,
+ top: 'auto',
+ bottom: MOBILE_COMPOSER_VOICE_ANCHOR_CARD_BOTTOM,
+ transform: [],
+ zIndex: 2,
+ };
+ }
+
+ return {
+ position: 'absolute',
+ right,
+ top: '50%',
+ bottom: 'auto',
+ transform: [{ translateY: -MOBILE_COMPOSER_CONTROL_SIZE / 2 }],
+ zIndex: 2,
+ };
+}