TextAlignments.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using Terminal.Gui;
  6. namespace UICatalog.Scenarios;
  7. [ScenarioMetadata ("Simple Text Alignment", "Demonstrates horizontal text alignment")]
  8. [ScenarioCategory ("Text and Formatting")]
  9. public class TextAlignments : Scenario
  10. {
  11. public override void Setup ()
  12. {
  13. Win.X = 10;
  14. Win.Width = Dim.Fill (10);
  15. var txt = "Hello world, how are you today? Pretty neat!";
  16. var unicodeSampleText = "A Unicode sentence (пÑРвеÑ) has words.";
  17. List<Justification> alignments = Enum.GetValues (typeof (Justification)).Cast<Justification> ().ToList ();
  18. Label [] singleLines = new Label [alignments.Count];
  19. Label [] multipleLines = new Label [alignments.Count];
  20. var multiLineHeight = 5;
  21. foreach (Justification alignment in alignments)
  22. {
  23. singleLines [(int)alignment] = new()
  24. {
  25. Justification = alignment,
  26. X = 1,
  27. Width = Dim.Fill (1),
  28. Height = 1,
  29. ColorScheme = Colors.ColorSchemes ["Dialog"],
  30. Text = txt
  31. };
  32. multipleLines [(int)alignment] = new()
  33. {
  34. Justification = alignment,
  35. X = 1,
  36. Width = Dim.Fill (1),
  37. Height = multiLineHeight,
  38. ColorScheme = Colors.ColorSchemes ["Dialog"],
  39. Text = txt
  40. };
  41. }
  42. // Add a label & text field so we can demo IsDefault
  43. var editLabel = new Label { X = 0, Y = 0, Text = "Text:" };
  44. Win.Add (editLabel);
  45. var edit = new TextView
  46. {
  47. X = Pos.Right (editLabel) + 1,
  48. Y = Pos.Y (editLabel),
  49. Width = Dim.Fill ("Text:".Length + " Unicode Sample".Length + 2),
  50. Height = 4,
  51. ColorScheme = Colors.ColorSchemes ["TopLevel"],
  52. Text = txt
  53. };
  54. edit.TextChanged += (s, e) =>
  55. {
  56. foreach (Justification alignment in alignments)
  57. {
  58. singleLines [(int)alignment].Text = edit.Text;
  59. multipleLines [(int)alignment].Text = edit.Text;
  60. }
  61. };
  62. Win.Add (edit);
  63. var unicodeSample = new Button { X = Pos.Right (edit) + 1, Y = 0, Text = "Unicode Sample" };
  64. unicodeSample.Accept += (s, e) => { edit.Text = unicodeSampleText; };
  65. Win.Add (unicodeSample);
  66. var update = new Button { X = Pos.Right (edit) + 1, Y = Pos.Bottom (edit) - 1, Text = "_Update" };
  67. update.Accept += (s, e) =>
  68. {
  69. foreach (Justification alignment in alignments)
  70. {
  71. singleLines [(int)alignment].Text = edit.Text;
  72. multipleLines [(int)alignment].Text = edit.Text;
  73. }
  74. };
  75. Win.Add (update);
  76. var enableHotKeyCheckBox = new CheckBox
  77. {
  78. X = 0, Y = Pos.Bottom (edit), Text = "Enable Hotkey (_)", Checked = false
  79. };
  80. Win.Add (enableHotKeyCheckBox);
  81. var label = new Label
  82. {
  83. Y = Pos.Bottom (enableHotKeyCheckBox) + 1, Text = "Demonstrating single-line (should clip):"
  84. };
  85. Win.Add (label);
  86. foreach (Justification alignment in alignments)
  87. {
  88. label = new() { Y = Pos.Bottom (label), Text = $"{alignment}:" };
  89. Win.Add (label);
  90. singleLines [(int)alignment].Y = Pos.Bottom (label);
  91. Win.Add (singleLines [(int)alignment]);
  92. label = singleLines [(int)alignment];
  93. }
  94. txt += "\nSecond line\n\nFourth Line.";
  95. label = new() { Y = Pos.Bottom (label), Text = "Demonstrating multi-line and word wrap:" };
  96. Win.Add (label);
  97. foreach (Justification alignment in alignments)
  98. {
  99. label = new() { Y = Pos.Bottom (label), Text = $"{alignment}:" };
  100. Win.Add (label);
  101. multipleLines [(int)alignment].Y = Pos.Bottom (label);
  102. Win.Add (multipleLines [(int)alignment]);
  103. label = multipleLines [(int)alignment];
  104. }
  105. enableHotKeyCheckBox.Toggled += (s, e) =>
  106. {
  107. foreach (Justification alignment in alignments)
  108. {
  109. singleLines [(int)alignment].HotKeySpecifier =
  110. e.OldValue == true ? (Rune)0xffff : (Rune)'_';
  111. multipleLines [(int)alignment].HotKeySpecifier =
  112. e.OldValue == true ? (Rune)0xffff : (Rune)'_';
  113. }
  114. Win.SetNeedsDisplay ();
  115. Win.LayoutSubviews ();
  116. };
  117. }
  118. }