TextAlignmentsAndDirection.cs 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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 (Name: "Text Alignment and Direction", Description: "Demos horiztonal and vertical text alignment and text direction.")]
  8. [ScenarioCategory ("Text and Formatting")]
  9. public class TextAlignmentsAndDirections : Scenario {
  10. public override void Setup ()
  11. {
  12. // string txt = ".\n...\n.....\nHELLO\n.....\n...\n.";
  13. // string txt = "┌──┴──┐\n┤HELLO├\n└──┬──┘";
  14. string txt = "HELLO WORLD";
  15. var color1 = new ColorScheme { Normal = new Attribute (Color.Black, Color.Gray) };
  16. var color2 = new ColorScheme { Normal = new Attribute (Color.Black, Color.DarkGray) };
  17. var txts = new List<Label> (); // single line
  18. var mtxts = new List<Label> (); // multi line
  19. // Horizontal Single-Line
  20. var labelHL = new Label ("Left") { X = 1, Y = 1, Width = 9, Height = 1, TextAlignment = TextAlignment.Right, ColorScheme = Colors.ColorSchemes ["Dialog"] };
  21. var labelHC = new Label ("Centered") { X = 1, Y = 2, Width = 9, Height = 1, TextAlignment = TextAlignment.Right, ColorScheme = Colors.ColorSchemes ["Dialog"] };
  22. var labelHR = new Label ("Right") { X = 1, Y = 3, Width = 9, Height = 1, TextAlignment = TextAlignment.Right, ColorScheme = Colors.ColorSchemes ["Dialog"] };
  23. var labelHJ = new Label ("Justified") { X = 1, Y = 4, Width = 9, Height = 1, TextAlignment = TextAlignment.Right, ColorScheme = Colors.ColorSchemes ["Dialog"] };
  24. var txtLabelHL = new Label (txt) { X = Pos.Right (labelHL) + 1, Y = Pos.Y (labelHL), Width = Dim.Fill (1) - 9, Height = 1, ColorScheme = color1, TextAlignment = TextAlignment.Left };
  25. var txtLabelHC = new Label (txt) { X = Pos.Right (labelHC) + 1, Y = Pos.Y (labelHC), Width = Dim.Fill (1) - 9, Height = 1, ColorScheme = color2, TextAlignment = TextAlignment.Centered };
  26. var txtLabelHR = new Label (txt) { X = Pos.Right (labelHR) + 1, Y = Pos.Y (labelHR), Width = Dim.Fill (1) - 9, Height = 1, ColorScheme = color1, TextAlignment = TextAlignment.Right };
  27. var txtLabelHJ = new Label (txt) { X = Pos.Right (labelHJ) + 1, Y = Pos.Y (labelHJ), Width = Dim.Fill (1) - 9, Height = 1, ColorScheme = color2, TextAlignment = TextAlignment.Justified };
  28. txts.Add (txtLabelHL); txts.Add (txtLabelHC); txts.Add (txtLabelHR); txts.Add (txtLabelHJ);
  29. Win.Add (labelHL); Win.Add (txtLabelHL);
  30. Win.Add (labelHC); Win.Add (txtLabelHC);
  31. Win.Add (labelHR); Win.Add (txtLabelHR);
  32. Win.Add (labelHJ); Win.Add (txtLabelHJ);
  33. // Vertical Single-Line
  34. var labelVT = new Label ("Top") { X = Pos.AnchorEnd (8), Y = 1, Width = 2, Height = 9, ColorScheme = color1, TextDirection = TextDirection.TopBottom_LeftRight, VerticalTextAlignment = VerticalTextAlignment.Bottom };
  35. var labelVM = new Label ("Middle") { X = Pos.AnchorEnd (6), Y = 1, Width = 2, Height = 9, ColorScheme = color1, TextDirection = TextDirection.TopBottom_LeftRight, VerticalTextAlignment = VerticalTextAlignment.Bottom };
  36. var labelVB = new Label ("Bottom") { X = Pos.AnchorEnd (4), Y = 1, Width = 2, Height = 9, ColorScheme = color1, TextDirection = TextDirection.TopBottom_LeftRight, VerticalTextAlignment = VerticalTextAlignment.Bottom };
  37. var labelVJ = new Label ("Justified") { X = Pos.AnchorEnd (2), Y = 1, Width = 1, Height = 9, ColorScheme = color1, TextDirection = TextDirection.TopBottom_LeftRight, VerticalTextAlignment = VerticalTextAlignment.Bottom };
  38. var txtLabelVT = new Label (txt) { X = Pos.X (labelVT), Y = Pos.Bottom (labelVT) + 1, Width = 1, Height = Dim.Fill (1), ColorScheme = color1, TextDirection = TextDirection.TopBottom_LeftRight, VerticalTextAlignment = VerticalTextAlignment.Top };
  39. var txtLabelVM = new Label (txt) { X = Pos.X (labelVM), Y = Pos.Bottom (labelVM) + 1, Width = 1, Height = Dim.Fill (1), ColorScheme = color2, TextDirection = TextDirection.TopBottom_LeftRight, VerticalTextAlignment = VerticalTextAlignment.Middle };
  40. var txtLabelVB = new Label (txt) { X = Pos.X (labelVB), Y = Pos.Bottom (labelVB) + 1, Width = 1, Height = Dim.Fill (1), ColorScheme = color1, TextDirection = TextDirection.TopBottom_LeftRight, VerticalTextAlignment = VerticalTextAlignment.Bottom };
  41. var txtLabelVJ = new Label (txt) { X = Pos.X (labelVJ), Y = Pos.Bottom (labelVJ) + 1, Width = 1, Height = Dim.Fill (1), ColorScheme = color2, TextDirection = TextDirection.TopBottom_LeftRight, VerticalTextAlignment = VerticalTextAlignment.Justified };
  42. txts.Add (txtLabelVT); txts.Add (txtLabelVM); txts.Add (txtLabelVB); txts.Add (txtLabelVJ);
  43. Win.Add (labelVT); Win.Add (txtLabelVT);
  44. Win.Add (labelVM); Win.Add (txtLabelVM);
  45. Win.Add (labelVB); Win.Add (txtLabelVB);
  46. Win.Add (labelVJ); Win.Add (txtLabelVJ);
  47. // Multi-Line
  48. var container = new View () { X = 0, Y = Pos.Bottom (txtLabelHJ), Width = Dim.Fill (31), Height = Dim.Fill (6), ColorScheme = color2 };
  49. var txtLabelTL = new Label (txt) { X = 1 /* */, Y = 1, Width = Dim.Percent (100f / 3f), Height = Dim.Percent (100f / 3f), TextAlignment = TextAlignment.Left, VerticalTextAlignment = VerticalTextAlignment.Top, ColorScheme = color1 };
  50. var txtLabelTC = new Label (txt) { X = Pos.Right (txtLabelTL) + 2, Y = 1, Width = Dim.Percent (100f / 3f), Height = Dim.Percent (100f / 3f), TextAlignment = TextAlignment.Centered, VerticalTextAlignment = VerticalTextAlignment.Top, ColorScheme = color1 };
  51. var txtLabelTR = new Label (txt) { X = Pos.Right (txtLabelTC) + 2, Y = 1, Width = Dim.Percent (100f, true), Height = Dim.Percent (100f / 3f), TextAlignment = TextAlignment.Right, VerticalTextAlignment = VerticalTextAlignment.Top, ColorScheme = color1 };
  52. var txtLabelML = new Label (txt) { X = Pos.X (txtLabelTL)/* */, Y = Pos.Bottom (txtLabelTL) + 1, Width = Dim.Width (txtLabelTL), Height = Dim.Percent (100f / 3f), TextAlignment = TextAlignment.Left, VerticalTextAlignment = VerticalTextAlignment.Middle, ColorScheme = color1 };
  53. var txtLabelMC = new Label (txt) { X = Pos.X (txtLabelTC)/* */, Y = Pos.Bottom (txtLabelTC) + 1, Width = Dim.Width (txtLabelTC), Height = Dim.Percent (100f / 3f), TextAlignment = TextAlignment.Centered, VerticalTextAlignment = VerticalTextAlignment.Middle, ColorScheme = color1 };
  54. var txtLabelMR = new Label (txt) { X = Pos.X (txtLabelTR)/* */, Y = Pos.Bottom (txtLabelTR) + 1, Width = Dim.Percent (100f, true), Height = Dim.Percent (100f / 3f), TextAlignment = TextAlignment.Right, VerticalTextAlignment = VerticalTextAlignment.Middle, ColorScheme = color1 };
  55. var txtLabelBL = new Label (txt) { X = Pos.X (txtLabelML)/* */, Y = Pos.Bottom (txtLabelML) + 1, Width = Dim.Width (txtLabelML), Height = Dim.Percent (100f, true), TextAlignment = TextAlignment.Left, VerticalTextAlignment = VerticalTextAlignment.Bottom, ColorScheme = color1 };
  56. var txtLabelBC = new Label (txt) { X = Pos.X (txtLabelMC)/* */, Y = Pos.Bottom (txtLabelMC) + 1, Width = Dim.Width (txtLabelMC), Height = Dim.Percent (100f, true), TextAlignment = TextAlignment.Centered, VerticalTextAlignment = VerticalTextAlignment.Bottom, ColorScheme = color1 };
  57. var txtLabelBR = new Label (txt) { X = Pos.X (txtLabelMR)/* */, Y = Pos.Bottom (txtLabelMR) + 1, Width = Dim.Percent (100f, true), Height = Dim.Percent (100f, true), TextAlignment = TextAlignment.Right, VerticalTextAlignment = VerticalTextAlignment.Bottom, ColorScheme = color1 };
  58. mtxts.Add (txtLabelTL); mtxts.Add (txtLabelTC); mtxts.Add (txtLabelTR);
  59. mtxts.Add (txtLabelML); mtxts.Add (txtLabelMC); mtxts.Add (txtLabelMR);
  60. mtxts.Add (txtLabelBL); mtxts.Add (txtLabelBC); mtxts.Add (txtLabelBR);
  61. // Save Alignments in Data
  62. foreach (var t in mtxts) {
  63. t.Data = new { h = t.TextAlignment, v = t.VerticalTextAlignment };
  64. }
  65. container.Add (txtLabelTL);
  66. container.Add (txtLabelTC);
  67. container.Add (txtLabelTR);
  68. container.Add (txtLabelML);
  69. container.Add (txtLabelMC);
  70. container.Add (txtLabelMR);
  71. container.Add (txtLabelBL);
  72. container.Add (txtLabelBC);
  73. container.Add (txtLabelBR);
  74. Win.Add (container);
  75. // Edit Text
  76. var editText = new TextView () {
  77. X = 1,
  78. Y = Pos.Bottom (container) + 1,
  79. Width = Dim.Fill (10),
  80. Height = Dim.Fill (1),
  81. ColorScheme = Colors.TopLevel,
  82. Text = txt
  83. };
  84. editText.MouseClick += (s, m) => {
  85. foreach (var v in txts) {
  86. v.Text = editText.Text;
  87. }
  88. foreach (var v in mtxts) {
  89. v.Text = editText.Text;
  90. }
  91. };
  92. Win.KeyUp += (s, m) => {
  93. foreach (var v in txts) {
  94. v.Text = editText.Text;
  95. }
  96. foreach (var v in mtxts) {
  97. v.Text = editText.Text;
  98. }
  99. };
  100. editText.SetFocus ();
  101. Win.Add (editText);
  102. // JUSTIFY CHECKBOX
  103. var justifyCheckbox = new CheckBox ("Justify") {
  104. X = Pos.Right (container) + 1,
  105. Y = Pos.Y (container) + 1,
  106. Width = Dim.Fill (10),
  107. Height = 1
  108. };
  109. justifyCheckbox.Toggled += (s,e) => {
  110. if (e.OldValue == true) {
  111. foreach (var t in mtxts) {
  112. t.TextAlignment = (TextAlignment)((dynamic)t.Data).h;
  113. t.VerticalTextAlignment = (VerticalTextAlignment)((dynamic)t.Data).v;
  114. }
  115. } else {
  116. foreach (var t in mtxts) {
  117. if (TextFormatter.IsVerticalDirection (t.TextDirection)) {
  118. t.VerticalTextAlignment = VerticalTextAlignment.Justified;
  119. t.TextAlignment = ((dynamic)t.Data).h;
  120. } else {
  121. t.TextAlignment = TextAlignment.Justified;
  122. t.VerticalTextAlignment = ((dynamic)t.Data).v;
  123. }
  124. }
  125. }
  126. };
  127. Win.Add (justifyCheckbox);
  128. // Direction Options
  129. var directionsEnum = Enum.GetValues (typeof (Terminal.Gui.TextDirection)).Cast<Terminal.Gui.TextDirection> ().ToList ();
  130. var directionOptions = new RadioGroup (directionsEnum.Select (e => e.ToString ()).ToArray ()) {
  131. X = Pos.Right (container) + 1,
  132. Y = Pos.Bottom (justifyCheckbox) + 1,
  133. Width = Dim.Fill (10),
  134. Height = Dim.Fill (1),
  135. HotKeySpecifier = (Rune)'\xffff'
  136. };
  137. directionOptions.SelectedItemChanged += (s, ev) => {
  138. foreach (var v in mtxts) {
  139. v.TextDirection = (TextDirection)ev.SelectedItem;
  140. }
  141. };
  142. Win.Add (directionOptions);
  143. }
  144. }
  145. }