AutocompleteBase.cs 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. using System;
  2. using System.Collections.ObjectModel;
  3. using System.Linq;
  4. namespace Terminal.Gui {
  5. /// <summary>
  6. /// Abstract implementation of <see cref="IAutocomplete"/> allows
  7. /// for tailoring how autocomplete is rendered/interacted with.
  8. /// </summary>
  9. public abstract class AutocompleteBase : IAutocomplete {
  10. /// <inheritdoc/>
  11. public abstract View HostControl { get; set; }
  12. /// <inheritdoc/>
  13. public bool PopupInsideContainer { get; set; }
  14. /// <inheritdoc/>
  15. public ISuggestionGenerator SuggestionGenerator { get; set; } = new SingleWordSuggestionGenerator ();
  16. /// <inheritdoc/>
  17. public virtual int MaxWidth { get; set; } = 10;
  18. /// <inheritdoc/>
  19. public virtual int MaxHeight { get; set; } = 6;
  20. /// <inheritdoc/>
  21. /// <inheritdoc/>
  22. public virtual bool Visible { get; set; }
  23. /// <inheritdoc/>
  24. public virtual ReadOnlyCollection<Suggestion> Suggestions { get; set; } = new ReadOnlyCollection<Suggestion> (new Suggestion [0]);
  25. /// <inheritdoc/>
  26. public virtual int SelectedIdx { get; set; }
  27. /// <inheritdoc/>
  28. public abstract ColorScheme ColorScheme { get; set; }
  29. // TODO: Update to use Key instead of KeyCode
  30. /// <inheritdoc/>
  31. public virtual KeyCode SelectionKey { get; set; } = KeyCode.Enter;
  32. // TODO: Update to use Key instead of KeyCode
  33. /// <inheritdoc/>
  34. public virtual KeyCode CloseKey { get; set; } = KeyCode.Esc;
  35. // TODO: Update to use Key instead of KeyCode
  36. /// <inheritdoc/>
  37. public virtual KeyCode Reopen { get; set; } = KeyCode.Space | KeyCode.CtrlMask | KeyCode.AltMask;
  38. /// <inheritdoc/>
  39. public virtual AutocompleteContext Context { get; set; }
  40. /// <inheritdoc/>
  41. public abstract bool MouseEvent (MouseEvent me, bool fromHost = false);
  42. /// <inheritdoc/>
  43. public abstract bool ProcessKey (Key a);
  44. /// <inheritdoc/>
  45. public abstract void RenderOverlay (Point renderAt);
  46. /// <inheritdoc/>>
  47. public virtual void ClearSuggestions ()
  48. {
  49. Suggestions = Enumerable.Empty<Suggestion> ().ToList ().AsReadOnly ();
  50. }
  51. /// <inheritdoc/>
  52. public virtual void GenerateSuggestions (AutocompleteContext context)
  53. {
  54. Suggestions = SuggestionGenerator.GenerateSuggestions (context).ToList ().AsReadOnly ();
  55. EnsureSelectedIdxIsValid ();
  56. }
  57. /// <summary>
  58. /// Updates <see cref="SelectedIdx"/> to be a valid index within <see cref="Suggestions"/>
  59. /// </summary>
  60. public virtual void EnsureSelectedIdxIsValid ()
  61. {
  62. SelectedIdx = Math.Max (0, Math.Min (Suggestions.Count - 1, SelectedIdx));
  63. }
  64. }
  65. }