PopupAutocomplete.cs 17 KB

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