ComboBox.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510
  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, TabStop = false };
  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, TabStop = false };
  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. listview.Y = Pos.Bottom (search);
  100. listview.OpenSelectedItem += (ListViewItemEventArgs a) => Selected ();
  101. this.Add (search, listview);
  102. // On resize
  103. LayoutComplete += (LayoutEventArgs a) => {
  104. if (!autoHide && search.Frame.Width != Bounds.Width ||
  105. autoHide && search.Frame.Width != Bounds.Width - 1) {
  106. search.Width = listview.Width = autoHide ? Bounds.Width - 1 : Bounds.Width;
  107. listview.Height = CalculatetHeight ();
  108. search.SetRelativeLayout (Bounds);
  109. listview.SetRelativeLayout (Bounds);
  110. }
  111. };
  112. listview.SelectedItemChanged += (ListViewItemEventArgs e) => {
  113. if (searchset.Count > 0) {
  114. SetValue (searchset [listview.SelectedItem]);
  115. }
  116. };
  117. Added += (View v) => {
  118. // Determine if this view is hosted inside a dialog
  119. for (View view = this.SuperView; view != null; view = view.SuperView) {
  120. if (view is Dialog) {
  121. autoHide = false;
  122. break;
  123. }
  124. }
  125. SetNeedsLayout ();
  126. SetNeedsDisplay ();
  127. Search_Changed (Text);
  128. };
  129. }
  130. /// <summary>
  131. /// Gets the index of the currently selected item in the <see cref="Source"/>
  132. /// </summary>
  133. /// <value>The selected item or -1 none selected.</value>
  134. public int SelectedItem { private set; get; }
  135. bool isShow = false;
  136. ///<inheritdoc/>
  137. public new ColorScheme ColorScheme {
  138. get {
  139. return base.ColorScheme;
  140. }
  141. set {
  142. listview.ColorScheme = value;
  143. base.ColorScheme = value;
  144. SetNeedsDisplay ();
  145. }
  146. }
  147. ///<inheritdoc/>
  148. public override bool MouseEvent (MouseEvent me)
  149. {
  150. if (me.X == Bounds.Right - 1 && me.Y == Bounds.Top && me.Flags == MouseFlags.Button1Pressed
  151. && autoHide) {
  152. if (isShow) {
  153. isShow = false;
  154. HideList ();
  155. } else {
  156. SetSearchSet ();
  157. isShow = true;
  158. ShowList ();
  159. FocusSelectedItem ();
  160. }
  161. return true;
  162. } else if (me.Flags == MouseFlags.Button1Pressed) {
  163. if (!search.HasFocus) {
  164. SetFocus (search);
  165. }
  166. return true;
  167. }
  168. return false;
  169. }
  170. private void FocusSelectedItem ()
  171. {
  172. listview.SelectedItem = SelectedItem > -1 ? SelectedItem : 0;
  173. if (SelectedItem > -1) {
  174. listview.TabStop = true;
  175. this.SetFocus (listview);
  176. }
  177. }
  178. ///<inheritdoc/>
  179. public override bool OnEnter (View view)
  180. {
  181. if (!search.HasFocus && !listview.HasFocus) {
  182. SetFocus (search);
  183. }
  184. search.CursorPosition = search.Text.RuneCount;
  185. return true;
  186. }
  187. ///<inheritdoc/>
  188. public override bool OnLeave (View view)
  189. {
  190. if (autoHide && isShow && view != this && view != search && view != listview) {
  191. isShow = false;
  192. HideList ();
  193. } else if (listview.TabStop) {
  194. listview.TabStop = false;
  195. }
  196. return true;
  197. }
  198. /// <summary>
  199. /// Invokes the SelectedChanged event if it is defined.
  200. /// </summary>
  201. /// <returns></returns>
  202. public virtual bool OnSelectedChanged ()
  203. {
  204. // Note: Cannot rely on "listview.SelectedItem != lastSelectedItem" because the list is dynamic.
  205. // So we cannot optimize. Ie: Don't call if not changed
  206. SelectedItemChanged?.Invoke (new ListViewItemEventArgs(SelectedItem, search.Text));
  207. return true;
  208. }
  209. /// <summary>
  210. /// Invokes the OnOpenSelectedItem event if it is defined.
  211. /// </summary>
  212. /// <returns></returns>
  213. public virtual bool OnOpenSelectedItem ()
  214. {
  215. var value = search.Text;
  216. OpenSelectedItem?.Invoke (new ListViewItemEventArgs (SelectedItem, value));
  217. return true;
  218. }
  219. ///<inheritdoc/>
  220. public override void Redraw (Rect bounds)
  221. {
  222. base.Redraw (bounds);
  223. if (!autoHide) {
  224. return;
  225. }
  226. Move (Bounds.Right - 1, 0);
  227. Driver.AddRune (Driver.DownArrow);
  228. }
  229. ///<inheritdoc/>
  230. public override bool ProcessKey (KeyEvent e)
  231. {
  232. if (e.Key == Key.Enter && listview.SelectedItem > -1) {
  233. Selected ();
  234. return true;
  235. }
  236. if (e.Key == Key.F4 && (search.HasFocus || listview.HasFocus)) {
  237. if (!isShow) {
  238. SetSearchSet ();
  239. isShow = true;
  240. ShowList ();
  241. FocusSelectedItem ();
  242. } else {
  243. isShow = false;
  244. HideList ();
  245. }
  246. return true;
  247. }
  248. if (e.Key == Key.CursorDown && search.HasFocus) { // jump to list
  249. if (searchset.Count > 0) {
  250. listview.TabStop = true;
  251. this.SetFocus (listview);
  252. SetValue (searchset [listview.SelectedItem]);
  253. return true;
  254. } else {
  255. listview.TabStop = false;
  256. SuperView.FocusNext ();
  257. }
  258. }
  259. if (e.Key == Key.CursorUp && search.HasFocus) { // stop odd behavior on KeyUp when search has focus
  260. return true;
  261. }
  262. if (e.Key == Key.CursorUp && listview.HasFocus && listview.SelectedItem == 0 && searchset.Count > 0) // jump back to search
  263. {
  264. search.CursorPosition = search.Text.RuneCount;
  265. this.SetFocus (search);
  266. return true;
  267. }
  268. if(e.Key == Key.PageDown) {
  269. if (listview.SelectedItem != -1) {
  270. listview.MovePageDown ();
  271. }
  272. return true;
  273. }
  274. if (e.Key == Key.PageUp) {
  275. if (listview.SelectedItem != -1) {
  276. listview.MovePageUp ();
  277. }
  278. return true;
  279. }
  280. if (e.Key == Key.Home) {
  281. if (listview.SelectedItem != -1) {
  282. listview.MoveHome ();
  283. }
  284. return true;
  285. }
  286. if(e.Key == Key.End) {
  287. if(listview.SelectedItem != -1) {
  288. listview.MoveEnd ();
  289. }
  290. return true;
  291. }
  292. if (e.Key == Key.Esc) {
  293. this.SetFocus (search);
  294. search.Text = text = "";
  295. OnSelectedChanged ();
  296. return true;
  297. }
  298. // Unix emulation
  299. if (e.Key == Key.ControlU) {
  300. Reset ();
  301. return true;
  302. }
  303. return base.ProcessKey (e);
  304. }
  305. /// <summary>
  306. /// The currently selected list item
  307. /// </summary>
  308. public new ustring Text {
  309. get {
  310. return text;
  311. }
  312. set {
  313. search.Text = text = value;
  314. }
  315. }
  316. private void SetValue (object text)
  317. {
  318. search.TextChanged -= Search_Changed;
  319. this.text = search.Text = text.ToString();
  320. search.CursorPosition = 0;
  321. search.TextChanged += Search_Changed;
  322. SelectedItem = GetSelectedItemFromSource (this.text);
  323. OnSelectedChanged ();
  324. }
  325. private void Selected ()
  326. {
  327. isShow = false;
  328. listview.TabStop = false;
  329. if (listview.Source.Count == 0 || searchset.Count == 0) {
  330. text = "";
  331. return;
  332. }
  333. SetValue (searchset [listview.SelectedItem]);
  334. search.CursorPosition = search.Text.RuneCount;
  335. Search_Changed (search.Text);
  336. OnOpenSelectedItem ();
  337. Reset (keepSearchText: true);
  338. }
  339. private int GetSelectedItemFromSource (ustring value)
  340. {
  341. if (source == null) {
  342. return -1;
  343. }
  344. for (int i = 0; i < source.Count; i++) {
  345. if (source.ToList () [i].ToString () == value) {
  346. return i;
  347. }
  348. }
  349. return -1;
  350. }
  351. /// <summary>
  352. /// Reset to full original list
  353. /// </summary>
  354. private void Reset (bool keepSearchText = false)
  355. {
  356. if (!keepSearchText) {
  357. search.Text = text = "";
  358. }
  359. ResetSearchSet ();
  360. listview.SetSource (searchset);
  361. listview.Height = CalculatetHeight ();
  362. this.SetFocus (search);
  363. }
  364. private void ResetSearchSet (bool noCopy = false)
  365. {
  366. if (searchset == null) {
  367. searchset = new List<object> ();
  368. } else {
  369. searchset.Clear ();
  370. }
  371. if (autoHide || noCopy)
  372. return;
  373. SetSearchSet ();
  374. }
  375. private void SetSearchSet ()
  376. {
  377. // force deep copy
  378. foreach (var item in Source.ToList ()) {
  379. searchset.Add (item);
  380. }
  381. }
  382. private void Search_Changed (ustring text)
  383. {
  384. if (source == null) { // Object initialization
  385. return;
  386. }
  387. if (ustring.IsNullOrEmpty (search.Text) && ustring.IsNullOrEmpty (text)) {
  388. ResetSearchSet ();
  389. } else if (search.Text != text) {
  390. isShow = true;
  391. ResetSearchSet (noCopy: true);
  392. foreach (var item in source.ToList ()) { // Iterate to preserver object type and force deep copy
  393. if (item.ToString().StartsWith (search.Text.ToString(), StringComparison.CurrentCultureIgnoreCase)) {
  394. searchset.Add (item);
  395. }
  396. }
  397. }
  398. ShowList ();
  399. }
  400. /// <summary>
  401. /// Show the search list
  402. /// </summary>
  403. ///
  404. /// Consider making public
  405. private void ShowList ()
  406. {
  407. listview.SetSource (searchset);
  408. listview.Clear (); // Ensure list shrinks in Dialog as you type
  409. listview.Height = CalculatetHeight ();
  410. this.SuperView?.BringSubviewToFront (this);
  411. }
  412. /// <summary>
  413. /// Hide the search list
  414. /// </summary>
  415. ///
  416. /// Consider making public
  417. private void HideList ()
  418. {
  419. Reset (SelectedItem > -1);
  420. listview.TabStop = false;
  421. }
  422. /// <summary>
  423. /// Internal height of dynamic search list
  424. /// </summary>
  425. /// <returns></returns>
  426. private int CalculatetHeight ()
  427. {
  428. if (Bounds.Height == 0)
  429. return 0;
  430. return Math.Min (Bounds.Height - 1, searchset?.Count > 0 ? searchset.Count : isShow ? Bounds.Height - 1 : 0);
  431. }
  432. }
  433. }