Files
Terminal.Gui/Tests/UnitTestsParallelizable/ViewBase/Adornment/MarginTests.cs
Tig 0a9f4b8ef1 Fixes #4492, #4480 - Transparent shadows cause underlying wide glyph rendering issues (#4490)
* WIP - experiments in fixing shadow rendering issues based on #4465

Previously, shadow size was fixed at 1x1. This change introduces ShadowWidth and ShadowHeight properties to both Margin and View, allowing variable shadow dimensions. The Margin class now manages its own shadow sizing, enforcing valid values based on ShadowStyle (e.g., Opaque and Transparent require a minimum of 1, and Opaque only allows 1). Margin.Thickness is dynamically adjusted to account for shadow size, with original values preserved and restored as needed.

ShadowView rendering is updated to correctly handle wide graphemes (such as emojis) in the shadow area, preventing rendering errors. The View class exposes ShadowWidth and ShadowHeight, synchronizing with Margin. Extensive new unit tests verify correct behavior for shadow sizing, style changes, thickness adjustments, and rendering, including edge cases and visual output.

Additional minor bug fixes and refactoring are included, such as proper management of Margin's cached clip region and correcting a loop order bug in ShadowView. The codebase is also modernized with recent C# features.

* more merge

* added border tests

* Experiment...

* Incorporated latest wideglyphs

* Comment tweaks

* Add Adornments and ViewportSettings editors to WideGlyphs

Introduce AdornmentsEditor and ViewportSettingsEditor with custom border styles and positioning, enhancing UI editing capabilities. Also update arrangeableViewAtEven to use Color.Black and Color.Green, and adjust a commented border style from Dashed to Dotted.

* Fix scenario editors and tweak scenarios.

Enhance ShadowStyles with a second shadow window (transparent style) and a button event handler that shows a message box. In WideGlyphs, add AdornmentsEditor and ViewportSettingsEditor for view property editing, apply custom color schemes to arrangeable views, and update superView with a transparent shadow and increased shadow width. These changes improve interactivity and visualization in the demo scenarios.

* Fix scenario editors and tweak scenarios.

Enhance ShadowStyles with a second shadow window (transparent style) and a button event handler that shows a message box. In WideGlyphs, add AdornmentsEditor and ViewportSettingsEditor for view property editing, apply custom color schemes to arrangeable views, and update superView with a transparent shadow and increased shadow width. These changes improve interactivity and visualization in the demo scenarios.

* Make replacement char themeable via Glyphs.ReplacementChar

Adds Glyphs.ReplacementChar as a configurable replacement character, replacing all uses of Rune.ReplacementChar. The default is now a space (' ') and can be set via config.json. Updates all rendering, string decoding, and buffer invalidation logic to use the new property, ensuring consistency and themeability. Updates tests and comments accordingly. Also includes minor UI tweaks in WideGlyphs.cs and .DotSettings updates.

* merging

* merge errors

* merged

* merged

* Refactor shadow properties to Margin; update tests

ShadowWidth and ShadowHeight are now managed solely in the Margin class, with related properties and validation logic removed from View. All code and tests now use view.Margin.ShadowWidth/ShadowHeight. Tests and documentation were updated accordingly, and wide glyph handling in test output was improved for consistency.

* Simplify ShadowSize; remove it from View as it's infreqnetly used. Make it a Size to reduce API surface area.

Replace ShadowWidth/ShadowHeight with a single ShadowSize property (of type Size) in the Margin class and related code. Update all usages, validation logic, and tests to use ShadowSize.Width and ShadowSize.Height. Introduce TryValidateShadowSize for unified validation. Modernize code with C# features and improve clarity and maintainability by treating shadow dimensions as a single unit.

* reveted

* Fix wide glyph attribute handling for second column

Ensure the attribute for the second column of wide glyphs is set correctly when within the clip region, addressing issues #4258 and #4492. Add comprehensive unit tests to verify correct attribute assignment and output rendering, including scenarios with transparent shadows. Remove obsolete test code for clarity. This improves color/style consistency for wide glyphs, especially in overlapping UI situations.

* added url
2025-12-16 14:23:32 -07:00

159 lines
6.0 KiB
C#

