Local-mode composition root (SQLite index, one JobEngine fed by a single handler registration list), keyset-paged library with thumbnail cache and hover scrub, live job list with throttled updates and the shell job bar, settings with live template validation and encoder self-test, server connection with trust-on-first-use and DPAPI token, first-run welcome, About with update check, and --capture screenshots with synthetic data. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012iLMBNwBE2GnphAsu4cjaC
127 lines
4.7 KiB
C#
127 lines
4.7 KiB
C#
using System.Windows;
|
|
using System.Windows.Threading;
|
|
using Titano.App.DesignTime;
|
|
using Titano.App.Infrastructure;
|
|
using Titano.App.Shell;
|
|
using Titano.Core.Diagnostics;
|
|
using Titano.Ui.Mvvm;
|
|
using Titano.Ui.Theming;
|
|
|
|
namespace Titano.App;
|
|
|
|
/// <summary>
|
|
/// Titano desktop client. Local mode starts here: settings and language before the first window, then the window,
|
|
/// then the index and the job engine (<see cref="AppServices"/>) off the UI thread. <c>--capture <dir></c>
|
|
/// renders every screen with synthetic data in a throw-away data folder and exits.
|
|
/// </summary>
|
|
public partial class App : Application
|
|
{
|
|
private FileLogSink? _log;
|
|
|
|
public AppEnvironment Environment { get; private set; } = null!;
|
|
public AppSettings Settings { get; private set; } = null!;
|
|
public AppServices Services { get; private set; } = null!;
|
|
|
|
public static new App Current => (App)Application.Current;
|
|
|
|
protected override void OnStartup(StartupEventArgs e)
|
|
{
|
|
base.OnStartup(e);
|
|
var captureIndex = Array.IndexOf(e.Args, "--capture");
|
|
var capture = captureIndex >= 0;
|
|
// Capture never reads or writes the user's data folder.
|
|
Environment = capture ? AppEnvironment.ForCapture() : AppEnvironment.Detect();
|
|
_log = new FileLogSink(Environment.LogDirectory, "titano");
|
|
// Capture runs in CI: mirror the log to stderr so failures are visible in the job output.
|
|
Log.SetSink(capture ? new TeeLogSink(_log, new StderrLogSink()) : _log);
|
|
Log.Info("app", $"Titano {AppServices.VersionText} starting; data at {Environment.DataDirectory}");
|
|
|
|
Settings = capture ? new AppSettings { Language = "en" } : AppSettings.Load(Environment.SettingsPath);
|
|
UiLanguage.Apply(Settings.Language);
|
|
ThemeManager.Initialize(this, capture ? AppTheme.Dark : Settings.Theme);
|
|
AppResources.Merge(this);
|
|
|
|
DispatcherUnhandledException += OnUnhandledException;
|
|
AppDomain.CurrentDomain.UnhandledException += (_, args) => Log.Error("app", "Unhandled exception", args.ExceptionObject as Exception);
|
|
TaskScheduler.UnobservedTaskException += (_, args) =>
|
|
{
|
|
Log.Error("app", "Unobserved task exception", args.Exception);
|
|
args.SetObserved();
|
|
};
|
|
|
|
var dialogs = new AppDialogs();
|
|
Services = new AppServices(Environment, Settings, SaveSettings, dialogs);
|
|
SampleData? sample = capture ? SampleData.Configure(Services) : null;
|
|
var shell = new ShellViewModel(Services);
|
|
AsyncRelayCommand.Failed += ex => shell.ReportError(ex);
|
|
var window = new MainWindow(shell);
|
|
dialogs.Owner = window;
|
|
MainWindow = window;
|
|
|
|
if (capture)
|
|
{
|
|
var dir = captureIndex + 1 < e.Args.Length ? e.Args[captureIndex + 1] : "screenshots";
|
|
_ = ScreenCapture.RunAsync(window, shell, sample!, dir);
|
|
return;
|
|
}
|
|
window.Show();
|
|
_ = StartAsync(shell);
|
|
}
|
|
|
|
/// <summary>Opens the index and the job engine after the window is visible, then fills the screens.</summary>
|
|
private async Task StartAsync(ShellViewModel shell)
|
|
{
|
|
try
|
|
{
|
|
await Services.OpenAsync(Environment.DatabasePath);
|
|
await shell.StartAsync();
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
shell.ReportError(ex);
|
|
}
|
|
}
|
|
|
|
public void SaveSettings()
|
|
{
|
|
try
|
|
{
|
|
Settings.Save(Environment.SettingsPath);
|
|
}
|
|
catch (Exception ex) when (ex is System.IO.IOException or UnauthorizedAccessException)
|
|
{
|
|
Log.Warning("app", "Settings not saved", ex);
|
|
}
|
|
}
|
|
|
|
protected override void OnExit(ExitEventArgs e)
|
|
{
|
|
SaveSettings();
|
|
if (MainWindow?.DataContext is ShellViewModel shell) shell.Dispose();
|
|
try
|
|
{
|
|
// Data and engine code continue on the thread pool, so waiting here cannot deadlock the dispatcher.
|
|
if (Services is not null && !Services.DisposeAsync().AsTask().Wait(TimeSpan.FromSeconds(10)))
|
|
Log.Warning("app", "Services did not stop within 10 s");
|
|
}
|
|
catch (AggregateException ex)
|
|
{
|
|
Log.Warning("app", "Services stopped with an error", ex);
|
|
}
|
|
Log.Info("app", "Titano exiting");
|
|
_log?.Dispose();
|
|
if (Environment.IsCapture) Environment.DeleteCaptureData();
|
|
base.OnExit(e);
|
|
}
|
|
|
|
private void OnUnhandledException(object sender, DispatcherUnhandledExceptionEventArgs e)
|
|
{
|
|
Log.Error("app", "Unhandled UI exception", e.Exception);
|
|
if (MainWindow?.DataContext is ShellViewModel shell)
|
|
{
|
|
shell.ReportError(e.Exception);
|
|
e.Handled = true;
|
|
}
|
|
}
|
|
}
|