@devries-cameron20 / t3code Mirror · read-only
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 04:45 UTC

main
tsx 367 lines 13.3 KB
Raw
1 import { msg, useGT, useMessages } from "gt-react-native";
2 import { memo, useId } from "react";
3 import { Pressable, View } from "react-native";
4 import Svg, { Circle, Defs, RadialGradient, Stop } from "react-native-svg";
5 import { ScopedTheme, ScopedVariables } from "uniwind";
6
7 import { mixThemePreviewBase, THEME_PREVIEW_RENDER_SPECS } from "@t3tools/shared/themePreview";
8
9 import { SymbolView } from "../../../../components/ui/AppSymbol";
10 import { AppText as Text } from "../../../../components/ui/AppText";
11 import {
12 getMobileThemePreviewColors,
13 MOBILE_THEME_OPTIONS,
14 type MobileThemeAppearance,
15 type MobileThemeId,
16 type MobileThemeIds,
17 type MobileThemeMode,
18 } from "../../../../lib/mobileTheme";
19 import { getMobileUniwindThemeName } from "../../../../lib/mobileThemeRuntime";
20 import type { MessageResolver } from "../../../../i18n/messages";
21 import { cn } from "../../../../lib/cn";
22 import { useAppearancePreferences } from "../AppearancePreferencesProvider";
23
24 const THEME_NAMES: Readonly<Record<string, string>> = {
25 grove: msg("Grove", { $context: "color theme name" }),
26 ocean: msg("Ocean", { $context: "color theme name" }),
27 ember: msg("Ember", { $context: "color theme name" }),
28 iris: msg("Iris", { $context: "color theme name" }),
29 };
30
31 function themeName(
32 theme: { readonly id: string; readonly label: string },
33 resolve: MessageResolver,
34 ) {
35 const name = THEME_NAMES[theme.id];
36 return name === undefined ? theme.label : resolve(name);
37 }
38
39 const APPEARANCE_MODES: ReadonlyArray<{
40 readonly id: MobileThemeMode;
41 readonly label: string;
42 }> = [
43 { id: "system", label: msg("System", { $context: "appearance follows the system" }) },
44 { id: "light", label: msg("Light", { $context: "light appearance" }) },
45 { id: "dark", label: msg("Dark", { $context: "dark appearance" }) },
46 ];
47
48 const previewPercentage = (value: number) => `${value * 100}%`;
49
50 const PreviewOrb = memo(function PreviewOrb(props: {
51 readonly appearance: MobileThemeAppearance;
52 readonly compact?: boolean;
53 readonly themeId: MobileThemeId;
54 }) {
55 const idPrefix = useId().replaceAll(":", "");
56 const accentGradientId = `${idPrefix}-accent-glow`;
57 const actionGradientId = `${idPrefix}-action-glow`;
58 const { systemColorPalettes } = useAppearancePreferences();
59 const palette = systemColorPalettes?.[props.appearance];
60 const colors =
61 props.themeId === "material-you" && palette
62 ? { canvas: palette.surface, accent: palette.primary, messageAction: palette.tertiary }
63 : getMobileThemePreviewColors(props.themeId, props.appearance);
64 const spec = THEME_PREVIEW_RENDER_SPECS[props.appearance];
65 const accentRadius = Math.hypot(
66 Math.max(spec.accent.center[0], 1 - spec.accent.center[0]),
67 Math.max(spec.accent.center[1], 1 - spec.accent.center[1]),
68 );
69 const actionRadius = Math.hypot(
70 Math.max(spec.action.center[0], 1 - spec.action.center[0]),
71 Math.max(spec.action.center[1], 1 - spec.action.center[1]),
72 );
73 return (
74 <View
75 className={`${props.compact ? "size-14" : "size-16"} overflow-hidden rounded-full border`}
76 style={{
77 borderColor:
78 props.appearance === "dark" ? "rgba(255, 255, 255, 0.14)" : "rgba(0, 0, 0, 0.1)",
79 }}
80 >
81 <Svg accessibilityElementsHidden height="100%" viewBox="0 0 64 64" width="100%">
82 <Defs>
83 <RadialGradient
84 cx={previewPercentage(spec.accent.center[0])}
85 cy={previewPercentage(spec.accent.center[1])}
86 fx={previewPercentage(spec.accent.center[0])}
87 fy={previewPercentage(spec.accent.center[1])}
88 id={accentGradientId}
89 r={previewPercentage(accentRadius)}
90 >
91 <Stop offset="0%" stopColor={colors.accent} stopOpacity={1} />
92 <Stop
93 offset={previewPercentage(spec.accent.middleOffset)}
94 stopColor={colors.accent}
95 stopOpacity={spec.accent.middleOpacity}
96 />
97 <Stop
98 offset={previewPercentage(spec.accent.endOffset)}
99 stopColor={colors.accent}
100 stopOpacity={0}
101 />
102 <Stop offset="100%" stopColor={colors.accent} stopOpacity={0} />
103 </RadialGradient>
104 <RadialGradient
105 cx={previewPercentage(spec.action.center[0])}
106 cy={previewPercentage(spec.action.center[1])}
107 fx={previewPercentage(spec.action.center[0])}
108 fy={previewPercentage(spec.action.center[1])}
109 id={actionGradientId}
110 r={previewPercentage(actionRadius)}
111 >
112 <Stop
113 offset="0%"
114 stopColor={colors.messageAction}
115 stopOpacity={spec.action.startOpacity}
116 />
117 <Stop
118 offset={previewPercentage(spec.action.endOffset)}
119 stopColor={colors.messageAction}
120 stopOpacity={0}
121 />
122 <Stop offset="100%" stopColor={colors.messageAction} stopOpacity={0} />
123 </RadialGradient>
124 </Defs>
125 <Circle cx="32" cy="32" fill={mixThemePreviewBase(colors, props.appearance)} r="32" />
126 <Circle cx="32" cy="32" fill={`url(#${actionGradientId})`} r="32" />
127 <Circle cx="32" cy="32" fill={`url(#${accentGradientId})`} r="32" />
128 </Svg>
129 </View>
130 );
131 });
132
133 function ThemeCard(props: {
134 readonly disabled: boolean;
135 readonly darkSelected: boolean;
136 readonly label: string;
137 readonly lightSelected: boolean;
138 readonly onSelectBoth: () => void;
139 readonly onSelect: (appearance: MobileThemeAppearance) => void;
140 readonly themeId: MobileThemeId;
141 }) {
142 const gt = useGT();
143 const choice = (appearance: MobileThemeAppearance, selected: boolean) => (
144 <Pressable
145 accessibilityHint={
146 appearance === "light"
147 ? gt("Sets the light appearance only")
148 : gt("Sets the dark appearance only")
149 }
150 accessibilityLabel={
151 appearance === "light"
152 ? gt("{theme} light theme", { theme: props.label })
153 : gt("{theme} dark theme", { theme: props.label })
154 }
155 accessibilityRole="button"
156 accessibilityState={{ disabled: props.disabled, selected }}
157 className={cn(
158 "size-[66px] items-center justify-center rounded-full border-[3px] active:scale-[0.94]",
159 selected ? "border-primary" : "border-transparent",
160 )}
161 disabled={props.disabled}
162 onPress={() => props.onSelect(appearance)}
163 >
164 <PreviewOrb appearance={appearance} compact themeId={props.themeId} />
165 {selected ? (
166 <View className="absolute -bottom-0.5 -right-0.5 size-5 items-center justify-center rounded-full border border-border bg-card">
167 <SymbolView
168 name={appearance === "light" ? "sun.max" : "moon"}
169 size={12}
170 tintColorClassName="accent-icon"
171 type="monochrome"
172 weight="medium"
173 />
174 </View>
175 ) : null}
176 </Pressable>
177 );
178
179 return (
180 <View className="min-w-36 flex-1 basis-[47%] gap-3 rounded-[24px] border border-border bg-grouped-card px-2 py-4">
181 <Pressable
182 accessibilityHint={gt("Sets both light and dark appearances")}
183 accessibilityLabel={gt("{theme} theme", { theme: props.label })}
184 accessibilityRole="button"
185 accessibilityState={{
186 disabled: props.disabled,
187 selected: props.lightSelected && props.darkSelected,
188 }}
189 className="absolute inset-0 rounded-[24px] active:bg-subtle"
190 disabled={props.disabled}
191 onPress={props.onSelectBoth}
192 />
193 <View className="flex-row items-center justify-center gap-2 py-1" pointerEvents="box-none">
194 {choice("light", props.lightSelected)}
195 {choice("dark", props.darkSelected)}
196 </View>
197 <Text
198 className="min-w-0 flex-1 px-1 text-lg font-t3-medium"
199 numberOfLines={1}
200 pointerEvents="none"
201 >
202 {props.label}
203 </Text>
204 </View>
205 );
206 }
207
208 function PreviewPane(props: { readonly compact?: boolean }) {
209 return (
210 <View className="flex-1 overflow-hidden bg-screen">
211 <View
212 className={cn("bg-card", props.compact ? "h-[18px] gap-0.5 px-1" : "h-[18px] gap-1 px-1.5")}
213 >
214 <View className="mt-2 flex-row items-center gap-1">
215 <View className="size-1.5 rounded-full bg-primary" />
216 <View className="h-1 flex-1 rounded-full bg-foreground-muted" />
217 </View>
218 </View>
219 <View
220 className={
221 props.compact ? "flex-1 justify-between px-1 py-2" : "flex-1 justify-between px-1.5 py-2"
222 }
223 >
224 <View className="gap-1">
225 <View className="h-1.5 w-[72%] rounded-full bg-subtle-strong" />
226 <View className="h-1.5 w-[46%] rounded-full bg-subtle-strong" />
227 </View>
228 <View className="items-end gap-1 pb-2">
229 <View className="h-3 w-[78%] rounded-full bg-user-bubble" />
230 <View className="h-1 w-[38%] rounded-full bg-foreground-muted" />
231 </View>
232 </View>
233 </View>
234 );
235 }
236
237 function ModePreview(props: { readonly mode: MobileThemeMode; readonly themeIds: MobileThemeIds }) {
238 const { themeVariablesByAppearance } = useAppearancePreferences();
239 if (props.mode === "system") {
240 return (
241 <View className="h-24 w-14 self-center rounded-[16px] border-[1.5px] border-border bg-drawer p-[3px]">
242 <View className="flex-1 flex-row overflow-hidden rounded-[11px]">
243 <ScopedTheme theme={getMobileUniwindThemeName(props.themeIds.light, "light")}>
244 <ScopedVariables variables={themeVariablesByAppearance.light}>
245 <PreviewPane compact />
246 </ScopedVariables>
247 </ScopedTheme>
248 <ScopedTheme theme={getMobileUniwindThemeName(props.themeIds.dark, "dark")}>
249 <ScopedVariables variables={themeVariablesByAppearance.dark}>
250 <PreviewPane compact />
251 </ScopedVariables>
252 </ScopedTheme>
253 </View>
254 <View className="absolute bottom-[6px] left-1/2 h-1 w-4 -translate-x-1/2 rounded-full bg-foreground-muted" />
255 </View>
256 );
257 }
258
259 return (
260 <ScopedTheme theme={getMobileUniwindThemeName(props.themeIds[props.mode], props.mode)}>
261 <View className="h-24 w-14 self-center rounded-[16px] border-[1.5px] border-border bg-drawer p-[3px]">
262 <View className="flex-1 flex-row overflow-hidden rounded-[11px]">
263 <ScopedVariables variables={themeVariablesByAppearance[props.mode]}>
264 <PreviewPane />
265 </ScopedVariables>
266 </View>
267 <View className="absolute bottom-[6px] left-1/2 h-1 w-4 -translate-x-1/2 rounded-full bg-foreground-muted" />
268 </View>
269 </ScopedTheme>
270 );
271 }
272
273 function ModeCard(props: {
274 readonly disabled: boolean;
275 readonly label: string;
276 readonly mode: MobileThemeMode;
277 readonly onPress: () => void;
278 readonly selected: boolean;
279 readonly themeIds: MobileThemeIds;
280 }) {
281 const gt = useGT();
282 return (
283 <Pressable
284 accessibilityLabel={gt("{mode} appearance", { mode: props.label })}
285 accessibilityRole="radio"
286 accessibilityState={{ checked: props.selected, disabled: props.disabled }}
287 className={cn(
288 "min-w-0 flex-1 gap-2 rounded-[24px] p-2 active:scale-[0.97]",
289 props.selected
290 ? "border-2 border-primary bg-subtle"
291 : "border border-border bg-grouped-card",
292 )}
293 disabled={props.disabled}
294 onPress={props.onPress}
295 >
296 <ModePreview mode={props.mode} themeIds={props.themeIds} />
297 <Text
298 className={
299 props.selected
300 ? "text-center text-base font-t3-bold text-foreground"
301 : "text-center text-base text-foreground-muted"
302 }
303 >
304 {props.label}
305 </Text>
306 </Pressable>
307 );
308 }
309
310 function SectionLabel({ children }: { readonly children: string }) {
311 return <Text className="px-2 text-sm font-t3-medium text-foreground-muted">{children}</Text>;
312 }
313
314 export function ThemeAppearanceSection() {
315 const gt = useGT();
316 const m = useMessages();
317 const {
318 isReady,
319 setThemeIdForAppearance,
320 setThemeIdForBothAppearances,
321 setThemeMode,
322 themeIds,
323 themeMode,
324 systemColorsAvailable,
325 } = useAppearancePreferences();
326
327 return (
328 <View className="gap-6">
329 <View className="gap-2">
330 <SectionLabel>{gt("Color scheme")}</SectionLabel>
331 <View accessibilityRole="radiogroup" className="flex-row gap-2">
332 {APPEARANCE_MODES.map((mode) => (
333 <ModeCard
334 disabled={!isReady}
335 key={mode.id}
336 label={m(mode.label)}
337 mode={mode.id}
338 onPress={() => setThemeMode(mode.id)}
339 selected={mode.id === themeMode}
340 themeIds={themeIds}
341 />
342 ))}
343 </View>
344 </View>
345
346 <View className="gap-3">
347 <SectionLabel>{gt("Themes")}</SectionLabel>
348 <View className="flex-row flex-wrap gap-3">
349 {MOBILE_THEME_OPTIONS.filter(
350 (theme) => theme.id !== "material-you" || systemColorsAvailable,
351 ).map((theme) => (
352 <ThemeCard
353 disabled={!isReady}
354 key={theme.id}
355 label={themeName(theme, m)}
356 darkSelected={theme.id === themeIds.dark}
357 lightSelected={theme.id === themeIds.light}
358 onSelect={(appearance) => setThemeIdForAppearance(appearance, theme.id)}
359 onSelectBoth={() => setThemeIdForBothAppearances(theme.id)}
360 themeId={theme.id}
361 />
362 ))}
363 </View>
364 </View>
365 </View>
366 );
367 }