Files
Terminal.Gui/UICatalog/Scenarios/CharacterMap.cs
Tig dcb3b359ad Fixes #2926 - Refactor KeyEvent and KeyEventEventArgs to simplify (#2927)
* Adds basic MainLoop unit tests

* Remove WinChange action from Curses

* Remove WinChange action from Curses

* Remove ProcessInput action from Windows MainLoop

* Simplified MainLoop/ConsoleDriver by making MainLoop internal and moving impt fns to Application

* Modernized Terminal resize events

* Modernized Terminal resize events

* Removed un used property

* for _isWindowsTerminal devenv->wininit; not sure what changed

* Modernized mouse/keyboard events (Action->EventHandler)

* Updated OnMouseEvent API docs

* Using WT_SESSION to detect WT

* removes hacky GetParentProcess

* Updates to fix #2634 (clear last line)

* removes hacky GetParentProcess2

* Addressed mac resize issue

* Addressed mac resize issue

* Removes ConsoleDriver.PrepareToRun, has Init return MainLoop

* Removes unneeded Attribute methods

* Removed GetProcesssName

* Removed GetProcesssName

* Refactored KeyEvent and KeyEventEventArgs into a single class

* Revert "Refactored KeyEvent and KeyEventEventArgs into a single class"

This reverts commit 88a00658db.

* Fixed key repeat issue; reverted stupidity on 1049/1047 confusion

* Updated CSI API Docs

* merge

* Rearranged Event.cs to Keyboard.cs and Mouse.cs

* Renamed KeyEventEventArgs KeyEventArgs

* temp renamed KeyEvent OldKeyEvent

* Merged KeyEvent into KeyEventArgs

* Renamed Application.ProcessKey members

* Renamed Application.ProcessKey members

* Renamed Application.ProcessKey members

* Added Responder.KeyPressed

* Removed unused references

* Fixed arg naming

* InvokeKeybindings->InvokeKeyBindings

* InvokeKeybindings->InvokeKeyBindings

* Fixed unit tests fail

* More progress on refactoring key input; still broken and probably wrong

* Moved OnKeyPressed out of Responder and made ProcessKeyPrssed non-virtual

* Updated API docs

* Moved key handling from Responder to View

* Updated API docs

* Updated HotKey API docs

* Updated shortcut API docs

* Fixed responder unit tests

* Removed Shortcut from View as it is not used

* Removed unneeded OnHotKey override from Button

* Fixed BackTab logic

* Button now uses Key Bindings exclusively

* Button now uses Key Bindings exclusively

* Updated keyboard.md docs

* Fixed unit tests to account for Toplevel handling default button

* Added View.InvokeCommand API

* Modernized RadioGroup

* Removed ColdKey

* Modernized (partially) StatusBar

* Worked around FileDialog issue with Ctrl-F

* Fixed driver unit test; view must be focused to reciev key pressed

* Application code cleanup

* Start on updaing menu

* Menu now mostly works

* Menu Select refinement

* Fixed known menu bugs!

* Enabled HotKey to cause focus- experimental

* Fixes #3022 & adds unit test to prove it

* Actually Fixes #3022 & adds unit test to prove it

* Working through hotkey issues

* Misc fixes

* removed hot/cold key stuff from Keys scenario

* Fixed scenarios

* Simplified shortcut string handling

* Modernized Checkbox

* Modernized TileView

* Updated API docs

* Updated API docs

* attempting to publish v2 docs

* Revert "attempting to publish v2 docs"

This reverts commit 59dcec111b.

* Playing with api docs

* Removed Key.BackTab

* Removed Caps/Scroll/Numlock

* Partial removal of keymodifiers - unit tests pass

* Partial removal of keymodifiers - broke netdriver somewhere

* WindowsDriver & added KeyEventArgsTests

* Fixing menu shortcut/hotkeys - broke Menu.cs into separate files

* Fixed MenuBar!

* Finished modernizing Menu/MenuBar

* Removed Key.a-z. Broke lots of stuff

* checkout@v4

* progress on key mapping and formatting

* VK tests are still failing

* Fixed some unit tests

* Added Hotkey and Keybinding unit tests

* fixed unit test

* All unit tests pass again...

* Fixed broken unit tests

* KeyEventArgs.KeyValue -> AsRune

* Fixed bugs. Still some broken

* Added KeyEventArgs.IsAlpha. Added KeyEventArgs.cast ops. Fixed bugs. Unit tests pass

* Fixed WindowsDriver

* Oops.

* Refactoring based on bdisp's help. Not complete!

* removed calling into subviews from OnKeyBindings

* removed calling into subviews from OnKeyBindings

* Improved View KeyEvent unit tests

* More hotkey unit tests

* BIg change - Got rid of KeyPress w/in Application/Drivers

* Unit tests now pass again

* Refreshed API docs

