CharMap.cs 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774
  1. #nullable enable
  2. using System.Diagnostics.CodeAnalysis;
  3. using System.Globalization;
  4. using System.Text.Json;
  5. namespace Terminal.Gui.Views;
  6. /// <summary>
  7. /// A scrollable map of the Unicode codepoints.
  8. /// </summary>
  9. /// <remarks>
  10. /// See <see href="../docs/CharacterMap.md"/> for details.
  11. /// </remarks>
  12. public class CharMap : View, IDesignable
  13. {
  14. private const int COLUMN_WIDTH = 3; // Width of each column of glyphs
  15. private const int HEADER_HEIGHT = 1; // Height of the header
  16. private int _rowHeight = 1; // Height of each row of 16 glyphs - changing this is not tested
  17. /// <summary>
  18. /// Initializes a new instance.
  19. /// </summary>
  20. [RequiresUnreferencedCode ("AOT")]
  21. [RequiresDynamicCode ("AOT")]
  22. public CharMap ()
  23. {
  24. CanFocus = true;
  25. CursorVisibility = CursorVisibility.Default;
  26. AddCommand (Command.Up, commandContext => Move (commandContext, -16));
  27. AddCommand (Command.Down, commandContext => Move (commandContext, 16));
  28. AddCommand (Command.Left, commandContext => Move (commandContext, -1));
  29. AddCommand (Command.Right, commandContext => Move (commandContext, 1));
  30. AddCommand (Command.PageUp, commandContext => Move (commandContext, -(Viewport.Height - HEADER_HEIGHT / _rowHeight) * 16));
  31. AddCommand (Command.PageDown, commandContext => Move (commandContext, (Viewport.Height - HEADER_HEIGHT / _rowHeight) * 16));
  32. AddCommand (Command.Start, commandContext => Move (commandContext, -SelectedCodePoint));
  33. AddCommand (Command.End, commandContext => Move (commandContext, MAX_CODE_POINT - SelectedCodePoint));
  34. AddCommand (Command.ScrollDown, () => ScrollVertical (1));
  35. AddCommand (Command.ScrollUp, () => ScrollVertical (-1));
  36. AddCommand (Command.ScrollRight, () => ScrollHorizontal (1));
  37. AddCommand (Command.ScrollLeft, () => ScrollHorizontal (-1));
  38. AddCommand (Command.Accept, HandleAcceptCommand);
  39. AddCommand (Command.Select, HandleSelectCommand);
  40. AddCommand (Command.Context, HandleContextCommand);
  41. KeyBindings.Add (Key.CursorUp, Command.Up);
  42. KeyBindings.Add (Key.CursorDown, Command.Down);
  43. KeyBindings.Add (Key.CursorLeft, Command.Left);
  44. KeyBindings.Add (Key.CursorRight, Command.Right);
  45. KeyBindings.Add (Key.PageUp, Command.PageUp);
  46. KeyBindings.Add (Key.PageDown, Command.PageDown);
  47. KeyBindings.Add (Key.Home, Command.Start);
  48. KeyBindings.Add (Key.End, Command.End);
  49. KeyBindings.Add (PopoverMenu.DefaultKey, Command.Context);
  50. MouseBindings.Add (MouseFlags.Button1DoubleClicked, Command.Accept);
  51. MouseBindings.ReplaceCommands (MouseFlags.Button3Clicked, Command.Context);
  52. MouseBindings.ReplaceCommands (MouseFlags.Button1Clicked | MouseFlags.ButtonCtrl, Command.Context);
  53. MouseBindings.Add (MouseFlags.WheeledDown, Command.ScrollDown);
  54. MouseBindings.Add (MouseFlags.WheeledUp, Command.ScrollUp);
  55. MouseBindings.Add (MouseFlags.WheeledLeft, Command.ScrollLeft);
  56. MouseBindings.Add (MouseFlags.WheeledRight, Command.ScrollRight);
  57. SetContentSize (new (COLUMN_WIDTH * 16 + RowLabelWidth, MAX_CODE_POINT / 16 * _rowHeight + HEADER_HEIGHT));
  58. // Set up the horizontal scrollbar. Turn off AutoShow since we do it manually.
  59. HorizontalScrollBar.AutoShow = false;
  60. HorizontalScrollBar.Increment = COLUMN_WIDTH;
  61. // This prevents scrolling past the last column
  62. HorizontalScrollBar.ScrollableContentSize = GetContentSize ().Width - RowLabelWidth;
  63. HorizontalScrollBar.X = RowLabelWidth;
  64. HorizontalScrollBar.Y = Pos.AnchorEnd ();
  65. HorizontalScrollBar.Width = Dim.Fill (1);
  66. // We want the horizontal scrollbar to only show when needed.
  67. // We can't use ScrollBar.AutoShow because we are using custom ContentSize
  68. // So, we do it manually on ViewportChanged events.
  69. ViewportChanged += (sender, args) =>
  70. {
  71. if (Viewport.Width < GetContentSize ().Width)
  72. {
  73. HorizontalScrollBar.Visible = true;
  74. }
  75. else
  76. {
  77. HorizontalScrollBar.Visible = false;
  78. }
  79. };
  80. // Set up the vertical scrollbar. Turn off AutoShow since it's always visible.
  81. VerticalScrollBar.AutoShow = true;
  82. VerticalScrollBar.Visible = false;
  83. VerticalScrollBar.X = Pos.AnchorEnd ();
  84. VerticalScrollBar.Y = HEADER_HEIGHT; // Header
  85. // The scrollbars are in the Padding. VisualRole.Focus/Active are used to draw the
  86. // CharMap headers. Override Padding to force it to draw to match.
  87. Padding!.GettingAttributeForRole += PaddingOnGettingAttributeForRole;
  88. }
  89. private void PaddingOnGettingAttributeForRole (object? sender, VisualRoleEventArgs e)
  90. {
  91. if (e.Role != VisualRole.Focus && e.Role != VisualRole.Active)
  92. {
  93. e.Result = GetAttributeForRole (HasFocus ? VisualRole.Focus : VisualRole.Active);
  94. }
  95. e.Handled = true;
  96. }
  97. private bool? Move (ICommandContext? commandContext, int cpOffset)
  98. {
  99. if (RaiseSelecting (commandContext) is true)
  100. {
  101. return true;
  102. }
  103. SelectedCodePoint += cpOffset;
  104. return true;
  105. }
  106. private void ScrollToMakeCursorVisible (Point offsetToNewCursor)
  107. {
  108. // Adjust vertical scrolling
  109. if (offsetToNewCursor.Y < 1) // Header is at Y = 0
  110. {
  111. ScrollVertical (offsetToNewCursor.Y - HEADER_HEIGHT);
  112. }
  113. else if (offsetToNewCursor.Y >= Viewport.Height)
  114. {
  115. ScrollVertical (offsetToNewCursor.Y - Viewport.Height + HEADER_HEIGHT);
  116. }
  117. // Adjust horizontal scrolling
  118. if (offsetToNewCursor.X < RowLabelWidth + 1)
  119. {
  120. ScrollHorizontal (offsetToNewCursor.X - (RowLabelWidth + 1));
  121. }
  122. else if (offsetToNewCursor.X >= Viewport.Width)
  123. {
  124. ScrollHorizontal (offsetToNewCursor.X - Viewport.Width + 1);
  125. }
  126. }
  127. #region Cursor
  128. private Point GetCursor (int codePoint)
  129. {
  130. // + 1 for padding between label and first column
  131. int x = codePoint % 16 * COLUMN_WIDTH + RowLabelWidth + 1 - Viewport.X;
  132. int y = codePoint / 16 * _rowHeight + HEADER_HEIGHT - Viewport.Y;
  133. return new (x, y);
  134. }
  135. /// <inheritdoc/>
  136. public override Point? PositionCursor ()
  137. {
  138. Point cursor = GetCursor (SelectedCodePoint);
  139. if (HasFocus
  140. && cursor.X >= RowLabelWidth
  141. && cursor.X < Viewport.Width
  142. && cursor.Y > 0
  143. && cursor.Y < Viewport.Height)
  144. {
  145. Move (cursor.X, cursor.Y);
  146. }
  147. else
  148. {
  149. return null;
  150. }
  151. return cursor;
  152. }
  153. #endregion Cursor
  154. // ReSharper disable once InconsistentNaming
  155. private static readonly int MAX_CODE_POINT = UnicodeRange.Ranges.Max (r => r.End);
  156. private int _selectedCodepoint; // Currently selected codepoint
  157. private int _startCodepoint; // The codepoint that will be displayed at the top of the Viewport
  158. /// <summary>
  159. /// Gets or sets the currently selected codepoint. Causes the Viewport to scroll to make the selected code point
  160. /// visible.
  161. /// </summary>
  162. public int SelectedCodePoint
  163. {
  164. get => _selectedCodepoint;
  165. set
  166. {
  167. if (_selectedCodepoint == value)
  168. {
  169. return;
  170. }
  171. int newSelectedCodePoint = Math.Clamp (value, 0, MAX_CODE_POINT);
  172. Point offsetToNewCursor = GetCursor (newSelectedCodePoint);
  173. _selectedCodepoint = newSelectedCodePoint;
  174. // Ensure the new cursor position is visible
  175. ScrollToMakeCursorVisible (offsetToNewCursor);
  176. SetNeedsDraw ();
  177. SelectedCodePointChanged?.Invoke (this, new (SelectedCodePoint));
  178. }
  179. }
  180. /// <summary>
  181. /// Raised when the selected code point changes.
  182. /// </summary>
  183. public event EventHandler<EventArgs<int>>? SelectedCodePointChanged;
  184. /// <summary>
  185. /// Specifies the starting offset for the character map. The default is 0x2500 which is the Box Drawing
  186. /// characters.
  187. /// </summary>
  188. public int StartCodePoint
  189. {
  190. get => _startCodepoint;
  191. set
  192. {
  193. _startCodepoint = value;
  194. SelectedCodePoint = value;
  195. }
  196. }
  197. /// <summary>
  198. /// Gets or sets whether the number of columns each glyph is displayed.
  199. /// </summary>
  200. public bool ShowGlyphWidths
  201. {
  202. get => _rowHeight == 2;
  203. set
  204. {
  205. _rowHeight = value ? 2 : 1;
  206. SetNeedsDraw ();
  207. }
  208. }
  209. private void CopyCodePoint () { Clipboard.Contents = $"U+{SelectedCodePoint:x5}"; }
  210. private void CopyGlyph () { Clipboard.Contents = $"{new Rune (SelectedCodePoint)}"; }
  211. #region Drawing
  212. private static int RowLabelWidth => $"U+{MAX_CODE_POINT:x5}".Length + 1;
  213. /// <inheritdoc/>
  214. protected override bool OnDrawingContent ()
  215. {
  216. if (Viewport.Height == 0 || Viewport.Width == 0)
  217. {
  218. return true;
  219. }
  220. int selectedCol = SelectedCodePoint % 16;
  221. int selectedRow = SelectedCodePoint / 16;
  222. // Headers
  223. // Clear the header area
  224. Move (0, 0);
  225. SetAttributeForRole (HasFocus ? VisualRole.Focus : VisualRole.Active);
  226. AddStr (new (' ', Viewport.Width));
  227. int firstColumnX = RowLabelWidth - Viewport.X;
  228. // Header
  229. var x = 0;
  230. for (var hexDigit = 0; hexDigit < 16; hexDigit++)
  231. {
  232. x = firstColumnX + hexDigit * COLUMN_WIDTH;
  233. if (x > RowLabelWidth - 2)
  234. {
  235. Move (x, 0);
  236. SetAttributeForRole (HasFocus ? VisualRole.Focus : VisualRole.Active);
  237. AddStr (" ");
  238. // Swap Active/Focus so the selected column is highlighted
  239. if (hexDigit == selectedCol)
  240. {
  241. SetAttributeForRole (HasFocus ? VisualRole.Active : VisualRole.Focus);
  242. }
  243. AddStr ($"{hexDigit:x}");
  244. SetAttributeForRole (HasFocus ? VisualRole.Focus : VisualRole.Active);
  245. AddStr (" ");
  246. }
  247. }
  248. // Start at 1 because Header.
  249. for (var y = 1; y < Viewport.Height; y++)
  250. {
  251. // What row is this?
  252. int row = (y + Viewport.Y - 1) / _rowHeight;
  253. int val = row * 16;
  254. // Draw the row label (U+XXXX_)
  255. SetAttributeForRole (HasFocus ? VisualRole.Focus : VisualRole.Active);
  256. Move (0, y);
  257. // Swap Active/Focus so the selected row is highlighted
  258. if (y + Viewport.Y - 1 == selectedRow)
  259. {
  260. SetAttributeForRole (HasFocus ? VisualRole.Active : VisualRole.Focus);
  261. }
  262. if (val > MAX_CODE_POINT)
  263. {
  264. // No row
  265. Move (0, y);
  266. AddStr (new (' ', RowLabelWidth));
  267. continue;
  268. }
  269. if (!ShowGlyphWidths || (y + Viewport.Y) % _rowHeight > 0)
  270. {
  271. AddStr ($"U+{val / 16:x5}_");
  272. }
  273. else
  274. {
  275. AddStr (new (' ', RowLabelWidth));
  276. }
  277. // Draw the row
  278. SetAttributeForRole (VisualRole.Normal);
  279. for (var col = 0; col < 16; col++)
  280. {
  281. x = firstColumnX + COLUMN_WIDTH * col + 1;
  282. if (x < RowLabelWidth || x > Viewport.Width - 1)
  283. {
  284. continue;
  285. }
  286. Move (x, y);
  287. // If we're at the cursor position highlight the cell
  288. if (row == selectedRow && col == selectedCol)
  289. {
  290. SetAttributeForRole (VisualRole.Active);
  291. }
  292. int scalar = val + col;
  293. var rune = (Rune)'?';
  294. if (Rune.IsValid (scalar))
  295. {
  296. rune = new (scalar);
  297. }
  298. int width = rune.GetColumns ();
  299. if (!ShowGlyphWidths || (y + Viewport.Y) % _rowHeight > 0)
  300. {
  301. // Draw the rune
  302. if (width > 0)
  303. {
  304. AddRune (rune);
  305. }
  306. else
  307. {
  308. if (rune.IsCombiningMark ())
  309. {
  310. // This is a hack to work around the fact that combining marks
  311. // a) can't be rendered on their own
  312. // b) that don't normalize are not properly supported in
  313. // any known terminal (esp Windows/AtlasEngine).
  314. // See Issue #2616
  315. var sb = new StringBuilder ();
  316. sb.Append ('a');
  317. sb.Append (rune);
  318. // Try normalizing after combining with 'a'. If it normalizes, at least
  319. // it'll show on the 'a'. If not, just show the replacement char.
  320. string normal = sb.ToString ().Normalize (NormalizationForm.FormC);
  321. if (normal.Length == 1)
  322. {
  323. AddRune ((Rune)normal [0]);
  324. }
  325. else
  326. {
  327. AddRune (Rune.ReplacementChar);
  328. }
  329. }
  330. }
  331. }
  332. else
  333. {
  334. // Draw the width of the rune faint
  335. Attribute attr = GetAttributeForRole (VisualRole.Normal);
  336. SetAttribute (attr with { Style = attr.Style | TextStyle.Faint });
  337. AddStr ($"{width}");
  338. }
  339. // If we're at the cursor position, and we don't have focus
  340. if (row == selectedRow && col == selectedCol)
  341. {
  342. SetAttributeForRole (VisualRole.Normal);
  343. }
  344. }
  345. }
  346. return true;
  347. }
  348. /// <summary>
  349. /// Helper to convert a string into camel case.
  350. /// </summary>
  351. /// <param name="str"></param>
  352. /// <returns></returns>
  353. public static string ToCamelCase (string str)
  354. {
  355. if (string.IsNullOrEmpty (str))
  356. {
  357. return str;
  358. }
  359. TextInfo textInfo = new CultureInfo ("en-US", false).TextInfo;
  360. str = textInfo.ToLower (str);
  361. str = textInfo.ToTitleCase (str);
  362. return str;
  363. }
  364. #endregion Drawing
  365. #region Mouse Handling
  366. private bool? HandleSelectCommand (ICommandContext? commandContext)
  367. {
  368. Point position = GetCursor (SelectedCodePoint);
  369. if (commandContext is CommandContext<MouseBinding> { Binding.MouseEventArgs: { } } mouseCommandContext)
  370. {
  371. // If the mouse is clicked on the headers, map it to the first glyph of the row/col
  372. position = mouseCommandContext.Binding.MouseEventArgs.Position;
  373. if (position.Y == 0)
  374. {
  375. position = position with { Y = GetCursor (SelectedCodePoint).Y };
  376. }
  377. if (position.X < RowLabelWidth || position.X > RowLabelWidth + 16 * COLUMN_WIDTH - 1)
  378. {
  379. position = position with { X = GetCursor (SelectedCodePoint).X };
  380. }
  381. }
  382. if (RaiseSelecting (commandContext) is true)
  383. {
  384. return true;
  385. }
  386. if (!TryGetCodePointFromPosition (position, out int cp))
  387. {
  388. return false;
  389. }
  390. if (cp != SelectedCodePoint)
  391. {
  392. if (!HasFocus && CanFocus)
  393. {
  394. SetFocus ();
  395. }
  396. SelectedCodePoint = cp;
  397. }
  398. return true;
  399. }
  400. [RequiresUnreferencedCode ("AOT")]
  401. [RequiresDynamicCode ("AOT")]
  402. private bool? HandleAcceptCommand (ICommandContext? commandContext)
  403. {
  404. if (RaiseAccepting (commandContext) is true)
  405. {
  406. return true;
  407. }
  408. if (commandContext is CommandContext<MouseBinding> { Binding.MouseEventArgs: { } } mouseCommandContext)
  409. {
  410. if (!HasFocus && CanFocus)
  411. {
  412. SetFocus ();
  413. }
  414. if (!TryGetCodePointFromPosition (mouseCommandContext.Binding.MouseEventArgs.Position, out int cp))
  415. {
  416. return false;
  417. }
  418. SelectedCodePoint = cp;
  419. }
  420. ShowDetails ();
  421. return true;
  422. }
  423. private bool? HandleContextCommand (ICommandContext? commandContext)
  424. {
  425. int newCodePoint = SelectedCodePoint;
  426. if (commandContext is CommandContext<MouseBinding> { Binding.MouseEventArgs: { } } mouseCommandContext)
  427. {
  428. if (!TryGetCodePointFromPosition (mouseCommandContext.Binding.MouseEventArgs.Position, out newCodePoint))
  429. {
  430. return false;
  431. }
  432. }
  433. if (!HasFocus && CanFocus)
  434. {
  435. SetFocus ();
  436. }
  437. SelectedCodePoint = newCodePoint;
  438. // This demonstrates how to create an ephemeral Popover; one that exists
  439. // ony as long as the popover is visible.
  440. // Note, for ephemeral Popovers, hotkeys are not supported.
  441. PopoverMenu? contextMenu = new (
  442. [
  443. new (Strings.charMapCopyGlyph, string.Empty, CopyGlyph),
  444. new (Strings.charMapCopyCP, string.Empty, CopyCodePoint)
  445. ]);
  446. // Registering with the PopoverManager will ensure that the context menu is closed when the view is no longer focused
  447. // and the context menu is disposed when it is closed.
  448. Application.Popover?.Register (contextMenu);
  449. contextMenu?.MakeVisible (ViewportToScreen (GetCursor (SelectedCodePoint)));
  450. return true;
  451. }
  452. private bool TryGetCodePointFromPosition (Point position, out int codePoint)
  453. {
  454. if (position.X < RowLabelWidth || position.Y < 1)
  455. {
  456. codePoint = 0;
  457. return false;
  458. }
  459. int row = (position.Y - 1 - -Viewport.Y) / _rowHeight; // -1 for header
  460. int col = (position.X - RowLabelWidth - -Viewport.X) / COLUMN_WIDTH;
  461. if (col > 15)
  462. {
  463. col = 15;
  464. }
  465. codePoint = row * 16 + col;
  466. if (codePoint > MAX_CODE_POINT)
  467. {
  468. return false;
  469. }
  470. return true;
  471. }
  472. #endregion Mouse Handling
  473. #region Details Dialog
  474. [RequiresUnreferencedCode ("AOT")]
  475. [RequiresDynamicCode ("AOT")]
  476. private void ShowDetails ()
  477. {
  478. if (!Application.Initialized)
  479. {
  480. // Some unit tests invoke Accept without Init
  481. return;
  482. }
  483. UcdApiClient? client = new ();
  484. var decResponse = string.Empty;
  485. var getCodePointError = string.Empty;
  486. Dialog? waitIndicator = new ()
  487. {
  488. Title = Strings.charMapCPInfoDlgTitle,
  489. X = Pos.Center (),
  490. Y = Pos.Center (),
  491. Width = 40,
  492. Height = 10,
  493. Buttons = [new () { Text = Strings.btnCancel }]
  494. };
  495. var errorLabel = new Label
  496. {
  497. Text = UcdApiClient.BaseUrl,
  498. X = 0,
  499. Y = 0,
  500. Width = Dim.Fill (),
  501. Height = Dim.Fill (3),
  502. TextAlignment = Alignment.Center
  503. };
  504. var spinner = new SpinnerView
  505. {
  506. X = Pos.Center (),
  507. Y = Pos.Bottom (errorLabel),
  508. Style = new SpinnerStyle.Aesthetic ()
  509. };
  510. spinner.AutoSpin = true;
  511. waitIndicator.Add (errorLabel);
  512. waitIndicator.Add (spinner);
  513. waitIndicator.Ready += async (s, a) =>
  514. {
  515. try
  516. {
  517. decResponse = await client.GetCodepointDec (SelectedCodePoint).ConfigureAwait (false);
  518. Application.Invoke (() => waitIndicator.RequestStop ());
  519. }
  520. catch (HttpRequestException e)
  521. {
  522. getCodePointError = errorLabel.Text = e.Message;
  523. Application.Invoke (() => waitIndicator.RequestStop ());
  524. }
  525. };
  526. Application.Run (waitIndicator);
  527. waitIndicator.Dispose ();
  528. if (!string.IsNullOrEmpty (decResponse))
  529. {
  530. var name = string.Empty;
  531. using (JsonDocument document = JsonDocument.Parse (decResponse))
  532. {
  533. JsonElement root = document.RootElement;
  534. // Get a property by name and output its value
  535. if (root.TryGetProperty ("name", out JsonElement nameElement))
  536. {
  537. name = nameElement.GetString ();
  538. }
  539. //// Navigate to a nested property and output its value
  540. //if (root.TryGetProperty ("property3", out JsonElement property3Element)
  541. //&& property3Element.TryGetProperty ("nestedProperty", out JsonElement nestedPropertyElement)) {
  542. // Console.WriteLine (nestedPropertyElement.GetString ());
  543. //}
  544. decResponse = JsonSerializer.Serialize (
  545. document.RootElement,
  546. new
  547. JsonSerializerOptions
  548. { WriteIndented = true }
  549. );
  550. }
  551. var title = $"{ToCamelCase (name!)} - {new Rune (SelectedCodePoint)} U+{SelectedCodePoint:x5}";
  552. Button? copyGlyph = new () { Text = Strings.charMapCopyGlyph };
  553. Button? copyCodepoint = new () { Text = Strings.charMapCopyCP };
  554. Button? cancel = new () { Text = Strings.btnCancel };
  555. var dlg = new Dialog { Title = title, Buttons = [copyGlyph, copyCodepoint, cancel] };
  556. copyGlyph.Accepting += (s, a) =>
  557. {
  558. CopyGlyph ();
  559. dlg!.RequestStop ();
  560. };
  561. copyCodepoint.Accepting += (s, a) =>
  562. {
  563. CopyCodePoint ();
  564. dlg!.RequestStop ();
  565. };
  566. cancel.Accepting += (s, a) => dlg!.RequestStop ();
  567. var rune = (Rune)SelectedCodePoint;
  568. var label = new Label { Text = "IsAscii: ", X = 0, Y = 0 };
  569. dlg.Add (label);
  570. label = new () { Text = $"{rune.IsAscii}", X = Pos.Right (label), Y = Pos.Top (label) };
  571. dlg.Add (label);
  572. label = new () { Text = ", Bmp: ", X = Pos.Right (label), Y = Pos.Top (label) };
  573. dlg.Add (label);
  574. label = new () { Text = $"{rune.IsBmp}", X = Pos.Right (label), Y = Pos.Top (label) };
  575. dlg.Add (label);
  576. label = new () { Text = ", CombiningMark: ", X = Pos.Right (label), Y = Pos.Top (label) };
  577. dlg.Add (label);
  578. label = new () { Text = $"{rune.IsCombiningMark ()}", X = Pos.Right (label), Y = Pos.Top (label) };
  579. dlg.Add (label);
  580. label = new () { Text = ", SurrogatePair: ", X = Pos.Right (label), Y = Pos.Top (label) };
  581. dlg.Add (label);
  582. label = new () { Text = $"{rune.IsSurrogatePair ()}", X = Pos.Right (label), Y = Pos.Top (label) };
  583. dlg.Add (label);
  584. label = new () { Text = ", Plane: ", X = Pos.Right (label), Y = Pos.Top (label) };
  585. dlg.Add (label);
  586. label = new () { Text = $"{rune.Plane}", X = Pos.Right (label), Y = Pos.Top (label) };
  587. dlg.Add (label);
  588. label = new () { Text = "Columns: ", X = 0, Y = Pos.Bottom (label) };
  589. dlg.Add (label);
  590. label = new () { Text = $"{rune.GetColumns ()}", X = Pos.Right (label), Y = Pos.Top (label) };
  591. dlg.Add (label);
  592. label = new () { Text = ", Utf16SequenceLength: ", X = Pos.Right (label), Y = Pos.Top (label) };
  593. dlg.Add (label);
  594. label = new () { Text = $"{rune.Utf16SequenceLength}", X = Pos.Right (label), Y = Pos.Top (label) };
  595. dlg.Add (label);
  596. label = new ()
  597. {
  598. Text =
  599. $"{Strings.charMapInfoDlgInfoLabel} {UcdApiClient.BaseUrl}codepoint/dec/{SelectedCodePoint}:",
  600. X = 0,
  601. Y = Pos.Bottom (label)
  602. };
  603. dlg.Add (label);
  604. var json = new TextView
  605. {
  606. X = 0,
  607. Y = Pos.Bottom (label),
  608. Width = Dim.Fill (),
  609. Height = Dim.Fill (2),
  610. ReadOnly = true,
  611. Text = decResponse
  612. };
  613. dlg.Add (json);
  614. Application.Run (dlg);
  615. dlg.Dispose ();
  616. }
  617. else
  618. {
  619. MessageBox.ErrorQuery (
  620. Strings.error,
  621. $"{UcdApiClient.BaseUrl}codepoint/dec/{SelectedCodePoint} {Strings.failedGetting}{Environment.NewLine}{new Rune (SelectedCodePoint)} U+{SelectedCodePoint:x5}.",
  622. Strings.btnOk
  623. );
  624. }
  625. // BUGBUG: This is a workaround for some weird ScrollView related mouse grab bug
  626. Application.GrabMouse (this);
  627. }
  628. #endregion Details Dialog
  629. }