TextViewAutocompletePopup.cs 5.0 KB

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