ComboBox.cs 9.2 KB

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