[NUI] Fix button layout when resize policy is FillToParent and etc. (#2809)
[platform/core/csapi/tizenfx.git] / src / Tizen.NUI.Components / Controls / Button.cs
index 3be84d7..96021fb 100755 (executable)
@@ -1,5 +1,5 @@
 /*
- * Copyright(c) 2020 Samsung Electronics Co., Ltd.
+ * Copyright(c) 2021 Samsung Electronics Co., Ltd.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -16,9 +16,9 @@
  */
 using System;
 using System.ComponentModel;
+using System.Diagnostics;
 using Tizen.NUI.BaseComponents;
 using Tizen.NUI.Binding;
-using Tizen.NUI.Components.Extension;
 using Tizen.NUI.Accessibility;
 
 namespace Tizen.NUI.Components
@@ -58,7 +58,7 @@ namespace Tizen.NUI.Components
             if (instance.iconRelativeOrientation != newIconOrientation)
             {
                 instance.iconRelativeOrientation = newIconOrientation;
-                instance.UpdateUIContent();
+                instance.LayoutItems();
             }
         },
         defaultValueCreator: (bindable) => ((Button)bindable).iconRelativeOrientation
@@ -75,12 +75,7 @@ namespace Tizen.NUI.Components
                 if (instance.isEnabled != newEnabled)
                 {
                     instance.isEnabled = newEnabled;
-
-                    if (instance.buttonStyle != null)
-                    {
-                        instance.buttonStyle.IsEnabled = newEnabled;
-                    }
-
+                    instance.Sensitive = newEnabled;
                     instance.UpdateState();
                 }
             }
@@ -98,11 +93,6 @@ namespace Tizen.NUI.Components
                 {
                     instance.isSelected = newSelected;
 
-                    if (instance.buttonStyle != null)
-                    {
-                        instance.buttonStyle.IsSelected = newSelected;
-                    }
-
                     if (instance.isSelectable)
                     {
                         instance.UpdateState();
@@ -126,12 +116,6 @@ namespace Tizen.NUI.Components
                 if (instance.isSelectable != newSelectable)
                 {
                     instance.isSelectable = newSelectable;
-
-                    if (instance.buttonStyle != null)
-                    {
-                        instance.buttonStyle.IsSelectable = newSelectable;
-                    }
-
                     instance.UpdateState();
                 }
             }
@@ -143,27 +127,53 @@ namespace Tizen.NUI.Components
         public static readonly BindableProperty IconPaddingProperty = BindableProperty.Create(nameof(IconPadding), typeof(Extents), typeof(Button), null, propertyChanged: (bindable, oldValue, newValue) =>
         {
             var instance = (Button)bindable;
-            instance.iconPadding = (Extents)((Extents)newValue).Clone();
-            instance.UpdateUIContent();
+            if (instance.buttonIcon == null)
+            {
+                return;
+            }
+            instance.buttonIcon.Padding = (Extents)newValue;
         },
-        defaultValueCreator: (bindable) => ((Button)bindable).iconPadding);
+        defaultValueCreator: (bindable) => ((Button)bindable).buttonIcon?.Padding);
 
         /// 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 TextPaddingProperty = BindableProperty.Create(nameof(TextPadding), typeof(Extents), typeof(Button), null, propertyChanged: (bindable, oldValue, newValue) =>
         {
             var instance = (Button)bindable;
-            instance.textPadding = (Extents)((Extents)newValue).Clone();
-            instance.UpdateUIContent();
+            if (instance.buttonText == null)
+            {
+                return;
+            }
+            instance.buttonText.Padding = (Extents)newValue;
         },
-        defaultValueCreator: (bindable) => ((Button)bindable).textPadding);
+        defaultValueCreator: (bindable) => ((Button)bindable).buttonText?.Padding);
 
