Skip to content

Commit 481e0bf

Browse files
committed
feat(web,mobile): choose whether usage meters count up or down
Adds Settings > Appearance > Usage meters (% left by default, or % used). Limits, /usage-limits, thread details limit rows and the mobile widgets follow it. Pace ticks mark time elapsed and reset hatches cover the used fill when counting up; warning colours stay at the same real points. Context keeps filling up as it is used in both modes.
1 parent 8e6a732 commit 481e0bf

24 files changed

Lines changed: 339 additions & 113 deletions

‎apps/mobile/src/features/settings/SettingsAppearanceRouteScreen.tsx‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { CodeAppearanceSection } from "./appearance/sections/CodeAppearanceSecti
88
import { TerminalAppearanceSection } from "./appearance/sections/TerminalAppearanceSection";
99
import { TextAppearanceSection } from "./appearance/sections/TextAppearanceSection";
1010
import { ThemeAppearanceSection } from "./appearance/sections/ThemeAppearanceSection";
11+
import { UsageAppearanceSection } from "./appearance/sections/UsageAppearanceSection";
1112

1213
export function SettingsAppearanceRouteScreen() {
1314
const insets = useSafeAreaInsets();
@@ -28,6 +29,7 @@ export function SettingsAppearanceRouteScreen() {
2829
<TextAppearanceSection />
2930
<TerminalAppearanceSection />
3031
<CodeAppearanceSection />
32+
<UsageAppearanceSection />
3133
</ScrollView>
3234
</SettingsScreen>
3335
);

‎apps/mobile/src/features/settings/appearance/AppearancePreferencesProvider.tsx‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import {
2020
import { appAtomRegistry } from "../../../state/atom-registry";
2121
import { mobilePreferencesAtom, updateMobilePreferencesAtom } from "../../../state/preferences";
2222
import type { Preferences } from "../../../persistence/mobile-preferences";
23+
import type { UsageDisplay } from "@t3tools/contracts/settings";
2324
import {
2425
isSystemColorsAvailable,
2526
readSystemColorPalettes,
@@ -72,6 +73,8 @@ interface AppearancePreferencesContextValue {
7273
readonly setCodeWordBreak: (value: boolean) => void;
7374
readonly mathRenderingEnabled: boolean;
7475
readonly setMathRenderingEnabled: (value: boolean) => void;
76+
readonly usageDisplay: UsageDisplay;
77+
readonly setUsageDisplay: (value: UsageDisplay) => void;
7578
}
7679

7780
const AppearancePreferencesContext = createContext<AppearancePreferencesContextValue | null>(null);
@@ -293,6 +296,9 @@ export function AppearancePreferencesProvider(props: { readonly children: ReactN
293296
const setMathRenderingEnabled = (value: boolean) =>
294297
updatePreferences({ mathRenderingEnabled: value });
295298

299+
const usageDisplay = storedPreferences?.usageDisplay ?? "left";
300+
const setUsageDisplay = (value: UsageDisplay) => updatePreferences({ usageDisplay: value });
301+
296302
const value = useMemo(
297303
(): AppearancePreferencesContextValue => ({
298304
appearance,
@@ -317,6 +323,8 @@ export function AppearancePreferencesProvider(props: { readonly children: ReactN
317323
setCodeWordBreak,
318324
mathRenderingEnabled,
319325
setMathRenderingEnabled,
326+
usageDisplay,
327+
setUsageDisplay,
320328
}),
321329
[
322330
appearance,
@@ -340,6 +348,8 @@ export function AppearancePreferencesProvider(props: { readonly children: ReactN
340348
setCodeWordBreak,
341349
mathRenderingEnabled,
342350
setMathRenderingEnabled,
351+
usageDisplay,
352+
setUsageDisplay,
343353
],
344354
);
345355

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { SettingsSection } from "../../components/SettingsSection";
2+
import { SettingsSwitchRow } from "../../components/SettingsSwitchRow";
3+
import { useAppearancePreferences } from "../AppearancePreferencesProvider";
4+
5+
export function UsageAppearanceSection() {
6+
const { isReady, usageDisplay, setUsageDisplay } = useAppearancePreferences();
7+
8+
return (
9+
<SettingsSection title="Usage meters">
10+
<SettingsSwitchRow
11+
disabled={!isReady}
12+
icon="chart.bar.xaxis"
13+
label="Show % used"
14+
subtitle="Count usage limits up from 0% instead of down from 100%, in the app and its widgets."
15+
value={usageDisplay === "used"}
16+
onValueChange={(value) => setUsageDisplay(value ? "used" : "left")}
17+
/>
18+
</SettingsSection>
19+
);
20+
}

‎apps/mobile/src/features/settings/settings-search.test.ts‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,13 @@ describe("filterSettingsSections", () => {
5858
query: "accounts",
5959
expected: [["Server settings", ["Provider accounts"]]],
6060
},
61-
{ query: "usage", expected: [[null, ["Usage"]]] },
61+
{
62+
query: "usage",
63+
expected: [
64+
[null, ["Usage"]],
65+
["Interface", ["Appearance"]],
66+
],
67+
},
6268
{ query: "zzzz", expected: [] },
6369
])("$query", ({ query, expected }) => {
6470
expect(visible(query)).toEqual(expected);

‎apps/mobile/src/features/settings/settings-search.ts‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,11 @@ const SETTINGS_SEARCH_TERMS = {
2626
"terminal",
2727
"word break",
2828
"math",
29+
"usage meters",
30+
"usage limits",
31+
"count up",
32+
"percent used",
33+
"percent left",
2934
],
3035
SettingsNavigation: ["tabs", "tab order", "reorder", "hide tabs"],
3136
SettingsKeyboard: ["return key", "send message", "new line", "hardware keyboard"],

‎apps/mobile/src/features/usage/UsageLimitsPooled.tsx‎

Lines changed: 42 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,12 @@ import {
1010
cursorUsageWindowDetails,
1111
displayLimitWindows,
1212
formatResetsIn,
13-
paceRemainingPercent,
14-
remainingPercent,
13+
meterPercent,
14+
paceMarkLabel,
15+
paceMarkPercent,
16+
poolMeterPercent,
17+
refillDelta,
18+
refillSpan,
1519
type LimitAccount,
1620
type LimitPoolWindow,
1721
} from "@t3tools/shared/usageLimits";
@@ -26,6 +30,7 @@ import { AppText as Text } from "../../components/ui/AppText";
2630
import { ProviderIcon } from "../../components/ProviderIcon";
2731
import { SettingsScreen } from "../settings/components/SettingsScreen";
2832
import { environmentPresentations } from "../../state/presentation";
33+
import { useAppearancePreferences } from "../settings/appearance/AppearancePreferencesProvider";
2934
import { ResetCredits } from "./UsageLimitsSection";
3035
import { usageProviderKind, useProviderColors } from "./usageProviders";
3136

@@ -73,17 +78,15 @@ function AccountChip({ email }: { readonly email: string }) {
7378

7479
// Same gap-and-tick as the Limits rows: two clipped copies of the segment leave a see-through gap.
7580
function PaceGap({
76-
timeLeft,
81+
mark,
7782
children,
7883
}: {
79-
readonly timeLeft: number | null;
84+
readonly mark: number | null;
8085
readonly children: ReactNode;
8186
}) {
8287
const [width, setWidth] = useState(0);
8388
const tickX =
84-
timeLeft === null || width < 6
85-
? null
86-
: Math.min(Math.max((width * timeLeft) / 100, 3), width - 3);
89+
mark === null || width < 6 ? null : Math.min(Math.max((width * mark) / 100, 3), width - 3);
8790
return (
8891
<View
8992
pointerEvents="none"
@@ -119,13 +122,13 @@ function PaceGap({
119122
}
120123

121124
function AccountSegment({
122-
remaining,
125+
percent,
123126
color,
124-
refillPercent,
127+
refill,
125128
}: {
126-
readonly remaining: number;
129+
readonly percent: number;
127130
readonly color: string;
128-
readonly refillPercent: number;
131+
readonly refill: ReturnType<typeof refillSpan>;
129132
}) {
130133
const patternId = useId().replace(/:/g, "");
131134
const [size, setSize] = useState({ width: 0, height: 0 });
@@ -148,15 +151,15 @@ function AccountSegment({
148151
<Path d="M-1 1L1 -1M0 6L6 0M5 7L7 5" stroke={color} strokeWidth={1} opacity={0.22} />
149152
</Pattern>
150153
</Defs>
151-
{refillPercent > 0 ? (
154+
<Rect width={`${percent}%`} height="100%" fill={color} opacity={0.35} />
155+
{refill ? (
152156
<Rect
153-
x={`${100 - Math.min(100 - remaining, refillPercent)}%`}
154-
width={`${Math.min(100 - remaining, refillPercent)}%`}
157+
x={`${refill.start}%`}
158+
width={`${refill.width}%`}
155159
height="100%"
156160
fill={`url(#${patternId})`}
157161
/>
158162
) : null}
159-
<Rect width={`${remaining}%`} height="100%" fill={color} opacity={0.35} />
160163
</Svg>
161164
) : null}
162165
</View>
@@ -181,6 +184,7 @@ function PoolWindowCard({
181184
readonly wide: boolean;
182185
}) {
183186
const navigation = useNavigation();
187+
const { usageDisplay } = useAppearancePreferences();
184188
const [barWidth, setBarWidth] = useState(0);
185189
// Each segment needs room for its account, percent and reset to label itself.
186190
const segmentWidth = barWidth / Math.max(pool.columns.length, 1);
@@ -203,9 +207,9 @@ function PoolWindowCard({
203207
<Text className="text-sm font-t3-medium text-foreground">{label ?? pool.label}</Text>
204208
<View className="flex-row items-baseline gap-1.5">
205209
<Text className="text-3xl font-t3-bold tabular-nums text-foreground">
206-
{pool.remainingPercent}%
210+
{poolMeterPercent(pool, usageDisplay)}%
207211
</Text>
208-
<Text className="text-sm text-foreground-muted">left</Text>
212+
<Text className="text-sm text-foreground-muted">{usageDisplay}</Text>
209213
</View>
210214
</View>
211215
{pool.pace ? (
@@ -215,7 +219,7 @@ function PoolWindowCard({
215219
{description ? <Text className="text-xs text-foreground-muted">{description}</Text> : null}
216220
{nextRefill ? (
217221
<Text className="text-xs tabular-nums text-foreground-muted">
218-
↻ +{nextRefill.restoresPercent}%{" "}
222+
↻ {refillDelta(nextRefill.restoresPercent, usageDisplay)}{" "}
219223
{formatResetsIn(nextRefill.member.window, now)
220224
?.replace("resets in ", "in ")
221225
.replace("resets now", "now")}
@@ -237,25 +241,30 @@ function PoolWindowCard({
237241
/>
238242
);
239243
const { account, window } = member;
240-
const timeLeft = paceRemainingPercent(window, now);
244+
const percent = meterPercent(window.usedPercent, usageDisplay);
245+
const mark = paceMarkPercent(window, now, usageDisplay);
241246
const credits = account.limits.resetCredits?.availableCount ?? 0;
242247
const resetsIn = formatResetsIn(window, now)?.replace("resets in ", "");
243248
return (
244249
<Pressable
245250
key={account.key}
246251
accessibilityRole="button"
247-
accessibilityLabel={`Segment ${index + 1}, ${accountName(account)}, ${remainingPercent(window)}% left${timeLeft === null ? "" : `, ${timeLeft}% of the window left`}`}
252+
accessibilityLabel={`Segment ${index + 1}, ${accountName(account)}, ${percent}% ${usageDisplay}${mark === null ? "" : `, ${paceMarkLabel(mark, usageDisplay)}`}`}
248253
accessibilityHint="Show account details"
249254
onPress={() => openAccount(account)}
250255
className={wide ? "h-9 min-w-0 flex-1 rounded-md" : "h-7 min-w-0 flex-1 rounded-md"}
251256
>
252-
<PaceGap timeLeft={timeLeft}>
257+
<PaceGap mark={mark}>
253258
<View className="absolute inset-0 rounded-md bg-subtle" />
254259
<View className="absolute inset-0 overflow-hidden rounded-md">
255260
<AccountSegment
256-
remaining={remainingPercent(window)}
261+
percent={percent}
257262
color={normalizeProviderAccentColor(account.accentColor) ?? color}
258-
refillPercent={window.resetsAt ? member.refillPercent : 0}
263+
refill={
264+
window.resetsAt
265+
? refillSpan(window.usedPercent, member.refillPercent, usageDisplay)
266+
: null
267+
}
259268
/>
260269
</View>
261270
</PaceGap>
@@ -275,7 +284,7 @@ function PoolWindowCard({
275284
</Text>
276285
)}
277286
<Text className="text-sm font-t3-bold tabular-nums text-foreground">
278-
{remainingPercent(window)}%
287+
{percent}%
279288
</Text>
280289
</View>
281290
{inlineReset && (resetsIn || credits) ? (
@@ -328,11 +337,12 @@ function PoolWindowCard({
328337
const segmentColor = normalizeProviderAccentColor(account.accentColor) ?? color;
329338
const credits = account.limits.resetCredits?.availableCount ?? 0;
330339
const resetsIn = formatResetsIn(window, now);
340+
const percent = meterPercent(window.usedPercent, usageDisplay);
331341
return (
332342
<Pressable
333343
key={account.key}
334344
accessibilityRole="button"
335-
accessibilityLabel={`Segment ${index + 1}, ${accountName(account)}, ${remainingPercent(window)}% left${resetsIn ? `, ${resetsIn}` : ""}${credits ? `, ${credits} reset credits banked` : ""}`}
345+
accessibilityLabel={`Segment ${index + 1}, ${accountName(account)}, ${percent}% ${usageDisplay}${resetsIn ? `, ${resetsIn}` : ""}${credits ? `, ${credits} reset credits banked` : ""}`}
336346
accessibilityHint="Show account details"
337347
onPress={() => openAccount(account)}
338348
className="min-h-[44px] flex-row items-center gap-2 active:opacity-60"
@@ -353,9 +363,7 @@ function PoolWindowCard({
353363
>
354364
{accountName(account)}
355365
</Text>
356-
<Text className="text-sm font-t3-medium tabular-nums text-foreground">
357-
{remainingPercent(window)}%
358-
</Text>
366+
<Text className="text-sm font-t3-medium tabular-nums text-foreground">{percent}%</Text>
359367
<View className="flex-row items-center gap-1">
360368
{resetsIn ? (
361369
<>
@@ -539,7 +547,8 @@ export function UsageLimitAccountScreen({ route }: AccountScreenProps) {
539547
.find((candidate) => candidate.driver === account?.driver)
540548
?.windows.find((candidate) => candidate.id === windowId && candidate.kind === windowKind);
541549
const window = pool?.members.find((member) => member.account.key === accountKey)?.window;
542-
const evenPace = window ? paceRemainingPercent(window, now) : null;
550+
const { usageDisplay } = useAppearancePreferences();
551+
const evenPace = window ? paceMarkPercent(window, now, usageDisplay) : null;
543552
const reset = pool?.resets.find((candidate) => candidate.member.account.key === accountKey);
544553
const [revealed, setRevealed] = useState(false);
545554
return (
@@ -583,7 +592,7 @@ export function UsageLimitAccountScreen({ route }: AccountScreenProps) {
583592
<View className="gap-3 rounded-[24px] border-continuous bg-grouped-card p-4">
584593
<Text className="text-sm font-t3-medium text-foreground">{window.label}</Text>
585594
<Text className="text-3xl font-t3-bold tabular-nums text-foreground">
586-
{remainingPercent(window)}% left
595+
{meterPercent(window.usedPercent, usageDisplay)}% {usageDisplay}
587596
</Text>
588597
{window.resetsAt ? (
589598
<Text selectable className="text-sm text-foreground-muted">
@@ -596,12 +605,13 @@ export function UsageLimitAccountScreen({ route }: AccountScreenProps) {
596605
) : null}
597606
{evenPace !== null ? (
598607
<Text className="text-sm text-foreground-muted">
599-
Even pace: {evenPace}% left by now
608+
Even pace: {evenPace}% {usageDisplay} by now
600609
</Text>
601610
) : null}
602611
{reset && reset.restoresPercent > 0 ? (
603612
<Text className="text-sm text-foreground-muted">
604-
Restores {reset.restoresPercent}% of the pool
613+
{usageDisplay === "used" ? "Clears" : "Restores"} {reset.restoresPercent}% of the
614+
pool
605615
</Text>
606616
) : null}
607617
</View>

‎apps/mobile/src/features/usage/UsageLimitsSection.tsx‎

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ import {
1414
formatDuration,
1515
formatResetsIn,
1616
limitsNotice,
17+
meterPercent,
18+
paceMarkPercent,
1719
paceOf,
18-
paceRemainingPercent,
19-
remainingPercent,
2020
} from "@t3tools/shared/usageLimits";
2121
import { type ReactNode, useContext, useRef, useState } from "react";
2222
import { Atom } from "effect/reactivity";
@@ -29,6 +29,7 @@ import { serverEnvironment } from "../../state/server";
2929
import { readEnvironmentScope, useEnvironmentScope } from "../../state/session";
3030
import { useAtomCommand } from "../../state/use-atom-command";
3131
import { useNowMinuteMs } from "../../lib/clock";
32+
import { useAppearancePreferences } from "../settings/appearance/AppearancePreferencesProvider";
3233
import {
3334
environmentIdsKey,
3435
limitsAutoRefreshSetAtom,
@@ -53,15 +54,17 @@ function WindowRow(props: {
5354
readonly now: number;
5455
}) {
5556
const { window, now } = props;
56-
const remaining = remainingPercent(window);
57-
const timeLeft = paceRemainingPercent(window, now);
57+
const { usageDisplay } = useAppearancePreferences();
58+
const remaining = meterPercent(window.usedPercent, "left");
59+
const percent = meterPercent(window.usedPercent, usageDisplay);
60+
const mark = paceMarkPercent(window, now, usageDisplay);
5861
const pace = paceOf(window, now);
5962
const resetsIn = formatResetsIn(window, now);
6063
const [barWidth, setBarWidth] = useState(0);
6164
const tickX =
62-
timeLeft === null || barWidth < 6
65+
mark === null || barWidth < 6
6366
? null
64-
: Math.min(Math.max((barWidth * timeLeft) / 100, 3), barWidth - 3);
67+
: Math.min(Math.max((barWidth * mark) / 100, 3), barWidth - 3);
6568
const bar = (
6669
<View
6770
className="h-1.5 flex-row overflow-hidden rounded-full bg-subtle"
@@ -76,19 +79,19 @@ function WindowRow(props: {
7679
: "h-full rounded-full bg-foreground"
7780
}
7881
style={[
79-
{ flex: remaining },
82+
{ flex: percent },
8083
remaining > 30 && props.color ? { backgroundColor: props.color } : null,
8184
]}
8285
/>
83-
<View style={{ flex: 100 - remaining }} />
86+
<View style={{ flex: 100 - percent }} />
8487
</View>
8588
);
8689
return (
8790
<View className="gap-1">
8891
<View className="flex-row items-baseline justify-between gap-3">
8992
<Text className="text-sm text-foreground">{window.label}</Text>
9093
<Text className="text-sm font-t3-medium tabular-nums text-foreground">
91-
{remaining}% left
94+
{percent}% {usageDisplay}
9295
</Text>
9396
</View>
9497
<View

0 commit comments

Comments
 (0)