Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions src/RatioMaster.App/App.axaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
};
Expand Down
10 changes: 8 additions & 2 deletions src/RatioMaster.App/ViewModels/MainWindowViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -266,12 +266,18 @@ public async Task HandleDroppedFilesAsync(IReadOnlyList<string> paths)
}
}

/// <summary>Stops every torrent (sending the stopped announce) and persists state. Called when quitting.</summary>
public async Task ShutdownAsync(TimeSpan timeout)
/// <summary>Saves state without waiting for tracker requests during OS shutdown.</summary>
public void SaveBeforeShutdown()
{
this.timer.Stop();
this.SaveLastSession();
this.settingsStore.Save(this.settings);
}

/// <summary>Stops every torrent (sending the stopped announce) and persists state. Called when quitting.</summary>
public async Task ShutdownAsync(TimeSpan timeout)
{
this.SaveBeforeShutdown();

using var cts = new CancellationTokenSource(timeout);
var stops = this.Torrents.Select(async t =>
Expand Down
6 changes: 5 additions & 1 deletion src/RatioMaster.App/Views/MainWindow.axaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public partial class MainWindow : Window
private readonly Dictionary<DataGridColumn, DataGridLength> defaultColumnWidths = [];

private MainWindowViewModel? viewModel;
private bool systemShutdownRequested;
private double detailsPaneHeight = 300;
private PixelPoint? normalPosition;
private Size? normalSize;
Expand Down Expand Up @@ -68,6 +69,9 @@ public MainWindow()

private MainWindowViewModel? ViewModel => this.viewModel;

/// <summary>Bypasses close-to-tray when Windows is ending the user session.</summary>
public void AllowSystemShutdown() => this.systemShutdownRequested = true;

private RowDefinition DetailsRow => this.ContentGrid.RowDefinitions[2];

private DataGridColumn NameColumn => this.TorrentGrid.Columns[0];
Expand All @@ -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:
Expand Down