* Better HotKey logic. More progress. Getting close.

* Fixed handling of shifted chars like ö

* Minor code cleanup

* Minor code cleanup2

* Why is build Action failing?

* Why is build Action failing??

* upgraded to .net8 to try to fix weird CI/CD build errors

* upgraded to .net8 to try to fix weird CI/CD build errors2

* Disabling TextViewTests to diagnose build errors

* reenable TextViewTests to diagnose build errors

* Arrrrrrg

* Merged v2_develop

* Fixed uppercase accented keys in WindowsDriver

* Fixed key binding api docs

* Experimental impl of CommandScope.SubViews for MenuBar

* Removed dead code from application.cs

* Removed dead code from application.cs

* Removed dead code from ConsoleDriver.cs

* Cleaned up some key binding stuff

* Disabled Alt to activate menu for now

* Updated label commands

* Fixed menu bugs. Upgraded menu unit tests

* Fixed unit tests

* Working on NetDriver

* fixed netdriver

* Fixed issues called out by @bdisp CR

* fixed CursesDriver

* added todo to netdriver

* Cherry picked treeview test fix 1b415e5

* Fix NetDriver.

* CommandScope->KeyBindingScope

* Address some tznind feedback

* Refactored KeyBindings big time!

* Added key consts to KeyEventArgs and renamed Key to ConsoleDriverKey

* Fixed some API docs

* Moved ConsoleDriverKey to ConsoleDriver.cs

* Renamed Key->ConsoleDriverKey

* Renamed Key->ConsoleDriverKey

* Renamed Key->ConsoleDriverKey

* renamed file I forgot to rename before

* Updated name and API docs of KeyEventArgs.isAlpha

* Fixed issues with OnKeyUp not doing the right thing.

* Fixed MainLoop.Running never being used

* Fixed MainLoop.Running never being used - unit tests

* Claned up BUGBUG comments

* Disabled a unit test to see why ci/cd tests are failing

* Removed defunct commented code

* Removed more defunct commented code

* Re-eanbled unit test; jsut removing one test case...

* Disabled more...

* Renambed Global->Applicaton and updated scope API docs

* Disabled more unit tests...

* Removed dead code

* Disabled more unit tests...2

* Disabled more unit tests...3

* Renambed Global->Applicaton and updated scope API docs 2

* Added more KeyBinding scope tests

* Added more KeyBinding scope tests2

* ConsoleDriverKey too long. Key too ambiguous. Settled on KeyCode. (Partialy because eventually I want to intro a class named Key).

* KeyEventArgs improvements. cast to Rune must be explicit as it's lossy

* Fixed warnings

* Renamed KeyEventArgs to Key... progress on fixing broken stuff that resulted

* Fix ConsoleKeyMapping bugs.

* Fix NetDriver issue from converting a lower case to a upper case.

* Started migration to Key from KeyCode - e.g. made HotKeys all consistent.

* Fixed build warnings

* Added key defns to Key

* KeyBindings now uses Key vs. KeyCode

* Verified by tweaking UICatalog

* Fixed treeview test ... again

* Renamed ProcessKeyDown/Up to NewKeyDown/Up and OnKeyPressed to OnProcessKeyDown to make things more clear

* Added test AllViews_KeyDown_All_EventsFire unit tests and fixed a few Views that were wrong

* fixed stupid KeyUp event bug

* If key not handled, return false for datefield

* dotnet test --no-restore --verbosity diag

* dotnet test --blame

* run tests on windows

* Fix TestVKPacket unit test and move it to ConsoleKeyMappingTests.cs file.

* Remove unnecessary commented code.

* Tweaked unit tests and removed Key.BareKey

* Fixed little details and updated api docs

* updated api docs

* AddKeyBindingsForHotKey: KeyCode->Key

* Cleaned up more old KeyCode usages. Added TODOs

---------

Co-authored-by: BDisp <bd.bdisp@gmail.com>
2023-12-16 12:04:23 -07:00

904 lines
27 KiB
C#

