PopupAutocomplete.cs 16 KB

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