From 87ea75b3c11d797a65260c42bb60824a7547bc9b Mon Sep 17 00:00:00 2001 From: BDisp Date: Sun, 26 May 2024 17:23:19 +0100 Subject: [PATCH 1/3] v1 Fixes #2897. ListView ListWrapper - marking does work if you give an IList which in which the count changes --- Terminal.Gui/Views/ListView.cs | 30 +++++++++++++++++++++++++++--- 1 file changed, 27 insertions(+), 3 deletions(-) diff --git a/Terminal.Gui/Views/ListView.cs b/Terminal.Gui/Views/ListView.cs index e0d4edbda..f340d7ebd 100644 --- a/Terminal.Gui/Views/ListView.cs +++ b/Terminal.Gui/Views/ListView.cs @@ -749,6 +749,11 @@ namespace Terminal.Gui { public void EnsureSelectedItemVisible () { SuperView?.LayoutSubviews (); + // If last item is selected and is removed, ensures a valid selected item + if (Source != null && selected > Source.Count - 1) { + SelectedItem = Source.Count - 1; + SetNeedsDisplay (); + } if (selected < top) { top = selected; } else if (Frame.Height > 0 && selected >= top + Frame.Height) { @@ -831,11 +836,30 @@ namespace Terminal.Gui { } /// - public int Count => src != null ? src.Count : 0; + public int Count { + get { + CheckAndResizeMarksIfRequired (); + return src?.Count ?? 0; + } + } /// public int Length => len; + void CheckAndResizeMarksIfRequired () + { + if (count != src.Count) { + count = src.Count; + BitArray newMarks = new BitArray (count); + for (var i = 0; i < Math.Min (marks.Length, newMarks.Length); i++) { + newMarks [i] = marks [i]; + } + marks = newMarks; + + len = GetMaxLengthItem (); + } + } + int GetMaxLengthItem () { if (src == null || src?.Count == 0) { @@ -896,7 +920,7 @@ namespace Terminal.Gui { /// public bool IsMarked (int item) { - if (item >= 0 && item < count) + if (item >= 0 && item < Count) return marks [item]; return false; } @@ -904,7 +928,7 @@ namespace Terminal.Gui { /// public void SetMark (int item, bool value) { - if (item >= 0 && item < count) + if (item >= 0 && item < Count) marks [item] = value; } From d7b0899f2beffaeacba6bc28a642ac477f302155 Mon Sep 17 00:00:00 2001 From: BDisp Date: Sun, 26 May 2024 17:39:34 +0100 Subject: [PATCH 2/3] Prevent System.NullReferenceException. --- Terminal.Gui/Views/ListView.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Terminal.Gui/Views/ListView.cs b/Terminal.Gui/Views/ListView.cs index f340d7ebd..0ef93d00e 100644 --- a/Terminal.Gui/Views/ListView.cs +++ b/Terminal.Gui/Views/ListView.cs @@ -848,7 +848,7 @@ namespace Terminal.Gui { void CheckAndResizeMarksIfRequired () { - if (count != src.Count) { + if (src != null && count != src.Count) { count = src.Count; BitArray newMarks = new BitArray (count); for (var i = 0; i < Math.Min (marks.Length, newMarks.Length); i++) { From d5bf412d376fbfc89440ea24370c6a685fb18411 Mon Sep 17 00:00:00 2001 From: BDisp Date: Wed, 29 May 2024 12:34:59 +0100 Subject: [PATCH 3/3] Adjust the top considering the selected item and the frame height. --- Terminal.Gui/Views/ListView.cs | 6 ++++-- UnitTests/Views/ListViewTests.cs | 18 +++++++++--------- 2 files changed, 13 insertions(+), 11 deletions(-) diff --git a/Terminal.Gui/Views/ListView.cs b/Terminal.Gui/Views/ListView.cs index 0ef93d00e..ae6528d94 100644 --- a/Terminal.Gui/Views/ListView.cs +++ b/Terminal.Gui/Views/ListView.cs @@ -601,10 +601,12 @@ namespace Terminal.Gui { /// public virtual bool MoveEnd () { - if (source.Count > 0 && selected != source.Count - 1) { + if (source?.Count > 0 && selected != source.Count - 1) { selected = source.Count - 1; if (top + selected > Frame.Height - 1) { - top = selected; + top = selected < Frame.Height - 1 + ? Math.Max (Frame.Height - selected + 1, 0) + : Math.Max (selected - Frame.Height + 1, 0); } OnSelectedChanged (); SetNeedsDisplay (); diff --git a/UnitTests/Views/ListViewTests.cs b/UnitTests/Views/ListViewTests.cs index f705dab25..1f6589204 100644 --- a/UnitTests/Views/ListViewTests.cs +++ b/UnitTests/Views/ListViewTests.cs @@ -300,16 +300,16 @@ namespace Terminal.Gui.ViewTests { Assert.Equal (19, lv.SelectedItem); TestHelpers.AssertDriverContentsWithFrameAre (@" ┌──────────┐ +│Line10 │ +│Line11 │ +│Line12 │ +│Line13 │ +│Line14 │ +│Line15 │ +│Line16 │ +│Line17 │ +│Line18 │ │Line19 │ -│ │ -│ │ -│ │ -│ │ -│ │ -│ │ -│ │ -│ │ -│ │ └──────────┘", output); Assert.True (lv.ScrollUp (20));