[Tizen] Load image synchronously (#140)
author윤정현/Common Platform Lab(SR)/Staff Engineer/삼성전자 <jh0506.yun@samsung.com>
Thu, 16 Jan 2020 02:07:23 +0000 (11:07 +0900)
committer부정균/Common Platform Lab(SR)/Staff Engineer/삼성전자 <jk.pu@samsung.com>
Thu, 16 Jan 2020 02:07:23 +0000 (11:07 +0900)
* [Tizen] Load image synchronously (#9025)

* [Tizen] Load image synchronously

Test/Alarm/Alarm/Controls/ImageButton.cs
Test/Alarm/Alarm/Renderers/ImageButtonRenderer.cs
Tizen.CircularUI/Tizen.Wearable.CircularUI.Forms.Renderer/CircleImageRenderer.cs
Xamarin.Forms/Xamarin.Forms.Core/PlatformConfiguration/TizenSpecific/Image.cs
Xamarin.Forms/Xamarin.Forms.Platform.Tizen/Extensions/ImageExtensions.cs
Xamarin.Forms/Xamarin.Forms.Platform.Tizen/Native/Image.cs
Xamarin.Forms/Xamarin.Forms.Platform.Tizen/Renderers/ImageButtonRenderer.cs
Xamarin.Forms/Xamarin.Forms.Platform.Tizen/Renderers/ImageRenderer.cs

index e698ee4..6f2d240 100644 (file)
@@ -26,7 +26,7 @@ namespace Alarm.Controls
     /// </summary>
     public class CustomImageButton : Image
     {
-        public static readonly BindableProperty BlendColorProperty = BindableProperty.Create("BlendColor", typeof(Color), typeof(CustomImageButton), Color.Default);
+        public static readonly BindableProperty PressedColorProperty = BindableProperty.Create("PressedColor", typeof(Color), typeof(CustomImageButton), Color.Default);
 
         public event EventHandler Clicked;
 
@@ -35,10 +35,10 @@ namespace Alarm.Controls
         /// <summary>
         /// A color when the button is pressed. 
         /// </summary>
-        public Color BlendColor
+        public Color PressedColor
         {
-            get { return (Color)GetValue(BlendColorProperty); }
-            set { SetValue(BlendColorProperty, value); }
+            get { return (Color)GetValue(PressedColorProperty); }
+            set { SetValue(PressedColorProperty, value); }
         }
 
         /// <summary>
@@ -62,7 +62,7 @@ namespace Alarm.Controls
         /// </summary>
         public CustomImageButton()
         {
-            BlendColor = Color.FromRgba(255, 255, 255, 100);
+            PressedColor = Color.FromRgba(255, 255, 255, 100);
         }
     }
 }
index b6d23d5..17df2d8 100644 (file)
@@ -47,7 +47,7 @@ namespace Alarm.Tizen.Renderers
 
         public CustomImageButtonRenderer()
         {
-            RegisterPropertyHandler(CustomImageButton.BlendColorProperty, UpdateBlendColor);
+            RegisterPropertyHandler(CustomImageButton.PressedColorProperty, UpdatePressedColor);
         }
 
         /// <summary>
@@ -140,14 +140,14 @@ namespace Alarm.Tizen.Renderers
                 return;
             }
 
-            Control.Color = BtnElement.BlendColor.ToNative();
+            Control.Color = BtnElement.PressedColor.ToNative();
             isTouched = true;
         }
 
-        private void UpdateBlendColor(bool obj)
+        private void UpdatePressedColor(bool obj)
         {
             CustomImageButton BtnElement = Element as CustomImageButton;
-            Control.Color = BtnElement.BlendColor.ToNative();
+            Control.Color = BtnElement.PressedColor.ToNative();
         }
 
         /// <summary>
index 0291cbe..9d8e527 100644 (file)
@@ -130,7 +130,7 @@ namespace UIComponents.Tizen.Wearable.Renderers
 
         void UpdateAspect()
         {
-            Control.Aspect = Element.Aspect;
+            Control.ApplyAspect(Element.Aspect);
         }
 
         void UpdateIsOpaque()
index 2c86fd0..15581ee 100644 (file)
@@ -6,6 +6,8 @@ namespace Xamarin.Forms.PlatformConfiguration.TizenSpecific
        {
                public static readonly BindableProperty BlendColorProperty = BindableProperty.Create("BlendColor", typeof(Color), typeof(FormsElement), Color.Default);
 
+               public static readonly BindableProperty FileProperty = BindableProperty.Create("File", typeof(string), typeof(FormsElement), default(string));
+
                public static Color GetBlendColor(BindableObject element)
                {
                        return (Color)element.GetValue(BlendColorProperty);
@@ -26,5 +28,26 @@ namespace Xamarin.Forms.PlatformConfiguration.TizenSpecific
                        SetBlendColor(config.Element, color);
                        return config;
                }
+
+               public static string GetFile(BindableObject element)
+               {
+                       return (string)element.GetValue(FileProperty);
+               }
+
+               public static void SetFile(BindableObject element, string file)
+               {
+                       element.SetValue(FileProperty, file);
+               }
+
+               public static string GetFile(this IPlatformElementConfiguration<Tizen, FormsElement> config)
+               {
+                       return GetFile(config.Element);
+               }
+
+               public static IPlatformElementConfiguration<Tizen, FormsElement> SetFile(this IPlatformElementConfiguration<Tizen, FormsElement> config, string file)
+               {
+                       SetFile(config.Element, file);
+                       return config;
+               }
        }
 }