#define DRAW_CONTENT
//#define BASE_DRAW_CONTENT
using System;
using System.Collections.Generic;
using System.Data;
using System.Globalization;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Text.Json;
using System.Text.Unicode;
using System.Threading.Tasks;
using Terminal.Gui;
using static Terminal.Gui.SpinnerStyle;
using static Terminal.Gui.TableView;
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 use ScrollView to do infinite scrolling
/// </summary>
[ScenarioMetadata ("Character Map", "Unicode viewer demonstrating the ScrollView control.")]
[ScenarioCategory ("Text and Formatting")]
[ScenarioCategory ("Controls")]
[ScenarioCategory ("ScrollView")]
public class CharacterMap : Scenario {
CharMap _charMap;
public Label _errorLabel;
TableView _categoryList;
// Don't create a Window, just return the top-level view
public override void Init ()
{
Application.Init ();
Application.Top.ColorScheme = Colors.Base;
}
public override void Setup ()
{
_charMap = new CharMap () {
X = 0,
Y = 1,
Height = Dim.Fill ()
};
Application.Top.Add (_charMap);
var jumpLabel = new Label ("_Jump To Code Point:") {
X = Pos.Right (_charMap) + 1,
Y = Pos.Y (_charMap),
HotKeySpecifier = (Rune)'_'
};
Application.Top.Add (jumpLabel);
var jumpEdit = new TextField () {
X = Pos.Right (jumpLabel) + 1, Y = Pos.Y (_charMap), Width = 10, Caption = "e.g. 01BE3"
};
Application.Top.Add (jumpEdit);
_errorLabel = new Label ("err") { X = Pos.Right (jumpEdit) + 1, Y = Pos.Y (_charMap), ColorScheme = Colors.ColorSchemes ["error"] };
Application.Top.Add (_errorLabel);
jumpEdit.TextChanged += JumpEdit_TextChanged;
_categoryList = new TableView () {
X = Pos.Right (_charMap),
Y = Pos.Bottom (jumpLabel),
Height = Dim.Fill ()
};
_categoryList.FullRowSelect = true;
//jumpList.Style.ShowHeaders = false;
//jumpList.Style.ShowHorizontalHeaderOverline = false;
//jumpList.Style.ShowHorizontalHeaderUnderline = false;
_categoryList.Style.ShowHorizontalBottomline = true;
//jumpList.Style.ShowVerticalCellLines = false;
//jumpList.Style.ShowVerticalHeaderLines = false;
_categoryList.Style.AlwaysShowHeaders = true;
bool isDescending = false;
_categoryList.Table = CreateCategoryTable (0, isDescending);
// if user clicks the mouse in TableView
_categoryList.MouseClick += (s, e) => {
_categoryList.ScreenToCell (e.MouseEvent.X, e.MouseEvent.Y, out int? clickedCol);
if (clickedCol != null && e.MouseEvent.Flags.HasFlag (MouseFlags.Button1Clicked)) {
var 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 ColumnStyle () { MaxWidth = longestName, MinWidth = longestName, MinAcceptableWidth = longestName });
_categoryList.Style.ColumnStyles.Add (1, new ColumnStyle () { MaxWidth = 1, MinWidth = 6 });
_categoryList.Style.ColumnStyles.Add (2, new ColumnStyle () { MaxWidth = 1, MinWidth = 6 });
_categoryList.Width = _categoryList.Style.ColumnStyles.Sum (c => c.Value.MinWidth) + 4;
_categoryList.SelectedCellChanged += (s, args) => {
var table = (EnumerableTableSource<UnicodeRange>)_categoryList.Table;
_charMap.StartCodePoint = table.Data.ToArray () [args.NewRow].Start;
};
Application.Top.Add (_categoryList);
_charMap.SelectedCodePoint = 0;
//jumpList.Refresh ();
_charMap.SetFocus ();
_charMap.Width = Dim.Fill () - _categoryList.Width;
var menu = new MenuBar (new MenuBarItem [] {
new ("_File", new MenuItem [] {
new ("_Quit", $"{Application.QuitKey}", () => Application.RequestStop ())
}),
new ("_Options", new MenuItem [] {
CreateMenuShowWidth ()
})
});
Application.Top.Add (menu);
//_charMap.Hover += (s, a) => {
// _errorLabel.Text = $"U+{a.Item:x5} {(Rune)a.Item}";
//};
}
MenuItem CreateMenuShowWidth ()
{
var item = new MenuItem {
Title = "_Show Glyph Width"
};
item.CheckType |= MenuItemCheckStyle.Checked;
item.Checked = _charMap?.ShowGlyphWidths;
item.Action += () => {
_charMap.ShowGlyphWidths = (bool)(item.Checked = !item.Checked);
};
return item;
}
EnumerableTableSource<UnicodeRange> CreateCategoryTable (int sortByColumn, bool descending)
{
Func<UnicodeRange, object> orderBy;
string categorySort = string.Empty;
string startSort = string.Empty;
string endSort = string.Empty;
string sortIndicator = descending ? CM.Glyphs.DownArrow.ToString () : CM.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.");
}
var sortedRanges = descending ?
UnicodeRange.Ranges.OrderByDescending (orderBy) :
UnicodeRange.Ranges.OrderBy (orderBy);
return new EnumerableTableSource<UnicodeRange> (sortedRanges, new Dictionary<string, Func<UnicodeRange, object>> () {
{ $"Category{categorySort}", s => s.Category },
{ $"Start{startSort}", s => $"{s.Start:x5}" },
{ $"End{endSort}", s => $"{s.End:x5}" }
});
}
void JumpEdit_TextChanged (object sender, TextChangedEventArgs e)
{
var jumpEdit = sender as TextField;
if (jumpEdit.Text.Length == 0) {
return;
}
uint result = 0;
if (jumpEdit.Text.StartsWith ("U+", StringComparison.OrdinalIgnoreCase) || jumpEdit.Text.StartsWith ("\\u")) {
try {
result = uint.Parse (jumpEdit.Text [2..^0], 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.Text = $"U+{result:x5}";
var 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;
}
}
class CharMap : ScrollView {
/// <summary>
/// Specifies the starting offset for the character map. The default is 0x2500
/// which is the Box Drawing characters.
/// </summary>
public int StartCodePoint {
get => _start;
set {
_start = value;
SelectedCodePoint = value;
SetNeedsDisplay ();
}
}
public event EventHandler<ListViewItemEventArgs> SelectedCodePointChanged;
/// <summary>
/// Specifies the starting offset for the character map. The default is 0x2500
/// which is the Box Drawing characters.
/// </summary>
public int SelectedCodePoint {
get => _selected;
set {
_selected = value;
int row = SelectedCodePoint / 16 * _rowHeight;
int col = SelectedCodePoint % 16 * COLUMN_WIDTH;
int height = Bounds.Height - (ShowHorizontalScrollIndicator ? 2 : 1);
if (row + ContentOffset.Y < 0) {
// Moving up.
ContentOffset = new Point (ContentOffset.X, row);
} else if (row + ContentOffset.Y >= height) {
// Moving down.
ContentOffset = new Point (ContentOffset.X, Math.Min (row, row - height + _rowHeight));
}
int width = Bounds.Width / COLUMN_WIDTH * COLUMN_WIDTH - (ShowVerticalScrollIndicator ? RowLabelWidth + 1 : RowLabelWidth);
if (col + ContentOffset.X < 0) {
// Moving left.
ContentOffset = new Point (col, ContentOffset.Y);
} else if (col + ContentOffset.X >= width) {
// Moving right.
ContentOffset = new Point (Math.Min (col, col - width + COLUMN_WIDTH), ContentOffset.Y);
}
SetNeedsDisplay ();
SelectedCodePointChanged?.Invoke (this, new ListViewItemEventArgs (SelectedCodePoint, null));
}
}
public event EventHandler<ListViewItemEventArgs> Hover;
/// <summary>
/// Gets the coordinates of the Cursor based on the SelectedCodePoint in screen coordinates
/// </summary>
public Point Cursor {
get {
int row = SelectedCodePoint / 16 * _rowHeight + ContentOffset.Y + 1;
int col = SelectedCodePoint % 16 * COLUMN_WIDTH + ContentOffset.X + RowLabelWidth + 1; // + 1 for padding
return new Point (col, row);
}
set => throw new NotImplementedException ();
}
public override void PositionCursor ()
{
if (HasFocus &&
Cursor.X >= RowLabelWidth &&
Cursor.X < Bounds.Width - (ShowVerticalScrollIndicator ? 1 : 0) &&
Cursor.Y > 0 &&
Cursor.Y < Bounds.Height - (ShowHorizontalScrollIndicator ? 1 : 0)) {
Driver.SetCursorVisibility (CursorVisibility.Default);
Move (Cursor.X, Cursor.Y);
} else {
Driver.SetCursorVisibility (CursorVisibility.Invisible);
}
}
public bool ShowGlyphWidths {
get => _rowHeight == 2;
set {
_rowHeight = value ? 2 : 1;
SetNeedsDisplay ();
}
}
int _start = 0;
int _selected = 0;
const int COLUMN_WIDTH = 3;
int _rowHeight = 1;
public static int MaxCodePoint => 0x10FFFF;
static int RowLabelWidth => $"U+{MaxCodePoint:x5}".Length + 1;
static int RowWidth => RowLabelWidth + COLUMN_WIDTH * 16;
public CharMap ()
{
ColorScheme = Colors.Dialog;
CanFocus = true;
ContentSize = new Size (RowWidth, (int)((MaxCodePoint / 16 + (ShowHorizontalScrollIndicator ? 2 : 1)) * _rowHeight));
AddCommand (Command.ScrollUp, () => {
if (SelectedCodePoint >= 16) {
SelectedCodePoint -= 16;
}
return true;
});
AddCommand (Command.ScrollDown, () => {
if (SelectedCodePoint < MaxCodePoint - 16) {
SelectedCodePoint += 16;
}
return true;
});
AddCommand (Command.ScrollLeft, () => {
if (SelectedCodePoint > 0) {
SelectedCodePoint--;
}
return true;
});
AddCommand (Command.ScrollRight, () => {
if (SelectedCodePoint < MaxCodePoint) {
SelectedCodePoint++;
}
return true;
});
AddCommand (Command.PageUp, () => {
int page = (Bounds.Height / _rowHeight - 1) * 16;
SelectedCodePoint -= Math.Min (page, SelectedCodePoint);
return true;
});
AddCommand (Command.PageDown, () => {
int page = (Bounds.Height / _rowHeight - 1) * 16;
SelectedCodePoint += Math.Min (page, MaxCodePoint - SelectedCodePoint);
return true;
});
AddCommand (Command.TopHome, () => {
SelectedCodePoint = 0;
return true;
});
AddCommand (Command.BottomEnd, () => {
SelectedCodePoint = MaxCodePoint;
return true;
});
KeyBindings.Add (Key.Enter, Command.Accept);
AddCommand (Command.Accept, () => {
ShowDetails ();
return true;
});
MouseClick += Handle_MouseClick;
}
void CopyCodePoint () => Clipboard.Contents = $"U+{SelectedCodePoint:x5}";
void CopyGlyph () => Clipboard.Contents = $"{new Rune (SelectedCodePoint)}";
public override void OnDrawContent (Rect contentArea) =>
//if (ShowHorizontalScrollIndicator && ContentSize.Height < (int)(MaxCodePoint / 16 + 2)) {
// //ContentSize = new Size (CharMap.RowWidth, (int)(MaxCodePoint / 16 + 2));
// //ContentSize = new Size (CharMap.RowWidth, (int)(MaxCodePoint / 16) * _rowHeight + 2);
// var width = (Bounds.Width / COLUMN_WIDTH * COLUMN_WIDTH) - (ShowVerticalScrollIndicator ? RowLabelWidth + 1 : RowLabelWidth);
// if (Cursor.X + ContentOffset.X >= width) {
// // Snap to the selected glyph.
// ContentOffset = new Point (
// Math.Min (Cursor.X, Cursor.X - width + COLUMN_WIDTH),
// ContentOffset.Y == -ContentSize.Height + Bounds.Height ? ContentOffset.Y - 1 : ContentOffset.Y);
// } else {
// ContentOffset = new Point (
// ContentOffset.X - Cursor.X,
// ContentOffset.Y == -ContentSize.Height + Bounds.Height ? ContentOffset.Y - 1 : ContentOffset.Y);
// }
//} else if (!ShowHorizontalScrollIndicator && ContentSize.Height > (int)(MaxCodePoint / 16 + 1)) {
// //ContentSize = new Size (CharMap.RowWidth, (int)(MaxCodePoint / 16 + 1));
// // Snap 1st column into view if it's been scrolled horizontally
// ContentOffset = new Point (0, ContentOffset.Y < -ContentSize.Height + Bounds.Height ? ContentOffset.Y - 1 : ContentOffset.Y);
//}
base.OnDrawContent (contentArea);
//public void CharMap_DrawContent (object s, DrawEventArgs a)
public override void OnDrawContentComplete (Rect contentArea)
{
if (contentArea.Height == 0 || contentArea.Width == 0) {
return;
}
var viewport = new Rect (ContentOffset,
new Size (Math.Max (Bounds.Width - (ShowVerticalScrollIndicator ? 1 : 0), 0),
Math.Max (Bounds.Height - (ShowHorizontalScrollIndicator ? 1 : 0), 0)));
var oldClip = ClipToBounds ();
if (ShowHorizontalScrollIndicator) {
// ClipToBounds doesn't know about the scroll indicators, so if off, subtract one from height
Driver.Clip = new Rect (Driver.Clip.Location, new Size (Driver.Clip.Width, Driver.Clip.Height - 1));
}
if (ShowVerticalScrollIndicator) {
// ClipToBounds doesn't know about the scroll indicators, so if off, subtract one from width
Driver.Clip = new Rect (Driver.Clip.Location, new Size (Driver.Clip.Width - 1, Driver.Clip.Height));
}
int cursorCol = Cursor.X - ContentOffset.X - RowLabelWidth - 1;
int cursorRow = Cursor.Y - ContentOffset.Y - 1;
Driver.SetAttribute (GetHotNormalColor ());
Move (0, 0);
Driver.AddStr (new string (' ', RowLabelWidth + 1));
for (int hexDigit = 0; hexDigit < 16; hexDigit++) {
int x = ContentOffset.X + RowLabelWidth + hexDigit * COLUMN_WIDTH;
if (x > RowLabelWidth - 2) {
Move (x, 0);
Driver.SetAttribute (GetHotNormalColor ());
Driver.AddStr (" ");
Driver.SetAttribute (HasFocus && cursorCol + ContentOffset.X + RowLabelWidth == x ? ColorScheme.HotFocus : GetHotNormalColor ());
Driver.AddStr ($"{hexDigit:x}");
Driver.SetAttribute (GetHotNormalColor ());
Driver.AddStr (" ");
}
}
int firstColumnX = viewport.X + RowLabelWidth;
for (int y = 1; y < Bounds.Height; y++) {
// What row is this?
int row = (y - ContentOffset.Y - 1) / _rowHeight;
int val = row * 16;
if (val > MaxCodePoint) {
continue;
}
Move (firstColumnX + COLUMN_WIDTH, y);
Driver.SetAttribute (GetNormalColor ());
for (int col = 0; col < 16; col++) {
int x = firstColumnX + COLUMN_WIDTH * col + 1;
Move (x, y);
if (cursorRow + ContentOffset.Y + 1 == y && cursorCol + ContentOffset.X + firstColumnX + 1 == x && !HasFocus) {
Driver.SetAttribute (GetFocusColor ());
}
int scalar = val + col;
var rune = (Rune)'?';
if (Rune.IsValid (scalar)) {
rune = new Rune (scalar);
}
int width = rune.GetColumns ();
// are we at first row of the row?
if (!ShowGlyphWidths || (y - ContentOffset.Y) % _rowHeight > 0) {
if (width > 0) {
Driver.AddRune (rune);
} else {
if (rune.IsCombiningMark ()) {
// This is a hack to work around the fact that combining marks
// a) can't be rendered on their own
// b) that don't normalize are not properly supported in
// any known terminal (esp Windows/AtlasEngine).
// See Issue #2616
var sb = new StringBuilder ();
sb.Append ('a');
sb.Append (rune);
// Try normalizing after combining with 'a'. If it normalizes, at least
// it'll show on the 'a'. If not, just show the replacement char.
string normal = sb.ToString ().Normalize (NormalizationForm.FormC);
if (normal.Length == 1) {
Driver.AddRune (normal [0]);
} else {
Driver.AddRune (Rune.ReplacementChar);
}
}
}
} else {
Driver.SetAttribute (ColorScheme.HotNormal);
Driver.AddStr ($"{width}");
}
if (cursorRow + ContentOffset.Y + 1 == y && cursorCol + ContentOffset.X + firstColumnX + 1 == x && !HasFocus) {
Driver.SetAttribute (GetNormalColor ());
}
}
Move (0, y);
Driver.SetAttribute (HasFocus && cursorRow + ContentOffset.Y + 1 == y ? ColorScheme.HotFocus : ColorScheme.HotNormal);
if (!ShowGlyphWidths || (y - ContentOffset.Y) % _rowHeight > 0) {
Driver.AddStr ($"U+{val / 16:x5}_ ");
} else {
Driver.AddStr (new string (' ', RowLabelWidth));
}
}
Driver.Clip = oldClip;
}
ContextMenu _contextMenu = new ();
void Handle_MouseClick (object sender, MouseEventEventArgs args)
{
var me = args.MouseEvent;
if (me.Flags != MouseFlags.ReportMousePosition && me.Flags != MouseFlags.Button1Clicked &&
me.Flags != MouseFlags.Button1DoubleClicked) {
return;
}
if (me.Y == 0) {
me.Y = Cursor.Y;
}
if (me.Y > 0) { }
if (me.X < RowLabelWidth || me.X > RowLabelWidth + 16 * COLUMN_WIDTH - 1) {
me.X = Cursor.X;
}
int row = (me.Y - 1 - ContentOffset.Y) / _rowHeight; // -1 for header
int col = (me.X - RowLabelWidth - ContentOffset.X) / COLUMN_WIDTH;
if (col > 15) {
col = 15;
}
int val = row * 16 + col;
if (val > MaxCodePoint) {
return;
}
if (me.Flags == MouseFlags.ReportMousePosition) {
Hover?.Invoke (this, new ListViewItemEventArgs (val, null));
}
if (me.Flags == MouseFlags.Button1Clicked) {
SelectedCodePoint = val;
return;
}
if (me.Flags == MouseFlags.Button1DoubleClicked) {
SelectedCodePoint = val;
ShowDetails ();
return;
}
if (me.Flags == _contextMenu.MouseFlags) {
SelectedCodePoint = val;
_contextMenu = new ContextMenu (me.X + 1, me.Y + 1,
new MenuBarItem (new MenuItem [] {
new ("_Copy Glyph", "", () => CopyGlyph (), null, null, (KeyCode)Key.C.WithCtrl),
new ("Copy Code _Point", "", () => CopyCodePoint (), null, null, (KeyCode)Key.C.WithCtrl.WithShift)
}) { }
);
_contextMenu.Show ();
}
}
public static string ToCamelCase (string str)
{
if (string.IsNullOrEmpty (str)) {
return str;
}
var textInfo = new CultureInfo ("en-US", false).TextInfo;
str = textInfo.ToLower (str);
str = textInfo.ToTitleCase (str);
return str;
}
void ShowDetails ()
{
var client = new UcdApiClient ();
string decResponse = string.Empty;
var waitIndicator = new Dialog (new Button ("Cancel")) {
Title = "Getting Code Point Information",
X = Pos.Center (),
Y = Pos.Center (),
Height = 7,
Width = 50
};
var errorLabel = new Label () {
Text = UcdApiClient.BaseUrl,
AutoSize = false,
X = 0,
Y = 1,
Width = Dim.Fill (),
Height = Dim.Fill (1),
TextAlignment = TextAlignment.Centered
};
var spinner = new SpinnerView () {
X = Pos.Center (),
Y = Pos.Center (),
Style = new Aesthetic ()
};
spinner.AutoSpin = true;
waitIndicator.Add (errorLabel);
waitIndicator.Add (spinner);
waitIndicator.Ready += async (s, a) => {
try {
decResponse = await client.GetCodepointDec ((int)SelectedCodePoint);
} catch (HttpRequestException e) {
(s as Dialog).Text = e.Message;
Application.Invoke (() => {
spinner.Visible = false;
errorLabel.Text = e.Message;
errorLabel.ColorScheme = Colors.ColorSchemes ["Error"];
errorLabel.Visible = true;
});
}
(s as Dialog)?.RequestStop ();
};
Application.Run (waitIndicator);
if (!string.IsNullOrEmpty (decResponse)) {
string name = string.Empty;
using (var document = JsonDocument.Parse (decResponse)) {
var root = document.RootElement;
// Get a property by name and output its value
if (root.TryGetProperty ("name", out var nameElement)) {
name = nameElement.GetString ();
}
//// Navigate to a nested property and output its value
//if (root.TryGetProperty ("property3", out JsonElement property3Element)
//&& property3Element.TryGetProperty ("nestedProperty", out JsonElement nestedPropertyElement)) {
// Console.WriteLine (nestedPropertyElement.GetString ());
//}
decResponse = JsonSerializer.Serialize (document.RootElement, new
JsonSerializerOptions {
WriteIndented = true
});
}
string title = $"{ToCamelCase (name)} - {new Rune (SelectedCodePoint)} U+{SelectedCodePoint:x5}";
var copyGlyph = new Button ("Copy _Glyph");
var copyCP = new Button ("Copy Code _Point");
var cancel = new Button ("Cancel");
var dlg = new Dialog (copyGlyph, copyCP, cancel) {
Title = title
};
copyGlyph.Clicked += (s, a) => {
CopyGlyph ();
dlg.RequestStop ();
};
copyCP.Clicked += (s, a) => {
CopyCodePoint ();
dlg.RequestStop ();
};
cancel.Clicked += (s, a) => dlg.RequestStop ();
var rune = (Rune)SelectedCodePoint;
var label = new Label () {
Text = "IsAscii: ",
X = 0,
Y = 0
};
dlg.Add (label);
label = new Label () {
Text = $"{rune.IsAscii}",
X = Pos.Right (label),
Y = Pos.Top (label)
};
dlg.Add (label);
label = new Label () {
Text = ", Bmp: ",
X = Pos.Right (label),
Y = Pos.Top (label)
};
dlg.Add (label);
label = new Label () {
Text = $"{rune.IsBmp}",
X = Pos.Right (label),
Y = Pos.Top (label)
};
dlg.Add (label);
label = new Label () {
Text = ", CombiningMark: ",
X = Pos.Right (label),
Y = Pos.Top (label)
};
dlg.Add (label);
label = new Label () {
Text = $"{rune.IsCombiningMark ()}",
X = Pos.Right (label),
Y = Pos.Top (label)
};
dlg.Add (label);
label = new Label () {
Text = ", SurrogatePair: ",
X = Pos.Right (label),
Y = Pos.Top (label)
};
dlg.Add (label);
label = new Label () {
Text = $"{rune.IsSurrogatePair ()}",
X = Pos.Right (label),
Y = Pos.Top (label)
};
dlg.Add (label);
label = new Label () {
Text = ", Plane: ",
X = Pos.Right (label),
Y = Pos.Top (label)
};
dlg.Add (label);
label = new Label () {
Text = $"{rune.Plane}",
X = Pos.Right (label),
Y = Pos.Top (label)
};
dlg.Add (label);
label = new Label () {
Text = "Columns: ",
X = 0,
Y = Pos.Bottom (label)
};
dlg.Add (label);
label = new Label () {
Text = $"{rune.GetColumns ()}",
X = Pos.Right (label),
Y = Pos.Top (label)
};
dlg.Add (label);
label = new Label () {
Text = ", Utf16SequenceLength: ",
X = Pos.Right (label),
Y = Pos.Top (label)
};
dlg.Add (label);
label = new Label () {
Text = $"{rune.Utf16SequenceLength}",
X = Pos.Right (label),
Y = Pos.Top (label)
};
dlg.Add (label);
label = new Label () {
Text = $"Code Point Information from {UcdApiClient.BaseUrl}codepoint/dec/{SelectedCodePoint}:",
X = 0,
Y = Pos.Bottom (label)
};
dlg.Add (label);
var json = new TextView () {
X = 0,
Y = Pos.Bottom (label),
Width = Dim.Fill (),
Height = Dim.Fill (2),
ReadOnly = true,
Text = decResponse
};
dlg.Add (json);
Application.Run (dlg);
} else {
MessageBox.ErrorQuery ("Code Point API", $"{UcdApiClient.BaseUrl}codepoint/dec/{SelectedCodePoint} did not return a result for\r\n {new Rune (SelectedCodePoint)} U+{SelectedCodePoint:x5}.", "Ok");
}
// BUGBUG: This is a workaround for some weird ScrollView related mouse grab bug
Application.GrabMouse (this);
}
public override bool OnEnter (View view)
{
if (IsInitialized) {
Application.Driver.SetCursorVisibility (CursorVisibility.Default);
}
return base.OnEnter (view);
}
public override bool OnLeave (View view)
{
Driver.SetCursorVisibility (CursorVisibility.Invisible);
return base.OnLeave (view);
}
}
public class UcdApiClient {
static readonly HttpClient httpClient = new ();
public const string BaseUrl = "https://ucdapi.org/unicode/latest/";
public async Task<string> GetCodepointHex (string hex)
{
var response = await httpClient.GetAsync ($"{BaseUrl}codepoint/hex/{hex}");
response.EnsureSuccessStatusCode ();
return await response.Content.ReadAsStringAsync ();
}
public async Task<string> GetCodepointDec (int dec)
{
var response = await httpClient.GetAsync ($"{BaseUrl}codepoint/dec/{dec}");
response.EnsureSuccessStatusCode ();
return await response.Content.ReadAsStringAsync ();
}
public async Task<string> GetChars (string chars)
{
var response = await httpClient.GetAsync ($"{BaseUrl}chars/{Uri.EscapeDataString (chars)}");
response.EnsureSuccessStatusCode ();
return await response.Content.ReadAsStringAsync ();
}
public async Task<string> GetCharsName (string chars)
{
var response = await httpClient.GetAsync ($"{BaseUrl}chars/{Uri.EscapeDataString (chars)}/name");
response.EnsureSuccessStatusCode ();
return await response.Content.ReadAsStringAsync ();
}
}
class UnicodeRange {
public int Start;
public int End;
public string Category;
public UnicodeRange (int start, int end, string category)
{
Start = start;
End = end;
Category = category;
}
public static List<UnicodeRange> GetRanges ()
{
var ranges = from r in typeof (UnicodeRanges).GetProperties (System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.Public)
let urange = r.GetValue (null) as System.Text.Unicode.UnicodeRange
let name = string.IsNullOrEmpty (r.Name) ? $"U+{urange.FirstCodePoint:x5}-U+{urange.FirstCodePoint + urange.Length:x5}" : r.Name
where name != "None" && name != "All"
select new UnicodeRange (urange.FirstCodePoint, urange.FirstCodePoint + urange.Length, name);
// .NET 8.0 only supports BMP in UnicodeRanges: https://learn.microsoft.com/en-us/dotnet/api/system.text.unicode.unicoderanges?view=net-8.0
var nonBmpRanges = new List<UnicodeRange> {
new (0x1F130, 0x1F149, "Squared Latin Capital Letters"),
new (0x12400, 0x1240f, "Cuneiform Numbers and Punctuation"),
new (0x10000, 0x1007F, "Linear B Syllabary"),
new (0x10080, 0x100FF, "Linear B Ideograms"),
new (0x10100, 0x1013F, "Aegean Numbers"),
new (0x10300, 0x1032F, "Old Italic"),
new (0x10330, 0x1034F, "Gothic"),
new (0x10380, 0x1039F, "Ugaritic"),
new (0x10400, 0x1044F, "Deseret"),
new (0x10450, 0x1047F, "Shavian"),
new (0x10480, 0x104AF, "Osmanya"),
new (0x10800, 0x1083F, "Cypriot Syllabary"),
new (0x1D000, 0x1D0FF, "Byzantine Musical Symbols"),
new (0x1D100, 0x1D1FF, "Musical Symbols"),
new (0x1D300, 0x1D35F, "Tai Xuan Jing Symbols"),
new (0x1D400, 0x1D7FF, "Mathematical Alphanumeric Symbols"),
new (0x1F600, 0x1F532, "Emojis Symbols"),
new (0x20000, 0x2A6DF, "CJK Unified Ideographs Extension B"),
new (0x2F800, 0x2FA1F, "CJK Compatibility Ideographs Supplement"),
new (0xE0000, 0xE007F, "Tags")
};
return ranges.Concat (nonBmpRanges).OrderBy (r => r.Category).ToList ();
}
public static List<UnicodeRange> Ranges = GetRanges ();
}