[NUI] Fixed the type conversion issue which cause TCT failed (#1273)
[platform/core/csapi/tizenfx.git] / src / Tizen.NUI.Components / Controls / Scrollbar.cs
index 4210f60..45d4080 100755 (executable)
@@ -17,6 +17,7 @@
 using System;
 using Tizen.NUI.BaseComponents;
 using System.ComponentModel;
+using Tizen.NUI.Binding;
 
 namespace Tizen.NUI.Components
 {
@@ -26,7 +27,99 @@ namespace Tizen.NUI.Components
     /// <since_tizen> 6 </since_tizen>
     public class ScrollBar : Control
     {
-        private ScrollBarAttributes scrollBarAttrs;
+        /// This will be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API.
+        [EditorBrowsable(EditorBrowsableState.Never)]
+        public static readonly BindableProperty DirectionProperty = BindableProperty.Create(nameof(Direction), typeof(DirectionType), typeof(ScrollBar), DirectionType.Horizontal, propertyChanged: (bindable, oldValue, newValue) =>
+        {
+            var instance = (ScrollBar)bindable;
+            if (newValue != null)
+            {
+                instance.Style.Direction = (DirectionType?)newValue;
+                instance.UpdateValue();
+            }
+        },
+        defaultValueCreator: (bindable) =>
+        {
+            var instance = (ScrollBar)bindable;
+            return instance.Style.Direction;
+        });
+        /// This will be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API.
+        [EditorBrowsable(EditorBrowsableState.Never)]
+        public static readonly BindableProperty MaxValueProperty = BindableProperty.Create(nameof(MaxValue), typeof(int), typeof(ScrollBar), default(int), propertyChanged: (bindable, oldValue, newValue) =>
+        {
+            var instance = (ScrollBar)bindable;
+            if (newValue != null)
+            {
+                if ((int)newValue >= 0)
+                {
+                    instance.maxValue = (int)newValue;
+                    instance.UpdateValue();
+                }
+            }
+        },
+        defaultValueCreator: (bindable) =>
+        {
+            var instance = (ScrollBar)bindable;
+            return instance.maxValue;
+        });
+        /// This will be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API.
+        [EditorBrowsable(EditorBrowsableState.Never)]
+        public static readonly BindableProperty MinValueProperty = BindableProperty.Create(nameof(MinValue), typeof(int), typeof(ScrollBar), default(int), propertyChanged: (bindable, oldValue, newValue) =>
+        {
+            var instance = (ScrollBar)bindable;
+            if (newValue != null)
+            {
+                if ((int)newValue >= 0)
+                {
+                    instance.minValue = (int)newValue;
+                    instance.UpdateValue();
+                }
+            }
+        },
+        defaultValueCreator: (bindable) =>
+        {
+            var instance = (ScrollBar)bindable;
+            return instance.minValue;
+        });
+        /// This will be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API.
+        [EditorBrowsable(EditorBrowsableState.Never)]
+        public static readonly BindableProperty CurrentValueProperty = BindableProperty.Create(nameof(CurrentValue), typeof(int), typeof(ScrollBar), default(int), propertyChanged: (bindable, oldValue, newValue) =>
+        {
+            var instance = (ScrollBar)bindable;
+            if (newValue != null)
+            {
+                if ((int)newValue >= 0)
+                {
+                    instance.curValue = (int)newValue;
+                    instance.UpdateValue();
+                }
+            }
+        },
+        defaultValueCreator: (bindable) =>
+        {
+            var instance = (ScrollBar)bindable;
+            return instance.curValue;
+        });
+        /// This will be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API.
+        [EditorBrowsable(EditorBrowsableState.Never)]
+        public static readonly BindableProperty DurationProperty = BindableProperty.Create(nameof(Duration), typeof(uint), typeof(ScrollBar), default(uint), propertyChanged: (bindable, oldValue, newValue) =>
+        {
+            var instance = (ScrollBar)bindable;
+            if (newValue != null)
+            {
+                instance.Style.Duration = (uint)newValue;
+                if (instance.scrollAniPlayer != null)
+                {
+                    instance.scrollAniPlayer.Duration = (int)(uint)newValue;
+                }
+            }
+        },
+        defaultValueCreator: (bindable) =>
+        {
+            var instance = (ScrollBar)bindable;
+            return instance.Style.Duration;
+        });
+
         private ImageView trackImage;
         private ImageView thumbImage;
         private Animation scrollAniPlayer = null;
@@ -36,9 +129,10 @@ namespace Tizen.NUI.Components
         private int minValue;
         private int maxValue;
         private int curValue;
+        static ScrollBar() { }
 
         /// <summary>
-        /// The constructor of ScrollBar
+        /// The constructor of ScrollBar.
         /// </summary>
         /// <since_tizen> 6 </since_tizen>
         public ScrollBar() : base()
@@ -59,13 +153,13 @@ namespace Tizen.NUI.Components
         }
 
         /// <summary>
-        /// The constructor of ScrollBar with specific Attributes.
+        /// The constructor of ScrollBar with specific style.
         /// </summary>
-        /// <param name="attributes">The Attributes object to initialize the ScrollBar.</param>
+        /// <param name="style">The style object to initialize the ScrollBar.</param>
         /// <since_tizen> 6 </since_tizen>
         /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
         [EditorBrowsable(EditorBrowsableState.Never)]
-        public ScrollBar(ScrollBarAttributes attributes) : base(attributes)
+        public ScrollBar(ScrollBarStyle style) : base(style)
         {
             Initialize();
         }
@@ -90,6 +184,9 @@ namespace Tizen.NUI.Components
         }
 
         #region public property 
+        /// This will be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API.
+        [EditorBrowsable(EditorBrowsableState.Never)]
+        public new ScrollBarStyle Style => ViewStyle as ScrollBarStyle;
 
         /// <summary>
         /// The property to get/set the direction of the ScrollBar.
@@ -99,12 +196,11 @@ namespace Tizen.NUI.Components
         {
             get
             {
-                return scrollBarAttrs.Direction.Value;
+                return (DirectionType)GetValue(DirectionProperty);
             }
             set
             {
-                scrollBarAttrs.Direction = value;
-                RelayoutRequest();
+                SetValue(DirectionProperty, value);
             }
         }
 
@@ -130,22 +226,13 @@ namespace Tizen.NUI.Components
         {
             get
             {
-                if (scrollBarAttrs.ThumbImageAttributes.Size == null)
-                {
-                    scrollBarAttrs.ThumbImageAttributes.Size = new Size();
-                }
-                return scrollBarAttrs.ThumbImageAttributes.Size;
+                return Style?.Thumb?.Size;
             }
             set
             {
-                if (scrollBarAttrs.ThumbImageAttributes.Size == null)
-                {
-                    scrollBarAttrs.ThumbImageAttributes.Size = new Size();
-                }
-                if (thumbImage != null)
+                if (null != Style?.Thumb)
                 {
-                    scrollBarAttrs.ThumbImageAttributes.Size.Width = value.Width;
-                    scrollBarAttrs.ThumbImageAttributes.Size.Height = value.Height;
+                    Style.Thumb.Size = value;
                     RelayoutRequest();
                 }
             }
@@ -159,19 +246,15 @@ namespace Tizen.NUI.Components
         {
             get
             {
-                return scrollBarAttrs.TrackImageAttributes.ResourceURL.All;
+                return Style?.Track?.ResourceUrl?.All;
             }
             set
             {
-                if (trackImage != null)
+                if (null != Style?.Track)
                 {
-                    if (scrollBarAttrs.TrackImageAttributes.ResourceURL == null)
-                    {
-                        scrollBarAttrs.TrackImageAttributes.ResourceURL = new StringSelector();
-                    }
-                    scrollBarAttrs.TrackImageAttributes.ResourceURL.All = value;
+                    Style.Track.ResourceUrl = value;
+                    RelayoutRequest();
                 }
-                RelayoutRequest();
             }
         }
 
@@ -183,19 +266,15 @@ namespace Tizen.NUI.Components
         {
             get
             {
-                return scrollBarAttrs.TrackImageAttributes.BackgroundColor?.All;
+                return Style?.Track?.BackgroundColor?.All;
             }
             set
             {
-                if (scrollBarAttrs.TrackImageAttributes.BackgroundColor == null)
+                if (null != Style?.Track)
                 {
-                    scrollBarAttrs.TrackImageAttributes.BackgroundColor = new ColorSelector { All = value };
-                }
-                else
-                {
-                    scrollBarAttrs.TrackImageAttributes.BackgroundColor.All = value;
+                    Style.Track.BackgroundColor = value;
+                    RelayoutRequest();
                 }
-                RelayoutRequest();
             }
         }
 
@@ -207,19 +286,15 @@ namespace Tizen.NUI.Components
         {
             get
             {
-                return scrollBarAttrs.ThumbImageAttributes.BackgroundColor?.All;
+                return Style?.Thumb?.BackgroundColor?.All;
             }
             set
             {
-                if(scrollBarAttrs.ThumbImageAttributes.BackgroundColor == null)
+                if(null != Style?.Thumb)
                 {
-                    scrollBarAttrs.ThumbImageAttributes.BackgroundColor = new ColorSelector { All = value };
-                }
-                else
-                {
-                    scrollBarAttrs.ThumbImageAttributes.BackgroundColor.All = value;
+                    Style.Thumb.BackgroundColor = value;
+                    RelayoutRequest();
                 }
-                RelayoutRequest();
             }
         }
 
@@ -231,15 +306,11 @@ namespace Tizen.NUI.Components
         {
             get
             {
-                return maxValue;
+                return (int)GetValue(MaxValueProperty);
             }
             set
             {
-                if(value >= 0)
-                {
-                    maxValue = value;
-                    RelayoutRequest();
-                }
+                SetValue(MaxValueProperty, value);
             }
         }
 
@@ -251,15 +322,11 @@ namespace Tizen.NUI.Components
         {
             get
             {
-                return minValue;
+                return (int)GetValue(MinValueProperty);
             }
             set
             {
-                if(value >= 0)
-                {
-                    minValue = value;
-                    RelayoutRequest();
-                }
+                SetValue(MinValueProperty, value);
             }
         }
 
@@ -287,15 +354,11 @@ namespace Tizen.NUI.Components
         {
             get
             {
-                return curValue;
+                return (int)GetValue(CurrentValueProperty);
             }
             set
             {
-                if(value >= 0)
-                {
-                    curValue = value;
-                    RelayoutRequest();
-                }
+                SetValue(CurrentValueProperty, value);
             }
         }
 
@@ -307,15 +370,11 @@ namespace Tizen.NUI.Components
         {
             get
             {
-                return scrollBarAttrs.Duration;
+                return (uint)GetValue(DurationProperty);
             }
             set
             {
-                scrollBarAttrs.Duration = value;
-                if (scrollAniPlayer != null)
-                {
-                    scrollAniPlayer.Duration = (int)value;
-                }
+                SetValue(DurationProperty, value);
             }
         }
         #endregion
@@ -324,7 +383,7 @@ namespace Tizen.NUI.Components
         /// Method to set current value. The thumb object would move to the corresponding position with animation or not.
         /// </summary>
         /// <param name="currentValue">The special current value.</param>
-        /// <param name="EnableAnimation">Enable move with animation or not, the default value is true.</param>
+        /// <param name="enableAnimation">Enable move with animation or not, the default value is true.</param>
         /// <exception cref="ArgumentOutOfRangeException">Throw when current size is less than the min value, or greater than the max value.</exception>
         /// <example>
         /// <code>
@@ -342,15 +401,17 @@ namespace Tizen.NUI.Components
         /// </code>
         /// </example>
         /// <since_tizen> 6 </since_tizen>
-        public void SetCurrentValue(int currentValue, bool EnableAnimation = true)
+        public void SetCurrentValue(int currentValue, bool enableAnimation = true)
         {
             if (currentValue < minValue || currentValue > maxValue)
             {
                 //TNLog.E("Current value is less than the Min value, or greater than the Max value. currentValue = " + currentValue + ";");
+#pragma warning disable CA2208 // Instantiate argument exceptions correctly
                 throw new ArgumentOutOfRangeException("Wrong Current value. It shoud be greater than the Min value, and less than the Max value!");
+#pragma warning restore CA2208 // Instantiate argument exceptions correctly
             }
 
-            enableAni = EnableAnimation;
+            enableAni = enableAnimation;
             CurrentValue = currentValue;
         }
 
@@ -372,15 +433,8 @@ namespace Tizen.NUI.Components
                 //Release your own managed resources here.
                 //You should release all of your own disposable objects here.
 
-                if (trackImage != null)
-                {
-                    Utility.Dispose(trackImage);
-                }
-
-                if (thumbImage != null)
-                {
-                    Utility.Dispose(thumbImage);
-                }
+                Utility.Dispose(trackImage);
+                Utility.Dispose(thumbImage);
 
                 if (scrollAniPlayer != null)
                 {
@@ -389,7 +443,6 @@ namespace Tizen.NUI.Components
                     scrollAniPlayer.Dispose();
                     scrollAniPlayer = null;
                 }
-                // UIDirectionChangedEvent -= OnUIDirectionChangedEvent;
             }
 
             //Release your own unmanaged resources here.
@@ -402,55 +455,14 @@ namespace Tizen.NUI.Components
         }
 
         /// <summary>
