Browse Source

Line based Border POC

Tig 1 year ago
parent
commit
8f10195342

+ 1 - 1
Terminal.Gui/View/Adornment/Adornment.cs

@@ -86,7 +86,7 @@ public class Adornment : View
             Thickness prev = _thickness;
             _thickness = value;
 
-            if (prev != _thickness)
+            if (IsInitialized && prev != _thickness)
             {
                 Parent?.SetNeedsLayout ();
                 Parent?.LayoutSubviews ();

+ 68 - 46
Terminal.Gui/View/Adornment/Border.cs

@@ -49,11 +49,6 @@ public class Border : Adornment
     { /* Do nothing; A parameter-less constructor is required to support all views unit tests. */
     }
 
-    /// <summary>
-    ///    The close button for the border. Set to <see cref="Button.Visible"/>, to <see langword="true"/> to enable.
-    /// </summary>
-    public Button CloseButton { get; internal set; }
-
     /// <inheritdoc/>
     public Border (View parent) : base (parent)
     {
@@ -61,6 +56,13 @@ public class Border : Adornment
         Parent = parent;
     }
 
+    private Line _left;
+
+    /// <summary>
+    ///    The close button for the border. Set to <see cref="View.Visible"/>, to <see langword="true"/> to enable.
+    /// </summary>
+    public Button CloseButton { get; internal set; }
+
     /// <inheritdoc/>
     public override void BeginInit ()
     {
@@ -68,28 +70,44 @@ public class Border : Adornment
 
         if (Parent is { })
         {
+            // Left
+            _left = new ()
+            {
+                Orientation = Orientation.Vertical,
+            };
+            Add (_left);
+
             CloseButton = new Button ()
             {
                 Text = "X",
                 CanFocus = true,
                 Visible = false,
             };
-
-            CloseButton.LayoutStarted += (sender, args) =>
-                                         {
-                                             CloseButton.X = Pos.AnchorEnd (Thickness.Right / 2 + 1) - 
-                                                             (Pos.Right (CloseButton) - 
-                                                              Pos.Left (CloseButton))  ;
-
-                                             CloseButton.Y = 0;;//Thickness.Top / 2;
-                                         };
+            CloseButton.Accept += (s, e) =>
+            {
+                e.Cancel = Parent.InvokeCommand (Command.QuitToplevel) == true;
+            };
             Add (CloseButton);
-            CloseButton.Accept += (s, e) => {
-                                      e.Cancel = Parent.InvokeCommand (Command.QuitToplevel) == true;
-                                  };
+
+            LayoutStarted += OnLayoutStarted;
         }
     }
 
+    private void OnLayoutStarted (object sender, LayoutEventArgs e)
+    {
+        _left.Border.LineStyle = LineStyle;
+
+        _left.X = Thickness.Left - 1;
+        _left.Y = Thickness.Top - 1;
+        _left.Width = 1;
+        _left.Height = Height;
+
+        CloseButton.X = Pos.AnchorEnd (Thickness.Right / 2 + 1) -
+                        (Pos.Right (CloseButton) -
+                         Pos.Left (CloseButton));
+        CloseButton.Y = 0;
+    }
+
     /// <summary>
     ///     The color scheme for the Border. If set to <see langword="null"/>, gets the <see cref="Adornment.Parent"/>
     ///     scheme. color scheme.
@@ -112,6 +130,31 @@ public class Border : Adornment
         }
     }
 
+    Rectangle GetBorderBounds (Rectangle screenBounds)
+    {
+        return new (
+                                      screenBounds.X + Math.Max (0, Thickness.Left - 1),
+                                      screenBounds.Y + Math.Max (0, Thickness.Top - 1),
+                                      Math.Max (
+                                                0,
+                                                screenBounds.Width
+                                                - Math.Max (
+                                                            0,
+                                                            Math.Max (0, Thickness.Left - 1)
+                                                            + Math.Max (0, Thickness.Right - 1)
+                                                           )
+                                               ),
+                                      Math.Max (
+                                                0,
+                                                screenBounds.Height
+                                                - Math.Max (
+                                                            0,
+                                                            Math.Max (0, Thickness.Top - 1)
+                                                            + Math.Max (0, Thickness.Bottom - 1)
+                                                           )
+                                               )
+                                     );
+    }
     /// <summary>
     ///     Sets the style of the border by changing the <see cref="Thickness"/>. This is a helper API for setting the
     ///     <see cref="Thickness"/> to <c>(1,1,1,1)</c> and setting the line style of the views that comprise the border. If
