Skip to content

Commit d2638dd

Browse files
bkudiessCopilot
andauthored
refactor(tray): extract Settings page into a WinUI-free view model over a settings store
Moves Settings page read and persistence logic into a WinUI-free view model backed by a settings store, wires navigation activation, and adds behavioral coverage for the new ownership seams. Co-authored-by: Copilot App <[email protected]>
1 parent 76f4d52 commit d2638dd

22 files changed

Lines changed: 1504 additions & 357 deletions

‎docs/ARCHITECTURE.md‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ These are the canonical homes. Do not reintroduce private copies elsewhere.
5757
| UI-thread marshaling for presentation code | `IUiDispatcher` | authoritative |
5858
| Page view-model activation/deactivation + disposal lifetime | `NavigationScopeManager` | authoritative |
5959
| Presentation-layer DI composition root | `AppServiceRegistration` (root `ServiceProvider`, owned by `App`) | authoritative |
60+
| Settings snapshot read + batched save + non-echoing change notification | `ISettingsStore` | authoritative |
61+
| Settings page load/persist view logic | `SettingsPageViewModel` | authoritative |
6062
| Capability UI metadata | `NodeCapabilityUiCatalog` (planned) | planned |
6163
| Capability registration/gating | `NodeCapabilityRegistrationPolicy` (planned) | planned |
6264
| Local MCP exposure policy | `McpCapabilityPolicy` (planned) | planned |
@@ -72,6 +74,7 @@ These are the canonical homes. Do not reintroduce private copies elsewhere.
7274
| `src/OpenClaw.Tray.WinUI/Chat/OpenClawChatTimeline.cs` | `TimelineScrollController`, `ChatBubbleRenderer`, `ToolCallCardRenderer`, `PermissionRequestCard`, `AttachmentBubbleRenderer` |
7375
| `src/OpenClaw.Tray.WinUI/Chat/OpenClawComposer.cs` | `ComposerViewModel`, `SlashCommandPalette`, `AttachmentPreviewStrip`, `VoiceComposerController` |
7476
| `src/OpenClaw.Tray.WinUI/Pages/ConnectionPage.xaml.cs` | `ConnectionPagePlan` (pure), `ConnectionPageViewModel`, gateway row models |
77+
| `src/OpenClaw.Tray.WinUI/Pages/SettingsPage.xaml.cs` | settings read/persist → `SettingsPageViewModel` + `ISettingsStore`; keep gateway-uninstall, uptime timer, saved-indicator, and app-info in the view |
7578
| `src/OpenClaw.Tray.WinUI/Services/NodeService.cs` | `McpServerHost`, `CanvasWindowManager`, `MediaCapabilityHost`, `RecordingConsentService`, `NodeCapabilityRegistry` |
7679
| `src/OpenClaw.Shared/OpenClawGatewayClient.cs` | `PendingRequestRegistry`, `ConnectEnvelopeBuilder`, `GatewayMessageRouter`, per-domain API facades |
7780
| `src/OpenClaw.Shared/Models.cs` | per-domain model files + `*Mapper` classes |
@@ -124,6 +127,8 @@ leading and trailing pipe. Columns, in order:
124127
| navigation-scope | authoritative | src/OpenClaw.Tray.WinUI/Windows/HubWindow.xaml.cs | page view-model activation/deactivation and disposal lifetime | NavigationScopeManager | HubWindow keeps frame navigation back-stack and rail selection | transient page view models are activated on navigation and deactivated then disposed on navigate-away | NavigationScopeManagerTests.NavigatingAway_DeactivatesAndDisposesPreviousViewModel | behavioral | - |
125128
| composition-root | authoritative | src/OpenClaw.Tray.WinUI/App.xaml.cs | presentation-layer service construction and wiring | AppServiceRegistration | App remains the composition root and owns non-DI service lifetimes | one validated root ServiceProvider; App-owned singletons registered as instances are never disposed by the container | AppServiceRegistrationTests.Dispose_DoesNotDisposeAppOwnedInstanceSingletons | behavioral | - |
126129
| node-summary-text | authoritative | src/OpenClaw.Tray.WinUI/App.xaml.cs | node-summary clipboard text formatting | NodeSummaryText | App keeps the clipboard side effect (building the DataPackage and setting clipboard content) | copied node-summary text is projected only by NodeSummaryText.Build (online/offline state, display-name fallback, short id, detail text, newline join) | NodeSummaryTextTests.Build_MultipleNodes_OneLinePerNodeJoinedByNewline | behavioral | - |
130+
| settings-store | authoritative | src/OpenClaw.Tray.WinUI/Pages/SettingsPage.xaml.cs | hand-rolled save/echo suppression flags for two-way settings binding | ISettingsStore | PermissionsPage and other surfaces may read SettingsManager directly until migrated | a save originating from Update does not echo Changed to the caller and external saves are republished on the UI thread | SettingsStoreTests.Update_DoesNotEchoChangedToSelf | behavioral | when all settings surfaces read and write through ISettingsStore |
131+
| settings-page-vm | authoritative | src/OpenClaw.Tray.WinUI/Pages/SettingsPage.xaml.cs | settings load, persist, echo-guard, and auto-save wiring | SettingsPageViewModel | code-behind keeps gateway-uninstall, gateway-info and uptime timer, saved-indicator visual, and app-info population | each settings control persists its field through the store preserving mutate-save-notify order and does not re-persist on external change | SettingsPageViewModelTests.ExternalChange_ReloadsWithoutRePersisting | behavioral | when the Settings page holds no settings persistence logic in code-behind |
127132
<!-- LEDGER:END -->
128133

