TextViewAutocompletePopup.cs 7.2 KB

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