-        /// The method to update Attributes.
-        /// </summary>
-        /// <since_tizen> 6 </since_tizen>
-        /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
-        [EditorBrowsable(EditorBrowsableState.Never)]
-        protected override void OnUpdate()
-        {
-            ApplyAttributes(this, scrollBarAttrs);
-            ApplyAttributes(trackImage, scrollBarAttrs.TrackImageAttributes);
-            ApplyAttributes(thumbImage, scrollBarAttrs.ThumbImageAttributes);
-            if (enableAni)
-            {
-                UpdateValue(true);
-                enableAni = false;
-            }
-            else
-            {
-                UpdateValue();
-            }
-        }
-
-        /// <summary>
-        /// Get Scrollbar attribues.
+        /// Get Scrollbar style.
         /// </summary>
         /// <since_tizen> 6 </since_tizen>
         /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
         [EditorBrowsable(EditorBrowsableState.Never)]
-        protected override Attributes GetAttributes()
+        protected override ViewStyle GetViewStyle()
         {
-            return new ScrollBarAttributes()
-            {
-                ThumbImageAttributes = new ImageAttributes
-                {
-                    WidthResizePolicy = ResizePolicyType.Fixed,
-                    HeightResizePolicy = ResizePolicyType.Fixed,
-                    PositionUsesPivotPoint = true,
-                    ParentOrigin = Tizen.NUI.ParentOrigin.TopLeft,
-                    PivotPoint = Tizen.NUI.PivotPoint.TopLeft
-                },
-                TrackImageAttributes = new ImageAttributes
-                {
-                    WidthResizePolicy = ResizePolicyType.FillToParent,
-                    HeightResizePolicy = ResizePolicyType.FillToParent,
-                    PositionUsesPivotPoint = true,
-                    ParentOrigin = Tizen.NUI.ParentOrigin.TopLeft,
-                    PivotPoint = Tizen.NUI.PivotPoint.TopLeft
-                }
-
-            };
+            return new ScrollBarStyle();
         }
 
         /// <summary>
