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