TextAlignmentsAndDirection.cs 9.9 KB

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