mirror of
https://github.com/gui-cs/Terminal.Gui.git
synced 2025-12-28 08:47:59 +01:00
413 lines
9.1 KiB
C#
413 lines
9.1 KiB
C#
//
|
|
// ComboBox.cs: ComboBox control
|
|
//
|
|
// Authors:
|
|
// Ross Ferguson (ross.c.ferguson@btinternet.com)
|
|
//
|
|
|
|
using System;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using NStack;
|
|
|
|
namespace Terminal.Gui {
|
|
/// <summary>
|
|
/// ComboBox control
|
|
/// </summary>
|
|
public class ComboBox : View {
|
|
|
|
IListDataSource source;
|
|
/// <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"/> to set a new <see cref="IList"/> source.
|
|
/// </remarks>
|
|
public IListDataSource Source {
|
|
get => source;
|
|
set {
|
|
source = value;
|
|
|
|
if(isAdded) {
|
|
Search_Changed ("");
|
|
SetNeedsDisplay ();
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Sets the source of the <see cref="ComboBox"/> to an <see cref="IList"/>.
|
|
/// </summary>
|
|
/// <value>An object implementing the IList interface.</value>
|
|
/// <remarks>
|
|
/// Use the <see cref="Source"/> property to set a new <see cref="IListDataSource"/> source and use custome rendering.
|
|
/// </remarks>
|
|
public void SetSource (IList source)
|
|
{
|
|
if (source == null) {
|
|
Source = null;
|
|
} else {
|
|
Source = MakeWrapper (source);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Changed event, raised when the selection has been confirmed.
|
|
/// </summary>
|
|
/// <remarks>
|
|
/// Client code can hook up to this event, it is
|
|
/// raised when the selection has been confirmed.
|
|
/// </remarks>
|
|
public event EventHandler<ustring> SelectedItemChanged;
|
|
|
|
IList searchset;
|
|
ustring text = "";
|
|
readonly TextField search;
|
|
readonly ListView listview;
|
|
bool autoHide = true;
|
|
bool isAdded;
|
|
|
|
/// <summary>
|
|
/// Public constructor
|
|
/// </summary>
|
|
public ComboBox () : base ()
|
|
{
|
|
search = new TextField ("");
|
|
listview = new ListView () { LayoutStyle = LayoutStyle.Computed, CanFocus = true };
|
|
|
|
Initialize ();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Public constructor
|
|
/// </summary>
|
|
/// <param name="text"></param>
|
|
public ComboBox (ustring text) : base ()
|
|
{
|
|
search = new TextField ("");
|
|
listview = new ListView () { LayoutStyle = LayoutStyle.Computed, CanFocus = true };
|
|
|
|
Initialize ();
|
|
Text = text;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Public constructor
|
|
/// </summary>
|
|
/// <param name="rect"></param>
|
|
/// <param name="source"></param>
|
|
public ComboBox (Rect rect, IList source) : base (rect)
|
|
{
|
|
search = new TextField ("") { Width = rect.Width };
|
|
listview = new ListView (rect, source) { LayoutStyle = LayoutStyle.Computed };
|
|
|
|
Initialize ();
|
|
SetSource (source);
|
|
}
|
|
|
|
static IListDataSource MakeWrapper (IList source)
|
|
{
|
|
return new ListWrapper (source);
|
|
}
|
|
|
|
private void Initialize ()
|
|
{
|
|
ColorScheme = Colors.Base;
|
|
|
|
search.TextChanged += Search_Changed;
|
|
search.MouseClick += Search_MouseClick;
|
|
|
|
listview.Y = Pos.Bottom (search);
|
|
listview.OpenSelectedItem += (ListViewItemEventArgs a) => Selected ();
|
|
|
|
this.Add (listview, search);
|
|
this.SetFocus (search);
|
|
|
|
// On resize
|
|
LayoutComplete += (LayoutEventArgs a) => {
|
|
if (!autoHide && search.Frame.Width != Bounds.Width ||
|
|
autoHide && search.Frame.Width != Bounds.Width - 1) {
|
|
search.Width = listview.Width = autoHide ? Bounds.Width - 1 : Bounds.Width;
|
|
listview.Height = CalculatetHeight ();
|
|
search.SetRelativeLayout (Bounds);
|
|
listview.SetRelativeLayout (Bounds);
|
|
}
|
|
};
|
|
|
|
listview.SelectedItemChanged += (ListViewItemEventArgs e) => {
|
|
|
|
if (searchset.Count > 0) {
|
|
SetValue (searchset [listview.SelectedItem]);
|
|
}
|
|
};
|
|
|
|
Adding += (View v) => {
|
|
|
|
// Determine if this view is hosted inside a dialog
|
|
for (View view = this.SuperView; view != null; view = view.SuperView) {
|
|
if (view is Dialog) {
|
|
autoHide = false;
|
|
break;
|
|
}
|
|
}
|
|
|
|
ColorScheme = autoHide ? Colors.Base : ColorScheme = null;
|
|
|
|
SetNeedsLayout ();
|
|
Search_Changed (Text);
|
|
|
|
if (autoHide) {
|
|
listview.ColorScheme = Colors.Menu;
|
|
} else {
|
|
search.ColorScheme = Colors.Menu;
|
|
}
|
|
|
|
isAdded = true;
|
|
};
|
|
}
|
|
|
|
bool isShow = false;
|
|
|
|
private void Search_MouseClick (MouseEventArgs me)
|
|
{
|
|
if (me.MouseEvent.X == Bounds.Right - 1 && me.MouseEvent.Y == Bounds.Top && me.MouseEvent.Flags == MouseFlags.Button1Pressed
|
|
&& search.Text == "" && autoHide) {
|
|
|
|
if (isShow) {
|
|
HideList ();
|
|
isShow = false;
|
|
} else {
|
|
// force deep copy
|
|
foreach (var item in Source.ToList()) {
|
|
searchset.Add (item);
|
|
}
|
|
|
|
ShowList ();
|
|
isShow = true;
|
|
}
|
|
} else {
|
|
SuperView.SetFocus (search);
|
|
}
|
|
}
|
|
|
|
///<inheritdoc/>
|
|
public override bool OnEnter ()
|
|
{
|
|
if (!search.HasFocus) {
|
|
this.SetFocus (search);
|
|
}
|
|
|
|
search.CursorPosition = search.Text.RuneCount;
|
|
|
|
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, search.Text);
|
|
|
|
return true;
|
|
}
|
|
|
|
///<inheritdoc/>
|
|
public override void Redraw (Rect bounds)
|
|
{
|
|
base.Redraw (bounds);
|
|
|
|
if (!autoHide) {
|
|
return;
|
|
}
|
|
|
|
Move (Bounds.Right - 1, 0);
|
|
Driver.AddRune (Driver.DownArrow);
|
|
}
|
|
|
|
///<inheritdoc/>
|
|
public override bool ProcessKey (KeyEvent e)
|
|
{
|
|
if (e.Key == Key.Tab) {
|
|
base.ProcessKey (e);
|
|
return false; // allow tab-out to next control
|
|
}
|
|
|
|
if (e.Key == Key.Enter && listview.HasFocus) {
|
|
Selected ();
|
|
return true;
|
|
}
|
|
|
|
if (e.Key == Key.CursorDown && search.HasFocus && searchset.Count > 0) { // jump to list
|
|
this.SetFocus (listview);
|
|
SetValue (searchset [listview.SelectedItem]);
|
|
return true;
|
|
}
|
|
|
|
if (e.Key == Key.CursorUp && search.HasFocus) { // stop odd behavior on KeyUp when search has focus
|
|
return true;
|
|
}
|
|
|
|
if (e.Key == Key.CursorUp && listview.HasFocus && listview.SelectedItem == 0 && searchset.Count > 0) // jump back to search
|
|
{
|
|
search.CursorPosition = search.Text.RuneCount;
|
|
this.SetFocus (search);
|
|
return true;
|
|
}
|
|
|
|
if(e.Key == Key.PageDown) {
|
|
listview.MovePageDown ();
|
|
return true;
|
|
}
|
|
|
|
if (e.Key == Key.PageUp) {
|
|
listview.MovePageUp ();
|
|
return true;
|
|
}
|
|
|
|
if (e.Key == Key.Esc) {
|
|
this.SetFocus (search);
|
|
search.Text = text = "";
|
|
OnSelectedChanged ();
|
|
return true;
|
|
}
|
|
|
|
// Unix emulation
|
|
if (e.Key == Key.ControlU) {
|
|
Reset ();
|
|
return true;
|
|
}
|
|
|
|
return base.ProcessKey (e);
|
|
}
|
|
|
|
/// <summary>
|
|
/// The currently selected list item
|
|
/// </summary>
|
|
public new ustring Text {
|
|
get {
|
|
return text;
|
|
}
|
|
set {
|
|
search.Text = text = value;
|
|
}
|
|
}
|
|
|
|
private void SetValue (object text)
|
|
{
|
|
search.TextChanged -= Search_Changed;
|
|
this.text = search.Text = text.ToString();
|
|
search.CursorPosition = 0;
|
|
search.TextChanged += Search_Changed;
|
|
}
|
|
|
|
private void Selected ()
|
|
{
|
|
if (listview.Source.Count == 0 || searchset.Count == 0) {
|
|
text = "";
|
|
return;
|
|
}
|
|
|
|
SetValue (searchset [listview.SelectedItem]);
|
|
search.CursorPosition = search.Text.RuneCount;
|
|
Search_Changed (search.Text);
|
|
OnSelectedChanged ();
|
|
Reset (keepSearchText: true);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Reset to full original list
|
|
/// </summary>
|
|
private void Reset (bool keepSearchText = false)
|
|
{
|
|
if (!keepSearchText) {
|
|
search.Text = text = "";
|
|
}
|
|
|
|
ResetSearchSet ();
|
|
|
|
listview.SetSource (searchset);
|
|
listview.Height = CalculatetHeight ();
|
|
|
|
this.SetFocus (search);
|
|
}
|
|
|
|
private void ResetSearchSet (bool noCopy = false)
|
|
{
|
|
if (searchset == null) {
|
|
searchset = new List<object> ();
|
|
} else {
|
|
searchset.Clear ();
|
|
}
|
|
|
|
if (autoHide || noCopy)
|
|
return;
|
|
|
|
// force deep copy
|
|
foreach (var item in Source.ToList ()) {
|
|
searchset.Add (item);
|
|
}
|
|
}
|
|
|
|
private void Search_Changed (ustring text)
|
|
{
|
|
if (source == null) { // Object initialization
|
|
return;
|
|
}
|
|
|
|
if (ustring.IsNullOrEmpty (search.Text)) {
|
|
ResetSearchSet ();
|
|
} else {
|
|
ResetSearchSet (noCopy: true);
|
|
|
|
foreach (var item in source.ToList ()) { // Iterate to preserver object type and force deep copy
|
|
if (item.ToString().StartsWith (search.Text.ToString(), StringComparison.CurrentCultureIgnoreCase)) {
|
|
searchset.Add (item);
|
|
}
|
|
}
|
|
}
|
|
|
|
ShowList ();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Show the search list
|
|
/// </summary>
|
|
///
|
|
/// Consider making public
|
|
private void ShowList ()
|
|
{
|
|
listview.SetSource (searchset);
|
|
listview.Height = CalculatetHeight ();
|
|
listview.Redraw (new Rect (0, 0, Bounds.Width, Bounds.Height)); // Ensure list shrinks in Dialog
|
|
this.SuperView?.BringSubviewToFront (this);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Hide the search list
|
|
/// </summary>
|
|
///
|
|
/// Consider making public
|
|
private void HideList ()
|
|
{
|
|
Reset ();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Internal height of dynamic search list
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
private int CalculatetHeight ()
|
|
{
|
|
if (Bounds.Height == 0)
|
|
return 0;
|
|
|
|
return Math.Min (Bounds.Height - 1, searchset?.Count ?? 0);
|
|
}
|
|
}
|
|
}
|