129134
## Deferred test builders

‎src/OpenClaw.Tray.WinUI/App.xaml.cs‎

Lines changed: 63 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,16 +64,22 @@ public partial class App : Application, OpenClawTray.Services.IAppCommands
6464
private ServiceProvider? _services;
6565

6666
/// <summary>
67-
/// Page type → view-model type map used by the navigation activation hook.
68-
/// Currently empty: no page resolves a view model from DI yet, so the activation
69-
/// hook is a runtime no-op. Entries are added here as pages adopt view models.
67+
/// Page type → view-model type map used by the navigation activation hook. The Settings
68+
/// page resolves its view model from DI and binds it as the page DataContext; pages absent
69+
/// from the map take the no-op activation path.
7070
/// </summary>
7171
private static readonly IReadOnlyDictionary<Type, Type> PageViewModelMap =
72-
new Dictionary<Type, Type>();
72+
new Dictionary<Type, Type>
73+
{
74+
[typeof(Pages.SettingsPage)] = typeof(SettingsPageViewModel),
75+
};
7376

7477
/// <summary>The root service provider, or null before startup / after shutdown.</summary>
7578
internal IServiceProvider? Services => _services;
7679

80+
/// <summary>The settings facade, or null before startup / after shutdown.</summary>
81+
internal ISettingsStore? SettingsStore => _services?.GetService<ISettingsStore>();
82+
7783
/// <summary>Resolves the page activator used by <c>HubWindow</c>'s navigation hook.</summary>
7884
internal IPageActivator? PageActivator => _services?.GetService<IPageActivator>();
7985
/// <summary>
@@ -4208,6 +4214,33 @@ private async Task ToggleAutoStartAsync()
42084214
await AutoStartManager.SetAutoStartAsync(_settings.AutoStart);
42094215
}
42104216

