Files
CommandForge/src/CommandForge.Wpf/ViewModels/MainViewModel.cs
T
poli0981andClaude Opus 5.5 4507faa6b3 fix(a11y): screen readers, keyboard, names and text size across the app
An accessibility review of every screen, with the keyboard alone, Narrator
and Axe.Windows:

- Screen readers hear what appears: a run starting, waiting for admin
  permission and finishing (result, exit code, time, restart needed), a
  search's result count, the Command Palette's highlighted result and its
  position, every notification and an update's progress. WPF ignores
  LiveSetting, so these go through AutomationPeer.RaiseNotificationEvent.
- The console and the Log viewer are read line by line; an error line says
  so and has a bar at its left, not only a colour; a blank line is read as
  one.
- Lists and buttons are read by their text instead of a type name; catalog
  rows carry their risk and badges as item status; a button repeated on
  every row names its row ("Undo: Flush DNS cache").
- WPF-UI template parts get names: the pane toggle and its two lists, the
  caption buttons, the clear button, the card expander headers. Icon glyphs
  (private-use characters) are kept out of the UI Automation tree
  (DecorativeIcon, GlyphHost), and an info bar's title and message, which
  the tree leaves out, are read through its severity icon.
- Keyboard: F6 moves between the pane, the page and the console; the focus
  moves into the page after navigation; Enter opens a command in the
  catalog and in History; Tab reaches a row's buttons; the splitters move
  with the arrow keys; every window and dialog starts with the keyboard
  inside it, a Caution confirmation on Cancel; Ctrl+Q does not quit while
  typing; page frames are no longer empty Tab stops.
- Seeing the screen: Windows' Text size multiplies the app's font size
  (the navigation pane widens at the largest sizes, badges and filters
  wrap); dimmed text is drawn at full strength under contrast themes;
  secondary text is darker; cut-off text has a tooltip; page transitions
  follow Windows' Animation effects setting.
- Tests: XAML rules (names, list items, headings, splitters, colours,
  icons) and the pure helpers behind the announcements and sizes.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-16 10:14:20 +07:00

359 lines
12 KiB
C#

