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; /// /// Titano desktop client. Local mode starts here: settings and language before the first window, then the window, /// then the index and the job engine () off the UI thread. --capture <dir> /// renders every screen with synthetic data in a throw-away data folder and exits. /// 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); } /// Opens the index and the job engine after the window is visible, then fills the screens. 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; } } }