TextAlignments.cs 3.7 KB

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