Skip to content

Commit d19026d

Browse files
authored
Merge 334a972 into 8560086
2 parents 8560086 + 334a972 commit d19026d

7 files changed

Lines changed: 158 additions & 66 deletions

File tree

‎src/OpenClaw.Tray.WinUI/Pages/UsagePage.xaml‎

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@
44
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
55
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
66

7-
<ScrollViewer VerticalScrollBarVisibility="Auto">
7+
<ScrollViewer VerticalScrollBarVisibility="Auto" HorizontalScrollBarVisibility="Disabled">
88
<Grid HorizontalAlignment="Stretch">
99
<StackPanel Padding="24" Spacing="16" HorizontalAlignment="Stretch" MaxWidth="900">
1010

11-
<TextBlock x:Uid="UsagePage_TextBlock_10" Text="📊 Usage &amp; Cost" Style="{StaticResource TitleTextBlockStyle}"/>
11+
<TextBlock x:Uid="UsagePage_TextBlock_10" Text="Usage &amp; Cost" Style="{StaticResource TitleTextBlockStyle}"/>
1212

1313
<InfoBar x:Uid="UsagePage_GatewayDisconnected" x:Name="ConnectionInfoBar"
1414
IsOpen="False" IsClosable="False" Severity="Warning"
@@ -27,43 +27,55 @@
2727
<TextBlock x:Uid="UsagePage_TextBlock_21" Text="Total Cost" Style="{StaticResource CaptionTextBlockStyle}"
2828
Foreground="{ThemeResource TextFillColorSecondaryBrush}"
2929
HorizontalAlignment="Center"/>
30-
<TextBlock x:Name="TotalCostText" Text="—" FontSize="28" FontWeight="SemiBold"
30+
<TextBlock x:Name="TotalCostText" Text="—"
31+
Style="{StaticResource TitleLargeTextBlockStyle}"
3132
HorizontalAlignment="Center"/>
3233
</StackPanel>
3334
</Border>
3435

35-
<!-- Stats row: 3 cards side by side -->
36-
<StackPanel Orientation="Horizontal" Spacing="12">
37-
<Border Background="{ThemeResource CardBackgroundFillColorDefaultBrush}"
36+
<!-- Stats row: 3 equal cards that grow with available width. -->
37+
<Grid ColumnSpacing="12">
38+
<Grid.ColumnDefinitions>
39+
<ColumnDefinition Width="*"/>
40+
<ColumnDefinition Width="*"/>
41+
<ColumnDefinition Width="*"/>
42+
</Grid.ColumnDefinitions>
43+
<Border Grid.Column="0"
44+
Background="{ThemeResource CardBackgroundFillColorDefaultBrush}"
3845
BorderBrush="{ThemeResource CardStrokeColorDefaultBrush}"
3946
BorderThickness="1" CornerRadius="8" Padding="16" MinWidth="150">
4047
<StackPanel Spacing="4">
4148
<TextBlock x:Uid="UsagePage_TextBlock_35" Text="Requests" Style="{StaticResource CaptionTextBlockStyle}"
4249
Foreground="{ThemeResource TextFillColorSecondaryBrush}"/>
43-
<TextBlock x:Name="RequestCountText" Text="—" FontSize="28" FontWeight="SemiBold"/>
50+
<TextBlock x:Name="RequestCountText" Text="—"
51+
Style="{StaticResource SubtitleTextBlockStyle}"/>
4452
</StackPanel>
4553
</Border>
4654

47-
<Border Background="{ThemeResource CardBackgroundFillColorDefaultBrush}"
55+
<Border Grid.Column="1"
56+
Background="{ThemeResource CardBackgroundFillColorDefaultBrush}"
4857
BorderBrush="{ThemeResource CardStrokeColorDefaultBrush}"
4958
BorderThickness="1" CornerRadius="8" Padding="16" MinWidth="150">
5059
<StackPanel Spacing="4">
5160
<TextBlock x:Uid="UsagePage_TextBlock_45" Text="Tokens" Style="{StaticResource CaptionTextBlockStyle}"
5261
Foreground="{ThemeResource TextFillColorSecondaryBrush}"/>
53-
<TextBlock x:Uid="TokenCountText" x:Name="TokenCountText" Text="—" FontSize="28" FontWeight="SemiBold"/>
62+
<TextBlock x:Name="TokenCountText" Text="—"
63+
Style="{StaticResource SubtitleTextBlockStyle}"/>
5464
</StackPanel>
5565
</Border>
5666

