[GTK] Add Alpha support to Frame background (#6092)
authormtz <martin@videolabs.io>
Tue, 28 May 2019 17:55:05 +0000 (19:55 +0200)
committerSamantha Houts <samhouts@users.noreply.github.com>
Tue, 28 May 2019 17:55:05 +0000 (10:55 -0700)
* [GTK] Add transparent background support for Gtk.Frame

Fixes https://github.com/xamarin/Xamarin.Forms/issues/4700

* [GTK] Disable Frame shadow

fixes #4700

Xamarin.Forms.Platform.GTK/Controls/CustomFrame.cs
Xamarin.Forms.Platform.GTK/Extensions/ColorExtensions.cs
Xamarin.Forms.Platform.GTK/Renderers/FrameRenderer.cs

index 85f7d1b..df8ebe6 100644 (file)
@@ -6,10 +6,10 @@ namespace Xamarin.Forms.Platform.GTK.Controls
 {
        public class CustomFrame : Gtk.Frame
        {
-               private Gdk.Color _defaultBorderColor;
-               private Gdk.Color _defaultBackgroundColor;
-               private Gdk.Color? _borderColor;
-               private Gdk.Color? _backgroundColor;
+               private Color _defaultBorderColor;
+               private Color _defaultBackgroundColor;
+               private Color? _borderColor;
+               private Color? _backgroundColor;
 
                private uint _borderWidth;
                private bool _hasShadow;
@@ -23,11 +23,11 @@ namespace Xamarin.Forms.Platform.GTK.Controls
                        _borderWidth = 0;
                        _hasShadow = false;
                        _shadowWidth = 2;
-                       _defaultBackgroundColor = Style.Backgrounds[(int)StateType.Normal];
-                       _defaultBorderColor = Style.BaseColors[(int)StateType.Active];
+                       _defaultBackgroundColor = Style.Backgrounds[(int)StateType.Normal].ToXFColor();
+                       _defaultBorderColor = Style.BaseColors[(int)StateType.Active].ToXFColor();
                }
 
-               public void SetBackgroundColor(Gdk.Color? color)
+               public void SetBackgroundColor(Color? color)
                {
                        _backgroundColor = color;
                        QueueDraw();
@@ -45,7 +45,7 @@ namespace Xamarin.Forms.Platform.GTK.Controls
                        QueueDraw();
                }
 
-               public void SetBorderColor(Gdk.Color? color)
+               public void SetBorderColor(Color? color)
                {
                        _borderColor = color;
                        QueueDraw();
@@ -77,15 +77,13 @@ namespace Xamarin.Forms.Platform.GTK.Controls
 
                protected override bool OnExposeEvent(EventExpose evnt)
                {
-                       double colorMaxValue = 65535;
-
                        using (var cr = CairoHelper.Create(GdkWindow))
                        {
-                               //Draw Shadow
+                               // Draw Shadow
                                if (_hasShadow)
                                {
-                                       var color = Color.Black.ToGtkColor();
-                                       cr.SetSourceRGBA(color.Red / colorMaxValue, color.Green / colorMaxValue, color.Blue / colorMaxValue, 1.0);
+                                       var color = Color.Black;
+                                       cr.SetSourceRGBA(color.R, color.G, color.B, color.A);
                                        cr.Rectangle(Allocation.Left + _shadowWidth, Allocation.Top + _shadowWidth, Allocation.Width + _shadowWidth, Allocation.Height + _shadowWidth);
                                        cr.Fill();
                                }
@@ -94,7 +92,7 @@ namespace Xamarin.Forms.Platform.GTK.Controls
                                if (_backgroundColor.HasValue)
                                {
                                        var color = _backgroundColor.Value;
-                                       cr.SetSourceRGBA(color.Red / colorMaxValue, color.Green / colorMaxValue, color.Blue / colorMaxValue, 1.0);
+                                       cr.SetSourceRGBA(color.R, color.G, color.B, color.A);
                                        cr.Rectangle(Allocation.Left, Allocation.Top, Allocation.Width, Allocation.Height);
                                        cr.FillPreserve();
                                }
@@ -104,7 +102,7 @@ namespace Xamarin.Forms.Platform.GTK.Controls
                                {
                                        cr.LineWidth = _borderWidth;
                                        var color = _borderColor.Value;
-                                       cr.SetSourceRGB(color.Red / colorMaxValue, color.Green / colorMaxValue, color.Blue / colorMaxValue);
+                                       cr.SetSourceRGBA(color.R, color.G, color.B, color.A);
                                        cr.Rectangle(Allocation.Left, Allocation.Top, Allocation.Width, Allocation.Height);
                                        cr.StrokePreserve();
                                }
index 165ca49..eed8884 100644 (file)
                        return gtkColor;
                }
 
+               internal static Xamarin.Forms.Color ToXFColor(this Gdk.Color color, double opacity = 255)
+               {
+                       return new Color(color.Red, color.Green, color.Blue, opacity);
+               }
+
                internal static string ToRgbaColor(this Color color)
                {
                        int red = (int)(color.R * 255);
index be2854b..e87fe9d 100644 (file)
@@ -7,8 +7,6 @@ namespace Xamarin.Forms.Platform.GTK.Renderers
        {
                protected override void OnElementChanged(ElementChangedEventArgs<Frame> e)
                {
-                       base.OnElementChanged(e);
-
                        if (e.NewElement != null)
                        {
                                if (Control == null)
@@ -24,6 +22,8 @@ namespace Xamarin.Forms.Platform.GTK.Renderers
                                PackChild();
                                SetupLayer();
                        }
+
+                       base.OnElementChanged(e);
                }
 
                protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
@@ -45,17 +45,12 @@ namespace Xamarin.Forms.Platform.GTK.Renderers
                        if (Element.BackgroundColor == Color.Default)
                                Control.ResetBackgroundColor();
                        else
-                               Control.SetBackgroundColor(Element.BackgroundColor.ToGtkColor());
+                               Control.SetBackgroundColor(Element.BackgroundColor);
 
                        if (Element.BorderColor == Color.Default)
                                Control.ResetBorderColor();
                        else
-                               Control.SetBorderColor(Element.BorderColor.ToGtkColor());
-
-                       if (Element.HasShadow)
-                               Control.SetShadow();
-                       else
-                               Control.ResetShadow();
+                               Control.SetBorderColor(Element.BorderColor);
                }
 
                private void PackChild()