Files
AppKit/src/KofTwentyTwo.AppKit.WinUI/WinUIShell.cs
T
james.maesandClaude Opus 5.5 20c95cffaa refactor: fix analyzer findings and add header comments
Fixes every finding the new quality gate reports, without changing behavior:

- One type per file (MA0048): AppBrand, Attribution, NullActivityLog,
  LogSnapshot, ISanitizable, AppTheme, UpdateCheckResult, UpdateText,
  IUpdateBackend, ContentDialogPrompter, WindowExtensions, MessageBoxPrompter,
  and the samples' SampleSettings/SampleSettingsContext get their own files
  (the samples now link samples/Shared/*.cs).
- Regex match timeouts on the two generated regexes (MA0009).
- AppInfo.Id reports nameof(value) as the parameter (MA0015); test updated.
- Splash dismissal is a named DismissAfterAsync started through the
  dispatcher (MA0147, MA0134), and the WinUI splash constructor is split into
  builders (MA0051). The WPF countdown must go through the dispatcher: the
  sample builds its window before Application.Run, when no synchronization
  context exists.
- The WinUI sample's startup update check is a named method (MA0147).
- CA1859 in BrandTool and the version tests; private static readonly fields
  renamed to s_camelCase (IDE1006).
- A header comment on every type and method (K22-CODE-09/CS-04): hand-written
  for library, sample, and tool code; test methods summarize their
  Subject_Behavior names.

Verified: zero-warning x64 build, 115 unit tests with 100% line coverage of
KofTwentyTwo.AppKit and KofTwentyTwo.AppKit.Updates, 12 UI tests, and
dotnet format --verify-no-changes --severity warn.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: James Maes <james@kof22.com>
2026-10-09 10:23:40 -05:00

76 lines
2.4 KiB
C#

/*
* Copyright (c) 2026 James Maes (KofTwentyTwo)
* SPDX-License-Identifier: MIT
*/
using KofTwentyTwo.AppKit.Logging;
using KofTwentyTwo.AppKit.Settings;
using Microsoft.UI.Xaml;
namespace KofTwentyTwo.AppKit.WinUI;
/// <summary>App-level wiring every WinUI app repeats: crash net and theme.</summary>
public static class WinUIShell
{
/// <summary>
/// Last-resort net: an exception escaping an async void event handler is rethrown
/// on the dispatcher with no frame above it, and the process dies as a stowed
/// exception (0xc000027b) with nothing in the log. This records it and keeps the app
/// running. Call from the App constructor.
/// </summary>
/// <param name="application">The app.</param>
/// <param name="log">Supplies the log; may return null early in startup.</param>
public static void InstallCrashNet(Application application, Func<IActivityLog?> log)
{
ArgumentNullException.ThrowIfNull(application);
ArgumentNullException.ThrowIfNull(log);
application.UnhandledException += (_, e) =>
{
e.Handled = true;
try
{
log()?.Error($"Unhandled UI exception: {e.Message}", e.Exception);
}
catch
{
// Logging must never turn a survivable error fatal; the stowed
// Exception property itself can throw when type info was lost.
}
};
}
/// <summary>
/// Applies the startup theme. Only valid in the App constructor, before any window
/// exists; for live changes use <see cref="ApplyTheme(FrameworkElement, AppTheme)"/>.
/// </summary>
public static void ApplyTheme(Application application, AppTheme theme)
{
ArgumentNullException.ThrowIfNull(application);
if(theme == AppTheme.Light)
{
application.RequestedTheme = ApplicationTheme.Light;
}
else if(theme == AppTheme.Dark)
{
application.RequestedTheme = ApplicationTheme.Dark;
}
}
/// <summary>Applies a theme change to a window's root element while the app runs.</summary>
public static void ApplyTheme(FrameworkElement root, AppTheme theme)
{
ArgumentNullException.ThrowIfNull(root);
root.RequestedTheme = theme switch
{
AppTheme.Light => ElementTheme.Light,
AppTheme.Dark => ElementTheme.Dark,
_ => ElementTheme.Default,
};
}
}