ComboBox.cs 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  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 NStack;
  11. namespace Terminal.Gui {
  12. /// <summary>
  13. /// ComboBox control
  14. /// </summary>
  15. public class ComboBox : View {
  16. IListDataSource source;
  17. /// <summary>
  18. /// Gets or sets the <see cref="IListDataSource"/> backing this <see cref="ComboBox"/>, enabling custom rendering.
  19. /// </summary>
  20. /// <value>The source.</value>
  21. /// <remarks>
  22. /// Use <see cref="SetSource"/> to set a new <see cref="IList"/> source.
  23. /// </remarks>
  24. public IListDataSource Source {
  25. get => source;
  26. set {
  27. source = value;
  28. if(isAdded) {
  29. Search_Changed ("");
  30. SetNeedsDisplay ();
  31. }
  32. }
  33. }
  34. /// <summary>
  35. /// Sets the source of the <see cref="ComboBox"/> to an <see cref="IList"/>.
  36. /// </summary>
  37. /// <value>An object implementing the IList interface.</value>
  38. /// <remarks>
  39. /// Use the <see cref="Source"/> property to set a new <see cref="IListDataSource"/> source and use custome rendering.
  40. /// </remarks>
  41. public void SetSource (IList source)
  42. {
  43. if (source == null) {
  44. Source = null;
  45. } else {
  46. Source = MakeWrapper (source);
  47. }
  48. }
  49. /// <summary>
  50. /// Changed event, raised when the selection has been confirmed.
  51. /// </summary>
  52. /// <remarks>
  53. /// Client code can hook up to this event, it is
  54. /// raised when the selection has been confirmed.
  55. /// </remarks>
  56. public event EventHandler<ustring> SelectedItemChanged;
  57. IList searchset;
  58. ustring text = "";
  59. readonly TextField search;
  60. readonly ListView listview;
  61. bool autoHide = true;
  62. bool isAdded;
  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. search = new TextField ("") { Width = rect.Width };
  91. listview = new ListView (rect, source) { LayoutStyle = LayoutStyle.Computed };
  92. Initialize ();
  93. SetSource (source);
  94. }
  95. static IListDataSource MakeWrapper (IList source)
  96. {
  97. return new ListWrapper (source);
  98. }
  99. private void Initialize ()
  100. {
  101. ColorScheme = Colors.Base;
  102. search.TextChanged += Search_Changed;
  103. search.MouseClick += Search_MouseClick;
  104. listview.Y = Pos.Bottom (search);
  105. listview.OpenSelectedItem += (ListViewItemEventArgs a) => Selected ();
  106. this.Add (listview, search);
  107. this.SetFocus (search);
  108. // On resize
  109. LayoutComplete += (LayoutEventArgs a) => {
  110. search.Width = Bounds.Width;
  111. listview.Width = autoHide ? Bounds.Width - 1 : Bounds.Width;
  112. listview.Height = CalculatetHeight ();
  113. };
  114. listview.SelectedItemChanged += (ListViewItemEventArgs e) => {
  115. if (searchset.Count > 0) {
  116. SetValue (searchset [listview.SelectedItem]);
  117. }
  118. };
  119. // This is needed in addition to 'Adding' to trigger the capture the Bounds.Width & Height
  120. Application.Loaded += (Application.ResizedEventArgs a) => {
  121. SetNeedsLayout ();
  122. Search_Changed (Text);
  123. };
  124. Adding += (View v) => {
  125. // Determine if this view is hosted inside a dialog
  126. for (View view = this.SuperView; view != null; view = view.SuperView) {
  127. if (view is Dialog) {
  128. autoHide = false;
  129. break;
  130. }
  131. }
  132. ColorScheme = autoHide ? Colors.Base : ColorScheme = null;
  133. SetNeedsLayout ();
  134. Search_Changed (Text);
  135. if (autoHide) {
  136. listview.ColorScheme = Colors.Menu;
  137. } else {
  138. search.ColorScheme = Colors.Menu;
  139. }
  140. isAdded = true;
  141. };
  142. }
  143. bool isShow = false;
  144. private void Search_MouseClick (MouseEventArgs me)
  145. {
  146. if (me.MouseEvent.X == Bounds.Right - 1 && me.MouseEvent.Y == Bounds.Top && me.MouseEvent.Flags == MouseFlags.Button1Pressed
  147. && search.Text == "" && autoHide) {
  148. if (isShow) {
  149. HideList ();
  150. isShow = false;
  151. } else {
  152. // force deep copy
  153. foreach (var item in Source.ToList()) {
  154. searchset.Add (item);
  155. }
  156. ShowList ();
  157. isShow = true;
  158. }
  159. } else {
  160. SuperView.SetFocus (search);
  161. }
  162. }
  163. ///<inheritdoc/>
  164. public override bool OnEnter ()
  165. {
  166. if (!search.HasFocus) {
  167. this.SetFocus (search);
  168. }
  169. search.CursorPosition = search.Text.RuneCount;
  170. return true;
  171. }
  172. /// <summary>
  173. /// Invokes the SelectedChanged event if it is defined.
  174. /// </summary>
  175. /// <returns></returns>
  176. public virtual bool OnSelectedChanged ()
  177. {
  178. // Note: Cannot rely on "listview.SelectedItem != lastSelectedItem" because the list is dynamic.
  179. // So we cannot optimize. Ie: Don't call if not changed
  180. SelectedItemChanged?.Invoke (this, search.Text);
  181. return true;
  182. }
  183. ///<inheritdoc/>
  184. public override void Redraw (Rect bounds)
  185. {
  186. base.Redraw (bounds);
  187. if (!autoHide) {
  188. return;
  189. }
  190. Move (Bounds.Right - 1, 0);
  191. Driver.AddRune (Driver.DownArrow);
  192. }
  193. ///<inheritdoc/>
  194. public override bool ProcessKey (KeyEvent e)
  195. {
  196. if (e.Key == Key.Tab) {
  197. base.ProcessKey (e);
  198. return false; // allow tab-out to next control
  199. }
  200. if (e.Key == Key.Enter && listview.HasFocus) {
  201. Selected ();
  202. return true;
  203. }
  204. if (e.Key == Key.CursorDown && search.HasFocus && searchset.Count > 0) { // jump to list
  205. this.SetFocus (listview);
  206. SetValue (searchset [listview.SelectedItem]);
  207. return true;
  208. }
  209. if (e.Key == Key.CursorUp && search.HasFocus) { // stop odd behavior on KeyUp when search has focus
  210. return true;
  211. }
  212. if (e.Key == Key.CursorUp && listview.HasFocus && listview.SelectedItem == 0 && searchset.Count > 0) // jump back to search
  213. {
  214. search.CursorPosition = search.Text.RuneCount;
  215. this.SetFocus (search);
  216. return true;
  217. }
  218. if(e.Key == Key.PageDown) {
  219. listview.MovePageDown ();
  220. return true;
  221. }
  222. if (e.Key == Key.PageUp) {
  223. listview.MovePageUp ();
  224. return true;
  225. }
  226. if (e.Key == Key.Esc) {
  227. this.SetFocus (search);
  228. search.Text = text = "";
  229. OnSelectedChanged ();
  230. return true;
  231. }
  232. // Unix emulation
  233. if (e.Key == Key.ControlU) {
  234. Reset ();
  235. return true;
  236. }
  237. return base.ProcessKey (e);
  238. }
  239. /// <summary>
  240. /// The currently selected list item
  241. /// </summary>
  242. public new ustring Text {
  243. get {
  244. return text;
  245. }
  246. set {
  247. search.Text = text = value;
  248. }
  249. }
  250. private void SetValue (object text)
  251. {
  252. search.TextChanged -= Search_Changed;
  253. this.text = search.Text = text.ToString();
  254. search.CursorPosition = 0;
  255. search.TextChanged += Search_Changed;
  256. }
  257. private void Selected ()
  258. {
  259. if (listview.Source.Count == 0 || searchset.Count == 0) {
  260. text = "";
  261. return;
  262. }
  263. SetValue (searchset [listview.SelectedItem]);
  264. search.CursorPosition = search.Text.RuneCount;
  265. Search_Changed (search.Text);
  266. OnSelectedChanged ();
  267. Reset (keepSearchText: true);
  268. }
  269. /// <summary>
  270. /// Reset to full original list
  271. /// </summary>
  272. private void Reset (bool keepSearchText = false)
  273. {
  274. if (!keepSearchText) {
  275. search.Text = text = "";
  276. }
  277. ResetSearchSet ();
  278. listview.SetSource (searchset);
  279. listview.Height = CalculatetHeight ();
  280. this.SetFocus (search);
  281. }
  282. private void ResetSearchSet (bool noCopy = false)
  283. {
  284. if (searchset == null) {
  285. searchset = new List<object> ();
  286. } else {
  287. searchset.Clear ();
  288. }
  289. if (autoHide || noCopy)
  290. return;
  291. // force deep copy
  292. foreach (var item in Source.ToList ()) {
  293. searchset.Add (item);
  294. }
  295. }
  296. private void Search_Changed (ustring text)
  297. {
  298. if (source == null) { // Object initialization
  299. return;
  300. }
  301. if (ustring.IsNullOrEmpty (search.Text)) {
  302. ResetSearchSet ();
  303. } else {
  304. ResetSearchSet (noCopy: true);
  305. foreach (var item in source.ToList ()) { // Iterate to preserver object type and force deep copy
  306. if (item.ToString().StartsWith (search.Text.ToString(), StringComparison.CurrentCultureIgnoreCase)) {
  307. searchset.Add (item);
  308. }
  309. }
  310. }
  311. ShowList ();
  312. }
  313. /// <summary>
  314. /// Show the search list
  315. /// </summary>
  316. ///
  317. /// Consider making public
  318. private void ShowList ()
  319. {
  320. listview.SetSource (searchset);
  321. listview.Height = CalculatetHeight ();
  322. listview.Redraw (new Rect (0, 0, Bounds.Width, Bounds.Height)); // Ensure list shrinks in Dialog
  323. this.SuperView?.BringSubviewToFront (this);
  324. }
  325. /// <summary>
  326. /// Hide the search list
  327. /// </summary>
  328. ///
  329. /// Consider making public
  330. private void HideList ()
  331. {
  332. Reset ();
  333. }
  334. /// <summary>
  335. /// Internal height of dynamic search list
  336. /// </summary>
  337. /// <returns></returns>
  338. private int CalculatetHeight ()
  339. {
  340. if (Bounds.Height == 0)
  341. return 0;
  342. return Math.Min (Bounds.Height - 1, searchset?.Count ?? 0);
  343. }
  344. }
  345. }