ComboBox.cs 5.1 KB

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