瀏覽代碼

Cell -> record struct

Tig 1 年之前
父節點
當前提交
04bbf6b9d1

+ 5 - 4
Terminal.Gui/Drawing/Cell.cs

@@ -4,18 +4,19 @@
 ///     Represents a single row/column in a Terminal.Gui rendering surface (e.g. <see cref="LineCanvas"/> and
 ///     <see cref="ConsoleDriver"/>).
 /// </summary>
-public class Cell
+public record struct Cell ()
 {
-    private Rune _rune;
 
     /// <summary>The attributes to use when drawing the Glyph.</summary>
-    public Attribute? Attribute { get; set; }
+    public Attribute? Attribute { get; set; } = null;
 
     /// <summary>
     ///     Gets or sets a value indicating whether this <see cref="T:Terminal.Gui.Cell"/> has been modified since the
     ///     last time it was drawn.
     /// </summary>
-    public bool IsDirty { get; set; }
+    public bool IsDirty { get; set; } = false;
+
+    private Rune _rune = default;
 
     /// <summary>The character to display. If <see cref="Rune"/> is <see langword="null"/>, then <see cref="Rune"/> is ignored.</summary>
     public Rune Rune

+ 2 - 2
Terminal.Gui/Drawing/LineCanvas.cs

@@ -165,9 +165,9 @@ public class LineCanvas : IDisposable
     ///     intersection symbols.
     /// </summary>
     /// <returns>A map of all the points within the canvas.</returns>
-    public Dictionary<Point, Cell> GetCellMap ()
+    public Dictionary<Point, Cell?> GetCellMap ()
     {
-        Dictionary<Point, Cell> map = new ();
+        Dictionary<Point, Cell?> map = new ();
 
         // walk through each pixel of the bitmap
         for (int y = Viewport.Y; y < Viewport.Y + Viewport.Height; y++)

+ 16 - 10
Terminal.Gui/View/ViewDrawing.cs

@@ -542,14 +542,17 @@ public partial class View
         // If we have a SuperView, it'll render our frames.
         if (!SuperViewRendersLineCanvas && LineCanvas.Viewport != Rectangle.Empty)
         {
-            foreach (KeyValuePair<Point, Cell> p in LineCanvas.GetCellMap ())
+            foreach (KeyValuePair<Point, Cell?> p in LineCanvas.GetCellMap ())
             {
                 // Get the entire map
-                Driver.SetAttribute (p.Value.Attribute ?? ColorScheme.Normal);
-                Driver.Move (p.Key.X, p.Key.Y);
+                if (p.Value is { })
+                {
+                    Driver.SetAttribute (p.Value.Value.Attribute ?? ColorScheme.Normal);
+                    Driver.Move (p.Key.X, p.Key.Y);
 
-                // TODO: #2616 - Support combining sequences that don't normalize
-                Driver.AddRune (p.Value.Rune);
+                    // TODO: #2616 - Support combining sequences that don't normalize
+                    Driver.AddRune (p.Value.Value.Rune);
+                }
             }
 
             LineCanvas.Clear ();
@@ -564,14 +567,17 @@ public partial class View
                 subview.LineCanvas.Clear ();
             }
 
-            foreach (KeyValuePair<Point, Cell> p in LineCanvas.GetCellMap ())
+            foreach (KeyValuePair<Point, Cell?> p in LineCanvas.GetCellMap ())
             {
                 // Get the entire map
-                Driver.SetAttribute (p.Value.Attribute ?? ColorScheme.Normal);
-                Driver.Move (p.Key.X, p.Key.Y);
+                if (p.Value is { })
+                {
+                    Driver.SetAttribute (p.Value.Value.Attribute ?? ColorScheme.Normal);
+                    Driver.Move (p.Key.X, p.Key.Y);
 
-                // TODO: #2616 - Support combining sequences that don't normalize
-                Driver.AddRune (p.Value.Rune);
+                    // TODO: #2616 - Support combining sequences that don't normalize
+                    Driver.AddRune (p.Value.Value.Rune);
+                }
             }
 
             LineCanvas.Clear ();

+ 7 - 4
UICatalog/Scenarios/LineDrawing.cs

@@ -42,12 +42,15 @@ public class LineDrawing : Scenario
 
             foreach (LineCanvas canvas in _layers)
             {
-                foreach (KeyValuePair<Point, Cell> c in canvas.GetCellMap ())
+                foreach (KeyValuePair<Point, Cell?> c in canvas.GetCellMap ())
                 {
-                    Driver.SetAttribute (c.Value.Attribute ?? ColorScheme.Normal);
+                    if (c.Value is { })
+                    {
+                        Driver.SetAttribute (c.Value.Value.Attribute ?? ColorScheme.Normal);
 
-                    // TODO: #2616 - Support combining sequences that don't normalize
-                    AddRune (c.Key.X, c.Key.Y, c.Value.Rune);
+                        // TODO: #2616 - Support combining sequences that don't normalize
+                        AddRune (c.Key.X, c.Key.Y, c.Value.Value.Rune);
+                    }
                 }
             }
         }

+ 28 - 14
UICatalog/Scenarios/Sliders.cs

@@ -174,7 +174,7 @@ public class Sliders : Scenario
             BorderStyle = LineStyle.Single
         };
 