#nullable enable
using UnitTests;
using Xunit.Abstractions;
namespace ViewBaseTests.Adornments;
public class MarginTests (ITestOutputHelper output)
{
[Fact]
public void Margin_Is_Transparent ()
{
IApplication? app = Application.Create ();
app.Init ("fake");
app.Driver!.SetScreenSize (5, 5);
var view = new View { Height = 3, Width = 3 };
view.Margin!.Diagnostics = ViewDiagnosticFlags.Thickness;
view.Margin.Thickness = new (1);
Runnable<bool> runnable = new ();
app.Begin (runnable);
runnable.SetScheme (new ()
{
Normal = new (Color.Red, Color.Green), Focus = new (Color.Green, Color.Red)
});
runnable.Add (view);
Assert.Equal (ColorName16.Red, view.Margin.GetAttributeForRole (VisualRole.Normal).Foreground.GetClosestNamedColor16 ());
Assert.Equal (ColorName16.Red, runnable.GetAttributeForRole (VisualRole.Normal).Foreground.GetClosestNamedColor16 ());
app.LayoutAndDraw ();
DriverAssert.AssertDriverContentsAre (
@"",
output,
app.Driver
);
DriverAssert.AssertDriverAttributesAre ("0", output, app.Driver, runnable.GetAttributeForRole (VisualRole.Normal));
}
[Fact]
public void Margin_ViewPortSettings_Not_Transparent_Is_NotTransparent ()
{
IApplication? app = Application.Create ();
app.Init ("fake");
app.Driver!.SetScreenSize (5, 5);
var view = new View { Height = 3, Width = 3 };
view.Margin!.Diagnostics = ViewDiagnosticFlags.Thickness;
view.Margin.Thickness = new (1);
view.Margin.ViewportSettings = ViewportSettingsFlags.None;
Runnable<bool> runnable = new ();
app.Begin (runnable);
runnable.SetScheme (new ()
{
Normal = new (Color.Red, Color.Green), Focus = new (Color.Green, Color.Red)
});
runnable.Add (view);
Assert.Equal (ColorName16.Red, view.Margin.GetAttributeForRole (VisualRole.Normal).Foreground.GetClosestNamedColor16 ());
Assert.Equal (ColorName16.Red, runnable.GetAttributeForRole (VisualRole.Normal).Foreground.GetClosestNamedColor16 ());
app.LayoutAndDraw ();
DriverAssert.AssertDriverContentsAre (
@"
MMM
M M
MMM",
output,
app.Driver
);
DriverAssert.AssertDriverAttributesAre ("0", output, app.Driver, runnable.GetAttributeForRole (VisualRole.Normal));
}
[Fact]
public void Is_Visually_Transparent ()
{
var view = new View { Height = 3, Width = 3 };
Assert.True(view.Margin!.ViewportSettings.HasFlag(ViewportSettingsFlags.Transparent), "Margin should be transparent by default.");
}
[Fact]
public void Is_Transparent_To_Mouse ()
{
var view = new View { Height = 3, Width = 3 };
Assert.True (view.Margin!.ViewportSettings.HasFlag (ViewportSettingsFlags.TransparentMouse), "Margin should be transparent to mouse by default.");
}
[Fact]
public void When_Not_Visually_Transparent ()
{
var view = new View { Height = 3, Width = 3 };
// Give the Margin some size
view.Margin!.Thickness = new Thickness (1, 1, 1, 1);
// Give it Text
view.Margin!.Text = "Test";
// Strip off ViewportSettings.Transparent
view.Margin!.ViewportSettings &= ~ViewportSettingsFlags.Transparent;
//
}
[Fact]
public void Thickness_Is_Empty_By_Default ()
{
var view = new View { Height = 3, Width = 3 };
Assert.Equal (Thickness.Empty, view.Margin!.Thickness);
}
// ShadowStyle
[Fact]
public void Margin_Uses_ShadowStyle_Transparent ()
{
var view = new View { Height = 3, Width = 3, ShadowStyle = ShadowStyle.Transparent };
Assert.Equal (ShadowStyle.Transparent, view.Margin!.ShadowStyle);
Assert.True (view.Margin!.ViewportSettings.HasFlag (ViewportSettingsFlags.TransparentMouse), "Margin should be transparent to mouse when ShadowStyle is Transparent.");
Assert.True (view.Margin!.ViewportSettings.HasFlag (ViewportSettingsFlags.Transparent), "Margin should be transparent when ShadowStyle is Transparent..");
}
[Fact]
public void Margin_Uses_ShadowStyle_Opaque ()
{
var view = new View { Height = 3, Width = 3, ShadowStyle = ShadowStyle.Opaque };
Assert.Equal (ShadowStyle.Opaque, view.Margin!.ShadowStyle);
Assert.True (view.Margin!.ViewportSettings.HasFlag (ViewportSettingsFlags.TransparentMouse), "Margin should be transparent to mouse when ShadowStyle is Opaque.");
Assert.True (view.Margin!.ViewportSettings.HasFlag (ViewportSettingsFlags.Transparent), "Margin should be transparent when ShadowStyle is Opaque..");
}
[Fact]
public void Margin_Layouts_Correctly ()
{
View superview = new () { Width = 10, Height = 5 };
View view = new () { Width = 3, Height = 1, BorderStyle = LineStyle.Single };
view.Margin!.Thickness = new (1);
View view2 = new () { X = Pos.Right (view), Width = 3, Height = 1, BorderStyle = LineStyle.Single };
view2.Margin!.Thickness = new (1);
View view3 = new () { Y = Pos.Bottom (view), Width = 3, Height = 1, BorderStyle = LineStyle.Single };
view3.Margin!.Thickness = new (1);
superview.Add (view, view2, view3);
superview.LayoutSubViews ();
Assert.Equal (new (0, 0, 10, 5), superview.Frame);
Assert.Equal (new (0, 0, 3, 1), view.Frame);
Assert.Equal (Rectangle.Empty, view.Viewport);
Assert.Equal (new (3, 0, 3, 1), view2.Frame);
Assert.Equal (Rectangle.Empty, view2.Viewport);
Assert.Equal (new (0, 1, 3, 1), view3.Frame);
Assert.Equal (Rectangle.Empty, view3.Viewport);
}
}