Renew the VisualElement.Opacity
authorKangho Hur <kangho.hur@samsung.com>
Tue, 28 Feb 2017 00:36:00 +0000 (09:36 +0900)
committerKangho Hur <kangho.hur@samsung.com>
Mon, 10 Jul 2017 02:11:19 +0000 (11:11 +0900)
- OpacityLayer is no more needed in renderer.
- There is a limitation on this chagne - Entry and Label are not support Opacity property anymore. It will be fixed soon when Elm# is support.

Change-Id: I52f0fc67fdfda65cf8141cceab877890d260a4e6

Xamarin.Forms.Platform.Tizen/Renderers/BoxViewRenderer.cs
Xamarin.Forms.Platform.Tizen/Renderers/VisualElementRenderer.cs
Xamarin.Forms.Platform.Tizen/Xamarin.Forms.Platform.Tizen.project.json

index 7a9f756..b065134 100644 (file)
@@ -4,11 +4,8 @@ using ERectangle = ElmSharp.Rectangle;
 
 namespace Xamarin.Forms.Platform.Tizen
 {
-       public class BoxViewRenderer :
-       VisualElementRenderer<BoxView>
+       public class BoxViewRenderer : VisualElementRenderer<BoxView>
        {
-               static readonly EColor s_defaultColor = EColor.Transparent;
-
                ERectangle _control;
 
                public BoxViewRenderer()
@@ -23,10 +20,6 @@ namespace Xamarin.Forms.Platform.Tizen
                                SetNativeControl(_control);
                        }
 
-                       if (e.OldElement != null)
-                       {
-                       }
-
                        if (e.NewElement != null)
                        {
                                UpdateColor();
@@ -35,27 +28,45 @@ namespace Xamarin.Forms.Platform.Tizen
                        base.OnElementChanged(e);
                }
 
-               void UpdateColor()
+               protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
                {
-                       Color colorToSet = Element.Color;
-
-                       if (colorToSet == Color.Default)
+                       if (e.PropertyName == BoxView.ColorProperty.PropertyName)
                        {
-                               colorToSet = Element.BackgroundColor;
+                               UpdateColor();
                        }
+                       base.OnElementPropertyChanged(sender, e);
+               }
 
-                       _control.Color = (colorToSet == Color.Default) ? s_defaultColor : colorToSet.ToNative();
+               protected override void UpdateBackgroundColor()
+               {
+                       UpdateColor();
                }
 
-               protected override void OnElementPropertyChanged(object sender,
-                       PropertyChangedEventArgs e)
+               protected override void UpdateOpacity()
+               {
+                       UpdateColor();
+               }
+
+               void UpdateColor()
                {
-                       if (e.PropertyName == BoxView.ColorProperty.PropertyName ||
-                               e.PropertyName == VisualElement.BackgroundColorProperty.PropertyName)
+                       if (Element.Color.IsDefault)
                        {
-                               UpdateColor();
+                               if (Element.BackgroundColor.IsDefault)
+                               {
+                                       // Set to default color. (Transparent)
+                                       _control.Color = EColor.Transparent;
+                               }
+                               else
+                               {
+                                       // Use BackgroundColor only if color is default and background color is not default.
+                                       _control.Color = Element.BackgroundColor.MultiplyAlpha(Element.Opacity).ToNative();
+                               }
+                       }
+                       else
+                       {
+                               // Color has higer priority than BackgroundColor.
+                               _control.Color = Element.Color.MultiplyAlpha(Element.Opacity).ToNative();
                        }
-                       base.OnElementPropertyChanged(sender, e);
                }
        }
 }
index 37a5faf..394ec02 100644 (file)
@@ -44,8 +44,6 @@ namespace Xamarin.Forms.Platform.Tizen
 
                HashSet<string> _batchedProperties = new HashSet<string>();
 
-               ERectangle _opacityLayer;
-
                internal GestureHandler _gestureHandler;
 
                /// <summary>
@@ -209,7 +207,6 @@ namespace Xamarin.Forms.Platform.Tizen
                        var y = ComputeAbsoluteY(Element);
                        NativeView.Geometry = new Rectangle(x, y, Element.Width, Element.Height).ToPixel();
                        ApplyTransformation();
-                       UpdateOpacityLayer();
                }
 
                void IVisualElementRenderer.SetElement(VisualElement element)
