[NUI] introduce CreateViewStyle that is alternative to GetViewStyle (#1681)
[platform/core/csapi/tizenfx.git] / src / Tizen.NUI.Components / Controls / Tab.cs
index 0c36281..d306d30 100755 (executable)
@@ -32,9 +32,10 @@ namespace Tizen.NUI.Components
         private List<TabItem> itemList = new List<TabItem>();
         private int curIndex = 0;
         private View underline = null;
-        private TabAttributes tabAttributes = null;
         private Animation underlineAni = null;
         private bool isNeedAnimation = false;
+        private Extents space;
+        static Tab() { }
 
         /// <summary>
         /// Creates a new instance of a Tab.
@@ -49,22 +50,18 @@ namespace Tizen.NUI.Components
         /// Creates a new instance of a Tab with style.
         /// </summary>
         /// <param name="style">Create Tab by special style defined in UX.</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)]
+        /// <since_tizen> 8 </since_tizen>
         public Tab(string style) : base(style)
         {
             Initialize();
         }
 
         /// <summary>
-        /// Creates a new instance of a Tab with attributes.
+        /// Creates a new instance of a Tab with style.
         /// </summary>
-        /// <param name="attributes">Create Tab by attributes customized by user.</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 Tab(TabAttributes attributes) : base(attributes)
+        /// <param name="tabStyle">Create Tab by style customized by user.</param>
+        /// <since_tizen> 8 </since_tizen>
+        public Tab(TabStyle tabStyle) : base(tabStyle)
         {
             Initialize();
         }
@@ -76,6 +73,35 @@ namespace Tizen.NUI.Components
         public event EventHandler<ItemChangedEventArgs> ItemChangedEvent;
 
         /// <summary>
+        /// Get style of tab.
+        /// </summary>
+        /// <since_tizen> 8 </since_tizen>
+        public new TabStyle Style => ViewStyle as TabStyle;
+
+        [EditorBrowsable(EditorBrowsableState.Never)]
+        public View Underline
+        {
+            get
+            {
+                if (null == underline)
+                {
+                    underline = new View()
+                    {
+                        PositionUsesPivotPoint = true,
+                        ParentOrigin = Tizen.NUI.ParentOrigin.BottomLeft,
+                        PivotPoint = Tizen.NUI.PivotPoint.BottomLeft,
+                    };
+                    Add(underline);
+                }
+                return underline;
+            }
+            internal set
+            {
+                underline = value;
+            }
+        }
+
+        /// <summary>
         /// Selected item's index in Tab.
         /// </summary>
         /// <since_tizen> 6 </since_tizen>
@@ -103,12 +129,15 @@ namespace Tizen.NUI.Components
         {
             get
             {
-                return tabAttributes.UseTextNaturalSize;
+                return Style?.UseTextNaturalSize ?? false;
             }
             set
             {
-                tabAttributes.UseTextNaturalSize = value;
-                RelayoutRequest();
+                if (null != Style)
+                {
+                    Style.UseTextNaturalSize = value;
+                    RelayoutRequest();
+                }
             }
         }
 
@@ -120,12 +149,15 @@ namespace Tizen.NUI.Components
         {
             get
             {
-                return tabAttributes.ItemSpace;
+                return Style?.ItemSpace ?? 0;
             }
             set
             {
-                tabAttributes.ItemSpace = value;
-                RelayoutRequest();
+                if (null != Style)
+                {
+                    Style.ItemSpace = value;
+                    RelayoutRequest();
+                }
             }
         }
 
@@ -137,20 +169,48 @@ namespace Tizen.NUI.Components
         {
             get
             {
-                if(null == tabAttributes || null == tabAttributes.Space)
-                {
-                    return null;
-                }
-                else
-                {
-                    return new Extents((ushort)tabAttributes.Space.X, (ushort)tabAttributes.Space.Y, (ushort)tabAttributes.Space.Z, (ushort)tabAttributes.Space.W);
-                }
+                return ItemPadding;
             }
             set
             {
-                if(null != value)
+                ItemPadding = value;
+            }
+        }
+
+        /// <summary>
+        /// Item paddings in Tab. Sequence as Left, Right, Top, Bottom
+        /// </summary>
+        /// <since_tizen> 6 </since_tizen>
+        /// 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 Extents ItemPadding
+        {
+            get
+            {
+                return space;
+            }
+            set
+            {
+                if(null != value && null != Style?.ItemPadding)
                 {
-                    tabAttributes.Space = new Vector4(value.Start, value.End, value.Top, value.Bottom);
+                    Style.ItemPadding.CopyFrom(value);
+
+                    if (null == space)
+                    {
+                        space = new Extents((ushort start, ushort end, ushort top, ushort bottom) =>
+                        {
+                            Style.ItemPadding.Start = start;
+                            Style.ItemPadding.End = end;
+                            Style.ItemPadding.Top = top;
+                            Style.ItemPadding.Bottom = bottom;
+                            RelayoutRequest();
+                        }, value.Start, value.End, value.Top, value.Bottom);
+                    }
+                    else
+                    {
+                        space.CopyFrom(value);
+                    }
+
                     RelayoutRequest();
                 }
             }
@@ -164,15 +224,13 @@ namespace Tizen.NUI.Components
         {
             get
             {
-                return tabAttributes.UnderLineAttributes?.Size;
+                return Style?.UnderLine?.Size;
             }
             set
             {
-                if (value != null)
+                if (null != Style?.UnderLine)
                 {
-                    CreateUnderLineAttributes();
-                    tabAttributes.UnderLineAttributes.Size = value;
-                    RelayoutRequest();
+                    Style.UnderLine.Size = value;
                 }
             }
         }
@@ -185,19 +243,13 @@ namespace Tizen.NUI.Components
         {
             get
             {
-                return tabAttributes.UnderLineAttributes?.BackgroundColor?.All;
+                return Style?.UnderLine?.BackgroundColor?.All;
             }
             set
             {
-                if (value != null)
+                if (null != Style?.UnderLine)
                 {
-                    CreateUnderLineAttributes();
-                    if (tabAttributes.UnderLineAttributes.BackgroundColor == null)
-                    {
-                        tabAttributes.UnderLineAttributes.BackgroundColor = new ColorSelector();
-                    }
-                    tabAttributes.UnderLineAttributes.BackgroundColor.All = value;
-                    RelayoutRequest();
+                    Style.UnderLine.BackgroundColor = value;
                 }
             }
         }
@@ -210,17 +262,14 @@ namespace Tizen.NUI.Components
         {
             get
             {
-                return tabAttributes.TextAttributes?.PointSize?.All ?? 0;
+                return Style?.Text?.PointSize?.All ?? 0;
             }
             set
             {
-                CreateTextAttributes();
-                if (tabAttributes.TextAttributes.PointSize == null)
+                if (null != Style?.Text)
                 {
-                    tabAttributes.TextAttributes.PointSize = new FloatSelector();
+                    Style.Text.PointSize = value;
                 }
-                tabAttributes.TextAttributes.PointSize.All = value;
-                RelayoutRequest();
             }
         }
 
@@ -232,13 +281,14 @@ namespace Tizen.NUI.Components
         {
             get
             {
-                return tabAttributes.TextAttributes?.FontFamily;
+                return Style?.Text?.FontFamily?.All;
             }
             set
             {
-                CreateTextAttributes();
-                tabAttributes.TextAttributes.FontFamily = value;
-                RelayoutRequest();
+                if (null != Style?.Text)
+                {
+                    Style.Text.FontFamily = value;
+                }
             }
         }
 
@@ -250,20 +300,18 @@ namespace Tizen.NUI.Components
         {
             get
             {
-                return tabAttributes.TextAttributes?.TextColor?.All;
+                return Style?.Text?.TextColor?.All;
             }
             set
             {
-                CreateTextAttributes();
-                if (tabAttributes.TextAttributes.TextColor == null)
+                if (null != Style?.Text)
                 {
-                    tabAttributes.TextAttributes.TextColor = new ColorSelector();
+                    Style.Text.TextColor = value;
                 }
-                tabAttributes.TextAttributes.TextColor.All = value;
-                RelayoutRequest();
             }
         }
 
+        private ColorSelector textColorSelector = new ColorSelector();
         /// <summary>
         /// Text color selector in Tab.
         /// </summary>
@@ -272,15 +320,18 @@ namespace Tizen.NUI.Components
         {
             get
             {
-                return tabAttributes.TextAttributes.TextColor;
+                return textColorSelector;
             }
             set
             {
-                if (value != null)
+                if (value == null || textColorSelector == null)
                 {
-                    CreateTextAttributes();
-                    tabAttributes.TextAttributes.TextColor = value.Clone() as ColorSelector;
-                    RelayoutRequest();
+                    Tizen.Log.Fatal("NUI", "[Exception] Tab.TextColorSelector is null");
+                    throw new NullReferenceException("Tab.TextColorSelector is null");
+                }
+                else
+                {
+                    textColorSelector.Clone(value);
                 }
             }
         }
@@ -331,6 +382,24 @@ namespace Tizen.NUI.Components
         }
 
         /// <summary>
+        /// Apply style to tab.
+        /// </summary>
+        /// <param name="viewStyle">The style to apply.</param>
+        /// <since_tizen> 8 </since_tizen>
+        public override void ApplyStyle(ViewStyle viewStyle)
+        {
+            base.ApplyStyle(viewStyle);
+
+            TabStyle tabStyle = viewStyle as TabStyle;
+
+            if (null != tabStyle)
+            {
+                Underline.ApplyStyle(tabStyle.UnderLine);
+                CreateUnderLineAnimation();
+            }
+        }
+
+        /// <summary>
         /// Dispose Tab and all children on it.
         /// </summary>
         /// <param name="type">Dispose type.</param>
@@ -371,65 +440,38 @@ namespace Tizen.NUI.Components
         }
 
         /// <summary>
