47 lines
2 KiB
C#
47 lines
2 KiB
C#
using Avalonia.Controls;
|
|
using Avalonia.Threading;
|
|
using Avalonia.Controls.ApplicationLifetimes;
|
|
|
|
namespace AstroFrame.Desktop;
|
|
|
|
public sealed partial class MainWindow
|
|
{
|
|
readonly AppUpdates updates = new();
|
|
readonly Button settingsButton = Button("Settings");
|
|
readonly DispatcherTimer updateTimer = new() { Interval = TimeSpan.FromHours(6) };
|
|
SettingsWindow? settingsWindow;
|
|
|
|
void InitializeUpdates()
|
|
{
|
|
settingsButton.Click += (_, _) => ShowSettings();
|
|
updates.Changed += () =>
|
|
{
|
|
settingsButton.Content = updates.AvailableVersion == null ? "Settings" : "Settings · Update available";
|
|
if (updates.Applying) { analyze.IsEnabled = false; export.IsEnabled = false; }
|
|
};
|
|
bool diagnostic = Program.Arguments.Any(a => a.StartsWith("--capture") || a == "--smoke");
|
|
if (!diagnostic)
|
|
{
|
|
Opened += async (_, _) => await updates.Check(automatic: true);
|
|
updateTimer.Tick += async (_, _) => await updates.Check(automatic: true);
|
|
updateTimer.Start();
|
|
}
|
|
Closed += (_, _) => { updateTimer.Stop(); updates.Dispose(); settingsWindow?.Close(); };
|
|
}
|
|
public SettingsWindow ShowSettings()
|
|
{
|
|
if (settingsWindow != null) { settingsWindow.Activate(); return settingsWindow; }
|
|
settingsWindow = new SettingsWindow(updates, InstallUpdate);
|
|
settingsWindow.Closed += (_, _) => settingsWindow = null;
|
|
settingsWindow.Show(this);
|
|
return settingsWindow;
|
|
}
|
|
async Task InstallUpdate()
|
|
{
|
|
bool analyzeEnabled = analyze.IsEnabled, exportEnabled = export.IsEnabled;
|
|
bool restarting = await updates.Install(() => !busy && !saving, async () => project == null || await Save(),
|
|
project == null ? [] : ["--project", projectDirectory]);
|
|
if (restarting && Avalonia.Application.Current?.ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop) desktop.Shutdown();
|
|
else { analyze.IsEnabled = analyzeEnabled; export.IsEnabled = exportEnabled; }
|
|
}
|
|
}
|