From 36469b729886f90270dd8cd84fd84d81279c62fa Mon Sep 17 00:00:00 2001 From: BDisp Date: Thu, 11 Apr 2024 17:44:16 +0100 Subject: [PATCH 1/7] Fix horizontal scrolling in the ListView. --- Terminal.Gui/Views/ListView.cs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/Terminal.Gui/Views/ListView.cs b/Terminal.Gui/Views/ListView.cs index 783d59388..f1de20abc 100644 --- a/Terminal.Gui/Views/ListView.cs +++ b/Terminal.Gui/Views/ListView.cs @@ -912,7 +912,7 @@ public class ListWrapper : IListDataSource int start = 0 ) { - container.Move (col, line); + container.Move (Math.Max (col - start, 0), line); object t = _source? [item]; if (t is null) @@ -1028,7 +1028,8 @@ public class ListWrapper : IListDataSource private void RenderUstr (ConsoleDriver driver, string ustr, int col, int line, int width, int start = 0) { - string u = TextFormatter.ClipAndJustify (ustr, width, TextAlignment.Left); + string str = start > ustr.GetColumns () ? string.Empty : ustr.Substring (Math.Min (start, ustr.ToRunes ().Length - 1)); + string u = TextFormatter.ClipAndJustify (str, width, TextAlignment.Left); driver.AddStr (u); width -= u.GetColumns (); From a121b62c770c83ef28f1e6226fb139db695585c2 Mon Sep 17 00:00:00 2001 From: BDisp Date: Thu, 11 Apr 2024 17:44:59 +0100 Subject: [PATCH 2/7] Fix size in the scenario. --- UICatalog/Scenarios/ListViewWithSelection.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/UICatalog/Scenarios/ListViewWithSelection.cs b/UICatalog/Scenarios/ListViewWithSelection.cs index 4dd7d00ad..a926e50f9 100644 --- a/UICatalog/Scenarios/ListViewWithSelection.cs +++ b/UICatalog/Scenarios/ListViewWithSelection.cs @@ -84,9 +84,9 @@ public class ListViewWithSelection : Scenario _listView.DrawContent += (s, e) => { - scrollBar.Size = _listView.Source.Count - 1; + scrollBar.Size = _listView.Source.Count; scrollBar.Position = _listView.TopItem; - scrollBar.OtherScrollBarView.Size = _listView.MaxLength - 1; + scrollBar.OtherScrollBarView.Size = _listView.MaxLength; scrollBar.OtherScrollBarView.Position = _listView.LeftItem; scrollBar.Refresh (); }; From 24aef987be65ff60dc3058ba733cf93009f63eeb Mon Sep 17 00:00:00 2001 From: BDisp Date: Thu, 11 Apr 2024 18:00:12 +0100 Subject: [PATCH 3/7] Restoring unit test from v1. --- UnitTests/Views/ListViewTests.cs | 35 ++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/UnitTests/Views/ListViewTests.cs b/UnitTests/Views/ListViewTests.cs index a7ee74fc7..835d0fe4e 100644 --- a/UnitTests/Views/ListViewTests.cs +++ b/UnitTests/Views/ListViewTests.cs @@ -759,4 +759,39 @@ Item 6", Assert.Equal ("Three", selected); Assert.Equal (2, lv.SelectedItem); } + + [Fact] + [AutoInitShutdown] + public void LeftItem_TopItem_Tests () + { + var source = new List (); + for (int i = 0; i < 5; i++) { + source.Add ($"Item {i}"); + } + var lv = new ListView () { + X = 1, + Width = 10, + Height = 5, + Source = new ListWrapper (source) + }; + var top = new Toplevel (); + top.Add (lv); + Application.Begin (top); + + TestHelpers.AssertDriverContentsWithFrameAre (@" + Item 0 + Item 1 + Item 2 + Item 3 + Item 4", _output); + + lv.LeftItem = 1; + lv.TopItem = 1; + Application.Refresh (); + TestHelpers.AssertDriverContentsWithFrameAre (@" + tem 1 + tem 2 + tem 3 + tem 4", _output); + } } From aa72a97cd677fa818d38ebb01268471479249a3b Mon Sep 17 00:00:00 2001 From: tznind Date: Sat, 13 Apr 2024 15:04:30 +0100 Subject: [PATCH 4/7] Add ability to disable AutoSize --- Terminal.Gui/View/Layout/ViewLayout.cs | 7 +++++++ Terminal.Gui/Views/Button.cs | 2 +- Terminal.Gui/Views/CheckBox.cs | 2 +- Terminal.Gui/Views/Label.cs | 2 +- 4 files changed, 10 insertions(+), 3 deletions(-) diff --git a/Terminal.Gui/View/Layout/ViewLayout.cs b/Terminal.Gui/View/Layout/ViewLayout.cs index 2d1c3224b..dd68c4f8d 100644 --- a/Terminal.Gui/View/Layout/ViewLayout.cs +++ b/Terminal.Gui/View/Layout/ViewLayout.cs @@ -35,6 +35,13 @@ public enum LayoutStyle public partial class View { + /// + /// Determines the default usage of in views + /// that support it (e.g. . Set to false if you frequently + /// get Exceptions about changing Width when AutoSize is true. + /// + public static bool DefaultAutoSize = true; + #region Frame private Rectangle _frame; diff --git a/Terminal.Gui/Views/Button.cs b/Terminal.Gui/Views/Button.cs index a710bc2d9..058a25483 100644 --- a/Terminal.Gui/Views/Button.cs +++ b/Terminal.Gui/Views/Button.cs @@ -52,7 +52,7 @@ public class Button : View Height = 1; CanFocus = true; - AutoSize = true; + AutoSize = View.DefaultAutoSize; HighlightStyle |= HighlightStyle.Pressed; #if HOVER HighlightStyle |= HighlightStyle.Hover; diff --git a/Terminal.Gui/Views/CheckBox.cs b/Terminal.Gui/Views/CheckBox.cs index 7b8dc08a8..d9aa9048f 100644 --- a/Terminal.Gui/Views/CheckBox.cs +++ b/Terminal.Gui/Views/CheckBox.cs @@ -24,7 +24,7 @@ public class CheckBox : View Height = 1; CanFocus = true; - AutoSize = true; + AutoSize = View.DefaultAutoSize; // Things this view knows how to do AddCommand (Command.Accept, OnToggled); diff --git a/Terminal.Gui/Views/Label.cs b/Terminal.Gui/Views/Label.cs index 7b7b331c1..ec62a6802 100644 --- a/Terminal.Gui/Views/Label.cs +++ b/Terminal.Gui/Views/Label.cs @@ -16,7 +16,7 @@ public class Label : View public Label () { Height = 1; - AutoSize = true; + AutoSize = View.DefaultAutoSize; // Things this view knows how to do AddCommand (Command.HotKey, FocusNext); From 7d4d77ca6499f760f3c6a1e4746ce5f63271296c Mon Sep 17 00:00:00 2001 From: tznind Date: Sun, 14 Apr 2024 09:40:31 +0100 Subject: [PATCH 5/7] Force AutoSize false instead of throw Exception when setting Width/Height --- Terminal.Gui/View/Layout/ViewLayout.cs | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/Terminal.Gui/View/Layout/ViewLayout.cs b/Terminal.Gui/View/Layout/ViewLayout.cs index dd68c4f8d..aed51b327 100644 --- a/Terminal.Gui/View/Layout/ViewLayout.cs +++ b/Terminal.Gui/View/Layout/ViewLayout.cs @@ -228,7 +228,8 @@ public partial class View if (AutoSize) { - throw new InvalidOperationException (@$"Must set AutoSize to false before setting {nameof (Height)}."); + Debug.WriteLine (@$"Must set AutoSize to false before setting {nameof (Height)}."); + AutoSize = false; } //if (ValidatePosDim) { @@ -236,9 +237,10 @@ public partial class View if (IsAdded && AutoSize && !isValidNewAutoSize) { - throw new InvalidOperationException ( + Debug.WriteLine ( @$"Must set AutoSize to false before setting the {nameof (Height)}." ); + AutoSize = false; } //} @@ -275,14 +277,16 @@ public partial class View if (AutoSize) { - throw new InvalidOperationException (@$"Must set AutoSize to false before setting {nameof (Width)}."); + Debug.WriteLine($@"Must set AutoSize to false before setting {nameof(Width)}."); + AutoSize = false; } bool isValidNewAutoSize = AutoSize && IsValidAutoSizeWidth (_width); if (IsAdded && AutoSize && !isValidNewAutoSize) { - throw new InvalidOperationException (@$"Must set AutoSize to false before setting {nameof (Width)}."); + Debug.WriteLine($@"Must set AutoSize to false before setting {nameof(Width)}."); + AutoSize = false; } OnResizeNeeded (); From fadecc150ab5811c954bbcef9d0f2ae37588057e Mon Sep 17 00:00:00 2001 From: tznind Date: Sun, 14 Apr 2024 09:41:05 +0100 Subject: [PATCH 6/7] Revert "Add ability to disable AutoSize" This reverts commit aa72a97cd677fa818d38ebb01268471479249a3b. --- Terminal.Gui/View/Layout/ViewLayout.cs | 7 ------- Terminal.Gui/Views/Button.cs | 2 +- Terminal.Gui/Views/CheckBox.cs | 2 +- Terminal.Gui/Views/Label.cs | 2 +- 4 files changed, 3 insertions(+), 10 deletions(-) diff --git a/Terminal.Gui/View/Layout/ViewLayout.cs b/Terminal.Gui/View/Layout/ViewLayout.cs index aed51b327..dcfce1110 100644 --- a/Terminal.Gui/View/Layout/ViewLayout.cs +++ b/Terminal.Gui/View/Layout/ViewLayout.cs @@ -35,13 +35,6 @@ public enum LayoutStyle public partial class View { - /// - /// Determines the default usage of in views - /// that support it (e.g. . Set to false if you frequently - /// get Exceptions about changing Width when AutoSize is true. - /// - public static bool DefaultAutoSize = true; - #region Frame private Rectangle _frame; diff --git a/Terminal.Gui/Views/Button.cs b/Terminal.Gui/Views/Button.cs index 058a25483..a710bc2d9 100644 --- a/Terminal.Gui/Views/Button.cs +++ b/Terminal.Gui/Views/Button.cs @@ -52,7 +52,7 @@ public class Button : View Height = 1; CanFocus = true; - AutoSize = View.DefaultAutoSize; + AutoSize = true; HighlightStyle |= HighlightStyle.Pressed; #if HOVER HighlightStyle |= HighlightStyle.Hover; diff --git a/Terminal.Gui/Views/CheckBox.cs b/Terminal.Gui/Views/CheckBox.cs index d9aa9048f..7b8dc08a8 100644 --- a/Terminal.Gui/Views/CheckBox.cs +++ b/Terminal.Gui/Views/CheckBox.cs @@ -24,7 +24,7 @@ public class CheckBox : View Height = 1; CanFocus = true; - AutoSize = View.DefaultAutoSize; + AutoSize = true; // Things this view knows how to do AddCommand (Command.Accept, OnToggled); diff --git a/Terminal.Gui/Views/Label.cs b/Terminal.Gui/Views/Label.cs index ec62a6802..7b7b331c1 100644 --- a/Terminal.Gui/Views/Label.cs +++ b/Terminal.Gui/Views/Label.cs @@ -16,7 +16,7 @@ public class Label : View public Label () { Height = 1; - AutoSize = View.DefaultAutoSize; + AutoSize = true; // Things this view knows how to do AddCommand (Command.HotKey, FocusNext); From caddcdb846f49ff40ad916dd1db9e3cd4bb297d4 Mon Sep 17 00:00:00 2001 From: tznind Date: Sun, 14 Apr 2024 21:46:50 +0100 Subject: [PATCH 7/7] Fix null reference removing tab --- Terminal.Gui/Views/TabView.cs | 2 +- UnitTests/Views/TabViewTests.cs | 20 ++++++++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/Terminal.Gui/Views/TabView.cs b/Terminal.Gui/Views/TabView.cs index 8c7638a1d..aac2bcf94 100644 --- a/Terminal.Gui/Views/TabView.cs +++ b/Terminal.Gui/Views/TabView.cs @@ -177,7 +177,7 @@ public class TabView : View { if (old?.HasFocus == true) { - SelectedTab.SetFocus (); + SelectedTab?.SetFocus (); } OnSelectedTabChanged (old, value); diff --git a/UnitTests/Views/TabViewTests.cs b/UnitTests/Views/TabViewTests.cs index e798fda2d..fd3fa15c3 100644 --- a/UnitTests/Views/TabViewTests.cs +++ b/UnitTests/Views/TabViewTests.cs @@ -1269,6 +1269,26 @@ public class TabViewTests Application.Shutdown (); } + [Fact] + public void RemoveTab_ThatHasFocus () + { + TabView tv = GetTabView (out Tab _, out Tab tab2); + + tv.SelectedTab = tab2; + tab2.HasFocus = true; + + Assert.Equal (2, tv.Tabs.Count); + foreach (var t in tv.Tabs.ToArray ()) + { + tv.RemoveTab (t); + } + + Assert.Empty (tv.Tabs); + + // Shutdown must be called to safely clean up Application if Init has been called + Application.Shutdown (); + } + private TabView GetTabView () { return GetTabView (out _, out _); } private TabView GetTabView (out Tab tab1, out Tab tab2, bool initFakeDriver = true)