Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes #2451. ListView SelectedItem should be -1 by default. #2452

Merged
merged 1 commit into from
Mar 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions Terminal.Gui/Views/ComboBox.cs
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ public IListDataSource Source {
if (SuperView != null && SuperView.Subviews.Contains (this)) {
SelectedItem = -1;
search.Text = "";
Search_Changed (this, new TextChangedEventArgs(""));
Search_Changed (this, new TextChangedEventArgs (""));
SetNeedsDisplay ();
}
}
Expand Down Expand Up @@ -316,7 +316,7 @@ private void Initialize ()
}
};

Added += (s, e) => {
Added += (s, e) => {

// Determine if this view is hosted inside a dialog and is the only control
for (View view = this.SuperView; view != null; view = view.SuperView) {
Expand All @@ -328,7 +328,7 @@ private void Initialize ()

SetNeedsLayout ();
SetNeedsDisplay ();
Search_Changed (this, new TextChangedEventArgs( Text));
Search_Changed (this, new TextChangedEventArgs (Text));
};

// Things this view knows how to do
Expand Down Expand Up @@ -745,15 +745,17 @@ private void Selected ()
if (listview.Source.Count == 0 || (searchset?.Count ?? 0) == 0) {
text = "";
HideList ();
isShow = false;
return;
}

SetValue (searchset [listview.SelectedItem]);
SetValue (listview.SelectedItem > -1 ? searchset [listview.SelectedItem] : text);
search.CursorPosition = search.Text.ConsoleWidth;
Search_Changed (this, new TextChangedEventArgs(search.Text));
Search_Changed (this, new TextChangedEventArgs (search.Text));
OnOpenSelectedItem ();
Reset (keepSearchText: true);
HideList ();
isShow = false;
}

private int GetSelectedItemFromSource (ustring value)
Expand Down
26 changes: 12 additions & 14 deletions Terminal.Gui/Views/ListView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public interface IListDataSource {
/// </remarks>
public class ListView : View {
int top, left;
int selected;
int selected = -1;

IListDataSource source;
/// <summary>
Expand All @@ -112,7 +112,7 @@ public IListDataSource Source {
source = value;
KeystrokeNavigator.Collection = source?.ToList ()?.Cast<object> ();
top = 0;
selected = 0;
selected = -1;
lastSelectedItem = -1;
SetNeedsDisplay ();
}
Expand Down Expand Up @@ -207,7 +207,7 @@ public int TopItem {

if (value < 0 || (source.Count > 0 && value >= source.Count))
throw new ArgumentException ("value");
top = value;
top = Math.Max (value, 0);
SetNeedsDisplay ();
}
}
Expand Down Expand Up @@ -485,7 +485,7 @@ public virtual bool MovePageUp ()
n = 0;
if (n != selected) {
selected = n;
top = selected;
top = Math.Max (selected, 0);
OnSelectedChanged ();
SetNeedsDisplay ();
}
Expand All @@ -506,7 +506,7 @@ public virtual bool MovePageDown ()
if (n != selected) {
selected = n;
if (source.Count >= Frame.Height)
top = selected;
top = Math.Max (selected, 0);
else
top = 0;
OnSelectedChanged ();
Expand Down Expand Up @@ -540,17 +540,15 @@ public virtual bool MoveDown ()
if (selected >= top + Frame.Height) {
top++;
} else if (selected < top) {
top = selected;
} else if (selected < top) {
top = selected;
top = Math.Max (selected, 0);
}
OnSelectedChanged ();
SetNeedsDisplay ();
} else if (selected == 0) {
OnSelectedChanged ();
SetNeedsDisplay ();
} else if (selected >= top + Frame.Height) {
top = source.Count - Frame.Height;
top = Math.Max (source.Count - Frame.Height, 0);
SetNeedsDisplay ();
}

Expand Down Expand Up @@ -581,14 +579,14 @@ public virtual bool MoveUp ()
selected = Source.Count - 1;
}
if (selected < top) {
top = selected;
top = Math.Max (selected, 0);
} else if (selected > top + Frame.Height) {
top = Math.Max (selected - Frame.Height + 1, 0);
}
OnSelectedChanged ();
SetNeedsDisplay ();
} else if (selected < top) {
top = selected;
top = Math.Max (selected, 0);
SetNeedsDisplay ();
}
return true;
Expand All @@ -604,7 +602,7 @@ public virtual bool MoveEnd ()
if (source.Count > 0 && selected != source.Count - 1) {
selected = source.Count - 1;
if (top + selected > Frame.Height - 1) {
top = selected;
top = Math.Max (selected, 0);
}
OnSelectedChanged ();
SetNeedsDisplay ();
Expand All @@ -622,7 +620,7 @@ public virtual bool MoveHome ()
{
if (selected != 0) {
selected = 0;
top = selected;
top = Math.Max (selected, 0);
OnSelectedChanged ();
SetNeedsDisplay ();
}
Expand Down Expand Up @@ -750,7 +748,7 @@ public void EnsureSelectedItemVisible ()
{
SuperView?.LayoutSubviews ();
if (selected < top) {
top = selected;
top = Math.Max (selected, 0);
} else if (Frame.Height > 0 && selected >= top + Frame.Height) {
top = Math.Max (selected - Frame.Height + 1, 0);
}
Expand Down
9 changes: 9 additions & 0 deletions UnitTests/Text/CollectionNavigatorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,21 @@ public void OutOfBoundsShouldBeIgnored ()
[Fact]
public void Cycling ()
{
// cycling with 'b'
var n = new CollectionNavigator (simpleStrings);
Assert.Equal (2, n.GetNextMatchingItem (0, 'b'));
Assert.Equal (3, n.GetNextMatchingItem (2, 'b'));

// if 4 (candle) is selected it should loop back to bat
Assert.Equal (2, n.GetNextMatchingItem (4, 'b'));

// cycling with 'a'
n = new CollectionNavigator (simpleStrings);
Assert.Equal (0, n.GetNextMatchingItem (-1, 'a'));
Assert.Equal (1, n.GetNextMatchingItem (0, 'a'));

// if 4 (candle) is selected it should loop back to appricot
Assert.Equal (0, n.GetNextMatchingItem (4, 'a'));
}

[Fact]
Expand Down
31 changes: 16 additions & 15 deletions UnitTests/Views/ComboBoxTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,13 +88,14 @@ public void KeyBindings_Command ()
Assert.Equal (-1, cb.SelectedItem);
Assert.Equal (string.Empty, cb.Text);
var opened = false;
cb.OpenSelectedItem += (s,_) => opened = true;
cb.OpenSelectedItem += (s, _) => opened = true;
Assert.True (cb.ProcessKey (new KeyEvent (Key.Enter, new KeyModifiers ())));
Assert.False (opened);
cb.Text = "Tw";
Assert.True (cb.ProcessKey (new KeyEvent (Key.Enter, new KeyModifiers ())));
Assert.True (opened);
Assert.Equal ("Two", cb.Text);
Assert.Equal ("", cb.Text);
Assert.False (cb.IsShow);
cb.SetSource (null);
Assert.False (cb.ProcessKey (new KeyEvent (Key.Enter, new KeyModifiers ())));
Assert.True (cb.ProcessKey (new KeyEvent (Key.F4, new KeyModifiers ()))); // with no source also expand empty
Expand Down Expand Up @@ -253,11 +254,11 @@ public void Source_Equal_Null_Or_Count_Equal_Zero_Sets_SelectedItem_Equal_To_Min
Assert.True (cb.ProcessKey (new KeyEvent (Key.Enter, new KeyModifiers ())));
Assert.False (cb.IsShow);
Assert.Equal (2, cb.Source.Count);
Assert.Equal (1, cb.SelectedItem);
Assert.Equal ("Two", cb.Text);
Assert.Equal (-1, cb.SelectedItem);
Assert.Equal ("", cb.Text);
Assert.True (cb.ProcessKey (new KeyEvent (Key.Esc, new KeyModifiers ())));
Assert.False (cb.IsShow);
Assert.Equal (1, cb.SelectedItem); // retains last accept selected item
Assert.Equal (-1, cb.SelectedItem); // retains last accept selected item
Assert.Equal ("", cb.Text); // clear text
cb.SetSource (new List<string> ());
Assert.Equal (0, cb.Source.Count);
Expand All @@ -274,7 +275,7 @@ public void HideDropdownListOnClick_Gets_Sets ()
Width = 5
};
cb.SetSource (new List<string> { "One", "Two", "Three" });
cb.OpenSelectedItem += (s,e) => selected = e.Value.ToString ();
cb.OpenSelectedItem += (s, e) => selected = e.Value.ToString ();
Application.Top.Add (cb);
Application.Begin (Application.Top);

Expand Down Expand Up @@ -369,7 +370,7 @@ public void HideDropdownListOnClick_True_OpenSelectedItem_With_Mouse_And_Key_And
HideDropdownListOnClick = true
};
cb.SetSource (new List<string> { "One", "Two", "Three" });
cb.OpenSelectedItem += (s,e) => selected = e.Value.ToString ();
cb.OpenSelectedItem += (s, e) => selected = e.Value.ToString ();
Application.Top.Add (cb);
Application.Begin (Application.Top);

Expand Down Expand Up @@ -431,7 +432,7 @@ public void HideDropdownListOnClick_True_OpenSelectedItem_With_Mouse_And_Key_Cur
HideDropdownListOnClick = true
};
cb.SetSource (new List<string> { "One", "Two", "Three" });
cb.OpenSelectedItem += (s,e) => selected = e.Value.ToString ();
cb.OpenSelectedItem += (s, e) => selected = e.Value.ToString ();
Application.Top.Add (cb);
Application.Begin (Application.Top);

