Files
Terminal.Gui/Examples/UICatalog/Scenarios/Shortcuts.cs
Tig 7422385457 Fixes #4057 - MASSIVE! Fully implements ColorScheme->Scheme + VisualRole + Colors.->SchemeManager. (#4062)
* touching publish.yml

* ColorScheme->Scheme

* ColorScheme->Scheme 2

* Prototype of GetAttributeForRole

* Badly broke CM

* Further Badly broke CM

* Refactored CM big-time. View still broken

* All unit test pass again. Tons added. CM is still WIP, but Schemes is not mostly refactored and working.

* Actually:
All unit test pass again.
Tons added.
CM is still WIP, but Schemes is not mostly refactored and working.

* Bug fixes.
DeepMemberWiseClone cleanup

* Further cleanup of Scope<T>, ConfigProperty, etc.

* Made ConfigManager thread safe.

* WIP: Broken

* WIP: new deep clone impl

* WIP: new deep clone impl is done. Now fixing CM

* WIP:
- config.md
- Working on AOT clean up
- Core CM is broken; but known.

* WIP

* Merged.
Removed CM from Application.Init

* WIP

* More WIP; Less broke

* All CM unit tests pass... Not sure if it actually works though

* All unit tests pass... Themes are broken though in UI Cat

* CM Ready for review?

* Fixed failures due to TextStyles PR

* Working on Scheme/Attribute

* Working on Scheme/Attribute 2

* Working on Scheme/Attribute 3

* Working on Scheme/Attribute 4

* Working on Scheme/Attribute 5

* Working on Scheme/Attribute 6

* Added test to show how awful memory usage is

* Improved schema. Updated config.json

* Nade Scope<T> concurrentdictionary and added test to prove

* Made Themes ConcrurrentDictionary. Added bunches of tests

* Code cleanup

* Code cleanup 2

* Code cleanup 3

* Tweaking Scheme

* ClearJsonErrors

* ClearJsonErrors2

* Updated Attribute API

* It all (mostly) works!

* Skip odd unit test

* Messed with Themes

* Theme tweaks

* Code reorg. New .md stuff

* Fixed Enabled. Added mock driver

* Fixed a bunch of View.Enabled related issues

* Scheme -> Get/SetScheme()

* Cleanup

* Cleanup2

* Broke something

* Fixed everything

* Made CM.Enable better

* Text Style Scenario

* Added comments

* Fixed UI Catalog Theme Changing

* Fixed more dynamic CM update stuff

* Warning cleanup

* New Default Theme

* fixed unit test

* Refactoring Scheme and Attribute to fix inheritance

* more unit tests

* ConfigProperty is not updating schemes correctly

* All unit tests pass.
Code cleanup

* All unit tests pass.
Code cleanup2

* Fixed unit tests

* Upgraded TextField and TextView

* Fixed TextView !Enabled bug

* More updates to TextView. More unit tests for SchemeManager

* Upgraded CharMap

* API docs

* Fixe HexView API

* upgrade HexView

* Fixed shortcut KeyView

* Fixed more bugs. Added new themes

* updated themes

* upgraded Border

* Fixed themes memory usage...mostly

* Fixed themes memory usage...mostly2

* Fixed themes memory usage...2

* Fixed themes memory usage...3

* Added new colors

* Fixed GetHardCodedConfig bug

* Added Themes Scenario - WIP

* Added Themes Scenario

* Tweaked Themes Scenario

* Code cleanup

* Fixed json schmea

* updated deepdives

* updated deepdives

* Tweaked Themes Scenario

* Made Schemes a concurrent dict

* Test cleanup

* Thread safe ConfigProperty tests

* trying to make things more thread safe

* more trying to make things more thread safe

* Fixing bugs in shadowview

* Fixing bugs in shadowview 2

* Refactored GetViewsUnderMouse to GetViewsUnderLocation etc...

* Fixed dupe unit tests?

* Added better description of layout and coordiantes to deep dive

* Added better description of layout and coordiantes to deep dive

