ComboBox.cs 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  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 System.Linq;
  11. using NStack;
  12. namespace Terminal.Gui {
  13. /// <summary>
  14. /// ComboBox control
  15. /// </summary>
  16. public class ComboBox : View {
  17. IListDataSource source;
  18. /// <summary>
  19. /// Gets or sets the <see cref="IListDataSource"/> backing this <see cref="ComboBox"/>, enabling custom rendering.
  20. /// </summary>
  21. /// <value>The source.</value>
  22. /// <remarks>
  23. /// Use <see cref="SetSource"/> to set a new <see cref="IList"/> source.
  24. /// </remarks>
  25. public IListDataSource Source {
  26. get => source;
  27. set {
  28. source = value;
  29. Search_Changed ("");
  30. SetNeedsDisplay ();
  31. }
  32. }
  33. /// <summary>
  34. /// Sets the source of the <see cref="ComboBox"/> to an <see cref="IList"/>.
  35. /// </summary>
  36. /// <value>An object implementing the IList interface.</value>
  37. /// <remarks>
  38. /// Use the <see cref="Source"/> property to set a new <see cref="IListDataSource"/> source and use custome rendering.
  39. /// </remarks>
  40. public void SetSource (IList source)
  41. {
  42. if (source == null) {
  43. Source = null;
  44. } else {
  45. Source = MakeWrapper (source);
  46. }
  47. }
  48. /// <summary>
  49. /// Changed event, raised when the selection has been confirmed.
  50. /// </summary>
  51. /// <remarks>
  52. /// Client code can hook up to this event, it is
  53. /// raised when the selection has been confirmed.
  54. /// </remarks>
  55. public event EventHandler<ustring> SelectedItemChanged;
  56. IList searchset;
  57. ustring text = "";
  58. readonly TextField search;
  59. readonly ListView listview;
  60. int height;
  61. int width;
  62. bool autoHide = true;
  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. this.height = rect.Height;
  91. this.width = rect.Width;
  92. search = new TextField ("") { Width = width };
  93. listview = new ListView (rect, source) { LayoutStyle = LayoutStyle.Computed };
  94. Initialize ();
  95. SetSource (source);
  96. }
  97. static IListDataSource MakeWrapper (IList source)
  98. {
  99. return new ListWrapper (source);
  100. }
  101. private void Initialize ()
  102. {
  103. ColorScheme = Colors.Base;
  104. search.TextChanged += Search_Changed;
  105. listview.OpenSelectedItem += (ListViewItemEventArgs a) => Selected ();
  106. // On resize
  107. LayoutComplete += (LayoutEventArgs a) => {
  108. search.Width = Bounds.Width;
  109. listview.Width = autoHide ? Bounds.Width - 1 : Bounds.Width;
  110. listview.Height = CalculatetHeight ();
  111. };
  112. listview.SelectedItemChanged += (ListViewItemEventArgs e) => {
  113. if (searchset.Count > 0) {
  114. SetValue ((ustring)searchset [listview.SelectedItem]);
  115. }
  116. };
  117. Application.Loaded += (Application.ResizedEventArgs a) => {
  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. ResetSearchSet ();
  126. ColorScheme = autoHide ? Colors.Base : ColorScheme = null;
  127. listview.Y = Pos.Bottom (search);
  128. if (Width != null && width == 0) { // new ComboBox() { Width =
  129. width = Bounds.Width;
  130. }
  131. search.Width = width;
  132. listview.Width = CalculateWidth ();
  133. if (Height != null && height == 0) { // new ComboBox() { Height =
  134. height = Bounds.Height;
  135. }
  136. listview.Height = CalculatetHeight ();
  137. SetNeedsLayout ();
  138. if (this.Text != null) {
  139. Search_Changed (Text);
  140. }
  141. if (autoHide) {
  142. listview.ColorScheme = Colors.Menu;
  143. } else {
  144. search.ColorScheme = Colors.Menu;
  145. }
  146. };
  147. search.MouseClick += Search_MouseClick;
  148. this.Add (listview, search);
  149. this.SetFocus (search);
  150. }
  151. #if COMBO_FEATURE
  152. bool isShow = false;
  153. #endif
  154. private void Search_MouseClick (MouseEventArgs me)
  155. {
  156. #if !COMBO_FEATURE
  157. if (me.MouseEvent.Flags != MouseFlags.Button1Clicked)
  158. return;
  159. #else
  160. if (me.MouseEvent.X == Bounds.Right - 1 && me.MouseEvent.Y == Bounds.Top && me.MouseEvent.Flags == MouseFlags.Button1Pressed
  161. && search.Text == "" && autoHide) {
  162. if (isShow) {
  163. HideList ();
  164. isShow = false;
  165. } else {
  166. searchset = Source.ToList().Cast<object>().ToList(); // force deep copy
  167. ShowList ();
  168. isShow = true;
  169. }
  170. }
  171. else
  172. #endif
  173. SuperView.SetFocus (search);
  174. }
  175. ///<inheritdoc/>
  176. public override bool OnEnter ()
  177. {
  178. if (!search.HasFocus) {
  179. this.SetFocus (search);
  180. }
  181. search.CursorPosition = search.Text.Length;
  182. return true;
  183. }
  184. /// <summary>
  185. /// Invokes the SelectedChanged event if it is defined.
  186. /// </summary>
  187. /// <returns></returns>
  188. public virtual bool OnSelectedChanged ()
  189. {
  190. // Note: Cannot rely on "listview.SelectedItem != lastSelectedItem" because the list is dynamic.
  191. // So we cannot optimize. Ie: Don't call if not changed
  192. SelectedItemChanged?.Invoke (this, search.Text);
  193. return true;
  194. }
  195. #if COMBO_FEATURE
  196. ///<inheritdoc/>
  197. public override void Redraw (Rect bounds)
  198. {
  199. base.Redraw (bounds);
  200. if (!autoHide) {
  201. return;
  202. }
  203. Move (Bounds.Right - 1, 0);
  204. Driver.AddRune (Driver.DownArrow);
  205. }
  206. #endif
  207. ///<inheritdoc/>
  208. public override bool ProcessKey (KeyEvent e)
  209. {
  210. if (e.Key == Key.Tab) {
  211. base.ProcessKey (e);
  212. return false; // allow tab-out to next control
  213. }
  214. if (e.Key == Key.Enter && listview.HasFocus) {
  215. Selected ();
  216. return true;
  217. }
  218. if (e.Key == Key.CursorDown && search.HasFocus && listview.SelectedItem == 0 && searchset.Count > 0) { // jump to list
  219. this.SetFocus (listview);
  220. SetValue ((ustring)searchset [listview.SelectedItem]);
  221. return true;
  222. }
  223. if (e.Key == Key.CursorUp && search.HasFocus) { // stop odd behavior on KeyUp when search has focus
  224. return true;
  225. }
  226. if (e.Key == Key.CursorUp && listview.HasFocus && listview.SelectedItem == 0 && searchset.Count > 0) { // jump back to search
  227. search.CursorPosition = search.Text.Length;
  228. this.SetFocus (search);
  229. return true;
  230. }
  231. if (e.Key == Key.Esc) {
  232. this.SetFocus (search);
  233. search.Text = text = "";
  234. OnSelectedChanged ();
  235. return true;
  236. }
  237. // Unix emulation
  238. if (e.Key == Key.ControlU) {
  239. Reset ();
  240. return true;
  241. }
  242. return base.ProcessKey (e);
  243. }
  244. /// <summary>
  245. /// The currently selected list item
  246. /// </summary>
  247. public ustring Text
  248. {
  249. get {
  250. return text;
  251. }
  252. set {
  253. search.Text = text = value;
  254. }
  255. }
  256. private void SetValue (ustring text)
  257. {
  258. search.TextChanged -= Search_Changed;
  259. this.text = search.Text = text;
  260. search.CursorPosition = 0;
  261. search.TextChanged += Search_Changed;
  262. }
  263. private void Selected ()
  264. {
  265. if (listview.Source.Count == 0 || searchset.Count == 0) {
  266. text = "";
  267. return;
  268. }
  269. SetValue ((ustring)searchset [listview.SelectedItem]);
  270. search.CursorPosition = search.Text.Length;
  271. Search_Changed (search.Text);
  272. Reset (keepSearchText: true);
  273. }
  274. /// <summary>
  275. /// Reset to full original list
  276. /// </summary>
  277. private void Reset (bool keepSearchText = false)
  278. {
  279. if (!keepSearchText) {
  280. search.Text = text = "";
  281. }
  282. OnSelectedChanged ();
  283. ResetSearchSet ();
  284. listview.SetSource (searchset);
  285. listview.Height = CalculatetHeight ();
  286. this.SetFocus (search);
  287. }
  288. private void ResetSearchSet ()
  289. {
  290. if (autoHide) {
  291. if (searchset == null) {
  292. searchset = new List<string> ();
  293. } else {
  294. searchset.Clear ();
  295. }
  296. } else {
  297. searchset = source.ToList ();
  298. }
  299. }
  300. private void Search_Changed (ustring text)
  301. {
  302. if (source == null) { // Object initialization
  303. return;
  304. }
  305. if (string.IsNullOrEmpty (search.Text.ToString ())) {
  306. ResetSearchSet ();
  307. } else {
  308. searchset = source.ToList ().Cast<object> ().Where (x => x.ToString ().StartsWith (search.Text.ToString (), StringComparison.CurrentCultureIgnoreCase)).ToList ();
  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, width, height)); // for any view behind this
  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 width of search list
  335. /// </summary>
  336. /// <returns></returns>
  337. private int CalculateWidth ()
  338. {
  339. return autoHide ? Math.Max (1, width - 1) : width;
  340. }
  341. /// <summary>
  342. /// Internal height of dynamic search list
  343. /// </summary>
  344. /// <returns></returns>
  345. private int CalculatetHeight ()
  346. {
  347. var h = (Height is Dim.DimAbsolute) ? height : Bounds.Height;
  348. return Math.Min (Math.Max (0, h - 1), searchset?.Count ?? 0);
  349. }
  350. }
  351. }