TextViewAutocompletePopup.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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 text = " jamp jemp jimp jomp jump";
  25. var menu = new MenuBar (new MenuBarItem [] {
  26. new MenuBarItem ("_File", new MenuItem [] {
  27. miMultiline = new MenuItem ("_Multiline", "", () => Multiline()){CheckType = MenuItemCheckStyle.Checked},
  28. miWrap = new MenuItem ("_Word Wrap", "", () => WordWrap()){CheckType = MenuItemCheckStyle.Checked},
  29. new MenuItem ("_Quit", "", () => Quit())
  30. })
  31. });
  32. Top.Add (menu);
  33. textViewTopLeft = new TextView () {
  34. Width = width,
  35. Height = height,
  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. Text = text
  45. };
  46. textViewTopRight.DrawContent += TextViewTopRight_DrawContent;
  47. Win.Add (textViewTopRight);
  48. textViewBottomLeft = new TextView () {
  49. Y = Pos.AnchorEnd (height),
  50. Width = width,
  51. Height = height,
  52. Text = text
  53. };
  54. textViewBottomLeft.DrawContent += TextViewBottomLeft_DrawContent;
  55. Win.Add (textViewBottomLeft);
  56. textViewBottomRight = new TextView () {
  57. X = Pos.AnchorEnd (width),
  58. Y = Pos.AnchorEnd (height),
  59. Width = width,
  60. Height = height,
  61. Text = text
  62. };
  63. textViewBottomRight.DrawContent += TextViewBottomRight_DrawContent;
  64. Win.Add (textViewBottomRight);
  65. textViewCentered = new TextView () {
  66. X = Pos.Center (),
  67. Y = Pos.Center (),
  68. Width = width,
  69. Height = height,
  70. Text = text
  71. };
  72. textViewCentered.DrawContent += TextViewCentered_DrawContent;
  73. Win.Add (textViewCentered);
  74. miMultiline.Checked = textViewTopLeft.Multiline;
  75. miWrap.Checked = textViewTopLeft.WordWrap;
  76. var statusBar = new StatusBar (new StatusItem [] {
  77. new StatusItem(Key.CtrlMask | Key.Q, "~^Q~ Quit", () => Quit()),
  78. siMultiline = new StatusItem(Key.Null, "", null),
  79. siWrap = new StatusItem(Key.Null, "", null)
  80. });
  81. Top.Add (statusBar);
  82. Win.LayoutStarted += Win_LayoutStarted;
  83. }
  84. private void Win_LayoutStarted (View.LayoutEventArgs obj)
  85. {
  86. miMultiline.Checked = textViewTopLeft.Multiline;
  87. miWrap.Checked = textViewTopLeft.WordWrap;
  88. SetMultilineStatusText ();
  89. SetWrapStatusText ();
  90. if (miMultiline.Checked) {
  91. height = 10;
  92. } else {
  93. height = 1;
  94. }
  95. textViewBottomLeft.Y = textViewBottomRight.Y = Pos.AnchorEnd (height);
  96. }
  97. private void SetMultilineStatusText ()
  98. {
  99. siMultiline.Title = $"Multiline: {miMultiline.Checked}";
  100. }
  101. private void SetWrapStatusText ()
  102. {
  103. siWrap.Title = $"WordWrap: {miWrap.Checked}";
  104. }
  105. private void SetAllSuggestions (TextView view)
  106. {
  107. view.Autocomplete.AllSuggestions = Regex.Matches (view.Text.ToString (), "\\w+")
  108. .Select (s => s.Value)
  109. .Distinct ().ToList ();
  110. }
  111. private void TextViewCentered_DrawContent (Rect obj)
  112. {
  113. SetAllSuggestions (textViewCentered);
  114. }
  115. private void TextViewBottomRight_DrawContent (Rect obj)
  116. {
  117. SetAllSuggestions (textViewBottomRight);
  118. }
  119. private void TextViewBottomLeft_DrawContent (Rect obj)
  120. {
  121. SetAllSuggestions (textViewBottomLeft);
  122. }
  123. private void TextViewTopRight_DrawContent (Rect obj)
  124. {
  125. SetAllSuggestions (textViewTopRight);
  126. }
  127. private void TextViewTopLeft_DrawContent (Rect obj)
  128. {
  129. SetAllSuggestions (textViewTopLeft);
  130. }
  131. private void Multiline ()
  132. {
  133. miMultiline.Checked = !miMultiline.Checked;
  134. SetMultilineStatusText ();
  135. textViewTopLeft.Multiline = miMultiline.Checked;
  136. textViewTopRight.Multiline = miMultiline.Checked;
  137. textViewBottomLeft.Multiline = miMultiline.Checked;
  138. textViewBottomRight.Multiline = miMultiline.Checked;
  139. textViewCentered.Multiline = miMultiline.Checked;
  140. }
  141. private void WordWrap ()
  142. {
  143. miWrap.Checked = !miWrap.Checked;
  144. textViewTopLeft.WordWrap = miWrap.Checked;
  145. textViewTopRight.WordWrap = miWrap.Checked;
  146. textViewBottomLeft.WordWrap = miWrap.Checked;
  147. textViewBottomRight.WordWrap = miWrap.Checked;
  148. textViewCentered.WordWrap = miWrap.Checked;
  149. miWrap.Checked = textViewTopLeft.WordWrap;
  150. SetWrapStatusText ();
  151. }
  152. private void Quit ()
  153. {
  154. Application.RequestStop ();
  155. }
  156. }
  157. }