123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008 |
- //
- // ComboBox.cs: ComboBox control
- //
- // Authors:
- // Ross Ferguson ([email protected])
- //
- using System.Collections.ObjectModel;
- using System.ComponentModel;
- using System.Diagnostics;
- namespace Terminal.Gui;
- /// <summary>Provides a drop-down list of items the user can select from.</summary>
- public class ComboBox : View, IDesignable
- {
- private readonly ComboListView _listview;
- private readonly int _minimumHeight = 2;
- private readonly TextField _search;
- private readonly ObservableCollection<object> _searchSet = [];
- private bool _autoHide = true;
- private bool _hideDropdownListOnClick;
- private int _lastSelectedItem = -1;
- private int _selectedItem = -1;
- private IListDataSource _source;
- private string _text = "";
- /// <summary>Public constructor</summary>
- public ComboBox ()
- {
- _search = new TextField () { CanFocus = true, TabStop = TabBehavior.NoStop };
- _listview = new ComboListView (this, HideDropdownListOnClick) { CanFocus = true, TabStop = TabBehavior.NoStop};
- _search.TextChanged += Search_Changed;
- _search.Accept += Search_Accept;
- _listview.Y = Pos.Bottom (_search);
- _listview.OpenSelectedItem += (sender, a) => Selected ();
- Add (_search, _listview);
- // BUGBUG: This should not be needed; LayoutComplete will handle
- Initialized += (s, e) => ProcessLayout ();
- // On resize
- LayoutComplete += (sender, a) => ProcessLayout ();
- ;
- _listview.SelectedItemChanged += (sender, e) =>
- {
- if (!HideDropdownListOnClick && _searchSet.Count > 0)
- {
- SetValue (_searchSet [_listview.SelectedItem]);
- }
- };
- Added += (s, e) =>
- {
- // Determine if this view is hosted inside a dialog and is the only control
- for (View view = SuperView; view != null; view = view.SuperView)
- {
- if (view is Dialog && SuperView is { } && SuperView.Subviews.Count == 1 && SuperView.Subviews [0] == this)
- {
- _autoHide = false;
- break;
- }
- }
- SetNeedsLayout ();
- SetNeedsDisplay ();
- ShowHideList (Text);
- };
- // Things this view knows how to do
- AddCommand (Command.Accept, () => ActivateSelected ());
- AddCommand (Command.ToggleExpandCollapse, () => ExpandCollapse ());
- AddCommand (Command.Expand, () => Expand ());
- AddCommand (Command.Collapse, () => Collapse ());
- AddCommand (Command.LineDown, () => MoveDown ());
- AddCommand (Command.LineUp, () => MoveUp ());
- AddCommand (Command.PageDown, () => PageDown ());
- AddCommand (Command.PageUp, () => PageUp ());
- AddCommand (Command.TopHome, () => MoveHome ());
- AddCommand (Command.BottomEnd, () => MoveEnd ());
- AddCommand (Command.Cancel, () => CancelSelected ());
- AddCommand (Command.UnixEmulation, () => UnixEmulation ());
- // Default keybindings for this view
- KeyBindings.Add (Key.Enter, Command.Accept);
- KeyBindings.Add (Key.F4, Command.ToggleExpandCollapse);
- KeyBindings.Add (Key.CursorDown, Command.LineDown);
- KeyBindings.Add (Key.CursorUp, Command.LineUp);
- KeyBindings.Add (Key.PageDown, Command.PageDown);
- KeyBindings.Add (Key.PageUp, Command.PageUp);
- KeyBindings.Add (Key.Home, Command.TopHome);
- KeyBindings.Add (Key.End, Command.BottomEnd);
- KeyBindings.Add (Key.Esc, Command.Cancel);
- KeyBindings.Add (Key.U.WithCtrl, Command.UnixEmulation);
- }
- /// <inheritdoc/>
- public new ColorScheme ColorScheme
- {
- get => base.ColorScheme;
- set
- {
- _listview.ColorScheme = value;
- base.ColorScheme = value;
- SetNeedsDisplay ();
- }
- }
- /// <summary>Gets or sets if the drop-down list can be hide with a button click event.</summary>
- public bool HideDropdownListOnClick
- {
- get => _hideDropdownListOnClick;
- set => _hideDropdownListOnClick = _listview.HideDropdownListOnClick = value;
- }
- /// <summary>Gets the drop-down list state, expanded or collapsed.</summary>
- public bool IsShow { get; private set; }
- /// <summary>If set to true, no changes to the text will be allowed.</summary>
- public bool ReadOnly
- {
- get => _search.ReadOnly;
- set
- {
- _search.ReadOnly = value;
- if (_search.ReadOnly)
- {
- if (_search.ColorScheme is { })
- {
- _search.ColorScheme = new ColorScheme (_search.ColorScheme) { Normal = _search.ColorScheme.Focus };
- }
- }
- }
- }
- /// <summary>Current search text</summary>
- public string SearchText
- {
- get => _search.Text;
- set => SetSearchText (value);
- }
- /// <summary>Gets the index of the currently selected item in the <see cref="Source"/></summary>
- /// <value>The selected item or -1 none selected.</value>
- public int SelectedItem
- {
- get => _selectedItem;
- set
- {
- if (_selectedItem != value
- && (value == -1
- || (_source is { } && value > -1 && value < _source.Count)))
- {
- _selectedItem = _lastSelectedItem = value;
- if (_selectedItem != -1)
- {
- SetValue (_source.ToList () [_selectedItem].ToString (), true);
- }
- else
- {
- SetValue ("", true);
- }
- OnSelectedChanged ();
- }
- }
- }
- /// <summary>Gets or sets the <see cref="IListDataSource"/> backing this <see cref="ComboBox"/>, enabling custom rendering.</summary>
- /// <value>The source.</value>
- /// <remarks>Use <see cref="SetSource{T}"/> to set a new <see cref="ObservableCollection{T}"/> source.</remarks>
- public IListDataSource Source
- {
- get => _source;
- set
- {
- _source = value;
- // Only need to refresh list if its been added to a container view
- if (SuperView is { } && SuperView.Subviews.Contains (this))
- {
- Text = string.Empty;
- SetNeedsDisplay ();
- }
- }
- }
- /// <summary>The text of the currently selected list item</summary>
- public new string Text
- {
- get => _text;
- set => SetSearchText (value);
- }
- /// <summary>
- /// Collapses the drop-down list. Returns true if the state changed or false if it was already collapsed and no
- /// action was taken
- /// </summary>
- public virtual bool Collapse ()
- {
- if (!IsShow)
- {
- return false;
- }
- IsShow = false;
- HideList ();
- return true;
- }
- /// <summary>This event is raised when the drop-down list is collapsed.</summary>
- public event EventHandler Collapsed;
- /// <summary>
- /// Expands the drop-down list. Returns true if the state changed or false if it was already expanded and no
- /// action was taken
- /// </summary>
- public virtual bool Expand ()
- {
- if (IsShow)
- {
- return false;
- }
- SetSearchSet ();
- IsShow = true;
- ShowList ();
- FocusSelectedItem ();
- return true;
- }
- /// <summary>This event is raised when the drop-down list is expanded.</summary>
- public event EventHandler Expanded;
- /// <inheritdoc/>
- protected internal override bool OnMouseEvent (MouseEvent me)
- {
- if (me.Position.X == Viewport.Right - 1
- && me.Position.Y == Viewport.Top
- && me.Flags == MouseFlags.Button1Pressed
- && _autoHide)
- {
- if (IsShow)
- {
- IsShow = false;
- HideList ();
- }
- else
- {
- SetSearchSet ();
- IsShow = true;
- ShowList ();
- FocusSelectedItem ();
- }
- return me.Handled = true;
- }
- if (me.Flags == MouseFlags.Button1Pressed)
- {
- if (!_search.HasFocus)
- {
- _search.SetFocus ();
- }
- return me.Handled = true;
- }
- return false;
- }
- /// <summary>Virtual method which invokes the <see cref="Collapsed"/> event.</summary>
- public virtual void OnCollapsed () { Collapsed?.Invoke (this, EventArgs.Empty); }
- /// <inheritdoc/>
- public override void OnDrawContent (Rectangle viewport)
- {
- base.OnDrawContent (viewport);
- if (!_autoHide)
- {
- return;
- }
- Driver.SetAttribute (ColorScheme.Focus);
- Move (Viewport.Right - 1, 0);
- Driver.AddRune (Glyphs.DownArrow);
- }
- /// <inheritdoc/>
- public override bool OnEnter (View view)
- {
- if (!_search.HasFocus && !_listview.HasFocus)
- {
- _search.SetFocus ();
- }
- _search.CursorPosition = _search.Text.GetRuneCount ();
- return base.OnEnter (view);
- }
- /// <summary>Virtual method which invokes the <see cref="Expanded"/> event.</summary>
- public virtual void OnExpanded () { Expanded?.Invoke (this, EventArgs.Empty); }
- /// <inheritdoc/>
- public override bool OnLeave (View view)
- {
- if (_source?.Count > 0
- && _selectedItem > -1
- && _selectedItem < _source.Count - 1
- && _text != _source.ToList () [_selectedItem].ToString ())
- {
- SetValue (_source.ToList () [_selectedItem].ToString ());
- }
- if (_autoHide && IsShow && view != this && view != _search && view != _listview)
- {
- IsShow = false;
- HideList ();
- }
- else if (_listview.TabStop?.HasFlag (TabBehavior.TabStop) ?? false)
- {
- _listview.TabStop = TabBehavior.NoStop;
- }
- return base.OnLeave (view);
- }
- /// <summary>Invokes the OnOpenSelectedItem event if it is defined.</summary>
- /// <returns></returns>
- public virtual bool OnOpenSelectedItem ()
- {
- string value = _search.Text;
- _lastSelectedItem = SelectedItem;
- OpenSelectedItem?.Invoke (this, new ListViewItemEventArgs (SelectedItem, value));
- return true;
- }
- /// <summary>Invokes the SelectedChanged event if it is defined.</summary>
- /// <returns></returns>
- public virtual bool OnSelectedChanged ()
- {
- // Note: Cannot rely on "listview.SelectedItem != lastSelectedItem" because the list is dynamic.
- // So we cannot optimize. Ie: Don't call if not changed
- SelectedItemChanged?.Invoke (this, new ListViewItemEventArgs (SelectedItem, _search.Text));
- return true;
- }
- /// <summary>This event is raised when the user Double Clicks on an item or presses ENTER to open the selected item.</summary>
- public event EventHandler<ListViewItemEventArgs> OpenSelectedItem;
- /// <summary>This event is raised when the selected item in the <see cref="ComboBox"/> has changed.</summary>
- public event EventHandler<ListViewItemEventArgs> SelectedItemChanged;
- /// <summary>Sets the source of the <see cref="ComboBox"/> to an <see cref="ObservableCollection{T}"/>.</summary>
- /// <value>An object implementing the INotifyCollectionChanged and INotifyPropertyChanged interface.</value>
- /// <remarks>
- /// Use the <see cref="Source"/> property to set a new <see cref="IListDataSource"/> source and use custom
- /// rendering.
- /// </remarks>
- public void SetSource<T> (ObservableCollection<T> source)
- {
- if (source is null)
- {
- Source = null;
- }
- else
- {
- _listview.SetSource<T> (source);
- Source = _listview.Source;
- }
- }
- private bool ActivateSelected ()
- {
- if (HasItems ())
- {
- Selected ();
- return true;
- }
- return false;
- }
- /// <summary>Internal height of dynamic search list</summary>
- /// <returns></returns>
- private int CalculateHeight ()
- {
- if (!IsInitialized || Viewport.Height == 0)
- {
- return 0;
- }
- return Math.Min (
- Math.Max (Viewport.Height - 1, _minimumHeight - 1),
- _searchSet?.Count > 0 ? _searchSet.Count :
- IsShow ? Math.Max (Viewport.Height - 1, _minimumHeight - 1) : 0
- );
- }
- private bool CancelSelected ()
- {
- _search.SetFocus ();
- if (ReadOnly || HideDropdownListOnClick)
- {
- SelectedItem = _lastSelectedItem;
- if (SelectedItem > -1 && _listview.Source?.Count > 0)
- {
- Text = _listview.Source.ToList () [SelectedItem]?.ToString ();
- }
- }
- else if (!ReadOnly)
- {
- Text = string.Empty;
- _selectedItem = _lastSelectedItem;
- OnSelectedChanged ();
- }
- return Collapse ();
- }
- /// <summary>Toggles the expand/collapse state of the sublist in the combo box</summary>
- /// <returns></returns>
- private bool ExpandCollapse ()
- {
- if (_search.HasFocus || _listview.HasFocus)
- {
- if (!IsShow)
- {
- return Expand ();
- }
- return Collapse ();
- }
- return false;
- }
- private void FocusSelectedItem ()
- {
- _listview.SelectedItem = SelectedItem > -1 ? SelectedItem : 0;
- _listview.TabStop = TabBehavior.TabStop;
- _listview.SetFocus ();
- OnExpanded ();
- }
- private int GetSelectedItemFromSource (string searchText)
- {
- if (_source is null)
- {
- return -1;
- }
- for (var i = 0; i < _searchSet.Count; i++)
- {
- if (_searchSet [i].ToString () == searchText)
- {
- return i;
- }
- }
- return -1;
- }
- private bool HasItems () { return Source?.Count > 0; }
- /// <summary>Hide the search list</summary>
- /// Consider making public
- private void HideList ()
- {
- if (_lastSelectedItem != _selectedItem)
- {
- OnOpenSelectedItem ();
- }
- Reset (true);
- _listview.Clear ();
- _listview.TabStop = TabBehavior.NoStop;
- SuperView?.SendSubviewToBack (this);
- Rectangle rect = _listview.ViewportToScreen (_listview.IsInitialized ? _listview.Viewport : Rectangle.Empty);
- SuperView?.SetNeedsDisplay (rect);
- OnCollapsed ();
- }
- private bool? MoveDown ()
- {
- if (_search.HasFocus)
- {
- // jump to list
- if (_searchSet?.Count > 0)
- {
- _listview.TabStop = TabBehavior.TabStop;
- _listview.SetFocus ();
- if (_listview.SelectedItem > -1)
- {
- SetValue (_searchSet [_listview.SelectedItem]);
- }
- else
- {
- _listview.SelectedItem = 0;
- }
- }
- else
- {
- _listview.TabStop = TabBehavior.NoStop;
- SuperView?.AdvanceFocus (NavigationDirection.Forward);
- }
- return true;
- }
- return null;
- }
- private bool? MoveEnd ()
- {
- if (!IsShow && _search.HasFocus)
- {
- return null;
- }
- if (HasItems ())
- {
- _listview.MoveEnd ();
- }
- return true;
- }
- private bool? MoveHome ()
- {
- if (!IsShow && _search.HasFocus)
- {
- return null;
- }
- if (HasItems ())
- {
- _listview.MoveHome ();
- }
- return true;
- }
- private bool? MoveUp ()
- {
- if (HasItems ())
- {
- _listview.MoveUp ();
- }
- return true;
- }
- private bool? MoveUpList ()
- {
- if (_listview.HasFocus && _listview.SelectedItem == 0 && _searchSet?.Count > 0) // jump back to search
- {
- _search.CursorPosition = _search.Text.GetRuneCount ();
- _search.SetFocus ();
- }
- else
- {
- MoveUp ();
- }
- return true;
- }
- private bool PageDown ()
- {
- if (HasItems ())
- {
- _listview.MovePageDown ();
- }
- return true;
- }
- private bool PageUp ()
- {
- if (HasItems ())
- {
- _listview.MovePageUp ();
- }
- return true;
- }
- // TODO: Upgrade Combobox to use Dim.Auto instead of all this stuff.
- private void ProcessLayout ()
- {
- if (Viewport.Height < _minimumHeight && (Height is null || Height is DimAbsolute))
- {
- Height = _minimumHeight;
- }
- // BUGBUG: This uses Viewport. Should use ContentSize
- if ((!_autoHide && Viewport.Width > 0 && _search.Frame.Width != Viewport.Width)
- || (_autoHide && Viewport.Width > 0 && _search.Frame.Width != Viewport.Width - 1))
- {
- _search.Width = _listview.Width = _autoHide ? Viewport.Width - 1 : Viewport.Width;
- _listview.Height = CalculateHeight ();
- _search.SetRelativeLayout (GetContentSize ());
- _listview.SetRelativeLayout (GetContentSize ());
- }
- }
- /// <summary>Reset to full original list</summary>
- private void Reset (bool keepSearchText = false)
- {
- if (!keepSearchText)
- {
- SetSearchText (string.Empty);
- }
- ResetSearchSet ();
- _listview.SetSource (_searchSet);
- _listview.Height = CalculateHeight ();
- if (Subviews.Count > 0 && HasFocus)
- {
- _search.SetFocus ();
- }
- }
- private void ResetSearchSet (bool noCopy = false)
- {
- _listview.SuspendCollectionChangedEvent ();
- _searchSet.Clear ();
- _listview.ResumeSuspendCollectionChangedEvent ();
- if (_autoHide || noCopy)
- {
- return;
- }
- SetSearchSet ();
- }
- // Tell TextField to handle Accept Command (Enter)
- void Search_Accept (object sender, HandledEventArgs e) { e.Handled = true; }
- private void Search_Changed (object sender, EventArgs e)
- {
- if (_source is null)
- {
- // Object initialization
- return;
- }
- ShowHideList (Text);
- }
- private void ShowHideList (string oldText)
- {
- if (string.IsNullOrEmpty (_search.Text) && string.IsNullOrEmpty (oldText))
- {
- ResetSearchSet ();
- }
- else if (_search.Text != oldText)
- {
- if (_search.Text.Length < oldText.Length)
- {
- _selectedItem = -1;
- }
- IsShow = true;
- ResetSearchSet (true);
- if (!string.IsNullOrEmpty (_search.Text))
- {
- _listview.SuspendCollectionChangedEvent ();
- foreach (object item in _source.ToList ())
- {
- // Iterate to preserver object type and force deep copy
- if (item.ToString ()
- .StartsWith (
- _search.Text,
- StringComparison.CurrentCultureIgnoreCase
- ))
- {
- _searchSet.Add (item);
- }
- }
- _listview.ResumeSuspendCollectionChangedEvent ();
- }
- }
- if (HasFocus)
- {
- ShowList ();
- }
- else if (_autoHide)
- {
- IsShow = false;
- HideList ();
- }
- }
- private void Selected ()
- {
- IsShow = false;
- _listview.TabStop = TabBehavior.NoStop;
- if (_listview.Source.Count == 0 || (_searchSet?.Count ?? 0) == 0)
- {
- _text = "";
- HideList ();
- IsShow = false;
- return;
- }
- SetValue (_listview.SelectedItem > -1 ? _searchSet [_listview.SelectedItem] : _text);
- _search.CursorPosition = _search.Text.GetColumns ();
- ShowHideList (Text);
- OnOpenSelectedItem ();
- Reset (true);
- HideList ();
- IsShow = false;
- }
- private void SetSearchSet ()
- {
- if (Source is null)
- {
- return;
- }
- // PERF: At the request of @dodexahedron in the comment https://github.com/gui-cs/Terminal.Gui/pull/3552#discussion_r1648112410.
- _listview.SuspendCollectionChangedEvent ();
- // force deep copy
- foreach (object item in Source.ToList ())
- {
- _searchSet.Add (item);
- }
- _listview.ResumeSuspendCollectionChangedEvent ();
- }
- // Sets the search text field Text as well as our own Text property
- private void SetSearchText (string value)
- {
- _search.Text = value;
- _text = value;
- }
- private void SetValue (object text, bool isFromSelectedItem = false)
- {
- // TOOD: The fact we have to suspend events to change the text makes this feel very hacky.
- _search.TextChanged -= Search_Changed;
- // Note we set _text, to avoid set_Text from setting _search.Text again
- _text = _search.Text = text.ToString ();
- _search.CursorPosition = 0;
- _search.TextChanged += Search_Changed;
- if (!isFromSelectedItem)
- {
- _selectedItem = GetSelectedItemFromSource (_text);
- OnSelectedChanged ();
- }
- }
- /// <summary>Show the search list</summary>
- /// Consider making public
- private void ShowList ()
- {
- _listview.SuspendCollectionChangedEvent ();
- _listview.SetSource (_searchSet);
- _listview.ResumeSuspendCollectionChangedEvent ();
- _listview.Clear ();
- _listview.Height = CalculateHeight ();
- SuperView?.BringSubviewToFront (this);
- }
- private bool UnixEmulation ()
- {
- // Unix emulation
- Reset ();
- return true;
- }
- private class ComboListView : ListView
- {
- private ComboBox _container;
- private bool _hideDropdownListOnClick;
- private int _highlighted = -1;
- private bool _isFocusing;
- public ComboListView (ComboBox container, bool hideDropdownListOnClick) { SetInitialProperties (container, hideDropdownListOnClick); }
- public ComboListView (ComboBox container, ObservableCollection<string> source, bool hideDropdownListOnClick)
- {
- Source = new ListWrapper<string> (source);
- SetInitialProperties (container, hideDropdownListOnClick);
- }
- public bool HideDropdownListOnClick
- {
- get => _hideDropdownListOnClick;
- set => _hideDropdownListOnClick = WantContinuousButtonPressed = value;
- }
- protected internal override bool OnMouseEvent (MouseEvent me)
- {
- var res = false;
- bool isMousePositionValid = IsMousePositionValid (me);
- if (isMousePositionValid)
- {
- res = base.OnMouseEvent (me);
- }
- if (HideDropdownListOnClick && me.Flags == MouseFlags.Button1Clicked)
- {
- if (!isMousePositionValid && !_isFocusing)
- {
- _container.IsShow = false;
- _container.HideList ();
- }
- else if (isMousePositionValid)
- {
- OnOpenSelectedItem ();
- }
- else
- {
- _isFocusing = false;
- }
- return true;
- }
- if (me.Flags == MouseFlags.ReportMousePosition && HideDropdownListOnClick)
- {
- if (isMousePositionValid)
- {
- _highlighted = Math.Min (TopItem + me.Position.Y, Source.Count);
- SetNeedsDisplay ();
- }
- _isFocusing = false;
- return true;
- }
- return res;
- }
- public override void OnDrawContent (Rectangle viewport)
- {
- Attribute current = ColorScheme.Focus;
- Driver.SetAttribute (current);
- Move (0, 0);
- Rectangle f = Frame;
- int item = TopItem;
- bool focused = HasFocus;
- int col = AllowsMarking ? 2 : 0;
- int start = LeftItem;
- for (var row = 0; row < f.Height; row++, item++)
- {
- bool isSelected = item == _container.SelectedItem;
- bool isHighlighted = _hideDropdownListOnClick && item == _highlighted;
- Attribute newcolor;
- if (isHighlighted || (isSelected && !_hideDropdownListOnClick))
- {
- newcolor = focused ? ColorScheme.Focus : ColorScheme.HotNormal;
- }
- else if (isSelected && _hideDropdownListOnClick)
- {
- newcolor = focused ? ColorScheme.HotFocus : ColorScheme.HotNormal;
- }
- else
- {
- newcolor = focused ? GetNormalColor () : GetNormalColor ();
- }
- if (newcolor != current)
- {
- Driver.SetAttribute (newcolor);
- current = newcolor;
- }
- Move (0, row);
- if (Source is null || item >= Source.Count)
- {
- for (var c = 0; c < f.Width; c++)
- {
- Driver.AddRune ((Rune)' ');
- }
- }
- else
- {
- var rowEventArgs = new ListViewRowEventArgs (item);
- OnRowRender (rowEventArgs);
- if (rowEventArgs.RowAttribute is { } && current != rowEventArgs.RowAttribute)
- {
- current = (Attribute)rowEventArgs.RowAttribute;
- Driver.SetAttribute (current);
- }
- if (AllowsMarking)
- {
- Driver.AddRune (
- Source.IsMarked (item) ? AllowsMultipleSelection ? Glyphs.CheckStateChecked : Glyphs.Selected :
- AllowsMultipleSelection ? Glyphs.CheckStateUnChecked : Glyphs.UnSelected
- );
- Driver.AddRune ((Rune)' ');
- }
- Source.Render (this, Driver, isSelected, item, col, row, f.Width - col, start);
- }
- }
- }
- public override bool OnEnter (View view)
- {
- if (_hideDropdownListOnClick)
- {
- _isFocusing = true;
- _highlighted = _container.SelectedItem;
- Application.GrabMouse (this);
- }
- return base.OnEnter (view);
- }
- public override bool OnLeave (View view)
- {
- if (_hideDropdownListOnClick)
- {
- _isFocusing = false;
- _highlighted = _container.SelectedItem;
- Application.UngrabMouse ();
- }
- return base.OnLeave (view);
- }
- public override bool OnSelectedChanged ()
- {
- bool res = base.OnSelectedChanged ();
- _highlighted = SelectedItem;
- return res;
- }
- private bool IsMousePositionValid (MouseEvent me)
- {
- if (me.Position.X >= 0 && me.Position.X < Frame.Width && me.Position.Y >= 0 && me.Position.Y < Frame.Height)
- {
- return true;
- }
- return false;
- }
- private void SetInitialProperties (ComboBox container, bool hideDropdownListOnClick)
- {
- _container = container
- ?? throw new ArgumentNullException (
- nameof (container),
- "ComboBox container cannot be null."
- );
- HideDropdownListOnClick = hideDropdownListOnClick;
- AddCommand (Command.LineUp, () => _container.MoveUpList ());
- }
- }
- /// <inheritdoc />
- public bool EnableForDesign ()
- {
- var source = new ObservableCollection<string> (["Combo Item 1", "Combo Item two", "Combo Item Quattro", "Last Combo Item"]);
- SetSource (source);
- Height = Dim.Auto (DimAutoStyle.Content, minimumContentDim: source.Count + 1);
- return true;
- }
- }
|