-        private IconOrientation? iconRelativeOrientation;
+        /// <summary> The bindable property of ItemAlignment. </summary>
+        [EditorBrowsable(EditorBrowsableState.Never)]
+        internal static readonly BindableProperty ItemAlignmentProperty = BindableProperty.Create(nameof(ItemAlignment), typeof(LinearLayout.Alignment), typeof(Button), LinearLayout.Alignment.Center, propertyChanged: (bindable, oldValue, newValue) =>
+        {
+            var instance = (Button)bindable;
+            instance.itemAlignment = (LinearLayout.Alignment)newValue;
+            instance.LayoutItems();
+        },
+        defaultValueCreator: (bindable) => ((Button)bindable).itemAlignment);
+
+        /// <summary> The bindable property of ItemSpacing. </summary>
+        [EditorBrowsable(EditorBrowsableState.Never)]
+        internal static readonly BindableProperty ItemSpacingProperty = BindableProperty.Create(nameof(ItemSpacing), typeof(Size2D), typeof(Button), null, propertyChanged: (bindable, oldValue, newValue) =>
+        {
+            var instance = (Button)bindable;
+            instance.itemSpacing = (Size2D)newValue;
+            instance.UpdateSizeAndSpacing();
+        },
+        defaultValueCreator: (bindable) => ((Button)bindable).itemSpacing);
+
+        private IconOrientation? iconRelativeOrientation = IconOrientation.Left;
         private bool isSelected = false;
         private bool isSelectable = false;
         private bool isEnabled = true;
-        private Extents iconPadding;
-        private Extents textPadding;
+        private Size2D itemSpacing;
+        private LinearLayout.Alignment itemAlignment;
 
         static Button() { }
 
@@ -173,7 +183,6 @@ namespace Tizen.NUI.Components
         /// <since_tizen> 6 </since_tizen>
         public Button() : base()
         {
-            Initialize();
         }
 
         /// <summary>
@@ -183,7 +192,6 @@ namespace Tizen.NUI.Components
         /// <since_tizen> 8 </since_tizen>
         public Button(string style) : base(style)
         {
-            Initialize();
         }
 
         /// <summary>
@@ -193,7 +201,6 @@ namespace Tizen.NUI.Components
         /// <since_tizen> 8 </since_tizen>
         public Button(ButtonStyle buttonStyle) : base(buttonStyle)
         {
-            Initialize();
         }
 
         /// <summary>
@@ -203,6 +210,7 @@ namespace Tizen.NUI.Components
         protected override AccessibilityStates AccessibilityCalculateStates()
         {
             var states = base.AccessibilityCalculateStates();
+            states.Set(AccessibilityState.Checked, this.IsSelected);
             states.Set(AccessibilityState.Enabled, this.IsEnabled);
             return states;
         }
@@ -271,23 +279,7 @@ namespace Tizen.NUI.Components
         /// <since_tizen> 8 </since_tizen>
         public ImageView Icon
         {
-            get
-            {
-                if (null == buttonIcon)
-                {
-                    buttonIcon = CreateIcon();
-                    if (null != Extension)
-                    {
-                        buttonIcon = Extension.OnCreateIcon(this, buttonIcon);
-                    }
-                    if (null != buttonIcon)
-                    {
-                        Add(buttonIcon);
-                        buttonIcon.Relayout += OnIconRelayout;
-                    }
-                }
-                return buttonIcon;
-            }
+            get => buttonIcon;
             internal set
             {
                 buttonIcon = value;
@@ -311,6 +303,7 @@ namespace Tizen.NUI.Components
                     }
                     if (null != overlayImage)
                     {
+                        overlayImage.ExcludeLayouting = true;
                         Add(overlayImage);
                     }
                 }
@@ -328,22 +321,7 @@ namespace Tizen.NUI.Components
         /// <since_tizen> 8 </since_tizen>
         public TextLabel TextLabel
         {
-            get
-            {
-                if (null == buttonText)
-                {
-                    buttonText = CreateText();
-                    if (null != Extension)
-                    {
-                        buttonText = Extension.OnCreateText(this, buttonText);
-                    }
-                    if (null != buttonText)
-                    {
-                        Add(buttonText);
-                    }
-                }
-                return buttonText;
-            }
+            get => buttonText;
             internal set
             {
                 buttonText = value;
@@ -352,25 +330,13 @@ namespace Tizen.NUI.Components
         }
 
         /// <summary>
-        /// Return a copied Style instance of Button
+        /// Return currently applied style.
         /// </summary>
         /// <remarks>
-        /// It returns copied Style instance and changing it does not effect to the Button.
-        /// Style setting is possible by using constructor or the function of ApplyStyle(ViewStyle viewStyle)
+        /// Modifying contents in style may cause unexpected behaviour.
         /// </remarks>
         /// <since_tizen> 8 </since_tizen>
-        public new ButtonStyle Style
-        {
-            get
-            {
-                var result = (ButtonStyle)ViewStyle.Clone();
-                result.CopyPropertiesFromView(this);
-                result.Text.CopyPropertiesFromView(TextLabel);
-                result.Icon.CopyPropertiesFromView(Icon);
-                result.Overlay.CopyPropertiesFromView(OverlayImage);
-                return result;
-            }
-        }
+        public ButtonStyle Style => (ButtonStyle)(ViewStyle as ButtonStyle)?.Clone();
 
         /// <summary>
         /// The text of Button.
@@ -528,7 +494,7 @@ namespace Tizen.NUI.Components
         }
 
         /// <summary>
