PopupAutocomplete.cs 16 KB

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