123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141 |
- using System.Collections.Generic;
- using System.Collections.ObjectModel;
- using System.IO;
- namespace Terminal.Gui {
- /// <summary>
- /// Autocomplete for a <see cref="TextField"/> which shows suggestions within the box.
- /// Displayed suggestions can be completed using the tab key.
- /// </summary>
- public class AppendAutocomplete : AutocompleteBase {
- private int? currentFragment = null;
- private string [] validFragments = new string [0];
- private TextField textField;
- public override View HostControl { get => textField; set => textField = (TextField)value; }
- public override ColorScheme ColorScheme { get; set; }
- public override void ClearSuggestions ()
- {
- this.currentFragment = null;
- this.validFragments = new string [0];
- textField.SetNeedsDisplay ();
- }
- public override void GenerateSuggestions (int columnOffset = 0)
- {
- validFragments = new string []{ "fish", "flipper", "fun" };
- }
- public override bool MouseEvent (MouseEvent me, bool fromHost = false)
- {
- return false;
- }
- public override bool ProcessKey (KeyEvent kb)
- {
- var key = kb.Key;
- if (key == SelectionKey) {
- return this.AcceptSelectionIfAny ();
- } else
- if (key == Key.CursorUp) {
- return this.CycleSuggestion (1);
- } else
- if (key == Key.CursorDown) {
- return this.CycleSuggestion (-1);
- }
- return false;
- }
- public override void RenderOverlay (Point renderAt)
- {
- if (!this.MakingSuggestion ()) {
- return;
- }
- // draw it like its selected even though its not
- Application.Driver.SetAttribute (new Attribute (Color.DarkGray, textField.ColorScheme.Focus.Background));
- textField.Move (textField.Text.Length, 0);
- Application.Driver.AddStr (this.validFragments [this.currentFragment.Value]);
- }
- public AppendAutocomplete (TextField textField)
- {
- this.textField = textField;
- }
- /// <summary>
- /// Accepts the current autocomplete suggestion displaying in the text box.
- /// Returns true if a valid suggestion was being rendered and acceptable or
- /// false if no suggestion was showing.
- /// </summary>
- /// <returns></returns>
- internal bool AcceptSelectionIfAny ()
- {
- if (this.MakingSuggestion ()) {
- textField.Text += this.validFragments [this.currentFragment.Value];
- this.MoveCursorToEnd ();
- this.ClearSuggestions ();
- return true;
- }
- return false;
- }
- internal void MoveCursorToEnd ()
- {
- textField.ClearAllSelection ();
- textField.CursorPosition = textField.Text.Length;
- }
- internal void SetTextTo (FileSystemInfo fileSystemInfo)
- {
- var newText = fileSystemInfo.FullName;
- if (fileSystemInfo is DirectoryInfo) {
- newText += System.IO.Path.DirectorySeparatorChar;
- }
- textField.Text = newText;
- this.MoveCursorToEnd ();
- }
- internal bool CursorIsAtEnd ()
- {
- return textField.CursorPosition == textField.Text.Length;
- }
- /// <summary>
- /// Returns true if there is a suggestion that can be made and the control
- /// is in a state where user would expect to see auto-complete (i.e. focused and
- /// cursor in right place).
- /// </summary>
- /// <returns></returns>
- private bool MakingSuggestion ()
- {
- return this.currentFragment != null && textField.HasFocus && this.CursorIsAtEnd ();
- }
- private bool CycleSuggestion (int direction)
- {
- if (this.currentFragment == null || this.validFragments.Length <= 1) {
- return false;
- }
- this.currentFragment = (this.currentFragment + direction) % this.validFragments.Length;
- if (this.currentFragment < 0) {
- this.currentFragment = this.validFragments.Length - 1;
- }
- textField.SetNeedsDisplay ();
- return true;
- }
- protected override string GetCurrentWord (int columnOffset = 0)
- {
- throw new System.NotImplementedException ();
- }
- }
- }
|