AutoSizeAndDirectionText.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. using Terminal.Gui;
  2. namespace UICatalog.Scenarios;
  3. [ScenarioMetadata ("Text Direction and AutoSize", "Demos TextFormatter Direction and View AutoSize.")]
  4. [ScenarioCategory ("Text and Formatting")]
  5. public class AutoSizeAndDirectionText : Scenario
  6. {
  7. public override void Setup ()
  8. {
  9. var text = "Hello World";
  10. var wideText = "Hello World 你";
  11. ColorScheme color = Colors.ColorSchemes ["Dialog"];
  12. var labelH = new Label
  13. {
  14. X = 1,
  15. Y = 1,
  16. // Width = 11,
  17. // Height = 1,
  18. ColorScheme = color,
  19. Text = text,
  20. TextDirection = TextDirection.LeftRight_TopBottom
  21. };
  22. Win.Add (labelH);
  23. var labelV = new Label
  24. {
  25. X = 70,
  26. Y = 1,
  27. // Width = 1,
  28. // Height = 11,
  29. ColorScheme = color,
  30. Text = text,
  31. TextDirection = TextDirection.TopBottom_LeftRight
  32. };
  33. Win.Add (labelV);
  34. var editText = new TextView
  35. {
  36. X = Pos.Center (),
  37. Y = Pos.Center (),
  38. Width = 20,
  39. Height = 5,
  40. Text = text
  41. };
  42. editText.SetFocus ();
  43. Win.Add (editText);
  44. var ckbDirection = new CheckBox { Text = "Toggle Direction", X = Pos.Center (), Y = Pos.Center () + 3 };
  45. ckbDirection.Toggled += (s, e) =>
  46. {
  47. if (labelH.TextDirection == TextDirection.LeftRight_TopBottom)
  48. {
  49. labelH.TextDirection = TextDirection.TopBottom_LeftRight;
  50. labelV.TextDirection = TextDirection.LeftRight_TopBottom;
  51. }
  52. else
  53. {
  54. labelH.TextDirection = TextDirection.LeftRight_TopBottom;
  55. labelV.TextDirection = TextDirection.TopBottom_LeftRight;
  56. }
  57. };
  58. Win.Add (ckbDirection);
  59. var ckbAutoSize = new CheckBox
  60. {
  61. Text = "Auto Size", X = Pos.Center (), Y = Pos.Center () + 5, Checked = labelH.AutoSize = labelV.AutoSize
  62. };
  63. ckbAutoSize.Toggled += (s, e) => labelH.AutoSize = labelV.AutoSize = (bool)ckbAutoSize.Checked;
  64. Win.Add (ckbAutoSize);
  65. var ckbPreserveTrailingSpaces = new CheckBox
  66. {
  67. Text = "Preserve Trailing Spaces",
  68. X = Pos.Center (),
  69. Y = Pos.Center () + 7,
  70. Checked = labelH.PreserveTrailingSpaces =
  71. labelV.PreserveTrailingSpaces
  72. };
  73. ckbPreserveTrailingSpaces.Toggled += (s, e) =>
  74. labelH.PreserveTrailingSpaces = labelV.PreserveTrailingSpaces = (bool)ckbPreserveTrailingSpaces.Checked;
  75. Win.Add (ckbPreserveTrailingSpaces);
  76. var ckbWideText = new CheckBox { Text = "Use wide runes", X = Pos.Center (), Y = Pos.Center () + 9 };
  77. ckbWideText.Toggled += (s, e) =>
  78. {
  79. if (ckbWideText.Checked == true)
  80. {
  81. labelH.Text = labelV.Text = editText.Text = wideText;
  82. labelH.Width = 14;
  83. labelV.Height = 13;
  84. }
  85. else
  86. {
  87. labelH.Text = labelV.Text = editText.Text = text;
  88. labelH.Width = 11;
  89. labelV.Width = 1;
  90. labelV.Height = 11;
  91. }
  92. };
  93. Win.Add (ckbWideText);
  94. Win.KeyUp += (s, e) =>
  95. labelH.Text = labelV.Text = text = editText.Text;
  96. }
  97. }