Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。
TypeScript agentai-agentai-assistantandroidclaude-codecodexelectroniosllmmacosreact-nativetypescriptwindows
import { useEffect, useState, type ReactNode } from "react";
import {
ActivityIndicator,
Platform,
Pressable,
StyleSheet,
View,
} from "react-native";
import SegmentedControl from "@expo/ui/community/segmented-control";
import { Check, ChevronDown, Maximize, RotateCcw } from "lucide-react-native";
import { useTranslation } from "react-i18next";
import {
REMOTE_DESKTOP_VIDEO_QUALITIES,
type RemoteDesktopDisplayMode,
type RemoteDesktopVideoQuality,
type RemoteDesktopVideoSettings,
} from "@cindy/device-link";
import { Text } from "@/components/AppText";
import { mobileInteractionStyles } from "@/components/mobileInteractionStyles";
import { RemoteDesktopActionButton } from "./RemoteDesktopActionButton";
import {
NativePullDownMenu,
usesNativePullDownMenu,
} from "@/platform/chrome/NativePullDownMenu";
import {
spacing,
radius,
typeScale,
iconSize,
fontWeight,
lineHeight,
useTheme,
} from "@/theme";
const QUALITY_LABELS = {
auto: "remoteDesktop.automatic",
saver: "remoteDesktop.saver",
hd: "remoteDesktop.hd",
} as const satisfies Record<RemoteDesktopVideoQuality, string>;
type Props = {
video: {
supported: boolean;
settings: RemoteDesktopVideoSettings;
busy: boolean;
modesSupported: boolean;
displayGeometry?: string;
viewerDisplaySupported?: boolean;
viewerDisplayMatched?: boolean;
onFitDisplay?(): void;
onChange(settings: Partial<RemoteDesktopVideoSettings>): void;
readModes(): Promise<RemoteDesktopDisplayMode[]>;
onResolution(mode: RemoteDesktopDisplayMode): Promise<void>;
};
connected: boolean;
controlling: boolean;
displayControl: ReactNode;
};
export function RemoteDesktopDisplaySettings({
video,
connected,
controlling,
displayControl,
}: Props) {
const { t } = useTranslation();
const { colors, mode: colorScheme } = useTheme();
const [modes, setModes] = useState<RemoteDesktopDisplayMode[]>([]);
const [expanded, setExpanded] = useState(false);
const [loading, setLoading] = useState(false);
const [failure, setFailure] = useState(false);
const [reload, setReload] = useState(0);
useEffect(() => {
if (!connected || !video.modesSupported) return;
let active = true;
setLoading(true);
setFailure(false);
void video
.readModes()
.then((next) => {
if (active) setModes(next);
})
.catch(() => {
if (active) setFailure(true);
})
.finally(() => {
if (active) setLoading(false);
});
return () => {
active = false;
};
}, [connected, video.modesSupported, video.displayGeometry, reload]);
const disabled = !connected || !video.supported;
const title = { color: colors.textPrimary, fontSize: typeScale.body, lineHeight: lineHeight.body };
// 说明档(13/18,二级字色):成句的提示与当前分辨率取值。
const hint = {
color: colors.textSecondary,
fontSize: typeScale.footnote,
lineHeight: lineHeight.caption,
};
const segment = (label: string, selected: boolean, onPress: () => void) => (
<Pressable
key={label}
accessibilityRole="radio"
accessibilityState={{ checked: selected, disabled }}
disabled={disabled}
onPress={onPress}
style={({ pressed }) => [
{
flex: 1,
minHeight: 44,
borderRadius: radius.pill,
alignItems: "center",
justifyContent: "center",
backgroundColor: selected ? colors.cta : "transparent",
},
disabled && { opacity: 0.6 },
pressed && !disabled && mobileInteractionStyles.pressed,
]}
>
<Text
style={{
...title,
color: selected ? colors.ctaText : colors.textPrimary,
fontWeight: fontWeight.medium,
}}
>
{label}
</Text>
</Pressable>
);
const segments = {
flexDirection: "row" as const,
padding: spacing.xs,
borderRadius: radius.pill,
backgroundColor: colors.surfaceChip,
};
const segmented = (
name: "frameRate" | "quality",
values: string[],
selectedIndex: number,
onSelect: (index: number) => void,
) =>
Platform.OS === "ios" ? (
<View
testID={`remoteDesktop.${name}Control`}
pointerEvents={disabled ? "none" : "auto"}
accessible={disabled}
accessibilityLabel={t(`remoteDesktop.${name}`)}
accessibilityState={{ disabled }}
>
<SegmentedControl
values={values}
selectedIndex={selectedIndex}
enabled={!disabled}
appearance={colorScheme}
style={{ height: 44 }}
onChange={({ nativeEvent }) => {
const index = nativeEvent.selectedSegmentIndex;
if (!disabled && index >= 0 && index < values.length)
onSelect(index);
}}
/>
</View>
) : (
<View style={segments}>
{values.map((label, index) =>
segment(label, index === selectedIndex, () => onSelect(index)),
)}
</View>
);
const fpsValues = [30, 60] as const;
const current = modes.find((mode) => mode.current);
const modeLabel = (mode: RemoteDesktopDisplayMode) =>
`${mode.width} × ${mode.height}${mode.native === true ? ` · ${t("remoteDesktop.nativeResolution")}` : ""}`;
const chooseMode = (id: string) => {
const mode = modes.find((item) => item.id === id);
if (!mode || mode.current || !controlling || video.busy || !connected)
return;
void video
.onResolution(mode)
.then(() => setExpanded(false))
.catch(() => {});
};
const nativeMenu =
usesNativePullDownMenu() &&
connected &&
video.modesSupported &&
!loading &&
!failure &&
!video.busy &&
modes.length > 0;
const resolutionTrigger = (
<Pressable
disabled={!connected || !video.modesSupported || loading || video.busy}
onPress={() =>
failure ? setReload(reload + 1) : !nativeMenu && setExpanded(!expanded)
}
style={styles.row}
accessibilityRole="button"
accessibilityState={{ expanded: nativeMenu ? undefined : expanded }}
accessibilityLabel={
failure
? t("remoteDesktop.retrySettings")
: t("remoteDesktop.resolution")
}
>
<View style={{ flex: 1, gap: spacing.xs }}>
<Text style={title}>{t("remoteDesktop.resolution")}</Text>
<Text style={hint} numberOfLines={1}>
{!video.modesSupported
? t("remoteDesktop.settingUnsupported")
: current
? modeLabel(current)
: t("remoteDesktop.followComputer")}
</Text>
</View>
<View
style={{
width: 24,
height: 24,
alignItems: "center",
justifyContent: "center",
}}
>
{loading || video.busy ? (
<ActivityIndicator
size="small"
color={colors.textTertiary}
accessibilityLabel={t("remoteDesktop.loadingSettings")}
/>
) : (
<ChevronDown size={iconSize.md} color={colors.textTertiary} />
)}
</View>
</Pressable>
);
return (
<View style={{ gap: spacing.xl }}>
<View style={{ gap: spacing.sm }}>
<Text style={[title, { fontWeight: fontWeight.medium }]}>
{t("remoteDesktop.frameRate")}
</Text>
{segmented(
"frameRate",
fpsValues.map((fps) => t("remoteDesktop.fps", { count: fps })),
fpsValues.indexOf(video.settings.fps),
(index) => video.onChange({ fps: fpsValues[index] }),
)}
</View>
<View style={{ gap: spacing.sm }}>
<Text style={[title, { fontWeight: fontWeight.medium }]}>
{t("remoteDesktop.quality")}
</Text>
{segmented(
"quality",
REMOTE_DESKTOP_VIDEO_QUALITIES.map((quality) =>
t(QUALITY_LABELS[quality]),
),
REMOTE_DESKTOP_VIDEO_QUALITIES.indexOf(video.settings.quality),
(index) =>
video.onChange({ quality: REMOTE_DESKTOP_VIDEO_QUALITIES[index] }),
)}
<Text style={hint}>{t("remoteDesktop.qualityHint")}</Text>
</View>
<View style={{ gap: spacing.sm }}>
<View
style={{
backgroundColor: colors.surfaceTranslucent,
borderRadius: radius.container,
borderWidth: StyleSheet.hairlineWidth,
borderColor: colors.sheetActionBorder,
padding: spacing.lg,
gap: spacing.lg,
}}
>
<View style={{ marginHorizontal: -spacing.md }}>
{displayControl}
</View>
{video.viewerDisplaySupported && (
<View style={{ gap: spacing.sm }}>
<RemoteDesktopActionButton
variant="glass"
systemImage={
video.viewerDisplayMatched
? "arrow.uturn.backward"
: "arrow.up.left.and.arrow.down.right"
}
accessibilityRole="button"
accessibilityLabel={t(
video.viewerDisplayMatched
? "remoteDesktop.restoreViewerDisplay"
: "remoteDesktop.fitViewerDisplay",
)}
disabled={!connected || !controlling || video.busy}
onPress={() => video.onFitDisplay?.()}
style={[
styles.row,
{
backgroundColor:
Platform.OS === "ios" ? "transparent" : colors.surface,
borderColor: colors.border,
borderWidth:
Platform.OS === "ios" ? 0 : StyleSheet.hairlineWidth,
borderRadius: radius.pill,
padding: spacing.md,
justifyContent: "center",
opacity: !connected || !controlling || video.busy ? 0.6 : 1,
},
]}
>
{video.busy ? (
<ActivityIndicator size="small" color={colors.textPrimary} />
) : video.viewerDisplayMatched ? (
<RotateCcw size={iconSize.md} color={colors.textPrimary} />
) : (
<Maximize size={iconSize.md} color={colors.textPrimary} />
)}
<Text
style={[
title,
{ fontWeight: fontWeight.medium, flexShrink: 1 },
]}
>
{t(
video.viewerDisplayMatched
? "remoteDesktop.restoreViewerDisplay"
: "remoteDesktop.fitViewerDisplay",
)}
</Text>
</RemoteDesktopActionButton>
<Text style={hint}>
{t("remoteDesktop.fitViewerDisplayHint")}
</Text>
</View>
)}
<View
style={{
height: StyleSheet.hairlineWidth,
backgroundColor: colors.border,
}}
/>
{nativeMenu ? (
<NativePullDownMenu
actions={modes.map((mode) => ({
id: mode.id,
title: modeLabel(mode),
state: mode.current ? "on" : "off",
disabled: !controlling || mode.current,
}))}
onAction={chooseMode}
>
{resolutionTrigger}
</NativePullDownMenu>
) : (
resolutionTrigger
)}
{!nativeMenu &&
expanded &&
modes.map((mode) => (
<Pressable
key={mode.id}
disabled={!controlling || video.busy || mode.current}
accessibilityRole="radio"
accessibilityState={{ checked: mode.current }}
onPress={() => chooseMode(mode.id)}
style={({ pressed }) => [
styles.row,
!controlling && { opacity: 0.4 },
pressed && mobileInteractionStyles.pressed,
]}
>
<Text style={[title, { flex: 1 }]}>{modeLabel(mode)}</Text>
{mode.current && (
<Check size={iconSize.md} color={colors.textPrimary} />
)}
</Pressable>
))}
{failure && (
<Text style={hint} accessibilityRole="alert">
{t("remoteDesktop.retrySettings")}
</Text>
)}
</View>
<Text style={[hint, { paddingHorizontal: spacing.xs }]}>
{t(
controlling
? "remoteDesktop.resolutionHint"
: "remoteDesktop.resolutionControlHint",
)}
</Text>
</View>
{!video.supported && (
<Text style={hint}>{t("remoteDesktop.settingUnsupported")}</Text>
)}
</View>
);
}
const styles = StyleSheet.create({
row: {
minHeight: 44,
flexDirection: "row",
alignItems: "center",
gap: spacing.sm,
},
});