ComboBox.cs 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  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="text"></param>
  76. public ComboBox (ustring text) : base ()
  77. {
  78. search = new TextField ("");
  79. listview = new ListView () { LayoutStyle = LayoutStyle.Computed, CanFocus = true };
  80. Initialize ();
  81. Text = text;
  82. }
  83. /// <summary>
  84. /// Public constructor
  85. /// </summary>
  86. /// <param name="rect"></param>
  87. /// <param name="source"></param>
  88. public ComboBox (Rect rect, IList source) : base (rect)
  89. {
  90. this.height = rect.Height;
  91. this.width = rect.Width;
  92. search = new TextField ("") { Width = width };
  93. listview = new ListView (rect, source) { LayoutStyle = LayoutStyle.Computed };
  94. Initialize ();
  95. SetSource (source);
  96. }
  97. static IListDataSource MakeWrapper (IList source)
  98. {
  99. return new ListWrapper (source);
  100. }
  101. private void Initialize()
  102. {
  103. ColorScheme = Colors.Base;
  104. search.TextChanged += Search_Changed;
  105. listview.OpenSelectedItem += (ListViewItemEventArgs a) => Selected();
  106. // On resize
  107. LayoutComplete += (LayoutEventArgs a) => {
  108. search.Width = Bounds.Width;
  109. listview.Width = autoHide ? Bounds.Width - 1 : Bounds.Width;
  110. listview.Height = CalculatetHeight ();
  111. };
  112. listview.SelectedItemChanged += (ListViewItemEventArgs e) => {
  113. if(searchset.Count > 0)
  114. SetValue ((string)searchset [listview.SelectedItem]);
  115. };
  116. Application.Loaded += (Application.ResizedEventArgs a) => {
  117. // Determine if this view is hosted inside a dialog
  118. for (View view = this.SuperView; view != null; view = view.SuperView) {
  119. if (view is Dialog) {
  120. autoHide = false;
  121. break;
  122. }
  123. }
  124. ResetSearchSet ();
  125. ColorScheme = autoHide ? Colors.Base : ColorScheme = null;
  126. listview.Y = Pos.Bottom (search);
  127. if (Width != null && width == 0) // new ComboBox() { Width =
  128. width = Bounds.Width;
  129. search.Width = width;
  130. listview.Width = CalculateWidth ();
  131. if (Height != null && height == 0) // new ComboBox() { Height =
  132. height = Bounds.Height;
  133. listview.Height = CalculatetHeight ();
  134. SetNeedsLayout ();
  135. if (this.Text != null)
  136. Search_Changed (Text);
  137. if (autoHide)
  138. listview.ColorScheme = Colors.Menu;
  139. else
  140. search.ColorScheme = Colors.Menu;
  141. };
  142. search.MouseClick += Search_MouseClick;
  143. this.Add(listview, search);
  144. this.SetFocus(search);
  145. }
  146. private void Search_MouseClick (MouseEventArgs e)
  147. {
  148. if (e.MouseEvent.Flags != MouseFlags.Button1Clicked)
  149. return;
  150. SuperView.SetFocus (search);
  151. }
  152. ///<inheritdoc/>
  153. public override bool OnEnter ()
  154. {
  155. if (!search.HasFocus)
  156. this.SetFocus (search);
  157. search.CursorPosition = search.Text.Length;
  158. return true;
  159. }
  160. /// <summary>
  161. /// Invokes the SelectedChanged event if it is defined.
  162. /// </summary>
  163. /// <returns></returns>
  164. public virtual bool OnSelectedChanged ()
  165. {
  166. // Note: Cannot rely on "listview.SelectedItem != lastSelectedItem" because the list is dynamic.
  167. // So we cannot optimize. Ie: Don't call if not changed
  168. SelectedItemChanged?.Invoke (this, search.Text);
  169. return true;
  170. }
  171. ///<inheritdoc/>
  172. public override bool ProcessKey(KeyEvent e)
  173. {
  174. if (e.Key == Key.Tab) {
  175. base.ProcessKey(e);
  176. return false; // allow tab-out to next control
  177. }
  178. if (e.Key == Key.Enter && listview.HasFocus) {
  179. Selected ();
  180. return true;
  181. }
  182. if (e.Key == Key.CursorDown && search.HasFocus && listview.SelectedItem == 0 && searchset.Count > 0) { // jump to list
  183. this.SetFocus (listview);
  184. SetValue ((string)searchset [listview.SelectedItem]);
  185. return true;
  186. }
  187. if (e.Key == Key.CursorUp && search.HasFocus) // stop odd behavior on KeyUp when search has focus
  188. return true;
  189. if (e.Key == Key.CursorUp && listview.HasFocus && listview.SelectedItem == 0 && searchset.Count > 0) // jump back to search
  190. {
  191. search.CursorPosition = search.Text.Length;
  192. this.SetFocus (search);
  193. return true;
  194. }
  195. if (e.Key == Key.Esc) {
  196. this.SetFocus (search);
  197. search.Text = text = "";
  198. OnSelectedChanged ();
  199. return true;
  200. }
  201. // Unix emulation
  202. if (e.Key == Key.ControlU)
  203. {
  204. Reset();
  205. return true;
  206. }
  207. return base.ProcessKey(e);
  208. }
  209. /// <summary>
  210. /// The currently selected list item
  211. /// </summary>
  212. public ustring Text
  213. {
  214. get
  215. {
  216. return text;
  217. }
  218. set {
  219. search.Text = text = value;
  220. }
  221. }
  222. private void SetValue(ustring text)
  223. {
  224. search.TextChanged -= Search_Changed;
  225. this.text = search.Text = text;
  226. search.CursorPosition = 0;
  227. search.TextChanged += Search_Changed;
  228. }
  229. private void Selected()
  230. {
  231. if (listview.Source.Count == 0 || searchset.Count == 0) {
  232. text = "";
  233. return;
  234. }
  235. SetValue ((string)searchset [listview.SelectedItem]);
  236. search.CursorPosition = search.Text.Length;
  237. Search_Changed (search.Text);
  238. Reset (keepSearchText: true);
  239. }
  240. /// <summary>
  241. /// Reset to full original list
  242. /// </summary>
  243. private void Reset(bool keepSearchText = false)
  244. {
  245. if(!keepSearchText)
  246. search.Text = text = "";
  247. OnSelectedChanged ();
  248. ResetSearchSet ();
  249. listview.SetSource(searchset);
  250. listview.Height = CalculatetHeight ();
  251. this.SetFocus(search);
  252. }
  253. private void ResetSearchSet()
  254. {
  255. if (autoHide) {
  256. if (searchset == null)
  257. searchset = new List<string> ();
  258. else
  259. searchset.Clear ();
  260. } else
  261. searchset = source.ToList ();
  262. }
  263. private void Search_Changed (ustring text)
  264. {
  265. if (source == null) // Object initialization
  266. return;
  267. if (string.IsNullOrEmpty (search.Text.ToString ()))
  268. ResetSearchSet ();
  269. else
  270. searchset = source.ToList().Cast<string>().Where (x => x.StartsWith (search.Text.ToString (), StringComparison.CurrentCultureIgnoreCase)).ToList();
  271. listview.SetSource (searchset);
  272. listview.Height = CalculatetHeight ();
  273. listview.Redraw (new Rect (0, 0, width, height)); // for any view behind this
  274. this.SuperView?.BringSubviewToFront (this);
  275. }
  276. /// <summary>
  277. /// Internal width of search list
  278. /// </summary>
  279. /// <returns></returns>
  280. private int CalculateWidth ()
  281. {
  282. return autoHide ? Math.Max (1, width - 1) : width;
  283. }
  284. /// <summary>
  285. /// Internal height of dynamic search list
  286. /// </summary>
  287. /// <returns></returns>
  288. private int CalculatetHeight ()
  289. {
  290. var h = (Height is Dim.DimAbsolute) ? height : Bounds.Height;
  291. return Math.Min (Math.Max (0, h - 1), searchset?.Count ?? 0);
  292. }
  293. }
  294. }