Expand Down Expand Up @@ -551,7 +552,7 @@ public void HideDropdownListOnClick_False_ReadOnly_True_OpenSelectedItem_With_Mo
ReadOnly = true
};
cb.SetSource (new List<string> { "One", "Two", "Three" });
cb.OpenSelectedItem += (s,e) => selected = e.Value.ToString ();
cb.OpenSelectedItem += (s, e) => selected = e.Value.ToString ();
Application.Top.Add (cb);
Application.Begin (Application.Top);

Expand Down Expand Up @@ -611,7 +612,7 @@ public void HideDropdownListOnClick_True_OpenSelectedItem_With_Mouse_And_Key_F4
HideDropdownListOnClick = true
};
cb.SetSource (new List<string> { "One", "Two", "Three" });
cb.OpenSelectedItem += (s,e) => selected = e.Value.ToString ();
cb.OpenSelectedItem += (s, e) => selected = e.Value.ToString ();
Application.Top.Add (cb);
Application.Begin (Application.Top);

Expand Down Expand Up @@ -648,7 +649,7 @@ public void HideDropdownListOnClick_False_OpenSelectedItem_With_Mouse_And_Key_F4
HideDropdownListOnClick = false
};
cb.SetSource (new List<string> { "One", "Two", "Three" });
cb.OpenSelectedItem += (s,e) => selected = e.Value.ToString ();
cb.OpenSelectedItem += (s, e) => selected = e.Value.ToString ();
Application.Top.Add (cb);
Application.Begin (Application.Top);

