TextViewAutocompletePopup.cs 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. using System.Linq;
  2. using System.Text.RegularExpressions;
  3. using Terminal.Gui;
  4. namespace UICatalog.Scenarios;
  5. [ScenarioMetadata ("TextView Autocomplete Popup", "Shows five TextView Autocomplete Popup effects")]
  6. [ScenarioCategory ("TextView")]
  7. [ScenarioCategory ("Controls")]
  8. [ScenarioCategory ("Mouse and Keyboard")]
  9. public class TextViewAutocompletePopup : Scenario
  10. {
  11. private int _height = 10;
  12. private MenuItem _miMultiline;
  13. private MenuItem _miWrap;
  14. private StatusItem _siMultiline;
  15. private StatusItem _siWrap;
  16. private TextView _textViewBottomLeft;
  17. private TextView _textViewBottomRight;
  18. private TextView _textViewCentered;
  19. private TextView _textViewTopLeft;
  20. private TextView _textViewTopRight;
  21. public override void Setup ()
  22. {
  23. Win.Title = GetName ();
  24. var width = 20;
  25. var text = " jamp jemp jimp jomp jump";
  26. var menu = new MenuBar
  27. {
  28. Menus =
  29. [
  30. new MenuBarItem (
  31. "_File",
  32. new []
  33. {
  34. _miMultiline =
  35. new MenuItem (
  36. "_Multiline",
  37. "",
  38. () => Multiline ()
  39. ) { CheckType = MenuItemCheckStyle.Checked },
  40. _miWrap = new MenuItem (
  41. "_Word Wrap",
  42. "",
  43. () => WordWrap ()
  44. ) { CheckType = MenuItemCheckStyle.Checked },
  45. new ("_Quit", "", () => Quit ())
  46. }
  47. )
  48. ]
  49. };
  50. Top.Add (menu);
  51. _textViewTopLeft = new TextView { Width = width, Height = _height, Text = text };
  52. _textViewTopLeft.DrawContent += TextViewTopLeft_DrawContent;
  53. Win.Add (_textViewTopLeft);
  54. _textViewTopRight = new TextView { X = Pos.AnchorEnd (width), Width = width, Height = _height, Text = text };
  55. _textViewTopRight.DrawContent += TextViewTopRight_DrawContent;
  56. Win.Add (_textViewTopRight);
  57. _textViewBottomLeft = new TextView
  58. {
  59. Y = Pos.AnchorEnd (_height), Width = width, Height = _height, Text = text
  60. };
  61. _textViewBottomLeft.DrawContent += TextViewBottomLeft_DrawContent;
  62. Win.Add (_textViewBottomLeft);
  63. _textViewBottomRight = new TextView
  64. {
  65. X = Pos.AnchorEnd (width),
  66. Y = Pos.AnchorEnd (_height),
  67. Width = width,
  68. Height = _height,
  69. Text = text
  70. };
  71. _textViewBottomRight.DrawContent += TextViewBottomRight_DrawContent;
  72. Win.Add (_textViewBottomRight);
  73. _textViewCentered = new TextView
  74. {
  75. X = Pos.Center (),
  76. Y = Pos.Center (),
  77. Width = width,
  78. Height = _height,
  79. Text = text
  80. };
  81. _textViewCentered.DrawContent += TextViewCentered_DrawContent;
  82. Win.Add (_textViewCentered);
  83. _miMultiline.Checked = _textViewTopLeft.Multiline;
  84. _miWrap.Checked = _textViewTopLeft.WordWrap;
  85. var statusBar = new StatusBar (
  86. new []
  87. {
  88. new (
  89. Application.QuitKey,
  90. $"{Application.QuitKey} to Quit",
  91. () => Quit ()
  92. ),
  93. _siMultiline = new StatusItem (KeyCode.Null, "", null),
  94. _siWrap = new StatusItem (KeyCode.Null, "", null)
  95. }
  96. );
  97. Top.Add (statusBar);
  98. Win.LayoutStarted += Win_LayoutStarted;
  99. }
  100. private void Multiline ()
  101. {
  102. _miMultiline.Checked = !_miMultiline.Checked;
  103. SetMultilineStatusText ();
  104. _textViewTopLeft.Multiline = (bool)_miMultiline.Checked;
  105. _textViewTopRight.Multiline = (bool)_miMultiline.Checked;
  106. _textViewBottomLeft.Multiline = (bool)_miMultiline.Checked;
  107. _textViewBottomRight.Multiline = (bool)_miMultiline.Checked;
  108. _textViewCentered.Multiline = (bool)_miMultiline.Checked;
  109. }
  110. private void Quit () { Application.RequestStop (); }
  111. private void SetAllSuggestions (TextView view)
  112. {
  113. ((SingleWordSuggestionGenerator)view.Autocomplete.SuggestionGenerator).AllSuggestions = Regex
  114. .Matches (view.Text, "\\w+")
  115. .Select (s => s.Value)
  116. .Distinct ()
  117. .ToList ();
  118. }
  119. private void SetMultilineStatusText () { _siMultiline.Title = $"Multiline: {_miMultiline.Checked}"; }
  120. private void SetWrapStatusText () { _siWrap.Title = $"WordWrap: {_miWrap.Checked}"; }
  121. private void TextViewBottomLeft_DrawContent (object sender, DrawEventArgs e) { SetAllSuggestions (_textViewBottomLeft); }
  122. private void TextViewBottomRight_DrawContent (object sender, DrawEventArgs e) { SetAllSuggestions (_textViewBottomRight); }
  123. private void TextViewCentered_DrawContent (object sender, DrawEventArgs e) { SetAllSuggestions (_textViewCentered); }
  124. private void TextViewTopLeft_DrawContent (object sender, DrawEventArgs e) { SetAllSuggestions (_textViewTopLeft); }
  125. private void TextViewTopRight_DrawContent (object sender, DrawEventArgs e) { SetAllSuggestions (_textViewTopRight); }
  126. private void Win_LayoutStarted (object sender, LayoutEventArgs obj)
  127. {
  128. _miMultiline.Checked = _textViewTopLeft.Multiline;
  129. _miWrap.Checked = _textViewTopLeft.WordWrap;
  130. SetMultilineStatusText ();
  131. SetWrapStatusText ();
  132. if (_miMultiline.Checked == true)
  133. {
  134. _height = 10;
  135. }
  136. else
  137. {
  138. _height = 1;
  139. }
  140. _textViewBottomLeft.Y = _textViewBottomRight.Y = Pos.AnchorEnd (_height);
  141. }
  142. private void WordWrap ()
  143. {
  144. _miWrap.Checked = !_miWrap.Checked;
  145. _textViewTopLeft.WordWrap = (bool)_miWrap.Checked;
  146. _textViewTopRight.WordWrap = (bool)_miWrap.Checked;
  147. _textViewBottomLeft.WordWrap = (bool)_miWrap.Checked;
  148. _textViewBottomRight.WordWrap = (bool)_miWrap.Checked;
  149. _textViewCentered.WordWrap = (bool)_miWrap.Checked;
  150. _miWrap.Checked = _textViewTopLeft.WordWrap;
  151. SetWrapStatusText ();
  152. }
  153. }