TextFormatterDemo.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using Terminal.Gui;
  5. namespace UICatalog {
  6. [ScenarioMetadata (Name: "TextFormatter Demo", Description: "Demos and tests the TextFormatter class.")]
  7. [ScenarioCategory ("Text")]
  8. [ScenarioCategory ("POC")]
  9. class TextFormatterDemo : Scenario {
  10. public override void Init (Toplevel top, ColorScheme colorScheme)
  11. {
  12. Application.Init ();
  13. Top = top;
  14. if (Top == null) {
  15. Top = Application.Top;
  16. }
  17. Win = null;
  18. }
  19. public override void Setup ()
  20. {
  21. 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" +
  22. "<-- 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.";
  23. Top.TextAlignment = TextAlignment.Centered;
  24. Top.ColorScheme = Colors.Base;
  25. string text = "Hello world, how are you today? Pretty neat!\nSecond line\n\nFourth Line.";
  26. string unicode = "Τὴ γλῶσσα μοῦ ἔδωσαν ἑλληνικὴ\nτὸ σπίτι φτωχικὸ στὶς ἀμμουδιὲς τοῦ Ὁμήρου.\nΜονάχη ἔγνοια ἡ γλῶσσα μου στὶς ἀμμουδιὲς τοῦ Ὁμήρου.";
  27. var unicodeCheckBox = new CheckBox ("Unicode", Top.HotKeySpecifier == (Rune)' ') {
  28. X = 0,
  29. Y = 3,
  30. };
  31. Top.Add (unicodeCheckBox);
  32. var alignments = Enum.GetValues (typeof (Terminal.Gui.TextAlignment)).Cast<Terminal.Gui.TextAlignment> ().ToList ();
  33. var singleLines = new Label [alignments.Count];
  34. var multipleLines = new Label [alignments.Count];
  35. var multiLineHeight = 5;
  36. foreach (var alignment in alignments) {
  37. singleLines [(int)alignment] = new Label (text) { TextAlignment = alignment, X = 0, Width = Dim.Fill (), Height = 1, ColorScheme = Colors.Dialog };
  38. multipleLines [(int)alignment] = new Label (text) { TextAlignment = alignment, X = 0, Width = Dim.Fill (), Height = multiLineHeight, ColorScheme = Colors.Dialog };
  39. }
  40. var label = new Label ($"Demonstrating single-line (should clip):") { Y = Pos.Bottom (unicodeCheckBox) + 1 };
  41. Top.Add (label);
  42. foreach (var alignment in alignments) {
  43. label = new Label ($"{alignment}:") { Y = Pos.Bottom (label) };
  44. Top.Add (label);
  45. singleLines [(int)alignment].Y = Pos.Bottom (label);
  46. Top.Add (singleLines [(int)alignment]);
  47. label = singleLines [(int)alignment];
  48. }
  49. label = new Label ($"Demonstrating multi-line and word wrap:") { Y = Pos.Bottom (label) };
  50. Top.Add (label);
  51. foreach (var alignment in alignments) {
  52. label = new Label ($"{alignment}:") { Y = Pos.Bottom (label) };
  53. Top.Add (label);
  54. multipleLines [(int)alignment].Y = Pos.Bottom (label);
  55. Top.Add (multipleLines [(int)alignment]);
  56. label = multipleLines [(int)alignment];
  57. }
  58. unicodeCheckBox.Toggled += (previous) => {
  59. foreach (var alignment in alignments) {
  60. singleLines [(int)alignment].Text = previous ? text : unicode;
  61. multipleLines [(int)alignment].Text = previous ? text : unicode;
  62. }
  63. };
  64. }
  65. }
  66. }