CharacterMap.cs 42 KB

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