TextViewAutocompletePopup.cs 4.9 KB

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