TextViewAutocompletePopup.cs 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. #nullable enable
  2. using System.Text.RegularExpressions;
  3. namespace UICatalog.Scenarios;
  4. [ScenarioMetadata ("TextView Autocomplete Popup", "Shows five TextView Autocomplete Popup effects")]
  5. [ScenarioCategory ("TextView")]
  6. [ScenarioCategory ("Controls")]
  7. [ScenarioCategory ("Mouse and Keyboard")]
  8. public class TextViewAutocompletePopup : Scenario
  9. {
  10. private int _height = 10;
  11. private CheckBox? _miMultilineCheckBox;
  12. private CheckBox? _miWrapCheckBox;
  13. private Shortcut? _siMultiline;
  14. private Shortcut? _siWrap;
  15. private TextView? _textViewBottomLeft;
  16. private TextView? _textViewBottomRight;
  17. private TextView? _textViewCentered;
  18. private TextView? _textViewTopLeft;
  19. private TextView? _textViewTopRight;
  20. public override void Main ()
  21. {
  22. Application.Init ();
  23. Window appWindow = new ()
  24. {
  25. BorderStyle = LineStyle.None
  26. };
  27. var width = 20;
  28. var text = " jamp jemp jimp jomp jump";
  29. // MenuBar
  30. MenuBar menu = new ();
  31. _textViewTopLeft = new ()
  32. {
  33. Y = Pos.Bottom (menu),
  34. Width = width,
  35. Height = _height,
  36. Text = text
  37. };
  38. _textViewTopLeft.DrawingContent += TextViewTopLeft_DrawContent;
  39. appWindow.Add (_textViewTopLeft);
  40. _textViewTopRight = new ()
  41. {
  42. X = Pos.AnchorEnd (width),
  43. Y = Pos.Bottom (menu),
  44. Width = width,
  45. Height = _height,
  46. Text = text
  47. };
  48. _textViewTopRight.DrawingContent += TextViewTopRight_DrawContent;
  49. appWindow.Add (_textViewTopRight);
  50. _textViewBottomLeft = new ()
  51. {
  52. Y = Pos.AnchorEnd (_height),
  53. Width = width,
  54. Height = _height,
  55. Text = text
  56. };
  57. _textViewBottomLeft.DrawingContent += TextViewBottomLeft_DrawContent;
  58. appWindow.Add (_textViewBottomLeft);
  59. _textViewBottomRight = new ()
  60. {
  61. X = Pos.AnchorEnd (width),
  62. Y = Pos.AnchorEnd (_height),
  63. Width = width,
  64. Height = _height,
  65. Text = text
  66. };
  67. _textViewBottomRight.DrawingContent += TextViewBottomRight_DrawContent;
  68. appWindow.Add (_textViewBottomRight);
  69. _textViewCentered = new ()
  70. {
  71. X = Pos.Center (),
  72. Y = Pos.Center (),
  73. Width = width,
  74. Height = _height,
  75. Text = text
  76. };
  77. _textViewCentered.DrawingContent += TextViewCentered_DrawContent;
  78. appWindow.Add (_textViewCentered);
  79. // Setup menu checkboxes
  80. _miMultilineCheckBox = new ()
  81. {
  82. Title = "_Multiline",
  83. CheckedState = _textViewTopLeft.Multiline ? CheckState.Checked : CheckState.UnChecked
  84. };
  85. _miMultilineCheckBox.CheckedStateChanged += (s, e) => Multiline ();
  86. _miWrapCheckBox = new ()
  87. {
  88. Title = "_Word Wrap",
  89. CheckedState = _textViewTopLeft.WordWrap ? CheckState.Checked : CheckState.UnChecked
  90. };
  91. _miWrapCheckBox.CheckedStateChanged += (s, e) => WordWrap ();
  92. menu.Add (
  93. new MenuBarItem (
  94. "_File",
  95. [
  96. new MenuItem
  97. {
  98. CommandView = _miMultilineCheckBox
  99. },
  100. new MenuItem
  101. {
  102. CommandView = _miWrapCheckBox
  103. },
  104. new MenuItem
  105. {
  106. Title = "_Quit",
  107. Action = Quit
  108. }
  109. ]
  110. )
  111. );
  112. // StatusBar
  113. _siMultiline = new (Key.Empty, "", null);
  114. _siWrap = new (Key.Empty, "", null);
  115. StatusBar statusBar = new (
  116. [
  117. new (
  118. Application.QuitKey,
  119. "Quit",
  120. () => Quit ()
  121. ),
  122. _siMultiline,
  123. _siWrap
  124. ]
  125. );
  126. appWindow.Add (menu, statusBar);
  127. appWindow.SubViewLayout += Win_LayoutStarted;
  128. Application.Run (appWindow);
  129. appWindow.Dispose ();
  130. Application.Shutdown ();
  131. }
  132. private void Multiline ()
  133. {
  134. if (_miMultilineCheckBox is null
  135. || _textViewTopLeft is null
  136. || _textViewTopRight is null
  137. || _textViewBottomLeft is null
  138. || _textViewBottomRight is null
  139. || _textViewCentered is null)
  140. {
  141. return;
  142. }
  143. SetMultilineStatusText ();
  144. _textViewTopLeft.Multiline = _miMultilineCheckBox.CheckedState == CheckState.Checked;
  145. _textViewTopRight.Multiline = _miMultilineCheckBox.CheckedState == CheckState.Checked;
  146. _textViewBottomLeft.Multiline = _miMultilineCheckBox.CheckedState == CheckState.Checked;
  147. _textViewBottomRight.Multiline = _miMultilineCheckBox.CheckedState == CheckState.Checked;
  148. _textViewCentered.Multiline = _miMultilineCheckBox.CheckedState == CheckState.Checked;
  149. }
  150. private void Quit () { Application.RequestStop (); }
  151. private void SetAllSuggestions (TextView view)
  152. {
  153. if (view.Autocomplete.SuggestionGenerator is SingleWordSuggestionGenerator generator)
  154. {
  155. generator.AllSuggestions = Regex
  156. .Matches (view.Text, "\\w+")
  157. .Select (s => s.Value)
  158. .Distinct ()
  159. .ToList ();
  160. }
  161. }
  162. private void SetMultilineStatusText ()
  163. {
  164. if (_siMultiline is { } && _miMultilineCheckBox is { })
  165. {
  166. _siMultiline.Title = $"Multiline: {_miMultilineCheckBox.CheckedState == CheckState.Checked}";
  167. }
  168. }
  169. private void SetWrapStatusText ()
  170. {
  171. if (_siWrap is { } && _miWrapCheckBox is { })
  172. {
  173. _siWrap.Title = $"WordWrap: {_miWrapCheckBox.CheckedState == CheckState.Checked}";
  174. }
  175. }
  176. private void TextViewBottomLeft_DrawContent (object? sender, DrawEventArgs e)
  177. {
  178. if (_textViewBottomLeft is { })
  179. {
  180. SetAllSuggestions (_textViewBottomLeft);
  181. }
  182. }
  183. private void TextViewBottomRight_DrawContent (object? sender, DrawEventArgs e)
  184. {
  185. if (_textViewBottomRight is { })
  186. {
  187. SetAllSuggestions (_textViewBottomRight);
  188. }
  189. }
  190. private void TextViewCentered_DrawContent (object? sender, DrawEventArgs e)
  191. {
  192. if (_textViewCentered is { })
  193. {
  194. SetAllSuggestions (_textViewCentered);
  195. }
  196. }
  197. private void TextViewTopLeft_DrawContent (object? sender, DrawEventArgs e)
  198. {
  199. if (_textViewTopLeft is { })
  200. {
  201. SetAllSuggestions (_textViewTopLeft);
  202. }
  203. }
  204. private void TextViewTopRight_DrawContent (object? sender, DrawEventArgs e)
  205. {
  206. if (_textViewTopRight is { })
  207. {
  208. SetAllSuggestions (_textViewTopRight);
  209. }
  210. }
  211. private void Win_LayoutStarted (object? sender, LayoutEventArgs obj)
  212. {
  213. if (_textViewTopLeft is null || _miMultilineCheckBox is null || _miWrapCheckBox is null || _textViewBottomLeft is null || _textViewBottomRight is null)
  214. {
  215. return;
  216. }
  217. _miMultilineCheckBox.CheckedState = _textViewTopLeft.Multiline ? CheckState.Checked : CheckState.UnChecked;
  218. _miWrapCheckBox.CheckedState = _textViewTopLeft.WordWrap ? CheckState.Checked : CheckState.UnChecked;
  219. SetMultilineStatusText ();
  220. SetWrapStatusText ();
  221. if (_miMultilineCheckBox.CheckedState == CheckState.Checked)
  222. {
  223. _height = 10;
  224. }
  225. else
  226. {
  227. _height = 1;
  228. }
  229. _textViewBottomLeft.Y = _textViewBottomRight.Y = Pos.AnchorEnd (_height);
  230. }
  231. private void WordWrap ()
  232. {
  233. if (_miWrapCheckBox is null
  234. || _textViewTopLeft is null
  235. || _textViewTopRight is null
  236. || _textViewBottomLeft is null
  237. || _textViewBottomRight is null
  238. || _textViewCentered is null)
  239. {
  240. return;
  241. }
  242. _textViewTopLeft.WordWrap = _miWrapCheckBox.CheckedState == CheckState.Checked;
  243. _textViewTopRight.WordWrap = _miWrapCheckBox.CheckedState == CheckState.Checked;
  244. _textViewBottomLeft.WordWrap = _miWrapCheckBox.CheckedState == CheckState.Checked;
  245. _textViewBottomRight.WordWrap = _miWrapCheckBox.CheckedState == CheckState.Checked;
  246. _textViewCentered.WordWrap = _miWrapCheckBox.CheckedState == CheckState.Checked;
  247. _miWrapCheckBox.CheckedState = _textViewTopLeft.WordWrap ? CheckState.Checked : CheckState.UnChecked;
  248. SetWrapStatusText ();
  249. }
  250. }