AutoSizeAndDirectionText.cs 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. Text = text
  33. };
  34. editText.SetFocus ();
  35. Win.Add (editText);
  36. var ckbDirection = new CheckBox ("Toggle Direction") {
  37. X = Pos.Center (),
  38. Y = Pos.Center () + 3
  39. };
  40. ckbDirection.Toggled += (s,e) => {
  41. if (labelH.TextDirection == TextDirection.LeftRight_TopBottom) {
  42. labelH.TextDirection = TextDirection.TopBottom_LeftRight;
  43. labelV.TextDirection = TextDirection.LeftRight_TopBottom;
  44. } else {
  45. labelH.TextDirection = TextDirection.LeftRight_TopBottom;
  46. labelV.TextDirection = TextDirection.TopBottom_LeftRight;
  47. }
  48. };
  49. Win.Add (ckbDirection);
  50. var ckbAutoSize = new CheckBox ("Auto Size") {
  51. X = Pos.Center (),
  52. Y = Pos.Center () + 5,
  53. Checked = labelH.AutoSize = labelV.AutoSize
  54. };
  55. ckbAutoSize.Toggled += (s,e) => labelH.AutoSize = labelV.AutoSize = (bool)ckbAutoSize.Checked;
  56. Win.Add (ckbAutoSize);
  57. var ckbPreserveTrailingSpaces = new CheckBox ("Preserve Trailing Spaces") {
  58. X = Pos.Center (),
  59. Y = Pos.Center () + 7,
  60. Checked = labelH.PreserveTrailingSpaces = labelV.PreserveTrailingSpaces
  61. };
  62. ckbPreserveTrailingSpaces.Toggled += (s, e) =>
  63. labelH.PreserveTrailingSpaces = labelV.PreserveTrailingSpaces = (bool)ckbPreserveTrailingSpaces.Checked;
  64. Win.Add (ckbPreserveTrailingSpaces);
  65. var ckbWideText = new CheckBox ("Use wide runes") {
  66. X = Pos.Center (),
  67. Y = Pos.Center () + 9
  68. };
  69. ckbWideText.Toggled += (s, e) => {
  70. if (ckbWideText.Checked == true) {
  71. labelH.Text = labelV.Text = editText.Text = wideText;
  72. labelH.Width = 14;
  73. labelV.Height = 13;
  74. } else {
  75. labelH.Text = labelV.Text = editText.Text = text;
  76. labelH.Width = 11;
  77. labelV.Width = 1;
  78. labelV.Height = 11;
  79. }
  80. };
  81. Win.Add (ckbWideText);
  82. Win.KeyUp += (s,e) =>
  83. labelH.Text = labelV.Text = text = editText.Text.ToString ();
  84. }
  85. }
  86. }