-        optionsSlider.Style.SetChar.Attribute = new Attribute (Color.BrightGreen, Color.Black);
+        optionsSlider.Style.SetChar = optionsSlider.Style.SetChar with { Attribute = new Attribute (Color.BrightGreen, Color.Black) };
         optionsSlider.Style.LegendAttributes.SetAttribute = new Attribute (Color.Green, Color.Black);
 
         optionsSlider.Options = new List<SliderOption<string>>
@@ -440,10 +440,10 @@ public class Sliders : Scenario
 
         foreach (Slider s in app.Subviews.OfType<Slider> ())
         {
-            s.Style.OptionChar.Attribute = app.GetNormalColor ();
-            s.Style.SetChar.Attribute = app.GetNormalColor ();
+            s.Style.OptionChar = s.Style.OptionChar with { Attribute = app.GetNormalColor () };
+            s.Style.SetChar = s.Style.SetChar with { Attribute = app.GetNormalColor () };
             s.Style.LegendAttributes.SetAttribute = app.GetNormalColor ();
-            s.Style.RangeChar.Attribute = app.GetNormalColor ();
+            s.Style.RangeChar = s.Style.RangeChar with { Attribute = app.GetNormalColor () };
         }
 
         Slider<(Color, Color)> sliderFGColor = new ()
@@ -463,7 +463,7 @@ public class Sliders : Scenario
             UseMinimumSize = true
         };
 
-        sliderFGColor.Style.SetChar.Attribute = new Attribute (Color.BrightGreen, Color.Black);
+        sliderFGColor.Style.SetChar = sliderFGColor.Style.SetChar with { Attribute = new Attribute (Color.BrightGreen, Color.Black) };
         sliderFGColor.Style.LegendAttributes.SetAttribute = new Attribute (Color.Green, Color.Blue);
 
         List<SliderOption<(Color, Color)>> colorOptions = new ();
@@ -505,16 +505,30 @@ public class Sliders : Scenario
                                                                                )
                                                     };
 
-                                                    s.Style.OptionChar.Attribute = new Attribute (data.Item1, s.ColorScheme.Normal.Background);
+                                                    s.Style.OptionChar = s.Style.OptionChar with
+                                                    {
+                                                        Attribute = new Attribute (data.Item1, s.ColorScheme.Normal.Background)
+                                                    };
 
-                                                    s.Style.SetChar.Attribute = new Attribute (
-                                                                                               data.Item1,
-                                                                                               s.Style.SetChar.Attribute?.Background
-                                                                                               ?? s.ColorScheme.Normal.Background
-                                                                                              );
+                                                    s.Style.SetChar = s.Style.SetChar with
+                                                    {
+                                                        Attribute = new Attribute (
+                                                                                   data.Item1,
+                                                                                   s.Style.SetChar.Attribute?.Background
+                                                                                   ?? s.ColorScheme.Normal.Background
+                                                                                  )
+                                                    };
                                                     s.Style.LegendAttributes.SetAttribute = new Attribute (data.Item1, s.ColorScheme.Normal.Background);
-                                                    s.Style.RangeChar.Attribute = new Attribute (data.Item1, s.ColorScheme.Normal.Background);
-                                                    s.Style.SpaceChar.Attribute = new Attribute (data.Item1, s.ColorScheme.Normal.Background);
+
+                                                    s.Style.RangeChar = s.Style.RangeChar with
+                                                    {
+                                                        Attribute = new Attribute (data.Item1, s.ColorScheme.Normal.Background)
+                                                    };
+
+                                                    s.Style.SpaceChar = s.Style.SpaceChar with
+                                                    {
+                                                        Attribute = new Attribute (data.Item1, s.ColorScheme.Normal.Background)
+                                                    };
 
                                                     s.Style.LegendAttributes.NormalAttribute =
                                                         new Attribute (data.Item1, s.ColorScheme.Normal.Background);
@@ -536,7 +550,7 @@ public class Sliders : Scenario
             UseMinimumSize = true
         };
 
-        sliderBGColor.Style.SetChar.Attribute = new Attribute (Color.BrightGreen, Color.Black);
+        sliderBGColor.Style.SetChar = sliderBGColor.Style.SetChar with { Attribute = new Attribute (Color.BrightGreen, Color.Black) };
         sliderBGColor.Style.LegendAttributes.SetAttribute = new Attribute (Color.Green, Color.Blue);
 
         sliderBGColor.Options = colorOptions;