ComboBox.cs 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. //
  2. // ComboBox.cs: ComboBox control
  3. //
  4. // Authors:
  5. // Ross Ferguson ([email protected])
  6. //
  7. using System;
  8. using System.Collections;
  9. using System.Collections.Generic;
  10. using System.Linq;
  11. using NStack;
  12. namespace Terminal.Gui {
  13. /// <summary>
  14. /// ComboBox control
  15. /// </summary>
  16. public class ComboBox : View {
  17. IListDataSource source;
  18. /// <summary>
  19. /// Gets or sets the <see cref="IListDataSource"/> backing this <see cref="ComboBox"/>, enabling custom rendering.
  20. /// </summary>
  21. /// <value>The source.</value>
  22. /// <remarks>
  23. /// Use <see cref="SetSource"/> to set a new <see cref="IList"/> source.
  24. /// </remarks>
  25. public IListDataSource Source {
  26. get => source;
  27. set {
  28. source = value;
  29. Search_Changed ("");
  30. SetNeedsDisplay ();
  31. }
  32. }
  33. /// <summary>
  34. /// Sets the source of the <see cref="ComboBox"/> to an <see cref="IList"/>.
  35. /// </summary>
  36. /// <value>An object implementing the IList interface.</value>
  37. /// <remarks>
  38. /// Use the <see cref="Source"/> property to set a new <see cref="IListDataSource"/> source and use custome rendering.
  39. /// </remarks>
  40. public void SetSource (IList source)
  41. {
  42. if (source == null)
  43. Source = null;
  44. else {
  45. Source = MakeWrapper (source);
  46. }
  47. }
  48. /// <summary>
  49. /// Changed event, raised when the selection has been confirmed.
  50. /// </summary>
  51. /// <remarks>
  52. /// Client code can hook up to this event, it is
  53. /// raised when the selection has been confirmed.
  54. /// </remarks>
  55. public event EventHandler<ustring> SelectedItemChanged;
  56. IList searchset;
  57. ustring text = "";
  58. readonly TextField search;
  59. readonly ListView listview;
  60. int height;
  61. int width;
  62. bool autoHide = true;
  63. /// <summary>
  64. /// Public constructor
  65. /// </summary>
  66. public ComboBox () : base()
  67. {
  68. search = new TextField ("");
  69. listview = new ListView () { LayoutStyle = LayoutStyle.Computed, CanFocus = true };
  70. Initialize ();
  71. }
  72. /// <summary>
  73. /// Public constructor
  74. /// </summary>
  75. /// <param name="rect"></param>
  76. /// <param name="source"></param>
  77. public ComboBox (Rect rect, IList source) : base (rect)
  78. {
  79. this.height = rect.Height;
  80. this.width = rect.Width;
  81. search = new TextField ("") { Width = width };
  82. listview = new ListView (rect, source) { LayoutStyle = LayoutStyle.Computed };
  83. Initialize ();
  84. SetSource (source);
  85. }
  86. static IListDataSource MakeWrapper (IList source)
  87. {
  88. return new ListWrapper (source);
  89. }
  90. private void Initialize()
  91. {
  92. ColorScheme = Colors.Base;
  93. search.TextChanged += Search_Changed;
  94. listview.OpenSelectedItem += (ListViewItemEventArgs a) => Selected();
  95. // On resize
  96. LayoutComplete += (LayoutEventArgs a) => {
  97. search.Width = Bounds.Width;
  98. listview.Width = autoHide ? Bounds.Width - 1 : Bounds.Width;
  99. listview.Height = CalculatetHeight ();
  100. };
  101. listview.SelectedItemChanged += (ListViewItemEventArgs e) => {
  102. if(searchset.Count > 0)
  103. SetValue ((string)searchset [listview.SelectedItem]);
  104. };
  105. Application.Loaded += (Application.ResizedEventArgs a) => {
  106. // Determine if this view is hosted inside a dialog
  107. for (View view = this.SuperView; view != null; view = view.SuperView) {
  108. if (view is Dialog) {
  109. autoHide = false;
  110. break;
  111. }
  112. }
  113. ResetSearchSet ();
  114. ColorScheme = autoHide ? Colors.Base : ColorScheme = null;
  115. listview.Y = Pos.Bottom (search);
  116. if (Width != null && width == 0) // new ComboBox() { Width =
  117. width = Bounds.Width;
  118. search.Width = width;
  119. listview.Width = CalculateWidth ();
  120. if (Height != null && height == 0) // new ComboBox() { Height =
  121. height = Bounds.Height;
  122. listview.Height = CalculatetHeight ();
  123. SetNeedsLayout ();
  124. if (this.Text != null)
  125. Search_Changed (Text);
  126. if (autoHide)
  127. listview.ColorScheme = Colors.Menu;
  128. else
  129. search.ColorScheme = Colors.Menu;
  130. };
  131. search.MouseClick += Search_MouseClick;
  132. this.Add(listview, search);
  133. this.SetFocus(search);
  134. }
  135. private void Search_MouseClick (MouseEventArgs e)
  136. {
  137. if (e.MouseEvent.Flags != MouseFlags.Button1Clicked)
  138. return;
  139. SuperView.SetFocus (search);
  140. }
  141. ///<inheritdoc/>
  142. public override bool OnEnter ()
  143. {
  144. if (!search.HasFocus)
  145. this.SetFocus (search);
  146. search.CursorPosition = search.Text.Length;
  147. return true;
  148. }
  149. /// <summary>
  150. /// Invokes the SelectedChanged event if it is defined.
  151. /// </summary>
  152. /// <returns></returns>
  153. public virtual bool OnSelectedChanged ()
  154. {
  155. // Note: Cannot rely on "listview.SelectedItem != lastSelectedItem" because the list is dynamic.
  156. // So we cannot optimize. Ie: Don't call if not changed
  157. SelectedItemChanged?.Invoke (this, search.Text);
  158. return true;
  159. }
  160. ///<inheritdoc/>
  161. public override bool ProcessKey(KeyEvent e)
  162. {
  163. if (e.Key == Key.Tab) {
  164. base.ProcessKey(e);
  165. return false; // allow tab-out to next control
  166. }
  167. if (e.Key == Key.Enter && listview.HasFocus) {
  168. Selected ();
  169. return true;
  170. }
  171. if (e.Key == Key.CursorDown && search.HasFocus && listview.SelectedItem == 0 && searchset.Count > 0) { // jump to list
  172. this.SetFocus (listview);
  173. SetValue ((string)searchset [listview.SelectedItem]);
  174. return true;
  175. }
  176. if (e.Key == Key.CursorUp && search.HasFocus) // stop odd behavior on KeyUp when search has focus
  177. return true;
  178. if (e.Key == Key.CursorUp && listview.HasFocus && listview.SelectedItem == 0 && searchset.Count > 0) // jump back to search
  179. {
  180. search.CursorPosition = search.Text.Length;
  181. this.SetFocus (search);
  182. return true;
  183. }
  184. if (e.Key == Key.Esc) {
  185. this.SetFocus (search);
  186. search.Text = text = "";
  187. OnSelectedChanged ();
  188. return true;
  189. }
  190. // Unix emulation
  191. if (e.Key == Key.ControlU)
  192. {
  193. Reset();
  194. return true;
  195. }
  196. return base.ProcessKey(e);
  197. }
  198. /// <summary>
  199. /// The currently selected list item
  200. /// </summary>
  201. public ustring Text
  202. {
  203. get
  204. {
  205. return text;
  206. }
  207. set {
  208. search.Text = text = value;
  209. }
  210. }
  211. private void SetValue(ustring text)
  212. {
  213. search.TextChanged -= Search_Changed;
  214. this.text = search.Text = text;
  215. search.CursorPosition = 0;
  216. search.TextChanged += Search_Changed;
  217. }
  218. private void Selected()
  219. {
  220. if (listview.Source.Count == 0 || searchset.Count == 0) {
  221. text = "";
  222. return;
  223. }
  224. SetValue ((string)searchset [listview.SelectedItem]);
  225. search.CursorPosition = search.Text.Length;
  226. Search_Changed (search.Text);
  227. Reset (keepSearchText: true);
  228. }
  229. /// <summary>
  230. /// Reset to full original list
  231. /// </summary>
  232. private void Reset(bool keepSearchText = false)
  233. {
  234. if(!keepSearchText)
  235. search.Text = text = "";
  236. OnSelectedChanged ();
  237. ResetSearchSet ();
  238. listview.SetSource(searchset);
  239. listview.Height = CalculatetHeight ();
  240. this.SetFocus(search);
  241. }
  242. private void ResetSearchSet()
  243. {
  244. if (autoHide) {
  245. if (searchset == null)
  246. searchset = new List<string> ();
  247. else
  248. searchset.Clear ();
  249. } else
  250. searchset = source.ToList ();
  251. }
  252. private void Search_Changed (ustring text)
  253. {
  254. if (source == null) // Object initialization
  255. return;
  256. if (string.IsNullOrEmpty (search.Text.ToString ()))
  257. ResetSearchSet ();
  258. else
  259. searchset = source.ToList().Cast<string>().Where (x => x.StartsWith (search.Text.ToString (), StringComparison.CurrentCultureIgnoreCase)).ToList();
  260. listview.SetSource (searchset);
  261. listview.Height = CalculatetHeight ();
  262. listview.Redraw (new Rect (0, 0, width, height)); // for any view behind this
  263. this.SuperView?.BringSubviewToFront (this);
  264. }
  265. /// <summary>
  266. /// Internal width of search list
  267. /// </summary>
  268. /// <returns></returns>
  269. private int CalculateWidth ()
  270. {
  271. return autoHide ? Math.Max (1, width - 1) : width;
  272. }
  273. /// <summary>
  274. /// Internal height of dynamic search list
  275. /// </summary>
  276. /// <returns></returns>
  277. private int CalculatetHeight ()
  278. {
  279. var h = (Height is Dim.DimAbsolute) ? height : Bounds.Height;
  280. return Math.Min (Math.Max (0, h - 1), searchset?.Count ?? 0);
  281. }
  282. }
  283. }