* Modified tests that call v2.AddTimeout; they were returning true which means restart the timer!
This was causing mac/linux unit test failures.
I think

* Fixed auto scheme.
Broke TextView/TextField selection

* Realized Attribute.IsExplicitlySet is stupid; just use nullable

* Fixed Attribute. Simplified. MOre theme testing

* Updated themes again

* GetViewsUnderMouse to GetViewsUnderLocation broke TransparentMouse.

* Fixing mouseunder bugs

* rewriting...

* All working again.
Shadows are now slick as snot.
GetViewsUnderLocation is rewritten to actually work and be readable.
Tons more low-level unit tests.
Margin is now actually ViewportSettings.Transparent.

* Code cleanup

* Code cleanup

* Code cleanup of color apis

* Fixed Hover/Highlight

* Update Examples/UICatalog/Scenarios/AllViewsTester.cs

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* Update Examples/UICatalog/Scenarios/CharacterMap/CharacterMap.cs

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* Update Examples/UICatalog/Scenarios/Clipping.cs

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* Fixed race condition?

* reverted

* Simplified Attribute API by removing events from SetAttributeForRole

* Removed recursion from GetViewsAtLocation

* Removed unneeded code

* Code clean up.
Fixed Scheme bug.

* reverted temporary disable

* Adjusted scheme algo

* Upgraded TextValidateField

* Fixed TextValidate bugs

* Tweaks

* Frameview rounded border by default

* API doc cleanup

* Readme fix

* Addressed tznind feeback

* Fixed more unit test issues by protecting Application statics from being set if Application.Initialized is not true

* Fixed more unit test issues by protecting Application statics from being set if Application.Initialized is not true 2

* cleanup

---------

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
2025-05-29 13:55:54 -06:00

535 lines
24 KiB
C#

#nullable enable
using System.Collections.ObjectModel;
using Terminal.Gui;
using Timer = System.Timers.Timer;
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),
SchemeName = "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
};
// ((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 (View 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
};
((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 (View 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",
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",
BindKeyToApplication = true
};
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
};
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),
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),
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.SchemeName = framedShortcut.CommandView.SchemeName = "Error";
framedShortcut.HelpView.Margin!.SchemeName = framedShortcut.HelpView.SchemeName = "Dialog";
framedShortcut.KeyView.Margin!.SchemeName = framedShortcut.KeyView.SchemeName = "Menu";
}
framedShortcut.SchemeName = "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.Handled = true;
};
bgColor.ColorChanged += (o, args) =>
{
if (o is { })
{
eventSource.Add ($"ColorChanged: {o.GetType ().Name} - {args.CurrentValue}");
eventLog.MoveDown ();
Application.Top.SetScheme (
new (Application.Top.GetScheme ())
{
Normal = new (
Application.Top!.GetAttributeForRole (VisualRole.Normal).Foreground,
args.CurrentValue,
Application.Top!.GetAttributeForRole (VisualRole.Normal).Style)
});
}
};
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,
BindKeyToApplication = true,
Title = "Quit",
HelpText = "App Scope"
};
appQuitShortcut.Accepting += (o, args) => { Application.RequestStop (); };
Application.Top.Add (appQuitShortcut);
foreach (Shortcut shortcut in Application.Top.SubViews.OfType<Shortcut> ())
{
shortcut.Selecting += (o, args) =>
{
if (args.Handled)
{
return;
}
eventSource.Add ($"{shortcut!.Id}.Selecting: {shortcut!.CommandView.Text} {shortcut!.CommandView.GetType ().Name}");
eventLog.MoveDown ();
};
shortcut.CommandView.Selecting += (o, args) =>
{
if (args.Handled)
{
return;
}
eventSource.Add (
$"{shortcut!.Id}.CommandView.Selecting: {shortcut!.CommandView.Text} {shortcut!.CommandView.GetType ().Name}");
eventLog.MoveDown ();
args.Handled = 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.Handled = 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.Handled = true;
var view = sender as View;
MessageBox.Query ("Hi", $"You clicked {view?.Text}", "_Ok");
}
}