AutocompleteBase.cs 2.4 KB

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