IAutocomplete.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Collections.ObjectModel;
  4. using Rune = System.Rune;
  5. namespace Terminal.Gui {
  6. /// <summary>
  7. /// A replacement suggestion made by <see cref="IAutocomplete"/>
  8. /// </summary>
  9. public class Suggestion {
  10. /// <summary>
  11. /// The number of characters to remove at the current cursor position
  12. /// before adding the <see cref="Replacement"/>
  13. /// </summary>
  14. public int Remove { get; }
  15. /// <summary>
  16. /// The user visible description for the <see cref="Replacement"/>. Typically
  17. /// this would be the same as <see cref="Replacement"/> but may vary in advanced
  18. /// use cases (e.g. Title= "ctor", Replacement = "MyClass()\n{\n}")
  19. /// </summary>
  20. public string Title {get;}
  21. /// <summary>
  22. /// The replacement text that will be added
  23. /// </summary>
  24. public string Replacement { get; }
  25. /// <summary>
  26. /// Creates a new instance of the <see cref="Suggestion"/> class.
  27. /// </summary>
  28. /// <param name="remove"></param>
  29. /// <param name="replacement"></param>
  30. /// <param name="title">User visible title for the suggestion or null if the same
  31. /// as <paramref name="replacement"/>.</param>
  32. public Suggestion (int remove, string replacement, string title = null)
  33. {
  34. Remove = remove;
  35. Replacement = replacement;
  36. Title = title ?? replacement;
  37. }
  38. }
  39. /// <summary>
  40. /// Renders an overlay on another view at a given point that allows selecting
  41. /// from a range of 'autocomplete' options.
  42. /// </summary>
  43. public interface IAutocomplete {
  44. /// <summary>
  45. /// The host control that will use autocomplete.
  46. /// </summary>
  47. View HostControl { get; set; }
  48. /// <summary>
  49. /// Gets or sets where the popup will be displayed.
  50. /// </summary>
  51. bool PopupInsideContainer { get; set; }
  52. /// <summary>
  53. /// The maximum width of the autocomplete dropdown
  54. /// </summary>
  55. int MaxWidth { get; set; }
  56. /// <summary>
  57. /// The maximum number of visible rows in the autocomplete dropdown to render
  58. /// </summary>
  59. int MaxHeight { get; set; }
  60. /// <summary>
  61. /// True if the autocomplete should be considered open and visible
  62. /// </summary>
  63. bool Visible { get; set; }
  64. /// <summary>
  65. /// The strings that form the current list of suggestions to render
  66. /// based on what the user has typed so far.
  67. /// </summary>
  68. ReadOnlyCollection<Suggestion> Suggestions { get; set; }
  69. /// <summary>
  70. /// The currently selected index into <see cref="Suggestions"/> that the user has highlighted
  71. /// </summary>
  72. int SelectedIdx { get; set; }
  73. /// <summary>
  74. /// The colors to use to render the overlay. Accessing this property before
  75. /// the Application has been initialized will cause an error
  76. /// </summary>
  77. ColorScheme ColorScheme { get; set; }
  78. /// <summary>
  79. /// The key that the user must press to accept the currently selected autocomplete suggestion
  80. /// </summary>
  81. Key SelectionKey { get; set; }
  82. /// <summary>
  83. /// The key that the user can press to close the currently popped autocomplete menu
  84. /// </summary>
  85. Key CloseKey { get; set; }
  86. /// <summary>
  87. /// The key that the user can press to reopen the currently popped autocomplete menu
  88. /// </summary>
  89. Key Reopen { get; set; }
  90. /// <summary>
  91. /// Renders the autocomplete dialog inside the given <see cref="HostControl"/> at the
  92. /// given point.
  93. /// </summary>
  94. /// <param name="renderAt"></param>
  95. void RenderOverlay (Point renderAt);
  96. /// <summary>
  97. /// Handle key events before <see cref="HostControl"/> e.g. to make key events like
  98. /// up/down apply to the autocomplete control instead of changing the cursor position in
  99. /// the underlying text view.
  100. /// </summary>
  101. /// <param name="kb">The key event.</param>
  102. /// <returns><c>true</c>if the key can be handled <c>false</c>otherwise.</returns>
  103. bool ProcessKey (KeyEvent kb);
  104. /// <summary>
  105. /// Handle mouse events before <see cref="HostControl"/> e.g. to make mouse events like
  106. /// report/click apply to the autocomplete control instead of changing the cursor position in
  107. /// the underlying text view.
  108. /// </summary>
  109. /// <param name="me">The mouse event.</param>
  110. /// <param name="fromHost">If was called from the popup or from the host.</param>
  111. /// <returns><c>true</c>if the mouse can be handled <c>false</c>otherwise.</returns>
  112. bool MouseEvent (MouseEvent me, bool fromHost = false);
  113. /// <summary>
  114. /// Clears <see cref="Suggestions"/>
  115. /// </summary>
  116. void ClearSuggestions ();
  117. ISuggestionGenerator SuggestionGenerator {get;set;}
  118. /// <summary>
  119. /// Populates <see cref="Suggestions"/> with all <see cref="Suggestion"/>
  120. /// proposed by <see cref="SuggestionGenerator"/> at the given <paramref name="idx"/>
  121. /// of <paramref name="currentLine"/>
  122. /// </summary>
  123. void GenerateSuggestions (List<Rune> currentLine, int idx);
  124. }
  125. }