ComboBox.cs 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972
  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. 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{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. 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="ObservableCollection{T}"/>.</summary>
  311. /// <value>An object implementing the INotifyCollectionChanged and INotifyPropertyChanged interface.</value>
  312. /// <remarks>
  313. /// Use the <see cref="Source"/> property to set a new <see cref="IListDataSource"/> source and use custom
  314. /// rendering.
  315. /// </remarks>
  316. public void SetSource<T> (ObservableCollection<T> source)
  317. {
  318. if (source is null)
  319. {
  320. Source = null;
  321. }
  322. else
  323. {
  324. _listview.SetSource<T> (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. if (!string.IsNullOrEmpty (_search.Text))
  574. {
  575. foreach (object item in _source.ToList ())
  576. {
  577. // Iterate to preserver object type and force deep copy
  578. if (item.ToString ()
  579. .StartsWith (
  580. _search.Text,
  581. StringComparison.CurrentCultureIgnoreCase
  582. ))
  583. {
  584. _searchSet.Add (item);
  585. }
  586. }
  587. }
  588. }
  589. if (HasFocus)
  590. {
  591. ShowList ();
  592. }
  593. else if (_autoHide)
  594. {
  595. IsShow = false;
  596. HideList ();
  597. }
  598. }
  599. private void Selected ()
  600. {
  601. IsShow = false;
  602. _listview.TabStop = false;
  603. if (_listview.Source.Count == 0 || (_searchSet?.Count ?? 0) == 0)
  604. {
  605. _text = "";
  606. HideList ();
  607. IsShow = false;
  608. return;
  609. }
  610. SetValue (_listview.SelectedItem > -1 ? _searchSet [_listview.SelectedItem] : _text);
  611. _search.CursorPosition = _search.Text.GetColumns ();
  612. Search_Changed (this, new StateEventArgs<string> (_search.Text, _search.Text));
  613. OnOpenSelectedItem ();
  614. Reset (true);
  615. HideList ();
  616. IsShow = false;
  617. }
  618. private void SetSearchSet ()
  619. {
  620. if (Source is null)
  621. {
  622. return;
  623. }
  624. // force deep copy
  625. foreach (object item in Source.ToList ())
  626. {
  627. _searchSet.Add (item);
  628. }
  629. }
  630. private void SetSearchText (string value) { _search.Text = _text = value; }
  631. private void SetValue (object text, bool isFromSelectedItem = false)
  632. {
  633. _search.TextChanged -= Search_Changed;
  634. _text = _search.Text = text.ToString ();
  635. _search.CursorPosition = 0;
  636. _search.TextChanged += Search_Changed;
  637. if (!isFromSelectedItem)
  638. {
  639. _selectedItem = GetSelectedItemFromSource (_text);
  640. OnSelectedChanged ();
  641. }
  642. }
  643. /// <summary>Show the search list</summary>
  644. /// Consider making public
  645. private void ShowList ()
  646. {
  647. _listview.SetSource (_searchSet);
  648. _listview.Clear ();
  649. _listview.Height = CalculatetHeight ();
  650. SuperView?.BringSubviewToFront (this);
  651. }
  652. private bool UnixEmulation ()
  653. {
  654. // Unix emulation
  655. Reset ();
  656. return true;
  657. }
  658. private class ComboListView : ListView
  659. {
  660. private ComboBox _container;
  661. private bool _hideDropdownListOnClick;
  662. private int _highlighted = -1;
  663. private bool _isFocusing;
  664. public ComboListView (ComboBox container, bool hideDropdownListOnClick) { SetInitialProperties (container, hideDropdownListOnClick); }
  665. public ComboListView (ComboBox container, ObservableCollection<string> source, bool hideDropdownListOnClick)
  666. {
  667. Source = new ListWrapper<string> (source);
  668. SetInitialProperties (container, hideDropdownListOnClick);
  669. }
  670. public bool HideDropdownListOnClick
  671. {
  672. get => _hideDropdownListOnClick;
  673. set => _hideDropdownListOnClick = WantContinuousButtonPressed = value;
  674. }
  675. protected internal override bool OnMouseEvent (MouseEvent me)
  676. {
  677. var res = false;
  678. bool isMousePositionValid = IsMousePositionValid (me);
  679. if (isMousePositionValid)
  680. {
  681. res = base.OnMouseEvent (me);
  682. }
  683. if (HideDropdownListOnClick && me.Flags == MouseFlags.Button1Clicked)
  684. {
  685. if (!isMousePositionValid && !_isFocusing)
  686. {
  687. _container.IsShow = false;
  688. _container.HideList ();
  689. }
  690. else if (isMousePositionValid)
  691. {
  692. OnOpenSelectedItem ();
  693. }
  694. else
  695. {
  696. _isFocusing = false;
  697. }
  698. return true;
  699. }
  700. if (me.Flags == MouseFlags.ReportMousePosition && HideDropdownListOnClick)
  701. {
  702. if (isMousePositionValid)
  703. {
  704. _highlighted = Math.Min (TopItem + me.Position.Y, Source.Count);
  705. SetNeedsDisplay ();
  706. }
  707. _isFocusing = false;
  708. return true;
  709. }
  710. return res;
  711. }
  712. public override void OnDrawContent (Rectangle viewport)
  713. {
  714. Attribute current = ColorScheme.Focus;
  715. Driver.SetAttribute (current);
  716. Move (0, 0);
  717. Rectangle f = Frame;
  718. int item = TopItem;
  719. bool focused = HasFocus;
  720. int col = AllowsMarking ? 2 : 0;
  721. int start = LeftItem;
  722. for (var row = 0; row < f.Height; row++, item++)
  723. {
  724. bool isSelected = item == _container.SelectedItem;
  725. bool isHighlighted = _hideDropdownListOnClick && item == _highlighted;
  726. Attribute newcolor;
  727. if (isHighlighted || (isSelected && !_hideDropdownListOnClick))
  728. {
  729. newcolor = focused ? ColorScheme.Focus : ColorScheme.HotNormal;
  730. }
  731. else if (isSelected && _hideDropdownListOnClick)
  732. {
  733. newcolor = focused ? ColorScheme.HotFocus : ColorScheme.HotNormal;
  734. }
  735. else
  736. {
  737. newcolor = focused ? GetNormalColor () : GetNormalColor ();
  738. }
  739. if (newcolor != current)
  740. {
  741. Driver.SetAttribute (newcolor);
  742. current = newcolor;
  743. }
  744. Move (0, row);
  745. if (Source is null || item >= Source.Count)
  746. {
  747. for (var c = 0; c < f.Width; c++)
  748. {
  749. Driver.AddRune ((Rune)' ');
  750. }
  751. }
  752. else
  753. {
  754. var rowEventArgs = new ListViewRowEventArgs (item);
  755. OnRowRender (rowEventArgs);
  756. if (rowEventArgs.RowAttribute is { } && current != rowEventArgs.RowAttribute)
  757. {
  758. current = (Attribute)rowEventArgs.RowAttribute;
  759. Driver.SetAttribute (current);
  760. }
  761. if (AllowsMarking)
  762. {
  763. Driver.AddRune (
  764. Source.IsMarked (item) ? AllowsMultipleSelection ? Glyphs.Checked : Glyphs.Selected :
  765. AllowsMultipleSelection ? Glyphs.UnChecked : Glyphs.UnSelected
  766. );
  767. Driver.AddRune ((Rune)' ');
  768. }
  769. Source.Render (this, Driver, isSelected, item, col, row, f.Width - col, start);
  770. }
  771. }
  772. }
  773. public override bool OnEnter (View view)
  774. {
  775. if (_hideDropdownListOnClick)
  776. {
  777. _isFocusing = true;
  778. _highlighted = _container.SelectedItem;
  779. Application.GrabMouse (this);
  780. }
  781. return base.OnEnter (view);
  782. }
  783. public override bool OnLeave (View view)
  784. {
  785. if (_hideDropdownListOnClick)
  786. {
  787. _isFocusing = false;
  788. _highlighted = _container.SelectedItem;
  789. Application.UngrabMouse ();
  790. }
  791. return base.OnLeave (view);
  792. }
  793. public override bool OnSelectedChanged ()
  794. {
  795. bool res = base.OnSelectedChanged ();
  796. _highlighted = SelectedItem;
  797. return res;
  798. }
  799. private bool IsMousePositionValid (MouseEvent me)
  800. {
  801. if (me.Position.X >= 0 && me.Position.X < Frame.Width && me.Position.Y >= 0 && me.Position.Y < Frame.Height)
  802. {
  803. return true;
  804. }
  805. return false;
  806. }
  807. private void SetInitialProperties (ComboBox container, bool hideDropdownListOnClick)
  808. {
  809. _container = container
  810. ?? throw new ArgumentNullException (
  811. nameof (container),
  812. "ComboBox container cannot be null."
  813. );
  814. HideDropdownListOnClick = hideDropdownListOnClick;
  815. AddCommand (Command.LineUp, () => _container.MoveUpList ());
  816. }
  817. }
  818. }