ComboBox.cs 9.1 KB

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