ComboBox.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  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 (View view)
  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.BackTab) {
  217. base.ProcessKey (e);
  218. this.FocusPrev ();
  219. return false; // allow tab-out to prev control
  220. }
  221. if (e.Key == Key.Enter && listview.HasFocus) {
  222. Selected ();
  223. return true;
  224. }
  225. if (e.Key == Key.CursorDown && search.HasFocus && searchset.Count > 0) { // jump to list
  226. this.SetFocus (listview);
  227. SetValue (searchset [listview.SelectedItem]);
  228. return true;
  229. }
  230. if (e.Key == Key.CursorUp && search.HasFocus) { // stop odd behavior on KeyUp when search has focus
  231. return true;
  232. }
  233. if (e.Key == Key.CursorUp && listview.HasFocus && listview.SelectedItem == 0 && searchset.Count > 0) // jump back to search
  234. {
  235. search.CursorPosition = search.Text.RuneCount;
  236. this.SetFocus (search);
  237. return true;
  238. }
  239. if(e.Key == Key.PageDown) {
  240. if (listview.SelectedItem != -1) {
  241. listview.MovePageDown ();
  242. }
  243. return true;
  244. }
  245. if (e.Key == Key.PageUp) {
  246. if (listview.SelectedItem != -1) {
  247. listview.MovePageUp ();
  248. }
  249. return true;
  250. }
  251. if (e.Key == Key.Home) {
  252. if (listview.SelectedItem != -1) {
  253. listview.MoveHome ();
  254. }
  255. return true;
  256. }
  257. if(e.Key == Key.End) {
  258. if(listview.SelectedItem != -1) {
  259. listview.MoveEnd ();
  260. }
  261. return true;
  262. }
  263. if (e.Key == Key.Esc) {
  264. this.SetFocus (search);
  265. search.Text = text = "";
  266. OnSelectedChanged ();
  267. return true;
  268. }
  269. // Unix emulation
  270. if (e.Key == Key.ControlU) {
  271. Reset ();
  272. return true;
  273. }
  274. return base.ProcessKey (e);
  275. }
  276. /// <summary>
  277. /// The currently selected list item
  278. /// </summary>
  279. public new ustring Text {
  280. get {
  281. return text;
  282. }
  283. set {
  284. search.Text = text = value;
  285. }
  286. }
  287. private void SetValue (object text)
  288. {
  289. search.TextChanged -= Search_Changed;
  290. this.text = search.Text = text.ToString();
  291. search.CursorPosition = 0;
  292. search.TextChanged += Search_Changed;
  293. SelectedItem = GetSelectedItemFromSource (this.text);
  294. OnSelectedChanged ();
  295. }
  296. private void Selected ()
  297. {
  298. if (listview.Source.Count == 0 || searchset.Count == 0) {
  299. text = "";
  300. return;
  301. }
  302. SetValue (searchset [listview.SelectedItem]);
  303. search.CursorPosition = search.Text.RuneCount;
  304. Search_Changed (search.Text);
  305. OnOpenSelectedItem ();
  306. Reset (keepSearchText: true);
  307. }
  308. private int GetSelectedItemFromSource (ustring value)
  309. {
  310. if (source == null) {
  311. return -1;
  312. }
  313. for (int i = 0; i < source.Count; i++) {
  314. if (source.ToList () [i].ToString () == value) {
  315. return i;
  316. }
  317. }
  318. return -1;
  319. }
  320. /// <summary>
  321. /// Reset to full original list
  322. /// </summary>
  323. private void Reset (bool keepSearchText = false)
  324. {
  325. if (!keepSearchText) {
  326. search.Text = text = "";
  327. }
  328. ResetSearchSet ();
  329. listview.SetSource (searchset);
  330. listview.Height = CalculatetHeight ();
  331. this.SetFocus (search);
  332. }
  333. private void ResetSearchSet (bool noCopy = false)
  334. {
  335. if (searchset == null) {
  336. searchset = new List<object> ();
  337. } else {
  338. searchset.Clear ();
  339. }
  340. if (autoHide || noCopy)
  341. return;
  342. // force deep copy
  343. foreach (var item in Source.ToList ()) {
  344. searchset.Add (item);
  345. }
  346. }
  347. private void Search_Changed (ustring text)
  348. {
  349. if (source == null) { // Object initialization
  350. return;
  351. }
  352. if (ustring.IsNullOrEmpty (search.Text)) {
  353. ResetSearchSet ();
  354. } else {
  355. ResetSearchSet (noCopy: true);
  356. foreach (var item in source.ToList ()) { // Iterate to preserver object type and force deep copy
  357. if (item.ToString().StartsWith (search.Text.ToString(), StringComparison.CurrentCultureIgnoreCase)) {
  358. searchset.Add (item);
  359. }
  360. }
  361. }
  362. ShowList ();
  363. }
  364. /// <summary>
  365. /// Show the search list
  366. /// </summary>
  367. ///
  368. /// Consider making public
  369. private void ShowList ()
  370. {
  371. listview.SetSource (searchset);
  372. listview.Clear (); // Ensure list shrinks in Dialog as you type
  373. listview.Height = CalculatetHeight ();
  374. this.SuperView?.BringSubviewToFront (this);
  375. }
  376. /// <summary>
  377. /// Hide the search list
  378. /// </summary>
  379. ///
  380. /// Consider making public
  381. private void HideList ()
  382. {
  383. Reset ();
  384. }
  385. /// <summary>
  386. /// Internal height of dynamic search list
  387. /// </summary>
  388. /// <returns></returns>
  389. private int CalculatetHeight ()
  390. {
  391. if (Bounds.Height == 0)
  392. return 0;
  393. return Math.Min (Bounds.Height - 1, searchset?.Count ?? 0);
  394. }
  395. }
  396. }