ComboBox.cs 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  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="text"></param>
  87. public ComboBox (ustring text) : base ()
  88. {
  89. search = new TextField ("");
  90. listview = new ListView () { LayoutStyle = LayoutStyle.Computed, CanFocus = true };
  91. Initialize ();
  92. Text = text;
  93. }
  94. /// <summary>
  95. /// Public constructor
  96. /// </summary>
  97. /// <param name="rect"></param>
  98. /// <param name="source"></param>
  99. public ComboBox (Rect rect, IList source) : base (rect)
  100. {
  101. this.height = rect.Height;
  102. this.width = rect.Width;
  103. search = new TextField ("") { Width = width };
  104. listview = new ListView (rect, source) { LayoutStyle = LayoutStyle.Computed };
  105. Initialize ();
  106. SetSource (source);
  107. }
  108. static IListDataSource MakeWrapper (IList source)
  109. {
  110. return new ListWrapper (source);
  111. }
  112. private void Initialize ()
  113. {
  114. ColorScheme = Colors.Base;
  115. search.TextChanged += Search_Changed;
  116. listview.OpenSelectedItem += (ListViewItemEventArgs a) => Selected ();
  117. // On resize
  118. LayoutComplete += (LayoutEventArgs a) => {
  119. search.Width = Bounds.Width;
  120. listview.Width = autoHide ? Bounds.Width - 1 : Bounds.Width;
  121. listview.Height = CalculatetHeight ();
  122. };
  123. listview.SelectedItemChanged += (ListViewItemEventArgs e) => {
  124. if (searchset.Count > 0) {
  125. SetValue ((ustring)searchset [listview.SelectedItem]);
  126. }
  127. };
  128. Application.Loaded += (Application.ResizedEventArgs a) => {
  129. // Determine if this view is hosted inside a dialog
  130. for (View view = this.SuperView; view != null; view = view.SuperView) {
  131. if (view is Dialog) {
  132. autoHide = false;
  133. break;
  134. }
  135. }
  136. ResetSearchSet ();
  137. ColorScheme = autoHide ? Colors.Base : ColorScheme = null;
  138. listview.Y = Pos.Bottom (search);
  139. if (Width != null && width == 0) { // new ComboBox() { Width =
  140. width = Bounds.Width;
  141. }
  142. search.Width = width;
  143. listview.Width = CalculateWidth ();
  144. if (Height != null && height == 0) { // new ComboBox() { Height =
  145. height = Bounds.Height;
  146. }
  147. listview.Height = CalculatetHeight ();
  148. SetNeedsLayout ();
  149. if (this.Text != null) {
  150. Search_Changed (Text);
  151. }
  152. if (autoHide) {
  153. listview.ColorScheme = Colors.Menu;
  154. } else {
  155. search.ColorScheme = Colors.Menu;
  156. }
  157. };
  158. search.MouseClick += Search_MouseClick;
  159. this.Add (listview, search);
  160. this.SetFocus (search);
  161. }
  162. #if COMBO_FEATURE
  163. bool isShow = false;
  164. #endif
  165. private void Search_MouseClick (MouseEventArgs me)
  166. {
  167. #if !COMBO_FEATURE
  168. if (me.MouseEvent.Flags != MouseFlags.Button1Clicked)
  169. return;
  170. #else
  171. if (me.MouseEvent.X == Bounds.Right - 1 && me.MouseEvent.Y == Bounds.Top && me.MouseEvent.Flags == MouseFlags.Button1Pressed
  172. && search.Text == "" && autoHide) {
  173. if (isShow) {
  174. HideList ();
  175. isShow = false;
  176. } else {
  177. searchset = Source.ToList().Cast<object>().ToList(); // force deep copy
  178. ShowList ();
  179. isShow = true;
  180. }
  181. }
  182. else
  183. #endif
  184. SuperView.SetFocus (search);
  185. }
  186. ///<inheritdoc/>
  187. public override bool OnEnter ()
  188. {
  189. if (!search.HasFocus) {
  190. this.SetFocus (search);
  191. }
  192. search.CursorPosition = search.Text.Length;
  193. return true;
  194. }
  195. /// <summary>
  196. /// Invokes the SelectedChanged event if it is defined.
  197. /// </summary>
  198. /// <returns></returns>
  199. public virtual bool OnSelectedChanged ()
  200. {
  201. // Note: Cannot rely on "listview.SelectedItem != lastSelectedItem" because the list is dynamic.
  202. // So we cannot optimize. Ie: Don't call if not changed
  203. SelectedItemChanged?.Invoke (this, search.Text);
  204. return true;
  205. }
  206. #if COMBO_FEATURE
  207. ///<inheritdoc/>
  208. public override void Redraw (Rect bounds)
  209. {
  210. base.Redraw (bounds);
  211. if (!autoHide) {
  212. return;
  213. }
  214. Move (Bounds.Right - 1, 0);
  215. Driver.AddRune (Driver.DownArrow);
  216. }
  217. #endif
  218. ///<inheritdoc/>
  219. public override bool ProcessKey (KeyEvent e)
  220. {
  221. if (e.Key == Key.Tab) {
  222. base.ProcessKey (e);
  223. return false; // allow tab-out to next control
  224. }
  225. if (e.Key == Key.Enter && listview.HasFocus) {
  226. Selected ();
  227. return true;
  228. }
  229. if (e.Key == Key.CursorDown && search.HasFocus && listview.SelectedItem == 0 && searchset.Count > 0) { // jump to list
  230. this.SetFocus (listview);
  231. SetValue ((ustring)searchset [listview.SelectedItem]);
  232. return true;
  233. }
  234. if (e.Key == Key.CursorUp && search.HasFocus) { // stop odd behavior on KeyUp when search has focus
  235. return true;
  236. }
  237. if (e.Key == Key.CursorUp && listview.HasFocus && listview.SelectedItem == 0 && searchset.Count > 0) { // jump back to search
  238. search.CursorPosition = search.Text.Length;
  239. this.SetFocus (search);
  240. return true;
  241. }
  242. if (e.Key == Key.Esc) {
  243. this.SetFocus (search);
  244. search.Text = text = "";
  245. OnSelectedChanged ();
  246. return true;
  247. }
  248. // Unix emulation
  249. if (e.Key == Key.ControlU) {
  250. Reset ();
  251. return true;
  252. }
  253. return base.ProcessKey (e);
  254. }
  255. /// <summary>
  256. /// The currently selected list item
  257. /// </summary>
  258. public ustring Text
  259. {
  260. get {
  261. return text;
  262. }
  263. set {
  264. search.Text = text = value;
  265. }
  266. }
  267. private void SetValue (ustring text)
  268. {
  269. search.TextChanged -= Search_Changed;
  270. this.text = search.Text = text;
  271. search.CursorPosition = 0;
  272. search.TextChanged += Search_Changed;
  273. }
  274. private void Selected ()
  275. {
  276. if (listview.Source.Count == 0 || searchset.Count == 0) {
  277. text = "";
  278. return;
  279. }
  280. SetValue ((ustring)searchset [listview.SelectedItem]);
  281. search.CursorPosition = search.Text.Length;
  282. Search_Changed (search.Text);
  283. Reset (keepSearchText: true);
  284. }
  285. /// <summary>
  286. /// Reset to full original list
  287. /// </summary>
  288. private void Reset (bool keepSearchText = false)
  289. {
  290. if (!keepSearchText) {
  291. search.Text = text = "";
  292. }
  293. OnSelectedChanged ();
  294. ResetSearchSet ();
  295. listview.SetSource (searchset);
  296. listview.Height = CalculatetHeight ();
  297. this.SetFocus (search);
  298. }
  299. private void ResetSearchSet ()
  300. {
  301. if (autoHide) {
  302. if (searchset == null) {
  303. searchset = new List<string> ();
  304. } else {
  305. searchset.Clear ();
  306. }
  307. } else {
  308. searchset = source.ToList ();
  309. }
  310. }
  311. private void Search_Changed (ustring text)
  312. {
  313. if (source == null) { // Object initialization
  314. return;
  315. }
  316. if (string.IsNullOrEmpty (search.Text.ToString ())) {
  317. ResetSearchSet ();
  318. } else {
  319. searchset = source.ToList ().Cast<object> ().Where (x => x.ToString ().StartsWith (search.Text.ToString (), StringComparison.CurrentCultureIgnoreCase)).ToList ();
  320. }
  321. ShowList ();
  322. }
  323. /// <summary>
  324. /// Show the search list
  325. /// </summary>
  326. ///
  327. /// Consider making public
  328. private void ShowList()
  329. {
  330. listview.SetSource (searchset);
  331. listview.Height = CalculatetHeight ();
  332. listview.Redraw (new Rect (0, 0, width, height)); // for any view behind this
  333. this.SuperView?.BringSubviewToFront (this);
  334. }
  335. /// <summary>
  336. /// Hide the search list
  337. /// </summary>
  338. ///
  339. /// Consider making public
  340. private void HideList()
  341. {
  342. Reset ();
  343. }
  344. /// <summary>
  345. /// Internal width of search list
  346. /// </summary>
  347. /// <returns></returns>
  348. private int CalculateWidth ()
  349. {
  350. return autoHide ? Math.Max (1, width - 1) : width;
  351. }
  352. /// <summary>
  353. /// Internal height of dynamic search list
  354. /// </summary>
  355. /// <returns></returns>
  356. private int CalculatetHeight ()
  357. {
  358. var h = (Height is Dim.DimAbsolute) ? height : Bounds.Height;
  359. return Math.Min (Math.Max (0, h - 1), searchset?.Count ?? 0);
  360. }
  361. }
  362. }