57-
<Border Background="{ThemeResource CardBackgroundFillColorDefaultBrush}"
67+
<Border Grid.Column="2"
68+
Background="{ThemeResource CardBackgroundFillColorDefaultBrush}"
5869
BorderBrush="{ThemeResource CardStrokeColorDefaultBrush}"
5970
BorderThickness="1" CornerRadius="8" Padding="16" MinWidth="150">
6071
<StackPanel Spacing="4">
6172
<TextBlock x:Uid="UsagePage_TextBlock_55" Text="Providers" Style="{StaticResource CaptionTextBlockStyle}"
6273
Foreground="{ThemeResource TextFillColorSecondaryBrush}"/>
63-
<TextBlock x:Name="ProviderCountText" Text="—" FontSize="28" FontWeight="SemiBold"/>
74+
<TextBlock x:Name="ProviderCountText" Text="—"
75+
Style="{StaticResource SubtitleTextBlockStyle}"/>
6476
</StackPanel>
6577
</Border>
66-
</StackPanel>
78+
</Grid>
6779

6880
<!-- Provider Breakdown card — real-time provider status (independent of the time range below). -->
6981
<Border Background="{ThemeResource CardBackgroundFillColorDefaultBrush}"

‎src/OpenClaw.Tray.WinUI/Pages/UsagePage.xaml.cs‎

