CharacterMap.cs 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228
  1. #define OTHER_CONTROLS
  2. using System;
  3. using System.Collections.Generic;
  4. using System.ComponentModel;
  5. using System.Globalization;
  6. using System.Linq;
  7. using System.Net.Http;
  8. using System.Reflection;
  9. using System.Text;
  10. using System.Text.Json;
  11. using System.Text.Unicode;
  12. using System.Threading.Tasks;
  13. using Terminal.Gui;
  14. using static Terminal.Gui.SpinnerStyle;
  15. namespace UICatalog.Scenarios;
  16. /// <summary>
  17. /// This Scenario demonstrates building a custom control (a class deriving from View) that: - Provides a
  18. /// "Character Map" application (like Windows' charmap.exe). - Helps test unicode character rendering in Terminal.Gui -
  19. /// Illustrates how to do infinite scrolling
  20. /// </summary>
  21. [ScenarioMetadata ("Character Map", "Unicode viewer demonstrating infinite content, scrolling, and Unicode.")]
  22. [ScenarioCategory ("Text and Formatting")]
  23. [ScenarioCategory ("Drawing")]
  24. [ScenarioCategory ("Controls")]
  25. [ScenarioCategory ("Layout")]
  26. [ScenarioCategory ("Scrolling")]
  27. public class CharacterMap : Scenario
  28. {
  29. public Label _errorLabel;
  30. private TableView _categoryList;
  31. private CharMap _charMap;
  32. // Don't create a Window, just return the top-level view
  33. public override void Main ()
  34. {
  35. Application.Init ();
  36. var top = new Window
  37. {
  38. BorderStyle = LineStyle.None
  39. };
  40. _charMap = new ()
  41. {
  42. X = 0,
  43. Y = 0,
  44. Width = Dim.Fill (),
  45. Height = Dim.Fill ()
  46. };
  47. top.Add (_charMap);
  48. #if OTHER_CONTROLS
  49. _charMap.Y = 1;
  50. var jumpLabel = new Label
  51. {
  52. X = Pos.Right (_charMap) + 1,
  53. Y = Pos.Y (_charMap),
  54. HotKeySpecifier = (Rune)'_',
  55. Text = "_Jump To Code Point:"
  56. };
  57. top.Add (jumpLabel);
  58. var jumpEdit = new TextField
  59. {
  60. X = Pos.Right (jumpLabel) + 1, Y = Pos.Y (_charMap), Width = 10, Caption = "e.g. 01BE3"
  61. };
  62. top.Add (jumpEdit);
  63. _errorLabel = new ()
  64. {
  65. X = Pos.Right (jumpEdit) + 1, Y = Pos.Y (_charMap), ColorScheme = Colors.ColorSchemes ["error"], Text = "err"
  66. };
  67. top.Add (_errorLabel);
  68. #if TEXT_CHANGED_TO_JUMP
  69. jumpEdit.TextChanged += JumpEdit_TextChanged;
  70. #else
  71. jumpEdit.Accept += JumpEditOnAccept;
  72. void JumpEditOnAccept (object sender, CancelEventArgs e)
  73. {
  74. JumpEdit_TextChanged (sender, new (jumpEdit.Text, jumpEdit.Text));
  75. // Cancel the event to prevent ENTER from being handled elsewhere
  76. e.Cancel = true;
  77. }
  78. #endif
  79. _categoryList = new () { X = Pos.Right (_charMap), Y = Pos.Bottom (jumpLabel), Height = Dim.Fill () };
  80. _categoryList.FullRowSelect = true;
  81. //jumpList.Style.ShowHeaders = false;
  82. //jumpList.Style.ShowHorizontalHeaderOverline = false;
  83. //jumpList.Style.ShowHorizontalHeaderUnderline = false;
  84. _categoryList.Style.ShowHorizontalBottomline = true;
  85. //jumpList.Style.ShowVerticalCellLines = false;
  86. //jumpList.Style.ShowVerticalHeaderLines = false;
  87. _categoryList.Style.AlwaysShowHeaders = true;
  88. var isDescending = false;
  89. _categoryList.Table = CreateCategoryTable (0, isDescending);
  90. // if user clicks the mouse in TableView
  91. _categoryList.MouseClick += (s, e) =>
  92. {
  93. _categoryList.ScreenToCell (e.MouseEvent.Position, out int? clickedCol);
  94. if (clickedCol != null && e.MouseEvent.Flags.HasFlag (MouseFlags.Button1Clicked))
  95. {
  96. EnumerableTableSource<UnicodeRange> table = (EnumerableTableSource<UnicodeRange>)_categoryList.Table;
  97. string prevSelection = table.Data.ElementAt (_categoryList.SelectedRow).Category;
  98. isDescending = !isDescending;
  99. _categoryList.Table = CreateCategoryTable (clickedCol.Value, isDescending);
  100. table = (EnumerableTableSource<UnicodeRange>)_categoryList.Table;
  101. _categoryList.SelectedRow = table.Data
  102. .Select ((item, index) => new { item, index })
  103. .FirstOrDefault (x => x.item.Category == prevSelection)
  104. ?.index
  105. ?? -1;
  106. }
  107. };
  108. int longestName = UnicodeRange.Ranges.Max (r => r.Category.GetColumns ());
  109. _categoryList.Style.ColumnStyles.Add (
  110. 0,
  111. new () { MaxWidth = longestName, MinWidth = longestName, MinAcceptableWidth = longestName }
  112. );
  113. _categoryList.Style.ColumnStyles.Add (1, new () { MaxWidth = 1, MinWidth = 6 });
  114. _categoryList.Style.ColumnStyles.Add (2, new () { MaxWidth = 1, MinWidth = 6 });
  115. _categoryList.Width = _categoryList.Style.ColumnStyles.Sum (c => c.Value.MinWidth) + 4;
  116. _categoryList.SelectedCellChanged += (s, args) =>
  117. {
  118. EnumerableTableSource<UnicodeRange> table = (EnumerableTableSource<UnicodeRange>)_categoryList.Table;
  119. _charMap.StartCodePoint = table.Data.ToArray () [args.NewRow].Start;
  120. };
  121. top.Add (_categoryList);
  122. // TODO: Replace this with Dim.Auto when that's ready
  123. _categoryList.Initialized += _categoryList_Initialized;
  124. var menu = new MenuBar
  125. {
  126. Menus =
  127. [
  128. new (
  129. "_File",
  130. new MenuItem []
  131. {
  132. new (
  133. "_Quit",
  134. $"{Application.QuitKey}",
  135. () => Application.RequestStop ()
  136. )
  137. }
  138. ),
  139. new (
  140. "_Options",
  141. new [] { CreateMenuShowWidth () }
  142. )
  143. ]
  144. };
  145. top.Add (menu);
  146. #endif // OTHER_CONTROLS
  147. _charMap.SelectedCodePoint = 0;
  148. _charMap.SetFocus ();
  149. Application.Run (top);
  150. top.Dispose ();
  151. }
  152. private void _categoryList_Initialized (object sender, EventArgs e) { _charMap.Width = Dim.Fill () - _categoryList.Width; }
  153. private EnumerableTableSource<UnicodeRange> CreateCategoryTable (int sortByColumn, bool descending)
  154. {
  155. Func<UnicodeRange, object> orderBy;
  156. var categorySort = string.Empty;
  157. var startSort = string.Empty;
  158. var endSort = string.Empty;
  159. string sortIndicator = descending ? CM.Glyphs.DownArrow.ToString () : CM.Glyphs.UpArrow.ToString ();
  160. switch (sortByColumn)
  161. {
  162. case 0:
  163. orderBy = r => r.Category;
  164. categorySort = sortIndicator;
  165. break;
  166. case 1:
  167. orderBy = r => r.Start;
  168. startSort = sortIndicator;
  169. break;
  170. case 2:
  171. orderBy = r => r.End;
  172. endSort = sortIndicator;
  173. break;
  174. default:
  175. throw new ArgumentException ("Invalid column number.");
  176. }
  177. IOrderedEnumerable<UnicodeRange> sortedRanges = descending
  178. ? UnicodeRange.Ranges.OrderByDescending (orderBy)
  179. : UnicodeRange.Ranges.OrderBy (orderBy);
  180. return new (
  181. sortedRanges,
  182. new ()
  183. {
  184. { $"Category{categorySort}", s => s.Category },
  185. { $"Start{startSort}", s => $"{s.Start:x5}" },
  186. { $"End{endSort}", s => $"{s.End:x5}" }
  187. }
  188. );
  189. }
  190. private MenuItem CreateMenuShowWidth ()
  191. {
  192. var item = new MenuItem { Title = "_Show Glyph Width" };
  193. item.CheckType |= MenuItemCheckStyle.Checked;
  194. item.Checked = _charMap?.ShowGlyphWidths;
  195. item.Action += () => { _charMap.ShowGlyphWidths = (bool)(item.Checked = !item.Checked); };
  196. return item;
  197. }
  198. private void JumpEdit_TextChanged (object sender, StateEventArgs<string> e)
  199. {
  200. var jumpEdit = sender as TextField;
  201. if (jumpEdit.Text.Length == 0)
  202. {
  203. return;
  204. }
  205. uint result = 0;
  206. if (jumpEdit.Text.StartsWith ("U+", StringComparison.OrdinalIgnoreCase) || jumpEdit.Text.StartsWith ("\\u"))
  207. {
  208. try
  209. {
  210. result = uint.Parse (jumpEdit.Text [2..], NumberStyles.HexNumber);
  211. }
  212. catch (FormatException)
  213. {
  214. _errorLabel.Text = "Invalid hex value";
  215. return;
  216. }
  217. }
  218. else if (jumpEdit.Text.StartsWith ("0", StringComparison.OrdinalIgnoreCase) || jumpEdit.Text.StartsWith ("\\u"))
  219. {
  220. try
  221. {
  222. result = uint.Parse (jumpEdit.Text, NumberStyles.HexNumber);
  223. }
  224. catch (FormatException)
  225. {
  226. _errorLabel.Text = "Invalid hex value";
  227. return;
  228. }
  229. }
  230. else
  231. {
  232. try
  233. {
  234. result = uint.Parse (jumpEdit.Text, NumberStyles.Integer);
  235. }
  236. catch (FormatException)
  237. {
  238. _errorLabel.Text = "Invalid value";
  239. return;
  240. }
  241. }
  242. if (result > RuneExtensions.MaxUnicodeCodePoint)
  243. {
  244. _errorLabel.Text = "Beyond maximum codepoint";
  245. return;
  246. }
  247. _errorLabel.Text = $"U+{result:x5}";
  248. EnumerableTableSource<UnicodeRange> table = (EnumerableTableSource<UnicodeRange>)_categoryList.Table;
  249. _categoryList.SelectedRow = table.Data
  250. .Select ((item, index) => new { item, index })
  251. .FirstOrDefault (x => x.item.Start <= result && x.item.End >= result)
  252. ?.index
  253. ?? -1;
  254. _categoryList.EnsureSelectedCellIsVisible ();
  255. // Ensure the typed glyph is selected
  256. _charMap.SelectedCodePoint = (int)result;
  257. }
  258. }
  259. internal class CharMap : View
  260. {
  261. private const int COLUMN_WIDTH = 3;
  262. private ContextMenu _contextMenu = new ();
  263. private int _rowHeight = 1;
  264. private int _selected;
  265. private int _start;
  266. public CharMap ()
  267. {
  268. ColorScheme = Colors.ColorSchemes ["Dialog"];
  269. CanFocus = true;
  270. CursorVisibility = CursorVisibility.Default;
  271. SetContentSize (new (RowWidth, (MaxCodePoint / 16 + 2) * _rowHeight));
  272. AddCommand (
  273. Command.ScrollUp,
  274. () =>
  275. {
  276. if (SelectedCodePoint >= 16)
  277. {
  278. SelectedCodePoint -= 16;
  279. }
  280. ScrollVertical (-_rowHeight);
  281. return true;
  282. }
  283. );
  284. AddCommand (
  285. Command.ScrollDown,
  286. () =>
  287. {
  288. if (SelectedCodePoint <= MaxCodePoint - 16)
  289. {
  290. SelectedCodePoint += 16;
  291. }
  292. if (Cursor.Y >= Viewport.Height)
  293. {
  294. ScrollVertical (_rowHeight);
  295. }
  296. return true;
  297. }
  298. );
  299. AddCommand (
  300. Command.ScrollLeft,
  301. () =>
  302. {
  303. if (SelectedCodePoint > 0)
  304. {
  305. SelectedCodePoint--;
  306. }
  307. if (Cursor.X > RowLabelWidth + 1)
  308. {
  309. ScrollHorizontal (-COLUMN_WIDTH);
  310. }
  311. return true;
  312. }
  313. );
  314. AddCommand (
  315. Command.ScrollRight,
  316. () =>
  317. {
  318. if (SelectedCodePoint < MaxCodePoint)
  319. {
  320. SelectedCodePoint++;
  321. }
  322. if (Cursor.X >= Viewport.Width)
  323. {
  324. ScrollHorizontal (COLUMN_WIDTH);
  325. }
  326. return true;
  327. }
  328. );
  329. AddCommand (
  330. Command.PageUp,
  331. () =>
  332. {
  333. int page = (Viewport.Height - 1 / _rowHeight) * 16;
  334. SelectedCodePoint -= Math.Min (page, SelectedCodePoint);
  335. Viewport = Viewport with { Y = SelectedCodePoint / 16 * _rowHeight };
  336. return true;
  337. }
  338. );
  339. AddCommand (
  340. Command.PageDown,
  341. () =>
  342. {
  343. int page = (Viewport.Height - 1 / _rowHeight) * 16;
  344. SelectedCodePoint += Math.Min (page, MaxCodePoint - SelectedCodePoint);
  345. Viewport = Viewport with { Y = SelectedCodePoint / 16 * _rowHeight };
  346. return true;
  347. }
  348. );
  349. AddCommand (
  350. Command.TopHome,
  351. () =>
  352. {
  353. SelectedCodePoint = 0;
  354. return true;
  355. }
  356. );
  357. AddCommand (
  358. Command.BottomEnd,
  359. () =>
  360. {
  361. SelectedCodePoint = MaxCodePoint;
  362. Viewport = Viewport with { Y = SelectedCodePoint / 16 * _rowHeight };
  363. return true;
  364. }
  365. );
  366. AddCommand (
  367. Command.Accept,
  368. () =>
  369. {
  370. ShowDetails ();
  371. return true;
  372. }
  373. );
  374. KeyBindings.Add (Key.Enter, Command.Accept);
  375. KeyBindings.Add (Key.CursorUp, Command.ScrollUp);
  376. KeyBindings.Add (Key.CursorDown, Command.ScrollDown);
  377. KeyBindings.Add (Key.CursorLeft, Command.ScrollLeft);
  378. KeyBindings.Add (Key.CursorRight, Command.ScrollRight);
  379. KeyBindings.Add (Key.PageUp, Command.PageUp);
  380. KeyBindings.Add (Key.PageDown, Command.PageDown);
  381. KeyBindings.Add (Key.Home, Command.TopHome);
  382. KeyBindings.Add (Key.End, Command.BottomEnd);
  383. MouseClick += Handle_MouseClick;
  384. MouseEvent += Handle_MouseEvent;
  385. // Prototype scrollbars
  386. Padding.Thickness = new (0, 0, 1, 1);
  387. var up = new Button
  388. {
  389. X = Pos.AnchorEnd (1),
  390. Y = 0,
  391. Height = 1,
  392. Width = 1,
  393. NoPadding = true,
  394. NoDecorations = true,
  395. Title = CM.Glyphs.UpArrow.ToString (),
  396. WantContinuousButtonPressed = true,
  397. CanFocus = false
  398. };
  399. up.Accept += (sender, args) => { args.Cancel = ScrollVertical (-1) == true; };
  400. var down = new Button
  401. {
  402. X = Pos.AnchorEnd (1),
  403. Y = Pos.AnchorEnd (2),
  404. Height = 1,
  405. Width = 1,
  406. NoPadding = true,
  407. NoDecorations = true,
  408. Title = CM.Glyphs.DownArrow.ToString (),
  409. WantContinuousButtonPressed = true,
  410. CanFocus = false
  411. };
  412. down.Accept += (sender, args) => { ScrollVertical (1); };
  413. var left = new Button
  414. {
  415. X = 0,
  416. Y = Pos.AnchorEnd (1),
  417. Height = 1,
  418. Width = 1,
  419. NoPadding = true,
  420. NoDecorations = true,
  421. Title = CM.Glyphs.LeftArrow.ToString (),
  422. WantContinuousButtonPressed = true,
  423. CanFocus = false
  424. };
  425. left.Accept += (sender, args) => { ScrollHorizontal (-1); };
  426. var right = new Button
  427. {
  428. X = Pos.AnchorEnd (2),
  429. Y = Pos.AnchorEnd (1),
  430. Height = 1,
  431. Width = 1,
  432. NoPadding = true,
  433. NoDecorations = true,
  434. Title = CM.Glyphs.RightArrow.ToString (),
  435. WantContinuousButtonPressed = true,
  436. CanFocus = false
  437. };
  438. right.Accept += (sender, args) => { ScrollHorizontal (1); };
  439. Padding.Add (up, down, left, right);
  440. }
  441. private void Handle_MouseEvent (object sender, MouseEventEventArgs e)
  442. {
  443. if (e.MouseEvent.Flags == MouseFlags.WheeledDown)
  444. {
  445. ScrollVertical (1);
  446. e.Handled = true;
  447. return;
  448. }
  449. if (e.MouseEvent.Flags == MouseFlags.WheeledUp)
  450. {
  451. ScrollVertical (-1);
  452. e.Handled = true;
  453. return;
  454. }
  455. if (e.MouseEvent.Flags == MouseFlags.WheeledRight)
  456. {
  457. ScrollHorizontal (1);
  458. e.Handled = true;
  459. return;
  460. }
  461. if (e.MouseEvent.Flags == MouseFlags.WheeledLeft)
  462. {
  463. ScrollHorizontal (-1);
  464. e.Handled = true;
  465. }
  466. }
  467. /// <summary>Gets the coordinates of the Cursor based on the SelectedCodePoint in screen coordinates</summary>
  468. public Point Cursor
  469. {
  470. get
  471. {
  472. int row = SelectedCodePoint / 16 * _rowHeight - Viewport.Y + 1;
  473. int col = SelectedCodePoint % 16 * COLUMN_WIDTH - Viewport.X + RowLabelWidth + 1; // + 1 for padding between label and first column
  474. return new (col, row);
  475. }
  476. set => throw new NotImplementedException ();
  477. }
  478. public static int MaxCodePoint = UnicodeRange.Ranges.Max (r => r.End);
  479. /// <summary>
  480. /// Specifies the starting offset for the character map. The default is 0x2500 which is the Box Drawing
  481. /// characters.
  482. /// </summary>
  483. public int SelectedCodePoint
  484. {
  485. get => _selected;
  486. set
  487. {
  488. if (_selected == value)
  489. {
  490. return;
  491. }
  492. _selected = value;
  493. if (IsInitialized)
  494. {
  495. int row = SelectedCodePoint / 16 * _rowHeight;
  496. int col = SelectedCodePoint % 16 * COLUMN_WIDTH;
  497. if (row - Viewport.Y < 0)
  498. {
  499. // Moving up.
  500. Viewport = Viewport with { Y = row };
  501. }
  502. else if (row - Viewport.Y >= Viewport.Height)
  503. {
  504. // Moving down.
  505. Viewport = Viewport with { Y = row - Viewport.Height };
  506. }
  507. int width = Viewport.Width / COLUMN_WIDTH * COLUMN_WIDTH - RowLabelWidth;
  508. if (col - Viewport.X < 0)
  509. {
  510. // Moving left.
  511. Viewport = Viewport with { X = col };
  512. }
  513. else if (col - Viewport.X >= width)
  514. {
  515. // Moving right.
  516. Viewport = Viewport with { X = col - width };
  517. }
  518. }
  519. SetNeedsDisplay ();
  520. SelectedCodePointChanged?.Invoke (this, new (SelectedCodePoint, null));
  521. }
  522. }
  523. public bool ShowGlyphWidths
  524. {
  525. get => _rowHeight == 2;
  526. set
  527. {
  528. _rowHeight = value ? 2 : 1;
  529. SetNeedsDisplay ();
  530. }
  531. }
  532. /// <summary>
  533. /// Specifies the starting offset for the character map. The default is 0x2500 which is the Box Drawing
  534. /// characters.
  535. /// </summary>
  536. public int StartCodePoint
  537. {
  538. get => _start;
  539. set
  540. {
  541. _start = value;
  542. SelectedCodePoint = value;
  543. Viewport = Viewport with { Y = SelectedCodePoint / 16 * _rowHeight };
  544. SetNeedsDisplay ();
  545. }
  546. }
  547. private static int RowLabelWidth => $"U+{MaxCodePoint:x5}".Length + 1;
  548. private static int RowWidth => RowLabelWidth + COLUMN_WIDTH * 16;
  549. public event EventHandler<ListViewItemEventArgs> Hover;
  550. public override void OnDrawContent (Rectangle viewport)
  551. {
  552. if (viewport.Height == 0 || viewport.Width == 0)
  553. {
  554. return;
  555. }
  556. Clear ();
  557. int cursorCol = Cursor.X + Viewport.X - RowLabelWidth - 1;
  558. int cursorRow = Cursor.Y + Viewport.Y - 1;
  559. Driver.SetAttribute (GetHotNormalColor ());
  560. Move (0, 0);
  561. Driver.AddStr (new (' ', RowLabelWidth + 1));
  562. int firstColumnX = RowLabelWidth - Viewport.X;
  563. // Header
  564. for (var hexDigit = 0; hexDigit < 16; hexDigit++)
  565. {
  566. int x = firstColumnX + hexDigit * COLUMN_WIDTH;
  567. if (x > RowLabelWidth - 2)
  568. {
  569. Move (x, 0);
  570. Driver.SetAttribute (GetHotNormalColor ());
  571. Driver.AddStr (" ");
  572. Driver.SetAttribute (HasFocus && cursorCol + firstColumnX == x ? ColorScheme.HotFocus : GetHotNormalColor ());
  573. Driver.AddStr ($"{hexDigit:x}");
  574. Driver.SetAttribute (GetHotNormalColor ());
  575. Driver.AddStr (" ");
  576. }
  577. }
  578. // Even though the Clip is set to prevent us from drawing on the row potentially occupied by the horizontal
  579. // scroll bar, we do the smart thing and not actually draw that row if not necessary.
  580. for (var y = 1; y < Viewport.Height; y++)
  581. {
  582. // What row is this?
  583. int row = (y + Viewport.Y - 1) / _rowHeight;
  584. int val = row * 16;
  585. if (val > MaxCodePoint)
  586. {
  587. break;
  588. }
  589. Move (firstColumnX + COLUMN_WIDTH, y);
  590. Driver.SetAttribute (GetNormalColor ());
  591. for (var col = 0; col < 16; col++)
  592. {
  593. int x = firstColumnX + COLUMN_WIDTH * col + 1;
  594. if (x < 0 || x > Viewport.Width - 1)
  595. {
  596. continue;
  597. }
  598. Move (x, y);
  599. // If we're at the cursor position, and we don't have focus, invert the colors.
  600. if (row == cursorRow && x == cursorCol && !HasFocus)
  601. {
  602. Driver.SetAttribute (GetFocusColor ());
  603. }
  604. int scalar = val + col;
  605. var rune = (Rune)'?';
  606. if (Rune.IsValid (scalar))
  607. {
  608. rune = new (scalar);
  609. }
  610. int width = rune.GetColumns ();
  611. if (!ShowGlyphWidths || (y + Viewport.Y) % _rowHeight > 0)
  612. {
  613. // Draw the rune
  614. if (width > 0)
  615. {
  616. Driver.AddRune (rune);
  617. }
  618. else
  619. {
  620. if (rune.IsCombiningMark ())
  621. {
  622. // This is a hack to work around the fact that combining marks
  623. // a) can't be rendered on their own
  624. // b) that don't normalize are not properly supported in
  625. // any known terminal (esp Windows/AtlasEngine).
  626. // See Issue #2616
  627. var sb = new StringBuilder ();
  628. sb.Append ('a');
  629. sb.Append (rune);
  630. // Try normalizing after combining with 'a'. If it normalizes, at least
  631. // it'll show on the 'a'. If not, just show the replacement char.
  632. string normal = sb.ToString ().Normalize (NormalizationForm.FormC);
  633. if (normal.Length == 1)
  634. {
  635. Driver.AddRune (normal [0]);
  636. }
  637. else
  638. {
  639. Driver.AddRune (Rune.ReplacementChar);
  640. }
  641. }
  642. }
  643. }
  644. else
  645. {
  646. // Draw the width of the rune
  647. Driver.SetAttribute (ColorScheme.HotNormal);
  648. Driver.AddStr ($"{width}");
  649. }
  650. // If we're at the cursor position, and we don't have focus, revert the colors to normal
  651. if (row == cursorRow && x == cursorCol && !HasFocus)
  652. {
  653. Driver.SetAttribute (GetNormalColor ());
  654. }
  655. }
  656. // Draw row label (U+XXXX_)
  657. Move (0, y);
  658. Driver.SetAttribute (HasFocus && y + Viewport.Y - 1 == cursorRow ? ColorScheme.HotFocus : ColorScheme.HotNormal);
  659. if (!ShowGlyphWidths || (y + Viewport.Y) % _rowHeight > 0)
  660. {
  661. Driver.AddStr ($"U+{val / 16:x5}_ ");
  662. }
  663. else
  664. {
  665. Driver.AddStr (new (' ', RowLabelWidth));
  666. }
  667. }
  668. }
  669. public override Point? PositionCursor ()
  670. {
  671. if (HasFocus
  672. && Cursor.X >= RowLabelWidth
  673. && Cursor.X < Viewport.Width
  674. && Cursor.Y > 0
  675. && Cursor.Y < Viewport.Height)
  676. {
  677. Move (Cursor.X, Cursor.Y);
  678. }
  679. else
  680. {
  681. return null;
  682. }
  683. return Cursor;
  684. }
  685. public event EventHandler<ListViewItemEventArgs> SelectedCodePointChanged;
  686. public static string ToCamelCase (string str)
  687. {
  688. if (string.IsNullOrEmpty (str))
  689. {
  690. return str;
  691. }
  692. TextInfo textInfo = new CultureInfo ("en-US", false).TextInfo;
  693. str = textInfo.ToLower (str);
  694. str = textInfo.ToTitleCase (str);
  695. return str;
  696. }
  697. private void CopyCodePoint () { Clipboard.Contents = $"U+{SelectedCodePoint:x5}"; }
  698. private void CopyGlyph () { Clipboard.Contents = $"{new Rune (SelectedCodePoint)}"; }
  699. private void Handle_MouseClick (object sender, MouseEventEventArgs args)
  700. {
  701. MouseEvent me = args.MouseEvent;
  702. if (me.Flags != MouseFlags.ReportMousePosition && me.Flags != MouseFlags.Button1Clicked && me.Flags != MouseFlags.Button1DoubleClicked)
  703. {
  704. return;
  705. }
  706. if (me.Position.Y == 0)
  707. {
  708. me.Position = me.Position with { Y = Cursor.Y };
  709. }
  710. if (me.Position.X < RowLabelWidth || me.Position.X > RowLabelWidth + 16 * COLUMN_WIDTH - 1)
  711. {
  712. me.Position = me.Position with { X = Cursor.X };
  713. }
  714. int row = (me.Position.Y - 1 - -Viewport.Y) / _rowHeight; // -1 for header
  715. int col = (me.Position.X - RowLabelWidth - -Viewport.X) / COLUMN_WIDTH;
  716. if (col > 15)
  717. {
  718. col = 15;
  719. }
  720. int val = row * 16 + col;
  721. if (val > MaxCodePoint)
  722. {
  723. return;
  724. }
  725. if (me.Flags == MouseFlags.ReportMousePosition)
  726. {
  727. Hover?.Invoke (this, new (val, null));
  728. }
  729. if (!HasFocus && CanFocus)
  730. {
  731. SetFocus ();
  732. }
  733. args.Handled = true;
  734. if (me.Flags == MouseFlags.Button1Clicked)
  735. {
  736. SelectedCodePoint = val;
  737. return;
  738. }
  739. if (me.Flags == MouseFlags.Button1DoubleClicked)
  740. {
  741. SelectedCodePoint = val;
  742. ShowDetails ();
  743. return;
  744. }
  745. if (me.Flags == _contextMenu.MouseFlags)
  746. {
  747. SelectedCodePoint = val;
  748. _contextMenu = new ()
  749. {
  750. Position = new (me.Position.X + 1, me.Position.Y + 1),
  751. MenuItems = new (
  752. new MenuItem []
  753. {
  754. new (
  755. "_Copy Glyph",
  756. "",
  757. CopyGlyph,
  758. null,
  759. null,
  760. (KeyCode)Key.C.WithCtrl
  761. ),
  762. new (
  763. "Copy Code _Point",
  764. "",
  765. CopyCodePoint,
  766. null,
  767. null,
  768. (KeyCode)Key.C.WithCtrl
  769. .WithShift
  770. )
  771. }
  772. )
  773. };
  774. _contextMenu.Show ();
  775. }
  776. }
  777. private void ShowDetails ()
  778. {
  779. var client = new UcdApiClient ();
  780. var decResponse = string.Empty;
  781. var getCodePointError = string.Empty;
  782. var waitIndicator = new Dialog
  783. {
  784. Title = "Getting Code Point Information",
  785. X = Pos.Center (),
  786. Y = Pos.Center (),
  787. Height = 7,
  788. Width = 50,
  789. Buttons = [new () { Text = "Cancel" }]
  790. };
  791. var errorLabel = new Label
  792. {
  793. Text = UcdApiClient.BaseUrl,
  794. X = 0,
  795. Y = 1,
  796. Width = Dim.Fill (),
  797. Height = Dim.Fill (1),
  798. TextAlignment = Alignment.Center
  799. };
  800. var spinner = new SpinnerView { X = Pos.Center (), Y = Pos.Center (), Style = new Aesthetic () };
  801. spinner.AutoSpin = true;
  802. waitIndicator.Add (errorLabel);
  803. waitIndicator.Add (spinner);
  804. waitIndicator.Ready += async (s, a) =>
  805. {
  806. try
  807. {
  808. decResponse = await client.GetCodepointDec (SelectedCodePoint);
  809. Application.Invoke (() => waitIndicator.RequestStop ());
  810. }
  811. catch (HttpRequestException e)
  812. {
  813. getCodePointError = errorLabel.Text = e.Message;
  814. Application.Invoke (() => waitIndicator.RequestStop ());
  815. }
  816. };
  817. Application.Run (waitIndicator);
  818. waitIndicator.Dispose ();
  819. if (!string.IsNullOrEmpty (decResponse))
  820. {
  821. var name = string.Empty;
  822. using (JsonDocument document = JsonDocument.Parse (decResponse))
  823. {
  824. JsonElement root = document.RootElement;
  825. // Get a property by name and output its value
  826. if (root.TryGetProperty ("name", out JsonElement nameElement))
  827. {
  828. name = nameElement.GetString ();
  829. }
  830. //// Navigate to a nested property and output its value
  831. //if (root.TryGetProperty ("property3", out JsonElement property3Element)
  832. //&& property3Element.TryGetProperty ("nestedProperty", out JsonElement nestedPropertyElement)) {
  833. // Console.WriteLine (nestedPropertyElement.GetString ());
  834. //}
  835. decResponse = JsonSerializer.Serialize (
  836. document.RootElement,
  837. new
  838. JsonSerializerOptions
  839. { WriteIndented = true }
  840. );
  841. }
  842. var title = $"{ToCamelCase (name)} - {new Rune (SelectedCodePoint)} U+{SelectedCodePoint:x5}";
  843. var copyGlyph = new Button { Text = "Copy _Glyph" };
  844. var copyCP = new Button { Text = "Copy Code _Point" };
  845. var cancel = new Button { Text = "Cancel" };
  846. var dlg = new Dialog { Title = title, Buttons = [copyGlyph, copyCP, cancel] };
  847. copyGlyph.Accept += (s, a) =>
  848. {
  849. CopyGlyph ();
  850. dlg.RequestStop ();
  851. };
  852. copyCP.Accept += (s, a) =>
  853. {
  854. CopyCodePoint ();
  855. dlg.RequestStop ();
  856. };
  857. cancel.Accept += (s, a) => dlg.RequestStop ();
  858. var rune = (Rune)SelectedCodePoint;
  859. var label = new Label { Text = "IsAscii: ", X = 0, Y = 0 };
  860. dlg.Add (label);
  861. label = new () { Text = $"{rune.IsAscii}", X = Pos.Right (label), Y = Pos.Top (label) };
  862. dlg.Add (label);
  863. label = new () { Text = ", Bmp: ", X = Pos.Right (label), Y = Pos.Top (label) };
  864. dlg.Add (label);
  865. label = new () { Text = $"{rune.IsBmp}", X = Pos.Right (label), Y = Pos.Top (label) };
  866. dlg.Add (label);
  867. label = new () { Text = ", CombiningMark: ", X = Pos.Right (label), Y = Pos.Top (label) };
  868. dlg.Add (label);
  869. label = new () { Text = $"{rune.IsCombiningMark ()}", X = Pos.Right (label), Y = Pos.Top (label) };
  870. dlg.Add (label);
  871. label = new () { Text = ", SurrogatePair: ", X = Pos.Right (label), Y = Pos.Top (label) };
  872. dlg.Add (label);
  873. label = new () { Text = $"{rune.IsSurrogatePair ()}", X = Pos.Right (label), Y = Pos.Top (label) };
  874. dlg.Add (label);
  875. label = new () { Text = ", Plane: ", X = Pos.Right (label), Y = Pos.Top (label) };
  876. dlg.Add (label);
  877. label = new () { Text = $"{rune.Plane}", X = Pos.Right (label), Y = Pos.Top (label) };
  878. dlg.Add (label);
  879. label = new () { Text = "Columns: ", X = 0, Y = Pos.Bottom (label) };
  880. dlg.Add (label);
  881. label = new () { Text = $"{rune.GetColumns ()}", X = Pos.Right (label), Y = Pos.Top (label) };
  882. dlg.Add (label);
  883. label = new () { Text = ", Utf16SequenceLength: ", X = Pos.Right (label), Y = Pos.Top (label) };
  884. dlg.Add (label);
  885. label = new () { Text = $"{rune.Utf16SequenceLength}", X = Pos.Right (label), Y = Pos.Top (label) };
  886. dlg.Add (label);
  887. label = new ()
  888. {
  889. Text =
  890. $"Code Point Information from {UcdApiClient.BaseUrl}codepoint/dec/{SelectedCodePoint}:",
  891. X = 0,
  892. Y = Pos.Bottom (label)
  893. };
  894. dlg.Add (label);
  895. var json = new TextView
  896. {
  897. X = 0,
  898. Y = Pos.Bottom (label),
  899. Width = Dim.Fill (),
  900. Height = Dim.Fill (2),
  901. ReadOnly = true,
  902. Text = decResponse
  903. };
  904. dlg.Add (json);
  905. Application.Run (dlg);
  906. dlg.Dispose ();
  907. }
  908. else
  909. {
  910. MessageBox.ErrorQuery (
  911. "Code Point API",
  912. $"{UcdApiClient.BaseUrl}codepoint/dec/{SelectedCodePoint} did not return a result for\r\n {new Rune (SelectedCodePoint)} U+{SelectedCodePoint:x5}.",
  913. "Ok"
  914. );
  915. }
  916. // BUGBUG: This is a workaround for some weird ScrollView related mouse grab bug
  917. Application.GrabMouse (this);
  918. }
  919. }
  920. public class UcdApiClient
  921. {
  922. public const string BaseUrl = "https://ucdapi.org/unicode/latest/";
  923. private static readonly HttpClient _httpClient = new ();
  924. public async Task<string> GetChars (string chars)
  925. {
  926. HttpResponseMessage response = await _httpClient.GetAsync ($"{BaseUrl}chars/{Uri.EscapeDataString (chars)}");
  927. response.EnsureSuccessStatusCode ();
  928. return await response.Content.ReadAsStringAsync ();
  929. }
  930. public async Task<string> GetCharsName (string chars)
  931. {
  932. HttpResponseMessage response =
  933. await _httpClient.GetAsync ($"{BaseUrl}chars/{Uri.EscapeDataString (chars)}/name");
  934. response.EnsureSuccessStatusCode ();
  935. return await response.Content.ReadAsStringAsync ();
  936. }
  937. public async Task<string> GetCodepointDec (int dec)
  938. {
  939. HttpResponseMessage response = await _httpClient.GetAsync ($"{BaseUrl}codepoint/dec/{dec}");
  940. response.EnsureSuccessStatusCode ();
  941. return await response.Content.ReadAsStringAsync ();
  942. }
  943. public async Task<string> GetCodepointHex (string hex)
  944. {
  945. HttpResponseMessage response = await _httpClient.GetAsync ($"{BaseUrl}codepoint/hex/{hex}");
  946. response.EnsureSuccessStatusCode ();
  947. return await response.Content.ReadAsStringAsync ();
  948. }
  949. }
  950. internal class UnicodeRange
  951. {
  952. public static List<UnicodeRange> Ranges = GetRanges ();
  953. public string Category;
  954. public int End;
  955. public int Start;
  956. public UnicodeRange (int start, int end, string category)
  957. {
  958. Start = start;
  959. End = end;
  960. Category = category;
  961. }
  962. public static List<UnicodeRange> GetRanges ()
  963. {
  964. IEnumerable<UnicodeRange> ranges =
  965. from r in typeof (UnicodeRanges).GetProperties (BindingFlags.Static | BindingFlags.Public)
  966. let urange = r.GetValue (null) as System.Text.Unicode.UnicodeRange
  967. let name = string.IsNullOrEmpty (r.Name)
  968. ? $"U+{urange.FirstCodePoint:x5}-U+{urange.FirstCodePoint + urange.Length:x5}"
  969. : r.Name
  970. where name != "None" && name != "All"
  971. select new UnicodeRange (urange.FirstCodePoint, urange.FirstCodePoint + urange.Length, name);
  972. // .NET 8.0 only supports BMP in UnicodeRanges: https://learn.microsoft.com/en-us/dotnet/api/system.text.unicode.unicoderanges?view=net-8.0
  973. List<UnicodeRange> nonBmpRanges = new ()
  974. {
  975. new (
  976. 0x1F130,
  977. 0x1F149,
  978. "Squared Latin Capital Letters"
  979. ),
  980. new (
  981. 0x12400,
  982. 0x1240f,
  983. "Cuneiform Numbers and Punctuation"
  984. ),
  985. new (0x10000, 0x1007F, "Linear B Syllabary"),
  986. new (0x10080, 0x100FF, "Linear B Ideograms"),
  987. new (0x10100, 0x1013F, "Aegean Numbers"),
  988. new (0x10300, 0x1032F, "Old Italic"),
  989. new (0x10330, 0x1034F, "Gothic"),
  990. new (0x10380, 0x1039F, "Ugaritic"),
  991. new (0x10400, 0x1044F, "Deseret"),
  992. new (0x10450, 0x1047F, "Shavian"),
  993. new (0x10480, 0x104AF, "Osmanya"),
  994. new (0x10800, 0x1083F, "Cypriot Syllabary"),
  995. new (
  996. 0x1D000,
  997. 0x1D0FF,
  998. "Byzantine Musical Symbols"
  999. ),
  1000. new (0x1D100, 0x1D1FF, "Musical Symbols"),
  1001. new (0x1D300, 0x1D35F, "Tai Xuan Jing Symbols"),
  1002. new (
  1003. 0x1D400,
  1004. 0x1D7FF,
  1005. "Mathematical Alphanumeric Symbols"
  1006. ),
  1007. new (0x1F600, 0x1F532, "Emojis Symbols"),
  1008. new (
  1009. 0x20000,
  1010. 0x2A6DF,
  1011. "CJK Unified Ideographs Extension B"
  1012. ),
  1013. new (
  1014. 0x2F800,
  1015. 0x2FA1F,
  1016. "CJK Compatibility Ideographs Supplement"
  1017. ),
  1018. new (0xE0000, 0xE007F, "Tags")
  1019. };
  1020. return ranges.Concat (nonBmpRanges).OrderBy (r => r.Category).ToList ();
  1021. }
  1022. }