-        /// Translateable text string selector in Button.
+        /// Translatable text string selector in Button.
         /// Getter returns copied selector value if exist, null otherwise.
         /// </summary>
         /// <exception cref="NullReferenceException">Thrown when setting null value.</exception>
@@ -664,7 +630,7 @@ namespace Tizen.NUI.Components
         }
 
         /// <summary>
-        /// Icon padding in Button, work only when show icon and text.
+        /// Icon padding in Button. It is shortcut of Icon.Padding.
         /// </summary>
         /// <since_tizen> 6 </since_tizen>
         public Extents IconPadding
@@ -674,7 +640,7 @@ namespace Tizen.NUI.Components
         }
 
         /// <summary>
-        /// Text padding in Button, work only when show icon and text.
+        /// Text padding in Button. It is shortcut of TextLabel.Padding.
         /// </summary>
         /// <since_tizen> 6 </since_tizen>
         public Extents TextPadding
@@ -683,7 +649,27 @@ namespace Tizen.NUI.Components
             set => SetValue(TextPaddingProperty, value);
         }
 
-        private ButtonStyle buttonStyle => ViewStyle as ButtonStyle;
+        /// <summary>
+        /// The item (text or icon or both) alignment.
+        /// </summary>
+        [EditorBrowsable(EditorBrowsableState.Never)]
+        public LinearLayout.Alignment ItemAlignment
+        {
+            get => (LinearLayout.Alignment)GetValue(ItemAlignmentProperty);
+            set => SetValue(ItemAlignmentProperty, value);
+        }
+
+        /// <summary>
+        /// The space between icon and text.
+        /// The value is applied when there exist icon and text both.
+        /// The width value is used when the items are arranged horizontally. Otherwise, the height value is used.
+        /// </summary>
+        [EditorBrowsable(EditorBrowsableState.Never)]
+        public Size2D ItemSpacing
+        {
+            get => (Size2D)GetValue(ItemSpacingProperty);
+            set => SetValue(ItemSpacingProperty, value);
+        }
 
         /// <summary>
         /// Called after a key event is received by the view that has had its focus set.
@@ -775,30 +761,45 @@ namespace Tizen.NUI.Components
         /// <since_tizen> 8 </since_tizen>
         public override void ApplyStyle(ViewStyle viewStyle)
         {
+            Debug.Assert(buttonIcon != null && buttonText != null);
+
             styleApplied = false;
 
             base.ApplyStyle(viewStyle);
 
-            if (null != buttonStyle)
+            if (viewStyle is ButtonStyle buttonStyle)
             {
                 Extension = buttonStyle.CreateExtension();
+
                 if (buttonStyle.Overlay != null)
-                {
+                {   
                     OverlayImage?.ApplyStyle(buttonStyle.Overlay);
                 }
 
+                if (Extension != null)
+                {
+                    buttonIcon.Unparent();
+                    buttonIcon = Extension.OnCreateIcon(this, buttonIcon);
+
+                    buttonText.Unparent();
+                    buttonText = Extension.OnCreateText(this, buttonText);
+
+                    LayoutItems();
+                }
+
                 if (buttonStyle.Text != null)
                 {
-                    TextLabel?.ApplyStyle(buttonStyle.Text);
+                    buttonText.ApplyStyle(buttonStyle.Text);
                 }
 
                 if (buttonStyle.Icon != null)
                 {
-                    Icon?.ApplyStyle(buttonStyle.Icon);
+                    buttonIcon.ApplyStyle(buttonStyle.Icon);
                 }
             }
 
             styleApplied = true;
+            UpdateState();
         }
 
         /// <summary>