-        /// Update Tab by attributes.
+        /// Update Tab.
         /// </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()
         {
-            if (tabAttributes.UnderLineAttributes != null)
-            {
-                if (underline == null)
-                {
-                    underline = new View()
-                    {
-                        PositionUsesPivotPoint = true,
-                        ParentOrigin = Tizen.NUI.ParentOrigin.BottomLeft,
-                        PivotPoint = Tizen.NUI.PivotPoint.BottomLeft,
-                    };
-                    Add(underline);
-                    CreateUnderLineAnimation();
-                }
-                ApplyAttributes(underline, tabAttributes.UnderLineAttributes);
-            }
-
-            if (tabAttributes.TextAttributes != null)
-            {
-                if (curIndex >= 0 && curIndex < itemList.Count)
-                {
-                    itemList[curIndex].UpdateItemText(tabAttributes.TextAttributes);
-                }
-            }
-
             LayoutChild();
         }
 
         /// <summary>
-        /// Get Tab attribues.
+        /// Get Tab 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()
+        /// <returns>The default tab style.</returns>
+        /// <since_tizen> 8 </since_tizen>
+        protected override ViewStyle CreateViewStyle()
         {
-            return new TabAttributes();
+            return new TabStyle();
         }
 
         /// <summary>
         /// Theme change callback when theme is changed, this callback will be trigger.
         /// </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)]
