ComboBox.cs 9.1 KB

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