PopupAutocomplete.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580
  1. using System.Diagnostics;
  2. namespace Terminal.Gui;
  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 ColorScheme _colorScheme;
  11. private View _hostControl;
  12. private View _top; // The _hostControl's SuperView
  13. private 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 ColorScheme ColorScheme
  22. {
  23. get
  24. {
  25. if (_colorScheme is null)
  26. {
  27. _colorScheme = Colors.ColorSchemes ["Menu"];
  28. }
  29. return _colorScheme;
  30. }
  31. set => _colorScheme = 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. Application.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 ((Rune)key))
  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. Visible = false;
  221. if (Suggestions.Count == 0)
  222. {
  223. Context.Canceled = false;
  224. }
  225. return;
  226. }
  227. LastPopupPos = renderAt;
  228. int height, width;
  229. if (PopupInsideContainer)
  230. {
  231. // don't overspill vertically
  232. height = Math.Min (HostControl.Viewport.Height - renderAt.Y, MaxHeight);
  233. // There is no space below, lets see if can popup on top
  234. if (height < Suggestions.Count && HostControl.Viewport.Height - renderAt.Y >= height)
  235. {
  236. // Verifies that the upper limit available is greater than the lower limit
  237. if (renderAt.Y > HostControl.Viewport.Height - renderAt.Y)
  238. {
  239. renderAt.Y = Math.Max (renderAt.Y - Math.Min (Suggestions.Count + 1, MaxHeight + 1), 0);
  240. height = Math.Min (Math.Min (Suggestions.Count, MaxHeight), LastPopupPos.Value.Y - 1);
  241. }
  242. }
  243. }
  244. else
  245. {
  246. // don't overspill vertically
  247. height = Math.Min (Math.Min (_top.Viewport.Height - HostControl.Frame.Bottom, MaxHeight), Suggestions.Count);
  248. // There is no space below, lets see if can popup on top
  249. if (height < Suggestions.Count && HostControl.Frame.Y - _top.Frame.Y >= height)
  250. {
  251. // Verifies that the upper limit available is greater than the lower limit
  252. if (HostControl.Frame.Y > _top.Viewport.Height - HostControl.Frame.Y)
  253. {
  254. renderAt.Y = Math.Max (HostControl.Frame.Y - Math.Min (Suggestions.Count, MaxHeight), 0);
  255. height = Math.Min (Math.Min (Suggestions.Count, MaxHeight), HostControl.Frame.Y);
  256. }
  257. }
  258. else
  259. {
  260. renderAt.Y = HostControl.Frame.Bottom;
  261. }
  262. }
  263. if (ScrollOffset > Suggestions.Count - height)
  264. {
  265. ScrollOffset = 0;
  266. }
  267. Suggestion [] toRender = Suggestions.Skip (ScrollOffset).Take (height).ToArray ();
  268. _toRenderLength = toRender.Length;
  269. if (toRender.Length == 0)
  270. {
  271. return;
  272. }
  273. width = Math.Min (MaxWidth, toRender.Max (s => s.Title.Length));
  274. if (PopupInsideContainer)
  275. {
  276. // don't overspill horizontally, let's see if it can be displayed on the left
  277. if (width > HostControl.Viewport.Width - renderAt.X)
  278. {
  279. // Verifies that the left limit available is greater than the right limit
  280. if (renderAt.X > HostControl.Viewport.Width - renderAt.X)
  281. {
  282. renderAt.X -= Math.Min (width, LastPopupPos.Value.X);
  283. width = Math.Min (width, LastPopupPos.Value.X);
  284. }
  285. else
  286. {
  287. width = Math.Min (width, HostControl.Viewport.Width - renderAt.X);
  288. }
  289. }
  290. }
  291. else
  292. {
  293. // don't overspill horizontally, let's see if it can be displayed on the left
  294. if (width > _top.Viewport.Width - (renderAt.X + HostControl.Frame.X))
  295. {
  296. // Verifies that the left limit available is greater than the right limit
  297. if (renderAt.X + HostControl.Frame.X > _top.Viewport.Width - (renderAt.X + HostControl.Frame.X))
  298. {
  299. renderAt.X -= Math.Min (width, LastPopupPos.Value.X);
  300. width = Math.Min (width, LastPopupPos.Value.X);
  301. }
  302. else
  303. {
  304. width = Math.Min (width, _top.Viewport.Width - renderAt.X);
  305. }
  306. }
  307. }
  308. if (PopupInsideContainer)
  309. {
  310. _popup.Frame = new (
  311. new (HostControl.Frame.X + renderAt.X, HostControl.Frame.Y + renderAt.Y),
  312. new (width, height)
  313. );
  314. }
  315. else
  316. {
  317. _popup.Frame = new (
  318. renderAt with { X = HostControl.Frame.X + renderAt.X },
  319. new (width, height)
  320. );
  321. }
  322. _popup.Move (0, 0);
  323. for (var i = 0; i < toRender.Length; i++)
  324. {
  325. if (i == SelectedIdx - ScrollOffset)
  326. {
  327. _popup.SetAttribute (ColorScheme.Focus);
  328. }
  329. else
  330. {
  331. _popup.SetAttribute (ColorScheme.Normal);
  332. }
  333. _popup.Move (0, i);
  334. string text = TextFormatter.ClipOrPad (toRender [i].Title, width);
  335. Application.Driver?.AddStr (text);
  336. }
  337. }
  338. /// <summary>
  339. /// When more suggestions are available than can be rendered the user can scroll down the dropdown list. This
  340. /// indicates how far down they have gone
  341. /// </summary>
  342. public virtual int ScrollOffset { get; set; }
  343. /// <summary>
  344. /// Closes the Autocomplete context menu if it is showing and <see cref="IAutocomplete.ClearSuggestions"/>
  345. /// </summary>
  346. protected void Close ()
  347. {
  348. ClearSuggestions ();
  349. Visible = false;
  350. _closed = true;
  351. HostControl?.SetNeedsDraw ();
  352. //RemovePopupFromTop ();
  353. }
  354. /// <summary>Deletes the text backwards before insert the selected text in the <see cref="HostControl"/>.</summary>
  355. protected abstract void DeleteTextBackwards ();
  356. /// <summary>
  357. /// Called when the user confirms a selection at the current cursor location in the <see cref="HostControl"/>. The
  358. /// <paramref name="accepted"/> string is the full autocomplete word to be inserted. Typically, a host will have to
  359. /// remove some characters such that the <paramref name="accepted"/> string completes the word instead of simply being
  360. /// appended.
  361. /// </summary>
  362. /// <param name="accepted"></param>
  363. /// <returns>True if the insertion was possible otherwise false</returns>
  364. protected virtual bool InsertSelection (Suggestion accepted)
  365. {
  366. SetCursorPosition (Context.CursorPosition + accepted.Remove);
  367. // delete the text
  368. for (var i = 0; i < accepted.Remove; i++)
  369. {
  370. DeleteTextBackwards ();
  371. }
  372. InsertText (accepted.Replacement);
  373. return true;
  374. }
  375. /// <summary>Insert the selected text in the <see cref="HostControl"/>.</summary>
  376. /// <param name="accepted"></param>
  377. protected abstract void InsertText (string accepted);
  378. /// <summary>Moves the selection in the Autocomplete context menu down one</summary>
  379. protected void MoveDown ()
  380. {
  381. SelectedIdx++;
  382. if (SelectedIdx > Suggestions.Count - 1)
  383. {
  384. SelectedIdx = 0;
  385. }
  386. EnsureSelectedIdxIsValid ();
  387. HostControl?.SetNeedsDraw ();
  388. }
  389. /// <summary>Moves the selection in the Autocomplete context menu up one</summary>
  390. protected void MoveUp ()
  391. {
  392. SelectedIdx--;
  393. if (SelectedIdx < 0)
  394. {
  395. SelectedIdx = Suggestions.Count - 1;
  396. }
  397. EnsureSelectedIdxIsValid ();
  398. HostControl?.SetNeedsDraw ();
  399. }
  400. /// <summary>Render the current selection in the Autocomplete context menu by the mouse reporting.</summary>
  401. /// <param name="me"></param>
  402. protected void RenderSelectedIdxByMouse (MouseEventArgs me)
  403. {
  404. if (SelectedIdx != me.Position.Y - ScrollOffset)
  405. {
  406. SelectedIdx = me.Position.Y - ScrollOffset;
  407. if (LastPopupPos is { })
  408. {
  409. RenderOverlay ((Point)LastPopupPos);
  410. }
  411. }
  412. }
  413. /// <summary>Reopen the popup after it has been closed.</summary>
  414. /// <returns></returns>
  415. protected bool ReopenSuggestions ()
  416. {
  417. // TODO: Revisit
  418. //GenerateSuggestions ();
  419. if (Suggestions.Count > 0)
  420. {
  421. Visible = true;
  422. _closed = false;
  423. HostControl?.SetNeedsDraw ();
  424. return true;
  425. }
  426. return false;
  427. }
  428. /// <summary>
  429. /// Completes the autocomplete selection process. Called when user hits the
  430. /// <see cref="IAutocomplete.SelectionKey"/>.
  431. /// </summary>
  432. /// <returns></returns>
  433. protected bool Select ()
  434. {
  435. if (SelectedIdx >= 0 && SelectedIdx < Suggestions.Count)
  436. {
  437. Suggestion accepted = Suggestions [SelectedIdx];
  438. return InsertSelection (accepted);
  439. }
  440. return false;
  441. }
  442. /// <summary>Set the cursor position in the <see cref="HostControl"/>.</summary>
  443. /// <param name="column"></param>
  444. protected abstract void SetCursorPosition (int column);
  445. #nullable enable
  446. private Point? LastPopupPos { get; set; }
  447. #nullable restore
  448. private void AddPopupToTop ()
  449. {
  450. if (_popup is null)
  451. {
  452. _popup = new Popup (this)
  453. {
  454. CanFocus = false
  455. };
  456. _top?.Add (_popup);
  457. }
  458. }
  459. private void RemovePopupFromTop ()
  460. {
  461. if (_popup is { } && _top.SubViews.Contains (_popup))
  462. {
  463. _top?.Remove (_popup);
  464. _popup.Dispose ();
  465. _popup = null;
  466. }
  467. }
  468. private void _top_Initialized (object sender, EventArgs e)
  469. {
  470. if (_top is null)
  471. {
  472. _top = sender as View;
  473. }
  474. AddPopupToTop ();
  475. }
  476. private void _top_Removed (object sender, SuperViewChangedEventArgs e)
  477. {
  478. Visible = false;
  479. RemovePopupFromTop ();
  480. }
  481. }