ComboBox.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. //
  2. // ComboBox.cs: ComboBox control
  3. //
  4. // Authors:
  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. /// ComboBox control
  14. /// </summary>
  15. public class ComboBox : 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. bool autoHide = true;
  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 completion source</param>
  40. public ComboBox(int x, int y, int w, int h, IList<string> source)
  41. {
  42. listsource = new List<string>(source);
  43. height = h;
  44. width = w;
  45. search = new TextField(x, y, w, "");
  46. search.Changed += Search_Changed;
  47. listview = new ListView(new Rect(x, y + 1, w, 0), listsource.ToList())
  48. {
  49. LayoutStyle = LayoutStyle.Computed,
  50. };
  51. listview.SelectedChanged += (object sender, ListViewItemEventArgs e) => {
  52. if(searchset.Count > 0)
  53. SetValue (searchset [listview.SelectedItem]);
  54. };
  55. Application.Loaded += (object sender, Application.ResizedEventArgs e) => {
  56. // Determine if this view is hosted inside a dialog
  57. for (View view = this.SuperView; view != null; view = view.SuperView) {
  58. if (view is Dialog) {
  59. autoHide = false;
  60. break;
  61. }
  62. }
  63. searchset = autoHide ? new List<string> () : listsource;
  64. // Needs to be re-applied for LayoutStyle.Computed
  65. listview.X = x;
  66. listview.Y = y + 1;
  67. listview.Width = CalculateWidth();
  68. listview.Height = CalculatetHeight ();
  69. if (autoHide)
  70. listview.ColorScheme = Colors.Menu;
  71. else
  72. search.ColorScheme = Colors.Menu;
  73. };
  74. search.MouseClick += Search_MouseClick;
  75. this.Add(listview);
  76. this.Add(search);
  77. this.SetFocus(search);
  78. }
  79. private void Search_MouseClick (object sender, MouseEventEventArgs e)
  80. {
  81. if (e.MouseEvent.Flags != MouseFlags.Button1Clicked)
  82. return;
  83. SuperView.SetFocus (((View)sender));
  84. }
  85. ///<inheritdoc cref="OnEnter"/>
  86. public override bool OnEnter ()
  87. {
  88. if (!search.HasFocus)
  89. this.SetFocus (search);
  90. search.CursorPosition = search.Text.Length;
  91. return true;
  92. }
  93. ///<inheritdoc cref="ProcessKey"/>
  94. public override bool ProcessKey(KeyEvent e)
  95. {
  96. if (e.Key == Key.Tab)
  97. {
  98. base.ProcessKey(e);
  99. return false; // allow tab-out to next control
  100. }
  101. if (e.Key == Key.Enter && listview.HasFocus) {
  102. if (listview.Source.Count == 0 || searchset.Count == 0) {
  103. text = "";
  104. return true;
  105. }
  106. SetValue( searchset [listview.SelectedItem]);
  107. search.CursorPosition = search.Text.Length;
  108. Search_Changed (search, search.Text);
  109. Changed?.Invoke (this, text);
  110. searchset.Clear();
  111. listview.SetSource(new List<string> ());
  112. listview.Height = 0;
  113. this.SetFocus(search);
  114. return true;
  115. }
  116. if (e.Key == Key.CursorDown && search.HasFocus && listview.SelectedItem == 0 && searchset.Count > 0) { // jump to list
  117. this.SetFocus (listview);
  118. SetValue (searchset [listview.SelectedItem]);
  119. return true;
  120. }
  121. if (e.Key == Key.CursorUp && listview.HasFocus && listview.SelectedItem == 0 && searchset.Count > 0) // jump back to search
  122. {
  123. search.CursorPosition = search.Text.Length;
  124. this.SetFocus (search);
  125. return true;
  126. }
  127. if (e.Key == Key.Esc) {
  128. this.SetFocus (search);
  129. search.Text = text = "";
  130. Changed?.Invoke (this, search.Text);
  131. return true;
  132. }
  133. // Unix emulation
  134. if (e.Key == Key.ControlU)
  135. {
  136. Reset();
  137. return true;
  138. }
  139. return base.ProcessKey(e);
  140. }
  141. /// <summary>
  142. /// The currently selected list item
  143. /// </summary>
  144. public ustring Text
  145. {
  146. get
  147. {
  148. return text;
  149. }
  150. set {
  151. search.Text = text = value;
  152. }
  153. }
  154. private void SetValue(ustring text)
  155. {
  156. search.Changed -= Search_Changed;
  157. this.text = search.Text = text;
  158. search.CursorPosition = 0;
  159. search.Changed += Search_Changed;
  160. }
  161. /// <summary>
  162. /// Reset to full original list
  163. /// </summary>
  164. private void Reset()
  165. {
  166. search.Text = text = "";
  167. Changed?.Invoke (this, search.Text);
  168. searchset = autoHide ? new List<string> () : listsource;
  169. listview.SetSource(searchset.ToList());
  170. listview.Height = CalculatetHeight ();
  171. this.SetFocus(search);
  172. }
  173. private void Search_Changed (object sender, ustring text)
  174. {
  175. if (string.IsNullOrEmpty (search.Text.ToString()))
  176. searchset = autoHide ? new List<string> () : listsource;
  177. else
  178. searchset = listsource.Where (x => x.StartsWith (search.Text.ToString (), StringComparison.CurrentCultureIgnoreCase)).ToList ();
  179. listview.SetSource (searchset.ToList ());
  180. listview.Height = CalculatetHeight ();
  181. listview.Redraw (new Rect (0, 0, width, height)); // for any view behind this
  182. this.SuperView?.BringSubviewToFront (this);
  183. }
  184. /// <summary>
  185. /// Internal height of dynamic search list
  186. /// </summary>
  187. /// <returns></returns>
  188. private int CalculatetHeight ()
  189. {
  190. return Math.Min (height, searchset.Count);
  191. }
  192. /// <summary>
  193. /// Internal width
  194. /// </summary>
  195. /// <returns></returns>
  196. private int CalculateWidth()
  197. {
  198. return autoHide? Math.Max (1, width - 1) : width;
  199. }
  200. }
  201. }