AutocompleteBase.cs 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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. /// <inheritdoc/>
  29. public virtual Key SelectionKey { get; set; } = Key.Enter;
  30. /// <inheritdoc/>
  31. public virtual Key CloseKey { get; set; } = Key.Esc;
  32. /// <inheritdoc/>
  33. public virtual Key Reopen { get; set; } = Key.Space.WithCtrl.WithAlt;
  34. /// <inheritdoc/>
  35. public virtual AutocompleteContext Context { get; set; }
  36. /// <inheritdoc/>
  37. public abstract bool OnMouseEvent (MouseEventArgs me, bool fromHost = false);
  38. /// <inheritdoc/>
  39. public abstract bool ProcessKey (Key a);
  40. /// <inheritdoc/>
  41. public abstract void RenderOverlay (Point renderAt);
  42. /// <inheritdoc/>
  43. /// >
  44. public virtual void ClearSuggestions () { Suggestions = Enumerable.Empty<Suggestion> ().ToList ().AsReadOnly (); }
  45. /// <inheritdoc/>
  46. public virtual void GenerateSuggestions (AutocompleteContext context)
  47. {
  48. Suggestions = SuggestionGenerator.GenerateSuggestions (context).ToList ().AsReadOnly ();
  49. EnsureSelectedIdxIsValid ();
  50. }
  51. /// <summary>Updates <see cref="SelectedIdx"/> to be a valid index within <see cref="Suggestions"/></summary>
  52. public virtual void EnsureSelectedIdxIsValid () { SelectedIdx = Math.Max (0, Math.Min (Suggestions.Count - 1, SelectedIdx)); }
  53. }