using Avalonia.Controls; using Avalonia.Interactivity; using Nonemm.App.Configuration; namespace Nonemm.App.Windows; /// The Buttons tab: the twelve command buttons under the traffic, as N1MM's /// telnet window has. public sealed partial class TelnetWindow { /// N1MM stops at twelve buttons; this shows as many as are defined, and one /// empty row to write the next one in. private const int LeastButtonRows = 12; private readonly List<(TextBox Label, TextBox Command)> buttonRows = []; private void LoadButtonRows() { buttonRows.Clear(); ButtonRows.Children.Clear(); ButtonRows.RowDefinitions.Clear(); IReadOnlyList stored = Buttons(); int rows = Math.Max(LeastButtonRows, stored.Count + 1); for (int at = 0; at < rows; at++) { ButtonRows.RowDefinitions.Add(new RowDefinition(GridLength.Auto)); StoredTelnetButton button = at < stored.Count ? stored[at] : new StoredTelnetButton(); TextBox label = Box(at, 0, button.Label, "label"); TextBox command = Box(at, 2, button.Command, "sh/dx; sh/wwv"); buttonRows.Add((label, command)); } } private TextBox Box(int row, int column, string text, string placeholder) { TextBox box = new() { Text = text, PlaceholderText = placeholder, Margin = new Avalonia.Thickness(0, 0, 0, 4), }; Grid.SetRow(box, row); Grid.SetColumn(box, column); ButtonRows.Children.Add(box); return box; } private void OnSaveButtons(object? sender, RoutedEventArgs e) { session.Save(session.Settings with { TelnetButtons = [.. buttonRows .Select(row => new StoredTelnetButton { Label = (row.Label.Text ?? "").Trim(), Command = (row.Command.Text ?? "").Trim(), }) .Where(button => button.Label.Length > 0 && button.Command.Length > 0)], }); LoadButtons(); Show("*** buttons saved", NoticeColour); } private void OnResetButtons(object? sender, RoutedEventArgs e) { session.Save(session.Settings with { TelnetButtons = [] }); LoadButtons(); LoadButtonRows(); Show("*** buttons back to the defaults", NoticeColour); } }