+        /// <param name="sender">The sender</param>
+        /// <param name="e">The event data</param>
+        /// <since_tizen> 8 </since_tizen>
         protected override void OnThemeChangedEvent(object sender, StyleManager.ThemeChangeEventArgs e)
         {
-            TabAttributes tempAttributes = StyleManager.Instance.GetAttributes(style) as TabAttributes;
-            if (tempAttributes != null)
+            TabStyle tabStyle = StyleManager.Instance.GetViewStyle(style) as TabStyle;
+            if (tabStyle != null)
             {
-                tempAttributes.UseTextNaturalSize = tabAttributes.UseTextNaturalSize; // keep IsNatureTextWidth as original
-                attributes = tabAttributes = tempAttributes;
-                RelayoutRequest();
+                Style.CopyFrom(tabStyle);
             }
         }
 
@@ -441,23 +483,24 @@ namespace Tizen.NUI.Components
         [EditorBrowsable(EditorBrowsableState.Never)]
         protected virtual void LayoutChild()
         {
-            if (tabAttributes == null || itemList == null)
+            if (itemList == null)
             {
                 return;
             }
+
             int totalNum = itemList.Count;
             if (totalNum == 0)
             {
                 return;
             }
 
-            int preX = (int)tabAttributes.Space.X;
+            int preX = (int)Style.ItemPadding.Start;
             int preW = 0;
-            int itemSpace = tabAttributes.ItemSpace;
+            int itemSpace = Style.ItemSpace;
 
             if (LayoutDirection == ViewLayoutDirectionType.LTR)
             {
-                if (tabAttributes.UseTextNaturalSize == true)
+                if (Style.UseTextNaturalSize == true)
                 {
                     for (int i = 0; i < totalNum; i++)
                     {
@@ -470,7 +513,7 @@ namespace Tizen.NUI.Components
                 }
                 else
                 {
-                    preW = (Size2D.Width - (int)tabAttributes.Space.X - (int)tabAttributes.Space.Y) / totalNum;
+                    preW = (Size2D.Width - (int)Style.ItemPadding.Start - (int)Style.ItemPadding.End) / totalNum;
                     for (int i = 0; i < totalNum; i++)
                     {
                         itemList[i].Position2D.X = preX;
@@ -482,13 +525,13 @@ namespace Tizen.NUI.Components
             }
             else
             {
-                preX = (int)tabAttributes.Space.Y;
-                if (tabAttributes.UseTextNaturalSize == true)
+                preX = (int)Style.ItemPadding.End;
+                if (Style.UseTextNaturalSize == true)
                 {
                     int w = Size2D.Width;
                     for (int i = 0; i < totalNum; i++)
                     {
-                        preW = (itemList[i].TextItem.NaturalSize2D != null ? itemList[i].TextItem.NaturalSize2D.Width : 0);
+                        preW = (itemList[i].NaturalSize2D != null ? itemList[i].NaturalSize2D.Width : 0);
                         itemList[i].Position2D.X = w - preW - preX;
                         itemList[i].Size2D.Width = preW;
                         preX = w - itemList[i].Position2D.X + itemSpace;
@@ -497,7 +540,7 @@ namespace Tizen.NUI.Components
                 }
                 else
                 {
-                    preW = (Size2D.Width - (int)tabAttributes.Space.X - (int)tabAttributes.Space.Y) / totalNum;
+                    preW = (Size2D.Width - (int)Style.ItemPadding.Start - (int)Style.ItemPadding.End) / totalNum;
                     for (int i = totalNum - 1; i >= 0; i--)
                     {
                         itemList[i].Position2D.X = preX;
@@ -512,13 +555,6 @@ namespace Tizen.NUI.Components
 
         private void Initialize()
         {
-            tabAttributes = attributes as TabAttributes;
-            if (tabAttributes == null)
-            {
-                throw new Exception("Tab attribute parse error.");
-            }
-
-            ApplyAttributes(this, tabAttributes);
             LayoutDirectionChanged += OnLayoutDirectionChanged;
         }
 
@@ -529,21 +565,24 @@ namespace Tizen.NUI.Components
 
         private void AddItemByIndex(TabItemData itemData, int index)
         {
+            if (null == itemData) return;
             int h = 0;
-            int topSpace = (int)tabAttributes.Space.Z;
-            if (tabAttributes.UnderLineAttributes != null && tabAttributes.UnderLineAttributes.Size != null)
+            int topSpace = (int)Style.ItemPadding.Top;
+            if (Style.UnderLine != null && Style.UnderLine.Size != null)
             {
-                h = (int)tabAttributes.UnderLineAttributes.Size.Height;
+                h = (int)Style.UnderLine.Size.Height;
             }
+
             Tab.TabItem item = new TabItem();
-            ApplyAttributes(item.TextItem, tabAttributes.TextAttributes);
-            item.TextItem.Text = itemData.Text;
+            item.TextItem.ApplyStyle(Style.Text);
+
+            item.Text = itemData.Text;
             item.Size2D.Height = Size2D.Height - h - topSpace;
             item.Position2D.Y = topSpace;
             item.TouchEvent += ItemTouchEvent;
             Add(item);
 
-            if(index >= itemList.Count)
+            if (index >= itemList.Count)
             {
                 itemList.Add(item);
             }
@@ -560,8 +599,7 @@ namespace Tizen.NUI.Components
             LayoutChild();
             if (itemList != null && curIndex >= 0 && curIndex < itemList.Count)
             {
-                itemList[curIndex].State = ControlStates.Selected;
-                itemList[curIndex].UpdateItemText(tabAttributes.TextAttributes);
+                itemList[curIndex].ControlState = ControlStates.Selected;
                 UpdateUnderLinePos();
             }
             else
@@ -573,36 +611,6 @@ namespace Tizen.NUI.Components
             }
         }
 
-        private void CreateUnderLineAttributes()
-        {
-            if (tabAttributes.UnderLineAttributes == null)
-            {
-                tabAttributes.UnderLineAttributes = new ViewAttributes()
-                {
-                    PositionUsesPivotPoint = true,
-                    ParentOrigin = Tizen.NUI.ParentOrigin.BottomLeft,
-                    PivotPoint = Tizen.NUI.PivotPoint.BottomLeft,
-                };
-            }
-        }
-
-        private void CreateTextAttributes()
-        {
-            if (tabAttributes.TextAttributes == null)
-            {
-                tabAttributes.TextAttributes = new TextAttributes()
-                {
-                    PositionUsesPivotPoint =  true,
-                    ParentOrigin = Tizen.NUI.ParentOrigin.Center,
-                    PivotPoint = Tizen.NUI.PivotPoint.Center,
-                    HorizontalAlignment = HorizontalAlignment.Center,
-                    VerticalAlignment = VerticalAlignment.Center,
-                    WidthResizePolicy =  ResizePolicyType.FillToParent,
-                    HeightResizePolicy = ResizePolicyType.FillToParent
-                };
-            }
-        }
-
         private void CreateUnderLineAnimation()
         {
             if (underlineAni == null)
@@ -613,16 +621,16 @@ namespace Tizen.NUI.Components
         
         private void UpdateUnderLinePos()
         {
-            if (underline == null || tabAttributes.UnderLineAttributes == null || tabAttributes.UnderLineAttributes.Size == null
+            if (underline == null || Style.UnderLine == null || Style.UnderLine.Size == null
                 || itemList == null || itemList.Count <= 0)
             {
                 return;
             }
 
-            tabAttributes.UnderLineAttributes.Size.Width = itemList[curIndex].Size2D.Width;
+            Style.UnderLine.Size.Width = itemList[curIndex].Size2D.Width;
 
-            underline.Size2D = new Size2D(itemList[curIndex].Size2D.Width, (int)tabAttributes.UnderLineAttributes.Size.Height);
-            underline.BackgroundColor = tabAttributes.UnderLineAttributes.BackgroundColor.All;
+            underline.Size2D = new Size2D(itemList[curIndex].Size2D.Width, (int)Style.UnderLine.Size.Height);
+            underline.BackgroundColor = Style.UnderLine.BackgroundColor.All;
             if (isNeedAnimation)
             {
                 CreateUnderLineAnimation();
@@ -657,11 +665,9 @@ namespace Tizen.NUI.Components
             };
             ItemChangedEvent?.Invoke(this, e);
 
-            itemList[curIndex].State = ControlStates.Normal;
-            itemList[curIndex].UpdateItemText(tabAttributes.TextAttributes);
+            itemList[curIndex].ControlState = ControlStates.Normal;
             curIndex = item.Index;
-            itemList[curIndex].State = ControlStates.Selected;
-            itemList[curIndex].UpdateItemText(tabAttributes.TextAttributes);
+            itemList[curIndex].ControlState = ControlStates.Selected;
 
             UpdateUnderLinePos();
         }
@@ -682,7 +688,7 @@ namespace Tizen.NUI.Components
             return true;
         }
 
-        internal class TabItem : Control
+        internal class TabItem : View
         {
             public TabItem() : base()
             {
@@ -697,6 +703,14 @@ namespace Tizen.NUI.Components
                     VerticalAlignment = VerticalAlignment.Center
                 };
                 Add(TextItem);
+
+                EnableControlStatePropagation = true;
+            }
+
+            internal int Index
+            {
+                get;
+                set;
             }
 
             public string Text
@@ -711,47 +725,11 @@ namespace Tizen.NUI.Components
                 }
             }
 
-            internal int Index
-            {
-                get;
-                set;
-            }
-
             internal TextLabel TextItem
             {
                 get;
                 set;
             }
-
-            protected override void Dispose(DisposeTypes type)
-            {
-                if (disposed)
-                {
-                    return;
-                }
-
-                if (type == DisposeTypes.Explicit)
-                {
-                    if (TextItem != null)
-                    {
-                        Remove(TextItem);
-                        TextItem.Dispose();
-                        TextItem = null;
-                    }
-                }
-
-                base.Dispose(type);
-            }
-
-            protected override Attributes GetAttributes()
-            {
-                return null;
-            }
-
-            internal void UpdateItemText(TextAttributes attrs)
-            {
-                ApplyAttributes(TextItem, attrs);
-            }
         }
 
         /// <summary>