From 45ccf1d5d5fdb2db467bf8067219c1b69303a8ff Mon Sep 17 00:00:00 2001 From: Tig Date: Mon, 26 Aug 2024 14:18:01 -0700 Subject: [PATCH] View.Hierarchy.cs -> nullable enable --- Terminal.Gui/View/View.Hierarchy.cs | 37 +++++++++---------- UnitTests/View/Navigation/AddRemoveTests.cs | 4 +- .../View/Navigation/AdvanceFocusTests.cs | 2 +- UnitTests/View/Navigation/CanFocusTests.cs | 2 +- .../Navigation/HasFocusChangeEventTests.cs | 2 +- UnitTests/View/Navigation/HasFocusTests.cs | 2 +- UnitTests/View/Navigation/NavigationTests.cs | 2 +- UnitTests/View/Navigation/SetFocusTests.cs | 2 +- 8 files changed, 26 insertions(+), 27 deletions(-) diff --git a/Terminal.Gui/View/View.Hierarchy.cs b/Terminal.Gui/View/View.Hierarchy.cs index b9949174d..baae44f7a 100644 --- a/Terminal.Gui/View/View.Hierarchy.cs +++ b/Terminal.Gui/View/View.Hierarchy.cs @@ -1,3 +1,4 @@ +#nullable enable using System.Diagnostics; namespace Terminal.Gui; @@ -5,8 +6,8 @@ namespace Terminal.Gui; public partial class View // SuperView/SubView hierarchy management (SuperView, SubViews, Add, Remove, etc.) { private static readonly IList _empty = new List (0).AsReadOnly (); - private List _subviews; // This is null, and allocated on demand. - private View _superView; + private List? _subviews; // This is null, and allocated on demand. + private View? _superView; /// Indicates whether the view was added to . public bool IsAdded { get; private set; } @@ -19,7 +20,7 @@ public partial class View // SuperView/SubView hierarchy management (SuperView, /// The super view. public virtual View SuperView { - get => _superView; + get => _superView!; set => throw new NotImplementedException (); } @@ -42,14 +43,9 @@ public partial class View // SuperView/SubView hierarchy management (SuperView, /// The view that was added. public virtual View Add (View view) { - if (view is null) - { - return view; - } - if (_subviews is null) { - _subviews = new (); + _subviews = []; } Debug.WriteLineIf (_subviews.Contains (view), $"BUGBUG: {view} has already been added to {this}."); @@ -116,11 +112,11 @@ public partial class View // SuperView/SubView hierarchy management (SuperView, /// Get the top superview of a given . /// The superview view. - public View GetTopSuperView (View view = null, View superview = null) + public View? GetTopSuperView (View? view = null, View? superview = null) { - View top = superview ?? Application.Top; + View? top = superview ?? Application.Top; - for (View v = view?.SuperView ?? this?.SuperView; v != null; v = v.SuperView) + for (View? v = view?.SuperView ?? this?.SuperView; v != null; v = v.SuperView) { top = v; @@ -160,9 +156,12 @@ public partial class View // SuperView/SubView hierarchy management (SuperView, /// lifecycle to be transferred to the caller; the caller muse call . /// /// - public virtual View Remove (View view) + /// + /// The removed View. if the View could not be removed. + /// + public virtual View? Remove (View view) { - if (view is null || _subviews is null) + if (_subviews is null) { return view; } @@ -236,7 +235,7 @@ public partial class View // SuperView/SubView hierarchy management (SuperView, subview, x => { - int idx = _subviews.IndexOf (x); + int idx = _subviews!.IndexOf (x); if (idx + 1 < _subviews.Count) { @@ -257,7 +256,7 @@ public partial class View // SuperView/SubView hierarchy management (SuperView, subview, x => { - _subviews.Remove (x); + _subviews!.Remove (x); _subviews.Add (x); } ); @@ -274,7 +273,7 @@ public partial class View // SuperView/SubView hierarchy management (SuperView, subview, x => { - int idx = _subviews.IndexOf (x); + int idx = _subviews!.IndexOf (x); if (idx > 0) { @@ -295,7 +294,7 @@ public partial class View // SuperView/SubView hierarchy management (SuperView, subview, x => { - _subviews.Remove (x); + _subviews!.Remove (x); _subviews.Insert (0, subview); } ); @@ -308,7 +307,7 @@ public partial class View // SuperView/SubView hierarchy management (SuperView, /// private void PerformActionForSubview (View subview, Action action) { - if (_subviews.Contains (subview)) + if (_subviews!.Contains (subview)) { action (subview); } diff --git a/UnitTests/View/Navigation/AddRemoveTests.cs b/UnitTests/View/Navigation/AddRemoveTests.cs index cb1cd66fa..ae84ba64f 100644 --- a/UnitTests/View/Navigation/AddRemoveTests.cs +++ b/UnitTests/View/Navigation/AddRemoveTests.cs @@ -2,7 +2,7 @@ namespace Terminal.Gui.ViewTests; -public class AddRemoveNavigationTests (ITestOutputHelper _output) : TestsAllViews +public class AddRemoveNavigationTests () : TestsAllViews { [Fact] public void Add_First_Subview_Gets_Focus () @@ -199,7 +199,7 @@ public class AddRemoveNavigationTests (ITestOutputHelper _output) : TestsAllView top.Remove (subView); Assert.True (top.HasFocus); - Assert.Equal (null, top.Focused); + Assert.Null (top.Focused); Assert.False (subView.HasFocus); } diff --git a/UnitTests/View/Navigation/AdvanceFocusTests.cs b/UnitTests/View/Navigation/AdvanceFocusTests.cs index 53f657d89..b3a2e00da 100644 --- a/UnitTests/View/Navigation/AdvanceFocusTests.cs +++ b/UnitTests/View/Navigation/AdvanceFocusTests.cs @@ -2,7 +2,7 @@ namespace Terminal.Gui.ViewTests; -public class AdvanceFocusTests (ITestOutputHelper _output) +public class AdvanceFocusTests () { [Fact] public void AdvanceFocus_CanFocus_Mixed () diff --git a/UnitTests/View/Navigation/CanFocusTests.cs b/UnitTests/View/Navigation/CanFocusTests.cs index 5d3bbc1e8..ede9422e5 100644 --- a/UnitTests/View/Navigation/CanFocusTests.cs +++ b/UnitTests/View/Navigation/CanFocusTests.cs @@ -2,7 +2,7 @@ namespace Terminal.Gui.ViewTests; -public class CanFocusTests (ITestOutputHelper _output) : TestsAllViews +public class CanFocusTests () : TestsAllViews { [Fact] public void CanFocus_False_Prevents_SubSubView_HasFocus () diff --git a/UnitTests/View/Navigation/HasFocusChangeEventTests.cs b/UnitTests/View/Navigation/HasFocusChangeEventTests.cs index 86d11ddd1..24e4d4478 100644 --- a/UnitTests/View/Navigation/HasFocusChangeEventTests.cs +++ b/UnitTests/View/Navigation/HasFocusChangeEventTests.cs @@ -2,7 +2,7 @@ namespace Terminal.Gui.ViewTests; -public class HasFocusChangeEventTests (ITestOutputHelper _output) : TestsAllViews +public class HasFocusChangeEventTests () : TestsAllViews { #region HasFocusChanging_NewValue_True diff --git a/UnitTests/View/Navigation/HasFocusTests.cs b/UnitTests/View/Navigation/HasFocusTests.cs index b078f5501..258c408e9 100644 --- a/UnitTests/View/Navigation/HasFocusTests.cs +++ b/UnitTests/View/Navigation/HasFocusTests.cs @@ -2,7 +2,7 @@ namespace Terminal.Gui.ViewTests; -public class HasFocusTests (ITestOutputHelper _output) : TestsAllViews +public class HasFocusTests () : TestsAllViews { [Fact] diff --git a/UnitTests/View/Navigation/NavigationTests.cs b/UnitTests/View/Navigation/NavigationTests.cs index 27d343f30..b83fdfad1 100644 --- a/UnitTests/View/Navigation/NavigationTests.cs +++ b/UnitTests/View/Navigation/NavigationTests.cs @@ -4,7 +4,7 @@ using static System.Net.Mime.MediaTypeNames; namespace Terminal.Gui.ViewTests; -public class NavigationTests (ITestOutputHelper _output) : TestsAllViews +public class NavigationTests () : TestsAllViews { [Theory] [MemberData (nameof (AllViewTypes))] diff --git a/UnitTests/View/Navigation/SetFocusTests.cs b/UnitTests/View/Navigation/SetFocusTests.cs index 2d3a5abd3..3896e51cb 100644 --- a/UnitTests/View/Navigation/SetFocusTests.cs +++ b/UnitTests/View/Navigation/SetFocusTests.cs @@ -2,7 +2,7 @@ namespace Terminal.Gui.ViewTests; -public class SetFocusTests (ITestOutputHelper _output) : TestsAllViews +public class SetFocusTests () : TestsAllViews { [Fact] public void SetFocus_With_Null_Superview_Does_Not_Throw_Exception ()