ComboBox.cs 27 KB

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