Lines changed: 129 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,24 @@ public sealed partial class UsagePage : Page
1414
{
1515
private static App CurrentApp => (App)Microsoft.UI.Xaml.Application.Current;
1616
private AppState? _appState;
17+
private IOperatorGatewayClient? _trackedClient;
1718
// Default matches the XAML-selected Period7DaysItem (IsSelected="True").
1819
private int _currentPeriodDays = 7;
1920
private readonly AsyncListLoadingState _providerLoading = new();
2021
private readonly AsyncListLoadingState _dailyCostLoading = new();
2122
private DateTime _lastAppliedUsageCostUpdatedAtUtc = DateTime.MinValue;
2223

24+
private const string DailyEmptyMessage = "No daily usage for this period";
25+
private const string ProviderEmptyMessage = "No providers configured";
26+
private const string DisconnectedListMessage = "Couldn't load. Check your gateway connection.";
27+
2328
public UsagePage()
2429
{
2530
InitializeComponent();
2631
Unloaded += (_, _) =>
2732
{
2833
if (_appState != null) _appState.PropertyChanged -= OnAppStateChanged;
34+
DetachClient();
2935
};
3036
}
3137

@@ -34,40 +40,95 @@ public void Initialize()
3440
if (_appState != null) _appState.PropertyChanged -= OnAppStateChanged;
3541
_appState = CurrentApp.AppState;
3642
_appState.PropertyChanged += OnAppStateChanged;
43+
3744
var client = CurrentApp.GatewayClient;
38-
if (client != null)
39-
{
40-
ConnectionInfoBar.IsOpen = false;
41-
// Apply cached data immediately, then request fresh.
42-
if (_appState?.Usage != null) UpdateUsage(_appState.Usage);
43-
// Only apply cached cost data when its period matches the current
44-
// selection — otherwise the daily list briefly shows e.g. 30-day
45-
// data while the selector reads "7 Days".
46-
if (_appState?.UsageCost != null && _appState.UsageCost.Days == _currentPeriodDays)
47-
{
48-
UpdateUsageCost(_appState.UsageCost);
49-
_dailyCostLoading.BeginRefresh();
50-
}
51-
else
52-
{
53-
_dailyCostLoading.BeginInitialRefresh();
54-
}
55-
if (_appState?.UsageStatus != null) UpdateUsageStatus(_appState.UsageStatus);
56-
else _providerLoading.BeginInitialRefresh();
57-
UpdateDailyCostLoadingVisuals();
58-
UpdateProviderLoadingVisuals();
59-
_ = client.RequestUsageAsync();
60-
_ = client.RequestUsageCostAsync(_currentPeriodDays);
61-
_ = client.RequestUsageStatusAsync();
62-
}
63-
else
45+
AttachClient(client);
46+
47+
// A non-null client may still be disconnected (e.g. WebSocket reconnecting).
48+
// The underlying RequestUsage*/RequestUsageCost*/RequestUsageStatus calls
49+
// silently no-op when !IsConnectedToGateway, so without this check the
50+
// page would spin its progress rings forever — see user reports of
51+
// "page not loading, no values".
52+
if (client == null || !client.IsConnectedToGateway)
6453
{
6554
_providerLoading.Fail();
6655
_dailyCostLoading.Fail();
6756
ShowDisconnected();
6857
UpdateProviderLoadingVisuals();
6958
UpdateDailyCostLoadingVisuals();
59+
return;
60+
}
61+
62+
ConnectionInfoBar.IsOpen = false;
63+
// Apply cached data immediately, then request fresh.
64+
if (_appState?.Usage != null) UpdateUsage(_appState.Usage);
65+
// Only apply cached cost data when its period matches the current
66+
// selection — otherwise the daily list briefly shows e.g. 30-day
67+
// data while the selector reads "7 Days".
68+
if (_appState?.UsageCost != null && _appState.UsageCost.Days == _currentPeriodDays)
69+
{
70+
UpdateUsageCost(_appState.UsageCost);
71+
_dailyCostLoading.BeginRefresh();
7072
}
73+
else
74+
{
75+
_dailyCostLoading.BeginInitialRefresh();
76+
}
77+
if (_appState?.UsageStatus != null) UpdateUsageStatus(_appState.UsageStatus);
78+
else _providerLoading.BeginInitialRefresh();
79+
UpdateDailyCostLoadingVisuals();
80+
UpdateProviderLoadingVisuals();
81+
RequestRefresh(client);
82+
}
83+
84+
private void AttachClient(IOperatorGatewayClient? client)
85+
{
86+
if (ReferenceEquals(_trackedClient, client)) return;
87+
DetachClient();
88+
_trackedClient = client;
89+
if (_trackedClient != null)
90+
{
91+
_trackedClient.StatusChanged += OnClientStatusChanged;
92+
}
93+
}
94+
95+
private void DetachClient()
96+
{
97+
if (_trackedClient != null)
98+
{
99+
_trackedClient.StatusChanged -= OnClientStatusChanged;
100+
_trackedClient = null;
101+
}
102+
}
103+
104+
private void OnClientStatusChanged(object? sender, ConnectionStatus status)
105+
{
106+
// Recover automatically when the gateway comes online while the page
107+
// is open (otherwise the user is stuck on the disconnected info bar
108+
// with stale cards until they navigate away and back).
109+
if (sender is not IOperatorGatewayClient client) return;
110+
if (!client.IsConnectedToGateway) return;
111+
112+
var dispatcher = DispatcherQueue;
113+
if (dispatcher == null) return;
114+
dispatcher.TryEnqueue(() =>
115+
{
116+
if (_trackedClient != client) return;
117+
ConnectionInfoBar.IsOpen = false;
118+
if (!_dailyCostLoading.HasLoaded) _dailyCostLoading.BeginInitialRefresh();
119+
else _dailyCostLoading.BeginRefresh();
120+
if (!_providerLoading.HasLoaded) _providerLoading.BeginInitialRefresh();
121+
UpdateDailyCostLoadingVisuals();
122+
UpdateProviderLoadingVisuals();
123+
RequestRefresh(client);
124+
});
125+
}
126+
127+
private void RequestRefresh(IOperatorGatewayClient client)
128+
{
129+
_ = client.RequestUsageAsync();
130+
_ = client.RequestUsageCostAsync(_currentPeriodDays);
131+
_ = client.RequestUsageStatusAsync();
71132
}
72133

73134
private void OnOpenConnectionClick(object sender, RoutedEventArgs e)
@@ -100,13 +161,24 @@ public void UpdateUsage(GatewayUsageInfo usage)
100161