index 7e49b75..1eab7ab 100644 (file)
@@ -1,8 +1,34 @@
+using EImage = ElmSharp.Image;
+
 namespace Xamarin.Forms.Platform.Tizen
 {
-       internal static class ImageExtensions
+       public static class ImageExtensions
        {
-               internal static bool IsNullOrEmpty(this ImageSource imageSource) =>
+               public static void ApplyAspect(this EImage image, Aspect aspect)
+               {
+                       Aspect _aspect = aspect;
+
+                       switch (_aspect)
+                       {
+                               case Aspect.AspectFit:
+                                       image.IsFixedAspect = true;
+                                       image.CanFillOutside = false;
+                                       break;
+                               case Aspect.AspectFill:
+                                       image.IsFixedAspect = true;
+                                       image.CanFillOutside = true;
+                                       break;
+                               case Aspect.Fill:
+                                       image.IsFixedAspect = false;
+                                       image.CanFillOutside = false;
+                                       break;
+                               default:
+                                       Log.Warn("Invalid Aspect value: {0}", _aspect);
+                                       break;
+                       }
+               }
+
+               public static bool IsNullOrEmpty(this ImageSource imageSource) =>
                        imageSource == null || imageSource.IsEmpty;
        }
 }
index bb50622..85c3398 100644 (file)
@@ -10,39 +10,12 @@ namespace Xamarin.Forms.Platform.Tizen.Native
        /// </summary>
        public class Image : EImage, IMeasurable
        {
-               Aspect _aspect;
-
                /// <summary>
                /// Initializes a new instance of the <see cref="Xamarin.Forms.Platform.Tizen.Native.Image"/> class.
                /// </summary>
                /// <param name="parent">The parent EvasObject.</param>
                public Image(EvasObject parent) : base(parent)
-               {
-                       IsScaling = true;
-                       CanScaleUp = true;
-                       CanScaleDown = true;
-
-                       ApplyAspect(Aspect.AspectFit);
-               }
-
-               /// <summary>
-               /// Gets or sets the image aspect ratio preserving option.
-               /// </summary>
-               /// <value>The aspect option.</value>
-               public Aspect Aspect
-               {
-                       get
-                       {
-                               return _aspect;
-                       }
-
-                       set
-                       {
-                               if (_aspect != value)
-                               {
-                                       ApplyAspect(value);
-                               }
-                       }
+               {                       
                }
 
                /// <summary>
@@ -67,6 +40,15 @@ namespace Xamarin.Forms.Platform.Tizen.Native
                        return isLoadComplate;
                }
 
+               public bool LoadFromFile(string file)
+               {
+                       if (!string.IsNullOrEmpty(file))
+                       {
+                               return Load(ResourcePath.GetPath(file));
+                       }
+                       return false;
+               }
+
                /// <summary>
                /// Implements the <see cref="Xamarin.Forms.Platform.Tizen.Native.IMeasurable"/> interface.
                /// </summary>
@@ -75,7 +57,6 @@ namespace Xamarin.Forms.Platform.Tizen.Native
                public ESize Measure(int availableWidth, int availableHeight)
                {
                        var imageSize = ObjectSize;
-
                        var size = new ESize()
                        {
                                Width = imageSize.Width,
@@ -96,36 +77,5 @@ namespace Xamarin.Forms.Platform.Tizen.Native
 
                        return size;
                }
-
-               /// <summary>
-               /// Sets the <c>IsFixedAspect</c> and <c>CanFillOutside</c> properties according to the given <paramref name="aspect"/>.
-               /// </summary>
-               /// <param name="aspect">The aspect setting to be applied to the image.</param>
-               void ApplyAspect(Aspect aspect)
-               {
-                       _aspect = aspect;
-
-                       switch (_aspect)
-                       {
-                               case Aspect.AspectFit:
-                                       IsFixedAspect = true;
-                                       CanFillOutside = false;
-                                       break;
-
-                               case Aspect.AspectFill:
-                                       IsFixedAspect = true;
-                                       CanFillOutside = true;
-                                       break;
-
-                               case Aspect.Fill:
-                                       IsFixedAspect = false;
-                                       CanFillOutside = false;
-                                       break;
-
-                               default:
-                                       Log.Warn("Invalid Aspect value: {0}", _aspect);
-                                       break;
-                       }
-               }
        }
 }
