using System; using System.Collections.ObjectModel; using System.Linq; namespace Terminal.Gui { /// /// Abstract implementation of allows /// for tailoring how autocomplete is rendered/interacted with. /// public abstract class AutocompleteBase : IAutocomplete { /// public abstract View HostControl { get; set; } /// public bool PopupInsideContainer { get; set; } /// public ISuggestionGenerator SuggestionGenerator { get; set; } = new SingleWordSuggestionGenerator (); /// public virtual int MaxWidth { get; set; } = 10; /// public virtual int MaxHeight { get; set; } = 6; /// /// public virtual bool Visible { get; set; } /// public virtual ReadOnlyCollection Suggestions { get; set; } = new ReadOnlyCollection (new Suggestion [0]); /// public virtual int SelectedIdx { get; set; } /// public abstract ColorScheme ColorScheme { get; set; } // TODO: Update to use Key instead of KeyCode /// public virtual KeyCode SelectionKey { get; set; } = KeyCode.Enter; // TODO: Update to use Key instead of KeyCode /// public virtual KeyCode CloseKey { get; set; } = KeyCode.Esc; // TODO: Update to use Key instead of KeyCode /// public virtual KeyCode Reopen { get; set; } = KeyCode.Space | KeyCode.CtrlMask | KeyCode.AltMask; /// public virtual AutocompleteContext Context { get; set; } /// public abstract bool MouseEvent (MouseEvent me, bool fromHost = false); /// public abstract bool ProcessKey (Key a); /// public abstract void RenderOverlay (Point renderAt); /// > public virtual void ClearSuggestions () { Suggestions = Enumerable.Empty ().ToList ().AsReadOnly (); } /// public virtual void GenerateSuggestions (AutocompleteContext context) { Suggestions = SuggestionGenerator.GenerateSuggestions (context).ToList ().AsReadOnly (); EnsureSelectedIdxIsValid (); } /// /// Updates to be a valid index within /// public virtual void EnsureSelectedIdxIsValid () { SelectedIdx = Math.Max (0, Math.Min (Suggestions.Count - 1, SelectedIdx)); } } }