ComboBox.cs 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001
  1. //
  2. // ComboBox.cs: ComboBox control
  3. //
  4. // Authors:
  5. // Ross Ferguson ([email protected])
  6. //
  7. using System.Collections.ObjectModel;
  8. using System.ComponentModel;
  9. namespace Terminal.Gui;
  10. /// <summary>Provides a drop-down list of items the user can select from.</summary>
  11. public class ComboBox : View
  12. {
  13. private readonly ComboListView _listview;
  14. private readonly int _minimumHeight = 2;
  15. private readonly TextField _search;
  16. private readonly ObservableCollection<object> _searchSet = [];
  17. private bool _autoHide = true;
  18. private bool _hideDropdownListOnClick;
  19. private int _lastSelectedItem = -1;
  20. private int _selectedItem = -1;
  21. private IListDataSource _source;
  22. private string _text = "";
  23. /// <summary>Public constructor</summary>
  24. public ComboBox ()
  25. {
  26. _search = new TextField ();
  27. _listview = new ComboListView (this, HideDropdownListOnClick) { CanFocus = true, TabStop = false };
  28. _search.TextChanged += Search_Changed;
  29. _search.Accept += Search_Accept;
  30. _listview.Y = Pos.Bottom (_search);
  31. _listview.OpenSelectedItem += (sender, a) => Selected ();
  32. Add (_search, _listview);
  33. // BUGBUG: This should not be needed; LayoutComplete will handle
  34. Initialized += (s, e) => ProcessLayout ();
  35. // On resize
  36. LayoutComplete += (sender, a) => ProcessLayout ();
  37. ;
  38. _listview.SelectedItemChanged += (sender, e) =>
  39. {
  40. if (!HideDropdownListOnClick && _searchSet.Count > 0)
  41. {
  42. SetValue (_searchSet [_listview.SelectedItem]);
  43. }
  44. };
  45. Added += (s, e) =>
  46. {
  47. // Determine if this view is hosted inside a dialog and is the only control
  48. for (View view = SuperView; view != null; view = view.SuperView)
  49. {
  50. if (view is Dialog && SuperView is { } && SuperView.Subviews.Count == 1 && SuperView.Subviews [0] == this)
  51. {
  52. _autoHide = false;
  53. break;
  54. }
  55. }
  56. SetNeedsLayout ();
  57. SetNeedsDisplay ();
  58. ShowHideList (Text);
  59. };
  60. // Things this view knows how to do
  61. AddCommand (Command.Accept, () => ActivateSelected ());
  62. AddCommand (Command.ToggleExpandCollapse, () => ExpandCollapse ());
  63. AddCommand (Command.Expand, () => Expand ());
  64. AddCommand (Command.Collapse, () => Collapse ());
  65. AddCommand (Command.LineDown, () => MoveDown ());
  66. AddCommand (Command.LineUp, () => MoveUp ());
  67. AddCommand (Command.PageDown, () => PageDown ());
  68. AddCommand (Command.PageUp, () => PageUp ());
  69. AddCommand (Command.TopHome, () => MoveHome ());
  70. AddCommand (Command.BottomEnd, () => MoveEnd ());
  71. AddCommand (Command.Cancel, () => CancelSelected ());
  72. AddCommand (Command.UnixEmulation, () => UnixEmulation ());
  73. // Default keybindings for this view
  74. KeyBindings.Add (Key.Enter, Command.Accept);
  75. KeyBindings.Add (Key.F4, Command.ToggleExpandCollapse);
  76. KeyBindings.Add (Key.CursorDown, Command.LineDown);
  77. KeyBindings.Add (Key.CursorUp, Command.LineUp);
  78. KeyBindings.Add (Key.PageDown, Command.PageDown);
  79. KeyBindings.Add (Key.PageUp, Command.PageUp);
  80. KeyBindings.Add (Key.Home, Command.TopHome);
  81. KeyBindings.Add (Key.End, Command.BottomEnd);
  82. KeyBindings.Add (Key.Esc, Command.Cancel);
  83. KeyBindings.Add (Key.U.WithCtrl, Command.UnixEmulation);
  84. }
  85. /// <inheritdoc/>
  86. public new ColorScheme ColorScheme
  87. {
  88. get => base.ColorScheme;
  89. set
  90. {
  91. _listview.ColorScheme = value;
  92. base.ColorScheme = value;
  93. SetNeedsDisplay ();
  94. }
  95. }
  96. /// <summary>Gets or sets if the drop-down list can be hide with a button click event.</summary>
  97. public bool HideDropdownListOnClick
  98. {
  99. get => _hideDropdownListOnClick;
  100. set => _hideDropdownListOnClick = _listview.HideDropdownListOnClick = value;
  101. }
  102. /// <summary>Gets the drop down list state, expanded or collapsed.</summary>
  103. public bool IsShow { get; private set; }
  104. /// <summary>If set to true its not allow any changes in the text.</summary>
  105. public bool ReadOnly
  106. {
  107. get => _search.ReadOnly;
  108. set
  109. {
  110. _search.ReadOnly = value;
  111. if (_search.ReadOnly)
  112. {
  113. if (_search.ColorScheme is { })
  114. {
  115. _search.ColorScheme = new ColorScheme (_search.ColorScheme) { Normal = _search.ColorScheme.Focus };
  116. }
  117. }
  118. }
  119. }
  120. /// <summary>Current search text</summary>
  121. public string SearchText
  122. {
  123. get => _search.Text;
  124. set => SetSearchText (value);
  125. }
  126. /// <summary>Gets the index of the currently selected item in the <see cref="Source"/></summary>
  127. /// <value>The selected item or -1 none selected.</value>
  128. public int SelectedItem
  129. {
  130. get => _selectedItem;
  131. set
  132. {
  133. if (_selectedItem != value
  134. && (value == -1
  135. || (_source is { } && value > -1 && value < _source.Count)))
  136. {
  137. _selectedItem = _lastSelectedItem = value;
  138. if (_selectedItem != -1)
  139. {
  140. SetValue (_source.ToList () [_selectedItem].ToString (), true);
  141. }
  142. else
  143. {
  144. SetValue ("", true);
  145. }
  146. OnSelectedChanged ();
  147. }
  148. }
  149. }
  150. /// <summary>Gets or sets the <see cref="IListDataSource"/> backing this <see cref="ComboBox"/>, enabling custom rendering.</summary>
  151. /// <value>The source.</value>
  152. /// <remarks>Use <see cref="SetSource{T}"/> to set a new <see cref="ObservableCollection{T}"/> source.</remarks>
  153. public IListDataSource Source
  154. {
  155. get => _source;
  156. set
  157. {
  158. _source = value;
  159. // Only need to refresh list if its been added to a container view
  160. if (SuperView is { } && SuperView.Subviews.Contains (this))
  161. {
  162. Text = string.Empty;
  163. // SelectedItem = -1;
  164. // _search.Text = string.Empty;
  165. // ResetSearchSet ();
  166. // HideList ();
  167. //// ShowHideList (string.Empty);
  168. SetNeedsDisplay ();
  169. }
  170. }
  171. }
  172. /// <summary>The text of the currently selected list item</summary>
  173. public new string Text
  174. {
  175. get => _text;
  176. set => SetSearchText (value);
  177. }
  178. /// <summary>
  179. /// Collapses the drop down list. Returns true if the state chagned or false if it was already collapsed and no
  180. /// action was taken
  181. /// </summary>
  182. public virtual bool Collapse ()
  183. {
  184. if (!IsShow)
  185. {
  186. return false;
  187. }
  188. IsShow = false;
  189. HideList ();
  190. return true;
  191. }
  192. /// <summary>This event is raised when the drop-down list is collapsed.</summary>
  193. public event EventHandler Collapsed;
  194. /// <summary>
  195. /// Expands the drop down list. Returns true if the state chagned or false if it was already expanded and no
  196. /// action was taken
  197. /// </summary>
  198. public virtual bool Expand ()
  199. {
  200. if (IsShow)
  201. {
  202. return false;
  203. }
  204. SetSearchSet ();
  205. IsShow = true;
  206. ShowList ();
  207. FocusSelectedItem ();
  208. return true;
  209. }
  210. /// <summary>This event is raised when the drop-down list is expanded.</summary>
  211. public event EventHandler Expanded;
  212. /// <inheritdoc/>
  213. protected internal override bool OnMouseEvent (MouseEvent me)
  214. {
  215. if (me.Position.X == Viewport.Right - 1
  216. && me.Position.Y == Viewport.Top
  217. && me.Flags == MouseFlags.Button1Pressed
  218. && _autoHide)
  219. {
  220. if (IsShow)
  221. {
  222. IsShow = false;
  223. HideList ();
  224. }
  225. else
  226. {
  227. SetSearchSet ();
  228. IsShow = true;
  229. ShowList ();
  230. FocusSelectedItem ();
  231. }
  232. return me.Handled = true;
  233. }
  234. if (me.Flags == MouseFlags.Button1Pressed)
  235. {
  236. if (!_search.HasFocus)
  237. {
  238. _search.SetFocus ();
  239. }
  240. return me.Handled = true;
  241. }
  242. return false;
  243. }
  244. /// <summary>Virtual method which invokes the <see cref="Collapsed"/> event.</summary>
  245. public virtual void OnCollapsed () { Collapsed?.Invoke (this, EventArgs.Empty); }
  246. /// <inheritdoc/>
  247. public override void OnDrawContent (Rectangle viewport)
  248. {
  249. base.OnDrawContent (viewport);
  250. if (!_autoHide)
  251. {
  252. return;
  253. }
  254. Driver.SetAttribute (ColorScheme.Focus);
  255. Move (Viewport.Right - 1, 0);
  256. Driver.AddRune (Glyphs.DownArrow);
  257. }
  258. /// <inheritdoc/>
  259. public override bool OnEnter (View view)
  260. {
  261. if (!_search.HasFocus && !_listview.HasFocus)
  262. {
  263. _search.SetFocus ();
  264. }
  265. _search.CursorPosition = _search.Text.GetRuneCount ();
  266. return base.OnEnter (view);
  267. }
  268. /// <summary>Virtual method which invokes the <see cref="Expanded"/> event.</summary>
  269. public virtual void OnExpanded () { Expanded?.Invoke (this, EventArgs.Empty); }
  270. /// <inheritdoc/>
  271. public override bool OnLeave (View view)
  272. {
  273. if (_source?.Count > 0
  274. && _selectedItem > -1
  275. && _selectedItem < _source.Count - 1
  276. && _text != _source.ToList () [_selectedItem].ToString ())
  277. {
  278. SetValue (_source.ToList () [_selectedItem].ToString ());
  279. }
  280. if (_autoHide && IsShow && view != this && view != _search && view != _listview)
  281. {
  282. IsShow = false;
  283. HideList ();
  284. }
  285. else if (_listview.TabStop)
  286. {
  287. _listview.TabStop = false;
  288. }
  289. return base.OnLeave (view);
  290. }
  291. /// <summary>Invokes the OnOpenSelectedItem event if it is defined.</summary>
  292. /// <returns></returns>
  293. public virtual bool OnOpenSelectedItem ()
  294. {
  295. string value = _search.Text;
  296. _lastSelectedItem = SelectedItem;
  297. OpenSelectedItem?.Invoke (this, new ListViewItemEventArgs (SelectedItem, value));
  298. return true;
  299. }
  300. /// <summary>Invokes the SelectedChanged event if it is defined.</summary>
  301. /// <returns></returns>
  302. public virtual bool OnSelectedChanged ()
  303. {
  304. // Note: Cannot rely on "listview.SelectedItem != lastSelectedItem" because the list is dynamic.
  305. // So we cannot optimize. Ie: Don't call if not changed
  306. SelectedItemChanged?.Invoke (this, new ListViewItemEventArgs (SelectedItem, _search.Text));
  307. return true;
  308. }
  309. /// <summary>This event is raised when the user Double Clicks on an item or presses ENTER to open the selected item.</summary>
  310. public event EventHandler<ListViewItemEventArgs> OpenSelectedItem;
  311. /// <summary>This event is raised when the selected item in the <see cref="ComboBox"/> has changed.</summary>
  312. public event EventHandler<ListViewItemEventArgs> SelectedItemChanged;
  313. /// <summary>Sets the source of the <see cref="ComboBox"/> to an <see cref="ObservableCollection{T}"/>.</summary>
  314. /// <value>An object implementing the INotifyCollectionChanged and INotifyPropertyChanged interface.</value>
  315. /// <remarks>
  316. /// Use the <see cref="Source"/> property to set a new <see cref="IListDataSource"/> source and use custom
  317. /// rendering.
  318. /// </remarks>
  319. public void SetSource<T> (ObservableCollection<T> source)
  320. {
  321. if (source is null)
  322. {
  323. Source = null;
  324. }
  325. else
  326. {
  327. _listview.SetSource<T> (source);
  328. Source = _listview.Source;
  329. }
  330. }
  331. private bool ActivateSelected ()
  332. {
  333. if (HasItems ())
  334. {
  335. Selected ();
  336. return true;
  337. }
  338. return false;
  339. }
  340. /// <summary>Internal height of dynamic search list</summary>
  341. /// <returns></returns>
  342. private int CalculatetHeight ()
  343. {
  344. if (!IsInitialized || Viewport.Height == 0)
  345. {
  346. return 0;
  347. }
  348. return Math.Min (
  349. Math.Max (Viewport.Height - 1, _minimumHeight - 1),
  350. _searchSet?.Count > 0 ? _searchSet.Count :
  351. IsShow ? Math.Max (Viewport.Height - 1, _minimumHeight - 1) : 0
  352. );
  353. }
  354. private bool CancelSelected ()
  355. {
  356. _search.SetFocus ();
  357. if (ReadOnly || HideDropdownListOnClick)
  358. {
  359. SelectedItem = _lastSelectedItem;
  360. if (SelectedItem > -1 && _listview.Source?.Count > 0)
  361. {
  362. Text = _listview.Source.ToList () [SelectedItem].ToString ();
  363. }
  364. }
  365. else if (!ReadOnly)
  366. {
  367. Text = string.Empty;
  368. _selectedItem = _lastSelectedItem;
  369. OnSelectedChanged ();
  370. }
  371. return Collapse ();
  372. }
  373. /// <summary>Toggles the expand/collapse state of the sublist in the combo box</summary>
  374. /// <returns></returns>
  375. private bool ExpandCollapse ()
  376. {
  377. if (_search.HasFocus || _listview.HasFocus)
  378. {
  379. if (!IsShow)
  380. {
  381. return Expand ();
  382. }
  383. return Collapse ();
  384. }
  385. return false;
  386. }
  387. private void FocusSelectedItem ()
  388. {
  389. _listview.SelectedItem = SelectedItem > -1 ? SelectedItem : 0;
  390. _listview.TabStop = true;
  391. _listview.SetFocus ();
  392. OnExpanded ();
  393. }
  394. private int GetSelectedItemFromSource (string searchText)
  395. {
  396. if (_source is null)
  397. {
  398. return -1;
  399. }
  400. for (var i = 0; i < _searchSet.Count; i++)
  401. {
  402. if (_searchSet [i].ToString () == searchText)
  403. {
  404. return i;
  405. }
  406. }
  407. return -1;
  408. }
  409. private bool HasItems () { return Source?.Count > 0; }
  410. /// <summary>Hide the search list</summary>
  411. /// Consider making public
  412. private void HideList ()
  413. {
  414. if (_lastSelectedItem != _selectedItem)
  415. {
  416. OnOpenSelectedItem ();
  417. }
  418. Reset (true);
  419. _listview.Clear ();
  420. _listview.TabStop = false;
  421. SuperView?.SendSubviewToBack (this);
  422. Rectangle rect = _listview.ViewportToScreen (_listview.IsInitialized ? _listview.Viewport : Rectangle.Empty);
  423. SuperView?.SetNeedsDisplay (rect);
  424. OnCollapsed ();
  425. }
  426. private bool? MoveDown ()
  427. {
  428. if (_search.HasFocus)
  429. {
  430. // jump to list
  431. if (_searchSet?.Count > 0)
  432. {
  433. _listview.TabStop = true;
  434. _listview.SetFocus ();
  435. if (_listview.SelectedItem > -1)
  436. {
  437. SetValue (_searchSet [_listview.SelectedItem]);
  438. }
  439. else
  440. {
  441. _listview.SelectedItem = 0;
  442. }
  443. }
  444. else
  445. {
  446. _listview.TabStop = false;
  447. SuperView?.FocusNext ();
  448. }
  449. return true;
  450. }
  451. return null;
  452. }
  453. private bool? MoveEnd ()
  454. {
  455. if (!IsShow && _search.HasFocus)
  456. {
  457. return null;
  458. }
  459. if (HasItems ())
  460. {
  461. _listview.MoveEnd ();
  462. }
  463. return true;
  464. }
  465. private bool? MoveHome ()
  466. {
  467. if (!IsShow && _search.HasFocus)
  468. {
  469. return null;
  470. }
  471. if (HasItems ())
  472. {
  473. _listview.MoveHome ();
  474. }
  475. return true;
  476. }
  477. private bool? MoveUp ()
  478. {
  479. if (HasItems ())
  480. {
  481. _listview.MoveUp ();
  482. }
  483. return true;
  484. }
  485. private bool? MoveUpList ()
  486. {
  487. if (_listview.HasFocus && _listview.SelectedItem == 0 && _searchSet?.Count > 0) // jump back to search
  488. {
  489. _search.CursorPosition = _search.Text.GetRuneCount ();
  490. _search.SetFocus ();
  491. }
  492. else
  493. {
  494. MoveUp ();
  495. }
  496. return true;
  497. }
  498. private bool PageDown ()
  499. {
  500. if (HasItems ())
  501. {
  502. _listview.MovePageDown ();
  503. }
  504. return true;
  505. }
  506. private bool PageUp ()
  507. {
  508. if (HasItems ())
  509. {
  510. _listview.MovePageUp ();
  511. }
  512. return true;
  513. }
  514. // TODO: Upgrade Combobox to use Dim.Auto instead of all this stuff.
  515. private void ProcessLayout ()
  516. {
  517. if (Viewport.Height < _minimumHeight && (Height is null || Height is DimAbsolute))
  518. {
  519. Height = _minimumHeight;
  520. }
  521. // BUGBUG: This uses Viewport. Should use ContentSize
  522. if ((!_autoHide && Viewport.Width > 0 && _search.Frame.Width != Viewport.Width)
  523. || (_autoHide && Viewport.Width > 0 && _search.Frame.Width != Viewport.Width - 1))
  524. {
  525. _search.Width = _listview.Width = _autoHide ? Viewport.Width - 1 : Viewport.Width;
  526. _listview.Height = CalculatetHeight ();
  527. _search.SetRelativeLayout (GetContentSize ());
  528. _listview.SetRelativeLayout (GetContentSize ());
  529. }
  530. }
  531. /// <summary>Reset to full original list</summary>
  532. private void Reset (bool keepSearchText = false)
  533. {
  534. if (!keepSearchText)
  535. {
  536. SetSearchText (string.Empty);
  537. }
  538. ResetSearchSet ();
  539. _listview.SetSource (_searchSet);
  540. _listview.Height = CalculatetHeight ();
  541. if (Subviews.Count > 0 && HasFocus)
  542. {
  543. _search.SetFocus ();
  544. }
  545. }
  546. private void ResetSearchSet (bool noCopy = false)
  547. {
  548. _listview.SuspendCollectionChangedEvent ();
  549. _searchSet.Clear ();
  550. _listview.ResumeSuspendCollectionChangedEvent ();
  551. if (_autoHide || noCopy)
  552. {
  553. return;
  554. }
  555. SetSearchSet ();
  556. }
  557. // Tell TextField to handle Accept Command (Enter)
  558. void Search_Accept (object sender, HandledEventArgs e) { e.Handled = true; }
  559. private void Search_Changed (object sender, EventArgs e)
  560. {
  561. if (_source is null)
  562. {
  563. // Object initialization
  564. return;
  565. }
  566. ShowHideList (Text);
  567. }
  568. private void ShowHideList (string oldText)
  569. {
  570. if (string.IsNullOrEmpty (_search.Text) && string.IsNullOrEmpty (oldText))
  571. {
  572. ResetSearchSet ();
  573. }
  574. else if (_search.Text != oldText)
  575. {
  576. if (_search.Text.Length < oldText.Length)
  577. {
  578. _selectedItem = -1;
  579. }
  580. IsShow = true;
  581. ResetSearchSet (true);
  582. if (!string.IsNullOrEmpty (_search.Text))
  583. {
  584. _listview.SuspendCollectionChangedEvent ();
  585. foreach (object item in _source.ToList ())
  586. {
  587. // Iterate to preserver object type and force deep copy
  588. if (item.ToString ()
  589. .StartsWith (
  590. _search.Text,
  591. StringComparison.CurrentCultureIgnoreCase
  592. ))
  593. {
  594. _searchSet.Add (item);
  595. }
  596. }
  597. _listview.ResumeSuspendCollectionChangedEvent ();
  598. }
  599. }
  600. if (HasFocus)
  601. {
  602. ShowList ();
  603. }
  604. else if (_autoHide)
  605. {
  606. IsShow = false;
  607. HideList ();
  608. }
  609. }
  610. private void Selected ()
  611. {
  612. IsShow = false;
  613. _listview.TabStop = false;
  614. if (_listview.Source.Count == 0 || (_searchSet?.Count ?? 0) == 0)
  615. {
  616. _text = "";
  617. HideList ();
  618. IsShow = false;
  619. return;
  620. }
  621. SetValue (_listview.SelectedItem > -1 ? _searchSet [_listview.SelectedItem] : _text);
  622. _search.CursorPosition = _search.Text.GetColumns ();
  623. ShowHideList (Text);
  624. OnOpenSelectedItem ();
  625. Reset (true);
  626. HideList ();
  627. IsShow = false;
  628. }
  629. private void SetSearchSet ()
  630. {
  631. if (Source is null)
  632. {
  633. return;
  634. }
  635. // PERF: At the request of @dodexahedron in the comment https://github.com/gui-cs/Terminal.Gui/pull/3552#discussion_r1648112410.
  636. _listview.SuspendCollectionChangedEvent ();
  637. // force deep copy
  638. foreach (object item in Source.ToList ())
  639. {
  640. _searchSet.Add (item);
  641. }
  642. _listview.ResumeSuspendCollectionChangedEvent ();
  643. }
  644. // Sets the search text field Text as well as our own Text property
  645. private void SetSearchText (string value)
  646. {
  647. // _text = value;
  648. _search.Text = value;
  649. _text = value;
  650. }
  651. private void SetValue (object text, bool isFromSelectedItem = false)
  652. {
  653. _search.TextChanged -= Search_Changed;
  654. _text = _search.Text = text.ToString ();
  655. _search.CursorPosition = 0;
  656. _search.TextChanged += Search_Changed;
  657. if (!isFromSelectedItem)
  658. {
  659. _selectedItem = GetSelectedItemFromSource (_text);
  660. OnSelectedChanged ();
  661. }
  662. }
  663. /// <summary>Show the search list</summary>
  664. /// Consider making public
  665. private void ShowList ()
  666. {
  667. _listview.SuspendCollectionChangedEvent ();
  668. _listview.SetSource (_searchSet);
  669. _listview.ResumeSuspendCollectionChangedEvent ();
  670. _listview.Clear ();
  671. _listview.Height = CalculatetHeight ();
  672. SuperView?.BringSubviewToFront (this);
  673. }
  674. private bool UnixEmulation ()
  675. {
  676. // Unix emulation
  677. Reset ();
  678. return true;
  679. }
  680. private class ComboListView : ListView
  681. {
  682. private ComboBox _container;
  683. private bool _hideDropdownListOnClick;
  684. private int _highlighted = -1;
  685. private bool _isFocusing;
  686. public ComboListView (ComboBox container, bool hideDropdownListOnClick) { SetInitialProperties (container, hideDropdownListOnClick); }
  687. public ComboListView (ComboBox container, ObservableCollection<string> source, bool hideDropdownListOnClick)
  688. {
  689. Source = new ListWrapper<string> (source);
  690. SetInitialProperties (container, hideDropdownListOnClick);
  691. }
  692. public bool HideDropdownListOnClick
  693. {
  694. get => _hideDropdownListOnClick;
  695. set => _hideDropdownListOnClick = WantContinuousButtonPressed = value;
  696. }
  697. protected internal override bool OnMouseEvent (MouseEvent me)
  698. {
  699. var res = false;
  700. bool isMousePositionValid = IsMousePositionValid (me);
  701. if (isMousePositionValid)
  702. {
  703. res = base.OnMouseEvent (me);
  704. }
  705. if (HideDropdownListOnClick && me.Flags == MouseFlags.Button1Clicked)
  706. {
  707. if (!isMousePositionValid && !_isFocusing)
  708. {
  709. _container.IsShow = false;
  710. _container.HideList ();
  711. }
  712. else if (isMousePositionValid)
  713. {
  714. OnOpenSelectedItem ();
  715. }
  716. else
  717. {
  718. _isFocusing = false;
  719. }
  720. return true;
  721. }
  722. if (me.Flags == MouseFlags.ReportMousePosition && HideDropdownListOnClick)
  723. {
  724. if (isMousePositionValid)
  725. {
  726. _highlighted = Math.Min (TopItem + me.Position.Y, Source.Count);
  727. SetNeedsDisplay ();
  728. }
  729. _isFocusing = false;
  730. return true;
  731. }
  732. return res;
  733. }
  734. public override void OnDrawContent (Rectangle viewport)
  735. {
  736. Attribute current = ColorScheme.Focus;
  737. Driver.SetAttribute (current);
  738. Move (0, 0);
  739. Rectangle f = Frame;
  740. int item = TopItem;
  741. bool focused = HasFocus;
  742. int col = AllowsMarking ? 2 : 0;
  743. int start = LeftItem;
  744. for (var row = 0; row < f.Height; row++, item++)
  745. {
  746. bool isSelected = item == _container.SelectedItem;
  747. bool isHighlighted = _hideDropdownListOnClick && item == _highlighted;
  748. Attribute newcolor;
  749. if (isHighlighted || (isSelected && !_hideDropdownListOnClick))
  750. {
  751. newcolor = focused ? ColorScheme.Focus : ColorScheme.HotNormal;
  752. }
  753. else if (isSelected && _hideDropdownListOnClick)
  754. {
  755. newcolor = focused ? ColorScheme.HotFocus : ColorScheme.HotNormal;
  756. }
  757. else
  758. {
  759. newcolor = focused ? GetNormalColor () : GetNormalColor ();
  760. }
  761. if (newcolor != current)
  762. {
  763. Driver.SetAttribute (newcolor);
  764. current = newcolor;
  765. }
  766. Move (0, row);
  767. if (Source is null || item >= Source.Count)
  768. {
  769. for (var c = 0; c < f.Width; c++)
  770. {
  771. Driver.AddRune ((Rune)' ');
  772. }
  773. }
  774. else
  775. {
  776. var rowEventArgs = new ListViewRowEventArgs (item);
  777. OnRowRender (rowEventArgs);
  778. if (rowEventArgs.RowAttribute is { } && current != rowEventArgs.RowAttribute)
  779. {
  780. current = (Attribute)rowEventArgs.RowAttribute;
  781. Driver.SetAttribute (current);
  782. }
  783. if (AllowsMarking)
  784. {
  785. Driver.AddRune (
  786. Source.IsMarked (item) ? AllowsMultipleSelection ? Glyphs.Checked : Glyphs.Selected :
  787. AllowsMultipleSelection ? Glyphs.UnChecked : Glyphs.UnSelected
  788. );
  789. Driver.AddRune ((Rune)' ');
  790. }
  791. Source.Render (this, Driver, isSelected, item, col, row, f.Width - col, start);
  792. }
  793. }
  794. }
  795. public override bool OnEnter (View view)
  796. {
  797. if (_hideDropdownListOnClick)
  798. {
  799. _isFocusing = true;
  800. _highlighted = _container.SelectedItem;
  801. Application.GrabMouse (this);
  802. }
  803. return base.OnEnter (view);
  804. }
  805. public override bool OnLeave (View view)
  806. {
  807. if (_hideDropdownListOnClick)
  808. {
  809. _isFocusing = false;
  810. _highlighted = _container.SelectedItem;
  811. Application.UngrabMouse ();
  812. }
  813. return base.OnLeave (view);
  814. }
  815. public override bool OnSelectedChanged ()
  816. {
  817. bool res = base.OnSelectedChanged ();
  818. _highlighted = SelectedItem;
  819. return res;
  820. }
  821. private bool IsMousePositionValid (MouseEvent me)
  822. {
  823. if (me.Position.X >= 0 && me.Position.X < Frame.Width && me.Position.Y >= 0 && me.Position.Y < Frame.Height)
  824. {
  825. return true;
  826. }
  827. return false;
  828. }
  829. private void SetInitialProperties (ComboBox container, bool hideDropdownListOnClick)
  830. {
  831. _container = container
  832. ?? throw new ArgumentNullException (
  833. nameof (container),
  834. "ComboBox container cannot be null."
  835. );
  836. HideDropdownListOnClick = hideDropdownListOnClick;
  837. AddCommand (Command.LineUp, () => _container.MoveUpList ());
  838. }
  839. }
  840. }