Repository navigation
Add an option to quit UniGetUI once automatic updates have finished #5530
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Gabriel Dufresne (GabrielDuf)
merged 8 commits into
main
from
feature-5527-quit-after-auto-updates
Oct 16, 2026
+555
−59
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
1c7e308
Add an option to quit UniGetUI once automatic updates have finished
GabrielDuf 28b7d52
Make the automatic quit wait for background work and never prompt
GabrielDuf cf83993
Move the automatic update restrictions to the scheduler page
GabrielDuf d18e955
Close the remaining gaps in the automatic quit idle check
GabrielDuf a3227dc
Give the self-update handoff enough time for archive extraction
GabrielDuf f639555
Track every outstanding self-update handoff, not just the latest
GabrielDuf 73ab2c6
Keep the automatic quit from discarding unsaved bundle edits
GabrielDuf 964280c
Cover the automatic quit decision with deterministic tests
GabrielDuf File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
75 changes: 75 additions & 0 deletions
75
src/UniGetUI.Avalonia/Infrastructure/AutomaticQuitMonitor.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| namespace UniGetUI.Avalonia.Infrastructure; | ||
|
|
||
| public readonly record struct AutomaticQuitSnapshot( | ||
| bool IsBusy, | ||
| bool IsEnabled, | ||
| bool IsAlreadyQuitting, | ||
| bool IsWindowVisible, | ||
| bool HasUnsavedWork); | ||
|
|
||
| public enum AutomaticQuitDecision | ||
| { | ||
| KeepWaiting, | ||
| Quit, | ||
| Cancelled, | ||
| WindowVisible, | ||
| UnsavedWork, | ||
| } | ||
|
|
||
| public static class AutomaticQuitMonitor | ||
| { | ||
| public const int RequiredIdleChecks = 2; | ||
|
|
||
| public static AutomaticQuitDecision Evaluate(AutomaticQuitSnapshot snapshot, ref int idleChecks) | ||
| { | ||
| if (snapshot.IsBusy) | ||
| { | ||
| idleChecks = 0; | ||
| return AutomaticQuitDecision.KeepWaiting; | ||
| } | ||
|
|
||
| if (++idleChecks < RequiredIdleChecks) | ||
| return AutomaticQuitDecision.KeepWaiting; | ||
|
|
||
| if (!snapshot.IsEnabled || snapshot.IsAlreadyQuitting) | ||
| return AutomaticQuitDecision.Cancelled; | ||
|
|
||
| if (snapshot.IsWindowVisible) | ||
| return AutomaticQuitDecision.WindowVisible; | ||
|
|
||
| if (snapshot.HasUnsavedWork) | ||
| return AutomaticQuitDecision.UnsavedWork; | ||
|
|
||
| return AutomaticQuitDecision.Quit; | ||
| } | ||
|
|
||
| public static async Task<AutomaticQuitDecision> RunAsync( | ||
| Task? launching, | ||
| Func<Task> waitForNextSample, | ||
| Func<Func<AutomaticQuitDecision>, Task<AutomaticQuitDecision>> runOnUiThread, | ||
| Func<AutomaticQuitSnapshot?> sample, | ||
| Action quit) | ||
| { | ||
| if (launching is not null) | ||
| await launching; | ||
|
|
||
| int idleChecks = 0; | ||
| while (true) | ||
| { | ||
| await waitForNextSample(); | ||
| AutomaticQuitDecision decision = await runOnUiThread(() => | ||
| { | ||
| if (sample() is not { } snapshot) | ||
| return AutomaticQuitDecision.Cancelled; | ||
|
|
||
| AutomaticQuitDecision result = Evaluate(snapshot, ref idleChecks); | ||
| if (result is AutomaticQuitDecision.Quit) | ||
| quit(); | ||
| return result; | ||
| }); | ||
|
|
||
| if (decision is not AutomaticQuitDecision.KeepWaiting) | ||
| return decision; | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
src/UniGetUI.Avalonia/Infrastructure/InstallerHandoffTracker.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| namespace UniGetUI.Avalonia.Infrastructure; | ||
|
|
||
| public sealed class InstallerHandoffTracker | ||
| { | ||
| private readonly object _lock = new(); | ||
| private readonly HashSet<TaskCompletionSource> _pending = []; | ||
|
|
||
| public bool HasPending | ||
| { | ||
| get | ||
| { | ||
| lock (_lock) | ||
| return _pending.Count > 0; | ||
| } | ||
| } | ||
|
|
||
| public TaskCompletionSource Begin() | ||
| { | ||
| var handoff = new TaskCompletionSource(TaskCreationOptions.RunContinuationsAsynchronously); | ||
| lock (_lock) | ||
| _pending.Add(handoff); | ||
| return handoff; | ||
| } | ||
|
|
||
| public void Complete(TaskCompletionSource handoff) | ||
| { | ||
| handoff.TrySetResult(); | ||
| lock (_lock) | ||
| _pending.Remove(handoff); | ||
| } | ||
|
|
||
| public async Task<bool> WaitForAllAsync(TimeSpan timeout) | ||
| { | ||
| Task[] pending; | ||
| lock (_lock) | ||
| pending = [.. _pending.Select(handoff => handoff.Task)]; | ||
|
|
||
| if (pending.Length == 0) | ||
| return true; | ||
|
|
||
| try | ||
| { | ||
| await Task.WhenAll(pending).WaitAsync(timeout); | ||
| return true; | ||
| } | ||
| catch (TimeoutException) | ||
| { | ||
| return false; | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
83 changes: 83 additions & 0 deletions
83
src/UniGetUI.Avalonia/Infrastructure/QuitAfterAutomaticUpdates.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,83 @@ | ||
| using Avalonia.Threading; | ||
| using UniGetUI.Avalonia.ViewModels.Pages.SettingsPages; | ||
| using UniGetUI.Avalonia.Views; | ||
| using UniGetUI.Core.Logging; | ||
| using UniGetUI.Core.SettingsEngine; | ||
| using UniGetUI.PackageOperations; | ||
|
|
||
| namespace UniGetUI.Avalonia.Infrastructure; | ||
|
|
||
| internal static class QuitAfterAutomaticUpdates | ||
| { | ||
| private static readonly TimeSpan PollInterval = TimeSpan.FromSeconds(5); | ||
|
|
||
| private static int _waiting; | ||
|
|
||
| public static void QuitWhenOperationsFinish(Task? launching = null) | ||
| { | ||
| if (!Settings.Get(Settings.K.QuitAfterAutomaticUpdates)) | ||
| return; | ||
|
|
||
| if (Interlocked.Exchange(ref _waiting, 1) == 1) | ||
| return; | ||
|
|
||
| _ = WaitAndQuitAsync(launching); | ||
| } | ||
|
|
||
| private static async Task WaitAndQuitAsync(Task? launching) | ||
| { | ||
| try | ||
| { | ||
| AutomaticQuitDecision decision = await AutomaticQuitMonitor.RunAsync( | ||
| launching, | ||
| () => Task.Delay(PollInterval), | ||
| decide => Dispatcher.UIThread.InvokeAsync(decide).GetTask(), | ||
| Sample, | ||
| Quit); | ||
|
|
||
| switch (decision) | ||
| { | ||
| case AutomaticQuitDecision.WindowVisible: | ||
| Logger.Info("Automatic updates have finished, but UniGetUI will not quit because its window is open"); | ||
| break; | ||
| case AutomaticQuitDecision.UnsavedWork: | ||
| Logger.Info("Automatic updates have finished, but UniGetUI will not quit because there is unsaved work"); | ||
| break; | ||
| } | ||
| } | ||
| catch (Exception ex) | ||
| { | ||
| Logger.Error(ex); | ||
| } | ||
| finally | ||
| { | ||
| Interlocked.Exchange(ref _waiting, 0); | ||
| } | ||
| } | ||
|
|
||
| private static AutomaticQuitSnapshot? Sample() | ||
| { | ||
| if (MainWindow.Instance is not { } window) | ||
| return null; | ||
|
|
||
| return new AutomaticQuitSnapshot( | ||
| IsBusy: IsBusy(), | ||
| IsEnabled: Settings.Get(Settings.K.QuitAfterAutomaticUpdates), | ||
| IsAlreadyQuitting: window.IsQuitting, | ||
| IsWindowVisible: window.IsVisible, | ||
| HasUnsavedWork: window.HasUnsavedWork); | ||
| } | ||
|
|
||
| private static bool IsBusy() | ||
| => AvaloniaOperationRegistry.HasActiveOperations() | ||
| || AbstractOperation.QueueLength() > 0 | ||
|
GabrielDuf marked this conversation as resolved.
|
||
| || MaintenanceScheduler.IsAnyTaskRunning | ||
|
GabrielDuf marked this conversation as resolved.
|
||
| || BackupViewModel.IsBackupRunning | ||
| || AvaloniaAutoUpdater.IsBusy; | ||
|
|
||
| private static void Quit() | ||
| { | ||
| Logger.ImportantInfo("Automatic updates have finished, quitting UniGetUI as configured"); | ||
| MainWindow.Instance?.QuitApplication(); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.