2
0

ComboBox.cs 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967
  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.X == Viewport.Right - 1
  213. && me.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 true;
  230. }
  231. if (me.Flags == MouseFlags.Button1Pressed)
  232. {
  233. if (!_search.HasFocus)
  234. {
  235. _search.SetFocus ();
  236. }
  237. return 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 (_listview.IsInitialized ? _listview.Viewport : Rectangle.Empty);
  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. private void ProcessLayout ()
  512. {
  513. if (Viewport.Height < _minimumHeight && (Height is null || Height is Dim.DimAbsolute))
  514. {
  515. Height = _minimumHeight;
  516. }
  517. if ((!_autoHide && Viewport.Width > 0 && _search.Frame.Width != Viewport.Width)
  518. || (_autoHide && Viewport.Width > 0 && _search.Frame.Width != Viewport.Width - 1))
  519. {
  520. _search.Width = _listview.Width = _autoHide ? Viewport.Width - 1 : Viewport.Width;
  521. _listview.Height = CalculatetHeight ();
  522. _search.SetRelativeLayout (Viewport);
  523. _listview.SetRelativeLayout (Viewport);
  524. }
  525. }
  526. /// <summary>Reset to full original list</summary>
  527. private void Reset (bool keepSearchText = false)
  528. {
  529. if (!keepSearchText)
  530. {
  531. SetSearchText (string.Empty);
  532. }
  533. ResetSearchSet ();
  534. _listview.SetSource (_searchset);
  535. _listview.Height = CalculatetHeight ();
  536. if (Subviews.Count > 0 && HasFocus)
  537. {
  538. _search.SetFocus ();
  539. }
  540. }
  541. private void ResetSearchSet (bool noCopy = false)
  542. {
  543. _searchset.Clear ();
  544. if (_autoHide || noCopy)
  545. {
  546. return;
  547. }
  548. SetSearchSet ();
  549. }
  550. // Tell TextField to handle Accept Command (Enter)
  551. void Search_Accept (object sender, CancelEventArgs e) { e.Cancel = true; }
  552. private void Search_Changed (object sender, StateEventArgs<string> e)
  553. {
  554. if (_source is null)
  555. {
  556. // Object initialization
  557. return;
  558. }
  559. if (string.IsNullOrEmpty (_search.Text) && string.IsNullOrEmpty (e.OldValue))
  560. {
  561. ResetSearchSet ();
  562. }
  563. else if (_search.Text != e.OldValue)
  564. {
  565. if (_search.Text.Length < e.OldValue.Length)
  566. {
  567. _selectedItem = -1;
  568. }
  569. IsShow = true;
  570. ResetSearchSet (true);
  571. foreach (object item in _source.ToList ())
  572. {
  573. // Iterate to preserver object type and force deep copy
  574. if (item.ToString ()
  575. .StartsWith (
  576. _search.Text,
  577. StringComparison.CurrentCultureIgnoreCase
  578. ))
  579. {
  580. _searchset.Add (item);
  581. }
  582. }
  583. }
  584. if (HasFocus)
  585. {
  586. ShowList ();
  587. }
  588. else if (_autoHide)
  589. {
  590. IsShow = false;
  591. HideList ();
  592. }
  593. }
  594. private void Selected ()
  595. {
  596. IsShow = false;
  597. _listview.TabStop = false;
  598. if (_listview.Source.Count == 0 || (_searchset?.Count ?? 0) == 0)
  599. {
  600. _text = "";
  601. HideList ();
  602. IsShow = false;
  603. return;
  604. }
  605. SetValue (_listview.SelectedItem > -1 ? _searchset [_listview.SelectedItem] : _text);
  606. _search.CursorPosition = _search.Text.GetColumns ();
  607. Search_Changed (this, new StateEventArgs<string> (_search.Text, _search.Text));
  608. OnOpenSelectedItem ();
  609. Reset (true);
  610. HideList ();
  611. IsShow = false;
  612. }
  613. private void SetSearchSet ()
  614. {
  615. if (Source is null)
  616. {
  617. return;
  618. }
  619. // force deep copy
  620. foreach (object item in Source.ToList ())
  621. {
  622. _searchset.Add (item);
  623. }
  624. }
  625. private void SetSearchText (string value) { _search.Text = _text = value; }
  626. private void SetValue (object text, bool isFromSelectedItem = false)
  627. {
  628. _search.TextChanged -= Search_Changed;
  629. _text = _search.Text = text.ToString ();
  630. _search.CursorPosition = 0;
  631. _search.TextChanged += Search_Changed;
  632. if (!isFromSelectedItem)
  633. {
  634. _selectedItem = GetSelectedItemFromSource (_text);
  635. OnSelectedChanged ();
  636. }
  637. }
  638. /// <summary>Show the search list</summary>
  639. /// Consider making public
  640. private void ShowList ()
  641. {
  642. _listview.SetSource (_searchset);
  643. _listview.Clear (Viewport); // Ensure list shrinks in Dialog as you type
  644. _listview.Height = CalculatetHeight ();
  645. SuperView?.BringSubviewToFront (this);
  646. }
  647. private bool UnixEmulation ()
  648. {
  649. // Unix emulation
  650. Reset ();
  651. return true;
  652. }
  653. private class ComboListView : ListView
  654. {
  655. private ComboBox _container;
  656. private bool _hideDropdownListOnClick;
  657. private int _highlighted = -1;
  658. private bool _isFocusing;
  659. public ComboListView (ComboBox container, bool hideDropdownListOnClick) { SetInitialProperties (container, hideDropdownListOnClick); }
  660. public ComboListView (ComboBox container, IList source, bool hideDropdownListOnClick)
  661. {
  662. Source = new ListWrapper (source);
  663. SetInitialProperties (container, hideDropdownListOnClick);
  664. }
  665. public bool HideDropdownListOnClick
  666. {
  667. get => _hideDropdownListOnClick;
  668. set => _hideDropdownListOnClick = WantContinuousButtonPressed = value;
  669. }
  670. protected internal override bool OnMouseEvent (MouseEvent me)
  671. {
  672. var res = false;
  673. bool isMousePositionValid = IsMousePositionValid (me);
  674. if (isMousePositionValid)
  675. {
  676. res = base.OnMouseEvent (me);
  677. }
  678. if (HideDropdownListOnClick && me.Flags == MouseFlags.Button1Clicked)
  679. {
  680. if (!isMousePositionValid && !_isFocusing)
  681. {
  682. _container.IsShow = false;
  683. _container.HideList ();
  684. }
  685. else if (isMousePositionValid)
  686. {
  687. OnOpenSelectedItem ();
  688. }
  689. else
  690. {
  691. _isFocusing = false;
  692. }
  693. return true;
  694. }
  695. if (me.Flags == MouseFlags.ReportMousePosition && HideDropdownListOnClick)
  696. {
  697. if (isMousePositionValid)
  698. {
  699. _highlighted = Math.Min (TopItem + me.Y, Source.Count);
  700. SetNeedsDisplay ();
  701. }
  702. _isFocusing = false;
  703. return true;
  704. }
  705. return res;
  706. }
  707. public override void OnDrawContent (Rectangle viewport)
  708. {
  709. Attribute current = ColorScheme.Focus;
  710. Driver.SetAttribute (current);
  711. Move (0, 0);
  712. Rectangle f = Frame;
  713. int item = TopItem;
  714. bool focused = HasFocus;
  715. int col = AllowsMarking ? 2 : 0;
  716. int start = LeftItem;
  717. for (var row = 0; row < f.Height; row++, item++)
  718. {
  719. bool isSelected = item == _container.SelectedItem;
  720. bool isHighlighted = _hideDropdownListOnClick && item == _highlighted;
  721. Attribute newcolor;
  722. if (isHighlighted || (isSelected && !_hideDropdownListOnClick))
  723. {
  724. newcolor = focused ? ColorScheme.Focus : ColorScheme.HotNormal;
  725. }
  726. else if (isSelected && _hideDropdownListOnClick)
  727. {
  728. newcolor = focused ? ColorScheme.HotFocus : ColorScheme.HotNormal;
  729. }
  730. else
  731. {
  732. newcolor = focused ? GetNormalColor () : GetNormalColor ();
  733. }
  734. if (newcolor != current)
  735. {
  736. Driver.SetAttribute (newcolor);
  737. current = newcolor;
  738. }
  739. Move (0, row);
  740. if (Source is null || item >= Source.Count)
  741. {
  742. for (var c = 0; c < f.Width; c++)
  743. {
  744. Driver.AddRune ((Rune)' ');
  745. }
  746. }
  747. else
  748. {
  749. var rowEventArgs = new ListViewRowEventArgs (item);
  750. OnRowRender (rowEventArgs);
  751. if (rowEventArgs.RowAttribute is { } && current != rowEventArgs.RowAttribute)
  752. {
  753. current = (Attribute)rowEventArgs.RowAttribute;
  754. Driver.SetAttribute (current);
  755. }
  756. if (AllowsMarking)
  757. {
  758. Driver.AddRune (
  759. Source.IsMarked (item) ? AllowsMultipleSelection ? Glyphs.Checked : Glyphs.Selected :
  760. AllowsMultipleSelection ? Glyphs.UnChecked : Glyphs.UnSelected
  761. );
  762. Driver.AddRune ((Rune)' ');
  763. }
  764. Source.Render (this, Driver, isSelected, item, col, row, f.Width - col, start);
  765. }
  766. }
  767. }
  768. public override bool OnEnter (View view)
  769. {
  770. if (_hideDropdownListOnClick)
  771. {
  772. _isFocusing = true;
  773. _highlighted = _container.SelectedItem;
  774. Application.GrabMouse (this);
  775. }
  776. return base.OnEnter (view);
  777. }
  778. public override bool OnLeave (View view)
  779. {
  780. if (_hideDropdownListOnClick)
  781. {
  782. _isFocusing = false;
  783. _highlighted = _container.SelectedItem;
  784. Application.UngrabMouse ();
  785. }
  786. return base.OnLeave (view);
  787. }
  788. public override bool OnSelectedChanged ()
  789. {
  790. bool res = base.OnSelectedChanged ();
  791. _highlighted = SelectedItem;
  792. return res;
  793. }
  794. private bool IsMousePositionValid (MouseEvent me)
  795. {
  796. if (me.X >= 0 && me.X < Frame.Width && me.Y >= 0 && me.Y < Frame.Height)
  797. {
  798. return true;
  799. }
  800. return false;
  801. }
  802. private void SetInitialProperties (ComboBox container, bool hideDropdownListOnClick)
  803. {
  804. _container = container
  805. ?? throw new ArgumentNullException (
  806. nameof (container),
  807. "ComboBox container cannot be null."
  808. );
  809. HideDropdownListOnClick = hideDropdownListOnClick;
  810. AddCommand (Command.LineUp, () => _container.MoveUpList ());
  811. }
  812. }
  813. }