4217+
/// <summary>
4218+
/// Persists the auto-start setting and applies the Windows OS registration in the original
4219+
/// order (save, then await the OS write, then notify). Returns true only when the OS write
4220+
/// and notify complete, so the caller shows its saved confirmation only on success. The save
4221+
/// is marked as a store self-write so it does not echo an external-change reload.
4222+
/// </summary>
4223+
public async Task<bool> ApplyAutoStart(bool autoStart)
4224+
{
4225+
if (_settings == null) return false;
4226+
try
4227+
{
4228+
_settings.AutoStart = autoStart;
4229+
using (SettingsStore?.BeginSelfWrite())
4230+
{
4231+
_settings.Save();
4232+
}
4233+
await AutoStartManager.SetAutoStartAsync(autoStart);
4234+
OnSettingsSaved(this, EventArgs.Empty);
4235+
return true;
4236+
}
4237+
catch (Exception ex)
4238+
{
4239+
Logger.Error($"ApplyAutoStart failed: {ex.Message}");
4240+
return false;
4241+
}
4242+
}
4243+
42114244
private void OpenLogFile()
42124245
{
42134246
try
@@ -4491,6 +4524,12 @@ public Task SpeakChatTextAsync(string text) =>
44914524
/// <summary>Raised when speaker mute state changes from any source (composer, settings, etc.).</summary>
44924525
public event Action<bool>? SpeakerMuteChanged;
44934526

4527+
/// <summary>
4528+
/// Sets speaker mute from any surface (chat window, chat page, voice settings) and persists it.
4529+
/// This public path is NOT store-self-write-suppressed, so an open Settings page still reflects
4530+
/// a mute toggled elsewhere. The Settings-page-originated call goes through the explicit
4531+
/// <see cref="IAppCommands.SetChatSpeakerMuted"/> below, which suppresses its own echo.
4532+
/// </summary>
44944533
public void SetChatSpeakerMuted(bool muted)
44954534
{
44964535
if (_chatCoordinator is { } c) c.IsMuted = muted;
@@ -4504,6 +4543,26 @@ public void SetChatSpeakerMuted(bool muted)
45044543
SpeakerMuteChanged?.Invoke(muted);
45054544
}
45064545

4546+
/// <summary>
4547+
/// Settings-page-originated mute: wraps the shared write in a store self-write so it does not
4548+
/// echo an external-change reload back to the Settings view model that triggered it.
4549+
/// </summary>
4550+
void IAppCommands.SetChatSpeakerMuted(bool muted)
4551+
{
4552+
using (SettingsStore?.BeginSelfWrite())
4553+
{
4554+
SetChatSpeakerMuted(muted);
4555+
}
4556+
}
4557+
4558+
/// <summary>
4559+
/// Pushes tool-call visibility into the live chat timeline. Forwards to the shared
4560+
/// static writer so a WinUI-free settings view model can drive it through IAppCommands
4561+
/// without referencing the chat UI directly.
4562+
/// </summary>
4563+
public void SetChatToolCallsVisible(bool visible) =>
4564+
OpenClawTray.Chat.OpenClawChatRoot.SetToolCallsVisible(visible);
4565+
45074566
private static void SendDeepLinkToRunningInstance(string uri)
45084567
{
45094568
try

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

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
TextWrapping="Wrap"/>
5353
</StackPanel>
5454
<ToggleSwitch Grid.Column="1" x:Name="AutoStartToggle" MinWidth="0" OnContent="" OffContent=""
55+
IsOn="{Binding AutoStart, Mode=TwoWay}"
5556
AutomationProperties.AutomationId="SettingsPageAutoStart"
5657
AutomationProperties.Name="Start with Windows"/>
5758
</Grid>
@@ -74,6 +75,8 @@
7475
TextWrapping="Wrap"/>
7576
</StackPanel>
7677
<ComboBox Grid.Column="1" x:Name="AppThemeComboBox" MinWidth="160"
78+
SelectedValuePath="Tag"
79+
SelectedValue="{Binding AppTheme, Mode=TwoWay}"
7780
AutomationProperties.AutomationId="SettingsPageAppTheme"
7881
AutomationProperties.Name="App theme">
7982
<ComboBoxItem x:Uid="SettingsPage_AppTheme_System" Content="System" Tag="System"/>
@@ -100,6 +103,7 @@
100103
TextWrapping="Wrap"/>
101104
</StackPanel>
102105
<ToggleSwitch Grid.Column="1" x:Name="GlobalHotkeyToggle" MinWidth="0" OnContent="" OffContent=""
106+
IsOn="{Binding GlobalHotkeyEnabled, Mode=TwoWay}"
103107
AutomationProperties.AutomationId="SettingsPageHotkey"
104108
AutomationProperties.Name="Global hotkey"/>
105109
</Grid>
@@ -122,6 +126,7 @@
122126
TextWrapping="Wrap"/>
123127
</StackPanel>
124128
<ToggleSwitch Grid.Column="1" x:Name="UseLegacyWebChatToggle" MinWidth="0" OnContent="" OffContent=""
129+
IsOn="{Binding UseLegacyWebChat, Mode=TwoWay}"
125130
AutomationProperties.AutomationId="SettingsPageUseLegacyWebChat"
126131
AutomationProperties.Name="Use gateway chat interface"/>
127132
</Grid>
@@ -149,6 +154,7 @@
149154
TextWrapping="Wrap"/>
150155
</StackPanel>
151156
<ToggleSwitch Grid.Column="1" x:Name="ReadResponsesAloudToggle" MinWidth="0" OnContent="" OffContent=""
157+
IsOn="{Binding ReadResponsesAloud, Mode=TwoWay}"
152158
AutomationProperties.AutomationId="SettingsPageReadAloud"
153159
AutomationProperties.LabeledBy="{Binding ElementName=ReadResponsesAloudLabel}"/>
154160
</Grid>
@@ -172,6 +178,7 @@
172178
TextWrapping="Wrap"/>
173179
</StackPanel>
174180
<ToggleSwitch Grid.Column="1" x:Name="ShowToolCallsToggle" MinWidth="0" OnContent="" OffContent=""
181+
IsOn="{Binding ShowChatToolCalls, Mode=TwoWay}"
175182
AutomationProperties.AutomationId="SettingsPageShowToolCalls"
176183
AutomationProperties.LabeledBy="{Binding ElementName=ShowToolCallsLabel}"/>
177184
</Grid>
@@ -198,6 +205,7 @@
198205
TextWrapping="Wrap"/>
199206
</StackPanel>
200207
<ToggleSwitch Grid.Column="1" x:Name="NotificationsToggle" MinWidth="0" OnContent="" OffContent=""
208+
IsOn="{Binding ShowNotifications, Mode=TwoWay}"
201209
AutomationProperties.AutomationId="SettingsPageNotifications"
202210
AutomationProperties.Name="Show notifications"/>
203211
</Grid>
@@ -221,6 +229,8 @@
221229
</StackPanel>
222230
<ComboBox Grid.Column="1" x:Uid="SettingsPage_NotificationSoundComboBox"
223231
x:Name="NotificationSoundComboBox" MinWidth="140"
232+
SelectedValuePath="Tag"
233+
SelectedValue="{Binding NotificationSound, Mode=TwoWay}"
224234
AutomationProperties.AutomationId="SettingsPageSound">
225235
<ComboBoxItem x:Uid="SettingsPage_ComboBoxItem_48" Content="Default" Tag="Default"/>
226236
<ComboBoxItem x:Uid="SettingsPage_ComboBoxItem_49" Content="None" Tag="None"/>
@@ -248,28 +258,28 @@
248258
<RowDefinition Height="Auto"/>
249259
</Grid.RowDefinitions>
250260
<CheckBox Grid.Row="0" Grid.Column="0" x:Uid="NotifyHealthCb" x:Name="NotifyHealthCb"
251-
Content="Health alerts"
261+
Content="Health alerts" IsChecked="{Binding NotifyHealth, Mode=TwoWay}"
252262
AutomationProperties.AutomationId="SettingsPageNotifyHealth"/>
253263
<CheckBox Grid.Row="0" Grid.Column="1" x:Uid="NotifyUrgentCb" x:Name="NotifyUrgentCb"
254-
Content="Urgent messages"
264+
Content="Urgent messages" IsChecked="{Binding NotifyUrgent, Mode=TwoWay}"
255265
AutomationProperties.AutomationId="SettingsPageNotifyUrgent"/>
256266
<CheckBox Grid.Row="1" Grid.Column="0" x:Uid="NotifyReminderCb" x:Name="NotifyReminderCb"
257-
Content="Reminders"
267+
Content="Reminders" IsChecked="{Binding NotifyReminder, Mode=TwoWay}"
258268
AutomationProperties.AutomationId="SettingsPageNotifyReminder"/>
259269
<CheckBox Grid.Row="1" Grid.Column="1" x:Uid="NotifyEmailCb" x:Name="NotifyEmailCb"
260-
Content="Email summaries"
270+
Content="Email summaries" IsChecked="{Binding NotifyEmail, Mode=TwoWay}"
261271
AutomationProperties.AutomationId="SettingsPageNotifyEmail"/>
262272
<CheckBox Grid.Row="2" Grid.Column="0" x:Uid="NotifyCalendarCb" x:Name="NotifyCalendarCb"
263-
Content="Calendar events"
273+
Content="Calendar events" IsChecked="{Binding NotifyCalendar, Mode=TwoWay}"
264274
AutomationProperties.AutomationId="SettingsPageNotifyCalendar"/>
265275
<CheckBox Grid.Row="2" Grid.Column="1" x:Uid="NotifyBuildCb" x:Name="NotifyBuildCb"
266-
Content="Build notifications"
276+
Content="Build notifications" IsChecked="{Binding NotifyBuild, Mode=TwoWay}"
267277
AutomationProperties.AutomationId="SettingsPageNotifyBuild"/>
268278
<CheckBox Grid.Row="3" Grid.Column="0" x:Uid="NotifyStockCb" x:Name="NotifyStockCb"
269-
Content="Stock alerts"
279+
Content="Stock alerts" IsChecked="{Binding NotifyStock, Mode=TwoWay}"
270280
AutomationProperties.AutomationId="SettingsPageNotifyStock"/>
271281
<CheckBox Grid.Row="3" Grid.Column="1" x:Uid="NotifyInfoCb" x:Name="NotifyInfoCb"
272-
Content="Info messages"
282+
Content="Info messages" IsChecked="{Binding NotifyInfo, Mode=TwoWay}"
273283
AutomationProperties.AutomationId="SettingsPageNotifyInfo"/>
274284
</Grid>
275285
<Button x:Uid="TestNotificationButton" x:Name="TestNotificationButton"
@@ -304,6 +314,7 @@
304314
TextWrapping="Wrap"/>
305315
</StackPanel>
306316
<ToggleSwitch Grid.Column="1" x:Name="ScreenRecordingToggle" MinWidth="0" OnContent="" OffContent=""
317+
IsOn="{Binding ScreenRecordingConsentGiven, Mode=TwoWay}"
307318
AutomationProperties.AutomationId="SettingsPageScreenRecording"
308319
AutomationProperties.Name="Allow screen recording"/>
309320
</Grid>
@@ -326,6 +337,7 @@
326337
TextWrapping="Wrap"/>
327338
</StackPanel>
328339
<ToggleSwitch Grid.Column="1" x:Name="CameraRecordingToggle" MinWidth="0" OnContent="" OffContent=""
340+
IsOn="{Binding CameraRecordingConsentGiven, Mode=TwoWay}"
329341
AutomationProperties.AutomationId="SettingsPageCameraRecording"
330342
AutomationProperties.Name="Allow camera recording"/>
331343
</Grid>
@@ -453,6 +465,7 @@
453465
MinWidth="0"
454466
OnContent=""
455467
OffContent=""
468+
IsOn="{Binding ShowDiagnostics, Mode=TwoWay}"
456469
AutomationProperties.AutomationId="SettingsPageShowDiagnostics"
457470
AutomationProperties.Name="Enable Diagnostics"/>
458471
</Grid>

0 commit comments

Comments
 (0)