TextAlignments.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using Terminal.Gui;
  5. namespace UICatalog {
  6. [ScenarioMetadata (Name: "Text Alignment", Description: "Demonstrates text alignment")]
  7. [ScenarioCategory ("Text")]
  8. class TextAlignments : Scenario {
  9. public override void Setup ()
  10. {
  11. Win.X = 10;
  12. Win.Width = Dim.Fill (10);
  13. string txt = "Hello world, how are you today? Pretty neat!";
  14. string unicodeSampleText = "A Unicode sentence (пÑРвеÑ) has words.";
  15. var alignments = Enum.GetValues (typeof (Terminal.Gui.TextAlignment)).Cast<Terminal.Gui.TextAlignment> ().ToList ();
  16. var singleLines = new Label [alignments.Count];
  17. var multipleLines = new Label [alignments.Count];
  18. var multiLineHeight = 5;
  19. foreach (var alignment in alignments) {
  20. singleLines [(int)alignment] = new Label (txt) { TextAlignment = alignment, X = 1, Width = Dim.Fill (1), Height = 1, ColorScheme = Colors.Dialog };
  21. multipleLines [(int)alignment] = new Label (txt) { TextAlignment = alignment, X = 1, Width = Dim.Fill (1), Height = multiLineHeight, ColorScheme = Colors.Dialog };
  22. }
  23. // Add a label & text field so we can demo IsDefault
  24. var editLabel = new Label ("Text:") {
  25. X = 0,
  26. Y = 0,
  27. };
  28. Win.Add (editLabel);
  29. var edit = new TextView () {
  30. X = Pos.Right (editLabel) + 1,
  31. Y = Pos.Y (editLabel),
  32. Width = Dim.Fill ("Text:".Length + " Unicode Sample".Length + 2),
  33. Height = 4,
  34. ColorScheme = Colors.TopLevel,
  35. Text = txt,
  36. };
  37. edit.TextChanged += () => {
  38. foreach (var alignment in alignments) {
  39. singleLines [(int)alignment].Text = edit.Text;
  40. multipleLines [(int)alignment].Text = edit.Text;
  41. }
  42. };
  43. Win.Add (edit);
  44. var unicodeSample = new Button ("Unicode Sample") {
  45. X = Pos.Right (edit) + 1,
  46. Y = 0,
  47. };
  48. unicodeSample.Clicked += () => {
  49. edit.Text = unicodeSampleText;
  50. };
  51. Win.Add (unicodeSample);
  52. var update = new Button ("_Update") {
  53. X = Pos.Right (edit) + 1,
  54. Y = Pos.Bottom (edit) - 1,
  55. };
  56. update.Clicked += () => {
  57. foreach (var alignment in alignments) {
  58. singleLines [(int) alignment].Text = edit.Text;
  59. multipleLines [(int) alignment].Text = edit.Text;
  60. }
  61. };
  62. Win.Add (update);
  63. var enableHotKeyCheckBox = new CheckBox ("Enable Hotkey (_)", false) {
  64. X = 0,
  65. Y = Pos.Bottom (edit),
  66. };
  67. Win.Add (enableHotKeyCheckBox);
  68. var label = new Label ($"Demonstrating single-line (should clip):") { Y = Pos.Bottom (enableHotKeyCheckBox) + 1 };
  69. Win.Add (label);
  70. foreach (var alignment in alignments) {
  71. label = new Label ($"{alignment}:") { Y = Pos.Bottom (label) };
  72. Win.Add (label);
  73. singleLines [(int)alignment].Y = Pos.Bottom (label);
  74. Win.Add (singleLines [(int)alignment]);
  75. label = singleLines [(int)alignment];
  76. }
  77. txt += "\nSecond line\n\nFourth Line.";
  78. label = new Label ($"Demonstrating multi-line and word wrap:") { Y = Pos.Bottom (label) };
  79. Win.Add (label);
  80. foreach (var alignment in alignments) {
  81. label = new Label ($"{alignment}:") { Y = Pos.Bottom (label) };
  82. Win.Add (label);
  83. multipleLines [(int)alignment].Y = Pos.Bottom (label);
  84. Win.Add (multipleLines [(int)alignment]);
  85. label = multipleLines [(int)alignment];
  86. }
  87. enableHotKeyCheckBox.Toggled += (previous) => {
  88. foreach (var alignment in alignments) {
  89. singleLines [(int)alignment].HotKeySpecifier = previous ? (Rune)0xffff : (Rune)'_';
  90. multipleLines [(int)alignment].HotKeySpecifier = previous ? (Rune)0xffff : (Rune)'_';
  91. }
  92. Win.SetNeedsDisplay ();
  93. Win.LayoutSubviews ();
  94. };
  95. }
  96. }
  97. }