PopupAutocomplete.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586
  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.Move (0, 0);
  326. for (var i = 0; i < toRender.Length; i++)
  327. {
  328. if (i == SelectedIdx - ScrollOffset)
  329. {
  330. _popup.SetAttribute (Scheme.Focus);
  331. }
  332. else
  333. {
  334. _popup.SetAttribute (Scheme.Normal);
  335. }
  336. _popup.Move (0, i);
  337. string text = TextFormatter.ClipOrPad (toRender [i].Title, width);
  338. _popup.App?.Driver?.AddStr (text);
  339. }
  340. }
  341. /// <summary>
  342. /// When more suggestions are available than can be rendered the user can scroll down the dropdown list. This
  343. /// indicates how far down they have gone
  344. /// </summary>
  345. public virtual int ScrollOffset { get; set; }
  346. /// <summary>
  347. /// Closes the Autocomplete context menu if it is showing and <see cref="IAutocomplete.ClearSuggestions"/>
  348. /// </summary>
  349. protected void Close ()
  350. {
  351. ClearSuggestions ();
  352. Visible = false;
  353. _closed = true;
  354. //RemovePopupFromTop ();
  355. _popup.Visible = false;
  356. HostControl?.SetNeedsDraw ();
  357. }
  358. /// <summary>Deletes the text backwards before insert the selected text in the <see cref="HostControl"/>.</summary>
  359. protected abstract void DeleteTextBackwards ();
  360. /// <summary>
  361. /// Called when the user confirms a selection at the current cursor location in the <see cref="HostControl"/>. The
  362. /// <paramref name="accepted"/> string is the full autocomplete word to be inserted. Typically, a host will have to
  363. /// remove some characters such that the <paramref name="accepted"/> string completes the word instead of simply being
  364. /// appended.
  365. /// </summary>
  366. /// <param name="accepted"></param>
  367. /// <returns>True if the insertion was possible otherwise false</returns>
  368. protected virtual bool InsertSelection (Suggestion accepted)
  369. {
  370. SetCursorPosition (Context.CursorPosition + accepted.Remove);
  371. // delete the text
  372. for (var i = 0; i < accepted.Remove; i++)
  373. {
  374. DeleteTextBackwards ();
  375. }
  376. InsertText (accepted.Replacement);
  377. return true;
  378. }
  379. /// <summary>Insert the selected text in the <see cref="HostControl"/>.</summary>
  380. /// <param name="accepted"></param>
  381. protected abstract void InsertText (string accepted);
  382. /// <summary>Moves the selection in the Autocomplete context menu down one</summary>
  383. protected void MoveDown ()
  384. {
  385. SelectedIdx++;
  386. if (SelectedIdx > Suggestions.Count - 1)
  387. {
  388. SelectedIdx = 0;
  389. }
  390. EnsureSelectedIdxIsValid ();
  391. HostControl?.SetNeedsDraw ();
  392. }
  393. /// <summary>Moves the selection in the Autocomplete context menu up one</summary>
  394. protected void MoveUp ()
  395. {
  396. SelectedIdx--;
  397. if (SelectedIdx < 0)
  398. {
  399. SelectedIdx = Suggestions.Count - 1;
  400. }
  401. EnsureSelectedIdxIsValid ();
  402. HostControl?.SetNeedsDraw ();
  403. }
  404. /// <summary>Render the current selection in the Autocomplete context menu by the mouse reporting.</summary>
  405. /// <param name="me"></param>
  406. protected void RenderSelectedIdxByMouse (MouseEventArgs me)
  407. {
  408. if (SelectedIdx != me.Position.Y - ScrollOffset)
  409. {
  410. SelectedIdx = me.Position.Y - ScrollOffset;
  411. if (LastPopupPos is { })
  412. {
  413. RenderOverlay ((Point)LastPopupPos);
  414. }
  415. }
  416. }
  417. /// <summary>Reopen the popup after it has been closed.</summary>
  418. /// <returns></returns>
  419. protected bool ReopenSuggestions ()
  420. {
  421. // TODO: Revisit
  422. //GenerateSuggestions ();
  423. if (Suggestions.Count > 0)
  424. {
  425. Visible = true;
  426. _closed = false;
  427. _popup.Visible = true;
  428. HostControl?.SetNeedsDraw ();
  429. return true;
  430. }
  431. return false;
  432. }
  433. /// <summary>
  434. /// Completes the autocomplete selection process. Called when user hits the
  435. /// <see cref="IAutocomplete.SelectionKey"/>.
  436. /// </summary>
  437. /// <returns></returns>
  438. protected bool Select ()
  439. {
  440. if (SelectedIdx >= 0 && SelectedIdx < Suggestions.Count)
  441. {
  442. Suggestion accepted = Suggestions [SelectedIdx];
  443. return InsertSelection (accepted);
  444. }
  445. return false;
  446. }
  447. /// <summary>Set the cursor position in the <see cref="HostControl"/>.</summary>
  448. /// <param name="column"></param>
  449. protected abstract void SetCursorPosition (int column);
  450. private Point? LastPopupPos { get; set; }
  451. #nullable restore
  452. private void AddPopupToTop ()
  453. {
  454. if (_popup is null)
  455. {
  456. _popup = new Popup (this)
  457. {
  458. CanFocus = false
  459. };
  460. _top?.Add (_popup);
  461. }
  462. }
  463. private void RemovePopupFromTop ()
  464. {
  465. if (_popup is { } && _top.SubViews.Contains (_popup))
  466. {
  467. _top?.Remove (_popup);
  468. _popup.Dispose ();
  469. _popup = null;
  470. }
  471. }
  472. private void _top_Initialized (object sender, EventArgs e)
  473. {
  474. if (_top is null)
  475. {
  476. _top = sender as View;
  477. }
  478. AddPopupToTop ();
  479. }
  480. private void _top_Removed (object sender, SuperViewChangedEventArgs e)
  481. {
  482. Visible = false;
  483. RemovePopupFromTop ();
  484. }
  485. }