ComboBox.cs 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969
  1. //
  2. // ComboBox.cs: ComboBox control
  3. //
  4. // Authors:
  5. // Ross Ferguson ([email protected])
  6. //
  7. using System.Collections;
  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 IList _searchset = new List<object> ();
  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. Search_Changed (this, new StateEventArgs<string> (string.Empty, 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"/> to set a new <see cref="IList"/> 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. SelectedItem = -1;
  163. _search.Text = string.Empty;
  164. Search_Changed (this, new StateEventArgs<string> (string.Empty, _search.Text));
  165. SetNeedsDisplay ();
  166. }
  167. }
  168. }
  169. /// <summary>The currently selected list item</summary>
  170. public new string Text
  171. {
  172. get => _text;
  173. set => SetSearchText (value);
  174. }
  175. /// <summary>
  176. /// Collapses the drop down list. Returns true if the state chagned or false if it was already collapsed and no
  177. /// action was taken
  178. /// </summary>
  179. public virtual bool Collapse ()
  180. {
  181. if (!IsShow)
  182. {
  183. return false;
  184. }
  185. IsShow = false;
  186. HideList ();
  187. return true;
  188. }
  189. /// <summary>This event is raised when the drop-down list is collapsed.</summary>
  190. public event EventHandler Collapsed;
  191. /// <summary>
  192. /// Expands the drop down list. Returns true if the state chagned or false if it was already expanded and no
  193. /// action was taken
  194. /// </summary>
  195. public virtual bool Expand ()
  196. {
  197. if (IsShow)
  198. {
  199. return false;
  200. }
  201. SetSearchSet ();
  202. IsShow = true;
  203. ShowList ();
  204. FocusSelectedItem ();
  205. return true;
  206. }
  207. /// <summary>This event is raised when the drop-down list is expanded.</summary>
  208. public event EventHandler Expanded;
  209. /// <inheritdoc/>
  210. protected internal override bool OnMouseEvent (MouseEvent me)
  211. {
  212. if (me.Position.X == Viewport.Right - 1
  213. && me.Position.Y == Viewport.Top
  214. && me.Flags == MouseFlags.Button1Pressed
  215. && _autoHide)
  216. {
  217. if (IsShow)
  218. {
  219. IsShow = false;
  220. HideList ();
  221. }
  222. else
  223. {
  224. SetSearchSet ();
  225. IsShow = true;
  226. ShowList ();
  227. FocusSelectedItem ();
  228. }
  229. return me.Handled = true;
  230. }
  231. if (me.Flags == MouseFlags.Button1Pressed)
  232. {
  233. if (!_search.HasFocus)
  234. {
  235. _search.SetFocus ();
  236. }
  237. return me.Handled = true;
  238. }
  239. return false;
  240. }
  241. /// <summary>Virtual method which invokes the <see cref="Collapsed"/> event.</summary>
  242. public virtual void OnCollapsed () { Collapsed?.Invoke (this, EventArgs.Empty); }
  243. /// <inheritdoc/>
  244. public override void OnDrawContent (Rectangle viewport)
  245. {
  246. base.OnDrawContent (viewport);
  247. if (!_autoHide)
  248. {
  249. return;
  250. }
  251. Driver.SetAttribute (ColorScheme.Focus);
  252. Move (Viewport.Right - 1, 0);
  253. Driver.AddRune (Glyphs.DownArrow);
  254. }
  255. /// <inheritdoc/>
  256. public override bool OnEnter (View view)
  257. {
  258. if (!_search.HasFocus && !_listview.HasFocus)
  259. {
  260. _search.SetFocus ();
  261. }
  262. _search.CursorPosition = _search.Text.GetRuneCount ();
  263. return base.OnEnter (view);
  264. }
  265. /// <summary>Virtual method which invokes the <see cref="Expanded"/> event.</summary>
  266. public virtual void OnExpanded () { Expanded?.Invoke (this, EventArgs.Empty); }
  267. /// <inheritdoc/>
  268. public override bool OnLeave (View view)
  269. {
  270. if (_source?.Count > 0
  271. && _selectedItem > -1
  272. && _selectedItem < _source.Count - 1
  273. && _text != _source.ToList () [_selectedItem].ToString ())
  274. {
  275. SetValue (_source.ToList () [_selectedItem].ToString ());
  276. }
  277. if (_autoHide && IsShow && view != this && view != _search && view != _listview)
  278. {
  279. IsShow = false;
  280. HideList ();
  281. }
  282. else if (_listview.TabStop)
  283. {
  284. _listview.TabStop = false;
  285. }
  286. return base.OnLeave (view);
  287. }
  288. /// <summary>Invokes the OnOpenSelectedItem event if it is defined.</summary>
  289. /// <returns></returns>
  290. public virtual bool OnOpenSelectedItem ()
  291. {
  292. string value = _search.Text;
  293. _lastSelectedItem = SelectedItem;
  294. OpenSelectedItem?.Invoke (this, new ListViewItemEventArgs (SelectedItem, value));
  295. return true;
  296. }
  297. /// <summary>Invokes the SelectedChanged event if it is defined.</summary>
  298. /// <returns></returns>
  299. public virtual bool OnSelectedChanged ()
  300. {
  301. // Note: Cannot rely on "listview.SelectedItem != lastSelectedItem" because the list is dynamic.
  302. // So we cannot optimize. Ie: Don't call if not changed
  303. SelectedItemChanged?.Invoke (this, new ListViewItemEventArgs (SelectedItem, _search.Text));
  304. return true;
  305. }
  306. /// <summary>This event is raised when the user Double Clicks on an item or presses ENTER to open the selected item.</summary>
  307. public event EventHandler<ListViewItemEventArgs> OpenSelectedItem;
  308. /// <summary>This event is raised when the selected item in the <see cref="ComboBox"/> has changed.</summary>
  309. public event EventHandler<ListViewItemEventArgs> SelectedItemChanged;
  310. /// <summary>Sets the source of the <see cref="ComboBox"/> to an <see cref="IList"/>.</summary>
  311. /// <value>An object implementing the IList interface.</value>
  312. /// <remarks>
  313. /// Use the <see cref="Source"/> property to set a new <see cref="IListDataSource"/> source and use custome
  314. /// rendering.
  315. /// </remarks>
  316. public void SetSource (IList source)
  317. {
  318. if (source is null)
  319. {
  320. Source = null;
  321. }
  322. else
  323. {
  324. _listview.SetSource (source);
  325. Source = _listview.Source;
  326. }
  327. }
  328. private bool ActivateSelected ()
  329. {
  330. if (HasItems ())
  331. {
  332. Selected ();
  333. return true;
  334. }
  335. return false;
  336. }
  337. /// <summary>Internal height of dynamic search list</summary>
  338. /// <returns></returns>
  339. private int CalculatetHeight ()
  340. {
  341. if (!IsInitialized || Viewport.Height == 0)
  342. {
  343. return 0;
  344. }
  345. return Math.Min (
  346. Math.Max (Viewport.Height - 1, _minimumHeight - 1),
  347. _searchset?.Count > 0 ? _searchset.Count :
  348. IsShow ? Math.Max (Viewport.Height - 1, _minimumHeight - 1) : 0
  349. );
  350. }
  351. private bool CancelSelected ()
  352. {
  353. _search.SetFocus ();
  354. if (ReadOnly || HideDropdownListOnClick)
  355. {
  356. SelectedItem = _lastSelectedItem;
  357. if (SelectedItem > -1 && _listview.Source?.Count > 0)
  358. {
  359. _search.Text = _text = _listview.Source.ToList () [SelectedItem].ToString ();
  360. }
  361. }
  362. else if (!ReadOnly)
  363. {
  364. _search.Text = _text = "";
  365. _selectedItem = _lastSelectedItem;
  366. OnSelectedChanged ();
  367. }
  368. return Collapse ();
  369. }
  370. /// <summary>Toggles the expand/collapse state of the sublist in the combo box</summary>
  371. /// <returns></returns>
  372. private bool ExpandCollapse ()
  373. {
  374. if (_search.HasFocus || _listview.HasFocus)
  375. {
  376. if (!IsShow)
  377. {
  378. return Expand ();
  379. }
  380. return Collapse ();
  381. }
  382. return false;
  383. }
  384. private void FocusSelectedItem ()
  385. {
  386. _listview.SelectedItem = SelectedItem > -1 ? SelectedItem : 0;
  387. _listview.TabStop = true;
  388. _listview.SetFocus ();
  389. OnExpanded ();
  390. }
  391. private int GetSelectedItemFromSource (string searchText)
  392. {
  393. if (_source is null)
  394. {
  395. return -1;
  396. }
  397. for (var i = 0; i < _searchset.Count; i++)
  398. {
  399. if (_searchset [i].ToString () == searchText)
  400. {
  401. return i;
  402. }
  403. }
  404. return -1;
  405. }
  406. private bool HasItems () { return Source?.Count > 0; }
  407. /// <summary>Hide the search list</summary>
  408. /// Consider making public
  409. private void HideList ()
  410. {
  411. if (_lastSelectedItem != _selectedItem)
  412. {
  413. OnOpenSelectedItem ();
  414. }
  415. Reset (true);
  416. _listview.Clear ();
  417. _listview.TabStop = false;
  418. SuperView?.SendSubviewToBack (this);
  419. Rectangle rect = _listview.ViewportToScreen (_listview.IsInitialized ? _listview.Viewport : Rectangle.Empty);
  420. SuperView?.SetNeedsDisplay (rect);
  421. OnCollapsed ();
  422. }
  423. private bool? MoveDown ()
  424. {
  425. if (_search.HasFocus)
  426. {
  427. // jump to list
  428. if (_searchset?.Count > 0)
  429. {
  430. _listview.TabStop = true;
  431. _listview.SetFocus ();
  432. if (_listview.SelectedItem > -1)
  433. {
  434. SetValue (_searchset [_listview.SelectedItem]);
  435. }
  436. else
  437. {
  438. _listview.SelectedItem = 0;
  439. }
  440. }
  441. else
  442. {
  443. _listview.TabStop = false;
  444. SuperView?.FocusNext ();
  445. }
  446. return true;
  447. }
  448. return null;
  449. }
  450. private bool? MoveEnd ()
  451. {
  452. if (!IsShow && _search.HasFocus)
  453. {
  454. return null;
  455. }
  456. if (HasItems ())
  457. {
  458. _listview.MoveEnd ();
  459. }
  460. return true;
  461. }
  462. private bool? MoveHome ()
  463. {
  464. if (!IsShow && _search.HasFocus)
  465. {
  466. return null;
  467. }
  468. if (HasItems ())
  469. {
  470. _listview.MoveHome ();
  471. }
  472. return true;
  473. }
  474. private bool? MoveUp ()
  475. {
  476. if (HasItems ())
  477. {
  478. _listview.MoveUp ();
  479. }
  480. return true;
  481. }
  482. private bool? MoveUpList ()
  483. {
  484. if (_listview.HasFocus && _listview.SelectedItem == 0 && _searchset?.Count > 0) // jump back to search
  485. {
  486. _search.CursorPosition = _search.Text.GetRuneCount ();
  487. _search.SetFocus ();
  488. }
  489. else
  490. {
  491. MoveUp ();
  492. }
  493. return true;
  494. }
  495. private bool PageDown ()
  496. {
  497. if (HasItems ())
  498. {
  499. _listview.MovePageDown ();
  500. }
  501. return true;
  502. }
  503. private bool PageUp ()
  504. {
  505. if (HasItems ())
  506. {
  507. _listview.MovePageUp ();
  508. }
  509. return true;
  510. }
  511. // TODO: Upgrade Combobox to use Dim.Auto instead of all this stuff.
  512. private void ProcessLayout ()
  513. {
  514. if (Viewport.Height < _minimumHeight && (Height is null || Height is DimAbsolute))
  515. {
  516. Height = _minimumHeight;
  517. }
  518. // BUGBUG: This uses Viewport. Should use ContentSize
  519. if ((!_autoHide && Viewport.Width > 0 && _search.Frame.Width != Viewport.Width)
  520. || (_autoHide && Viewport.Width > 0 && _search.Frame.Width != Viewport.Width - 1))
  521. {
  522. _search.Width = _listview.Width = _autoHide ? Viewport.Width - 1 : Viewport.Width;
  523. _listview.Height = CalculatetHeight ();
  524. _search.SetRelativeLayout (GetContentSize ());
  525. _listview.SetRelativeLayout (GetContentSize ());
  526. }
  527. }
  528. /// <summary>Reset to full original list</summary>
  529. private void Reset (bool keepSearchText = false)
  530. {
  531. if (!keepSearchText)
  532. {
  533. SetSearchText (string.Empty);
  534. }
  535. ResetSearchSet ();
  536. _listview.SetSource (_searchset);
  537. _listview.Height = CalculatetHeight ();
  538. if (Subviews.Count > 0 && HasFocus)
  539. {
  540. _search.SetFocus ();
  541. }
  542. }
  543. private void ResetSearchSet (bool noCopy = false)
  544. {
  545. _searchset.Clear ();
  546. if (_autoHide || noCopy)
  547. {
  548. return;
  549. }
  550. SetSearchSet ();
  551. }
  552. // Tell TextField to handle Accept Command (Enter)
  553. void Search_Accept (object sender, CancelEventArgs e) { e.Cancel = true; }
  554. private void Search_Changed (object sender, StateEventArgs<string> e)
  555. {
  556. if (_source is null)
  557. {
  558. // Object initialization
  559. return;
  560. }
  561. if (string.IsNullOrEmpty (_search.Text) && string.IsNullOrEmpty (e.OldValue))
  562. {
  563. ResetSearchSet ();
  564. }
  565. else if (_search.Text != e.OldValue)
  566. {
  567. if (_search.Text.Length < e.OldValue.Length)
  568. {
  569. _selectedItem = -1;
  570. }
  571. IsShow = true;
  572. ResetSearchSet (true);
  573. foreach (object item in _source.ToList ())
  574. {
  575. // Iterate to preserver object type and force deep copy
  576. if (item.ToString ()
  577. .StartsWith (
  578. _search.Text,
  579. StringComparison.CurrentCultureIgnoreCase
  580. ))
  581. {
  582. _searchset.Add (item);
  583. }
  584. }
  585. }
  586. if (HasFocus)
  587. {
  588. ShowList ();
  589. }
  590. else if (_autoHide)
  591. {
  592. IsShow = false;
  593. HideList ();
  594. }
  595. }
  596. private void Selected ()
  597. {
  598. IsShow = false;
  599. _listview.TabStop = false;
  600. if (_listview.Source.Count == 0 || (_searchset?.Count ?? 0) == 0)
  601. {
  602. _text = "";
  603. HideList ();
  604. IsShow = false;
  605. return;
  606. }
  607. SetValue (_listview.SelectedItem > -1 ? _searchset [_listview.SelectedItem] : _text);
  608. _search.CursorPosition = _search.Text.GetColumns ();
  609. Search_Changed (this, new StateEventArgs<string> (_search.Text, _search.Text));
  610. OnOpenSelectedItem ();
  611. Reset (true);
  612. HideList ();
  613. IsShow = false;
  614. }
  615. private void SetSearchSet ()
  616. {
  617. if (Source is null)
  618. {
  619. return;
  620. }
  621. // force deep copy
  622. foreach (object item in Source.ToList ())
  623. {
  624. _searchset.Add (item);
  625. }
  626. }
  627. private void SetSearchText (string value) { _search.Text = _text = value; }
  628. private void SetValue (object text, bool isFromSelectedItem = false)
  629. {
  630. _search.TextChanged -= Search_Changed;
  631. _text = _search.Text = text.ToString ();
  632. _search.CursorPosition = 0;
  633. _search.TextChanged += Search_Changed;
  634. if (!isFromSelectedItem)
  635. {
  636. _selectedItem = GetSelectedItemFromSource (_text);
  637. OnSelectedChanged ();
  638. }
  639. }
  640. /// <summary>Show the search list</summary>
  641. /// Consider making public
  642. private void ShowList ()
  643. {
  644. _listview.SetSource (_searchset);
  645. _listview.Clear ();
  646. _listview.Height = CalculatetHeight ();
  647. SuperView?.BringSubviewToFront (this);
  648. }
  649. private bool UnixEmulation ()
  650. {
  651. // Unix emulation
  652. Reset ();
  653. return true;
  654. }
  655. private class ComboListView : ListView
  656. {
  657. private ComboBox _container;
  658. private bool _hideDropdownListOnClick;
  659. private int _highlighted = -1;
  660. private bool _isFocusing;
  661. public ComboListView (ComboBox container, bool hideDropdownListOnClick) { SetInitialProperties (container, hideDropdownListOnClick); }
  662. public ComboListView (ComboBox container, IList source, bool hideDropdownListOnClick)
  663. {
  664. Source = new ListWrapper (source);
  665. SetInitialProperties (container, hideDropdownListOnClick);
  666. }
  667. public bool HideDropdownListOnClick
  668. {
  669. get => _hideDropdownListOnClick;
  670. set => _hideDropdownListOnClick = WantContinuousButtonPressed = value;
  671. }
  672. protected internal override bool OnMouseEvent (MouseEvent me)
  673. {
  674. var res = false;
  675. bool isMousePositionValid = IsMousePositionValid (me);
  676. if (isMousePositionValid)
  677. {
  678. res = base.OnMouseEvent (me);
  679. }
  680. if (HideDropdownListOnClick && me.Flags == MouseFlags.Button1Clicked)
  681. {
  682. if (!isMousePositionValid && !_isFocusing)
  683. {
  684. _container.IsShow = false;
  685. _container.HideList ();
  686. }
  687. else if (isMousePositionValid)
  688. {
  689. OnOpenSelectedItem ();
  690. }
  691. else
  692. {
  693. _isFocusing = false;
  694. }
  695. return true;
  696. }
  697. if (me.Flags == MouseFlags.ReportMousePosition && HideDropdownListOnClick)
  698. {
  699. if (isMousePositionValid)
  700. {
  701. _highlighted = Math.Min (TopItem + me.Position.Y, Source.Count);
  702. SetNeedsDisplay ();
  703. }
  704. _isFocusing = false;
  705. return true;
  706. }
  707. return res;
  708. }
  709. public override void OnDrawContent (Rectangle viewport)
  710. {
  711. Attribute current = ColorScheme.Focus;
  712. Driver.SetAttribute (current);
  713. Move (0, 0);
  714. Rectangle f = Frame;
  715. int item = TopItem;
  716. bool focused = HasFocus;
  717. int col = AllowsMarking ? 2 : 0;
  718. int start = LeftItem;
  719. for (var row = 0; row < f.Height; row++, item++)
  720. {
  721. bool isSelected = item == _container.SelectedItem;
  722. bool isHighlighted = _hideDropdownListOnClick && item == _highlighted;
  723. Attribute newcolor;
  724. if (isHighlighted || (isSelected && !_hideDropdownListOnClick))
  725. {
  726. newcolor = focused ? ColorScheme.Focus : ColorScheme.HotNormal;
  727. }
  728. else if (isSelected && _hideDropdownListOnClick)
  729. {
  730. newcolor = focused ? ColorScheme.HotFocus : ColorScheme.HotNormal;
  731. }
  732. else
  733. {
  734. newcolor = focused ? GetNormalColor () : GetNormalColor ();
  735. }
  736. if (newcolor != current)
  737. {
  738. Driver.SetAttribute (newcolor);
  739. current = newcolor;
  740. }
  741. Move (0, row);
  742. if (Source is null || item >= Source.Count)
  743. {
  744. for (var c = 0; c < f.Width; c++)
  745. {
  746. Driver.AddRune ((Rune)' ');
  747. }
  748. }
  749. else
  750. {
  751. var rowEventArgs = new ListViewRowEventArgs (item);
  752. OnRowRender (rowEventArgs);
  753. if (rowEventArgs.RowAttribute is { } && current != rowEventArgs.RowAttribute)
  754. {
  755. current = (Attribute)rowEventArgs.RowAttribute;
  756. Driver.SetAttribute (current);
  757. }
  758. if (AllowsMarking)
  759. {
  760. Driver.AddRune (
  761. Source.IsMarked (item) ? AllowsMultipleSelection ? Glyphs.Checked : Glyphs.Selected :
  762. AllowsMultipleSelection ? Glyphs.UnChecked : Glyphs.UnSelected
  763. );
  764. Driver.AddRune ((Rune)' ');
  765. }
  766. Source.Render (this, Driver, isSelected, item, col, row, f.Width - col, start);
  767. }
  768. }
  769. }
  770. public override bool OnEnter (View view)
  771. {
  772. if (_hideDropdownListOnClick)
  773. {
  774. _isFocusing = true;
  775. _highlighted = _container.SelectedItem;
  776. Application.GrabMouse (this);
  777. }
  778. return base.OnEnter (view);
  779. }
  780. public override bool OnLeave (View view)
  781. {
  782. if (_hideDropdownListOnClick)
  783. {
  784. _isFocusing = false;
  785. _highlighted = _container.SelectedItem;
  786. Application.UngrabMouse ();
  787. }
  788. return base.OnLeave (view);
  789. }
  790. public override bool OnSelectedChanged ()
  791. {
  792. bool res = base.OnSelectedChanged ();
  793. _highlighted = SelectedItem;
  794. return res;
  795. }
  796. private bool IsMousePositionValid (MouseEvent me)
  797. {
  798. if (me.Position.X >= 0 && me.Position.X < Frame.Width && me.Position.Y >= 0 && me.Position.Y < Frame.Height)
  799. {
  800. return true;
  801. }
  802. return false;
  803. }
  804. private void SetInitialProperties (ComboBox container, bool hideDropdownListOnClick)
  805. {
  806. _container = container
  807. ?? throw new ArgumentNullException (
  808. nameof (container),
  809. "ComboBox container cannot be null."
  810. );
  811. HideDropdownListOnClick = hideDropdownListOnClick;
  812. AddCommand (Command.LineUp, () => _container.MoveUpList ());
  813. }
  814. }
  815. }