From 65efddbc9ff1a7f080579c6bfec3c02407f434ff Mon Sep 17 00:00:00 2001 From: Tig Date: Tue, 9 Jul 2024 13:26:19 -0600 Subject: [PATCH 1/3] Added Border Settings. Enabled BorderSettings.Gradient --- Terminal.Gui/View/Adornment/Border.cs | 146 ++++++++++++------ Terminal.Gui/View/Adornment/BorderSettings.cs | 26 ++++ Terminal.Gui/View/Adornment/Margin.cs | 2 +- Terminal.Gui/Views/Shortcut.cs | 2 +- UICatalog/Scenarios/BorderEditor.cs | 58 +++++-- 5 files changed, 173 insertions(+), 61 deletions(-) create mode 100644 Terminal.Gui/View/Adornment/BorderSettings.cs diff --git a/Terminal.Gui/View/Adornment/Border.cs b/Terminal.Gui/View/Adornment/Border.cs index 39d98f635f..1ed64abca0 100644 --- a/Terminal.Gui/View/Adornment/Border.cs +++ b/Terminal.Gui/View/Adornment/Border.cs @@ -1,3 +1,5 @@ +using Terminal.Gui.Drawing; + namespace Terminal.Gui; /// The Border for a . @@ -78,7 +80,7 @@ public override void BeginInit () if ((Parent?.Arrangement & ViewArrangement.Movable) != 0) { HighlightStyle |= HighlightStyle.Hover; - } + } #endif base.BeginInit (); @@ -149,31 +151,32 @@ public override ColorScheme ColorScheme } } - Rectangle GetBorderRectangle (Rectangle screenRect) + private Rectangle GetBorderRectangle (Rectangle screenRect) { return new ( - screenRect.X + Math.Max (0, Thickness.Left - 1), - screenRect.Y + Math.Max (0, Thickness.Top - 1), - Math.Max ( - 0, - screenRect.Width - - Math.Max ( - 0, - Math.Max (0, Thickness.Left - 1) - + Math.Max (0, Thickness.Right - 1) - ) - ), - Math.Max ( - 0, - screenRect.Height - - Math.Max ( - 0, - Math.Max (0, Thickness.Top - 1) - + Math.Max (0, Thickness.Bottom - 1) - ) - ) - ); + screenRect.X + Math.Max (0, Thickness.Left - 1), + screenRect.Y + Math.Max (0, Thickness.Top - 1), + Math.Max ( + 0, + screenRect.Width + - Math.Max ( + 0, + Math.Max (0, Thickness.Left - 1) + + Math.Max (0, Thickness.Right - 1) + ) + ), + Math.Max ( + 0, + screenRect.Height + - Math.Max ( + 0, + Math.Max (0, Thickness.Top - 1) + + Math.Max (0, Thickness.Bottom - 1) + ) + ) + ); } + /// /// Sets the style of the border by changing the . This is a helper API for setting the /// to (1,1,1,1) and setting the line style of the views that comprise the border. If @@ -196,21 +199,22 @@ public LineStyle LineStyle set => _lineStyle = value; } - private bool _showTitle = true; + private BorderSettings _settings = BorderSettings.Title; /// - /// Gets or sets whether the title should be shown. The default is . + /// Gets or sets the settings for the border. /// - public bool ShowTitle + public BorderSettings Settings { - get => _showTitle; + get => _settings; set { - if (value == _showTitle) + if (value == _settings) { return; } - _showTitle = value; + + _settings = value; Parent?.SetNeedsDisplay (); } @@ -225,6 +229,7 @@ private void Border_Highlight (object sender, CancelEventArgs e) if (!Parent.Arrangement.HasFlag (ViewArrangement.Movable)) { e.Cancel = true; + return; } @@ -235,9 +240,9 @@ private void Border_Highlight (object sender, CancelEventArgs e) _savedForeColor = ColorScheme.Normal.Foreground; } - ColorScheme cs = new ColorScheme (ColorScheme) + var cs = new ColorScheme (ColorScheme) { - Normal = new Attribute (ColorScheme.Normal.Foreground.GetHighlightColor (), ColorScheme.Normal.Background) + Normal = new (ColorScheme.Normal.Foreground.GetHighlightColor (), ColorScheme.Normal.Background) }; ColorScheme = cs; } @@ -254,12 +259,13 @@ private void Border_Highlight (object sender, CancelEventArgs e) if (e.NewValue == HighlightStyle.None && _savedForeColor.HasValue) { - ColorScheme cs = new ColorScheme (ColorScheme) + var cs = new ColorScheme (ColorScheme) { - Normal = new Attribute (_savedForeColor.Value, ColorScheme.Normal.Background) + Normal = new (_savedForeColor.Value, ColorScheme.Normal.Background) }; ColorScheme = cs; } + Parent?.SetNeedsDisplay (); e.Cancel = true; } @@ -267,7 +273,7 @@ private void Border_Highlight (object sender, CancelEventArgs e) private Point? _dragPosition; private Point _startGrabPoint; - /// + /// protected internal override bool OnMouseEvent (MouseEvent mouseEvent) { if (base.OnMouseEvent (mouseEvent)) @@ -322,16 +328,17 @@ protected internal override bool OnMouseEvent (MouseEvent mouseEvent) _dragPosition = mouseEvent.Position; - Point parentLoc = Parent.SuperView?.ScreenToViewport (new (mouseEvent.ScreenPosition.X, mouseEvent.ScreenPosition.Y)) ?? mouseEvent.ScreenPosition; + Point parentLoc = Parent.SuperView?.ScreenToViewport (new (mouseEvent.ScreenPosition.X, mouseEvent.ScreenPosition.Y)) + ?? mouseEvent.ScreenPosition; GetLocationEnsuringFullVisibility ( - Parent, - parentLoc.X - _startGrabPoint.X, - parentLoc.Y - _startGrabPoint.Y, - out int nx, - out int ny, - out _ - ); + Parent, + parentLoc.X - _startGrabPoint.X, + parentLoc.Y - _startGrabPoint.Y, + out int nx, + out int ny, + out _ + ); Parent.X = nx; Parent.Y = ny; @@ -352,7 +359,6 @@ out _ return false; } - /// protected override void Dispose (bool disposing) { @@ -403,7 +409,7 @@ public override void OnDrawContent (Rectangle viewport) // ...thickness extends outward (border/title is always as far in as possible) // PERF: How about a call to Rectangle.Offset? - var borderBounds = GetBorderRectangle (screenBounds); + Rectangle borderBounds = GetBorderRectangle (screenBounds); int topTitleLineY = borderBounds.Y; int titleY = borderBounds.Y; var titleBarsLength = 0; // the little vertical thingies @@ -421,7 +427,7 @@ public override void OnDrawContent (Rectangle viewport) int sideLineLength = borderBounds.Height; bool canDrawBorder = borderBounds is { Width: > 0, Height: > 0 }; - if (ShowTitle) + if (Settings.FastHasFlags (BorderSettings.Title)) { if (Thickness.Top == 2) { @@ -453,9 +459,10 @@ public override void OnDrawContent (Rectangle viewport) } } - if (canDrawBorder && Thickness.Top > 0 && maxTitleWidth > 0 && ShowTitle && !string.IsNullOrEmpty (Parent?.Title)) + if (canDrawBorder && Thickness.Top > 0 && maxTitleWidth > 0 && Settings.FastHasFlags (BorderSettings.Title) && !string.IsNullOrEmpty (Parent?.Title)) { - var focus = Parent.GetNormalColor (); + Attribute focus = Parent.GetNormalColor (); + if (Parent.SuperView is { } && Parent.SuperView?.Subviews!.Count (s => s.CanFocus) > 1) { // Only use focus color if there are multiple focusable views @@ -492,7 +499,7 @@ public override void OnDrawContent (Rectangle viewport) { // ╔╡Title╞═════╗ // ╔╡╞═════╗ - if (borderBounds.Width < 4 || !ShowTitle || string.IsNullOrEmpty (Parent?.Title)) + if (borderBounds.Width < 4 || !Settings.FastHasFlags (BorderSettings.Title) || string.IsNullOrEmpty (Parent?.Title)) { // ╔╡╞╗ should be ╔══╗ lc.AddLine ( @@ -631,7 +638,7 @@ public override void OnDrawContent (Rectangle viewport) Driver.SetAttribute (prevAttr); // TODO: This should be moved to LineCanvas as a new BorderStyle.Ruler - if (View.Diagnostics.HasFlag (ViewDiagnosticFlags.Ruler)) + if (Diagnostics.HasFlag (ViewDiagnosticFlags.Ruler)) { // Top var hruler = new Ruler { Length = screenBounds.Width, Orientation = Orientation.Horizontal }; @@ -642,7 +649,7 @@ public override void OnDrawContent (Rectangle viewport) } // Redraw title - if (drawTop && maxTitleWidth > 0 && ShowTitle) + if (drawTop && maxTitleWidth > 0 && Settings.FastHasFlags (BorderSettings.Title)) { Parent.TitleTextFormatter.Draw ( new (borderBounds.X + 2, titleY, maxTitleWidth, 1), @@ -670,6 +677,45 @@ public override void OnDrawContent (Rectangle viewport) vruler.Draw (new (screenBounds.X + screenBounds.Width - 1, screenBounds.Y + 1), 1); } } + + // TODO: This should not be done on each draw? + if (Settings.FastHasFlags (BorderSettings.Gradient)) + { + SetupGradientLineCanvas (lc, screenBounds); + } + else + { + lc.Fill = null; + } } } + + private void SetupGradientLineCanvas (LineCanvas lc, Rectangle rect) + { + GetAppealingGradientColors (out List stops, out List steps); + + var g = new Gradient (stops, steps); + + var fore = new GradientFill (rect, g, GradientDirection.Diagonal); + var back = new SolidFill (GetNormalColor ().Background); + + lc.Fill = new (fore, back); + } + + private static void GetAppealingGradientColors (out List stops, out List steps) + { + // Define the colors of the gradient stops with more appealing colors + stops = new() + { + new (0, 128, 255), // Bright Blue + new (0, 255, 128), // Bright Green + new (255, 255), // Bright Yellow + new (255, 128), // Bright Orange + new (255, 0, 128) // Bright Pink + }; + + // Define the number of steps between each color for smoother transitions + // If we pass only a single value then it will assume equal steps between all pairs + steps = new() { 15 }; + } } diff --git a/Terminal.Gui/View/Adornment/BorderSettings.cs b/Terminal.Gui/View/Adornment/BorderSettings.cs new file mode 100644 index 0000000000..5829d1ed67 --- /dev/null +++ b/Terminal.Gui/View/Adornment/BorderSettings.cs @@ -0,0 +1,26 @@ +using Terminal.Gui.Analyzers.Internal.Attributes; + +namespace Terminal.Gui; + +/// +/// Determines the settings for . +/// +[Flags] +[GenerateEnumExtensionMethods (FastHasFlags = true)] +public enum BorderSettings +{ + /// + /// No settings. + /// + None = 0, + + /// + /// Show the title. + /// + Title = 1, + + /// + /// Use to draw the border. + /// + Gradient = 2, +} diff --git a/Terminal.Gui/View/Adornment/Margin.cs b/Terminal.Gui/View/Adornment/Margin.cs index 2e1ea57602..046965e321 100644 --- a/Terminal.Gui/View/Adornment/Margin.cs +++ b/Terminal.Gui/View/Adornment/Margin.cs @@ -223,7 +223,7 @@ private void Margin_LayoutStarted (object? sender, LayoutEventArgs e) if (ShadowStyle != ShadowStyle.None && _rightShadow is { } && _bottomShadow is { }) { _rightShadow.Y = Parent.Border.Thickness.Top > 0 - ? Parent.Border.Thickness.Top - (Parent.Border.Thickness.Top > 2 && Parent.Border.ShowTitle ? 1 : 0) + ? Parent.Border.Thickness.Top - (Parent.Border.Thickness.Top > 2 && Parent.Border.Settings.FastHasFlags (BorderSettings.Title) ? 1 : 0) : 1; _bottomShadow.X = Parent.Border.Thickness.Left > 0 ? Parent.Border.Thickness.Left : 1; } diff --git a/Terminal.Gui/Views/Shortcut.cs b/Terminal.Gui/Views/Shortcut.cs index 802631d521..7ddbe7c5a0 100644 --- a/Terminal.Gui/Views/Shortcut.cs +++ b/Terminal.Gui/Views/Shortcut.cs @@ -104,7 +104,7 @@ public Shortcut (Key key, string commandText, Action action, string helpText = n void OnInitialized (object sender, EventArgs e) { SuperViewRendersLineCanvas = true; - Border.ShowTitle = false; + Border.Settings &= ~BorderSettings.Title; ShowHide (); diff --git a/UICatalog/Scenarios/BorderEditor.cs b/UICatalog/Scenarios/BorderEditor.cs index a5ccae2129..5e0a6e77a6 100644 --- a/UICatalog/Scenarios/BorderEditor.cs +++ b/UICatalog/Scenarios/BorderEditor.cs @@ -9,29 +9,30 @@ public class BorderEditor : AdornmentEditor { private CheckBox _ckbTitle; private RadioGroup _rbBorderStyle; + private CheckBox _ckbGradient; public BorderEditor () { Title = "_Border"; Initialized += BorderEditor_Initialized; AdornmentChanged += BorderEditor_AdornmentChanged; - } private void BorderEditor_AdornmentChanged (object sender, EventArgs e) { - _ckbTitle.State = ((Border)AdornmentToEdit).ShowTitle ? CheckState.Checked : CheckState.UnChecked; + _ckbTitle.State = ((Border)AdornmentToEdit).Settings.FastHasFlags (BorderSettings.Title) ? CheckState.Checked : CheckState.UnChecked; _rbBorderStyle.SelectedItem = (int)((Border)AdornmentToEdit).LineStyle; + _ckbGradient.State = ((Border)AdornmentToEdit).Settings.FastHasFlags (BorderSettings.Gradient) ? CheckState.Checked : CheckState.UnChecked; } private void BorderEditor_Initialized (object sender, EventArgs e) { - List borderStyleEnum = Enum.GetValues (typeof (LineStyle)).Cast ().ToList (); - _rbBorderStyle = new RadioGroup + _rbBorderStyle = new() { X = 0, + // BUGBUG: Hack until dimauto is working properly Y = Pos.Bottom (Subviews [^1]), Width = Dim.Width (Subviews [^2]) + Dim.Width (Subviews [^1]) - 1, @@ -46,21 +47,34 @@ private void BorderEditor_Initialized (object sender, EventArgs e) _rbBorderStyle.SelectedItemChanged += OnRbBorderStyleOnSelectedItemChanged; - _ckbTitle = new CheckBox + _ckbTitle = new() { X = 0, Y = Pos.Bottom (_rbBorderStyle), State = CheckState.Checked, SuperViewRendersLineCanvas = true, - Text = "Show Title", + Text = "Title", Enabled = AdornmentToEdit is { } }; - _ckbTitle.Toggle += OnCkbTitleOnToggle; Add (_ckbTitle); + _ckbGradient = new () + { + X = 0, + Y = Pos.Bottom (_ckbTitle), + + State = CheckState.Checked, + SuperViewRendersLineCanvas = true, + Text = "Gradient", + Enabled = AdornmentToEdit is { } + }; + + _ckbGradient.Toggle += OnCkbGradientOnToggle; + Add (_ckbGradient); + return; void OnRbBorderStyleOnSelectedItemChanged (object s, SelectedItemChangedArgs e) @@ -81,6 +95,32 @@ void OnRbBorderStyleOnSelectedItemChanged (object s, SelectedItemChangedArgs e) LayoutSubviews (); } - void OnCkbTitleOnToggle (object sender, CancelEventArgs args) { ((Border)AdornmentToEdit).ShowTitle = args.NewValue == CheckState.Checked; } + void OnCkbTitleOnToggle (object sender, CancelEventArgs args) + { + if (args.NewValue == CheckState.Checked) + + { + ((Border)AdornmentToEdit).Settings |= BorderSettings.Title; + } + else + + { + ((Border)AdornmentToEdit).Settings &= ~BorderSettings.Title; + } + } + + void OnCkbGradientOnToggle (object sender, CancelEventArgs args) + { + if (args.NewValue == CheckState.Checked) + + { + ((Border)AdornmentToEdit).Settings |= BorderSettings.Gradient; + } + else + + { + ((Border)AdornmentToEdit).Settings &= ~BorderSettings.Gradient; + } + } } -} \ No newline at end of file +} From d0f1280f293ae5d63bd0cff442c51655a8e481e4 Mon Sep 17 00:00:00 2001 From: Tig Date: Tue, 9 Jul 2024 13:37:08 -0600 Subject: [PATCH 2/3] Code cleanup --- Terminal.Gui/Drawing/FillPair.cs | 27 +-- Terminal.Gui/Drawing/Gradient.cs | 138 ++++++------ Terminal.Gui/Drawing/GradientFill.cs | 27 +-- Terminal.Gui/Drawing/IFill.cs | 9 +- Terminal.Gui/Drawing/LineCanvas.cs | 29 ++- Terminal.Gui/Drawing/SolidFill.cs | 23 +- Terminal.Gui/Drawing/StraightLine.cs | 71 +++--- UICatalog/Scenarios/TextEffectsScenario.cs | 209 +++++++++--------- UnitTests/Drawing/FillPairTests.cs | 11 +- UnitTests/Drawing/GradientFillTests.cs | 52 ++--- UnitTests/Drawing/GradientTests.cs | 164 +++++++------- UnitTests/Drawing/LineCanvasTests.cs | 96 ++++---- UnitTests/Drawing/SolidFillTests.cs | 9 +- .../Drawing/StraightLineExtensionsTests.cs | 25 +-- UnitTests/Drawing/StraightLineTests.cs | 9 +- 15 files changed, 442 insertions(+), 457 deletions(-) diff --git a/Terminal.Gui/Drawing/FillPair.cs b/Terminal.Gui/Drawing/FillPair.cs index 648dbb40f2..2150f778d5 100644 --- a/Terminal.Gui/Drawing/FillPair.cs +++ b/Terminal.Gui/Drawing/FillPair.cs @@ -1,18 +1,14 @@ - -using Terminal.Gui.Drawing; - -namespace Terminal.Gui; - +namespace Terminal.Gui; /// -/// Describes a pair of which cooperate in creating -/// . One gives foreground color while other gives background. +/// Describes a pair of which cooperate in creating +/// . One gives foreground color while other gives background. /// public class FillPair { /// - /// Creates a new instance using the provided fills for foreground and background - /// color when assembling . + /// Creates a new instance using the provided fills for foreground and background + /// color when assembling . /// /// /// @@ -23,26 +19,23 @@ public FillPair (IFill fore, IFill back) } /// - /// The fill which provides point based foreground color. + /// The fill which provides point based foreground color. /// public IFill Foreground { get; init; } /// - /// The fill which provides point based background color. + /// The fill which provides point based background color. /// public IFill Background { get; init; } /// - /// Returns the color pair (foreground+background) to use when rendering - /// a rune at the given . + /// Returns the color pair (foreground+background) to use when rendering + /// a rune at the given . /// /// /// public Attribute GetAttribute (Point point) { - return new Attribute ( - Foreground.GetColor (point), - Background.GetColor (point) - ); + return new (Foreground.GetColor (point), Background.GetColor (point)); } } diff --git a/Terminal.Gui/Drawing/Gradient.cs b/Terminal.Gui/Drawing/Gradient.cs index 5091cc314b..fa1cb04823 100644 --- a/Terminal.Gui/Drawing/Gradient.cs +++ b/Terminal.Gui/Drawing/Gradient.cs @@ -2,59 +2,57 @@ namespace Terminal.Gui; -using System; -using System.Collections.Generic; -using System.Linq; - /// -/// Describes the pattern that a results in e.g. , etc +/// Describes the pattern that a results in e.g. , +/// etc /// public enum GradientDirection { /// - /// Color varies along Y axis but is constant on X axis. + /// Color varies along Y axis but is constant on X axis. /// Vertical, /// - /// Color varies along X axis but is constant on Y axis. + /// Color varies along X axis but is constant on Y axis. /// Horizontal, - /// - /// Color varies by distance from center (i.e. in circular ripples) + /// Color varies by distance from center (i.e. in circular ripples) /// Radial, /// - /// Color varies by X and Y axis (i.e. a slanted gradient) + /// Color varies by X and Y axis (i.e. a slanted gradient) /// Diagonal } /// -/// Describes +/// Describes /// public class Gradient { /// - /// The discrete colors that will make up the . + /// The discrete colors that will make up the . /// - public List Spectrum { get; private set; } + public List Spectrum { get; } + private readonly bool _loop; private readonly List _stops; private readonly List _steps; - /// - /// Creates a new instance of the class which hosts a - /// of colors including all and interpolated colors - /// between each corresponding pair. + /// Creates a new instance of the class which hosts a + /// of colors including all and interpolated colors + /// between each corresponding pair. /// /// The colors to use in the spectrum (N) - /// The number of colors to generate between each pair (must be N-1 numbers). - /// If only one step is passed then it is assumed to be the same distance for all pairs. + /// + /// The number of colors to generate between each pair (must be N-1 numbers). + /// If only one step is passed then it is assumed to be the same distance for all pairs. + /// /// True to duplicate the first stop and step so that the gradient repeats itself /// public Gradient (IEnumerable stops, IEnumerable steps, bool loop = false) @@ -71,13 +69,13 @@ public Gradient (IEnumerable stops, IEnumerable steps, bool loop = f // If multiple colors and only 1 step assume same distance applies to all steps if (_stops.Count > 2 && _steps.Count == 1) { - _steps = Enumerable.Repeat (_steps.Single (),_stops.Count() - 1).ToList(); + _steps = Enumerable.Repeat (_steps.Single (), _stops.Count () - 1).ToList (); } if (_steps.Any (step => step < 1)) { throw new ArgumentException ("Steps must be greater than 0."); - } + } if (_steps.Count != _stops.Count - 1) { @@ -89,11 +87,13 @@ public Gradient (IEnumerable stops, IEnumerable steps, bool loop = f } /// - /// Returns the color to use at the given part of the spectrum + /// Returns the color to use at the given part of the spectrum /// - /// Proportion of the way through the spectrum, must be between - /// 0 and 1 (inclusive). Returns the last color if is - /// . + /// + /// Proportion of the way through the spectrum, must be between + /// 0 and 1 (inclusive). Returns the last color if is + /// . + /// /// /// public Color GetColorAtFraction (double fraction) @@ -103,30 +103,32 @@ public Color GetColorAtFraction (double fraction) return Spectrum.Last (); } - if (fraction < 0 || fraction > 1) + if (fraction is < 0 or > 1) { - throw new ArgumentOutOfRangeException (nameof (fraction), "Fraction must be between 0 and 1."); + throw new ArgumentOutOfRangeException (nameof (fraction), @"Fraction must be between 0 and 1."); } - int index = (int)(fraction * (Spectrum.Count - 1)); + var index = (int)(fraction * (Spectrum.Count - 1)); + return Spectrum [index]; } private List GenerateGradient (IEnumerable steps) { - List gradient = new List (); + List gradient = new (); if (_stops.Count == 1) { - for (int i = 0; i < steps.Sum (); i++) + for (var i = 0; i < steps.Sum (); i++) { gradient.Add (_stops [0]); } + return gradient; } - var stopsToUse = _stops.ToList (); - var stepsToUse = _steps.ToList (); + List stopsToUse = _stops.ToList (); + List stepsToUse = _steps.ToList (); if (_loop) { @@ -135,9 +137,9 @@ private List GenerateGradient (IEnumerable steps) } var colorPairs = stopsToUse.Zip (stopsToUse.Skip (1), (start, end) => new { start, end }); - var stepsList = stepsToUse; + List stepsList = stepsToUse; - foreach (var (colorPair, thesteps) in colorPairs.Zip (stepsList, (pair, step) => (pair, step))) + foreach ((var colorPair, int thesteps) in colorPairs.Zip (stepsList, (pair, step) => (pair, step))) { gradient.AddRange (InterpolateColors (colorPair.start, colorPair.end, thesteps)); } @@ -145,28 +147,29 @@ private List GenerateGradient (IEnumerable steps) return gradient; } - private IEnumerable InterpolateColors (Color start, Color end, int steps) + private static IEnumerable InterpolateColors (Color start, Color end, int steps) { - for (int step = 0; step < steps; step++) + for (var step = 0; step < steps; step++) { double fraction = (double)step / steps; - int r = (int)(start.R + fraction * (end.R - start.R)); - int g = (int)(start.G + fraction * (end.G - start.G)); - int b = (int)(start.B + fraction * (end.B - start.B)); - yield return new Color (r, g, b); + var r = (int)(start.R + fraction * (end.R - start.R)); + var g = (int)(start.G + fraction * (end.G - start.G)); + var b = (int)(start.B + fraction * (end.B - start.B)); + + yield return new (r, g, b); } + yield return end; // Ensure the last color is included } - /// - /// - /// Creates a mapping starting at 0,0 and going to and - /// (inclusively) using the supplied . - /// - /// - /// Note that this method is inclusive i.e. passing 1/1 results in 4 mapped coordinates. - /// + /// + /// Creates a mapping starting at 0,0 and going to and + /// (inclusively) using the supplied . + /// + /// + /// Note that this method is inclusive i.e. passing 1/1 results in 4 mapped coordinates. + /// /// /// /// @@ -174,63 +177,69 @@ private IEnumerable InterpolateColors (Color start, Color end, int steps) /// public Dictionary BuildCoordinateColorMapping (int maxRow, int maxColumn, GradientDirection direction) { - var gradientMapping = new Dictionary (); + Dictionary gradientMapping = new (); switch (direction) { case GradientDirection.Vertical: - for (int row = 0; row <= maxRow; row++) + for (var row = 0; row <= maxRow; row++) { double fraction = maxRow == 0 ? 1.0 : (double)row / maxRow; Color color = GetColorAtFraction (fraction); - for (int col = 0; col <= maxColumn; col++) + + for (var col = 0; col <= maxColumn; col++) { - gradientMapping [new Point (col, row)] = color; + gradientMapping [new (col, row)] = color; } } + break; case GradientDirection.Horizontal: - for (int col = 0; col <= maxColumn; col++) + for (var col = 0; col <= maxColumn; col++) { double fraction = maxColumn == 0 ? 1.0 : (double)col / maxColumn; Color color = GetColorAtFraction (fraction); - for (int row = 0; row <= maxRow; row++) + + for (var row = 0; row <= maxRow; row++) { - gradientMapping [new Point (col, row)] = color; + gradientMapping [new (col, row)] = color; } } + break; case GradientDirection.Radial: - for (int row = 0; row <= maxRow; row++) + for (var row = 0; row <= maxRow; row++) { - for (int col = 0; col <= maxColumn; col++) + for (var col = 0; col <= maxColumn; col++) { - double distanceFromCenter = FindNormalizedDistanceFromCenter (maxRow, maxColumn, new Point (col, row)); + double distanceFromCenter = FindNormalizedDistanceFromCenter (maxRow, maxColumn, new (col, row)); Color color = GetColorAtFraction (distanceFromCenter); - gradientMapping [new Point (col, row)] = color; + gradientMapping [new (col, row)] = color; } } + break; case GradientDirection.Diagonal: - for (int row = 0; row <= maxRow; row++) + for (var row = 0; row <= maxRow; row++) { - for (int col = 0; col <= maxColumn; col++) + for (var col = 0; col <= maxColumn; col++) { double fraction = ((double)row * 2 + col) / (maxRow * 2 + maxColumn); Color color = GetColorAtFraction (fraction); - gradientMapping [new Point (col, row)] = color; + gradientMapping [new (col, row)] = color; } } + break; } return gradientMapping; } - private double FindNormalizedDistanceFromCenter (int maxRow, int maxColumn, Point coord) + private static double FindNormalizedDistanceFromCenter (int maxRow, int maxColumn, Point coord) { double centerX = maxColumn / 2.0; double centerY = maxRow / 2.0; @@ -238,6 +247,7 @@ private double FindNormalizedDistanceFromCenter (int maxRow, int maxColumn, Poin double dy = coord.Y - centerY; double distance = Math.Sqrt (dx * dx + dy * dy); double maxDistance = Math.Sqrt (centerX * centerX + centerY * centerY); + return distance / maxDistance; } -} \ No newline at end of file +} diff --git a/Terminal.Gui/Drawing/GradientFill.cs b/Terminal.Gui/Drawing/GradientFill.cs index 6763de8857..6518d2dabb 100644 --- a/Terminal.Gui/Drawing/GradientFill.cs +++ b/Terminal.Gui/Drawing/GradientFill.cs @@ -1,17 +1,17 @@ namespace Terminal.Gui; /// -/// Implementation of that uses a color gradient (including -/// radial, diagonal etc). +/// Implementation of that uses a color gradient (including +/// radial, diagonal etc.). /// public class GradientFill : IFill { - private Dictionary _map; + private readonly Dictionary _map; /// - /// Creates a new instance of the class that can return - /// color for any point in the given using the provided - /// and . + /// Creates a new instance of the class that can return + /// color for any point in the given using the provided + /// and . /// /// /// @@ -19,23 +19,24 @@ public class GradientFill : IFill public GradientFill (Rectangle area, Gradient gradient, GradientDirection direction) { _map = gradient.BuildCoordinateColorMapping (area.Height - 1, area.Width - 1, direction) - .ToDictionary ( - kvp => new Point (kvp.Key.X + area.X, kvp.Key.Y + area.Y), - kvp => kvp.Value); + .ToDictionary ( + kvp => new Point (kvp.Key.X + area.X, kvp.Key.Y + area.Y), + kvp => kvp.Value); } /// - /// Returns the color to use for the given or Black if it - /// lies outside of the prepared gradient area (see constructor). + /// Returns the color to use for the given or Black if it + /// lies outside the prepared gradient area (see constructor). /// /// /// public Color GetColor (Point point) { - if (_map.TryGetValue (point, out var color)) + if (_map.TryGetValue (point, out Color color)) { return color; } - return new Color (0, 0, 0); // Default to black if point not found + + return new (0, 0); // Default to black if point not found } } diff --git a/Terminal.Gui/Drawing/IFill.cs b/Terminal.Gui/Drawing/IFill.cs index 8f81d305a7..7d1d19a68f 100644 --- a/Terminal.Gui/Drawing/IFill.cs +++ b/Terminal.Gui/Drawing/IFill.cs @@ -1,15 +1,14 @@ - -namespace Terminal.Gui; +namespace Terminal.Gui; /// -/// Describes an area fill (e.g. solid color or gradient). +/// Describes an area fill (e.g. solid color or gradient). /// public interface IFill { /// - /// Returns the color that should be used at the given point + /// Returns the color that should be used at the given point /// /// /// Color GetColor (Point point); -} \ No newline at end of file +} diff --git a/Terminal.Gui/Drawing/LineCanvas.cs b/Terminal.Gui/Drawing/LineCanvas.cs index 2c7367fcfb..2bda9e5dd4 100644 --- a/Terminal.Gui/Drawing/LineCanvas.cs +++ b/Terminal.Gui/Drawing/LineCanvas.cs @@ -5,9 +5,9 @@ namespace Terminal.Gui; public class LineCanvas : IDisposable { /// - /// Optional which when present overrides the - /// (colors) of lines in the canvas. This can be used e.g. to apply a global - /// across all lines. + /// Optional which when present overrides the + /// (colors) of lines in the canvas. This can be used e.g. to apply a global + /// across all lines. /// public FillPair? Fill { get; set; } @@ -142,7 +142,7 @@ public void AddLine ( ) { _cachedViewport = Rectangle.Empty; - _lines.Add (new StraightLine (start, length, orientation, style, attribute)); + _lines.Add (new (start, length, orientation, style, attribute)); } /// Adds a new line to the canvas @@ -190,7 +190,7 @@ public void Clear () if (cell is { }) { - map.Add (new Point (x, y), cell); + map.Add (new (x, y), cell); } } } @@ -225,7 +225,7 @@ public Dictionary GetMap (Rectangle inArea) if (rune is { }) { - map.Add (new Point (x, y), rune.Value); + map.Add (new (x, y), rune.Value); } } } @@ -333,8 +333,7 @@ private void ConfigurationManager_Applied (object? sender, ConfigurationManagerE private Attribute? GetAttributeForIntersects (IntersectionDefinition? [] intersects) { - return Fill != null ? Fill.GetAttribute (intersects [0]!.Point) : - intersects [0]!.Line.Attribute; + return Fill != null ? Fill.GetAttribute (intersects [0]!.Point) : intersects [0]!.Line.Attribute; } private Cell? GetCellForIntersects (ConsoleDriver driver, IntersectionDefinition? [] intersects) @@ -439,12 +438,12 @@ private void ConfigurationManager_Applied (object? sender, ConfigurationManagerE useThickDotted ? Glyphs.VLineHvDa4 : Glyphs.VLine; default: - throw new Exception ( - "Could not find resolver or switch case for " - + nameof (runeType) - + ":" - + runeType - ); + throw new ( + "Could not find resolver or switch case for " + + nameof (runeType) + + ":" + + runeType + ); } } @@ -854,4 +853,4 @@ public override void SetGlyphs () _normal = Glyphs.URCorner; } } -} \ No newline at end of file +} diff --git a/Terminal.Gui/Drawing/SolidFill.cs b/Terminal.Gui/Drawing/SolidFill.cs index 67e1bdf1f9..70c549608d 100644 --- a/Terminal.Gui/Drawing/SolidFill.cs +++ b/Terminal.Gui/Drawing/SolidFill.cs @@ -1,31 +1,24 @@ namespace Terminal.Gui.Drawing; - /// -/// implementation that uses a solid color for all points +/// implementation that uses a solid color for all points /// public class SolidFill : IFill { - readonly Color _color; + private readonly Color _color; /// - /// Creates a new instance of the class which will return - /// the provided regardless of which point is requested. + /// Creates a new instance of the class which will return + /// the provided regardless of which point is requested. /// /// - public SolidFill (Color color) - { - _color = color; - } + public SolidFill (Color color) { _color = color; } /// - /// Returns the color this instance was constructed with regardless of - /// which is being colored. + /// Returns the color this instance was constructed with regardless of + /// which is being colored. /// /// /// - public Color GetColor (Point point) - { - return _color; - } + public Color GetColor (Point point) { return _color; } } diff --git a/Terminal.Gui/Drawing/StraightLine.cs b/Terminal.Gui/Drawing/StraightLine.cs index 8bbd83494c..2f36995df6 100644 --- a/Terminal.Gui/Drawing/StraightLine.cs +++ b/Terminal.Gui/Drawing/StraightLine.cs @@ -45,6 +45,7 @@ public StraightLine ( /// Gets the rectangle that describes the bounds of the canvas. Location is the coordinates of the line that is /// furthest left/top and Size is defined by the line that extends the furthest right/bottom. /// + // PERF: Probably better to store the rectangle rather than make a new one on every single access to Viewport. internal Rectangle Viewport { @@ -115,22 +116,24 @@ IntersectionType typeWhenPositive if (StartsAt (x, y)) { - return new IntersectionDefinition (p, - GetTypeByLength ( - IntersectionType.StartLeft, - IntersectionType.PassOverHorizontal, - IntersectionType.StartRight - ), - this - ); + return new ( + p, + GetTypeByLength ( + IntersectionType.StartLeft, + IntersectionType.PassOverHorizontal, + IntersectionType.StartRight + ), + this + ); } if (EndsAt (x, y)) { - return new IntersectionDefinition (p, - Length < 0 ? IntersectionType.StartRight : IntersectionType.StartLeft, - this - ); + return new ( + p, + Length < 0 ? IntersectionType.StartRight : IntersectionType.StartLeft, + this + ); } int xmin = Math.Min (Start.X, Start.X + Length); @@ -138,10 +141,11 @@ IntersectionType typeWhenPositive if (xmin < x && xmax > x) { - return new IntersectionDefinition (p, - IntersectionType.PassOverHorizontal, - this - ); + return new ( + p, + IntersectionType.PassOverHorizontal, + this + ); } return null; @@ -158,22 +162,24 @@ IntersectionType typeWhenPositive if (StartsAt (x, y)) { - return new IntersectionDefinition (p, - GetTypeByLength ( - IntersectionType.StartUp, - IntersectionType.PassOverVertical, - IntersectionType.StartDown - ), - this - ); + return new ( + p, + GetTypeByLength ( + IntersectionType.StartUp, + IntersectionType.PassOverVertical, + IntersectionType.StartDown + ), + this + ); } if (EndsAt (x, y)) { - return new IntersectionDefinition (p, - Length < 0 ? IntersectionType.StartDown : IntersectionType.StartUp, - this - ); + return new ( + p, + Length < 0 ? IntersectionType.StartDown : IntersectionType.StartUp, + this + ); } int ymin = Math.Min (Start.Y, Start.Y + Length); @@ -181,10 +187,11 @@ IntersectionType typeWhenPositive if (ymin < y && ymax > y) { - return new IntersectionDefinition (p, - IntersectionType.PassOverVertical, - this - ); + return new ( + p, + IntersectionType.PassOverVertical, + this + ); } return null; diff --git a/UICatalog/Scenarios/TextEffectsScenario.cs b/UICatalog/Scenarios/TextEffectsScenario.cs index b34e77321b..4c4f5d7cbb 100644 --- a/UICatalog/Scenarios/TextEffectsScenario.cs +++ b/UICatalog/Scenarios/TextEffectsScenario.cs @@ -1,24 +1,25 @@ -using System; -using System.Collections.Generic; -using System.Text; -using System.Threading; +using System.Collections.Generic; using Terminal.Gui; - using Terminal.Gui.Drawing; namespace UICatalog.Scenarios; [ScenarioMetadata ("Text Effects", "Text Effects.")] [ScenarioCategory ("Colors")] +[ScenarioCategory ("Text and Formatting")] public class TextEffectsScenario : Scenario { private TabView _tabView; - public static bool LoopingGradient = false; + /// + /// Enable or disable looping of the gradient colors. + /// + public static bool LoopingGradient; public override void Main () { Application.Init (); + var w = new Window { Width = Dim.Fill (), @@ -26,57 +27,56 @@ public override void Main () Title = "Text Effects Scenario" }; - w.Loaded += (s, e) => - { - SetupGradientLineCanvas (w, w.Frame.Size); - }; - w.SizeChanging += (s, e) => - { - if (e.Size.HasValue) - { - SetupGradientLineCanvas (w, e.Size.Value); - } - }; + w.Loaded += (s, e) => { SetupGradientLineCanvas (w, w.Frame.Size); }; - w.ColorScheme = new ColorScheme + w.SizeChanging += (s, e) => + { + if (e.Size.HasValue) + { + SetupGradientLineCanvas (w, e.Size.Value); + } + }; + + w.ColorScheme = new () { - Normal = new Terminal.Gui.Attribute (ColorName.White, ColorName.Black), - Focus = new Terminal.Gui.Attribute (ColorName.Black, ColorName.White), - HotNormal = new Terminal.Gui.Attribute (ColorName.White, ColorName.Black), - HotFocus = new Terminal.Gui.Attribute (ColorName.White, ColorName.Black), - Disabled = new Terminal.Gui.Attribute (ColorName.Gray, ColorName.Black) + Normal = new (ColorName.White, ColorName.Black), + Focus = new (ColorName.Black, ColorName.White), + HotNormal = new (ColorName.White, ColorName.Black), + HotFocus = new (ColorName.White, ColorName.Black), + Disabled = new (ColorName.Gray, ColorName.Black) }; // Creates a window that occupies the entire terminal with a title. - _tabView = new TabView () + _tabView = new () { Width = Dim.Fill (), - Height = Dim.Fill (), + Height = Dim.Fill () }; - var gradientsView = new GradientsView () + var gradientsView = new GradientsView { Width = Dim.Fill (), - Height = Dim.Fill (), + Height = Dim.Fill () }; - var t1 = new Tab () + + var t1 = new Tab { View = gradientsView, DisplayText = "Gradients" }; - - var cbLooping = new CheckBox () + var cbLooping = new CheckBox { Text = "Looping", Y = Pos.AnchorEnd (1) }; + cbLooping.Toggle += (s, e) => - { - LoopingGradient = e.NewValue == CheckState.Checked; - SetupGradientLineCanvas (w, w.Frame.Size); - _tabView.SetNeedsDisplay (); - }; + { + LoopingGradient = e.NewValue == CheckState.Checked; + SetupGradientLineCanvas (w, w.Frame.Size); + _tabView.SetNeedsDisplay (); + }; gradientsView.Add (cbLooping); @@ -88,50 +88,50 @@ public override void Main () w.Dispose (); Application.Shutdown (); - this.Dispose (); + Dispose (); } - - private void SetupGradientLineCanvas (View w, Size size) + private static void SetupGradientLineCanvas (View w, Size size) { - GetAppealingGradientColors (out var stops, out var steps); + GetAppealingGradientColors (out List stops, out List steps); var g = new Gradient (stops, steps, LoopingGradient); var fore = new GradientFill ( - new Rectangle (0, 0, size.Width, size.Height), g, GradientDirection.Diagonal); - var back = new SolidFill (new Terminal.Gui.Color (ColorName.Black)); - - w.LineCanvas.Fill = new FillPair ( - fore, - back); + new (0, 0, size.Width, size.Height), + g, + GradientDirection.Diagonal); + var back = new SolidFill (new (ColorName.Black)); + + w.LineCanvas.Fill = new ( + fore, + back); } public static void GetAppealingGradientColors (out List stops, out List steps) { // Define the colors of the gradient stops with more appealing colors - stops = new List - { - new Color(0, 128, 255), // Bright Blue - new Color(0, 255, 128), // Bright Green - new Color(255, 255, 0), // Bright Yellow - new Color(255, 128, 0), // Bright Orange - new Color(255, 0, 128) // Bright Pink - }; + stops = + [ + new (0, 128, 255), // Bright Blue + new (0, 255, 128), // Bright Green + new (255, 255), // Bright Yellow + new (255, 128), // Bright Orange + new (255, 0, 128) + ]; // Define the number of steps between each color for smoother transitions // If we pass only a single value then it will assume equal steps between all pairs - steps = new List { 15 }; + steps = [15]; } } - internal class GradientsView : View { - private const int GradientWidth = 30; - private const int GradientHeight = 15; - private const int LabelHeight = 1; - private const int GradientWithLabelHeight = GradientHeight + LabelHeight + 1; // +1 for spacing + private const int GRADIENT_WIDTH = 30; + private const int GRADIENT_HEIGHT = 15; + private const int LABEL_HEIGHT = 1; + private const int GRADIENT_WITH_LABEL_HEIGHT = GRADIENT_HEIGHT + LABEL_HEIGHT + 1; // +1 for spacing public override void OnDrawContent (Rectangle viewport) { @@ -139,10 +139,10 @@ public override void OnDrawContent (Rectangle viewport) DrawTopLineGradient (viewport); - int x = 2; - int y = 3; + var x = 2; + var y = 3; - var gradients = new List<(string Label, GradientDirection Direction)> + List<(string Label, GradientDirection Direction)> gradients = new () { ("Horizontal", GradientDirection.Horizontal), ("Vertical", GradientDirection.Vertical), @@ -150,74 +150,74 @@ public override void OnDrawContent (Rectangle viewport) ("Diagonal", GradientDirection.Diagonal) }; - foreach (var (label, direction) in gradients) + foreach ((string label, GradientDirection direction) in gradients) { - if (x + GradientWidth > viewport.Width) + if (x + GRADIENT_WIDTH > viewport.Width) { x = 2; // Reset to left margin - y += GradientWithLabelHeight; // Move down to next row + y += GRADIENT_WITH_LABEL_HEIGHT; // Move down to next row } DrawLabeledGradientArea (label, direction, x, y); - x += GradientWidth + 2; // Move right for next gradient, +2 for spacing + x += GRADIENT_WIDTH + 2; // Move right for next gradient, +2 for spacing } } private void DrawLabeledGradientArea (string label, GradientDirection direction, int xOffset, int yOffset) { DrawGradientArea (direction, xOffset, yOffset); - CenterText (label, xOffset, yOffset + GradientHeight); // Adjusted for text below the gradient + CenterText (label, xOffset, yOffset + GRADIENT_HEIGHT); // Adjusted for text below the gradient } private void CenterText (string text, int xOffset, int yOffset) { - if(yOffset+1 >= Viewport.Height) + if (yOffset + 1 >= Viewport.Height) { // Not enough space for label return; } - var width = text.Length; - var x = xOffset + (GradientWidth - width) / 2; // Center the text within the gradient area width + int width = text.Length; + int x = xOffset + (GRADIENT_WIDTH - width) / 2; // Center the text within the gradient area width Driver.SetAttribute (GetNormalColor ()); - Move (x, yOffset+1); + Move (x, yOffset + 1); Driver.AddStr (text); } private void DrawGradientArea (GradientDirection direction, int xOffset, int yOffset) { // Define the colors of the gradient stops - var stops = new List - { - new Color(255, 0, 0), // Red - new Color(0, 255, 0), // Green - new Color(238, 130, 238) // Violet - }; + List stops = + [ + new (255, 0), // Red + new (0, 255), // Green + new (238, 130, 238) + ]; // Define the number of steps between each color - var steps = new List { 10, 10 }; // 10 steps between Red -> Green, and Green -> Blue + List steps = [10, 10]; // 10 steps between Red -> Green, and Green -> Blue // Create the gradient - var radialGradient = new Gradient (stops, steps, loop: TextEffectsScenario.LoopingGradient); + var radialGradient = new Gradient (stops, steps, TextEffectsScenario.LoopingGradient); // Define the size of the rectangle - int maxRow = GradientHeight; // Adjusted to keep aspect ratio - int maxColumn = GradientWidth; + int maxRow = GRADIENT_HEIGHT; // Adjusted to keep aspect ratio + int maxColumn = GRADIENT_WIDTH; // Build the coordinate-color mapping for a radial gradient - var gradientMapping = radialGradient.BuildCoordinateColorMapping (maxRow, maxColumn, direction); + Dictionary gradientMapping = radialGradient.BuildCoordinateColorMapping (maxRow, maxColumn, direction); // Print the gradient - for (int row = 0; row <= maxRow; row++) + for (var row = 0; row <= maxRow; row++) { - for (int col = 0; col <= maxColumn; col++) + for (var col = 0; col <= maxColumn; col++) { var coord = new Point (col, row); - var color = gradientMapping [coord]; + Color color = gradientMapping [coord]; SetColor (color); - AddRune (col + xOffset, row + yOffset, new Rune ('█')); + AddRune (col + xOffset, row + yOffset, new ('█')); } } } @@ -225,44 +225,41 @@ private void DrawGradientArea (GradientDirection direction, int xOffset, int yOf private void DrawTopLineGradient (Rectangle viewport) { // Define the colors of the rainbow - var stops = new List - { - new Color(255, 0, 0), // Red - new Color(255, 165, 0), // Orange - new Color(255, 255, 0), // Yellow - new Color(0, 128, 0), // Green - new Color(0, 0, 255), // Blue - new Color(75, 0, 130), // Indigo - new Color(238, 130, 238) // Violet - }; + List stops = + [ + new (255, 0), // Red + new (255, 165), // Orange + new (255, 255), // Yellow + new (0, 128), // Green + new (0, 0, 255), // Blue + new (75, 0, 130), // Indigo + new (238, 130, 238) + ]; // Define the number of steps between each color - var steps = new List - { + List steps = + [ 20, // between Red and Orange 20, // between Orange and Yellow 20, // between Yellow and Green 20, // between Green and Blue 20, // between Blue and Indigo - 20 // between Indigo and Violet - }; + 20 + ]; // Create the gradient var rainbowGradient = new Gradient (stops, steps, TextEffectsScenario.LoopingGradient); - for (int x = 0; x < viewport.Width; x++) + for (var x = 0; x < viewport.Width; x++) { double fraction = (double)x / (viewport.Width - 1); Color color = rainbowGradient.GetColorAtFraction (fraction); SetColor (color); - AddRune (x, 0, new Rune ('█')); + AddRune (x, 0, new ('█')); } } - private void SetColor (Color color) - { - Application.Driver.SetAttribute (new Attribute (color, color)); - } + private static void SetColor (Color color) { Application.Driver.SetAttribute (new (color, color)); } } diff --git a/UnitTests/Drawing/FillPairTests.cs b/UnitTests/Drawing/FillPairTests.cs index bbe8b6563e..067053e6e1 100644 --- a/UnitTests/Drawing/FillPairTests.cs +++ b/UnitTests/Drawing/FillPairTests.cs @@ -1,15 +1,9 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using Terminal.Gui.Drawing; +using Terminal.Gui.Drawing; namespace Terminal.Gui.DrawingTests; public class FillPairTests { - [Fact] public void GetAttribute_ReturnsCorrectColors () { @@ -22,11 +16,10 @@ public void GetAttribute_ReturnsCorrectColors () var fillPair = new FillPair (foregroundFill, backgroundFill); // Act - var resultAttribute = fillPair.GetAttribute (new Point (0, 0)); + Attribute resultAttribute = fillPair.GetAttribute (new (0, 0)); // Assert Assert.Equal (foregroundColor, resultAttribute.Foreground); Assert.Equal (backgroundColor, resultAttribute.Background); } } - diff --git a/UnitTests/Drawing/GradientFillTests.cs b/UnitTests/Drawing/GradientFillTests.cs index e1f035d8db..75bc65bbc8 100644 --- a/UnitTests/Drawing/GradientFillTests.cs +++ b/UnitTests/Drawing/GradientFillTests.cs @@ -2,21 +2,21 @@ public class GradientFillTests { - private Gradient _gradient; + private readonly Gradient _gradient; public GradientFillTests () { // Define the colors of the gradient stops - var stops = new List + List stops = new List { - new Color(255, 0, 0), // Red - new Color(0, 0, 255) // Blue + new (255, 0), // Red + new (0, 0, 255) // Blue }; // Define the number of steps between each color - var steps = new List { 10 }; // 10 steps between Red -> Blue + List steps = new() { 10 }; // 10 steps between Red -> Blue - _gradient = new Gradient (stops, steps, loop: false); + _gradient = new (stops, steps); } [Fact] @@ -31,13 +31,13 @@ public void TestGradientFillCorners_AtOrigin () var bottomLeft = new Point (0, area.Height - 1); var bottomRight = new Point (area.Width - 1, area.Height - 1); - var topLeftColor = gradientFill.GetColor (topLeft); - var topRightColor = gradientFill.GetColor (topRight); - var bottomLeftColor = gradientFill.GetColor (bottomLeft); - var bottomRightColor = gradientFill.GetColor (bottomRight); + Color topLeftColor = gradientFill.GetColor (topLeft); + Color topRightColor = gradientFill.GetColor (topRight); + Color bottomLeftColor = gradientFill.GetColor (bottomLeft); + Color bottomRightColor = gradientFill.GetColor (bottomRight); // Expected colors - var expectedTopLeftColor = new Color (255, 0, 0); // Red + var expectedTopLeftColor = new Color (255, 0); // Red var expectedBottomRightColor = new Color (0, 0, 255); // Blue Assert.Equal (expectedTopLeftColor, topLeftColor); @@ -56,13 +56,13 @@ public void TestGradientFillCorners_NotAtOrigin () var bottomLeft = new Point (5, area.Bottom - 1); var bottomRight = new Point (area.Right - 1, area.Bottom - 1); - var topLeftColor = gradientFill.GetColor (topLeft); - var topRightColor = gradientFill.GetColor (topRight); - var bottomLeftColor = gradientFill.GetColor (bottomLeft); - var bottomRightColor = gradientFill.GetColor (bottomRight); + Color topLeftColor = gradientFill.GetColor (topLeft); + Color topRightColor = gradientFill.GetColor (topRight); + Color bottomLeftColor = gradientFill.GetColor (bottomLeft); + Color bottomRightColor = gradientFill.GetColor (bottomRight); // Expected colors - var expectedTopLeftColor = new Color (255, 0, 0); // Red + var expectedTopLeftColor = new Color (255, 0); // Red var expectedBottomRightColor = new Color (0, 0, 255); // Blue Assert.Equal (expectedTopLeftColor, topLeftColor); @@ -75,15 +75,15 @@ public void TestGradientFillColorTransition () var area = new Rectangle (0, 0, 10, 10); var gradientFill = new GradientFill (area, _gradient, GradientDirection.Diagonal); - for (int row = 0; row < area.Height; row++) + for (var row = 0; row < area.Height; row++) { - int previousRed = 255; - int previousBlue = 0; + var previousRed = 255; + var previousBlue = 0; - for (int col = 0; col < area.Width; col++) + for (var col = 0; col < area.Width; col++) { var point = new Point (col, row); - var color = gradientFill.GetColor (point); + Color color = gradientFill.GetColor (point); // Check if the current color is 'more blue' and 'less red' as it goes right and down Assert.True (color.R <= previousRed, $"Failed at ({col}, {row}): {color.R} > {previousRed}"); @@ -95,15 +95,15 @@ public void TestGradientFillColorTransition () } } - for (int col = 0; col < area.Width; col++) + for (var col = 0; col < area.Width; col++) { - int previousRed = 255; - int previousBlue = 0; + var previousRed = 255; + var previousBlue = 0; - for (int row = 0; row < area.Height; row++) + for (var row = 0; row < area.Height; row++) { var point = new Point (col, row); - var color = gradientFill.GetColor (point); + Color color = gradientFill.GetColor (point); // Check if the current color is 'more blue' and 'less red' as it goes right and down Assert.True (color.R <= previousRed, $"Failed at ({col}, {row}): {color.R} > {previousRed}"); diff --git a/UnitTests/Drawing/GradientTests.cs b/UnitTests/Drawing/GradientTests.cs index 8fcfc13543..0174a7ff9a 100644 --- a/UnitTests/Drawing/GradientTests.cs +++ b/UnitTests/Drawing/GradientTests.cs @@ -1,5 +1,4 @@ - -namespace Terminal.Gui.DrawingTests; +namespace Terminal.Gui.DrawingTests; public class GradientTests { @@ -7,8 +6,8 @@ public class GradientTests public static IEnumerable GradientDirectionValues () { return typeof (GradientDirection).GetEnumValues () - .Cast () - .Select (direction => new object [] { direction }); + .Cast () + .Select (direction => new object [] { direction }); } [Theory] @@ -16,16 +15,16 @@ public static IEnumerable GradientDirectionValues () public void GradientIsInclusive_2_by_2 (GradientDirection direction) { // Define the colors of the gradient stops - var stops = new List - { - new Color(255, 0, 0), // Red - new Color(0, 0, 255) // Blue - }; + List stops = new() + { + new (255, 0), // Red + new (0, 0, 255) // Blue + }; // Define the number of steps between each color - var steps = new List { 10 }; // 10 steps between Red -> Blue + List steps = new() { 10 }; // 10 steps between Red -> Blue - var g = new Gradient (stops, steps, loop: false); + var g = new Gradient (stops, steps); Assert.Equal (4, g.BuildCoordinateColorMapping (1, 1, direction).Count); } @@ -34,77 +33,77 @@ public void GradientIsInclusive_2_by_2 (GradientDirection direction) public void GradientIsInclusive_1_by_1 (GradientDirection direction) { // Define the colors of the gradient stops - var stops = new List - { - new Color(255, 0, 0), // Red - new Color(0, 0, 255) // Blue - }; + List stops = new() + { + new (255, 0), // Red + new (0, 0, 255) // Blue + }; // Define the number of steps between each color - var steps = new List { 10 }; // 10 steps between Red -> Blue + List steps = new() { 10 }; // 10 steps between Red -> Blue - var g = new Gradient (stops, steps, loop: false); + var g = new Gradient (stops, steps); // Note that maxRow and maxCol are inclusive so this results in 1x1 area i.e. a single cell. - var c = Assert.Single (g.BuildCoordinateColorMapping (0, 0, direction)); - Assert.Equal (c.Key, new Point(0,0)); - Assert.Equal (c.Value, new Color (0, 0, 255)); + KeyValuePair c = Assert.Single (g.BuildCoordinateColorMapping (0, 0, direction)); + Assert.Equal (c.Key, new (0, 0)); + Assert.Equal (c.Value, new (0, 0, 255)); } [Fact] public void SingleColorStop () { - var stops = new List { new Color (255, 0, 0) }; // Red - var steps = new List { }; + List stops = new() { new (255, 0) }; // Red + List steps = new (); - var g = new Gradient (stops, steps, loop: false); - Assert.All (g.Spectrum, color => Assert.Equal (new Color (255, 0, 0), color)); + var g = new Gradient (stops, steps); + Assert.All (g.Spectrum, color => Assert.Equal (new (255, 0), color)); } [Fact] public void LoopingGradient_CorrectColors () { - var stops = new List - { - new Color(255, 0, 0), // Red - new Color(0, 0, 255) // Blue - }; + List stops = new() + { + new (255, 0), // Red + new (0, 0, 255) // Blue + }; - var steps = new List { 10 }; + List steps = new() { 10 }; - var g = new Gradient (stops, steps, loop: true); - Assert.Equal (new Color (255, 0, 0), g.Spectrum.First ()); - Assert.Equal (new Color (255, 0, 0), g.Spectrum.Last ()); + var g = new Gradient (stops, steps, true); + Assert.Equal (new (255, 0), g.Spectrum.First ()); + Assert.Equal (new (255, 0), g.Spectrum.Last ()); } [Fact] public void DifferentStepSizes () { - var stops = new List - { - new Color(255, 0, 0), // Red - new Color(0, 255, 0), // Green - new Color(0, 0, 255) // Blue - }; + List stops = new List + { + new (255, 0), // Red + new (0, 255), // Green + new (0, 0, 255) // Blue + }; - var steps = new List { 5, 15 }; // Different steps + List steps = new() { 5, 15 }; // Different steps - var g = new Gradient (stops, steps, loop: false); + var g = new Gradient (stops, steps); Assert.Equal (22, g.Spectrum.Count); } [Fact] public void FractionOutOfRange_ThrowsException () { - var stops = new List - { - new Color(255, 0, 0), // Red - new Color(0, 0, 255) // Blue - }; + List stops = new() + { + new (255, 0), // Red + new (0, 0, 255) // Blue + }; - var steps = new List { 10 }; + List steps = new() { 10 }; - var g = new Gradient (stops, steps, loop: false); + var g = new Gradient (stops, steps); Assert.Throws (() => g.GetColorAtFraction (-0.1)); Assert.Throws (() => g.GetColorAtFraction (1.1)); @@ -113,30 +112,30 @@ public void FractionOutOfRange_ThrowsException () [Fact] public void NaNFraction_ReturnsLastColor () { - var stops = new List - { - new Color(255, 0, 0), // Red - new Color(0, 0, 255) // Blue - }; + List stops = new() + { + new (255, 0), // Red + new (0, 0, 255) // Blue + }; - var steps = new List { 10 }; + List steps = new() { 10 }; - var g = new Gradient (stops, steps, loop: false); - Assert.Equal (new Color (0, 0, 255), g.GetColorAtFraction (double.NaN)); + var g = new Gradient (stops, steps); + Assert.Equal (new (0, 0, 255), g.GetColorAtFraction (double.NaN)); } [Fact] public void Constructor_SingleStepProvided_ReplicatesForAllPairs () { - var stops = new List - { - new Color(255, 0, 0), // Red - new Color(0, 255, 0), // Green - new Color(0, 0, 255) // Blue - }; + List stops = new List + { + new (255, 0), // Red + new (0, 255), // Green + new (0, 0, 255) // Blue + }; - var singleStep = new List { 5 }; // Single step provided - var gradient = new Gradient (stops, singleStep, loop: false); + List singleStep = new() { 5 }; // Single step provided + var gradient = new Gradient (stops, singleStep); Assert.NotNull (gradient.Spectrum); Assert.Equal (12, gradient.Spectrum.Count); // 5 steps Red -> Green + 5 steps Green -> Blue + 2 end colors @@ -145,31 +144,30 @@ public void Constructor_SingleStepProvided_ReplicatesForAllPairs () [Fact] public void Constructor_InvalidStepsLength_ThrowsArgumentException () { - var stops = new List - { - new Color(255, 0, 0), // Red - new Color(0, 0, 255) // Blue - }; - - var invalidSteps = new List { 5, 5 }; // Invalid length (N-1 expected) - Assert.Throws (() => new Gradient (stops, invalidSteps, loop: false)); + List stops = new() + { + new (255, 0), // Red + new (0, 0, 255) // Blue + }; + + List invalidSteps = new() { 5, 5 }; // Invalid length (N-1 expected) + Assert.Throws (() => new Gradient (stops, invalidSteps)); } [Fact] public void Constructor_ValidStepsLength_DoesNotThrow () { - var stops = new List - { - new Color(255, 0, 0), // Red - new Color(0, 255, 0), // Green - new Color(0, 0, 255) // Blue - }; + List stops = new List + { + new (255, 0), // Red + new (0, 255), // Green + new (0, 0, 255) // Blue + }; - var validSteps = new List { 5, 5 }; // Valid length (N-1) - var gradient = new Gradient (stops, validSteps, loop: false); + List validSteps = new() { 5, 5 }; // Valid length (N-1) + var gradient = new Gradient (stops, validSteps); Assert.NotNull (gradient.Spectrum); Assert.Equal (12, gradient.Spectrum.Count); // 5 steps Red -> Green + 5 steps Green -> Blue + 2 end colors } - -} \ No newline at end of file +} diff --git a/UnitTests/Drawing/LineCanvasTests.cs b/UnitTests/Drawing/LineCanvasTests.cs index d160b5420b..7d002770d9 100644 --- a/UnitTests/Drawing/LineCanvasTests.cs +++ b/UnitTests/Drawing/LineCanvasTests.cs @@ -4,7 +4,7 @@ namespace Terminal.Gui.DrawingTests; -public class LineCanvasTests (ITestOutputHelper output) +public class LineCanvasTests (ITestOutputHelper _output) { [Theory] @@ -295,7 +295,7 @@ string expected lc.AddLine (new (x1, y1), len1, o1, s1); lc.AddLine (new (x2, y2), len2, o2, s2); - TestHelpers.AssertEqual (output, expected, lc.ToString ()); + TestHelpers.AssertEqual (_output, expected, lc.ToString ()); v.Dispose (); } @@ -504,7 +504,7 @@ public void Viewport_Specific () Assert.Equal (new (x, y, 4, 2), lc.Viewport); TestHelpers.AssertEqual ( - output, + _output, @" ╔╡╞╗ ║ ║", @@ -554,7 +554,7 @@ public void Viewport_Specific_With_Ustring () Assert.Equal (new (x, y, 4, 2), lc.Viewport); TestHelpers.AssertEqual ( - output, + _output, @" ╔╡╞╗ ║ ║", @@ -597,7 +597,7 @@ public void Length_0_Is_1_Long (int x, int y, Orientation orientation, string ex // Add a line at 5, 5 that's has length of 1 canvas.AddLine (new (x, y), 1, orientation, LineStyle.Single); - TestHelpers.AssertEqual (output, $"{expected}", $"{canvas}"); + TestHelpers.AssertEqual (_output, $"{expected}", $"{canvas}"); } // X is offset by 2 @@ -654,7 +654,7 @@ public void Length_n_Is_n_Long (int x, int y, int length, Orientation orientatio canvas.AddLine (new (x, y), length, orientation, LineStyle.Single); var result = canvas.ToString (); - TestHelpers.AssertEqual (output, expected, result); + TestHelpers.AssertEqual (_output, expected, result); } [Fact] @@ -681,7 +681,7 @@ public void Length_Zero_Alone_Is_Line (Orientation orientation, string expected) // Add a line at 0, 0 that's has length of 0 lc.AddLine (Point.Empty, 0, orientation, LineStyle.Single); - TestHelpers.AssertEqual (output, expected, $"{lc}"); + TestHelpers.AssertEqual (_output, expected, $"{lc}"); } [InlineData (Orientation.Horizontal, "┼")] @@ -702,7 +702,7 @@ public void Length_Zero_Cross_Is_Cross (Orientation orientation, string expected // Add a line at 0, 0 that's has length of 0 lc.AddLine (Point.Empty, 0, orientation, LineStyle.Single); - TestHelpers.AssertEqual (output, expected, $"{lc}"); + TestHelpers.AssertEqual (_output, expected, $"{lc}"); } [InlineData (Orientation.Horizontal, "╥")] @@ -725,7 +725,7 @@ public void Length_Zero_NextTo_Opposite_Is_T (Orientation orientation, string ex // Add a line at 0, 0 that's has length of 0 lc.AddLine (Point.Empty, 0, orientation, LineStyle.Single); - TestHelpers.AssertEqual (output, expected, $"{lc}"); + TestHelpers.AssertEqual (_output, expected, $"{lc}"); } [Fact] @@ -741,7 +741,7 @@ public void TestLineCanvas_LeaveMargin_Top1_Left1 () @" ┌─ │ "; - TestHelpers.AssertEqual (output, looksLike, $"{Environment.NewLine}{canvas}"); + TestHelpers.AssertEqual (_output, looksLike, $"{Environment.NewLine}{canvas}"); } [Fact] @@ -768,7 +768,7 @@ public void TestLineCanvas_Window_Heavy () ┣━━━━╋━━━┫ ┃ ┃ ┃ ┗━━━━┻━━━┛"; - TestHelpers.AssertDriverContentsAre (looksLike, output); + TestHelpers.AssertDriverContentsAre (looksLike, _output); v.Dispose (); } @@ -799,7 +799,7 @@ public void TestLineCanvas_Window_HeavyTop_ThinSides (LineStyle thinStyle) │ │ │ ┕━━━━┷━━━┙ "; - TestHelpers.AssertDriverContentsAre (looksLike, output); + TestHelpers.AssertDriverContentsAre (looksLike, _output); v.Dispose (); } @@ -831,7 +831,7 @@ public void TestLineCanvas_Window_ThinTop_HeavySides (LineStyle thinStyle) ┖────┸───┚ "; - TestHelpers.AssertDriverContentsAre (looksLike, output); + TestHelpers.AssertDriverContentsAre (looksLike, _output); v.Dispose (); } @@ -849,7 +849,7 @@ public void Top_Left_From_TopRight_LeftUp () @" ┌─ │ "; - TestHelpers.AssertEqual (output, looksLike, $"{Environment.NewLine}{canvas}"); + TestHelpers.AssertEqual (_output, looksLike, $"{Environment.NewLine}{canvas}"); } [Fact] @@ -879,7 +879,7 @@ public void Top_With_1Down () Assert.Equal (2, map.Count); TestHelpers.AssertEqual ( - output, + _output, @" ─ ─", @@ -892,7 +892,7 @@ public void Top_With_1Down () public void ToString_Empty () { var lc = new LineCanvas (); - TestHelpers.AssertEqual (output, string.Empty, lc.ToString ()); + TestHelpers.AssertEqual (_output, string.Empty, lc.ToString ()); } // 012 @@ -911,7 +911,7 @@ public void ToString_Positive_Horizontal_1Line_Offset (int x, int y, string expe { var lc = new LineCanvas (); lc.AddLine (new (x, y), 3, Orientation.Horizontal, LineStyle.Double); - TestHelpers.AssertEqual (output, expected, $"{lc}"); + TestHelpers.AssertEqual (_output, expected, $"{lc}"); } [InlineData (0, 0, 0, 0, "═══")] @@ -936,7 +936,7 @@ public void ToString_Positive_Horizontal_2Line_Offset (int x1, int y1, int x2, i lc.AddLine (new (x1, y1), 3, Orientation.Horizontal, LineStyle.Double); lc.AddLine (new (x2, y2), 3, Orientation.Horizontal, LineStyle.Double); - TestHelpers.AssertEqual (output, expected, $"{lc}"); + TestHelpers.AssertEqual (_output, expected, $"{lc}"); } // [Fact, SetupFakeDriver] @@ -996,7 +996,7 @@ string expected v.Draw (); - TestHelpers.AssertDriverContentsAre (expected, output); + TestHelpers.AssertDriverContentsAre (expected, _output); v.Dispose (); } @@ -1015,7 +1015,7 @@ public void View_Draws_Corner_Correct () @" ┌─ │"; - TestHelpers.AssertDriverContentsAre (looksLike, output); + TestHelpers.AssertDriverContentsAre (looksLike, _output); v.Dispose (); } @@ -1038,7 +1038,7 @@ public void View_Draws_Corner_NoOverlap () ── │ │"; - TestHelpers.AssertDriverContentsAre (looksLike, output); + TestHelpers.AssertDriverContentsAre (looksLike, _output); v.Dispose (); } @@ -1056,7 +1056,7 @@ public void View_Draws_Horizontal (LineStyle style) var looksLike = @" ──"; - TestHelpers.AssertDriverContentsAre (looksLike, output); + TestHelpers.AssertDriverContentsAre (looksLike, _output); v.Dispose (); } @@ -1072,7 +1072,7 @@ public void View_Draws_Horizontal_Double () var looksLike = @" ══"; - TestHelpers.AssertDriverContentsAre (looksLike, output); + TestHelpers.AssertDriverContentsAre (looksLike, _output); v.Dispose (); } @@ -1091,7 +1091,7 @@ public void View_Draws_Vertical (LineStyle style) @" │ │"; - TestHelpers.AssertDriverContentsAre (looksLike, output); + TestHelpers.AssertDriverContentsAre (looksLike, _output); v.Dispose (); } @@ -1108,7 +1108,7 @@ public void View_Draws_Vertical_Double () @" ║ ║"; - TestHelpers.AssertDriverContentsAre (looksLike, output); + TestHelpers.AssertDriverContentsAre (looksLike, _output); v.Dispose (); } @@ -1136,7 +1136,7 @@ public void View_Draws_Window_Double () ╠════╬═══╣ ║ ║ ║ ╚════╩═══╝"; - TestHelpers.AssertDriverContentsAre (looksLike, output); + TestHelpers.AssertDriverContentsAre (looksLike, _output); v.Dispose (); } @@ -1167,7 +1167,7 @@ public void View_Draws_Window_DoubleTop_SingleSides (LineStyle thinStyle) │ │ │ ╘════╧═══╛ "; - TestHelpers.AssertDriverContentsAre (looksLike, output); + TestHelpers.AssertDriverContentsAre (looksLike, _output); v.Dispose (); } @@ -1204,7 +1204,7 @@ public void View_Draws_Window_Rounded () ├────┼───┤ │ │ │ ╰────┴───╯"; - TestHelpers.AssertDriverContentsAre (looksLike, output); + TestHelpers.AssertDriverContentsAre (looksLike, _output); v.Dispose (); } @@ -1236,7 +1236,7 @@ public void View_Draws_Window_SingleTop_DoubleSides (LineStyle thinStyle) ╙────╨───╜ "; - TestHelpers.AssertDriverContentsAre (looksLike, output); + TestHelpers.AssertDriverContentsAre (looksLike, _output); v.Dispose (); } @@ -1263,7 +1263,7 @@ public void Window () ├────┼───┤ │ │ │ └────┴───┘"; - TestHelpers.AssertEqual (output, looksLike, $"{Environment.NewLine}{canvas}"); + TestHelpers.AssertEqual (_output, looksLike, $"{Environment.NewLine}{canvas}"); } [Fact] @@ -1301,15 +1301,15 @@ public void Zero_Length_Intersections () var looksLike = @" ╔╡╞══╗ ║ ║"; - TestHelpers.AssertEqual (output, looksLike, $"{Environment.NewLine}{lc}"); + TestHelpers.AssertEqual (_output, looksLike, $"{Environment.NewLine}{lc}"); } [Fact] public void LineCanvas_UsesFillCorrectly () { // Arrange - var foregroundColor = new Color (255, 0, 0); // Red - var backgroundColor = new Color (0, 0, 0); // Black + var foregroundColor = new Color (255, 0); // Red + var backgroundColor = new Color (0, 0); // Black var foregroundFill = new SolidFill (foregroundColor); var backgroundFill = new SolidFill (backgroundColor); var fillPair = new FillPair (foregroundFill, backgroundFill); @@ -1320,11 +1320,11 @@ public void LineCanvas_UsesFillCorrectly () }; // Act - lineCanvas.AddLine (new Point (0, 0), 5, Orientation.Horizontal, LineStyle.Single); - var cellMap = lineCanvas.GetCellMap (); + lineCanvas.AddLine (new (0, 0), 5, Orientation.Horizontal, LineStyle.Single); + Dictionary cellMap = lineCanvas.GetCellMap (); // Assert - foreach (var cell in cellMap.Values) + foreach (Cell? cell in cellMap.Values) { Assert.NotNull (cell); Assert.Equal (foregroundColor, cell.Value.Attribute.Value.Foreground); @@ -1336,9 +1336,9 @@ public void LineCanvas_UsesFillCorrectly () public void LineCanvas_LineColorIgnoredBecauseOfFill () { // Arrange - var foregroundColor = new Color (255, 0, 0); // Red - var backgroundColor = new Color (0, 0, 0); // Black - var lineColor = new Attribute (new Color (0, 255, 0), new Color (255, 255, 255)); // Green on White + var foregroundColor = new Color (255, 0); // Red + var backgroundColor = new Color (0, 0); // Black + var lineColor = new Attribute (new Color (0, 255), new Color (255, 255, 255)); // Green on White var foregroundFill = new SolidFill (foregroundColor); var backgroundFill = new SolidFill (backgroundColor); var fillPair = new FillPair (foregroundFill, backgroundFill); @@ -1349,11 +1349,11 @@ public void LineCanvas_LineColorIgnoredBecauseOfFill () }; // Act - lineCanvas.AddLine (new Point (0, 0), 5, Orientation.Horizontal, LineStyle.Single, lineColor); - var cellMap = lineCanvas.GetCellMap (); + lineCanvas.AddLine (new (0, 0), 5, Orientation.Horizontal, LineStyle.Single, lineColor); + Dictionary cellMap = lineCanvas.GetCellMap (); // Assert - foreach (var cell in cellMap.Values) + foreach (Cell? cell in cellMap.Values) { Assert.NotNull (cell); Assert.Equal (foregroundColor, cell.Value.Attribute.Value.Foreground); @@ -1365,8 +1365,8 @@ public void LineCanvas_LineColorIgnoredBecauseOfFill () public void LineCanvas_IntersectingLinesUseFillCorrectly () { // Arrange - var foregroundColor = new Color (255, 0, 0); // Red - var backgroundColor = new Color (0, 0, 0); // Black + var foregroundColor = new Color (255, 0); // Red + var backgroundColor = new Color (0, 0); // Black var foregroundFill = new SolidFill (foregroundColor); var backgroundFill = new SolidFill (backgroundColor); var fillPair = new FillPair (foregroundFill, backgroundFill); @@ -1377,12 +1377,12 @@ public void LineCanvas_IntersectingLinesUseFillCorrectly () }; // Act - lineCanvas.AddLine (new Point (0, 0), 5, Orientation.Horizontal, LineStyle.Single); - lineCanvas.AddLine (new Point (2, -2), 5, Orientation.Vertical, LineStyle.Single); - var cellMap = lineCanvas.GetCellMap (); + lineCanvas.AddLine (new (0, 0), 5, Orientation.Horizontal, LineStyle.Single); + lineCanvas.AddLine (new (2, -2), 5, Orientation.Vertical, LineStyle.Single); + Dictionary cellMap = lineCanvas.GetCellMap (); // Assert - foreach (var cell in cellMap.Values) + foreach (Cell? cell in cellMap.Values) { Assert.NotNull (cell); Assert.Equal (foregroundColor, cell.Value.Attribute.Value.Foreground); diff --git a/UnitTests/Drawing/SolidFillTests.cs b/UnitTests/Drawing/SolidFillTests.cs index 50eaa9f9f4..3cf0168679 100644 --- a/UnitTests/Drawing/SolidFillTests.cs +++ b/UnitTests/Drawing/SolidFillTests.cs @@ -1,5 +1,4 @@ - -using Terminal.Gui.Drawing; +using Terminal.Gui.Drawing; namespace Terminal.Gui.DrawingTests; @@ -13,7 +12,7 @@ public void GetColor_ReturnsCorrectColor () var solidFill = new SolidFill (expectedColor); // Act - var resultColor = solidFill.GetColor (new Point (0, 0)); + Color resultColor = solidFill.GetColor (new (0, 0)); // Assert Assert.Equal (expectedColor, resultColor); @@ -32,9 +31,9 @@ public void GetColor_ReturnsSameColorForDifferentPoints (int x, int y) var solidFill = new SolidFill (expectedColor); // Act - var resultColor = solidFill.GetColor (new Point (x, y)); + Color resultColor = solidFill.GetColor (new (x, y)); // Assert Assert.Equal (expectedColor, resultColor); } -} \ No newline at end of file +} diff --git a/UnitTests/Drawing/StraightLineExtensionsTests.cs b/UnitTests/Drawing/StraightLineExtensionsTests.cs index b7a5f36b10..865ae805ac 100644 --- a/UnitTests/Drawing/StraightLineExtensionsTests.cs +++ b/UnitTests/Drawing/StraightLineExtensionsTests.cs @@ -2,11 +2,8 @@ namespace Terminal.Gui.DrawingTests; -public class StraightLineExtensionsTests +public class StraightLineExtensionsTests (ITestOutputHelper output) { - private readonly ITestOutputHelper _output; - public StraightLineExtensionsTests (ITestOutputHelper output) { _output = output; } - [Fact] [AutoInitShutdown] public void LineCanvasIntegrationTest () @@ -18,7 +15,7 @@ public void LineCanvasIntegrationTest () lc.AddLine (new Point (0, 4), -5, Orientation.Vertical, LineStyle.Single); TestHelpers.AssertEqual ( - _output, + output, @" ┌────────┐ │ │ @@ -32,7 +29,7 @@ public void LineCanvasIntegrationTest () lc = new LineCanvas (origLines.Exclude (Point.Empty, 10, Orientation.Horizontal)); TestHelpers.AssertEqual ( - _output, + output, @" │ │ │ │ @@ -44,7 +41,7 @@ public void LineCanvasIntegrationTest () lc = new LineCanvas (origLines.Exclude (new Point (0, 1), 10, Orientation.Horizontal)); TestHelpers.AssertEqual ( - _output, + output, @" ┌────────┐ @@ -57,7 +54,7 @@ public void LineCanvasIntegrationTest () lc = new LineCanvas (origLines.Exclude (new Point (0, 2), 10, Orientation.Horizontal)); TestHelpers.AssertEqual ( - _output, + output, @" ┌────────┐ │ │ @@ -70,7 +67,7 @@ public void LineCanvasIntegrationTest () lc = new LineCanvas (origLines.Exclude (new Point (0, 3), 10, Orientation.Horizontal)); TestHelpers.AssertEqual ( - _output, + output, @" ┌────────┐ │ │ @@ -83,7 +80,7 @@ public void LineCanvasIntegrationTest () lc = new LineCanvas (origLines.Exclude (new Point (0, 4), 10, Orientation.Horizontal)); TestHelpers.AssertEqual ( - _output, + output, @" ┌────────┐ │ │ @@ -95,7 +92,7 @@ public void LineCanvasIntegrationTest () lc = new LineCanvas (origLines.Exclude (Point.Empty, 10, Orientation.Vertical)); TestHelpers.AssertEqual ( - _output, + output, @" ────────┐ │ @@ -108,7 +105,7 @@ public void LineCanvasIntegrationTest () lc = new LineCanvas (origLines.Exclude (new Point (1, 0), 10, Orientation.Vertical)); TestHelpers.AssertEqual ( - _output, + output, @" ┌ ───────┐ │ │ @@ -121,7 +118,7 @@ public void LineCanvasIntegrationTest () lc = new LineCanvas (origLines.Exclude (new Point (8, 0), 10, Orientation.Vertical)); TestHelpers.AssertEqual ( - _output, + output, @" ┌─────── ┐ │ │ @@ -134,7 +131,7 @@ public void LineCanvasIntegrationTest () lc = new LineCanvas (origLines.Exclude (new Point (9, 0), 10, Orientation.Vertical)); TestHelpers.AssertEqual ( - _output, + output, @" ┌──────── │ diff --git a/UnitTests/Drawing/StraightLineTests.cs b/UnitTests/Drawing/StraightLineTests.cs index bb68708214..4395ea0c17 100644 --- a/UnitTests/Drawing/StraightLineTests.cs +++ b/UnitTests/Drawing/StraightLineTests.cs @@ -2,10 +2,9 @@ namespace Terminal.Gui.DrawingTests; -public class StraightLineTests +public class StraightLineTests (ITestOutputHelper output) { - private readonly ITestOutputHelper output; - public StraightLineTests (ITestOutputHelper output) { this.output = output; } + private readonly ITestOutputHelper _output = output; [InlineData ( Orientation.Horizontal, @@ -320,8 +319,8 @@ public void Viewport ( int expectedHeight ) { - var sl = new StraightLine (new Point (x, y), length, orientation, LineStyle.Single); + var sl = new StraightLine (new (x, y), length, orientation, LineStyle.Single); - Assert.Equal (new Rectangle (expectedX, expectedY, expectedWidth, expectedHeight), sl.Viewport); + Assert.Equal (new (expectedX, expectedY, expectedWidth, expectedHeight), sl.Viewport); } } From f770bb983a023c3c55a531c1446a15ae4af0ffd8 Mon Sep 17 00:00:00 2001 From: Tig Date: Tue, 9 Jul 2024 13:39:52 -0600 Subject: [PATCH 3/3] Code cleanup2 --- Terminal.Gui/View/Adornment/Border.cs | 2 -- 1 file changed, 2 deletions(-) diff --git a/Terminal.Gui/View/Adornment/Border.cs b/Terminal.Gui/View/Adornment/Border.cs index 1ed64abca0..2931cd9ad2 100644 --- a/Terminal.Gui/View/Adornment/Border.cs +++ b/Terminal.Gui/View/Adornment/Border.cs @@ -1,5 +1,3 @@ -using Terminal.Gui.Drawing; - namespace Terminal.Gui; /// The Border for a .