123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519 |
- #nullable enable
- using System;
- using System.Collections.Generic;
- using System.Collections.ObjectModel;
- using System.Linq;
- using System.Timers;
- using Terminal.Gui;
- namespace UICatalog.Scenarios;
- [ScenarioMetadata ("Shortcuts", "Illustrates Shortcut class.")]
- [ScenarioCategory ("Controls")]
- public class Shortcuts : Scenario
- {
- public override void Main ()
- {
- Application.Init ();
- Window app = new ();
- app.Loaded += App_Loaded;
- Application.Run (app);
- app.Dispose ();
- Application.Shutdown ();
- }
- // Setting everything up in Loaded handler because we change the
- // QuitKey and it only sticks if changed after init
- private void App_Loaded (object? sender, EventArgs e)
- {
- Application.QuitKey = Key.F4.WithCtrl;
- Application.Top!.Title = GetQuitKeyAndName ();
- ObservableCollection<string> eventSource = new ();
- var eventLog = new ListView
- {
- Id = "eventLog",
- X = Pos.AnchorEnd (),
- Y = 0,
- Height = Dim.Fill (4),
- ColorScheme = Colors.ColorSchemes ["Toplevel"],
- Source = new ListWrapper<string> (eventSource),
- BorderStyle = LineStyle.Double,
- Title = "E_vents"
- };
- eventLog.Width = Dim.Func (() => Math.Min (Application.Top.Viewport.Width / 2, eventLog?.MaxLength + eventLog!.GetAdornmentsThickness ().Horizontal ?? 0));
- eventLog.Width = Dim.Func (() => Math.Min (eventLog.SuperView!.Viewport.Width / 2, eventLog?.MaxLength + eventLog!.GetAdornmentsThickness ().Horizontal ?? 0));
- Application.Top.Add (eventLog);
- var alignKeysShortcut = new Shortcut
- {
- Id = "alignKeysShortcut",
- X = 0,
- Y = 0,
- Width = Dim.Fill ()! - Dim.Width (eventLog),
- HelpText = "Fill to log",
- CommandView = new CheckBox
- {
- Text = "_Align Keys",
- CanFocus = false,
- HighlightStyle = HighlightStyle.None,
- },
- Key = Key.F5.WithCtrl.WithAlt.WithShift,
- KeyBindingScope = KeyBindingScope.HotKey,
- };
- // ((CheckBox)vShortcut3.CommandView).CheckedStateChanging += (_, args) =>
- ((CheckBox)alignKeysShortcut.CommandView).CheckedStateChanging += (s, e) =>
- {
- if (alignKeysShortcut.CommandView is CheckBox cb)
- {
- eventSource.Add ($"{alignKeysShortcut.Id}.CommandView.CheckedStateChanging: {cb.Text}");
- eventLog.MoveDown ();
- var max = 0;
- IEnumerable<View> toAlign = Application.Top.Subviews.Where (v => v is Shortcut { Width: not DimAbsolute });
- IEnumerable<View> enumerable = toAlign as View [] ?? toAlign.ToArray ();
- if (e.NewValue == CheckState.Checked)
- {
- max = (from Shortcut? peer in enumerable select peer.Key.ToString ().GetColumns ()).Prepend (max).Max ();
- foreach (var view in enumerable)
- {
- var peer = (Shortcut)view;
- max = Math.Max (max, peer.KeyView.Text.GetColumns ());
- }
- }
- foreach (View view in enumerable)
- {
- var peer = (Shortcut)view;
- peer.MinimumKeyTextSize = max;
- }
- }
- };
- Application.Top.Add (alignKeysShortcut);
- var commandFirstShortcut = new Shortcut
- {
- Id = "commandFirstShortcut",
- X = 0,
- Y = Pos.Bottom (alignKeysShortcut),
- Width = Dim.Fill ()! - Dim.Width (eventLog),
- HelpText = "Show Command first",
- CommandView = new CheckBox
- {
- Text = "Command _First",
- CanFocus = false,
- HighlightStyle = HighlightStyle.None,
- },
- Key = Key.F.WithCtrl,
- KeyBindingScope = KeyBindingScope.HotKey,
- };
- ((CheckBox)commandFirstShortcut.CommandView).CheckedState =
- commandFirstShortcut.AlignmentModes.HasFlag (AlignmentModes.EndToStart) ? CheckState.UnChecked : CheckState.Checked;
- ((CheckBox)commandFirstShortcut.CommandView).CheckedStateChanging += (s, e) =>
- {
- if (commandFirstShortcut.CommandView is CheckBox cb)
- {
- eventSource.Add ($"{commandFirstShortcut.Id}.CommandView.CheckedStateChanging: {cb.Text}");
- eventLog.MoveDown ();
- IEnumerable<View> toAlign = Application.Top.Subviews.Where (v => v is Shortcut { Width: not DimAbsolute });
- IEnumerable<View> enumerable = toAlign as View [] ?? toAlign.ToArray ();
- foreach (var view in enumerable)
- {
- var peer = (Shortcut)view;
- if (e.NewValue == CheckState.Checked)
- {
- peer.AlignmentModes &= ~AlignmentModes.EndToStart;
- }
- else
- {
- peer.AlignmentModes |= AlignmentModes.EndToStart;
- }
- }
- }
- };
- Application.Top.Add (commandFirstShortcut);
- var canFocusShortcut = new Shortcut
- {
- Id = "canFocusShortcut",
- X = 0,
- Y = Pos.Bottom (commandFirstShortcut),
- Width = Dim.Fill ()! - Dim.Width (eventLog),
- Key = Key.F4,
- HelpText = "Changes all Command.CanFocus",
- KeyBindingScope = KeyBindingScope.HotKey,
- CommandView = new CheckBox { Text = "_CanFocus" },
- };
- ((CheckBox)canFocusShortcut.CommandView).CheckedStateChanging += (s, e) =>
- {
- if (canFocusShortcut.CommandView is CheckBox cb)
- {
- eventSource.Add ($"Toggle: {cb.Text}");
- eventLog.MoveDown ();
- //cb.CanFocus = e.NewValue == CheckState.Checked;
- foreach (Shortcut peer in Application.Top.Subviews.Where (v => v is Shortcut)!)
- {
- if (peer.CanFocus)
- {
- peer.CommandView.CanFocus = e.NewValue == CheckState.Checked;
- }
- }
- }
- };
- Application.Top.Add (canFocusShortcut);
- var appShortcut = new Shortcut
- {
- Id = "appShortcut",
- X = 0,
- Y = Pos.Bottom (canFocusShortcut),
- Width = Dim.Fill (Dim.Func (() => eventLog.Frame.Width)),
- Title = "A_pp Shortcut",
- Key = Key.F1,
- Text = "Width is DimFill",
- KeyBindingScope = KeyBindingScope.Application,
- };
- Application.Top.Add (appShortcut);
- var buttonShortcut = new Shortcut
- {
- Id = "buttonShortcut",
- X = 0,
- Y = Pos.Bottom (appShortcut),
- Width = Dim.Fill ()! - Dim.Width (eventLog),
- HelpText = "Accepting pops MB",
- CommandView = new Button
- {
- Title = "_Button",
- ShadowStyle = ShadowStyle.None,
- HighlightStyle = HighlightStyle.None
- },
- Key = Key.K,
- KeyBindingScope = KeyBindingScope.HotKey,
- };
- var button = (Button)buttonShortcut.CommandView;
- buttonShortcut.Accepting += Button_Clicked;
- Application.Top.Add (buttonShortcut);
- var radioGroupShortcut = new Shortcut
- {
- Id = "radioGroupShortcut",
- X = 0,
- Y = Pos.Bottom (buttonShortcut),
- Key = Key.F2,
- Width = Dim.Fill ()! - Dim.Width (eventLog),
- KeyBindingScope = KeyBindingScope.HotKey,
- CommandView = new RadioGroup
- {
- Orientation = Orientation.Vertical,
- RadioLabels = ["O_ne", "T_wo", "Th_ree", "Fo_ur"],
- },
- };
- ((RadioGroup)radioGroupShortcut.CommandView).SelectedItemChanged += (o, args) =>
- {
- if (o is { })
- {
- eventSource.Add (
- $"SelectedItemChanged: {o.GetType ().Name} - {args.SelectedItem}");
- eventLog.MoveDown ();
- }
- };
- Application.Top.Add (radioGroupShortcut);
- var sliderShortcut = new Shortcut
- {
- Id = "sliderShortcut",
- X = 0,
- Y = Pos.Bottom (radioGroupShortcut),
- Width = Dim.Fill ()! - Dim.Width (eventLog),
- KeyBindingScope = KeyBindingScope.HotKey,
- HelpText = "Sliders work!",
- CommandView = new Slider<string>
- {
- Orientation = Orientation.Horizontal,
- AllowEmpty = true
- },
- Key = Key.F5,
- };
- ((Slider<string>)sliderShortcut.CommandView).Options = [new () { Legend = "A" }, new () { Legend = "B" }, new () { Legend = "C" }];
- ((Slider<string>)sliderShortcut.CommandView).SetOption (0);
- ((Slider<string>)sliderShortcut.CommandView).OptionsChanged += (o, args) =>
- {
- eventSource.Add ($"OptionsChanged: {o?.GetType ().Name} - {string.Join (",", ((Slider<string>)o!)!.GetSetOptions ())}");
- eventLog.MoveDown ();
- };
- Application.Top.Add (sliderShortcut);
- var noCommandShortcut = new Shortcut
- {
- Id = "noCommandShortcut",
- X = 0,
- Y = Pos.Bottom (sliderShortcut),
- Width = Dim.Width (sliderShortcut),
- HelpText = "No Command",
- Key = Key.D0
- };
- Application.Top.Add (noCommandShortcut);
- var noKeyShortcut = new Shortcut
- {
- Id = "noKeyShortcut",
- X = 0,
- Y = Pos.Bottom (noCommandShortcut),
- Width = Dim.Width (noCommandShortcut),
- Title = "No Ke_y",
- HelpText = "Keyless",
- };
- Application.Top.Add (noKeyShortcut);
- var noHelpShortcut = new Shortcut
- {
- Id = "noHelpShortcut",
- X = 0,
- Y = Pos.Bottom (noKeyShortcut),
- Width = Dim.Width (noKeyShortcut),
- Key = Key.F6,
- Title = "Not _very much help",
- HelpText = "",
- };
- Application.Top.Add (noHelpShortcut);
- noHelpShortcut.SetFocus ();
- var framedShortcut = new Shortcut
- {
- Id = "framedShortcut",
- X = 0,
- Y = Pos.Bottom (noHelpShortcut) + 1,
- Title = "Framed",
- Key = Key.K.WithCtrl,
- Text = "Resize frame",
- BorderStyle = LineStyle.Dotted,
- Arrangement = ViewArrangement.RightResizable | ViewArrangement.BottomResizable,
- };
- framedShortcut.Orientation = Orientation.Horizontal;
- if (framedShortcut.Padding is { })
- {
- framedShortcut.Padding.Thickness = new (0, 1, 0, 0);
- framedShortcut.Padding.Diagnostics = ViewDiagnosticFlags.Ruler;
- }
- if (framedShortcut.CommandView.Margin is { })
- {
- framedShortcut.CommandView.Margin.ColorScheme = framedShortcut.CommandView.ColorScheme = Colors.ColorSchemes ["Error"];
- framedShortcut.HelpView.Margin!.ColorScheme = framedShortcut.HelpView.ColorScheme = Colors.ColorSchemes ["Dialog"];
- framedShortcut.KeyView.Margin!.ColorScheme = framedShortcut.KeyView.ColorScheme = Colors.ColorSchemes ["Menu"];
- }
- framedShortcut.ColorScheme = Colors.ColorSchemes ["Toplevel"];
- Application.Top.Add (framedShortcut);
- // Horizontal
- var progressShortcut = new Shortcut
- {
- Id = "progressShortcut",
- X = Pos.Align (Alignment.Start, AlignmentModes.IgnoreFirstOrLast, 1),
- Y = Pos.AnchorEnd () - 1,
- Key = Key.F7,
- HelpText = "Horizontal",
- };
- progressShortcut.CommandView = new ProgressBar
- {
- Text = "Progress",
- Title = "P",
- Fraction = 0.5f,
- Width = 10,
- Height = 1,
- ProgressBarStyle = ProgressBarStyle.Continuous
- };
- progressShortcut.CommandView.Width = 10;
- progressShortcut.CommandView.Height = 1;
- progressShortcut.CommandView.CanFocus = false;
- Timer timer = new (10)
- {
- AutoReset = true,
- };
- timer.Elapsed += (o, args) =>
- {
- if (progressShortcut.CommandView is ProgressBar pb)
- {
- if (pb.Fraction == 1.0)
- {
- pb.Fraction = 0;
- }
- pb.Fraction += 0.01f;
- Application.Wakeup ();
- pb.SetNeedsDraw ();
- }
- };
- timer.Start ();
- Application.Top.Add (progressShortcut);
- var textField = new TextField ()
- {
- Text = "Edit me",
- Width = 10,
- Height = 1,
- };
- var textFieldShortcut = new Shortcut
- {
- Id = "textFieldShortcut",
- X = Pos.Align (Alignment.Start, AlignmentModes.IgnoreFirstOrLast, 1),
- Y = Pos.AnchorEnd () - 1,
- Key = Key.F8,
- HelpText = "TextField",
- CanFocus = true,
- CommandView = textField,
- };
- textField.CanFocus = true;
- Application.Top.Add (textFieldShortcut);
- var bgColorShortcut = new Shortcut
- {
- Id = "bgColorShortcut",
- X = Pos.Align (Alignment.Start, AlignmentModes.IgnoreFirstOrLast, 1),
- Y = Pos.AnchorEnd (),
- Key = Key.F9,
- HelpText = "Cycles BG Color",
- };
- var bgColor = new ColorPicker16 ()
- {
- BoxHeight = 1,
- BoxWidth = 1,
- };
- bgColorShortcut.Selecting += (o, args) =>
- {
- //args.Cancel = true;
- };
- bgColorShortcut.Accepting += (o, args) =>
- {
- if (bgColor.SelectedColor == ColorName16.White)
- {
- bgColor.SelectedColor = ColorName16.Black;
- return;
- }
- bgColor.SelectedColor++;
- args.Cancel = true;
- };
- bgColor.ColorChanged += (o, args) =>
- {
- if (o is { })
- {
- eventSource.Add ($"ColorChanged: {o.GetType ().Name} - {args.CurrentValue}");
- eventLog.MoveDown ();
- Application.Top.ColorScheme = new ColorScheme (Application.Top.ColorScheme)
- {
- Normal = new (Application.Top!.GetNormalColor ().Foreground, args.CurrentValue),
- };
- }
- };
- bgColorShortcut.CommandView = bgColor;
- Application.Top.Add (bgColorShortcut);
- var appQuitShortcut = new Shortcut
- {
- Id = "appQuitShortcut",
- X = Pos.Align (Alignment.Start, AlignmentModes.IgnoreFirstOrLast, 1),
- Y = Pos.AnchorEnd () - 1,
- Key = Key.Esc,
- KeyBindingScope = KeyBindingScope.Application,
- Title = "Quit",
- HelpText = "App Scope",
- };
- appQuitShortcut.Accepting += (o, args) =>
- {
- Application.RequestStop ();
- };
- Application.Top.Add (appQuitShortcut);
- foreach (View sh in Application.Top.Subviews.Where (v => v is Shortcut)!)
- {
- if (sh is Shortcut shortcut)
- {
- shortcut.Selecting += (o, args) =>
- {
- if (args.Cancel)
- {
- return;
- }
- eventSource.Add ($"{shortcut!.Id}.Selecting: {shortcut!.CommandView.Text} {shortcut!.CommandView.GetType ().Name}");
- eventLog.MoveDown ();
- };
- shortcut.CommandView.Selecting += (o, args) =>
- {
- if (args.Cancel)
- {
- return;
- }
- eventSource.Add ($"{shortcut!.Id}.CommandView.Selecting: {shortcut!.CommandView.Text} {shortcut!.CommandView.GetType ().Name}");
- eventLog.MoveDown ();
- args.Cancel = true;
- };
- shortcut.Accepting += (o, args) =>
- {
- eventSource.Add ($"{shortcut!.Id}.Accepting: {shortcut!.CommandView.Text} {shortcut!.CommandView.GetType ().Name}");
- eventLog.MoveDown ();
- // We don't want this to exit the Scenario
- args.Cancel = true;
- };
- shortcut.CommandView.Accepting += (o, args) =>
- {
- eventSource.Add ($"{shortcut!.Id}.CommandView.Accepting: {shortcut!.CommandView.Text} {shortcut!.CommandView.GetType ().Name}");
- eventLog.MoveDown ();
- };
- }
- }
- }
- private void Button_Clicked (object? sender, CommandEventArgs e)
- {
- e.Cancel = true;
- View? view = sender as View;
- MessageBox.Query ("Hi", $"You clicked {view?.Text}", "_Ok");
- }
- }
|