@@ -309,7 +306,6 @@ namespace Xamarin.Forms.Platform.Tizen
                                if (NativeView != null)
                                {
                                        NativeView.Deleted -= NativeViewDeleted;
-                                       EnsureOpacityLayerIsDestroyed();
                                        NativeView.Unrealize();
                                        SetNativeView(null);
                                }
@@ -425,7 +421,6 @@ namespace Xamarin.Forms.Platform.Tizen
                        {
                                NativeView.Moved -= OnMoved;
                                NativeView.Deleted -= NativeViewDeleted;
-                               EnsureOpacityLayerIsDestroyed();
                        }
 
                        Widget widget = NativeView as Widget;
@@ -584,6 +579,18 @@ namespace Xamarin.Forms.Platform.Tizen
                        }
                }
 
+               protected virtual void UpdateOpacity()
+               {
+                       if (NativeView is Widget)
+                       {
+                               (NativeView as Widget).Opacity = (int)(Element.Opacity * 255.0);
+                       }
+                       else
+                       {
+                               Log.Warn("{0} uses {1} which does not support opacity", this, NativeView);
+                       }
+               }
+
                static double ComputeAbsoluteX(VisualElement e)
                {
                        return e.X + (e.RealParent is VisualElement ? Forms.ConvertToScaledDP(Platform.GetRenderer(e.RealParent).NativeView.Geometry.X) : 0.0);
@@ -719,7 +726,6 @@ namespace Xamarin.Forms.Platform.Tizen
 
                void OnMoved(object sender, EventArgs e)
                {
-                       UpdateOpacityLayer();
                        ApplyTransformation();
                        _gestureHandler?.UpdateHitBox();
                }
@@ -752,30 +758,12 @@ namespace Xamarin.Forms.Platform.Tizen
                        }
                }
 
-               void UpdateOpacity()
-               {
-                       if (null != NativeView)
-                       {
-                               if (Element.Opacity < 1.0)
-                               {
-                                       EnsureOpacityLayerExists();
-
-                                       var alpha = (int)(Element.Opacity * 255.0);
-                                       _opacityLayer.Color = new EColor(255, 255, 255, alpha);
-                               }
-                               else
-                               {
-                                       EnsureOpacityLayerIsDestroyed();
-                               }
-                       }
-               }
-
                /// <summary>
                /// Updates the IsEnabled property.
                /// </summary>
                void UpdateIsEnabled()
                {
-                       Widget widget = NativeView as Widget;
+                       var widget = NativeView as Widget;
                        if (widget != null)
                        {
                                widget.IsEnabled = Element.IsEnabled;
@@ -874,48 +862,12 @@ namespace Xamarin.Forms.Platform.Tizen
                        ApplyTranslation(map, geometry, ref changed);
 
                        NativeView.IsMapEnabled = changed;
-
                        if (changed)
                        {
                                NativeView.EvasMap = map;
-
-                               if (_opacityLayer != null)
-                               {
-                                       _opacityLayer.IsMapEnabled = true;
-                                       _opacityLayer.EvasMap = map;
-                               }
                        }
                        _gestureHandler?.UpdateHitBox();
                }
-
-               protected virtual void UpdateOpacityLayer()
-               {
-                       if (_opacityLayer != null)
-                       {
-                               _opacityLayer.Geometry = NativeView.Geometry;
-                               NativeView.SetClip(_opacityLayer);
-                       }
-               }
-
-               void EnsureOpacityLayerExists()
-               {
-                       if (_opacityLayer == null)
-                       {
-                               _opacityLayer = new ERectangle(NativeView);
-                               UpdateOpacityLayer();
-                               _opacityLayer.Show();
-                       }
-               }
-
-               void EnsureOpacityLayerIsDestroyed()
-               {
-                       if (_opacityLayer != null)
-                       {
-                               NativeView.SetClip(null);
-                               _opacityLayer.Unrealize();
-                               _opacityLayer = null;
-                       }
-               }
        }
 
        internal static class Settings
index 8f6afe7..b77e8cb 100644 (file)
@@ -1,6 +1,6 @@
-{
+{
   "dependencies": {
-    "ElmSharp": "1.1.0-beta-010",
+    "ElmSharp": "1.1.0-beta-011",
     "NETStandard.Library": "1.6.0",
     "System.Runtime.Serialization.Xml": "4.1.1",
     "Tizen.Applications": "1.1.0",
@@ -11,4 +11,4 @@
       "imports": "portable-net45+win8+wpa81+wp8"
     }
   }
-}
+}
\ No newline at end of file