101162
public void UpdateUsageCost(GatewayCostUsageInfo cost)
102163
{
103-
if (cost.Days != _currentPeriodDays)
104-
return;
105-
106164
if (cost.UpdatedAt < _lastAppliedUsageCostUpdatedAtUtc)
107165
return;
108166

167+
// The Windows tray fires usage.cost twice per refresh: once via
168+
// RequestUsageAsync() (always days=30) and once directly via the
169+
// selector (currently 7 or 30). If the gateway ignores the `days`
170+
// request param or only replies to one of the two, the page used to
171+
// reject the response that didn't match _currentPeriodDays and the
172+
// spinner ran forever. Accept whatever days the server returns and,
173+
// when the selector is out of sync, silently snap it to that period
174+
// so the header isn't lying about what data the user sees.
175+
if (cost.Days > 0 && cost.Days != _currentPeriodDays)
176+
{
177+
SyncSelectorToServerDays(cost.Days);
178+
}
179+
109180
_lastAppliedUsageCostUpdatedAtUtc = cost.UpdatedAt;
181+
ConnectionInfoBar.IsOpen = false;
110182
TotalCostText.Text = $"${cost.Totals.TotalCost:F2}";
111183
TokenCountText.Text = FormatLargeNumber(cost.Totals.TotalTokens);
112184

@@ -119,8 +191,24 @@ public void UpdateUsageCost(GatewayCostUsageInfo cost)
119191
UpdateDailyCostLoadingVisuals();
120192
}
121193

194+
private void SyncSelectorToServerDays(int days)
195+
{
196+
// Only the two SelectorBar items are valid targets; ignore anything
197+
// else (e.g. server-defaulted 14 days) and just keep the user's pick.
198+
if (days != 7 && days != 30) return;
199+
_currentPeriodDays = days;
200+
var target = days == 30 ? Period30DaysItem : Period7DaysItem;
201+
if (!ReferenceEquals(PeriodSelector.SelectedItem, target))
202+
{
203+
PeriodSelector.SelectionChanged -= OnPeriodSelectionChanged;
204+
try { PeriodSelector.SelectedItem = target; }
205+
finally { PeriodSelector.SelectionChanged += OnPeriodSelectionChanged; }
206+
}
207+
}
208+
122209
public void UpdateUsageStatus(GatewayUsageStatusInfo status)
123210
{
211+
ConnectionInfoBar.IsOpen = false;
124212
ProviderCountText.Text = status.Providers.Count.ToString();
125213
ProviderListView.ItemsSource = status.Providers.Select(p => new ProviderRow
126214
{
@@ -130,7 +218,6 @@ public void UpdateUsageStatus(GatewayUsageStatusInfo status)
130218
Status = p.Error ?? "",
131219
}).ToList();
132220

133-
bool hasProviders = status.Providers.Count > 0;
134221
_providerLoading.Complete(status.Providers.Count);
135222
UpdateProviderLoadingVisuals();
136223
}
@@ -152,9 +239,10 @@ private void SelectPeriod(int days)
152239
_dailyCostLoading.BeginInitialRefresh();
153240
UpdateDailyCostLoadingVisuals();
154241

