using Avalonia; using Avalonia.Controls; using Avalonia.Layout; using FinalToken.Core; using FinalToken.Core.StreamDeck; namespace FinalToken.App; public sealed class SettingsWindow : Window { public SettingsWindow(Settings initial, PluginInstallStatus status, Func save, Action? quit = null, Action? openFolder = null) { Title = "FinalToken settings"; Width = 760; Height = 900; MinWidth = 600; MinHeight = 500; Ui.Theme(this); var body = new StackPanel { Spacing = 16, Margin = new Thickness(24) }; body.Children.Add(Ui.Text("Settings", 24)); var general = Ui.Stack(Ui.Text("General", 15)); var refresh = Number(general, "Token refresh (seconds, 1–60)", (decimal)initial.RefreshInterval.TotalSeconds, 1, 60); var probe = Number(general, "Limit probe (minutes, 1–60)", (decimal)initial.ProbeInterval.TotalMinutes, 1, 60); var threshold = Number(general, "Pace threshold (points)", (decimal)initial.PaceThreshold, 0.1m, 100); var startup = Check(general, "Start with the OS", initial.StartWithOs); var tray = Check(general, "Close to tray", initial.CloseToTray); body.Children.Add(Ui.Card(general)); var popups = Ui.Stack(Ui.Text("Popups", 15)); var ahead = Check(popups, "Ahead popups", initial.PopupAhead); var behind = Check(popups, "Behind popups", initial.PopupBehind); var claudePopups = PopupControls(popups, "Claude", initial.Claude); var codexPopups = PopupControls(popups, "Codex", initial.Codex); body.Children.Add(Ui.Card(popups)); var providers = Ui.Stack(Ui.Text("Providers", 15)); var claude = ProviderControls(providers, "Claude", initial.Claude); var codex = ProviderControls(providers, "Codex", initial.Codex); body.Children.Add(Ui.Card(providers)); var streamDeck = Ui.Stack(Ui.Text("Stream Deck", 15), Ui.Text($"{status.State} · shipped {status.ShippedVersion ?? "none"} · installed {status.InstalledVersion ?? "none"}", 13, "Secondary")); var selectedMode = initial.StreamDeckInstallMode; streamDeck.Children.Add(PanelWindow.Segments(["Off", "Ask", "Automatic"], (int)selectedMode, i => selectedMode = (InstallMode)i)); streamDeck.Children.Add(Ui.Text("Dismissed plugin version (clear to ask again)", 12, "Secondary")); var dismissed = new TextBox { Text = initial.DismissedPluginVersion ?? "" }; streamDeck.Children.Add(dismissed); body.Children.Add(Ui.Card(streamDeck)); var about = Ui.Stack(Ui.Text("About", 15), Ui.Text("FinalToken " + typeof(Program).Assembly.GetName().Version?.ToString(3)), Ui.Text(DataPaths.DataDirectory, 12, "Secondary")); var error = Ui.Text("", 12, "Secondary"); about.Children.Add(Ui.Button("Open folder", () => { try { if (openFolder is not null) openFolder(DataPaths.DataDirectory); else System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo(DataPaths.DataDirectory) { UseShellExecute = true }); } catch (Exception e) when (e is IOException or UnauthorizedAccessException or System.ComponentModel.Win32Exception or InvalidOperationException) { error.Text = "Could not open the data folder."; } })); body.Children.Add(Ui.Card(about)); var footer = new Grid { ColumnDefinitions = new ColumnDefinitions("Auto,*,Auto,Auto"), ColumnSpacing = 8, Margin = new Thickness(24, 16) }; var quitButton = Ui.Button("Quit FinalToken", () => quit?.Invoke()); quitButton.IsEnabled = quit is not null; footer.Children.Add(quitButton); var cancel = Ui.Button("Cancel", Close); Grid.SetColumn(cancel, 2); footer.Children.Add(cancel); var button = Ui.Button("Save", () => { }); Grid.SetColumn(button, 3); footer.Children.Add(button); button.Click += async (_, _) => { var value = initial with { RefreshInterval = TimeSpan.FromSeconds((double)(refresh.Value ?? 0)), ProbeInterval = TimeSpan.FromMinutes((double)(probe.Value ?? 0)), PaceThreshold = (double)(threshold.Value ?? 0), PopupAhead = ahead.IsChecked == true, PopupBehind = behind.IsChecked == true, StartWithOs = startup.IsChecked == true, CloseToTray = tray.IsChecked == true, Claude = claudePopups(claude()), Codex = codexPopups(codex()), StreamDeckInstallMode = selectedMode, DismissedPluginVersion = string.IsNullOrWhiteSpace(dismissed.Text) ? null : dismissed.Text.Trim() }; if (!value.IsValid) { error.Text = "Check the intervals and pace threshold."; return; } button.IsEnabled = false; try { await save(value); Close(); } catch (OperationCanceledException) { error.Text = "Settings save was cancelled."; button.IsEnabled = true; } catch (Exception e) when (e is IOException or UnauthorizedAccessException or InvalidOperationException or ArgumentException) { error.Text = "Could not apply settings: " + e.Message; button.IsEnabled = true; } }; var dock = new DockPanel(); var bottom = Ui.Stack(error, footer); DockPanel.SetDock(bottom, Dock.Bottom); dock.Children.Add(bottom); dock.Children.Add(new ScrollViewer { Content = body }); Content = dock; } private static NumericUpDown Number(StackPanel parent, string label, decimal value, decimal min, decimal max) { var grid = new Grid { ColumnDefinitions = new ColumnDefinitions("*,160"), ColumnSpacing = 16 }; grid.Children.Add(Ui.Text(label, 13, "Secondary")); var control = new NumericUpDown { Value = value, Minimum = min, Maximum = max }; Grid.SetColumn(control, 1); grid.Children.Add(control); parent.Children.Add(grid); return control; } private static CheckBox Check(StackPanel parent, string label, bool value) { var c = new NeutralCheckBox { Content = label, IsChecked = value, Name = label.Replace(" ", "") }; parent.Children.Add(c); return c; } private static Func PopupControls(StackPanel parent, string name, ProviderSettings config) { var row = new StackPanel { Orientation = Orientation.Horizontal, Spacing = 16 }; row.Children.Add(Ui.Text(name)); var enabled = Check(row, "Popups enabled", config.PopupsEnabled); var ahead = Check(row, "Ahead", config.PopupAhead); var behind = Check(row, "Behind", config.PopupBehind); foreach (var c in row.Children.OfType()) c.Name = name + c.Name; parent.Children.Add(row); return value => value with { PopupsEnabled = enabled.IsChecked == true, PopupAhead = ahead.IsChecked == true, PopupBehind = behind.IsChecked == true }; } private static Func ProviderControls(StackPanel parent, string name, ProviderSettings config) { parent.Children.Add(Ui.Text(name)); var enabled = Check(parent, "Enabled", config.Enabled); enabled.Name = name + "Enabled"; parent.Children.Add(Ui.Text("CLI path (empty uses PATH)", 12, "Secondary")); var path = new TextBox { Name = name + "CliPath", Text = config.CliPath ?? "" }; parent.Children.Add(path); return () => config with { Enabled = enabled.IsChecked == true, CliPath = string.IsNullOrWhiteSpace(path.Text) ? null : path.Text.Trim() }; } }