\ No newline at end of file
index ad75d4a..d77f36c 100644 (file)
@@ -177,7 +177,7 @@ namespace Xamarin.Forms.Platform.Tizen
 
                void UpdateAspect()
                {
-                       _image.Aspect = Element.Aspect;
+                       _image.ApplyAspect(Element.Aspect);
                }
        }
 }
index 8d6e668..1fcfe75 100644 (file)
@@ -8,52 +8,40 @@ namespace Xamarin.Forms.Platform.Tizen
 {
        public class ImageRenderer : ViewRenderer<Image, Native.Image>
        {
+               public ImageRenderer()
+               {
+                       RegisterPropertyHandler(Image.SourceProperty, UpdateSource);
+                       RegisterPropertyHandler(Image.AspectProperty, UpdateAspect);
+                       RegisterPropertyHandler(Image.IsOpaqueProperty, UpdateIsOpaque);
+                       RegisterPropertyHandler(Specific.BlendColorProperty, UpdateBlendColor);
+                       RegisterPropertyHandler(Specific.FileProperty, UpdateFile);
+               }
+
                protected override void OnElementChanged(ElementChangedEventArgs<Image> e)
                {
                        if (Control == null)
                        {
-                               var image = NativeFactory.GetNativeControl(typeof(Native.Image)) as Native.Image;
-                               SetNativeControl(image);
+                               SetNativeControl(new Native.Image(Forms.NativeParent));
                        }
-
-                       UpdateAll();
                        base.OnElementChanged(e);
                }
 
-               protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
+               async void UpdateSource(bool initialize)
                {
-                       base.OnElementPropertyChanged(sender, e);
-                       if (e.PropertyName == Image.SourceProperty.PropertyName)
-                       {
-                               UpdateSource();
-                       }
-                       else if (e.PropertyName == Image.AspectProperty.PropertyName)
-                       {
-                               UpdateAspect();
-                       }
-                       else if (e.PropertyName == Image.IsOpaqueProperty.PropertyName)
-                       {
-                               UpdateIsOpaque();
-                       }
-                       else if (e.PropertyName == Specific.BlendColorProperty.PropertyName)
-                       {
-                               UpdateBlendColor();
-                       }
-               }
+                       if (initialize && Element.Source == default(ImageSource))
+                               return;
 
-               async void UpdateSource()
-               {
                        ImageSource source = Element.Source;
-
                        ((IImageController)Element).SetIsLoading(true);
 
                        if (Control != null)
                        {
                                bool success = await Control.LoadFromImageSourceAsync(source);
+
                                if (!IsDisposed && success)
                                {
                                        ((IVisualElementController)Element).NativeSizeChanged();
-                                       UpdateAfterLoading();
+                                       UpdateAfterLoading(initialize);
                                }
                        }
 
@@ -61,31 +49,51 @@ namespace Xamarin.Forms.Platform.Tizen
                                ((IImageController)Element).SetIsLoading(false);
                }
 
-               protected virtual void UpdateAfterLoading()
+               void UpdateFile(bool initialize)
                {
-                       UpdateIsOpaque();
-                       UpdateBlendColor();
+                       if (initialize && Specific.GetFile(Element) == default || Element.Source != default(ImageSource))
+                               return;
+
+                       if (Control != null)
+                       {
+                               bool success = Control.LoadFromFile(Specific.GetFile(Element));
+
+                               if (!IsDisposed && success)
+                               {
+                                       ((IVisualElementController)Element).NativeSizeChanged();
+                                       UpdateAfterLoading(initialize);
+                               }
+                       }
                }
 
-               void UpdateAspect()
+               protected virtual void UpdateAfterLoading(bool initialize)
                {
-                       Control.Aspect = Element.Aspect;
+                       UpdateIsOpaque(initialize);
+                       UpdateBlendColor(initialize);
                }
 
-               void UpdateIsOpaque()
+               void UpdateAspect(bool initialize)
                {
-                       Control.IsOpaque = Element.IsOpaque;
+                       if (initialize && Element.Aspect == Aspect.AspectFit)
+                               return;
+
+                       Control.ApplyAspect(Element.Aspect);
                }
 
-               void UpdateBlendColor()
+               void UpdateIsOpaque(bool initialize)
                {
-                       Control.Color = Specific.GetBlendColor(Element).ToNative();
+                       if (initialize && !Element.IsOpaque)
+                               return;
+
+                       Control.IsOpaque = Element.IsOpaque;
                }
 
-               void UpdateAll()
+               void UpdateBlendColor(bool initialize)
                {
-                       UpdateSource();
-                       UpdateAspect();
+                       if (initialize && Specific.GetBlendColor(Element).IsDefault)
+                               return;
+
+                       Control.Color = Specific.GetBlendColor(Element).ToNative();
                }
        }