From 1d2878dcd1d82239943aa53349604e42d3212166 Mon Sep 17 00:00:00 2001 From: elgegeto <341694139+elgegeto@users.noreply.github.com> Date: Tue, 10 Nov 2026 16:39:25 -0300 Subject: [PATCH] fix: allow Windows shutdown and sign-out without cancelling (#58) Save session state synchronously during OS shutdown, bypass close-to-tray, and leave the existing graceful tracker shutdown for ordinary exit requests. --- src/RatioMaster.App/App.axaml.cs | 15 +++++++++++++++ .../ViewModels/MainWindowViewModel.cs | 10 ++++++++-- src/RatioMaster.App/Views/MainWindow.axaml.cs | 6 +++++- 3 files changed, 28 insertions(+), 3 deletions(-) diff --git a/src/RatioMaster.App/App.axaml.cs b/src/RatioMaster.App/App.axaml.cs index d8f1ddb..e01c4ec 100644 --- a/src/RatioMaster.App/App.axaml.cs +++ b/src/RatioMaster.App/App.axaml.cs @@ -67,6 +67,21 @@ public override void OnFrameworkInitializationCompleted() viewModel.RestoreRequested += (_, _) => window.RestoreFromTray(); desktop.ShutdownRequested += async (_, e) => { + if (OperatingSystem.IsWindows()) + { + window.AllowSystemShutdown(); + try + { + viewModel.SaveBeforeShutdown(); + } + catch (Exception) + { + // Saving must not prevent Windows from signing out or shutting down. + } + + return; + } + e.Cancel = true; await this.ShutdownAsync(desktop); }; diff --git a/src/RatioMaster.App/ViewModels/MainWindowViewModel.cs b/src/RatioMaster.App/ViewModels/MainWindowViewModel.cs index f371d88..d123ff2 100644 --- a/src/RatioMaster.App/ViewModels/MainWindowViewModel.cs +++ b/src/RatioMaster.App/ViewModels/MainWindowViewModel.cs @@ -266,12 +266,18 @@ public async Task HandleDroppedFilesAsync(IReadOnlyList paths) } } - /// Stops every torrent (sending the stopped announce) and persists state. Called when quitting. - public async Task ShutdownAsync(TimeSpan timeout) + /// Saves state without waiting for tracker requests during OS shutdown. + public void SaveBeforeShutdown() { this.timer.Stop(); this.SaveLastSession(); this.settingsStore.Save(this.settings); + } + + /// Stops every torrent (sending the stopped announce) and persists state. Called when quitting. + public async Task ShutdownAsync(TimeSpan timeout) + { + this.SaveBeforeShutdown(); using var cts = new CancellationTokenSource(timeout); var stops = this.Torrents.Select(async t => diff --git a/src/RatioMaster.App/Views/MainWindow.axaml.cs b/src/RatioMaster.App/Views/MainWindow.axaml.cs index 941be8e..c55fa7b 100644 --- a/src/RatioMaster.App/Views/MainWindow.axaml.cs +++ b/src/RatioMaster.App/Views/MainWindow.axaml.cs @@ -28,6 +28,7 @@ public partial class MainWindow : Window private readonly Dictionary defaultColumnWidths = []; private MainWindowViewModel? viewModel; + private bool systemShutdownRequested; private double detailsPaneHeight = 300; private PixelPoint? normalPosition; private Size? normalSize; @@ -68,6 +69,9 @@ public MainWindow() private MainWindowViewModel? ViewModel => this.viewModel; + /// Bypasses close-to-tray when Windows is ending the user session. + public void AllowSystemShutdown() => this.systemShutdownRequested = true; + private RowDefinition DetailsRow => this.ContentGrid.RowDefinitions[2]; private DataGridColumn NameColumn => this.TorrentGrid.Columns[0]; @@ -85,7 +89,7 @@ protected override void OnClosing(WindowClosingEventArgs e) { this.SavePlacement(); - if (!e.IsProgrammatic && this.ViewModel is { } viewModel) + if (!e.IsProgrammatic && !this.systemShutdownRequested && this.ViewModel is { } viewModel) { // Close to tray keeps the torrents announcing; quitting happens through File > Exit or the tray menu. // Otherwise closing the window quits as File > Exit does, which stops the torrents and then closes it: