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
18 changes: 18 additions & 0 deletions src/UniGetUI.Avalonia/App.axaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,24 @@ private static void StartMainWindow(IClassicDesktopStyleApplicationLifetime desk
AvaloniaAppHost.SecondaryInstanceArgsReceived += args =>
HandleSecondaryInstanceArgs(mainWindow, args);

desktop.ShutdownRequested += (_, e) =>
{
if (mainWindow.IsQuitting)
return;

e.Cancel = true;
mainWindow.QuitApplication();
};

if (Current?.TryGetFeature<IActivatableLifetime>() is { } activatable)
{
activatable.Activated += (_, e) =>
{
if (e.Kind == ActivationKind.Reopen)
mainWindow.ShowFromTray();
};
}

if (CoreData.WasDaemon)
{
// Start silently: hide the window on first open only.
Expand Down
2 changes: 1 addition & 1 deletion src/UniGetUI.Avalonia/ViewModels/MainWindowViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,7 @@ public static PageType GetPreviousPage(PageType type) =>
public void NavigateTo(PageType newPage_t, bool toHistory = true)
{
if (newPage_t is PageType.About) { _ = ShowAboutDialog(); return; }
if (newPage_t is PageType.Quit) { (Application.Current?.ApplicationLifetime as IClassicDesktopStyleApplicationLifetime)?.Shutdown(); return; }
if (newPage_t is PageType.Quit) { MainWindow.Instance?.QuitApplication(); return; }

if (_currentPage == newPage_t)
{
Expand Down
27 changes: 21 additions & 6 deletions src/UniGetUI.Avalonia/Views/MainWindow.axaml.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
using System;
using System.Runtime.InteropServices;
using System.Threading;
using Avalonia;
using Avalonia.Controls;
using Avalonia.Controls.ApplicationLifetimes;
using Avalonia.Data;
using Avalonia.Input;
using Avalonia.Interactivity;
Expand Down Expand Up @@ -73,6 +73,7 @@ public partial class MainWindow : Window
private bool _focusSidebarSelectionOnNextPageChange;
private TrayService? _trayService;
private bool _allowClose;
private int _isQuitting;

public enum RuntimeNotificationLevel
{
Expand Down Expand Up @@ -136,18 +137,23 @@ protected override void OnOpened(EventArgs e)

protected override void OnClosing(WindowClosingEventArgs e)
{
if (!_allowClose && !Settings.Get(Settings.K.DisableSystemTray))
if (!_allowClose && (OperatingSystem.IsMacOS() || !Settings.Get(Settings.K.DisableSystemTray)))
{
e.Cancel = true;
Hide();
return;
}

e.Cancel = true;
QuitApplication();
}

private void ReleaseWindowResources()
{
SaveGeometryNow();
AvaloniaAutoUpdater.ReleaseLockForAutoupdate_Window = true;
_trayService?.Dispose();
_trayService = null;
base.OnClosing(e);
}

private void Window_KeyDown(object? sender, KeyEventArgs e)
Expand Down Expand Up @@ -1004,14 +1010,25 @@ public void ShowFromTray()
Activate();
}

public bool IsQuitting => Interlocked.CompareExchange(ref _isQuitting, 0, 0) == 1;

public void QuitApplication()
{
if (Interlocked.Exchange(ref _isQuitting, 1) == 1)
return;

_allowClose = true;
ReleaseWindowResources();

if (IsVisible)
Hide();

_ = QuitApplicationAsync();
}

private static async Task QuitApplicationAsync()
{
Logger.Warn("Quitting UniGetUI");
try
{
await AvaloniaBootstrapper.StopIpcApiAsync().WaitAsync(TimeSpan.FromSeconds(5));
Expand All @@ -1026,9 +1043,7 @@ private static async Task QuitApplicationAsync()
Logger.Error(ex);
}

Dispatcher.UIThread.Post(() =>
(global::Avalonia.Application.Current?.ApplicationLifetime
as IClassicDesktopStyleApplicationLifetime)?.Shutdown());
Environment.Exit(0);
}

public static void ApplyProxyVariableToProcess()
Expand Down
Loading