PopupAutocomplete.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585
  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. private void _top_Added (object sender, SuperViewChangedEventArgs e)
  67. {
  68. throw new NotImplementedException ();
  69. }
  70. /// <inheritdoc/>
  71. public override void EnsureSelectedIdxIsValid ()
  72. {
  73. base.EnsureSelectedIdxIsValid ();
  74. // if user moved selection up off top of current scroll window
  75. if (SelectedIdx < ScrollOffset)
  76. {
  77. ScrollOffset = SelectedIdx;
  78. }
  79. // if user moved selection down past bottom of current scroll window
  80. while (_toRenderLength > 0 && SelectedIdx >= ScrollOffset + _toRenderLength)
  81. {
  82. ScrollOffset++;
  83. }
  84. }
  85. /// <summary>
  86. /// Handle mouse events before <see cref="HostControl"/> e.g. to make mouse events like report/click apply to the
  87. /// autocomplete control instead of changing the cursor position in the underlying text view.
  88. /// </summary>
  89. /// <param name="me">The mouse event.</param>
  90. /// <param name="fromHost">If was called from the popup or from the host.</param>
  91. /// <returns><c>true</c>if the mouse can be handled <c>false</c>otherwise.</returns>
  92. public override bool OnMouseEvent (MouseEventArgs me, bool fromHost = false)
  93. {
  94. if (fromHost)
  95. {
  96. if (!Visible)
  97. {
  98. return false;
  99. }
  100. // TODO: Revisit this
  101. //GenerateSuggestions ();
  102. if (Visible && Suggestions.Count == 0)
  103. {
  104. Visible = false;
  105. HostControl?.SetNeedsDraw ();
  106. return true;
  107. }
  108. if (!Visible && Suggestions.Count > 0)
  109. {
  110. Visible = true;
  111. HostControl?.SetNeedsDraw ();
  112. Application.UngrabMouse ();
  113. return false;
  114. }
  115. // not in the popup
  116. if (Visible && HostControl is { })
  117. {
  118. Visible = false;
  119. _closed = false;
  120. }
  121. HostControl?.SetNeedsDraw ();
  122. return false;
  123. }
  124. if (_popup is null || Suggestions.Count == 0)
  125. {
  126. //AddPopupToTop ();
  127. //Debug.Fail ("popup is null");
  128. return false;
  129. }
  130. if (me.Flags == MouseFlags.ReportMousePosition)
  131. {
  132. RenderSelectedIdxByMouse (me);
  133. return true;
  134. }
  135. if (me.Flags == MouseFlags.Button1Clicked)
  136. {
  137. SelectedIdx = me.Position.Y - ScrollOffset;
  138. return Select ();
  139. }
  140. if (me.Flags == MouseFlags.WheeledDown)
  141. {
  142. MoveDown ();
  143. return true;
  144. }
  145. if (me.Flags == MouseFlags.WheeledUp)
  146. {
  147. MoveUp ();
  148. return true;
  149. }
  150. return false;
  151. }
  152. /// <summary>
  153. /// Handle key events before <see cref="HostControl"/> e.g. to make key events like up/down apply to the
  154. /// autocomplete control instead of changing the cursor position in the underlying text view.
  155. /// </summary>
  156. /// <param name="key">The key event.</param>
  157. /// <returns><c>true</c>if the key can be handled <c>false</c>otherwise.</returns>
  158. public override bool ProcessKey (Key key)
  159. {
  160. if (SuggestionGenerator.IsWordChar ((Rune)key))
  161. {
  162. Visible = true;
  163. _closed = false;
  164. return false;
  165. }
  166. if (key == Reopen)
  167. {
  168. Context.Canceled = false;
  169. return ReopenSuggestions ();
  170. }
  171. if (_closed || Suggestions.Count == 0)
  172. {
  173. Visible = false;
  174. if (!_closed)
  175. {
  176. Close ();
  177. }
  178. return false;
  179. }
  180. if (key == Key.CursorDown)
  181. {
  182. MoveDown ();
  183. return true;
  184. }
  185. if (key == Key.CursorUp)
  186. {
  187. MoveUp ();
  188. return true;
  189. }
  190. // TODO : Revisit this
  191. /*if (a.ConsoleDriverKey == Key.CursorLeft || a.ConsoleDriverKey == Key.CursorRight) {
  192. GenerateSuggestions (a.ConsoleDriverKey == Key.CursorLeft ? -1 : 1);
  193. if (Suggestions.Count == 0) {
  194. Visible = false;
  195. if (!closed) {
  196. Close ();
  197. }
  198. }
  199. return false;
  200. }*/
  201. if (key == SelectionKey)
  202. {
  203. return Select ();
  204. }
  205. if (key == CloseKey)
  206. {
  207. Close ();
  208. Context.Canceled = true;
  209. return true;
  210. }
  211. return false;
  212. }
  213. /// <summary>Renders the autocomplete dialog inside or outside the given <see cref="HostControl"/> at the given point.</summary>
  214. /// <param name="renderAt"></param>
  215. public override void RenderOverlay (Point renderAt)
  216. {
  217. if (!Context.Canceled && Suggestions.Count > 0 && !Visible && HostControl?.HasFocus == true)
  218. {
  219. ProcessKey (new (Suggestions [0].Title [0]));
  220. }
  221. else if (!Visible || HostControl?.HasFocus == false || Suggestions.Count == 0)
  222. {
  223. LastPopupPos = null;
  224. Visible = false;
  225. if (Suggestions.Count == 0)
  226. {
  227. Context.Canceled = false;
  228. }
  229. return;
  230. }
  231. LastPopupPos = renderAt;
  232. int height, width;
  233. if (PopupInsideContainer)
  234. {
  235. // don't overspill vertically
  236. height = Math.Min (HostControl.Viewport.Height - renderAt.Y, MaxHeight);
  237. // There is no space below, lets see if can popup on top
  238. if (height < Suggestions.Count && HostControl.Viewport.Height - renderAt.Y >= height)
  239. {
  240. // Verifies that the upper limit available is greater than the lower limit
  241. if (renderAt.Y > HostControl.Viewport.Height - renderAt.Y)
  242. {
  243. renderAt.Y = Math.Max (renderAt.Y - Math.Min (Suggestions.Count + 1, MaxHeight + 1), 0);
  244. height = Math.Min (Math.Min (Suggestions.Count, MaxHeight), LastPopupPos.Value.Y - 1);
  245. }
  246. }
  247. }
  248. else
  249. {
  250. // don't overspill vertically
  251. height = Math.Min (Math.Min (_top.Viewport.Height - HostControl.Frame.Bottom, MaxHeight), Suggestions.Count);
  252. // There is no space below, lets see if can popup on top
  253. if (height < Suggestions.Count && HostControl.Frame.Y - _top.Frame.Y >= height)
  254. {
  255. // Verifies that the upper limit available is greater than the lower limit
  256. if (HostControl.Frame.Y > _top.Viewport.Height - HostControl.Frame.Y)
  257. {
  258. renderAt.Y = Math.Max (HostControl.Frame.Y - Math.Min (Suggestions.Count, MaxHeight), 0);
  259. height = Math.Min (Math.Min (Suggestions.Count, MaxHeight), HostControl.Frame.Y);
  260. }
  261. }
  262. else
  263. {
  264. renderAt.Y = HostControl.Frame.Bottom;
  265. }
  266. }
  267. if (ScrollOffset > Suggestions.Count - height)
  268. {
  269. ScrollOffset = 0;
  270. }
  271. Suggestion [] toRender = Suggestions.Skip (ScrollOffset).Take (height).ToArray ();
  272. _toRenderLength = toRender.Length;
  273. if (toRender.Length == 0)
  274. {
  275. return;
  276. }
  277. width = Math.Min (MaxWidth, toRender.Max (s => s.Title.Length));
  278. if (PopupInsideContainer)
  279. {
  280. // don't overspill horizontally, let's see if it can be displayed on the left
  281. if (width > HostControl.Viewport.Width - renderAt.X)
  282. {
  283. // Verifies that the left limit available is greater than the right limit
  284. if (renderAt.X > HostControl.Viewport.Width - renderAt.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, HostControl.Viewport.Width - renderAt.X);
  292. }
  293. }
  294. }
  295. else
  296. {
  297. // don't overspill horizontally, let's see if it can be displayed on the left
  298. if (width > _top.Viewport.Width - (renderAt.X + HostControl.Frame.X))
  299. {
  300. // Verifies that the left limit available is greater than the right limit
  301. if (renderAt.X + HostControl.Frame.X > _top.Viewport.Width - (renderAt.X + HostControl.Frame.X))
  302. {
  303. renderAt.X -= Math.Min (width, LastPopupPos.Value.X);
  304. width = Math.Min (width, LastPopupPos.Value.X);
  305. }
  306. else
  307. {
  308. width = Math.Min (width, _top.Viewport.Width - renderAt.X);
  309. }
  310. }
  311. }
  312. if (PopupInsideContainer)
  313. {
  314. _popup.Frame = new (
  315. new (HostControl.Frame.X + renderAt.X, HostControl.Frame.Y + renderAt.Y),
  316. new (width, height)
  317. );
  318. }
  319. else
  320. {
  321. _popup.Frame = new (
  322. renderAt with { X = HostControl.Frame.X + renderAt.X },
  323. new (width, height)
  324. );
  325. }
  326. _popup.Move (0, 0);
  327. for (var i = 0; i < toRender.Length; i++)
  328. {
  329. if (i == SelectedIdx - ScrollOffset)
  330. {
  331. _popup.SetAttribute (ColorScheme.Focus);
  332. }
  333. else
  334. {
  335. _popup.SetAttribute (ColorScheme.Normal);
  336. }
  337. _popup.Move (0, i);
  338. string text = TextFormatter.ClipOrPad (toRender [i].Title, width);
  339. Application.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. HostControl?.SetNeedsDraw ();
  356. //RemovePopupFromTop ();
  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. HostControl?.SetNeedsDraw ();
  428. return true;
  429. }
  430. return false;
  431. }
  432. /// <summary>
  433. /// Completes the autocomplete selection process. Called when user hits the
  434. /// <see cref="IAutocomplete.SelectionKey"/>.
  435. /// </summary>
  436. /// <returns></returns>
  437. protected bool Select ()
  438. {
  439. if (SelectedIdx >= 0 && SelectedIdx < Suggestions.Count)
  440. {
  441. Suggestion accepted = Suggestions [SelectedIdx];
  442. return InsertSelection (accepted);
  443. }
  444. return false;
  445. }
  446. /// <summary>Set the cursor position in the <see cref="HostControl"/>.</summary>
  447. /// <param name="column"></param>
  448. protected abstract void SetCursorPosition (int column);
  449. #nullable enable
  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. }