TextFieldAutoComplete.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. //
  2. // TextFieldAutoComplete.cs: TextField with AutoComplete
  3. //
  4. // Author:
  5. // Ross Ferguson ([email protected])
  6. //
  7. using System;
  8. using System.Linq;
  9. using System.Collections.Generic;
  10. using NStack;
  11. namespace Terminal.Gui {
  12. /// <summary>
  13. /// TextField with AutoComplete
  14. /// </summary>
  15. public class TextFieldAutoComplete : View {
  16. /// <summary>
  17. /// Changed event, raised when the selection has been confirmed.
  18. /// </summary>
  19. /// <remarks>
  20. /// Client code can hook up to this event, it is
  21. /// raised when the selection has been confirmed.
  22. /// </remarks>
  23. public event EventHandler<ustring> Changed;
  24. readonly IList<string> listsource;
  25. IList<string> searchset;
  26. ustring text = "";
  27. readonly TextField search;
  28. readonly ListView listview;
  29. readonly int height;
  30. readonly int width;
  31. readonly bool isAutoHide;
  32. /// <summary>
  33. /// Public constructor
  34. /// </summary>
  35. /// <param name="x">The x coordinate</param>
  36. /// <param name="y">The y coordinate</param>
  37. /// <param name="w">The width</param>
  38. /// <param name="h">The height</param>
  39. /// <param name="source">Auto completetion source</param>
  40. /// <param name="autoHide">Completetion list hidden until start of typing. Use when hosting in Window as opposed to a Dialog</param>
  41. public TextFieldAutoComplete(int x, int y, int w, int h, IList<string> source, bool autoHide = false)
  42. {
  43. listsource = new List<string>(source);
  44. isAutoHide = autoHide;
  45. searchset = isAutoHide ? new List<string> () : listsource;
  46. height = h;
  47. width = w;
  48. isAutoHide = autoHide;
  49. search = new TextField(x, y, w, "");
  50. search.Changed += Search_Changed;
  51. listview = new ListView(new Rect(x, y + 1, w, CalculatetHeight()), listsource.ToList())
  52. {
  53. LayoutStyle = LayoutStyle.Computed,
  54. ColorScheme = Colors.Dialog
  55. };
  56. listview.SelectedChanged += () => {
  57. SetValue (searchset [listview.SelectedItem]);
  58. };
  59. // Needs to be re-applied for LayoutStyle.Computed
  60. listview.X = x;
  61. listview.Y = y + 1;
  62. listview.Width = w;
  63. listview.Height = CalculatetHeight ();
  64. this.Add(listview);
  65. this.Add(search);
  66. this.SetFocus(search);
  67. }
  68. public new Dim Width
  69. {
  70. get { return base.Width; }
  71. set { base.Width = value; }
  72. }
  73. public new Dim Height
  74. {
  75. get { return base.Height-1; }
  76. set { base.Width = value+1; }
  77. }
  78. public override bool OnEnter ()
  79. {
  80. if (!search.HasFocus)
  81. this.SetFocus (search);
  82. search.CursorPosition = search.Text.Length;
  83. return true;
  84. }
  85. public override bool ProcessKey(KeyEvent e)
  86. {
  87. if (e.Key == Key.Tab)
  88. {
  89. base.ProcessKey(e);
  90. return false; // allow tab-out to next control
  91. }
  92. if (e.Key == Key.Enter && listview.HasFocus) {
  93. if (listview.Source.Count == 0 || searchset.Count == 0) {
  94. text = "";
  95. return true;
  96. }
  97. SetValue( searchset [listview.SelectedItem]);
  98. search.CursorPosition = search.Text.Length;
  99. Changed?.Invoke (this, text);
  100. searchset.Clear();
  101. listview.SetSource(new List<string> ());
  102. listview.Height = 0;
  103. this.SetFocus(search);
  104. return true;
  105. }
  106. if (e.Key == Key.CursorDown && search.HasFocus && listview.SelectedItem == 0) { // jump to list
  107. this.SetFocus (listview);
  108. SetValue (searchset [listview.SelectedItem]);
  109. return true;
  110. }
  111. if (e.Key == Key.CursorUp && listview.HasFocus && listview.SelectedItem == 0) // jump back to search
  112. {
  113. search.CursorPosition = search.Text.Length;
  114. this.SetFocus (search);
  115. return true;
  116. }
  117. if (e.Key == Key.Esc) {
  118. this.SetFocus (search);
  119. search.Text = text = "";
  120. Changed?.Invoke (this, search.Text);
  121. return true;
  122. }
  123. // Unix emulation
  124. if (e.Key == Key.ControlU)
  125. {
  126. Reset();
  127. return true;
  128. }
  129. return base.ProcessKey(e);
  130. }
  131. /// <summary>
  132. /// The currenlty selected list item
  133. /// </summary>
  134. public ustring Text
  135. {
  136. get
  137. {
  138. return text;
  139. }
  140. set {
  141. search.Text = text = value;
  142. }
  143. }
  144. private void SetValue(ustring text)
  145. {
  146. search.Changed -= Search_Changed;
  147. this.text = search.Text = text;
  148. search.CursorPosition = 0;
  149. search.Changed += Search_Changed;
  150. }
  151. /// <summary>
  152. /// Reset to full original list
  153. /// </summary>
  154. private void Reset()
  155. {
  156. search.Text = text = "";
  157. Changed?.Invoke (this, search.Text);
  158. searchset = isAutoHide ? new List<string> () : listsource;
  159. listview.SetSource(searchset.ToList());
  160. listview.Height = CalculatetHeight ();
  161. this.SetFocus(search);
  162. }
  163. private void Search_Changed (object sender, ustring text)
  164. {
  165. if (string.IsNullOrEmpty (search.Text.ToString())) {
  166. searchset = isAutoHide ? new List<string> () : listsource;
  167. }
  168. else
  169. searchset = listsource.Where (x => x.StartsWith (search.Text.ToString (), StringComparison.CurrentCultureIgnoreCase)).ToList ();
  170. listview.SetSource (searchset.ToList ());
  171. listview.Height = CalculatetHeight ();
  172. listview.Redraw (new Rect (0, 0, width, height)); // for any view behind this
  173. }
  174. /// <summary>
  175. /// Internal height of dynamic search list
  176. /// </summary>
  177. /// <returns></returns>
  178. private int CalculatetHeight ()
  179. {
  180. return Math.Min (height, searchset.Count);
  181. }
  182. }
  183. }