@@ -155,29 +198,8 @@ public class Border : Adornment
         // For Border
         // ...thickness extends outward (border/title is always as far in as possible)
         // PERF: How about a call to Rectangle.Offset?
-        Rectangle borderBounds = new (
-                                      screenBounds.X + Math.Max (0, Thickness.Left - 1),
-                                      screenBounds.Y + Math.Max (0, Thickness.Top - 1),
-                                      Math.Max (
-                                                0,
-                                                screenBounds.Width
-                                                - Math.Max (
-                                                            0,
-                                                            Math.Max (0, Thickness.Left - 1)
-                                                            + Math.Max (0, Thickness.Right - 1)
-                                                           )
-                                               ),
-                                      Math.Max (
-                                                0,
-                                                screenBounds.Height
-                                                - Math.Max (
-                                                            0,
-                                                            Math.Max (0, Thickness.Top - 1)
-                                                            + Math.Max (0, Thickness.Bottom - 1)
-                                                           )
-                                               )
-                                     );
 
+        var borderBounds = GetBorderBounds (screenBounds);
         int topTitleLineY = borderBounds.Y;
         int titleY = borderBounds.Y;
         var titleBarsLength = 0; // the little vertical thingies
@@ -360,13 +382,13 @@ public class Border : Adornment
 
             if (drawLeft)
             {
-                lc.AddLine (
-                            new (borderBounds.Location.X, titleY),
-                            sideLineLength,
-                            Orientation.Vertical,
-                            LineStyle,
-                            Driver.GetAttribute ()
-                           );
+                //lc.AddLine (
+                //            new (borderBounds.Location.X, titleY),
+                //            sideLineLength,
+                //            Orientation.Vertical,
+                //            LineStyle,
+                //            Driver.GetAttribute ()
+                //           );
             }
 
             if (drawBottom)

+ 2 - 2
Terminal.Gui/View/Layout/ViewLayout.cs

@@ -509,7 +509,7 @@ public partial class View
     /// </remarks>
     public LineStyle BorderStyle
     {
-        get => Border.LineStyle;
+        get => Border?.LineStyle ?? LineStyle.Single;
         set
         {
             if (Border is null)
@@ -558,7 +558,7 @@ public partial class View
     /// <summary>Overriden by <see cref="Adornment"/> to do nothing, as the <see cref="Adornment"/> does not have adornments.</summary>
     internal virtual void LayoutAdornments ()
     {
-        if (Margin is null)
+        if (!IsInitialized || Margin is null)
         {
             return; // CreateAdornments () has not been called yet
         }

+ 12 - 2
Terminal.Gui/Views/Line.cs

@@ -4,7 +4,11 @@
 public class Line : View
 {
     /// <summary>Constructs a Line object.</summary>
-    public Line () { }
+    public Line ()
+    {
+        BorderStyle = LineStyle.Single;
+        Border.Thickness = new Thickness (0);
+    }
 
     /// <summary>
     ///     The direction of the line.  If you change this you will need to manually update the Width/Height of the
@@ -15,7 +19,13 @@ public class Line : View
     /// <inheritdoc/>
     public override void OnDrawContent (Rectangle contentArea)
     {
-        LineCanvas.AddLine (
+        LineCanvas lc = LineCanvas;
+
+        if (SuperView is Adornment adornment)
+        {
+            lc = adornment.Parent.LineCanvas;
+        }
+        lc.AddLine (
                     BoundsToScreen (contentArea).Location,
                     Orientation == Orientation.Horizontal ? Frame.Width : Frame.Height,
                     Orientation,