@@ -461,22 +473,16 @@ namespace Tizen.NUI.Components
         [EditorBrowsable(EditorBrowsableState.Never)]
         protected override void OnThemeChangedEvent(object sender, StyleManager.ThemeChangeEventArgs e)
         {
-            ScrollBarAttributes tempAttributes = StyleManager.Instance.GetAttributes(style) as ScrollBarAttributes;
-            if (tempAttributes != null)
+            ScrollBarStyle tempStyle = StyleManager.Instance.GetViewStyle(style) as ScrollBarStyle;
+            if (tempStyle != null)
             {
-                attributes = scrollBarAttrs = tempAttributes;
-                RelayoutRequest();
+                Style.CopyFrom(tempStyle);
+                UpdateValue();
             }
         }
 
         private void Initialize()
         {
-            scrollBarAttrs = this.attributes as ScrollBarAttributes;
-            if(scrollBarAttrs == null)
-            {
-                throw new Exception("ScrollBar attribute parse error.");
-            }
-
             this.Focusable = false;
 
             trackImage = new ImageView
@@ -485,22 +491,23 @@ namespace Tizen.NUI.Components
                 WidthResizePolicy = ResizePolicyType.FillToParent,
                 HeightResizePolicy = ResizePolicyType.FillToParent,
                 PositionUsesPivotPoint = true,
-                ParentOrigin = Tizen.NUI.ParentOrigin.TopLeft,
-                PivotPoint = Tizen.NUI.PivotPoint.TopLeft
-
+                ParentOrigin = Tizen.NUI.ParentOrigin.CenterLeft,
+                PivotPoint = Tizen.NUI.PivotPoint.CenterLeft
             };
