Files
Terminal.Gui/Examples/UICatalog/Scenarios/CharacterMap/CharacterMap.cs
Copilot f3fc20306e Remove TextField.Caption property; use Title with hotkey navigation support (#4352)
* Initial plan

* Initial exploration - understanding TextField Caption and Title

Co-authored-by: tig <585482+tig@users.noreply.github.com>

* Remove TextField.Caption and use Title instead with hotkey support

Co-authored-by: tig <585482+tig@users.noreply.github.com>

* Add defensive check to ensure TitleTextFormatter.Text is set

Co-authored-by: tig <585482+tig@users.noreply.github.com>

* Final changes - all tests passing

Co-authored-by: tig <585482+tig@users.noreply.github.com>

* Fixed bugs.

* Add comprehensive tests for caption rendering with attributes validation

Co-authored-by: tig <585482+tig@users.noreply.github.com>

* Fix: Disable TextField hotkey functionality to prevent input interception

TextField's Title is used as a caption/placeholder, not for hotkey
navigation. Hotkey visual formatting (underline) is still rendered
in the caption, but hotkey functionality is disabled to prevent
keys like 'E' and 'F' from being intercepted when typing in the field.

Updated test to expect "_Find" instead of "Find" to match resource change.

Co-authored-by: tig <585482+tig@users.noreply.github.com>

* Fix: Support Alt+key hotkey navigation while allowing normal typing

Override AddKeyBindingsForHotKey to only bind Alt+key combinations
(e.g., Alt+F for "_Find"), not the bare keys. This allows:
- Alt+F to navigate to the TextField with Title="_Find"
- Normal typing of 'F', 'E', etc. without interception

Previously, both bare key and Alt+key were bound, causing typing
issues. Now TextField properly supports hotkey navigation without
interfering with text input.

Co-authored-by: tig <585482+tig@users.noreply.github.com>

* Changes before error encountered

Co-authored-by: tig <585482+tig@users.noreply.github.com>

* Refactor hotkey handling to support command context

Refactored `RaiseHandlingHotKey` to accept an `ICommandContext? ctx` parameter, enabling context-aware hotkey handling. Updated `Command.HotKey` definitions across multiple classes (`View`, `CheckBox`, `Label`, `MenuBarv2`, `RadioGroup`, `TextField`) to utilize the new context parameter.

Enhanced XML documentation for `RaiseHandlingHotKey` to clarify its usage and return values. Added a context-aware hotkey handler to `TextField` with additional logic for focus handling.

Refactored attribute initialization and improved code readability in `TextField` by aligning parameters and removing unused `HotKeySpecifier` initialization. These changes improve flexibility, maintainability, and consistency across the codebase.

* Remove TextField.Caption property; use Title with hotkey navigation support

Co-authored-by: tig <585482+tig@users.noreply.github.com>

---------

Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: tig <585482+tig@users.noreply.github.com>
Co-authored-by: Tig <tig@users.noreply.github.com>
2025-10-27 07:50:09 -06:00

405 lines
14 KiB
C#

#nullable enable
using System.Globalization;
using System.Text;
namespace UICatalog.Scenarios;
/// <summary>
/// This Scenario demonstrates building a custom control (a class deriving from View) that: - Provides a
/// "Character Map" application (like Windows' charmap.exe). - Helps test unicode character rendering in Terminal.Gui -
/// Illustrates how to do infinite scrolling
/// </summary>
[ScenarioMetadata ("Character Map", "Unicode viewer. Demos infinite content drawing and scrolling.")]
[ScenarioCategory ("Text and Formatting")]
[ScenarioCategory ("Drawing")]
[ScenarioCategory ("Controls")]
[ScenarioCategory ("Layout")]
[ScenarioCategory ("Scrolling")]
public class CharacterMap : Scenario
{
private Label? _errorLabel;
private TableView? _categoryList;
private CharMap? _charMap;
private OptionSelector? _unicodeCategorySelector;
public override List<Key> GetDemoKeyStrokes ()
{
List<Key> keys = new ();
for (var i = 0; i < 200; i++)
{
keys.Add (Key.CursorDown);
}
// Category table
keys.Add (Key.Tab.WithShift);
// Block elements
keys.Add (Key.B);
keys.Add (Key.L);
keys.Add (Key.Tab);
for (var i = 0; i < 200; i++)
{
keys.Add (Key.CursorLeft);
}
return keys;
}
// Don't create a Window, just return the top-level view
public override void Main ()
{
Application.Init ();
var top = new Window
{
BorderStyle = LineStyle.None
};
_charMap = new ()
{
X = 0,
Y = 1,
Height = Dim.Fill ()
// SchemeName = "Base"
};
top.Add (_charMap);
var jumpLabel = new Label
{
X = Pos.Right (_charMap) + 1,
Y = Pos.Y (_charMap),
HotKeySpecifier = (Rune)'_',
Text = "_Jump To:"
//SchemeName = "Dialog"
};
top.Add (jumpLabel);
var jumpEdit = new TextField
{
X = Pos.Right (jumpLabel) + 1,
Y = Pos.Y (_charMap),
Width = 17,
Title = "e.g. 01BE3 or ✈"
//SchemeName = "Dialog"
};
top.Add (jumpEdit);
_charMap.SelectedCodePointChanged += (sender, args) =>
{
if (Rune.IsValid (args.Value))
{
jumpEdit.Text = ((Rune)args.Value).ToString ();
}
else
{
jumpEdit.Text = $"U+{args.Value:x5}";
}
};
_errorLabel = new ()
{
X = Pos.Right (jumpEdit) + 1,
Y = Pos.Y (_charMap),
SchemeName = "error",
Text = "err",
Visible = false
};
top.Add (_errorLabel);
jumpEdit.Accepting += JumpEditOnAccept;
_categoryList = new ()
{
X = Pos.Right (_charMap),
Y = Pos.Bottom (jumpLabel),
Height = Dim.Fill ()
//SchemeName = "Dialog"
};
_categoryList.FullRowSelect = true;
_categoryList.MultiSelect = false;
_categoryList.Style.ShowVerticalCellLines = false;
_categoryList.Style.AlwaysShowHeaders = true;
var isDescending = false;
_categoryList.Table = CreateCategoryTable (0, isDescending);
// if user clicks the mouse in TableView
_categoryList.MouseClick += (s, e) =>
{
_categoryList.ScreenToCell (e.Position, out int? clickedCol);
if (clickedCol != null && e.Flags.HasFlag (MouseFlags.Button1Clicked))
{
EnumerableTableSource<UnicodeRange> table = (EnumerableTableSource<UnicodeRange>)_categoryList.Table;
string prevSelection = table.Data.ElementAt (_categoryList.SelectedRow).Category;
isDescending = !isDescending;
_categoryList.Table = CreateCategoryTable (clickedCol.Value, isDescending);
table = (EnumerableTableSource<UnicodeRange>)_categoryList.Table;
_categoryList.SelectedRow = table.Data
.Select ((item, index) => new { item, index })
.FirstOrDefault (x => x.item.Category == prevSelection)
?.index
?? -1;
}
};
int longestName = UnicodeRange.Ranges.Max (r => r.Category.GetColumns ());
_categoryList.Style.ColumnStyles.Add (
0,
new () { MaxWidth = longestName, MinWidth = longestName, MinAcceptableWidth = longestName }
);
_categoryList.Style.ColumnStyles.Add (1, new () { MaxWidth = 1, MinWidth = 6 });
_categoryList.Style.ColumnStyles.Add (2, new () { MaxWidth = 1, MinWidth = 6 });
_categoryList.Width = _categoryList.Style.ColumnStyles.Sum (c => c.Value.MinWidth) + 4;
_categoryList.SelectedCellChanged += (s, args) =>
{
EnumerableTableSource<UnicodeRange> table = (EnumerableTableSource<UnicodeRange>)_categoryList.Table;
_charMap.StartCodePoint = table.Data.ToArray () [args.NewRow].Start;
jumpEdit.Text = $"U+{_charMap.SelectedCodePoint:x5}";
};
top.Add (_categoryList);
var menu = new MenuBarv2
{
Menus =
[
new (
"_File",
new MenuItemv2 []
{
new (
"_Quit",
$"{Application.QuitKey}",
() => Application.RequestStop ()
)
}
),
new (
"_Options",
[CreateMenuShowWidth (), CreateMenuUnicodeCategorySelector ()]
)
]
};
top.Add (menu);
_charMap.Width = Dim.Fill (Dim.Func (v => v!.Frame.Width, _categoryList));
_charMap.SelectedCodePoint = 0;
_charMap.SetFocus ();
Application.Run (top);
top.Dispose ();
Application.Shutdown ();
return;
void JumpEditOnAccept (object? sender, CommandEventArgs e)
{
if (jumpEdit.Text.Length == 0)
{
return;
}
_errorLabel.Visible = true;
uint result = 0;
if (jumpEdit.Text.Length == 1)
{
result = (uint)jumpEdit.Text.ToRunes () [0].Value;
}
else if (jumpEdit.Text.StartsWith ("U+", StringComparison.OrdinalIgnoreCase) || jumpEdit.Text.StartsWith ("\\u"))
{
try
{
result = uint.Parse (jumpEdit.Text [2..], NumberStyles.HexNumber);
}
catch (FormatException)
{
_errorLabel.Text = "Invalid hex value";
return;
}
}
else if (jumpEdit.Text.StartsWith ("0", StringComparison.OrdinalIgnoreCase) || jumpEdit.Text.StartsWith ("\\u"))
{
try
{
result = uint.Parse (jumpEdit.Text, NumberStyles.HexNumber);
}
catch (FormatException)
{
_errorLabel.Text = "Invalid hex value";
return;
}
}
else
{
try
{
result = uint.Parse (jumpEdit.Text, NumberStyles.Integer);
}
catch (FormatException)
{
_errorLabel.Text = "Invalid value";
return;
}
}
if (result > RuneExtensions.MaxUnicodeCodePoint)
{
_errorLabel.Text = "Beyond maximum codepoint";
return;
}
_errorLabel.Visible = false;
EnumerableTableSource<UnicodeRange> table = (EnumerableTableSource<UnicodeRange>)_categoryList!.Table;
_categoryList.SelectedRow = table.Data
.Select ((item, index) => new { item, index })
.FirstOrDefault (x => x.item.Start <= result && x.item.End >= result)
?.index
?? -1;
_categoryList.EnsureSelectedCellIsVisible ();
// Ensure the typed glyph is selected
_charMap.SelectedCodePoint = (int)result;
_charMap.SetFocus ();
// Cancel the event to prevent ENTER from being handled elsewhere
e.Handled = true;
}
}
private EnumerableTableSource<UnicodeRange> CreateCategoryTable (int sortByColumn, bool descending)
{
Func<UnicodeRange, object> orderBy;
var categorySort = string.Empty;
var startSort = string.Empty;
var endSort = string.Empty;
string sortIndicator = descending ? Glyphs.DownArrow.ToString () : Glyphs.UpArrow.ToString ();
switch (sortByColumn)
{
case 0:
orderBy = r => r.Category;
categorySort = sortIndicator;
break;
case 1:
orderBy = r => r.Start;
startSort = sortIndicator;
break;
case 2:
orderBy = r => r.End;
endSort = sortIndicator;
break;
default:
throw new ArgumentException ("Invalid column number.");
}
IOrderedEnumerable<UnicodeRange> sortedRanges = descending
? UnicodeRange.Ranges.OrderByDescending (orderBy)
: UnicodeRange.Ranges.OrderBy (orderBy);
return new (
sortedRanges,
new ()
{
{ $"Category{categorySort}", s => s.Category },
{ $"Start{startSort}", s => $"{s.Start:x5}" },
{ $"End{endSort}", s => $"{s.End:x5}" }
}
);
}
private MenuItemv2 CreateMenuShowWidth ()
{
CheckBox cb = new ()
{
Title = "_Show Glyph Width",
CheckedState = _charMap!.ShowGlyphWidths ? CheckState.Checked : CheckState.None
};
var item = new MenuItemv2 { CommandView = cb };
item.Action += () =>
{
if (_charMap is { })
{
_charMap.ShowGlyphWidths = cb.CheckedState == CheckState.Checked;
}
};
return item;
}
private MenuItemv2 CreateMenuUnicodeCategorySelector ()
{
// First option is "All" (no filter), followed by all UnicodeCategory names
string [] allCategoryNames = Enum.GetNames<UnicodeCategory> ();
var options = new string [allCategoryNames.Length + 1];
options [0] = "All";
Array.Copy (allCategoryNames, 0, options, 1, allCategoryNames.Length);
// TODO: When #4126 is merged update this to use OptionSelector<UnicodeCategory?>
var selector = new OptionSelector
{
AssignHotKeysToCheckBoxes = true,
Options = options
};
_unicodeCategorySelector = selector;
// Default to "All"
selector.SelectedItem = 0;
_charMap!.ShowUnicodeCategory = null;
selector.SelectedItemChanged += (s, e) =>
{
int? idx = selector.SelectedItem;
if (idx is null)
{
return;
}
if (idx.Value == 0)
{
_charMap.ShowUnicodeCategory = null;
}
else
{
// Map index to UnicodeCategory (offset by 1 because 0 is "All")
UnicodeCategory cat = Enum.GetValues<UnicodeCategory> () [idx.Value - 1];
_charMap.ShowUnicodeCategory = cat;
}
};
return new() { CommandView = selector };
}
}