Skip to content
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
28 changes: 21 additions & 7 deletions Terminal.Gui/Core/View.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1093,8 +1093,15 @@ public void BringSubviewForward (View subview)
/// </remarks>
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++)
Expand Down Expand Up @@ -1511,11 +1518,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);
Expand Down Expand Up @@ -1558,6 +1561,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;
}

/// <summary>
/// Event invoked when the content area of the View is to be drawn.
/// </summary>
Expand Down
59 changes: 59 additions & 0 deletions UnitTests/ViewTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
}