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
16 changes: 16 additions & 0 deletions src/RatioMaster.App/App.axaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,22 @@ public override void OnFrameworkInitializationCompleted()
viewModel.RestoreRequested += (_, _) => window.RestoreFromTray();
desktop.ShutdownRequested += async (_, e) =>
{
if (OperatingSystem.IsWindows())
{
// WM_QUERYENDSESSION must not be cancelled for shutdown or sign-out.
window.WindowsSessionEnding = true;
try
{
viewModel.SaveForSystemShutdown();
}
catch (Exception)
{
// A failed save must not prevent Windows from ending the session.
}

return;
}

e.Cancel = true;
await this.ShutdownAsync(desktop);
};
Expand Down
11 changes: 9 additions & 2 deletions src/RatioMaster.App/ViewModels/MainWindowViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,13 @@ public async Task HandleDroppedFilesAsync(IReadOnlyList<string> paths)
}
}

/// <summary>Saves immediately without waiting for tracker announces during a system shutdown.</summary>
public void SaveForSystemShutdown()
{
this.SaveLastSession(force: true);
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)
{
Expand Down Expand Up @@ -852,9 +859,9 @@ private async Task RestoreSessionAsync(string path, bool startImmediately, bool
}
}

private void SaveLastSession()
private void SaveLastSession(bool force = false)
{
if (this.deferredListChanges > 0)
if (!force && this.deferredListChanges > 0)
{
return;
}
Expand Down
20 changes: 18 additions & 2 deletions src/RatioMaster.App/Views/MainWindow.axaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ public MainWindow()
this.BuildColumnsMenu();
}

internal bool WindowsSessionEnding { get; set; }

private MainWindowViewModel? ViewModel => this.viewModel;

private RowDefinition DetailsRow => this.ContentGrid.RowDefinitions[2];
Expand All @@ -83,9 +85,23 @@ public void RestoreFromTray()

protected override void OnClosing(WindowClosingEventArgs e)
{
this.SavePlacement();
if (this.WindowsSessionEnding)
{
try
{
this.SavePlacement();
}
catch (Exception)
{
// A layout save failure must not interrupt Windows sign-out.
}
}
else
{
this.SavePlacement();
}

if (!e.IsProgrammatic && this.ViewModel is { } viewModel)
if (!e.IsProgrammatic && !this.WindowsSessionEnding && 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