+            this.Add(trackImage);
+            trackImage.ApplyStyle(Style.Track);
+
             thumbImage = new ImageView
             {
                 Focusable = false,
                 WidthResizePolicy = ResizePolicyType.Fixed,
                 HeightResizePolicy = ResizePolicyType.Fixed,
                 PositionUsesPivotPoint = true,
-                ParentOrigin = Tizen.NUI.ParentOrigin.TopLeft,
-                PivotPoint = Tizen.NUI.PivotPoint.TopLeft
+                ParentOrigin = Tizen.NUI.ParentOrigin.CenterLeft,
+                PivotPoint = Tizen.NUI.PivotPoint.CenterLeft
             };
-
-            Add(trackImage);
-            Add(thumbImage);
+            this.Add(thumbImage);
+            thumbImage.ApplyStyle(Style.Thumb);
 
             scrollAniPlayer = new Animation(334);
             scrollAniPlayer.DefaultAlphaFunction = new AlphaFunction(AlphaFunction.BuiltinFunctions.Linear);
@@ -515,33 +522,26 @@ namespace Tizen.NUI.Components
             RelayoutRequest();
         }
 
-        private void UpdateValue(bool enableAni = false)
+        private void UpdateValue()
         {
-            if (trackImage == null || thumbImage == null || scrollBarAttrs.Direction == null)
-            {             
-                return;
-            }
+            if (minValue >= maxValue || curValue < minValue || curValue > maxValue) return;
 
-            if (minValue >= maxValue || curValue < minValue || curValue > maxValue)
-            {
-                return;
-            }
             float width = (float)Size2D.Width;
             float height = (float)Size2D.Height;
             float thumbW = 0.0f;
             float thumbH = 0.0f;
-            if (scrollBarAttrs.ThumbImageAttributes.Size == null)
+            if (Style.Thumb.Size == null)
             {
                 return;
             }
             else
             {
-                thumbW = scrollBarAttrs.ThumbImageAttributes.Size.Width;
-                thumbH = scrollBarAttrs.ThumbImageAttributes.Size.Height;
+                thumbW = Style.Thumb.Size.Width;
+                thumbH = Style.Thumb.Size.Height;
             }
             float ratio = (float)(curValue - minValue) / (float)(maxValue - minValue);
 
-            if (scrollBarAttrs.Direction == DirectionType.Horizontal)
+            if (Style.Direction == DirectionType.Horizontal)
             {
                 if (LayoutDirection == ViewLayoutDirectionType.RTL)
                 {
@@ -552,7 +552,7 @@ namespace Tizen.NUI.Components
                 float posY = (height - thumbH) / 2.0f;
 
                 thumbImagePosX = posX;
-                if (scrollAniPlayer != null)
+                if (null != scrollAniPlayer)
                 {
                     scrollAniPlayer.Stop();
                 }
@@ -563,7 +563,7 @@ namespace Tizen.NUI.Components
                 }
                 else
                 {
-                    if (scrollAniPlayer != null)
+                    if (null != scrollAniPlayer)
                     {
                         scrollAniPlayer.Clear();
                         scrollAniPlayer.AnimateTo(thumbImage, "PositionX", posX);
@@ -577,7 +577,7 @@ namespace Tizen.NUI.Components
                 float posY = ratio * (height - thumbH);
 
                 thumbImagePosY = posY;
-                if (scrollAniPlayer != null)
+                if (null != scrollAniPlayer)
                 {
                     scrollAniPlayer.Stop();
                 }
@@ -588,7 +588,7 @@ namespace Tizen.NUI.Components
                 }
                 else
                 {
-                    if (scrollAniPlayer != null)
+                    if (null != scrollAniPlayer)
                     {
                         scrollAniPlayer.Clear();
                         scrollAniPlayer.AnimateTo(thumbImage, "PositionY", posY);
@@ -596,17 +596,16 @@ namespace Tizen.NUI.Components
                     }
                 }
             }
+
+            if (enableAni) enableAni = false;
         }
 
         private DirectionType CurrentDirection()
         {
             DirectionType dir = DirectionType.Horizontal;
-            if (scrollBarAttrs != null)
+            if (null != Style.Direction)
             {
-                if (scrollBarAttrs.Direction != null)
-                {
-                    dir = scrollBarAttrs.Direction.Value;
-                }
+                dir = Style.Direction.Value;
             }
             return dir;
         }
@@ -652,7 +651,7 @@ namespace Tizen.NUI.Components
                 }
             }
 
-            return (int)curValue;
+            return curValue;
         }
     }
 }