TextFormatterDemo.cs 3.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using Terminal.Gui;
  6. using Rune = System.Rune;
  7. namespace UICatalog {
  8. [ScenarioMetadata (Name: "TextFormatter Demo", Description: "Demos and tests the TextFormatter class.")]
  9. [ScenarioCategory ("Text")]
  10. [ScenarioCategory ("POC")]
  11. class TextFormatterDemo : Scenario {
  12. public override void Setup ()
  13. {
  14. // TODO: Move this to another Scenario that specifically tests `Views` that have no subviews.
  15. //Top.Text = "Press CTRL-Q to Quit. This is the Text for the TopLevel View. TextAlignment.Centered was specified. It is intentionally very long to illustrate word wrap.\n" +
  16. // "<-- There is a new line here to show a hard line break. You should see this text bleed underneath the subviews, which start at Y = 3.";
  17. //Top.TextAlignment = TextAlignment.Centered;
  18. //Top.ColorScheme = Colors.Base;
  19. // Make Win smaller so sizing the window horizontally will make the
  20. // labels shrink to zero-width
  21. Win.X = 10;
  22. Win.Width = Dim.Fill (10);
  23. string text = "Hello world, how are you today? Pretty neat!\nSecond line\n\nFourth Line.";
  24. string unicode = "Τὴ γλῶσσα μοῦ ἔδωσαν ἑλληνικὴ\nτὸ σπίτι φτωχικὸ στὶς ἀμμουδιὲς τοῦ Ὁμήρου.\nΜονάχη ἔγνοια ἡ γλῶσσα μου στὶς ἀμμουδιὲς τοῦ Ὁμήρου.";
  25. Label blockText = new Label () { ColorScheme = Colors.TopLevel, X = 0, Y = 3, Height = 7, Width = Dim.Fill (0) };
  26. var block = new StringBuilder ();
  27. block.AppendLine (" _/ ");
  28. block.AppendLine (" _/_/_/ _/ _/ _/_/_/ _/_/_/");
  29. block.AppendLine (" _/ _/ _/ _/ _/ _/ _/_/ ");
  30. block.AppendLine (" _/ _/ _/ _/ _/ _/ _/_/ ");
  31. block.AppendLine (" _/_/_/ _/_/_/ _/ _/ _/_/_/ _/_/_/ ");
  32. block.AppendLine (" _/ ");
  33. block.AppendLine ("_ /_/ ");
  34. blockText.Text = block.ToString ();
  35. Win.Add (blockText);
  36. var unicodeCheckBox = new CheckBox ("Unicode", Top.HotKeySpecifier == (Rune)' ') {
  37. X = 0,
  38. Y = Pos.Bottom (blockText) + 1,
  39. };
  40. Win.Add (unicodeCheckBox);
  41. var alignments = Enum.GetValues (typeof (Terminal.Gui.TextAlignment)).Cast<Terminal.Gui.TextAlignment> ().ToList ();
  42. var singleLines = new Label [alignments.Count];
  43. var multipleLines = new Label [alignments.Count];
  44. var multiLineHeight = 5;
  45. foreach (var alignment in alignments) {
  46. singleLines [(int)alignment] = new Label (text) { TextAlignment = alignment, X = 0, Width = Dim.Fill (), Height = 1, ColorScheme = Colors.Dialog };
  47. multipleLines [(int)alignment] = new Label (text) { TextAlignment = alignment, X = 0, Width = Dim.Fill (), Height = multiLineHeight, ColorScheme = Colors.Dialog };
  48. }
  49. var label = new Label ($"Demonstrating single-line (should clip):") { Y = Pos.Bottom (unicodeCheckBox) + 1 };
  50. Win.Add (label);
  51. foreach (var alignment in alignments) {
  52. label = new Label ($"{alignment}:") { Y = Pos.Bottom (label) };
  53. Win.Add (label);
  54. singleLines [(int)alignment].Y = Pos.Bottom (label);
  55. Win.Add (singleLines [(int)alignment]);
  56. label = singleLines [(int)alignment];
  57. }
  58. label = new Label ($"Demonstrating multi-line and word wrap:") { Y = Pos.Bottom (label) };
  59. Win.Add (label);
  60. foreach (var alignment in alignments) {
  61. label = new Label ($"{alignment}:") { Y = Pos.Bottom (label) };
  62. Win.Add (label);
  63. multipleLines [(int)alignment].Y = Pos.Bottom (label);
  64. Win.Add (multipleLines [(int)alignment]);
  65. label = multipleLines [(int)alignment];
  66. }
  67. unicodeCheckBox.Toggled += (previous) => {
  68. foreach (var alignment in alignments) {
  69. singleLines [(int)alignment].Text = previous ? text : unicode;
  70. multipleLines [(int)alignment].Text = previous ? text : unicode;
  71. }
  72. };
  73. }
  74. }
  75. }