TextAlignments.cs 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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 (20);
  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, Width = Dim.Fill (), Height = 1, ColorScheme = Colors.Dialog };
  21. multipleLines [(int)alignment] = new Label (txt) { TextAlignment = alignment, Width = Dim.Fill (), 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. Clicked = () => {
  48. edit.Text = unicodeSampleText;
  49. }
  50. };
  51. Win.Add (unicodeSample);
  52. var update = new Button ("_Update", is_default: true) {
  53. X = Pos.Right (edit) + 1,
  54. Y = Pos.Bottom (edit) - 1,
  55. Clicked = () => {
  56. foreach (var alignment in alignments) {
  57. singleLines [(int)alignment].Text = edit.Text;
  58. multipleLines [(int)alignment].Text = edit.Text;
  59. }
  60. }
  61. };
  62. Win.Add (update);
  63. var label = new Label ($"Demonstrating single-line (should clip):") { Y = Pos.Bottom (edit) + 1 };
  64. Win.Add (label);
  65. foreach (var alignment in alignments) {
  66. label = new Label ($"{alignment}:") { Y = Pos.Bottom (label) };
  67. Win.Add (label);
  68. singleLines [(int)alignment].Y = Pos.Bottom (label);
  69. Win.Add (singleLines [(int)alignment]);
  70. label = singleLines [(int)alignment];
  71. }
  72. txt += "\nSecond line\n\nFourth Line.";
  73. label = new Label ($"Demonstrating multi-line and word wrap:") { Y = Pos.Bottom (label) + 1 };
  74. Win.Add (label);
  75. foreach (var alignment in alignments) {
  76. label = new Label ($"{alignment}:") { Y = Pos.Bottom (label) };
  77. Win.Add (label);
  78. multipleLines [(int)alignment].Y = Pos.Bottom (label);
  79. Win.Add (multipleLines [(int)alignment]);
  80. label = multipleLines [(int)alignment];
  81. }
  82. }
  83. }
  84. }