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): 速度面板打开时移入读屏焦点,关闭时还给触发按钮

Dasheb4dc6b2a364
diff --git a/apps/mobile/src/__tests__/runningTokenRatePopover.test.tsx b/apps/mobile/src/__tests__/runningTokenRatePopover.test.tsx
index d7ba33f375..eb289d0375 100644
--- a/apps/mobile/src/__tests__/runningTokenRatePopover.test.tsx
+++ b/apps/mobile/src/__tests__/runningTokenRatePopover.test.tsx
@@ -35,6 +35,7 @@ const harness = vi.hoisted(() => ({
onOutsideTap: () => void;
},
back: null as null | (() => boolean),
+ focus: [] as unknown[],
}));
vi.mock("react-native", () => {
const view = ({ children, testID }: any) =>
@@ -42,6 +43,7 @@ vi.mock("react-native", () => {
return {
View: forwardRef((props: any, ref) => {
useImperativeHandle(ref, () => ({
+ testID: props.testID,
measureInWindow: (callback: any) =>
callback(
harness.anchor.x,
@@ -53,6 +55,10 @@ vi.mock("react-native", () => {
if (props.testID === "session.tokenRate.card") harness.card = props;
return view(props);
}),
+ AccessibilityInfo: {
+ sendAccessibilityEvent: (target: any) =>
+ harness.focus.push(target?.testID ?? target),
+ },
BackHandler: {
addEventListener: (_: string, handler: () => boolean) => {
harness.back = handler;
@@ -65,6 +71,7 @@ vi.mock("react-native", () => {
StyleSheet: { create: (s: unknown) => s },
Pressable: (props: any) => {
harness.press = props;
+ if (props.ref) props.ref.current = "trigger";
return view(props);
},
};
@@ -317,6 +324,7 @@ it.each(["onPress", "onLongPress"])(
);
beforeEach(() => {
Object.assign(globalThis, { IS_REACT_ACT_ENVIRONMENT: true });
+ harness.focus = [];
clearRateHistoryCache();
harness.viewport = { x: 0, y: 0, width: 320, height: 800 };
harness.window = { width: 320, height: 800 };
@@ -521,3 +529,39 @@ it("uses paired generation samples, expires recent speed, and isolates a differe
await gesture("onPress");
expect(card()!.textContent).not.toContain("100 tok/s");
});
+
+it("moves screen reader focus into the pinned card and back to the trigger, except after an outside tap", async () => {
+ const layout = () =>
+ act(async () =>
+ harness.card.onLayout({ nativeEvent: { layout: { height: 120 } } }),
+ );
+ await render();
+ await gesture("onPressIn");
+ await gesture("onLongPress");
+ await layout();
+ await gesture("onPressOut");
+ // Holding is a touch gesture; it never moves screen reader focus.
+ expect(harness.focus).toEqual([]);
+ for (const close of [
+ () => harness.back!(),
+ () => harness.card.onAccessibilityEscape(),
+ ]) {
+ await gesture("onPressIn");
+ await gesture("onPress");
+ await layout();
+ await layout();
+ expect(harness.focus).toEqual(["session.tokenRate.card"]);
+ await act(async () => {
+ close();
+ });
+ expect(card()).toBeNull();
+ expect(harness.focus).toEqual(["session.tokenRate.card", "trigger"]);
+ harness.focus = [];
+ }
+ await gesture("onPressIn");
+ await gesture("onPress");
+ await layout();
+ await act(async () => harness.outsideTap!.onOutsideTap());
+ expect(card()).toBeNull();
+ expect(harness.focus).toEqual(["session.tokenRate.card"]);
+});
diff --git a/apps/mobile/src/session/RunningTokenRatePopover.tsx b/apps/mobile/src/session/RunningTokenRatePopover.tsx
index d0f3a6b6f4..d34f56435d 100644
--- a/apps/mobile/src/session/RunningTokenRatePopover.tsx
+++ b/apps/mobile/src/session/RunningTokenRatePopover.tsx
@@ -1,5 +1,6 @@
import { useEffect, useRef, useState, type ReactNode } from "react";
import {
+ AccessibilityInfo,
BackHandler,
Pressable,
ScrollView,
@@ -74,6 +75,8 @@ export function RunningTokenRatePopover({
const window = useWindowDimensions();
const insets = useSafeAreaInsets();
const anchorRef = useRef<View>(null);
+ const triggerRef = useRef<View>(null);
+ const cardRef = useRef<View>(null);
const [anchor, setAnchor] = useState({ x: 0, y: 0, width: 0, height: 0 });
const [cardHeight, setCardHeight] = useState(0);
const measureAnchor = () =>
@@ -153,12 +156,31 @@ export function RunningTokenRatePopover({
}),
() => setMode("closed"),
);
+ // The card is not an accessibility modal, so the chat stays usable. Screen
+ // reader focus moves into it on open and back to the trigger when the user
+ // closes it from the trigger, back or escape; an outside tap keeps focus
+ // wherever that tap went.
+ const cardFocused = useRef(false);
+ const refocusTrigger = useRef(false);
+ const closeAndRefocus = () => {
+ refocusTrigger.current = true;
+ setMode("closed");
+ };
+ useEffect(() => {
+ if (mode === "pinned") return;
+ cardFocused.current = false;
+ if (mode === "closed" && refocusTrigger.current) {
+ refocusTrigger.current = false;
+ if (triggerRef.current)
+ AccessibilityInfo.sendAccessibilityEvent(triggerRef.current, "focus");
+ }
+ }, [mode]);
useEffect(() => {
if (mode !== "pinned") return;
const subscription = BackHandler.addEventListener(
"hardwareBackPress",
() => {
- setMode("closed");
+ closeAndRefocus();
return true;
},
);
@@ -238,9 +260,16 @@ export function RunningTokenRatePopover({
<View
pointerEvents={mode === "held" ? "none" : "auto"}
onStartShouldSetResponder={() => true}
- onAccessibilityEscape={() => setMode("closed")}
+ ref={cardRef}
+ onAccessibilityEscape={closeAndRefocus}
testID="session.tokenRate.card"
- onLayout={(event) => setCardHeight(event.nativeEvent.layout.height)}
+ onLayout={(event) => {
+ setCardHeight(event.nativeEvent.layout.height);
+ if (mode === "pinned" && !cardFocused.current && cardRef.current) {
+ cardFocused.current = true;
+ AccessibilityInfo.sendAccessibilityEvent(cardRef.current, "focus");
+ }
+ }}
style={[
styles.card,
{
@@ -331,6 +360,7 @@ export function RunningTokenRatePopover({
onLayout={measureAnchor}
>
<Pressable
+ ref={triggerRef}
accessibilityRole="button"
accessibilityLabel={label}
accessibilityState={{ expanded: mode !== "closed" }}