TextAlignments.cs 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. string txt = "Hello world, how are you today? Pretty neat!";
  12. string unicodeSampleText = "A Unicode sentence (пÑивеÑ) has words.";
  13. var alignments = Enum.GetValues (typeof (Terminal.Gui.TextAlignment)).Cast<Terminal.Gui.TextAlignment> ().ToList ();
  14. var singleLines = new Label [alignments.Count];
  15. var multipleLines = new Label [alignments.Count];
  16. var multiLineHeight = 5;
  17. foreach (var alignment in alignments) {
  18. singleLines[(int)alignment] = new Label (txt) { TextAlignment = alignment, Width = Dim.Fill (), Height = 1, ColorScheme = Colors.Dialog };
  19. multipleLines [(int)alignment] = new Label (txt) { TextAlignment = alignment, Width = Dim.Fill (), Height = multiLineHeight, ColorScheme = Colors.Dialog };
  20. }
  21. // Add a label & text field so we can demo IsDefault
  22. var editLabel = new Label ("Text:") {
  23. X = 0,
  24. Y = 0,
  25. };
  26. Win.Add (editLabel);
  27. var edit = new TextView () {
  28. X = Pos.Right (editLabel) + 1,
  29. Y = Pos.Y (editLabel),
  30. Width = Dim.Fill("Text:".Length + " Unicode Sample".Length + 2),
  31. Height = 4,
  32. ColorScheme = Colors.TopLevel,
  33. Text = txt,
  34. };
  35. edit.TextChanged = () => {
  36. foreach (var alignment in alignments) {
  37. singleLines [(int)alignment].Text = edit.Text;
  38. multipleLines [(int)alignment].Text = edit.Text;
  39. }
  40. };
  41. Win.Add (edit);
  42. var unicodeSample = new Button ("Unicode Sample") {
  43. X = Pos.Right (edit) + 1,
  44. Y = 0,
  45. Clicked = () => {
  46. edit.Text = unicodeSampleText;
  47. }
  48. };
  49. Win.Add (unicodeSample);
  50. var update = new Button ("_Update", is_default: true) {
  51. X = Pos.Right (edit) + 1,
  52. Y = Pos.Bottom (edit) - 1,
  53. Clicked = () => {
  54. foreach (var alignment in alignments) {
  55. singleLines [(int)alignment].Text = edit.Text;
  56. multipleLines [(int)alignment].Text = edit.Text;
  57. }
  58. }
  59. };
  60. Win.Add (update);
  61. var label = new Label ($"Demonstrating single-line (should clip):") { Y = Pos.Bottom (edit) + 1 };
  62. Win.Add (label);
  63. foreach (var alignment in alignments) {
  64. label = new Label ($"{alignment}:") { Y = Pos.Bottom (label) };
  65. Win.Add (label);
  66. singleLines [(int)alignment].Y = Pos.Bottom (label);
  67. Win.Add (singleLines [(int)alignment]);
  68. label = singleLines [(int)alignment];
  69. }
  70. txt += "\nSecond line\n\nFourth Line.";
  71. label = new Label ($"Demonstrating multi-line and word wrap:") { Y = Pos.Bottom (label) + 1 };
  72. Win.Add (label);
  73. foreach (var alignment in alignments) {
  74. label = new Label ($"{alignment}:") { Y = Pos.Bottom (label) };
  75. Win.Add (label);
  76. multipleLines [(int)alignment].Y = Pos.Bottom (label);
  77. Win.Add (multipleLines [(int)alignment]);
  78. label = multipleLines [(int)alignment];
  79. }
  80. }
  81. }
  82. }