Browse Source

Padding button om Adornments scenario WORKS!

Tig Kindel 1 year ago
parent
commit
b3c3c2e90b

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

@@ -125,6 +125,32 @@ public class Adornment : View
         return new (new (parent.X + Frame.X, parent.Y + Frame.Y), Frame.Size);
     }
 
+    /// <inheritdoc/>
+    public override Point ScreenToFrame (int x, int y)
+    {
+            return Parent.ScreenToFrame (x - Frame.X, y - Frame.Y);
+    }
+
+    ///// <inheritdoc/>
+    //public override void SetNeedsDisplay (Rectangle region)
+    //{
+    //    SetSubViewNeedsDisplay ();
+    //    foreach (View subView in Subviews)
+    //    {
+    //        subView.SetNeedsDisplay ();
+    //    }
+    //}
+
+    /// <inheritdoc/>
+    //protected override void ClearNeedsDisplay ()
+    //{
+    //    base.ClearNeedsDisplay ();
+    //    foreach (View subView in Subviews)
+    //    {
+    //        subView.NeedsDisplay = false;
+    //    }
+    //}
+
     /// <summary>Does nothing for Adornment</summary>
     /// <returns></returns>
     public override bool OnDrawAdornments () { return false; }
@@ -156,7 +182,13 @@ public class Adornment : View
 
         TextFormatter?.Draw (screenBounds, normalAttr, normalAttr, Rectangle.Empty);
 
-        //base.OnDrawContent (contentArea);
+        if (Subviews.Count > 0)
+        {
+            base.OnDrawContent (contentArea);
+        }
+
+        ClearLayoutNeeded ();
+        ClearNeedsDisplay ();
     }
 
     /// <summary>Does nothing for Adornment</summary>

+ 9 - 9
Terminal.Gui/View/Adornment/Padding.cs

@@ -52,16 +52,16 @@ public class Padding : Adornment
     /// <returns><see langword="true"/>, if the event was handled, <see langword="false"/> otherwise.</returns>
     protected internal override bool OnMouseEvent (MouseEvent mouseEvent)
     {
-        if (mouseEvent.Flags.HasFlag (MouseFlags.Button1Clicked))
-        {
-            if (Parent.CanFocus && !Parent.HasFocus)
-            {
-                Parent.SetFocus ();
-                Parent.SetNeedsDisplay ();
-            }
+        //if (mouseEvent.Flags.HasFlag (MouseFlags.Button1Clicked))
+        //{
+        //    if (Parent.CanFocus && !Parent.HasFocus)
+        //    {
+        //        Parent.SetFocus ();
+        //        Parent.SetNeedsDisplay ();
+        //    }
 
-            return OnMouseClick (new MouseEventEventArgs (mouseEvent));
-        }
+        //    return OnMouseEvent(mouseEvent);
+        //}
 
         return false;
     }

+ 23 - 12
Terminal.Gui/View/Layout/ViewLayout.cs

@@ -534,9 +534,16 @@ public partial class View
 
         while (super is { })
         {
+            if (super is Adornment ador)
+            {
+                // TODO: Move this into Adornment somehow to remove coupling.
+                ador.BoundsToScreen (rx, ry, out rx, out ry);
+            }
+
             boundsOffset = super.GetBoundsOffset ();
             rx += super.Frame.X + boundsOffset.X;
             ry += super.Frame.Y + boundsOffset.Y;
+
             super = super.SuperView;
         }
 
@@ -596,14 +603,16 @@ public partial class View
             found = start.Padding;
         }
 
