PopupAutocomplete.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Collections.ObjectModel;
  4. using System.Linq;
  5. using System.Text;
  6. namespace Terminal.Gui {
  7. /// <summary>
  8. /// Renders an overlay on another view at a given point that allows selecting
  9. /// from a range of 'autocomplete' options.
  10. /// </summary>
  11. public abstract class PopupAutocomplete : AutocompleteBase {
  12. private class Popup : View {
  13. PopupAutocomplete autocomplete;
  14. public Popup (PopupAutocomplete autocomplete)
  15. {
  16. this.autocomplete = autocomplete;
  17. CanFocus = true;
  18. WantMousePositionReports = true;
  19. }
  20. public override void OnDrawContent (Rect contentArea)
  21. {
  22. if (autocomplete.LastPopupPos == null) {
  23. return;
  24. }
  25. autocomplete.RenderOverlay ((Point)autocomplete.LastPopupPos);
  26. }
  27. public override bool MouseEvent (MouseEvent mouseEvent)
  28. {
  29. return autocomplete.MouseEvent (mouseEvent);
  30. }
  31. }
  32. private View top, popup;
  33. private bool closed;
  34. int toRenderLength;
  35. private Point? LastPopupPos { get; set; }
  36. private ColorScheme colorScheme;
  37. private View hostControl;
  38. /// <summary>
  39. /// The host control to handle.
  40. /// </summary>
  41. public override View HostControl {
  42. get => hostControl;
  43. set {
  44. hostControl = value;
  45. top = hostControl.SuperView;
  46. if (top != null) {
  47. top.DrawContent += Top_DrawContent;
  48. top.DrawContentComplete += Top_DrawContentComplete;
  49. top.Removed += Top_Removed;
  50. }
  51. }
  52. }
  53. /// <summary>
  54. /// Creates a new instance of the <see cref="PopupAutocomplete"/> class.
  55. /// </summary>
  56. public PopupAutocomplete ()
  57. {
  58. PopupInsideContainer = true;
  59. }
  60. private void Top_Removed (object sender, SuperViewChangedEventArgs e)
  61. {
  62. Visible = false;
  63. ManipulatePopup ();
  64. }
  65. private void Top_DrawContentComplete (object sender, DrawEventArgs e)
  66. {
  67. ManipulatePopup ();
  68. }
  69. private void Top_DrawContent (object sender, DrawEventArgs e)
  70. {
  71. if (!closed) {
  72. ReopenSuggestions ();
  73. }
  74. ManipulatePopup ();
  75. if (Visible) {
  76. top.BringSubviewToFront (popup);
  77. }
  78. }
  79. private void ManipulatePopup ()
  80. {
  81. if (Visible && popup == null) {
  82. popup = new Popup (this) {
  83. Frame = Rect.Empty
  84. };
  85. top?.Add (popup);
  86. }
  87. if (!Visible && popup != null) {
  88. top?.Remove (popup);
  89. popup.Dispose ();
  90. popup = null;
  91. }
  92. }
  93. /// <summary>
  94. /// When more suggestions are available than can be rendered the user
  95. /// can scroll down the dropdown list. This indicates how far down they
  96. /// have gone
  97. /// </summary>
  98. public virtual int ScrollOffset { get; set; }
  99. /// <summary>
  100. /// The colors to use to render the overlay. Accessing this property before
  101. /// the Application has been initialized will cause an error
  102. /// </summary>
  103. public override ColorScheme ColorScheme {
  104. get {
  105. if (colorScheme == null) {
  106. colorScheme = Colors.ColorSchemes ["Menu"];
  107. }
  108. return colorScheme;
  109. }
  110. set {
  111. colorScheme = value;
  112. }
  113. }
  114. /// <summary>
  115. /// Renders the autocomplete dialog inside or outside the given <see cref="HostControl"/> at the
  116. /// given point.
  117. /// </summary>
  118. /// <param name="renderAt"></param>
  119. public override void RenderOverlay (Point renderAt)
  120. {
  121. if (!Context.Canceled && Suggestions.Count > 0 && !Visible && HostControl?.HasFocus == true) {
  122. ProcessKey (new ((KeyCode)(Suggestions [0].Title [0])));
  123. } else if (!Visible || HostControl?.HasFocus == false || Suggestions.Count == 0) {
  124. LastPopupPos = null;
  125. Visible = false;
  126. if (Suggestions.Count == 0) {
  127. Context.Canceled = false;
  128. }
  129. return;
  130. }
  131. LastPopupPos = renderAt;
  132. int height, width;
  133. if (PopupInsideContainer) {
  134. // don't overspill vertically
  135. height = Math.Min (HostControl.Bounds.Height - renderAt.Y, MaxHeight);
  136. // There is no space below, lets see if can popup on top
  137. if (height < Suggestions.Count && HostControl.Bounds.Height - renderAt.Y >= height) {
  138. // Verifies that the upper limit available is greater than the lower limit
  139. if (renderAt.Y > HostControl.Bounds.Height - renderAt.Y) {
  140. renderAt.Y = Math.Max (renderAt.Y - Math.Min (Suggestions.Count + 1, MaxHeight + 1), 0);
  141. height = Math.Min (Math.Min (Suggestions.Count, MaxHeight), LastPopupPos.Value.Y - 1);
  142. }
  143. }
  144. } else {
  145. // don't overspill vertically
  146. height = Math.Min (Math.Min (top.Bounds.Height - HostControl.Frame.Bottom, MaxHeight), Suggestions.Count);
  147. // There is no space below, lets see if can popup on top
  148. if (height < Suggestions.Count && HostControl.Frame.Y - top.Frame.Y >= height) {
  149. // Verifies that the upper limit available is greater than the lower limit
  150. if (HostControl.Frame.Y > top.Bounds.Height - HostControl.Frame.Y) {
  151. renderAt.Y = Math.Max (HostControl.Frame.Y - Math.Min (Suggestions.Count, MaxHeight), 0);
  152. height = Math.Min (Math.Min (Suggestions.Count, MaxHeight), HostControl.Frame.Y);
  153. }
  154. } else {
  155. renderAt.Y = HostControl.Frame.Bottom;
  156. }
  157. }
  158. if (ScrollOffset > Suggestions.Count - height) {
  159. ScrollOffset = 0;
  160. }
  161. var toRender = Suggestions.Skip (ScrollOffset).Take (height).ToArray ();
  162. toRenderLength = toRender.Length;
  163. if (toRender.Length == 0) {
  164. return;
  165. }
  166. width = Math.Min (MaxWidth, toRender.Max (s => s.Title.Length));
  167. if (PopupInsideContainer) {
  168. // don't overspill horizontally, let's see if can be displayed on the left
  169. if (width > HostControl.Bounds.Width - renderAt.X) {
  170. // Verifies that the left limit available is greater than the right limit
  171. if (renderAt.X > HostControl.Bounds.Width - renderAt.X) {
  172. renderAt.X -= Math.Min (width, LastPopupPos.Value.X);
  173. width = Math.Min (width, LastPopupPos.Value.X);
  174. } else {
  175. width = Math.Min (width, HostControl.Bounds.Width - renderAt.X);
  176. }
  177. }
  178. } else {
  179. // don't overspill horizontally, let's see if can be displayed on the left
  180. if (width > top.Bounds.Width - (renderAt.X + HostControl.Frame.X)) {
  181. // Verifies that the left limit available is greater than the right limit
  182. if (renderAt.X + HostControl.Frame.X > top.Bounds.Width - (renderAt.X + HostControl.Frame.X)) {
  183. renderAt.X -= Math.Min (width, LastPopupPos.Value.X);
  184. width = Math.Min (width, LastPopupPos.Value.X);
  185. } else {
  186. width = Math.Min (width, top.Bounds.Width - renderAt.X);
  187. }
  188. }
  189. }
  190. if (PopupInsideContainer) {
  191. popup.Frame = new Rect (
  192. new Point (HostControl.Frame.X + renderAt.X, HostControl.Frame.Y + renderAt.Y),
  193. new Size (width, height));
  194. } else {
  195. popup.Frame = new Rect (
  196. new Point (HostControl.Frame.X + renderAt.X, renderAt.Y),
  197. new Size (width, height));
  198. }
  199. popup.Move (0, 0);
  200. for (int i = 0; i < toRender.Length; i++) {
  201. if (i == SelectedIdx - ScrollOffset) {
  202. Application.Driver.SetAttribute (ColorScheme.Focus);
  203. } else {
  204. Application.Driver.SetAttribute (ColorScheme.Normal);
  205. }
  206. popup.Move (0, i);
  207. var text = TextFormatter.ClipOrPad (toRender [i].Title, width);
  208. Application.Driver.AddStr (text);
  209. }
  210. }
  211. /// <inheritdoc/>
  212. public override void EnsureSelectedIdxIsValid ()
  213. {
  214. base.EnsureSelectedIdxIsValid ();
  215. // if user moved selection up off top of current scroll window
  216. if (SelectedIdx < ScrollOffset) {
  217. ScrollOffset = SelectedIdx;
  218. }
  219. // if user moved selection down past bottom of current scroll window
  220. while (toRenderLength > 0 && SelectedIdx >= ScrollOffset + toRenderLength) {
  221. ScrollOffset++;
  222. }
  223. }
  224. /// <summary>
  225. /// Handle key events before <see cref="HostControl"/> e.g. to make key events like
  226. /// up/down apply to the autocomplete control instead of changing the cursor position in
  227. /// the underlying text view.
  228. /// </summary>
  229. /// <param name="a">The key event.</param>
  230. /// <returns><c>true</c>if the key can be handled <c>false</c>otherwise.</returns>
  231. public override bool ProcessKey (Key a)
  232. {
  233. if (SuggestionGenerator.IsWordChar ((Rune)a)) {
  234. Visible = true;
  235. ManipulatePopup ();
  236. closed = false;
  237. return false;
  238. }
  239. if (a.KeyCode == Reopen) {
  240. Context.Canceled = false;
  241. return ReopenSuggestions ();
  242. }
  243. if (closed || Suggestions.Count == 0) {
  244. Visible = false;
  245. if (!closed) {
  246. Close ();
  247. }
  248. return false;
  249. }
  250. if (a.KeyCode == KeyCode.CursorDown) {
  251. MoveDown ();
  252. return true;
  253. }
  254. if (a.KeyCode == KeyCode.CursorUp) {
  255. MoveUp ();
  256. return true;
  257. }
  258. // TODO : Revisit this
  259. /*if (a.ConsoleDriverKey == Key.CursorLeft || a.ConsoleDriverKey == Key.CursorRight) {
  260. GenerateSuggestions (a.ConsoleDriverKey == Key.CursorLeft ? -1 : 1);
  261. if (Suggestions.Count == 0) {
  262. Visible = false;
  263. if (!closed) {
  264. Close ();
  265. }
  266. }
  267. return false;
  268. }*/
  269. if (a.KeyCode == SelectionKey) {
  270. return Select ();
  271. }
  272. if (a.KeyCode == CloseKey) {
  273. Close ();
  274. Context.Canceled = true;
  275. return true;
  276. }
  277. return false;
  278. }
  279. /// <summary>
  280. /// Handle mouse events before <see cref="HostControl"/> e.g. to make mouse events like
  281. /// report/click apply to the autocomplete control instead of changing the cursor position in
  282. /// the underlying text view.
  283. /// </summary>
  284. /// <param name="me">The mouse event.</param>
  285. /// <param name="fromHost">If was called from the popup or from the host.</param>
  286. /// <returns><c>true</c>if the mouse can be handled <c>false</c>otherwise.</returns>
  287. public override bool MouseEvent (MouseEvent me, bool fromHost = false)
  288. {
  289. if (fromHost) {
  290. if (!Visible) {
  291. return false;
  292. }
  293. // TODO: Revisit this
  294. //GenerateSuggestions ();
  295. if (Visible && Suggestions.Count == 0) {
  296. Visible = false;
  297. HostControl?.SetNeedsDisplay ();
  298. return true;
  299. } else if (!Visible && Suggestions.Count > 0) {
  300. Visible = true;
  301. HostControl?.SetNeedsDisplay ();
  302. Application.UngrabMouse ();
  303. return false;
  304. } else {
  305. // not in the popup
  306. if (Visible && HostControl != null) {
  307. Visible = false;
  308. closed = false;
  309. }
  310. HostControl?.SetNeedsDisplay ();
  311. }
  312. return false;
  313. }
  314. if (popup == null || Suggestions.Count == 0) {
  315. ManipulatePopup ();
  316. return false;
  317. }
  318. if (me.Flags == MouseFlags.ReportMousePosition) {
  319. RenderSelectedIdxByMouse (me);
  320. return true;
  321. }
  322. if (me.Flags == MouseFlags.Button1Clicked) {
  323. SelectedIdx = me.Y - ScrollOffset;
  324. return Select ();
  325. }
  326. if (me.Flags == MouseFlags.WheeledDown) {
  327. MoveDown ();
  328. return true;
  329. }
  330. if (me.Flags == MouseFlags.WheeledUp) {
  331. MoveUp ();
  332. return true;
  333. }
  334. return false;
  335. }
  336. /// <summary>
  337. /// Render the current selection in the Autocomplete context menu by the mouse reporting.
  338. /// </summary>
  339. /// <param name="me"></param>
  340. protected void RenderSelectedIdxByMouse (MouseEvent me)
  341. {
  342. if (SelectedIdx != me.Y - ScrollOffset) {
  343. SelectedIdx = me.Y - ScrollOffset;
  344. if (LastPopupPos != null) {
  345. RenderOverlay ((Point)LastPopupPos);
  346. }
  347. }
  348. }
  349. /// <summary>
  350. /// Completes the autocomplete selection process. Called when user hits the <see cref="IAutocomplete.SelectionKey"/>.
  351. /// </summary>
  352. /// <returns></returns>
  353. protected bool Select ()
  354. {
  355. if (SelectedIdx >= 0 && SelectedIdx < Suggestions.Count) {
  356. var accepted = Suggestions [SelectedIdx];
  357. return InsertSelection (accepted);
  358. }
  359. return false;
  360. }
  361. /// <summary>
  362. /// Called when the user confirms a selection at the current cursor location in
  363. /// the <see cref="HostControl"/>. The <paramref name="accepted"/> string
  364. /// is the full autocomplete word to be inserted. Typically a host will have to
  365. /// remove some characters such that the <paramref name="accepted"/> string
  366. /// completes the word instead of simply being appended.
  367. /// </summary>
  368. /// <param name="accepted"></param>
  369. /// <returns>True if the insertion was possible otherwise false</returns>
  370. protected virtual bool InsertSelection (Suggestion accepted)
  371. {
  372. SetCursorPosition (Context.CursorPosition + accepted.Remove);
  373. // delete the text
  374. for (int i = 0; i < accepted.Remove; i++) {
  375. DeleteTextBackwards ();
  376. }
  377. InsertText (accepted.Replacement);
  378. return true;
  379. }
  380. /// <summary>
  381. /// Deletes the text backwards before insert the selected text in the <see cref="HostControl"/>.
  382. /// </summary>
  383. protected abstract void DeleteTextBackwards ();
  384. /// <summary>
  385. /// Insert the selected text in the <see cref="HostControl"/>.
  386. /// </summary>
  387. /// <param name="accepted"></param>
  388. protected abstract void InsertText (string accepted);
  389. /// <summary>
  390. /// Set the cursor position in the <see cref="HostControl"/>.
  391. /// </summary>
  392. /// <param name="column"></param>
  393. protected abstract void SetCursorPosition (int column);
  394. /// <summary>
  395. /// Closes the Autocomplete context menu if it is showing and <see cref="IAutocomplete.ClearSuggestions"/>
  396. /// </summary>
  397. protected void Close ()
  398. {
  399. ClearSuggestions ();
  400. Visible = false;
  401. closed = true;
  402. HostControl?.SetNeedsDisplay ();
  403. ManipulatePopup ();
  404. }
  405. /// <summary>
  406. /// Moves the selection in the Autocomplete context menu up one
  407. /// </summary>
  408. protected void MoveUp ()
  409. {
  410. SelectedIdx--;
  411. if (SelectedIdx < 0) {
  412. SelectedIdx = Suggestions.Count - 1;
  413. }
  414. EnsureSelectedIdxIsValid ();
  415. HostControl?.SetNeedsDisplay ();
  416. }
  417. /// <summary>
  418. /// Moves the selection in the Autocomplete context menu down one
  419. /// </summary>
  420. protected void MoveDown ()
  421. {
  422. SelectedIdx++;
  423. if (SelectedIdx > Suggestions.Count - 1) {
  424. SelectedIdx = 0;
  425. }
  426. EnsureSelectedIdxIsValid ();
  427. HostControl?.SetNeedsDisplay ();
  428. }
  429. /// <summary>
  430. /// Reopen the popup after it has been closed.
  431. /// </summary>
  432. /// <returns></returns>
  433. protected bool ReopenSuggestions ()
  434. {
  435. // TODO: Revisit
  436. //GenerateSuggestions ();
  437. if (Suggestions.Count > 0) {
  438. Visible = true;
  439. closed = false;
  440. HostControl?.SetNeedsDisplay ();
  441. return true;
  442. }
  443. return false;
  444. }
  445. }
  446. }