mirror of
https://github.com/gui-cs/Terminal.Gui.git
synced 2025-12-26 15:57:56 +01:00
Updated the Transparent scenario to better demonstrate transparency features, including dynamic resizing, custom drawing, and improved clarity in the TransparentView class. Added new methods to support non-rectangular drawn regions and transparency effects.
Enhanced the `DrawContext` and `View` classes with detailed documentation and examples for implementing transparency. Improved the `OnDrawingContent` method and `DrawingContent` event to support reporting drawn regions for transparency. Performed general code cleanup, including removing unused code, simplifying `ViewportSettings` usage, and improving property initialization. Minor namespace cleanup was also included.
This commit is contained in:
@@ -1,8 +1,9 @@
|
||||
// ReSharper disable AccessToDisposedClosure
|
||||
#nullable enable
|
||||
|
||||
namespace UICatalog.Scenarios;
|
||||
|
||||
[ScenarioMetadata ("Transparent", "Testing Transparency")]
|
||||
[ScenarioMetadata ("Transparent", "Demonstrates View Transparency")]
|
||||
public sealed class Transparent : Scenario
|
||||
{
|
||||
public override void Main ()
|
||||
@@ -53,11 +54,19 @@ public sealed class Transparent : Scenario
|
||||
|
||||
var tv = new TransparentView ()
|
||||
{
|
||||
X = 3,
|
||||
Y = 3,
|
||||
Width = 50,
|
||||
Height = 15
|
||||
X = 2,
|
||||
Y = 2,
|
||||
Width = Dim.Fill (10),
|
||||
Height = Dim.Fill (10)
|
||||
};
|
||||
|
||||
appWindow.ViewportChanged += (sender, args) =>
|
||||
{
|
||||
// Little hack to convert the Dim.Fill to actual size
|
||||
// So resizing works
|
||||
tv.Width = appWindow!.Frame.Width - 10;
|
||||
tv.Height = appWindow!.Frame.Height - 10;
|
||||
};
|
||||
appWindow.Add (tv);
|
||||
|
||||
// Run - Start the application.
|
||||
@@ -72,34 +81,31 @@ public sealed class Transparent : Scenario
|
||||
{
|
||||
public TransparentView ()
|
||||
{
|
||||
Title = "Transparent View";
|
||||
//base.Text = "View.Text.\nThis should be opaque.\nNote how clipping works?";
|
||||
TextFormatter.Alignment = Alignment.Center;
|
||||
TextFormatter.VerticalAlignment = Alignment.Center;
|
||||
Title = "Transparent View - Move and Resize To See Transparency In Action";
|
||||
base.Text = "View.Text.\nThis should be opaque. Note how clipping works?";
|
||||
Arrangement = ViewArrangement.Overlapped | ViewArrangement.Resizable | ViewArrangement.Movable;
|
||||
ViewportSettings |= Terminal.Gui.ViewBase.ViewportSettingsFlags.Transparent | Terminal.Gui.ViewBase.ViewportSettingsFlags.TransparentMouse;
|
||||
ViewportSettings |= ViewportSettingsFlags.Transparent | ViewportSettingsFlags.TransparentMouse;
|
||||
BorderStyle = LineStyle.RoundedDotted;
|
||||
//SchemeName = "Base";
|
||||
SchemeName = "Base";
|
||||
|
||||
var transparentSubView = new View ()
|
||||
{
|
||||
Text = "Sizable/Movable View with border. Should be opaque. No Shadow.",
|
||||
Text = "Sizable/Movable SunView with border and shadow.",
|
||||
Id = "transparentSubView",
|
||||
X = 4,
|
||||
Y = 8,
|
||||
X = Pos.Center (),
|
||||
Y = Pos.Center (),
|
||||
Width = 20,
|
||||
Height = 8,
|
||||
BorderStyle = LineStyle.Dashed,
|
||||
Arrangement = ViewArrangement.Movable | ViewArrangement.Resizable,
|
||||
// ShadowStyle = ShadowStyle.Transparent,
|
||||
ShadowStyle = ShadowStyle.Transparent,
|
||||
};
|
||||
transparentSubView.Border!.Thickness = new (1, 1, 1, 1);
|
||||
transparentSubView.SchemeName = "Dialog";
|
||||
//transparentSubView.Visible = false;
|
||||
|
||||
Button button = new Button ()
|
||||
{
|
||||
Title = "_Opaque Shadows No Worky",
|
||||
Title = "_Opaque Shadow",
|
||||
X = Pos.Center (),
|
||||
Y = 2,
|
||||
SchemeName = "Dialog",
|
||||
@@ -109,8 +115,6 @@ public sealed class Transparent : Scenario
|
||||
MessageBox.Query (App, "Clicked!", "Button in Transparent View", "_Ok");
|
||||
args.Handled = true;
|
||||
};
|
||||
//button.Visible = false;
|
||||
|
||||
|
||||
var shortcut = new Shortcut ()
|
||||
{
|
||||
@@ -121,7 +125,6 @@ public sealed class Transparent : Scenario
|
||||
HelpText = "Help!",
|
||||
Key = Key.F11,
|
||||
SchemeName = "Base"
|
||||
|
||||
};
|
||||
|
||||
button.ClearingViewport += (sender, args) =>
|
||||
@@ -129,16 +132,91 @@ public sealed class Transparent : Scenario
|
||||
args.Cancel = true;
|
||||
};
|
||||
|
||||
// Subscribe to DrawingContent event to draw "TUI"
|
||||
DrawingContent += TransparentView_DrawingContent;
|
||||
|
||||
base.Add (button);
|
||||
base.Add (shortcut);
|
||||
base.Add (transparentSubView);
|
||||
|
||||
//Padding.Thickness = new (1);
|
||||
//Padding.SchemeName = "Error";
|
||||
Padding!.Thickness = new (1);
|
||||
Padding.Text = "This is the Padding";
|
||||
}
|
||||
|
||||
Margin!.Thickness = new (1);
|
||||
// Margin.ViewportSettings |= Terminal.Gui.ViewportSettingsFlags.Transparent;
|
||||
private void TransparentView_DrawingContent (object? sender, DrawEventArgs e)
|
||||
{
|
||||
// Draw "TUI" text using rectangular regions, positioned after "Hi"
|
||||
// Letter "T"
|
||||
Rectangle tTop = new (20, 5, 7, 2); // Top horizontal bar
|
||||
Rectangle tStem = new (23, 7, 2, 8); // Vertical stem
|
||||
|
||||
// Letter "U"
|
||||
Rectangle uLeft = new (30, 5, 2, 8); // Left vertical bar
|
||||
Rectangle uBottom = new (32, 13, 3, 2); // Bottom horizontal bar
|
||||
Rectangle uRight = new (35, 5, 2, 8); // Right vertical bar
|
||||
|
||||
// Letter "I"
|
||||
Rectangle iTop = new (39, 5, 4, 2); // Bar on top
|
||||
Rectangle iStem = new (40, 7, 2, 6); // Vertical stem
|
||||
Rectangle iBottom = new (39, 13, 4, 2); // Bar on Bottom
|
||||
|
||||
// Draw "TUI" using the HotActive attribute
|
||||
SetAttributeForRole (VisualRole.HotActive);
|
||||
FillRect (tTop, Glyphs.BlackCircle);
|
||||
FillRect (tStem, Glyphs.BlackCircle);
|
||||
FillRect (uLeft, Glyphs.BlackCircle);
|
||||
FillRect (uBottom, Glyphs.BlackCircle);
|
||||
FillRect (uRight, Glyphs.BlackCircle);
|
||||
FillRect (iTop, Glyphs.BlackCircle);
|
||||
FillRect (iStem, Glyphs.BlackCircle);
|
||||
FillRect (iBottom, Glyphs.BlackCircle);
|
||||
|
||||
Region tuiRegion = new Region (ViewportToScreen (tTop));
|
||||
tuiRegion.Union (ViewportToScreen (tStem));
|
||||
tuiRegion.Union (ViewportToScreen (uLeft));
|
||||
tuiRegion.Union (ViewportToScreen (uBottom));
|
||||
tuiRegion.Union (ViewportToScreen (uRight));
|
||||
tuiRegion.Union (ViewportToScreen (iTop));
|
||||
tuiRegion.Union (ViewportToScreen (iStem));
|
||||
tuiRegion.Union (ViewportToScreen (iBottom));
|
||||
|
||||
// Register the drawn region for "TUI" to enable transparency effects
|
||||
e.DrawContext?.AddDrawnRegion (tuiRegion);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
protected override bool OnDrawingContent (DrawContext? context)
|
||||
{
|
||||
base.OnDrawingContent (context);
|
||||
|
||||
// Draw "Hi" text using rectangular regions
|
||||
// Letter "H"
|
||||
Rectangle hLeft = new (5, 5, 2, 10); // Left vertical bar
|
||||
Rectangle hMiddle = new (7, 9, 3, 2); // Middle horizontal bar
|
||||
Rectangle hRight = new (10, 5, 2, 10); // Right vertical bar
|
||||
|
||||
// Letter "i" (with some space between H and i)
|
||||
Rectangle iDot = new (15, 5, 2, 2); // Dot on top
|
||||
Rectangle iStem = new (15, 9, 2, 6); // Vertical stem
|
||||
|
||||
// Draw "Hi" using the Highlight attribute
|
||||
SetAttributeForRole (VisualRole.Highlight);
|
||||
FillRect (hLeft, Glyphs.BlackCircle);
|
||||
FillRect (hMiddle, Glyphs.BlackCircle);
|
||||
FillRect (hRight, Glyphs.BlackCircle);
|
||||
FillRect (iDot, Glyphs.BlackCircle);
|
||||
FillRect (iStem, Glyphs.BlackCircle);
|
||||
|
||||
// Register the drawn region for "Hi" to enable transparency effects
|
||||
Region hiRegion = new Region (ViewportToScreen (hLeft));
|
||||
hiRegion.Union (ViewportToScreen (hMiddle));
|
||||
hiRegion.Union (ViewportToScreen (hRight));
|
||||
hiRegion.Union (ViewportToScreen (iDot));
|
||||
hiRegion.Union (ViewportToScreen (iStem));
|
||||
context?.AddDrawnRegion (hiRegion);
|
||||
|
||||
// Return false to allow DrawingContent event to fire
|
||||
return false;
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
|
||||
Reference in New Issue
Block a user