AppendAutocomplete.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. using System.Collections.Generic;
  2. using System.Collections.ObjectModel;
  3. using System.IO;
  4. namespace Terminal.Gui {
  5. /// <summary>
  6. /// Autocomplete for a <see cref="TextField"/> which shows suggestions within the box.
  7. /// Displayed suggestions can be completed using the tab key.
  8. /// </summary>
  9. public class AppendAutocomplete : IAutocomplete {
  10. private int? currentFragment = null;
  11. private string [] validFragments = new string [0];
  12. private TextField textField;
  13. public View HostControl { get => textField; set => textField = (TextField)value; }
  14. public bool PopupInsideContainer { get; set; }
  15. public int MaxWidth { get; set; }
  16. public int MaxHeight { get; set; }
  17. public bool Visible { get; set; }
  18. public ReadOnlyCollection<string> Suggestions { get; set; }
  19. public List<string> AllSuggestions { get; set; }
  20. public int SelectedIdx { get; set; }
  21. public ColorScheme ColorScheme { get; set; }
  22. public Key SelectionKey { get; set; } = Key.Tab;
  23. public Key CloseKey { get; set; }
  24. public Key Reopen { get; set; }
  25. public void ClearSuggestions ()
  26. {
  27. this.currentFragment = null;
  28. this.validFragments = new string [0];
  29. textField.SetNeedsDisplay ();
  30. }
  31. public void GenerateSuggestions (int columnOffset = 0)
  32. {
  33. validFragments = new string []{ "fish", "flipper", "fun" };
  34. }
  35. public bool MouseEvent (MouseEvent me, bool fromHost = false)
  36. {
  37. return false;
  38. }
  39. public bool ProcessKey (KeyEvent kb)
  40. {
  41. var key = kb.Key;
  42. if (key == SelectionKey) {
  43. return this.AcceptSelectionIfAny ();
  44. } else
  45. if (key == Key.CursorUp) {
  46. return this.CycleSuggestion (1);
  47. } else
  48. if (key == Key.CursorDown) {
  49. return this.CycleSuggestion (-1);
  50. }
  51. return false;
  52. }
  53. public void RenderOverlay (Point renderAt)
  54. {
  55. if (!this.MakingSuggestion ()) {
  56. return;
  57. }
  58. // draw it like its selected even though its not
  59. Application.Driver.SetAttribute (new Attribute (Color.DarkGray, Color.Black));
  60. textField.Move (textField.Text.Length, 0);
  61. Application.Driver.AddStr (this.validFragments [this.currentFragment.Value]);
  62. }
  63. public AppendAutocomplete (TextField textField)
  64. {
  65. this.textField = textField;
  66. }
  67. /// <summary>
  68. /// Accepts the current autocomplete suggestion displaying in the text box.
  69. /// Returns true if a valid suggestion was being rendered and acceptable or
  70. /// false if no suggestion was showing.
  71. /// </summary>
  72. /// <returns></returns>
  73. internal bool AcceptSelectionIfAny ()
  74. {
  75. if (this.MakingSuggestion ()) {
  76. textField.Text += this.validFragments [this.currentFragment.Value];
  77. this.MoveCursorToEnd ();
  78. this.ClearSuggestions ();
  79. return true;
  80. }
  81. return false;
  82. }
  83. internal void MoveCursorToEnd ()
  84. {
  85. textField.ClearAllSelection ();
  86. textField.CursorPosition = textField.Text.Length;
  87. }
  88. internal void SetTextTo (FileSystemInfo fileSystemInfo)
  89. {
  90. var newText = fileSystemInfo.FullName;
  91. if (fileSystemInfo is DirectoryInfo) {
  92. newText += System.IO.Path.DirectorySeparatorChar;
  93. }
  94. textField.Text = newText;
  95. this.MoveCursorToEnd ();
  96. }
  97. internal bool CursorIsAtEnd ()
  98. {
  99. return textField.CursorPosition == textField.Text.Length;
  100. }
  101. /// <summary>
  102. /// Returns true if there is a suggestion that can be made and the control
  103. /// is in a state where user would expect to see auto-complete (i.e. focused and
  104. /// cursor in right place).
  105. /// </summary>
  106. /// <returns></returns>
  107. private bool MakingSuggestion ()
  108. {
  109. return this.currentFragment != null && textField.HasFocus && this.CursorIsAtEnd ();
  110. }
  111. private bool CycleSuggestion (int direction)
  112. {
  113. if (this.currentFragment == null || this.validFragments.Length <= 1) {
  114. return false;
  115. }
  116. this.currentFragment = (this.currentFragment + direction) % this.validFragments.Length;
  117. if (this.currentFragment < 0) {
  118. this.currentFragment = this.validFragments.Length - 1;
  119. }
  120. textField.SetNeedsDisplay ();
  121. return true;
  122. }
  123. }
  124. }