ComboBox.cs 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  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 Action<ustring> SelectedItemChanged;
  24. IList<string> listsource;
  25. IList<string> searchset;
  26. ustring text = "";
  27. TextField search;
  28. ListView listview;
  29. int x;
  30. int y;
  31. int height;
  32. int width;
  33. bool autoHide = true;
  34. /// <summary>
  35. /// Public constructor
  36. /// </summary>
  37. public ComboBox () : base()
  38. {
  39. search = new TextField ("") { LayoutStyle = LayoutStyle.Computed };
  40. listview = new ListView () { LayoutStyle = LayoutStyle.Computed, CanFocus = true /* why? */ };
  41. Initialize ();
  42. }
  43. /// <summary>
  44. /// Public constructor
  45. /// </summary>
  46. /// <param name="x">The x coordinate</param>
  47. /// <param name="y">The y coordinate</param>
  48. /// <param name="w">The width</param>
  49. /// <param name="h">The height</param>
  50. /// <param name="source">Auto completion source</param>
  51. public ComboBox (int x, int y, int w, int h, IList<string> source)
  52. {
  53. SetSource (source);
  54. this.x = x;
  55. this.y = y;
  56. height = h;
  57. width = w;
  58. search = new TextField (x, y, w, "");
  59. listview = new ListView (new Rect (x, y + 1, w, 0), listsource.ToList ()) {
  60. LayoutStyle = LayoutStyle.Computed,
  61. };
  62. Initialize ();
  63. }
  64. private void Initialize()
  65. {
  66. search.Changed += Search_Changed;
  67. listview.SelectedChanged += (object sender, ListViewItemEventArgs e) => {
  68. if(searchset.Count > 0)
  69. SetValue (searchset [listview.SelectedItem]);
  70. };
  71. // TODO: LayoutComplete event breaks cursor up/down. Revert to Application.Loaded
  72. Application.Loaded += (sender, a) => {
  73. // Determine if this view is hosted inside a dialog
  74. for (View view = this.SuperView; view != null; view = view.SuperView) {
  75. if (view is Dialog) {
  76. autoHide = false;
  77. break;
  78. }
  79. }
  80. searchset = autoHide ? new List<string> () : listsource;
  81. // Needs to be re-applied for LayoutStyle.Computed
  82. // If Dim or Pos are null, these are the from the parametrized constructor
  83. if (X == null)
  84. listview.X = x;
  85. if (Y == null)
  86. listview.Y = y + 1;
  87. else
  88. listview.Y = Pos.Bottom (search);
  89. if (Width == null)
  90. listview.Width = CalculateWidth ();
  91. else {
  92. width = GetDimAsInt (Width);
  93. listview.Width = CalculateWidth ();
  94. }
  95. if (Height == null)
  96. listview.Height = CalculatetHeight ();
  97. else {
  98. height = GetDimAsInt (Height);
  99. listview.Height = CalculatetHeight ();
  100. }
  101. if (this.Text != null)
  102. Search_Changed (search, Text);
  103. if (autoHide)
  104. listview.ColorScheme = Colors.Menu;
  105. else
  106. search.ColorScheme = Colors.Menu;
  107. };
  108. search.MouseClick += Search_MouseClick;
  109. this.Add(listview, search);
  110. this.SetFocus(search);
  111. }
  112. /// <summary>
  113. /// Set search list source
  114. /// </summary>
  115. public void SetSource(IList<string> source)
  116. {
  117. listsource = new List<string> (source);
  118. }
  119. private void Search_MouseClick (object sender, MouseEventArgs e)
  120. {
  121. if (e.MouseEvent.Flags != MouseFlags.Button1Clicked)
  122. return;
  123. SuperView.SetFocus ((View)sender);
  124. }
  125. ///<inheritdoc/>
  126. public override bool OnEnter ()
  127. {
  128. if (!search.HasFocus)
  129. this.SetFocus (search);
  130. search.CursorPosition = search.Text.Length;
  131. return true;
  132. }
  133. ///<inheritdoc/>
  134. public override bool ProcessKey(KeyEvent e)
  135. {
  136. if (e.Key == Key.Tab)
  137. {
  138. base.ProcessKey(e);
  139. return false; // allow tab-out to next control
  140. }
  141. if (e.Key == Key.Enter && listview.HasFocus) {
  142. if (listview.Source.Count == 0 || searchset.Count == 0) {
  143. text = "";
  144. return true;
  145. }
  146. SetValue( searchset [listview.SelectedItem]);
  147. search.CursorPosition = search.Text.Length;
  148. Search_Changed (search, search.Text);
  149. Changed?.Invoke (this, text);
  150. searchset.Clear();
  151. listview.Clear ();
  152. listview.Height = 0;
  153. this.SetFocus(search);
  154. return true;
  155. }
  156. if (e.Key == Key.CursorDown && search.HasFocus && listview.SelectedItem == 0 && searchset.Count > 0) { // jump to list
  157. this.SetFocus (listview);
  158. SetValue (searchset [listview.SelectedItem]);
  159. return true;
  160. }
  161. if (e.Key == Key.CursorUp && search.HasFocus) // stop odd behavior on KeyUp when search has focus
  162. return true;
  163. if (e.Key == Key.CursorUp && listview.HasFocus && listview.SelectedItem == 0 && searchset.Count > 0) // jump back to search
  164. {
  165. search.CursorPosition = search.Text.Length;
  166. this.SetFocus (search);
  167. return true;
  168. }
  169. if (e.Key == Key.Esc) {
  170. this.SetFocus (search);
  171. search.Text = text = "";
  172. Changed?.Invoke (this, search.Text);
  173. return true;
  174. }
  175. // Unix emulation
  176. if (e.Key == Key.ControlU)
  177. {
  178. Reset();
  179. return true;
  180. }
  181. return base.ProcessKey(e);
  182. }
  183. /// <summary>
  184. /// The currently selected list item
  185. /// </summary>
  186. public ustring Text
  187. {
  188. get
  189. {
  190. return text;
  191. }
  192. set {
  193. search.Text = text = value;
  194. }
  195. }
  196. private void SetValue(ustring text)
  197. {
  198. search.Changed -= Search_Changed;
  199. this.text = search.Text = text;
  200. search.CursorPosition = 0;
  201. search.Changed += Search_Changed;
  202. }
  203. /// <summary>
  204. /// Reset to full original list
  205. /// </summary>
  206. private void Reset()
  207. {
  208. search.Text = text = "";
  209. Changed?.Invoke (this, search.Text);
  210. searchset = autoHide ? new List<string> () : listsource;
  211. listview.SetSource(searchset.ToList());
  212. listview.Height = CalculatetHeight ();
  213. this.SetFocus(search);
  214. }
  215. private void Search_Changed (object sender, ustring text)
  216. {
  217. if (listsource == null) // Object initialization
  218. return;
  219. if (string.IsNullOrEmpty (search.Text.ToString()))
  220. searchset = autoHide ? new List<string> () : listsource;
  221. else
  222. searchset = listsource.Where (x => x.StartsWith (search.Text.ToString (), StringComparison.CurrentCultureIgnoreCase)).ToList ();
  223. listview.SetSource (searchset.ToList ());
  224. listview.Height = CalculatetHeight ();
  225. listview.Redraw (new Rect (0, 0, width, height)); // for any view behind this
  226. this.SuperView?.BringSubviewToFront (this);
  227. }
  228. /// <summary>
  229. /// Internal height of dynamic search list
  230. /// </summary>
  231. /// <returns></returns>
  232. private int CalculatetHeight ()
  233. {
  234. return Math.Min (height, searchset.Count);
  235. }
  236. /// <summary>
  237. /// Internal width of search list
  238. /// </summary>
  239. /// <returns></returns>
  240. private int CalculateWidth ()
  241. {
  242. return autoHide ? Math.Max (1, width - 1) : width;
  243. }
  244. /// <summary>
  245. /// Get DimAbsolute as integer value
  246. /// </summary>
  247. /// <param name="dim"></param>
  248. /// <returns></returns>
  249. private int GetDimAsInt(Dim dim)
  250. {
  251. if (!(dim is Dim.DimAbsolute))
  252. throw new ArgumentException ("Dim must be an absolute value");
  253. // Anchor in the case of DimAbsolute returns absolute value. No documentation on what Anchor() does so not sure if this will change in the future.
  254. //
  255. // TODO: Does Dim need:-
  256. // public static implicit operator int (Dim d)
  257. //
  258. return dim.Anchor (0);
  259. }
  260. }
  261. }