AutoSizeAndDirectionText.cs 2.7 KB

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