ComboBox.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  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. /// This event is raised when the selected item in the <see cref="ComboBox"/> has changed.
  53. /// </summary>
  54. public Action<ListViewItemEventArgs> SelectedItemChanged;
  55. /// <summary>
  56. /// This event is raised when the user Double Clicks on an item or presses ENTER to open the selected item.
  57. /// </summary>
  58. public Action<ListViewItemEventArgs> OpenSelectedItem;
  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. /// <summary>
  134. /// Gets the index of the currently selected item in the <see cref="Source"/>
  135. /// </summary>
  136. /// <value>The selected item or -1 none selected.</value>
  137. public int SelectedItem { private set; get; }
  138. bool isShow = false;
  139. ///<inheritdoc/>
  140. public new ColorScheme ColorScheme {
  141. get {
  142. return base.ColorScheme;
  143. }
  144. set {
  145. listview.ColorScheme = value;
  146. base.ColorScheme = value;
  147. SetNeedsDisplay ();
  148. }
  149. }
  150. private void Search_MouseClick (MouseEventArgs me)
  151. {
  152. if (me.MouseEvent.X == Bounds.Right - 1 && me.MouseEvent.Y == Bounds.Top && me.MouseEvent.Flags == MouseFlags.Button1Pressed
  153. && search.Text == "" && autoHide) {
  154. if (isShow) {
  155. HideList ();
  156. isShow = false;
  157. } else {
  158. // force deep copy
  159. foreach (var item in Source.ToList()) {
  160. searchset.Add (item);
  161. }
  162. ShowList ();
  163. isShow = true;
  164. }
  165. } else {
  166. SuperView.SetFocus (search);
  167. }
  168. }
  169. ///<inheritdoc/>
  170. public override bool OnEnter ()
  171. {
  172. if (!search.HasFocus) {
  173. this.SetFocus (search);
  174. }
  175. search.CursorPosition = search.Text.RuneCount;
  176. return true;
  177. }
  178. /// <summary>
  179. /// Invokes the SelectedChanged event if it is defined.
  180. /// </summary>
  181. /// <returns></returns>
  182. public virtual bool OnSelectedChanged ()
  183. {
  184. // Note: Cannot rely on "listview.SelectedItem != lastSelectedItem" because the list is dynamic.
  185. // So we cannot optimize. Ie: Don't call if not changed
  186. SelectedItemChanged?.Invoke (new ListViewItemEventArgs(SelectedItem, search.Text));
  187. return true;
  188. }
  189. /// <summary>
  190. /// Invokes the OnOpenSelectedItem event if it is defined.
  191. /// </summary>
  192. /// <returns></returns>
  193. public virtual bool OnOpenSelectedItem ()
  194. {
  195. var value = search.Text;
  196. OpenSelectedItem?.Invoke (new ListViewItemEventArgs (SelectedItem, value));
  197. return true;
  198. }
  199. ///<inheritdoc/>
  200. public override void Redraw (Rect bounds)
  201. {
  202. base.Redraw (bounds);
  203. if (!autoHide) {
  204. return;
  205. }
  206. Move (Bounds.Right - 1, 0);
  207. Driver.AddRune (Driver.DownArrow);
  208. }
  209. ///<inheritdoc/>
  210. public override bool ProcessKey (KeyEvent e)
  211. {
  212. if (e.Key == Key.Tab) {
  213. base.ProcessKey (e);
  214. return false; // allow tab-out to next control
  215. }
  216. if (e.Key == Key.Enter && listview.HasFocus) {
  217. Selected ();
  218. return true;
  219. }
  220. if (e.Key == Key.CursorDown && search.HasFocus && searchset.Count > 0) { // jump to list
  221. this.SetFocus (listview);
  222. SetValue (searchset [listview.SelectedItem]);
  223. return true;
  224. }
  225. if (e.Key == Key.CursorUp && search.HasFocus) { // stop odd behavior on KeyUp when search has focus
  226. return true;
  227. }
  228. if (e.Key == Key.CursorUp && listview.HasFocus && listview.SelectedItem == 0 && searchset.Count > 0) // jump back to search
  229. {
  230. search.CursorPosition = search.Text.RuneCount;
  231. this.SetFocus (search);
  232. return true;
  233. }
  234. if(e.Key == Key.PageDown) {
  235. if (listview.SelectedItem != -1) {
  236. listview.MovePageDown ();
  237. }
  238. return true;
  239. }
  240. if (e.Key == Key.PageUp) {
  241. if (listview.SelectedItem != -1) {
  242. listview.MovePageUp ();
  243. }
  244. return true;
  245. }
  246. if (e.Key == Key.Home) {
  247. if (listview.SelectedItem != -1) {
  248. listview.MoveHome ();
  249. }
  250. return true;
  251. }
  252. if(e.Key == Key.End) {
  253. if(listview.SelectedItem != -1) {
  254. listview.MoveEnd ();
  255. }
  256. return true;
  257. }
  258. if (e.Key == Key.Esc) {
  259. this.SetFocus (search);
  260. search.Text = text = "";
  261. OnSelectedChanged ();
  262. return true;
  263. }
  264. // Unix emulation
  265. if (e.Key == Key.ControlU) {
  266. Reset ();
  267. return true;
  268. }
  269. return base.ProcessKey (e);
  270. }
  271. /// <summary>
  272. /// The currently selected list item
  273. /// </summary>
  274. public new ustring Text {
  275. get {
  276. return text;
  277. }
  278. set {
  279. search.Text = text = value;
  280. }
  281. }
  282. private void SetValue (object text)
  283. {
  284. search.TextChanged -= Search_Changed;
  285. this.text = search.Text = text.ToString();
  286. search.CursorPosition = 0;
  287. search.TextChanged += Search_Changed;
  288. SelectedItem = GetSelectedItemFromSource (this.text);
  289. OnSelectedChanged ();
  290. }
  291. private void Selected ()
  292. {
  293. if (listview.Source.Count == 0 || searchset.Count == 0) {
  294. text = "";
  295. return;
  296. }
  297. SetValue (searchset [listview.SelectedItem]);
  298. search.CursorPosition = search.Text.RuneCount;
  299. Search_Changed (search.Text);
  300. OnOpenSelectedItem ();
  301. Reset (keepSearchText: true);
  302. }
  303. private int GetSelectedItemFromSource (ustring value)
  304. {
  305. if (source == null) {
  306. return -1;
  307. }
  308. for (int i = 0; i < source.Count; i++) {
  309. if (source.ToList () [i].ToString () == value) {
  310. return i;
  311. }
  312. }
  313. return -1;
  314. }
  315. /// <summary>
  316. /// Reset to full original list
  317. /// </summary>
  318. private void Reset (bool keepSearchText = false)
  319. {
  320. if (!keepSearchText) {
  321. search.Text = text = "";
  322. }
  323. ResetSearchSet ();
  324. listview.SetSource (searchset);
  325. listview.Height = CalculatetHeight ();
  326. this.SetFocus (search);
  327. }
  328. private void ResetSearchSet (bool noCopy = false)
  329. {
  330. if (searchset == null) {
  331. searchset = new List<object> ();
  332. } else {
  333. searchset.Clear ();
  334. }
  335. if (autoHide || noCopy)
  336. return;
  337. // force deep copy
  338. foreach (var item in Source.ToList ()) {
  339. searchset.Add (item);
  340. }
  341. }
  342. private void Search_Changed (ustring text)
  343. {
  344. if (source == null) { // Object initialization
  345. return;
  346. }
  347. if (ustring.IsNullOrEmpty (search.Text)) {
  348. ResetSearchSet ();
  349. } else {
  350. ResetSearchSet (noCopy: true);
  351. foreach (var item in source.ToList ()) { // Iterate to preserver object type and force deep copy
  352. if (item.ToString().StartsWith (search.Text.ToString(), StringComparison.CurrentCultureIgnoreCase)) {
  353. searchset.Add (item);
  354. }
  355. }
  356. }
  357. ShowList ();
  358. }
  359. /// <summary>
  360. /// Show the search list
  361. /// </summary>
  362. ///
  363. /// Consider making public
  364. private void ShowList ()
  365. {
  366. listview.SetSource (searchset);
  367. listview.Clear (); // Ensure list shrinks in Dialog as you type
  368. listview.Height = CalculatetHeight ();
  369. this.SuperView?.BringSubviewToFront (this);
  370. }
  371. /// <summary>
  372. /// Hide the search list
  373. /// </summary>
  374. ///
  375. /// Consider making public
  376. private void HideList ()
  377. {
  378. Reset ();
  379. }
  380. /// <summary>
  381. /// Internal height of dynamic search list
  382. /// </summary>
  383. /// <returns></returns>
  384. private int CalculatetHeight ()
  385. {
  386. if (Bounds.Height == 0)
  387. return 0;
  388. return Math.Min (Bounds.Height - 1, searchset?.Count ?? 0);
  389. }
  390. }
  391. }