+        Point boundsOffset = start.GetBoundsOffset ();
+
         if (found is { })
         {
             start = found;
+            boundsOffset = found.Parent.Frame.Location;
         }
 
         if (start.InternalSubviews is { Count: > 0 })
         {
-            Point boundsOffset = start.GetBoundsOffset ();
             int rx = x - (start.Frame.X + boundsOffset.X);
             int ry = y - (start.Frame.Y + boundsOffset.Y);
 
@@ -759,18 +768,11 @@ public partial class View
     /// <returns>The coordinate relative to the <see cref="SuperView"/>'s <see cref="Bounds"/>.</returns>
     /// <param name="x">Screen-relative column.</param>
     /// <param name="y">Screen-relative row.</param>
-    public Point ScreenToFrame (int x, int y)
+    public virtual Point ScreenToFrame (int x, int y)
     {
         Point superViewBoundsOffset = SuperView?.GetBoundsOffset () ?? Point.Empty;
-        // BUGBUG: Hack. Move into Adornment somehow.
-        if (this is Adornment adornment)
-        {
-            superViewBoundsOffset = adornment.Parent.SuperView?.GetBoundsOffset () ?? Point.Empty;
-            return adornment.Parent.ScreenToFrame (x, y);
-        }
 
         var ret = new Point (x - Frame.X - superViewBoundsOffset.X, y - Frame.Y - superViewBoundsOffset.Y);
-
         if (SuperView is { })
         {
             Point superFrame = SuperView.ScreenToFrame (x - superViewBoundsOffset.X, y - superViewBoundsOffset.Y);
@@ -1040,7 +1042,10 @@ public partial class View
             Margin.Width = Frame.Size.Width;
             Margin.Height = Frame.Size.Height;
             Margin.SetNeedsLayout ();
-            Margin.SetNeedsDisplay ();
+            if (Margin.Subviews.Count > 0)
+            {
+                Margin.LayoutSubviews ();
+            }
         }
 
         Rectangle border = Margin.Thickness.GetInside (Margin.Frame);
@@ -1053,7 +1058,10 @@ public partial class View
             Border.Width = border.Size.Width;
             Border.Height = border.Size.Height;
             Border.SetNeedsLayout ();
-            Border.SetNeedsDisplay ();
+            if (Border.Subviews.Count > 0)
+            {
+                Border.LayoutSubviews ();
+            }
         }
 
         Rectangle padding = Border.Thickness.GetInside (Border.Frame);
@@ -1066,7 +1074,10 @@ public partial class View
             Padding.Width = padding.Size.Width;
             Padding.Height = padding.Size.Height;
             Padding.SetNeedsLayout ();
-            Padding.SetNeedsDisplay ();
+            if (Padding.Subviews.Count > 0)
+            {
+                Padding.LayoutSubviews ();
+            }
         }
     }
 

+ 6 - 0
Terminal.Gui/View/View.cs

@@ -459,6 +459,9 @@ public partial class View : Responder, ISupportInitializeNotification
 
         _oldCanFocus = CanFocus;
         _oldTabIndex = _tabIndex;
+        Margin?.BeginInit ();
+        Border?.BeginInit ();
+        Padding?.BeginInit ();
 
         if (_subviews?.Count > 0)
         {
@@ -487,6 +490,9 @@ public partial class View : Responder, ISupportInitializeNotification
         }
 
         IsInitialized = true;
+        Margin?.EndInit ();
+        Border?.EndInit ();
+        Padding?.EndInit ();
 
         // TODO: Move these into ViewText.cs as EndInit_Text() to consolodate.
         // TODO: Verify UpdateTextDirection really needs to be called here.

+ 6 - 18
Terminal.Gui/View/ViewDrawing.cs

@@ -510,12 +510,11 @@ public partial class View
     ///     redrawn will be the <paramref name="region"/>.
     /// </remarks>
     /// <param name="region">The Bounds-relative region that needs to be redrawn.</param>
-    public void SetNeedsDisplay (Rectangle region)
+    public virtual void SetNeedsDisplay (Rectangle region)
     {
         if (!IsInitialized)
         {
             _needsDisplayRect = region;
-
             return;
         }
 
@@ -534,22 +533,11 @@ public partial class View
 
         _superView?.SetSubViewNeedsDisplay ();
 
-        if (_needsDisplayRect.X < Bounds.X
-            || _needsDisplayRect.Y < Bounds.Y
-            || _needsDisplayRect.Width > Bounds.Width
-            || _needsDisplayRect.Height > Bounds.Height)
-        {
-            Margin?.SetNeedsDisplay (Margin.Bounds);
-            Border?.SetNeedsDisplay (Border.Bounds);
-            Padding?.SetNeedsDisplay (Padding.Bounds);
-        }
-
-        if (_subviews is null)
-        {
-            return;
-        }
+        Margin?.SetNeedsDisplay (Margin.Bounds);
+        Border?.SetNeedsDisplay (Border.Bounds);
+        Padding?.SetNeedsDisplay (Padding.Bounds);
 
-        foreach (View subview in _subviews)
+        foreach (View subview in Subviews)
         {
             if (subview.Frame.IntersectsWith (region))
             {
@@ -573,7 +561,7 @@ public partial class View
     }
 
     /// <summary>Clears <see cref="NeedsDisplay"/> and <see cref="SubViewNeedsDisplay"/>.</summary>
-    protected void ClearNeedsDisplay ()
+    protected virtual void ClearNeedsDisplay ()
     {
         _needsDisplayRect = Rectangle.Empty;
         SubViewNeedsDisplay = false;

+ 1 - 0
UICatalog/Scenarios/Adornments.cs

@@ -86,6 +86,7 @@ public class Adornments : Scenario
         editor.Initialized += (s, e) => { editor.ViewToEdit = view; };
 
         var btnButtonInPadding = new Button { X = Pos.Center(), Y = 0, Text = "Button in Padding" };
+        btnButtonInPadding.Accept += (s, e) => MessageBox.Query (20, 7, "Hi", "I'm in the padding", "Ok");
         view.Padding.Add (btnButtonInPadding);
 
         Application.Run (editor);