Expand Down Expand Up @@ -685,7 +686,7 @@ public void HideDropdownListOnClick_True_Colapse_On_Click_Outside_Frame ()
HideDropdownListOnClick = true
};
cb.SetSource (new List<string> { "One", "Two", "Three" });
cb.OpenSelectedItem += (s,e) => selected = e.Value.ToString ();
cb.OpenSelectedItem += (s, e) => selected = e.Value.ToString ();
Application.Top.Add (cb);
Application.Begin (Application.Top);

Expand Down Expand Up @@ -790,7 +791,7 @@ public void HideDropdownListOnClick_True_Highlight_Current_Item ()
HideDropdownListOnClick = true,
};
cb.SetSource (new List<string> { "One", "Two", "Three" });
cb.OpenSelectedItem += (s,e) => selected = e.Value.ToString ();
cb.OpenSelectedItem += (s, e) => selected = e.Value.ToString ();
Application.Top.Add (cb);
Application.Begin (Application.Top);

Expand Down Expand Up @@ -912,8 +913,8 @@ public void Expanded_Collapsed_Events ()
};
var list = new List<string> { "One", "Two", "Three" };

cb.Expanded += (s,e) => cb.SetSource (list);
cb.Collapsed += (s,e) => cb.Source = null;
cb.Expanded += (s, e) => cb.SetSource (list);
cb.Collapsed += (s, e) => cb.Source = null;

Application.Top.Add (cb);
Application.Begin (Application.Top);
Expand Down
Loading