PopupAutocomplete.cs 14 KB

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