155-
if (CurrentApp.GatewayClient != null)
242+
var client = CurrentApp.GatewayClient;
243+
if (client != null && client.IsConnectedToGateway)
156244
{
157-
_ = CurrentApp.GatewayClient.RequestUsageCostAsync(days);
245+
_ = client.RequestUsageCostAsync(days);
158246
}
159247
else
160248
{
@@ -168,15 +256,22 @@ private void UpdateDailyCostLoadingVisuals()
168256
{
169257
DailyLoadingPanel.Visibility = _dailyCostLoading.ShouldShowLoading ? Visibility.Visible : Visibility.Collapsed;
170258
DailyListView.Visibility = _dailyCostLoading.ShouldShowContent ? Visibility.Visible : Visibility.Collapsed;
171-
DailyEmptyText.Visibility = _dailyCostLoading.ShouldShowEmpty ? Visibility.Visible : Visibility.Collapsed;
259+
// After Fail() the state is !IsRefreshing && !HasLoaded, which leaves the
260+
// card visually empty (no spinner, no rows, no message). Surface a
261+
// message in that case so the page never looks frozen.
262+
bool failed = !_dailyCostLoading.IsRefreshing && !_dailyCostLoading.HasLoaded;
263+
DailyEmptyText.Text = failed ? DisconnectedListMessage : DailyEmptyMessage;
264+
DailyEmptyText.Visibility = (_dailyCostLoading.ShouldShowEmpty || failed) ? Visibility.Visible : Visibility.Collapsed;
172265
PeriodSelector.IsEnabled = _dailyCostLoading.CanEdit;
173266
}
174267

175268
private void UpdateProviderLoadingVisuals()
176269
{
177270
ProviderLoadingPanel.Visibility = _providerLoading.ShouldShowLoading ? Visibility.Visible : Visibility.Collapsed;
178271
ProviderListView.Visibility = _providerLoading.ShouldShowContent ? Visibility.Visible : Visibility.Collapsed;
179-
ProviderEmptyText.Visibility = _providerLoading.ShouldShowEmpty ? Visibility.Visible : Visibility.Collapsed;
272+
bool failed = !_providerLoading.IsRefreshing && !_providerLoading.HasLoaded;
273+
ProviderEmptyText.Text = failed ? DisconnectedListMessage : ProviderEmptyMessage;
274+
ProviderEmptyText.Visibility = (_providerLoading.ShouldShowEmpty || failed) ? Visibility.Visible : Visibility.Collapsed;
180275
}
181276

182277
private void ShowDisconnected()

‎src/OpenClaw.Tray.WinUI/Strings/en-us/Resources.resw‎

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2154,9 +2154,6 @@ On your gateway host (Mac/Linux), run:
21542154
<data name="UsagePage_TextBlock_45.Text" xml:space="preserve">
21552155
<value>Tokens</value>
21562156
</data>
2157-
<data name="TokenCountText.Text" xml:space="preserve">
2158-
<value>284.5K</value>
2159-
</data>
21602157
<data name="UsagePage_TextBlock_55.Text" xml:space="preserve">
21612158
<value>Providers</value>
21622159
</data>
@@ -2416,7 +2413,7 @@ On your gateway host (Mac/Linux), run:
24162413
<value>OpenClaw Menu</value>
24172414
</data>
24182415
<data name="UsagePage_TextBlock_10.Text" xml:space="preserve">
2419-
<value>📊 Usage &amp; Cost</value>
2416+
<value>Usage &amp; Cost</value>
24202417
</data>
24212418
<data name="ChatWindowOpenInBrowserToolTip.Content" xml:space="preserve">
24222419
<value>Open in Companion app</value>

‎src/OpenClaw.Tray.WinUI/Strings/fr-fr/Resources.resw‎

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2106,9 +2106,6 @@ Sur votre hôte passerelle (Mac/Linux), exécutez :
21062106
<data name="UsagePage_TextBlock_45.Text" xml:space="preserve">
21072107
<value>Jetons</value>
21082108
</data>
2109-
<data name="TokenCountText.Text" xml:space="preserve">
2110-
<value>284.5K</value>
2111-
</data>
21122109
<data name="UsagePage_TextBlock_55.Text" xml:space="preserve">
21132110
<value>Fournisseurs</value>
21142111
</data>
@@ -2368,7 +2365,7 @@ Sur votre hôte passerelle (Mac/Linux), exécutez :
23682365
<value>OpenClaw Menu</value>
23692366
</data>
23702367
<data name="UsagePage_TextBlock_10.Text" xml:space="preserve">
2371-
<value>📊 Utilisation et coût</value>
2368+
<value>Utilisation et coût</value>
23722369
</data>
23732370
<data name="ChatWindowOpenInBrowserToolTip.Content" xml:space="preserve">
23742371
<value>Ouvrir dans le navigateur</value>

‎src/OpenClaw.Tray.WinUI/Strings/nl-nl/Resources.resw‎

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2107,9 +2107,6 @@ Voer op uw gateway-host (Mac/Linux) uit:
21072107
<data name="UsagePage_TextBlock_45.Text" xml:space="preserve">
21082108
<value>Toegangstokens</value>
21092109
</data>
2110-
<data name="TokenCountText.Text" xml:space="preserve">
2111-
<value>284.5K</value>
2112-
</data>
21132110
<data name="UsagePage_TextBlock_55.Text" xml:space="preserve">
21142111
<value>Aanbieders</value>
21152112
</data>
@@ -2369,7 +2366,7 @@ Voer op uw gateway-host (Mac/Linux) uit:
23692366
<value>OpenClaw Menu</value>
23702367
</data>
23712368
<data name="UsagePage_TextBlock_10.Text" xml:space="preserve">
2372-
<value>📊 Gebruik en kosten</value>
2369+
<value>Gebruik en kosten</value>
23732370
</data>
23742371
<data name="ChatWindowOpenInBrowserToolTip.Content" xml:space="preserve">
23752372
<value>Openen in browser</value>

0 commit comments

Comments
 (0)