TextViewAutocompletePopup.cs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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 Shortcut _siMultiline;
  15. private Shortcut _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 Main ()
  22. {
  23. // Init
  24. Application.Init ();
  25. // Setup - Create a top-level application window and configure it.
  26. Toplevel appWindow = new ();
  27. var width = 20;
  28. var text = " jamp jemp jimp jomp jump";
  29. var menu = new MenuBar
  30. {
  31. Menus =
  32. [
  33. new (
  34. "_File",
  35. new []
  36. {
  37. _miMultiline =
  38. new (
  39. "_Multiline",
  40. "",
  41. () => Multiline ()
  42. ) { CheckType = MenuItemCheckStyle.Checked },
  43. _miWrap = new (
  44. "_Word Wrap",
  45. "",
  46. () => WordWrap ()
  47. ) { CheckType = MenuItemCheckStyle.Checked },
  48. new ("_Quit", "", () => Quit ())
  49. }
  50. )
  51. ]
  52. };
  53. appWindow.Add (menu);
  54. _textViewTopLeft = new()
  55. {
  56. Y = 1,
  57. Width = width, Height = _height, Text = text
  58. };
  59. _textViewTopLeft.DrawingContent += TextViewTopLeft_DrawContent;
  60. appWindow.Add (_textViewTopLeft);
  61. _textViewTopRight = new()
  62. {
  63. X = Pos.AnchorEnd (width), Y = 1,
  64. Width = width, Height = _height, Text = text
  65. };
  66. _textViewTopRight.DrawingContent += TextViewTopRight_DrawContent;
  67. appWindow.Add (_textViewTopRight);
  68. _textViewBottomLeft = new()
  69. {
  70. Y = Pos.AnchorEnd (_height), Width = width, Height = _height, Text = text
  71. };
  72. _textViewBottomLeft.DrawingContent += TextViewBottomLeft_DrawContent;
  73. appWindow.Add (_textViewBottomLeft);
  74. _textViewBottomRight = new()
  75. {
  76. X = Pos.AnchorEnd (width),
  77. Y = Pos.AnchorEnd (_height),
  78. Width = width,
  79. Height = _height,
  80. Text = text
  81. };
  82. _textViewBottomRight.DrawingContent += TextViewBottomRight_DrawContent;
  83. appWindow.Add (_textViewBottomRight);
  84. _textViewCentered = new()
  85. {
  86. X = Pos.Center (),
  87. Y = Pos.Center (),
  88. Width = width,
  89. Height = _height,
  90. Text = text
  91. };
  92. _textViewCentered.DrawingContent += TextViewCentered_DrawContent;
  93. appWindow.Add (_textViewCentered);
  94. _miMultiline.Checked = _textViewTopLeft.Multiline;
  95. _miWrap.Checked = _textViewTopLeft.WordWrap;
  96. var statusBar = new StatusBar (
  97. new []
  98. {
  99. new (
  100. Application.QuitKey,
  101. "Quit",
  102. () => Quit ()
  103. ),
  104. _siMultiline = new (Key.Empty, "", null),
  105. _siWrap = new (Key.Empty, "", null)
  106. }
  107. );
  108. appWindow.Add (statusBar);
  109. appWindow.SubviewLayout += Win_LayoutStarted;
  110. // Run - Start the application.
  111. Application.Run (appWindow);
  112. appWindow.Dispose ();
  113. // Shutdown - Calling Application.Shutdown is required.
  114. Application.Shutdown ();
  115. }
  116. private void Multiline ()
  117. {
  118. _miMultiline.Checked = !_miMultiline.Checked;
  119. SetMultilineStatusText ();
  120. _textViewTopLeft.Multiline = (bool)_miMultiline.Checked;
  121. _textViewTopRight.Multiline = (bool)_miMultiline.Checked;
  122. _textViewBottomLeft.Multiline = (bool)_miMultiline.Checked;
  123. _textViewBottomRight.Multiline = (bool)_miMultiline.Checked;
  124. _textViewCentered.Multiline = (bool)_miMultiline.Checked;
  125. }
  126. private void Quit () { Application.RequestStop (); }
  127. private void SetAllSuggestions (TextView view)
  128. {
  129. ((SingleWordSuggestionGenerator)view.Autocomplete.SuggestionGenerator).AllSuggestions = Regex
  130. .Matches (view.Text, "\\w+")
  131. .Select (s => s.Value)
  132. .Distinct ()
  133. .ToList ();
  134. }
  135. private void SetMultilineStatusText () { _siMultiline.Title = $"Multiline: {_miMultiline.Checked}"; }
  136. private void SetWrapStatusText () { _siWrap.Title = $"WordWrap: {_miWrap.Checked}"; }
  137. private void TextViewBottomLeft_DrawContent (object sender, DrawEventArgs e) { SetAllSuggestions (_textViewBottomLeft); }
  138. private void TextViewBottomRight_DrawContent (object sender, DrawEventArgs e) { SetAllSuggestions (_textViewBottomRight); }
  139. private void TextViewCentered_DrawContent (object sender, DrawEventArgs e) { SetAllSuggestions (_textViewCentered); }
  140. private void TextViewTopLeft_DrawContent (object sender, DrawEventArgs e) { SetAllSuggestions (_textViewTopLeft); }
  141. private void TextViewTopRight_DrawContent (object sender, DrawEventArgs e) { SetAllSuggestions (_textViewTopRight); }
  142. private void Win_LayoutStarted (object sender, LayoutEventArgs obj)
  143. {
  144. _miMultiline.Checked = _textViewTopLeft.Multiline;
  145. _miWrap.Checked = _textViewTopLeft.WordWrap;
  146. SetMultilineStatusText ();
  147. SetWrapStatusText ();
  148. if (_miMultiline.Checked == true)
  149. {
  150. _height = 10;
  151. }
  152. else
  153. {
  154. _height = 1;
  155. }
  156. _textViewBottomLeft.Y = _textViewBottomRight.Y = Pos.AnchorEnd (_height);
  157. }
  158. private void WordWrap ()
  159. {
  160. _miWrap.Checked = !_miWrap.Checked;
  161. _textViewTopLeft.WordWrap = (bool)_miWrap.Checked;
  162. _textViewTopRight.WordWrap = (bool)_miWrap.Checked;
  163. _textViewBottomLeft.WordWrap = (bool)_miWrap.Checked;
  164. _textViewBottomRight.WordWrap = (bool)_miWrap.Checked;
  165. _textViewCentered.WordWrap = (bool)_miWrap.Checked;
  166. _miWrap.Checked = _textViewTopLeft.WordWrap;
  167. SetWrapStatusText ();
  168. }
  169. }