From cfcf8efb59d75f7c506b6fa9499351af894b1530 Mon Sep 17 00:00:00 2001 From: Ross Ferguson Date: Sat, 20 Jun 2020 09:34:55 +0100 Subject: [PATCH] ComboBox multiple fixes. Dim.Fill() incorrectly calculated. List height not resizing. SetSource() not immeadiately updating list. Double click not selecting item. Example now demo resizes with view --- Example/demo.cs | 5 +- Terminal.Gui/Views/ComboBox.cs | 104 ++++++++++---------------- UICatalog/Scenarios/ListsAndCombos.cs | 4 +- 3 files changed, 43 insertions(+), 70 deletions(-) diff --git a/Example/demo.cs b/Example/demo.cs index 1195db8ab..dda4a3d7a 100644 --- a/Example/demo.cs +++ b/Example/demo.cs @@ -459,11 +459,12 @@ static class Demo { .OrderBy (x => x).ToList (); } } - var list = new ComboBox () { X = 0, Y = 0, Width = Dim.Fill(), Height = Dim.Fill() }; + var list = new ComboBox () { Width = Dim.Fill(), Height = Dim.Fill() }; list.SetSource(items.ToList()); list.SelectedItemChanged += (object sender, ustring text) => { Application.RequestStop (); }; - var d = new Dialog ("Select source file", 40, 12) { list }; + var d = new Dialog () { Title = "Select source file", Width = Dim.Percent (50), Height = Dim.Percent (50) }; + d.Add (list); Application.Run (d); MessageBox.Query (60, 10, "Selected file", list.Text.ToString() == "" ? "Nothing selected" : list.Text.ToString(), "Ok"); diff --git a/Terminal.Gui/Views/ComboBox.cs b/Terminal.Gui/Views/ComboBox.cs index 99e306412..73b2b21a7 100644 --- a/Terminal.Gui/Views/ComboBox.cs +++ b/Terminal.Gui/Views/ComboBox.cs @@ -4,10 +4,6 @@ // Authors: // Ross Ferguson (ross.c.ferguson@btinternet.com) // -// TODO: -// LayoutComplete() resize Height implement -// Cursor rolls of end of list when Height = Dim.Fill() and list fills frame -// using System; using System.Collections; @@ -33,6 +29,7 @@ namespace Terminal.Gui { get => source; set { source = value; + Search_Changed (""); SetNeedsDisplay (); } } @@ -88,7 +85,6 @@ namespace Terminal.Gui { /// public ComboBox (Rect rect, IList source) : base (rect) { - SetSource (source); this.height = rect.Height; this.width = rect.Width; @@ -96,6 +92,7 @@ namespace Terminal.Gui { listview = new ListView (rect, source) { LayoutStyle = LayoutStyle.Computed }; Initialize (); + SetSource (source); } static IListDataSource MakeWrapper (IList source) @@ -108,12 +105,14 @@ namespace Terminal.Gui { ColorScheme = Colors.Base; search.TextChanged += Search_Changed; + listview.OpenSelectedItem += (ListViewItemEventArgs a) => Selected(); // On resize LayoutComplete += (LayoutEventArgs a) => { search.Width = Bounds.Width; listview.Width = autoHide ? Bounds.Width - 1 : Bounds.Width; + listview.Height = CalculatetHeight (); }; listview.SelectedItemChanged += (ListViewItemEventArgs e) => { @@ -135,30 +134,20 @@ namespace Terminal.Gui { ColorScheme = autoHide ? Colors.Base : ColorScheme = null; - // Needs to be re-applied for LayoutStyle.Computed - // If Dim or Pos are null, these are the from the parametrized constructor - listview.Y = 1; + listview.Y = Pos.Bottom (search); - if (Width == null) { - listview.Width = CalculateWidth (); - search.Width = width; - } else { - width = GetDimAsInt (Width, vertical: false); - search.Width = width; - listview.Width = CalculateWidth (); - } + if (Width != null && width == 0) // new ComboBox() { Width = + width = Bounds.Width; - if (Height == null) { - var h = CalculatetHeight (); - listview.Height = h; - this.Height = h + 1; // adjust view to account for search box - } else { - if (height == 0) - height = GetDimAsInt (Height, vertical: true); + search.Width = width; + listview.Width = CalculateWidth (); - listview.Height = CalculatetHeight (); - this.Height = height + 1; // adjust view to account for search box - } + if (Height != null && height == 0) // new ComboBox() { Height = + height = Bounds.Height; + + listview.Height = CalculatetHeight (); + + SetNeedsLayout (); if (this.Text != null) Search_Changed (Text); @@ -216,21 +205,7 @@ namespace Terminal.Gui { } if (e.Key == Key.Enter && listview.HasFocus) { - if (listview.Source.Count == 0 || searchset.Count == 0) { - text = ""; - return true; - } - - SetValue((string)searchset [listview.SelectedItem]); - search.CursorPosition = search.Text.Length; - Search_Changed (search.Text); - OnSelectedChanged (); - - searchset.Clear(); - listview.Clear (); - listview.Height = 0; - this.SetFocus(search); - + Selected (); return true; } @@ -289,14 +264,28 @@ namespace Terminal.Gui { search.TextChanged += Search_Changed; } + private void Selected() + { + if (listview.Source.Count == 0 || searchset.Count == 0) { + text = ""; + return; + } + + SetValue ((string)searchset [listview.SelectedItem]); + search.CursorPosition = search.Text.Length; + Search_Changed (search.Text); + Reset (keepSearchText: true); + } + /// /// Reset to full original list /// - private void Reset() + private void Reset(bool keepSearchText = false) { - search.Text = text = ""; - OnSelectedChanged(); + if(!keepSearchText) + search.Text = text = ""; + OnSelectedChanged (); ResetSearchSet (); listview.SetSource(searchset); @@ -333,15 +322,6 @@ namespace Terminal.Gui { this.SuperView?.BringSubviewToFront (this); } - /// - /// Internal height of dynamic search list - /// - /// - private int CalculatetHeight () - { - return Math.Min (height, searchset.Count); - } - /// /// Internal width of search list /// @@ -352,21 +332,13 @@ namespace Terminal.Gui { } /// - /// Get Dim as integer value + /// Internal height of dynamic search list /// - /// - /// - /// n - private int GetDimAsInt (Dim dim, bool vertical) + /// + private int CalculatetHeight () { - if (dim is Dim.DimAbsolute) - return dim.Anchor (0); - else { // Dim.Fill Dim.Factor - if(autoHide) - return vertical ? dim.Anchor (SuperView.Bounds.Height) : dim.Anchor (SuperView.Bounds.Width); - else - return vertical ? dim.Anchor (Bounds.Height) : dim.Anchor (Bounds.Width); - } + var h = (Height is Dim.DimAbsolute) ? height : Bounds.Height; + return Math.Min (Math.Max (0, h - 1), searchset.Count); } } } diff --git a/UICatalog/Scenarios/ListsAndCombos.cs b/UICatalog/Scenarios/ListsAndCombos.cs index 19d10539c..7418c3c4d 100644 --- a/UICatalog/Scenarios/ListsAndCombos.cs +++ b/UICatalog/Scenarios/ListsAndCombos.cs @@ -26,14 +26,14 @@ namespace UICatalog.Scenarios { var lbListView = new Label ("Listview") { ColorScheme = Colors.TopLevel, X = 0, - Width = 30 + Width = Dim.Percent (40) }; var listview = new ListView (items) { X = 0, Y = Pos.Bottom (lbListView) + 1, Height = Dim.Fill(2), - Width = 30 + Width = Dim.Percent (40) }; listview.OpenSelectedItem += (ListViewItemEventArgs e) => lbListView.Text = items [listview.SelectedItem]; Win.Add (lbListView, listview);