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): 首页设备名放得下时居中,放不下时贴右侧按钮显示更多 (#5365)

Dashbade9ff80335
diff --git a/apps/mobile/src/__tests__/balancedTitle.test.ts b/apps/mobile/src/__tests__/balancedTitle.test.ts
new file mode 100644
index 0000000000..0565cb8a4a
--- /dev/null
+++ b/apps/mobile/src/__tests__/balancedTitle.test.ts
@@ -0,0 +1,28 @@
+import { describe, expect, it } from 'vitest';
+import { balancedTitleShift } from '@/platform/chrome/balancedTitle';
+
+// 402pt bar: one leading button, two trailing buttons, title slot 68…266.
+const slot = { center: 201, slotEnd: 266, slotStart: 68 };
+
+describe('balancedTitleShift', () => {
+ it('centers a title that fits between the symmetric margins', () => {
+ const contentWidth = 100;
+ const shift = balancedTitleShift({ ...slot, contentWidth });
+ const end = slot.slotEnd + shift;
+ expect(end - contentWidth / 2).toBe(slot.center);
+ });
+
+ it('pins a title that overflows the centered space to the trailing actions', () => {
+ expect(balancedTitleShift({ ...slot, contentWidth: 150 })).toBe(0);
+ expect(balancedTitleShift({ ...slot, contentWidth: 198 })).toBe(0);
+ });
+
+ it('keeps the title inside the slot when the slot sits right of the midline', () => {
+ const shift = balancedTitleShift({ center: 100, contentWidth: 80, slotEnd: 300, slotStart: 120 });
+ expect(300 + shift - 80).toBe(120);
+ });
+
+ it('never shifts toward the trailing edge', () => {
+ expect(balancedTitleShift({ center: 400, contentWidth: 40, slotEnd: 266, slotStart: 68 })).toBe(0);
+ });
+});
diff --git a/apps/mobile/src/platform/chrome/HomeNativeStackHeader.tsx b/apps/mobile/src/platform/chrome/HomeNativeStackHeader.tsx
index 83a04ca85f..4032de048d 100644
--- a/apps/mobile/src/platform/chrome/HomeNativeStackHeader.tsx
+++ b/apps/mobile/src/platform/chrome/HomeNativeStackHeader.tsx
@@ -1,7 +1,8 @@
-import type { ComponentProps, ReactNode } from "react";
+import { useState, type ComponentProps, type ReactNode } from "react";
import { Stack } from "expo-router";
import { HomeHeaderGlassButton } from "@/session/HomeHeaderGlassButton";
import { QuietSyncIndicator } from '@/components/QuietSyncIndicator';
+import { useDelayedConnectionNotice } from '@/components/ConnectionNoticeOverlay';
import { ChevronDown, Menu } from "lucide-react-native";
import { Pressable, StyleSheet, View } from "react-native";
import { useSafeAreaFrame, useSafeAreaInsets } from "react-native-safe-area-context";
@@ -66,15 +67,34 @@ export function HomeNativeStackHeader({
const nativeMenus = usesNativePullDownMenu();
const { width } = useSafeAreaFrame();
const insets = useSafeAreaInsets();
- // Keep the native title view's width independent of the selected device and
- // sync indicator. The guide only has the left menu; otherwise reserve the
- // two-action toolbar. Mirror that space, outer margin and UIKit's title
- // clearance on both sides. Without the extra clearance, iOS 26 moves
- // an otherwise centered title toward the leading edge to avoid the toolbar.
- const actionCount = showRemoteGuide ? 1 : 2;
- const sideSpace = navigationChrome.target * actionCount + spacing.lg * 2 + spacing.md;
- const titleWidth = Math.max(navigationChrome.target,
- Math.min(220, width - insets.left - insets.right - sideSpace * 2));
+ const barWidth = width - insets.left - insets.right;
+ // UIKit owns where the title view sits and moves it whenever the toolbar
+ // changes, without telling React, so the title never reads its position.
+ // It only picks the title view's width; UIKit then places it predictably:
+ // - a view that fits between mirrored side margins is centered on the bar;
+ // - a view spanning the gap between the leading and trailing items is pinned
+ // against the trailing items (UIKit measured that gap at 204pt of 402 with
+ // one leading and two trailing items, see navigationTitleWidth).
+ // With one item per side both are the same centered view. With the extra
+ // remote-desktop action, a name that fits the mirrored width stays centered;
+ // a longer one takes the gap, right-aligned, and grows into the spare room
+ // beside the menu before it ellipsizes. Mirrored margins add UIKit's title
+ // clearance, otherwise iOS 26 shifts the title toward the leading edge. The
+ // cap keeps wide windows, whose bar margins differ, from clipping both ends.
+ const trailingItems = showRemoteGuide ? 0 : onOpenRemoteDesktop ? 2 : 1;
+ const mirroredWidth = Math.max(navigationChrome.target, Math.min(360,
+ barWidth - (navigationChrome.target * Math.max(1, trailingItems) + spacing.lg * 2 + spacing.md) * 2));
+ const gapWidth = Math.max(navigationChrome.target, Math.min(360,
+ barWidth - navigationChrome.target * (1 + trailingItems) - spacing.lg * 4 - spacing.xs));
+ const [naturalWidth, setNaturalWidth] = useState<number | null>(null);
+ // One delayed visibility for the glyph and its measured footprint, so a short
+ // sync that never shows the glyph never moves the title either.
+ const syncVisible = useDelayedConnectionNotice(syncing);
+ const pinnedTrailing = trailingItems > 1
+ && naturalWidth != null
+ && naturalWidth > mirroredWidth - spacing.xs * 2;
+ const titleWidth = showRemoteGuide ? Math.min(220, mirroredWidth)
+ : trailingItems > 1 && !pinnedTrailing ? mirroredWidth : gapWidth;
if (!usesNativeStackHeader()) return null;
@@ -91,7 +111,7 @@ export function HomeNativeStackHeader({
accessibilityRole="button"
onPress={nativeMenus ? undefined : onOpenDeviceMenu}
onPressIn={nativeMenus ? undefined : onOpenDeviceMenu}
- style={({ pressed }) => [styles.titleHit, pressed && styles.pressed]}
+ style={({ pressed }) => [styles.titleHit, pinnedTrailing && styles.titleHitTrailing, pressed && styles.pressed]}
testID="devices.title"
>
{/* The title sits directly on the bar: no capsule material behind it. */}
@@ -104,11 +124,33 @@ export function HomeNativeStackHeader({
size={iconSize.xs}
strokeWidth={iconStroke.medium}
/>
- <QuietSyncIndicator active={syncing} />
+ <QuietSyncIndicator active={syncVisible} immediate />
</View>
</Pressable>
</NativePullDownMenu>
);
+ // Unconstrained, invisible copy: its width decides centered vs pinned and
+ // must not depend on the width that decision picks.
+ const titleMeasure = showRemoteGuide ? null : (
+ <View
+ accessibilityElementsHidden
+ importantForAccessibility="no-hide-descendants"
+ pointerEvents="none"
+ style={styles.titleMeasureBox}
+ >
+ <View
+ onLayout={(event) => setNaturalWidth(event.nativeEvent.layout.width)}
+ style={styles.titleMeasureRow}
+ >
+ <Text numberOfLines={1} style={styles.title}>
+ {title}
+ </Text>
+ <ChevronDown size={iconSize.xs} strokeWidth={iconStroke.medium} />
+ {/* Same footprint as the sync glyph, without a second accessible spinner. */}
+ {syncVisible ? <View style={styles.titleMeasureSync} /> : null}
+ </View>
+ </View>
+ );
return (
<>
@@ -122,6 +164,7 @@ export function HomeNativeStackHeader({
headerTransparent: true,
headerTitle: () => (
<View style={[styles.titleFrame, { width: titleWidth }]}>
+ {titleMeasure}
{titleNode}
</View>
),
@@ -207,4 +250,19 @@ const makeStyles = (colors: ThemeColors) =>
minHeight: 44,
minWidth: 44,
},
+ titleHitTrailing: { alignItems: "flex-end" },
+ titleMeasureBox: {
+ left: 0,
+ opacity: 0,
+ position: "absolute",
+ top: 0,
+ width: 2000,
+ },
+ titleMeasureRow: {
+ alignItems: "center",
+ alignSelf: "flex-start",
+ flexDirection: "row",
+ gap: spacing.xs,
+ },
+ titleMeasureSync: { height: iconSize.md, width: iconSize.md },
});
diff --git a/apps/mobile/src/platform/chrome/balancedTitle.ts b/apps/mobile/src/platform/chrome/balancedTitle.ts
new file mode 100644
index 0000000000..0d5bf9e071
--- /dev/null
+++ b/apps/mobile/src/platform/chrome/balancedTitle.ts
@@ -0,0 +1,41 @@
+import { useCallback, useState } from 'react';
+import type { LayoutChangeEvent } from 'react-native';
+
+/**
+ * Horizontal shift for a title laid out against the trailing edge of its slot.
+ * A title that fits stays centered on `center` (the bar's midline); one that
+ * does not stays against the trailing actions and grows toward the leading
+ * edge, where the bar has spare room, before it ellipsizes.
+ */
+export function balancedTitleShift({ center, contentWidth, slotEnd, slotStart }: {
+ center: number;
+ contentWidth: number;
+ slotEnd: number;
+ slotStart: number;
+}): number {
+ const end = Math.min(slotEnd, Math.max(slotStart + contentWidth, center + contentWidth / 2));
+ return Math.min(0, end - slotEnd);
+}
+
+export type BalancedTitleSlot = { start: number; end: number; center: number };
+
+/**
+ * Callers report where the slot sits relative to the bar's midline; the title
+ * itself reports its rendered width. Translating instead of padding keeps the
+ * shift out of layout, so the measured width never depends on the shift.
+ * `shift` is null until both are known.
+ */
+export function useBalancedTitle() {
+ const [slot, setSlot] = useState<BalancedTitleSlot | null>(null);
+ const [contentWidth, setContentWidth] = useState<number | null>(null);
+ const onContentLayout = useCallback((event: LayoutChangeEvent) => {
+ setContentWidth(event.nativeEvent.layout.width);
+ }, []);
+ const reportSlot = useCallback((next: BalancedTitleSlot) => {
+ setSlot(prev => prev && prev.start === next.start && prev.end === next.end && prev.center === next.center ? prev : next);
+ }, []);
+ const shift = slot && contentWidth != null && slot.end > slot.start
+ ? balancedTitleShift({ center: slot.center, contentWidth, slotEnd: slot.end, slotStart: slot.start })
+ : null;
+ return { onContentLayout, reportSlot, shift };
+}
diff --git a/apps/mobile/src/session/HomeSurface.tsx b/apps/mobile/src/session/HomeSurface.tsx
index 8e2d6742bd..506b4cfa5c 100644
--- a/apps/mobile/src/session/HomeSurface.tsx
+++ b/apps/mobile/src/session/HomeSurface.tsx
@@ -85,6 +85,7 @@ import { HomeChromeFrost } from '@/session/HomeChromeFrost';
import { HomeGlassMenuPanel, HomeMenuScrim } from '@/session/HomeGlassMenuPanel';
import { HomeHeaderGlassButton } from '@/session/HomeHeaderGlassButton';
import { useAdaptiveWindow } from '@/platform/AdaptiveWindowContext';
+import { useBalancedTitle } from '@/platform/chrome/balancedTitle';
import { HomeSearchBar } from '@/session/HomeSearchBar';
import { HomeProjectMachineLabel } from '@/session/HomeProjectMachineLabel';
import { buildHomeProjectMachineIdentities, type HomeProjectMachineIdentity } from '@/session/homeProjectMachineIdentity';
@@ -2062,6 +2063,21 @@ function HomeScreenContent({ active = true, onModeChange, width, onDismiss, newS
?? restoredDeviceName
?? t('devices.list.thisComputer');
}, [home.deviceFilters, restoredDeviceName, selectedDeviceId, t]);
+ // 设备名放得下时居中在顶栏中线;放不下时贴住右侧按钮,向左侧富余空间伸展后才截断。
+ const homeTitle = useBalancedTitle();
+ // Embedded drawers never show the remote-desktop action.
+ const showHeaderRemoteDesktop = Boolean(selectedDeviceId) && !embedded;
+ const reportHomeTitleSlot = homeTitle.reportSlot;
+ const [homeHeaderWidth, setHomeHeaderWidth] = useState(0);
+ const [homeTitleSlotFrame, setHomeTitleSlotFrame] = useState<{ x: number; width: number } | null>(null);
+ useEffect(() => {
+ if (!homeHeaderWidth || !homeTitleSlotFrame) return;
+ reportHomeTitleSlot({
+ center: homeHeaderWidth / 2,
+ end: homeTitleSlotFrame.x + homeTitleSlotFrame.width - spacing.sm,
+ start: homeTitleSlotFrame.x + spacing.sm,
+ });
+ }, [homeHeaderWidth, homeTitleSlotFrame, reportHomeTitleSlot]);
const openSession = useCallback((item: RemoteSessionListItem) => {
// 有行处于滑开状态时,点击(本行或他行)只负责收起,不进会话(iOS 列表滑动操作惯例)。
@@ -2891,8 +2907,8 @@ function HomeScreenContent({ active = true, onModeChange, width, onDismiss, newS
<HomeChromeFrost disabled={nativeHomeHeader} visible={headerFrosted}>
<View style={{ paddingTop: nativeHomeHeader ? 0 : embedded ? spacing.lg : edgePadding.paddingTop }}>
{nativeHomeHeader ? null : (
- <View style={styles.homeHeader}>
- <View style={[styles.headerLeadingActions, embedded && styles.headerEmbeddedActions]}>
+ <View onLayout={(e) => setHomeHeaderWidth(e.nativeEvent.layout.width)} style={styles.homeHeader}>
+ <View style={styles.headerLeadingActions}>
<HomeHeaderGlassButton
accessibilityLabel={onDismiss ? t('home.drawer.closeA11y') : t('devices.list.a11y.openMenu')}
onPress={onDismiss ?? openChromeMenu}
@@ -2907,32 +2923,42 @@ function HomeScreenContent({ active = true, onModeChange, width, onDismiss, newS
<Text style={styles.headerTitle} numberOfLines={1}>Cindy</Text>
</View>
) : (
+ <View
+ onLayout={(e) => {
+ const { x, width } = e.nativeEvent.layout;
+ setHomeTitleSlotFrame(prev => prev && prev.x === x && prev.width === width ? prev : { x, width });
+ }}
+ style={styles.headerTitleSlot}
+ >
<NativePullDownMenu
actions={homeScopePullDownActions}
onAction={handleHomeScopeAction}
- style={styles.headerTitleSlot}
>
<Pressable
accessibilityLabel={t('devices.list.a11y.selectScope')}
accessibilityRole="button"
onPress={nativeHomeMenus ? () => undefined : openDeviceMenu}
onPressIn={nativeHomeMenus ? undefined : openDeviceMenu}
- style={({ pressed }) => [styles.headerTitleWrap, pressed && styles.pressed]}
+ style={({ pressed }) => [styles.headerTitleWrap, homeTitle.shift != null && styles.headerTitleWrapTrailing, pressed && styles.pressed]}
testID="devices.title"
>
- <View style={styles.headerTitleCluster}>
+ <View
+ onLayout={homeTitle.onContentLayout}
+ style={[styles.headerTitleCluster, homeTitle.shift != null && { transform: [{ translateX: homeTitle.shift }] }]}
+ >
<Text style={styles.headerTitle} numberOfLines={1}>{selectedDeviceLabel}</Text>
<ChevronDown color={colors.textSecondary} size={iconSize.xs} strokeWidth={iconStroke.medium} />
<QuietSyncIndicator active={quietSyncing} />
</View>
</Pressable>
</NativePullDownMenu>
+ </View>
)}
{showRemoteGuide ? (
- <View style={[styles.headerActions, embedded && styles.headerEmbeddedActions]} />
+ <View style={styles.headerActions} />
) : (
- <View style={[styles.headerActions, embedded && styles.headerEmbeddedActions]}>
- {selectedDeviceId && !embedded ? (
+ <View style={[styles.headerActions, showHeaderRemoteDesktop && styles.headerActionsWide]}>
+ {showHeaderRemoteDesktop ? (
<HomeHeaderGlassButton accessibilityLabel={t('remoteDesktop.title')} onPress={openSelectedRemoteDesktop} testID="home.remoteDesktopButton">
<Monitor color={colors.textPrimary} size={iconSize.action} strokeWidth={iconStroke.regular} />
</HomeHeaderGlassButton>
@@ -4691,27 +4717,27 @@ const makeStyles = (colors: ThemeColors) => StyleSheet.create({
paddingHorizontal: spacing.lg,
},
headerLeadingActions: {
- // Match the trailing two-button slot so the title is centered on the
- // screen, even when selecting a device reveals the remote-desktop action.
+ // Only the menu button lives here. The title centers itself on the header
+ // midline and may use the spare room on this side when the name is long.
alignItems: 'flex-start',
flexShrink: 0,
height: navigationChrome.target,
justifyContent: 'center',
- width: navigationChrome.target * 2 + spacing.xs,
+ width: navigationChrome.target,
},
+ // Sized to the buttons actually shown, so the guide's brand title stays
+ // centered and a long device name is not cut short by an empty slot.
headerActions: {
alignItems: 'center',
flexDirection: 'row',
flexShrink: 0,
gap: spacing.xs,
justifyContent: 'flex-end',
- width: navigationChrome.target * 2 + spacing.xs,
- },
- headerEmbeddedActions: {
- // Embedded drawers never show the remote-desktop action. Keep both sides
- // symmetric without reserving space for a second button that cannot appear.
width: navigationChrome.target,
},
+ headerActionsWide: {
+ width: navigationChrome.target * 2 + spacing.xs,
+ },
// 菜单外层替标题占住顶栏中间的剩余宽度,长设备名在这里截断而不是挤开右侧按钮。
headerTitleSlot: {
flex: 1,
@@ -4725,6 +4751,9 @@ const makeStyles = (colors: ThemeColors) => StyleSheet.create({
minWidth: 0,
paddingHorizontal: spacing.sm,
},
+ headerTitleWrapTrailing: {
+ alignItems: 'flex-end',
+ },
headerTitleCluster: {
alignItems: 'center',
flexDirection: 'row',