From a3ae8cf1b6f752fb191390fd9a3ae9f579008703 Mon Sep 17 00:00:00 2001 From: BDisp Date: Sun, 15 Jan 2023 22:44:18 +0000 Subject: [PATCH 1/2] Fixes #2289. View.Clear method is clearing beyond its parent bounds. --- Terminal.Gui/Core/View.cs | 29 +++++++++++++------ UnitTests/ViewTests.cs | 59 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 80 insertions(+), 8 deletions(-) diff --git a/Terminal.Gui/Core/View.cs b/Terminal.Gui/Core/View.cs index 2c51d48eed..053b6ebd8e 100644 --- a/Terminal.Gui/Core/View.cs +++ b/Terminal.Gui/Core/View.cs @@ -1093,8 +1093,15 @@ public void BringSubviewForward (View subview) /// public void Clear () { - var h = Frame.Height; - var w = Frame.Width; + Rect containerBounds = GetContainerBounds (); + Rect viewBounds = Bounds; + if (!containerBounds.IsEmpty) { + viewBounds.Width = Math.Min (viewBounds.Width, containerBounds.Width); + viewBounds.Height = Math.Min (viewBounds.Height, containerBounds.Height); + } + + var h = viewBounds.Height; + var w = viewBounds.Width; for (var line = 0; line < h; line++) { Move (0, line); for (var col = 0; col < w; col++) @@ -1499,7 +1506,6 @@ public virtual void Redraw (Rect bounds) } else if (ustring.IsNullOrEmpty (TextFormatter.Text) && (GetType ().IsNestedPublic) && !IsOverridden (this, "Redraw") && (!NeedDisplay.IsEmpty || ChildNeedsDisplay || LayoutNeeded)) { - Clear (); SetChildNeedsDisplay (); } @@ -1511,11 +1517,7 @@ public virtual void Redraw (Rect bounds) if (TextFormatter != null) { TextFormatter.NeedsFormat = true; } - var containerBounds = SuperView == null ? default : SuperView.ViewToScreen (SuperView.Bounds); - containerBounds.X = Math.Max (containerBounds.X, Driver.Clip.X); - containerBounds.Y = Math.Max (containerBounds.Y, Driver.Clip.Y); - containerBounds.Width = Math.Min (containerBounds.Width, Driver.Clip.Width); - containerBounds.Height = Math.Min (containerBounds.Height, Driver.Clip.Height); + Rect containerBounds = GetContainerBounds (); TextFormatter?.Draw (ViewToScreen (Bounds), HasFocus ? ColorScheme.Focus : GetNormalColor (), HasFocus ? ColorScheme.HotFocus : Enabled ? ColorScheme.HotNormal : ColorScheme.Disabled, containerBounds); @@ -1558,6 +1560,17 @@ public virtual void Redraw (Rect bounds) ClearNeedsDisplay (); } + Rect GetContainerBounds () + { + var containerBounds = SuperView == null ? default : SuperView.ViewToScreen (SuperView.Bounds); + var driverClip = Driver == null ? Rect.Empty : Driver.Clip; + containerBounds.X = Math.Max (containerBounds.X, driverClip.X); + containerBounds.Y = Math.Max (containerBounds.Y, driverClip.Y); + containerBounds.Width = Math.Min (containerBounds.Width, driverClip.Width); + containerBounds.Height = Math.Min (containerBounds.Height, driverClip.Height); + return containerBounds; + } + /// /// Event invoked when the content area of the View is to be drawn. /// diff --git a/UnitTests/ViewTests.cs b/UnitTests/ViewTests.cs index 07931404c9..dd5fbf3ea7 100644 --- a/UnitTests/ViewTests.cs +++ b/UnitTests/ViewTests.cs @@ -4117,5 +4117,64 @@ public void GetHotNormalColor_ColorScheme () view.Enabled = false; Assert.Equal (view.ColorScheme.Disabled, view.GetHotNormalColor ()); } + + [Theory, AutoInitShutdown] + [InlineData (true)] + [InlineData (false)] + public void Clear_Does_Not_Spillover_Its_Parent (bool label) + { + var root = new View () { Width = 20, Height = 10 }; + + var v = label == true ? + new Label (new string ('c', 100)) { + Width = Dim.Fill () + } : + (View)new TextView () { + Height = 1, + Text = new string ('c', 100), + Width = Dim.Fill () + }; + + root.Add (v); + + Application.Top.Add (root); + Application.Begin (Application.Top); + + if (label) { + Assert.True (v.AutoSize); + Assert.False (v.CanFocus); + Assert.Equal (new Rect (0, 0, 100, 1), v.Frame); + } else { + Assert.False (v.AutoSize); + Assert.True (v.CanFocus); + Assert.Equal (new Rect (0, 0, 20, 1), v.Frame); + } + + TestHelpers.AssertDriverContentsWithFrameAre (@" +cccccccccccccccccccc", output); + + var attributes = new Attribute [] { + Colors.TopLevel.Normal, + Colors.TopLevel.Focus, + + }; + if (label) { + TestHelpers.AssertDriverColorsAre (@" +000000000000000000000", attributes); + } else { + TestHelpers.AssertDriverColorsAre (@" +111111111111111111110", attributes); + } + + if (label) { + root.CanFocus = true; + v.CanFocus = true; + Assert.False (v.HasFocus); + v.SetFocus (); + Application.Refresh (); + TestHelpers.AssertDriverColorsAre (@" +111111111111111111110", attributes); + } + } } } From e570a076675f07c329e94c60860d88e4a63087fc Mon Sep 17 00:00:00 2001 From: BDisp Date: Sun, 15 Jan 2023 22:49:34 +0000 Subject: [PATCH 2/2] Reformat. --- Terminal.Gui/Core/View.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/Terminal.Gui/Core/View.cs b/Terminal.Gui/Core/View.cs index 053b6ebd8e..c7104fcdba 100644 --- a/Terminal.Gui/Core/View.cs +++ b/Terminal.Gui/Core/View.cs @@ -1506,6 +1506,7 @@ public virtual void Redraw (Rect bounds) } else if (ustring.IsNullOrEmpty (TextFormatter.Text) && (GetType ().IsNestedPublic) && !IsOverridden (this, "Redraw") && (!NeedDisplay.IsEmpty || ChildNeedsDisplay || LayoutNeeded)) { + Clear (); SetChildNeedsDisplay (); }