PopupAutocomplete.cs 16 KB

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