AppendAutocomplete.cs 3.3 KB

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