using System.ComponentModel;
using CommandForge.Application;
using CommandForge.Application.Ports;
using CommandForge.Desktop.Resources;
using CommandForge.Desktop.Services;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
namespace CommandForge.Desktop.ViewModels;
/// <summary>
/// The main shell: which page is showing (<see cref="CurrentSection"/>, mirrored by the navigation
/// pane through <see cref="NavigationKey"/>), the page view-models, and app-wide actions (updates,
/// bug report, restore point, the elevated session).
/// </summary>
public sealed partial class MainViewModel : ObservableObject
{
private readonly IUpdateDialogService _updateDialog;
private readonly IReportBugDialogService _reportBug;
private readonly IElevationService _elevation;
private readonly IShellService _shell;
private readonly INotificationService _notifications;
public MainViewModel(
ICatalogProvider catalog,
ExecutionViewModel execution,
IConfirmationService confirmation,
IUpdateDialogService updateDialog,
IReportBugDialogService reportBug,
ISettingsService settings,
IExecutionHistoryService history,
IClock clock,
IRecipeStore recipeStore,
IUserCommandStore userCommandStore,
IRegistryService registry,
ISystemInfoService systemInfo,
IElevationService elevation,
SettingsViewModel settingsViewModel,
LogViewerViewModel logViewer,
DebugViewModel debug,
IDialogService dialogs,
IClipboardService clipboard,
IShellService shell,
INotificationService notifications)
{
ArgumentNullException.ThrowIfNull(catalog);
ArgumentNullException.ThrowIfNull(execution);
ArgumentNullException.ThrowIfNull(confirmation);
ArgumentNullException.ThrowIfNull(updateDialog);
ArgumentNullException.ThrowIfNull(reportBug);
ArgumentNullException.ThrowIfNull(settings);
ArgumentNullException.ThrowIfNull(history);
ArgumentNullException.ThrowIfNull(clock);
ArgumentNullException.ThrowIfNull(recipeStore);
ArgumentNullException.ThrowIfNull(userCommandStore);
ArgumentNullException.ThrowIfNull(registry);
ArgumentNullException.ThrowIfNull(systemInfo);
ArgumentNullException.ThrowIfNull(elevation);
ArgumentNullException.ThrowIfNull(settingsViewModel);
ArgumentNullException.ThrowIfNull(logViewer);
ArgumentNullException.ThrowIfNull(debug);
ArgumentNullException.ThrowIfNull(dialogs);
ArgumentNullException.ThrowIfNull(clipboard);
ArgumentNullException.ThrowIfNull(shell);
ArgumentNullException.ThrowIfNull(notifications);
_updateDialog = updateDialog;
_reportBug = reportBug;
_elevation = elevation;
_shell = shell;
_notifications = notifications;
Execution = execution;
Settings = settingsViewModel;
LogViewer = logViewer;
Debug = debug;
Catalog = new CatalogViewModel(
catalog,
systemInfo.GetStatus().OsBuild,
execution,
settingsViewModel,
confirmation,
settings,
history,
clock,
registry,
clipboard,
shell,
notifications);
Home = new HomeViewModel(Catalog.ItemsById, settings, systemInfo, item => SelectCommandById(item.Command.Id));
History = new HistoryViewModel(Catalog.ItemsById, history, SelectCommandById, RunCommandByIdAsync, execution, dialogs);
Recipes = new RecipesViewModel(Catalog.ItemsById, execution, recipeStore, history, clock, confirmation, dialogs, settings, Catalog.RestorePointCommand);
UserCommands = new UserCommandsViewModel(userCommandStore, execution, dialogs);
_isPaneOpen = !settings.CollapseSidebarByDefault;
Execution.AutoScroll = settings.AutoScrollConsole;
_isAdminSessionActive = elevation.IsSessionActive;
Execution.PropertyChanged += (_, e) =>
{
if (e.PropertyName == nameof(ExecutionViewModel.IsRunning))
{
CreateRestorePointCommand.NotifyCanExecuteChanged();
EndAdminSessionCommand.NotifyCanExecuteChanged();
}
};
// The pane mirrors the catalog's browse state (Favorites / a category / everything).
Catalog.PropertyChanged += (_, e) =>
{
if (e.PropertyName is nameof(CatalogViewModel.IsFavoritesMode) or nameof(CatalogViewModel.SelectedCategory))
{
OnPropertyChanged(nameof(NavigationKey));
}
};
Settings.PropertyChanged += OnSettingsChanged;
Settings.PortableSettingsApplied += OnPortableSettingsApplied;
// Elevated-session changes arrive on a broker thread.
_elevation.SessionChanged += (_, _) => System.Windows.Application.Current?.Dispatcher.InvokeAsync(
() => IsAdminSessionActive = _elevation.IsSessionActive);
// Live language switching: re-resolve display strings in place (keeps selection/scroll).
LocalizationManager.Instance.CultureChanged += OnCultureChanged;
Home.Refresh();
}
public HomeViewModel Home { get; }
public CatalogViewModel Catalog { get; }
public HistoryViewModel History { get; }
public RecipesViewModel Recipes { get; }
public UserCommandsViewModel UserCommands { get; }
public SettingsViewModel Settings { get; }
public LogViewerViewModel LogViewer { get; }
public DebugViewModel Debug { get; }
public ExecutionViewModel Execution { get; }
[ObservableProperty]
[NotifyPropertyChangedFor(nameof(NavigationKey))]
private ShellSection _currentSection = ShellSection.Home;
/// <summary>Whether the navigation pane is expanded (Ctrl+B toggles it).</summary>
[ObservableProperty]
private bool _isPaneOpen;
/// <summary>Whether an elevated helper is running (admin commands run without a new UAC prompt).</summary>
[ObservableProperty]
[NotifyCanExecuteChangedFor(nameof(EndAdminSessionCommand))]
private bool _isAdminSessionActive;
/// <summary>The navigation target that matches the current state (see <see cref="NavigationKeys"/>).</summary>
public string NavigationKey => CurrentSection switch
{
ShellSection.Home => NavigationKeys.Home,
ShellSection.Catalog when Catalog.IsFavoritesMode => NavigationKeys.Favorites,
ShellSection.Catalog when Catalog.SelectedCategory?.Id is { } id => NavigationKeys.Category(id),
ShellSection.Catalog => NavigationKeys.Catalog,
ShellSection.History => NavigationKeys.History,
ShellSection.Recipes => NavigationKeys.Recipes,
ShellSection.UserCommands => NavigationKeys.UserCommands,
ShellSection.Settings => NavigationKeys.Settings,
ShellSection.LogViewer => NavigationKeys.Logs,
ShellSection.Debug => NavigationKeys.Debug,
_ => NavigationKeys.Home,
};
/// <summary>Goes to the page a navigation key names (from the navigation pane).</summary>
public void Navigate(string key)
{
ArgumentNullException.ThrowIfNull(key);
if (string.Equals(key, NavigationKey, StringComparison.Ordinal))
{
return;
}
if (NavigationKeys.CategoryIdOf(key) is { } categoryId)
{
Catalog.ShowCategory(categoryId);
CurrentSection = ShellSection.Catalog;
return;
}
switch (key)
{
case NavigationKeys.Favorites:
ShowFavorites();
break;
case NavigationKeys.Catalog:
ShowCatalog();
break;
case NavigationKeys.History:
ShowHistory();
break;
case NavigationKeys.Recipes:
ShowRecipes();
break;
case NavigationKeys.UserCommands:
ShowUserCommands();
break;
case NavigationKeys.Settings:
ShowSettings();
break;
case NavigationKeys.Logs:
ShowLogViewer();
break;
case NavigationKeys.Debug:
ShowDebug();
break;
default:
ShowHome();
break;
}
}
[RelayCommand]
private void ToggleSidebar() => IsPaneOpen = !IsPaneOpen;
[RelayCommand]
private void ShowHome()
{
Home.Refresh();
CurrentSection = ShellSection.Home;
}
[RelayCommand]
private void ShowCatalog()
{
Catalog.ShowAll();
CurrentSection = ShellSection.Catalog;
}
[RelayCommand]
private void ShowFavorites()
{
Catalog.ShowFavorites();
CurrentSection = ShellSection.Catalog;
}
[RelayCommand]
private void ShowHistory()
{
History.Refresh();
CurrentSection = ShellSection.History;
}
[RelayCommand]
private void ShowRecipes()
{
Recipes.Refresh();
CurrentSection = ShellSection.Recipes;
}
[RelayCommand]
private void ShowUserCommands()
{
UserCommands.Refresh();
CurrentSection = ShellSection.UserCommands;
}
[RelayCommand]
private void ShowSettings() => CurrentSection = ShellSection.Settings;
[RelayCommand]
private void ShowLogViewer() => CurrentSection = ShellSection.LogViewer;
[RelayCommand]
private void ShowDebug() => CurrentSection = ShellSection.Debug;
[RelayCommand]
private void OpenUserGuide() => _shell.TryOpen(ProductLinks.UserGuide);
[RelayCommand]
private Task ReportBugAsync() => _reportBug.ShowAsync();
[RelayCommand]
private Task CheckForUpdatesAsync() => _updateDialog.ShowAsync(startedFromStartup: false);
/// <summary>Creates a System Restore Point by running the vetted catalog command (elevated via the broker).</summary>
[RelayCommand(CanExecute = nameof(CanCreateRestorePoint))]
private Task CreateRestorePointAsync()
{
if (Catalog.RestorePointCommand is not { } command)
{
return Task.CompletedTask;
}
CurrentSection = ShellSection.Catalog; // its console output shows on the catalog page
return Catalog.RunCommandByIdAsync(command.Id);
}
private bool CanCreateRestorePoint() => Catalog.RestorePointCommand is not null && !Execution.IsRunning;
/// <summary>Closes the elevated helper; the next admin command asks for permission again.</summary>
[RelayCommand(CanExecute = nameof(CanEndAdminSession))]
private async Task EndAdminSessionAsync()
{
await _elevation.EndSessionAsync();
IsAdminSessionActive = _elevation.IsSessionActive;
_notifications.Show(Strings.Get("Admin_SessionEnded"));
}
private bool CanEndAdminSession() => IsAdminSessionActive && !Execution.IsRunning;
/// <summary>Re-runs a command by id (History's "Run again" / "Undo"); ignores unknown ids.</summary>
public Task RunCommandByIdAsync(string commandId)
{
CurrentSection = ShellSection.Catalog;
return Catalog.RunCommandByIdAsync(commandId);
}
/// <summary>Shows a command in the catalog (from the palette, Home and History).</summary>
public void SelectCommandById(string commandId)
{
Catalog.SelectCommandById(commandId);
CurrentSection = ShellSection.Catalog;
CommandOpened?.Invoke(this, EventArgs.Empty);
}
/// <summary>
/// Raised after a command was opened in the catalog from elsewhere (Home, History, the palette),
/// so the window can move keyboard focus to its row: the control that opened it is gone.
/// </summary>
public event EventHandler? CommandOpened;
private void OnSettingsChanged(object? sender, PropertyChangedEventArgs e)
{
if (e.PropertyName == nameof(SettingsViewModel.AutoScrollConsole))
{
Execution.AutoScroll = Settings.AutoScrollConsole;
}
}
private void OnCultureChanged(object? sender, EventArgs e)
{
Catalog.RefreshLanguage();
Home.Refresh();
History.Refresh();
}
private void OnPortableSettingsApplied()
{
// Favorites may have changed (import / apply-profile). Theme, font and language already
// applied themselves through the Settings view-model's property setters.
Catalog.SyncFavorites();
Home.Refresh();
History.Refresh();
}
}