PopupAutocomplete.cs 13 KB

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