PopupAutocomplete.cs 16 KB

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