AstroFrame/src/AstroFrame.Desktop/SettingsWindow.cs

57 lines
3.4 KiB
C#

using Avalonia;
using Avalonia.Controls;
using Avalonia.Layout;
using Avalonia.Media;
namespace AstroFrame.Desktop;
public sealed class SettingsWindow : Window
{
readonly AppUpdates updates;
readonly TextBlock message = Text(""), lastCheck = Text("");
readonly Button check = new() { Content = "Check for updates", Padding = new(12, 8) };
readonly Button install = new() { Content = "Install update and restart", Padding = new(12, 8) };
readonly CheckBox automatic = new() { Content = "Automatically check for updates", FontSize = 13 };
public SettingsWindow(AppUpdates updates, Func<Task> installUpdate)
{
this.updates = updates;
Title = "AstroFrame settings"; Width = 760; Height = 530; MinWidth = 620; MinHeight = 390;
Background = Palette.Background; FontFamily = "Inter"; WindowStartupLocation = WindowStartupLocation.CenterOwner;
var root = new Grid { ColumnDefinitions = new("160,*") };
// Add categories and their views here as the application gains more settings.
var categories = new ListBox { ItemsSource = new[] { "Updates" }, SelectedIndex = 0, Background = Palette.Panel, Padding = new(10, 18) };
root.Children.Add(categories);
var page = new StackPanel { Spacing = 18, Margin = new(26), MaxWidth = 650 };
page.Children.Add(Text("Updates", 23));
page.Children.Add(Text($"Installed version {updates.InstalledVersion}", 15));
page.Children.Add(Text("Receive stable AstroFrame releases and install them when you are ready to restart."));
page.Children.Add(automatic); page.Children.Add(message); page.Children.Add(lastCheck);
var buttons = new WrapPanel { Orientation = Orientation.Horizontal };
check.Margin = new(0, 0, 10, 10); install.Margin = new(0, 0, 0, 10);
buttons.Children.Add(check); buttons.Children.Add(install); page.Children.Add(buttons);
var scroll = new ScrollViewer { Content = page, HorizontalScrollBarVisibility = Avalonia.Controls.Primitives.ScrollBarVisibility.Disabled };
Grid.SetColumn(scroll, 1); root.Children.Add(scroll); Content = root;
check.Click += async (_, _) => await updates.Check();
install.Click += async (_, _) => await installUpdate();
automatic.IsChecked = updates.Settings.AutomaticallyCheckForUpdates;
automatic.IsCheckedChanged += (_, _) =>
{
bool requested = automatic.IsChecked == true;
if (requested != updates.Settings.AutomaticallyCheckForUpdates && !updates.SetAutomaticChecks(requested)) automatic.IsChecked = updates.Settings.AutomaticallyCheckForUpdates;
};
updates.Changed += Refresh;
Closed += (_, _) => updates.Changed -= Refresh;
Refresh();
}
static TextBlock Text(string value, double size = 13) => new() { Text = value, FontSize = size, Foreground = Palette.Text, TextWrapping = TextWrapping.Wrap };
void Refresh()
{
message.Text = updates.Message;
lastCheck.Text = updates.Settings.LastUpdateCheck is { } time ? $"Last successful check {time.ToLocalTime():g}" : "Last successful check Never";
lastCheck.Foreground = Palette.Muted;
check.IsEnabled = updates.IsInstalled && !updates.Working && !updates.Applying;
install.IsVisible = updates.AvailableVersion != null;
install.IsEnabled = !updates.Working && !updates.Applying;
}
}