TextFormatterDemo.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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 Init (Toplevel top, ColorScheme colorScheme)
  13. //{
  14. // Application.Init ();
  15. // Top = top;
  16. // if (Top == null) {
  17. // Top = Application.Top;
  18. // }
  19. // Win = null;
  20. //}
  21. public override void Setup ()
  22. {
  23. //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" +
  24. // "<-- 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.";
  25. //Top.TextAlignment = TextAlignment.Centered;
  26. //Top.ColorScheme = Colors.Base;
  27. // Make Win smaller so sizing the window horizontally will make the
  28. // labels shrink to zero-width
  29. Win.X = 10;
  30. Win.Width = Dim.Fill (10);
  31. string text = "Hello world, how are you today? Pretty neat!\nSecond line\n\nFourth Line.";
  32. string unicode = "Τὴ γλῶσσα μοῦ ἔδωσαν ἑλληνικὴ\nτὸ σπίτι φτωχικὸ στὶς ἀμμουδιὲς τοῦ Ὁμήρου.\nΜονάχη ἔγνοια ἡ γλῶσσα μου στὶς ἀμμουδιὲς τοῦ Ὁμήρου.";
  33. Label blockText = new Label () { ColorScheme = Colors.TopLevel, X = 0, Y = 3, Height = 7, Width = Dim.Fill (0) };
  34. var block = new StringBuilder ();
  35. block.AppendLine (" _/ ");
  36. block.AppendLine (" _/_/_/ _/ _/ _/_/_/ _/_/_/");
  37. block.AppendLine (" _/ _/ _/ _/ _/ _/ _/_/ ");
  38. block.AppendLine (" _/ _/ _/ _/ _/ _/ _/_/ ");
  39. block.AppendLine (" _/_/_/ _/_/_/ _/ _/ _/_/_/ _/_/_/ ");
  40. block.AppendLine (" _/ ");
  41. block.AppendLine ("_ /_/ ");
  42. blockText.Text = block.ToString ();
  43. Win.Add (blockText);
  44. var unicodeCheckBox = new CheckBox ("Unicode", Top.HotKeySpecifier == (Rune)' ') {
  45. X = 0,
  46. Y = Pos.Bottom (blockText) + 1,
  47. };
  48. Win.Add (unicodeCheckBox);
  49. var alignments = Enum.GetValues (typeof (Terminal.Gui.TextAlignment)).Cast<Terminal.Gui.TextAlignment> ().ToList ();
  50. var singleLines = new Label [alignments.Count];
  51. var multipleLines = new Label [alignments.Count];
  52. var multiLineHeight = 5;
  53. foreach (var alignment in alignments) {
  54. singleLines [(int)alignment] = new Label (text) { TextAlignment = alignment, X = 0, Width = Dim.Fill (), Height = 1, ColorScheme = Colors.Dialog };
  55. multipleLines [(int)alignment] = new Label (text) { TextAlignment = alignment, X = 0, Width = Dim.Fill (), Height = multiLineHeight, ColorScheme = Colors.Dialog };
  56. }
  57. var label = new Label ($"Demonstrating single-line (should clip):") { Y = Pos.Bottom (unicodeCheckBox) + 1 };
  58. Win.Add (label);
  59. foreach (var alignment in alignments) {
  60. label = new Label ($"{alignment}:") { Y = Pos.Bottom (label) };
  61. Win.Add (label);
  62. singleLines [(int)alignment].Y = Pos.Bottom (label);
  63. Win.Add (singleLines [(int)alignment]);
  64. label = singleLines [(int)alignment];
  65. }
  66. label = new Label ($"Demonstrating multi-line and word wrap:") { Y = Pos.Bottom (label) };
  67. Win.Add (label);
  68. foreach (var alignment in alignments) {
  69. label = new Label ($"{alignment}:") { Y = Pos.Bottom (label) };
  70. Win.Add (label);
  71. multipleLines [(int)alignment].Y = Pos.Bottom (label);
  72. Win.Add (multipleLines [(int)alignment]);
  73. label = multipleLines [(int)alignment];
  74. }
  75. unicodeCheckBox.Toggled += (previous) => {
  76. foreach (var alignment in alignments) {
  77. singleLines [(int)alignment].Text = previous ? text : unicode;
  78. multipleLines [(int)alignment].Text = previous ? text : unicode;
  79. }
  80. };
  81. }
  82. }
  83. }