[NUI] Fix Button Theme not to set fixed size
[platform/core/csapi/tizenfx.git] / src / Tizen.NUI.Components / Controls / Button.Internal.cs
old mode 100755 (executable)
new mode 100644 (file)
index 295bffa..ea06e8f
@@ -1,5 +1,5 @@
 /*
- * Copyright(c) 2021 Samsung Electronics Co., Ltd.
+ * Copyright(c) 2022 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.
@@ -17,6 +17,7 @@
 
 using System;
 using System.ComponentModel;
+using System.Diagnostics;
 using Tizen.NUI.BaseComponents;
 using Tizen.NUI.Components.Extension;
 using Tizen.NUI.Accessibility; // To use AccessibilityManager
@@ -28,14 +29,15 @@ namespace Tizen.NUI.Components
         private ImageView overlayImage;
         private TextLabel buttonText;
         private ImageView buttonIcon;
+        private Vector2 size;
 
         private EventHandler<StateChangedEventArgs> stateChangeHandler;
 
         private bool isPressed = false;
-        private bool styleApplied = false;
+        internal int styleApplying = 0;
 
         /// <summary>
-        /// Get accessibility name.
+        /// Gets accessibility name.
         /// </summary>
         [EditorBrowsable(EditorBrowsableState.Never)]
         protected override string AccessibilityGetName()
@@ -44,15 +46,6 @@ namespace Tizen.NUI.Components
         }
 
         /// <summary>
-        /// Prevents from showing child widgets in AT-SPI tree.
-        /// </summary>
-        [EditorBrowsable(EditorBrowsableState.Never)]
-        protected override bool AccessibilityShouldReportZeroChildren()
-        {
-            return true;
-        }
-
-        /// <summary>
         /// The ButtonExtension instance that is injected by ButtonStyle.
         /// </summary>
         [EditorBrowsable(EditorBrowsableState.Never)]
@@ -65,16 +58,11 @@ namespace Tizen.NUI.Components
         [EditorBrowsable(EditorBrowsableState.Never)]
         protected virtual TextLabel CreateText()
         {
-            return new TextLabel
+            return new TextLabel(new TextLabelStyle())
             {
-                PositionUsesPivotPoint = true,
-                ParentOrigin = NUI.ParentOrigin.Center,
-                PivotPoint = NUI.PivotPoint.Center,
-                WidthResizePolicy = ResizePolicyType.FillToParent,
-                HeightResizePolicy = ResizePolicyType.FillToParent,
                 HorizontalAlignment = HorizontalAlignment.Center,
                 VerticalAlignment = VerticalAlignment.Center,
-                AccessibilityHighlightable = false
+                AccessibilityHidden = true,
             };
         }
 
@@ -85,12 +73,9 @@ namespace Tizen.NUI.Components
         [EditorBrowsable(EditorBrowsableState.Never)]
         protected virtual ImageView CreateIcon()
         {
-            return new ImageView
+            return new ImageView()
             {
-                PositionUsesPivotPoint = true,
-                ParentOrigin = NUI.ParentOrigin.Center,
-                PivotPoint = NUI.PivotPoint.Center,
-                AccessibilityHighlightable = false
+                AccessibilityHidden = true,
             };
         }
 
@@ -108,7 +93,7 @@ namespace Tizen.NUI.Components
                 PivotPoint = NUI.PivotPoint.Center,
                 WidthResizePolicy = ResizePolicyType.FillToParent,
                 HeightResizePolicy = ResizePolicyType.FillToParent,
-                AccessibilityHighlightable = false
+                AccessibilityHidden = true,
             };
         }
 
@@ -136,8 +121,6 @@ namespace Tizen.NUI.Components
         protected override void OnUpdate()
         {
             base.OnUpdate();
-            UpdateUIContent();
-
             Extension?.OnRelayout(this);
         }
 
@@ -165,7 +148,10 @@ namespace Tizen.NUI.Components
                     return true;
                 case PointStateType.Up:
                     {
-                        bool clicked = isPressed && IsEnabled;
+                        if (!isPressed)
+                        {
+                            return false;
+                        }
 
                         isPressed = false;
 
@@ -180,11 +166,8 @@ namespace Tizen.NUI.Components
                             UpdateState();
                         }
 
-                        if (clicked)
-                        {
-                            ClickedEventArgs eventArgs = new ClickedEventArgs();
-                            OnClickedInternal(eventArgs);
-                        }
+                        ClickedEventArgs eventArgs = new ClickedEventArgs();
+                        OnClickedInternal(eventArgs, touch);
 
                         return true;
                     }
@@ -194,6 +177,14 @@ namespace Tizen.NUI.Components
             return base.HandleControlStateOnTouch(touch);
         }
 
+        /// <inheritdoc/>
+        [EditorBrowsable(EditorBrowsableState.Never)]
+        protected override void OnEnabled(bool enabled)
+        {
+            base.OnEnabled(enabled);
+            UpdateState();
+        }
+
         /// <summary>
         /// Update Button State.
         /// </summary>
@@ -202,7 +193,7 @@ namespace Tizen.NUI.Components
         [EditorBrowsable(EditorBrowsableState.Never)]
         protected void UpdateState()
         {
-            if (!styleApplied) return;
+            if (styleApplying > 0) return;
 
             ControlState sourceState = ControlState;
             ControlState targetState;
@@ -236,157 +227,6 @@ namespace Tizen.NUI.Components
         }
 
         /// <summary>
-        /// Measure text, it can be override.
-        /// </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 virtual void MeasureText()
-        {
-            if (buttonIcon == null || buttonText == null)
-            {
-                return;
-            }
-            buttonText.WidthResizePolicy = ResizePolicyType.Fixed;
-            buttonText.HeightResizePolicy = ResizePolicyType.Fixed;
-
-            var textPadding = TextPadding;
-            int textPaddingStart = textPadding.Start;
-            int textPaddingEnd = textPadding.End;
-            int textPaddingTop = textPadding.Top;
-            int textPaddingBottom = textPadding.Bottom;
-
-            var iconPadding = IconPadding;
-            int iconPaddingStart = iconPadding.Start;
-            int iconPaddingEnd = iconPadding.End;
-            int iconPaddingTop = iconPadding.Top;
-            int iconPaddingBottom = iconPadding.Bottom;
-
-            if (IconRelativeOrientation == IconOrientation.Top || IconRelativeOrientation == IconOrientation.Bottom)
-            {
-                buttonText.SizeWidth = SizeWidth - textPaddingStart - textPaddingEnd;
-                buttonText.SizeHeight = SizeHeight - textPaddingTop - textPaddingBottom - iconPaddingTop - iconPaddingBottom - buttonIcon.SizeHeight;
-            }
-            else
-            {
-                buttonText.SizeWidth = SizeWidth - textPaddingStart - textPaddingEnd - iconPaddingStart - iconPaddingEnd - buttonIcon.SizeWidth;
-                buttonText.SizeHeight = SizeHeight - textPaddingTop - textPaddingBottom;
-            }
-        }
-
-        /// <summary>
-        /// Layout child, it can be override.
-        /// </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 virtual void LayoutChild()
-        {
-            if (buttonIcon == null || buttonText == null)
-            {
-                return;
-            }
-
-            var textPadding = TextPadding;
-            int textPaddingStart = textPadding.Start;
-            int textPaddingEnd = textPadding.End;
-            int textPaddingTop = textPadding.Top;
-            int textPaddingBottom = textPadding.Bottom;
-
-            var iconPadding = IconPadding;
-            int iconPaddingStart = iconPadding.Start;
-            int iconPaddingEnd = iconPadding.End;
-            int iconPaddingTop = iconPadding.Top;
-            int iconPaddingBottom = iconPadding.Bottom;
-
-            switch (IconRelativeOrientation)
-            {
-                case IconOrientation.Top:
-                    buttonIcon.PositionUsesPivotPoint = true;
-                    buttonIcon.ParentOrigin = NUI.ParentOrigin.TopCenter;
-                    buttonIcon.PivotPoint = NUI.PivotPoint.TopCenter;
-                    buttonIcon.Position2D = new Position2D(0, iconPaddingTop);
-
-                    buttonText.PositionUsesPivotPoint = true;
-                    buttonText.ParentOrigin = NUI.ParentOrigin.BottomCenter;
-                    buttonText.PivotPoint = NUI.PivotPoint.BottomCenter;
-                    buttonText.Position2D = new Position2D(0, -textPaddingBottom);
-                    break;
-                case IconOrientation.Bottom:
-                    buttonIcon.PositionUsesPivotPoint = true;
-                    buttonIcon.ParentOrigin = NUI.ParentOrigin.BottomCenter;
-                    buttonIcon.PivotPoint = NUI.PivotPoint.BottomCenter;
-                    buttonIcon.Position2D = new Position2D(0, -iconPaddingBottom);
-
-                    buttonText.PositionUsesPivotPoint = true;
-                    buttonText.ParentOrigin = NUI.ParentOrigin.TopCenter;
-                    buttonText.PivotPoint = NUI.PivotPoint.TopCenter;
-                    buttonText.Position2D = new Position2D(0, textPaddingTop);
-                    break;
-                case IconOrientation.Left:
-                    if (LayoutDirection == ViewLayoutDirectionType.LTR)
-                    {
-                        buttonIcon.PositionUsesPivotPoint = true;
-                        buttonIcon.ParentOrigin = NUI.ParentOrigin.CenterLeft;
-                        buttonIcon.PivotPoint = NUI.PivotPoint.CenterLeft;
-                        buttonIcon.Position2D = new Position2D(iconPaddingStart, 0);
-
-                        buttonText.PositionUsesPivotPoint = true;
-                        buttonText.ParentOrigin = NUI.ParentOrigin.CenterRight;
-                        buttonText.PivotPoint = NUI.PivotPoint.CenterRight;
-                        buttonText.Position2D = new Position2D(-textPaddingEnd, 0);
-                    }
-                    else
-                    {
-                        buttonIcon.PositionUsesPivotPoint = true;
-                        buttonIcon.ParentOrigin = NUI.ParentOrigin.CenterRight;
-                        buttonIcon.PivotPoint = NUI.PivotPoint.CenterRight;
-                        buttonIcon.Position2D = new Position2D(-iconPaddingStart, 0);
-
-                        buttonText.PositionUsesPivotPoint = true;
-                        buttonText.ParentOrigin = NUI.ParentOrigin.CenterLeft;
-                        buttonText.PivotPoint = NUI.PivotPoint.CenterLeft;
-                        buttonText.Position2D = new Position2D(textPaddingEnd, 0);
-                    }
-
-                    break;
-                case IconOrientation.Right:
-                    if (LayoutDirection == ViewLayoutDirectionType.RTL)
-                    {
-                        buttonIcon.PositionUsesPivotPoint = true;
-                        buttonIcon.ParentOrigin = NUI.ParentOrigin.CenterLeft;
-                        buttonIcon.PivotPoint = NUI.PivotPoint.CenterLeft;
-                        buttonIcon.Position2D = new Position2D(iconPaddingEnd, 0);
-
-                        buttonText.PositionUsesPivotPoint = true;
-                        buttonText.ParentOrigin = NUI.ParentOrigin.CenterRight;
-                        buttonText.PivotPoint = NUI.PivotPoint.CenterRight;
-                        buttonText.Position2D = new Position2D(-textPaddingStart, 0);
-                    }
-                    else
-                    {
-                        buttonIcon.PositionUsesPivotPoint = true;
-                        buttonIcon.ParentOrigin = NUI.ParentOrigin.CenterRight;
-                        buttonIcon.PivotPoint = NUI.PivotPoint.CenterRight;
-                        buttonIcon.Position2D = new Position2D(-iconPaddingEnd, 0);
-
-                        buttonText.PositionUsesPivotPoint = true;
-                        buttonText.ParentOrigin = NUI.ParentOrigin.CenterLeft;
-                        buttonText.PivotPoint = NUI.PivotPoint.CenterLeft;
-                        buttonText.Position2D = new Position2D(textPaddingStart, 0);
-                    }
-                    break;
-                default:
-                    break;
-            }
-            if (string.IsNullOrEmpty(buttonText.Text))
-            {
-                buttonIcon.ParentOrigin = NUI.ParentOrigin.Center;
-                buttonIcon.PivotPoint = NUI.PivotPoint.Center;
-            }
-        }
-
-        /// <summary>
         /// Dispose Button and all children on it.
         /// </summary>
         /// <param name="type">Dispose type.</param>
@@ -426,17 +266,34 @@ namespace Tizen.NUI.Components
         public override void OnInitialize()
         {
             base.OnInitialize();
-            SetAccessibilityConstructor(Role.PushButton);
 
+            AccessibilityRole = Role.PushButton;
             AccessibilityHighlightable = true;
             EnableControlStatePropagation = true;
-            LayoutDirectionChanged += OnLayoutDirectionChanged;
 
             AccessibilityManager.Instance.SetAccessibilityAttribute(this, AccessibilityManager.AccessibilityAttribute.Trait, "Button");
 
-#if PROFILE_MOBILE
+            buttonText = CreateText();
+            buttonIcon = CreateIcon();
+            LayoutItems();
+
             Feedback = true;
-#endif
+        }
+
+        /// <inheritdoc/>
+        [EditorBrowsable(EditorBrowsableState.Never)]
+        public override void OnRelayout(Vector2 size, RelayoutContainer container)
+        {
+            if (size == null) return;
+
+            if (size.Equals(this.size))
+            {
+                return;
+            }
+
+            this.size = new Vector2(size);
+
+            UpdateSizeAndSpacing();
         }
 
         /// <inheritdoc/>
@@ -458,20 +315,159 @@ namespace Tizen.NUI.Components
             {
                 isPressed = statePressed;
             }
+
+            if (IsSelectable)
+            {
+                var stateSelected = controlStateChangedInfo.CurrentState.Contains(ControlState.Selected);
+
+                if (IsSelected != stateSelected)
+                {
+                    IsSelected = stateSelected;
+                }
+            }
         }
 
-        private void UpdateUIContent()
+        /// <summary>
+        /// Put sub items (e.g. buttonText, buttonIcon) to the right place.
+        /// </summary>
+        [EditorBrowsable(EditorBrowsableState.Never)]
+        protected virtual void LayoutItems()
         {
-            MeasureText();
-            LayoutChild();
+            if (buttonIcon == null || buttonText == null)
+            {
+                return;
+            }
 
-            Sensitive = IsEnabled;
+            buttonIcon.Unparent();
+            buttonText.Unparent();
+            overlayImage?.Unparent();
+
+#pragma warning disable CA2000
+            Size2D cellPadding = String.IsNullOrEmpty(buttonText.Text) ? new Size2D(0, 0) : itemSpacing;
+#pragma warning restore CA2000
+
+            if (IconRelativeOrientation == IconOrientation.Left)
+            {
+                Layout = new LinearLayout()
+                {
+                    LinearOrientation = LinearLayout.Orientation.Horizontal,
+                    HorizontalAlignment = itemHorizontalAlignment,
+                    VerticalAlignment = itemVerticalAlignment,
+                    CellPadding = cellPadding
+                };
+
+                Add(buttonIcon);
+                Add(buttonText);
+            }
+            else if (IconRelativeOrientation == IconOrientation.Right)
+            {
+                Layout = new LinearLayout()
+                {
+                    LinearOrientation = LinearLayout.Orientation.Horizontal,
+                    HorizontalAlignment = itemHorizontalAlignment,
+                    VerticalAlignment = itemVerticalAlignment,
+                    CellPadding = cellPadding
+                };
+
+                Add(buttonText);
+                Add(buttonIcon);
+            }
+            else if (IconRelativeOrientation == IconOrientation.Top)
+            {
+                Layout = new LinearLayout()
+                {
+                    LinearOrientation = LinearLayout.Orientation.Vertical,
+                    HorizontalAlignment = itemHorizontalAlignment,
+                    VerticalAlignment = itemVerticalAlignment,
+                    CellPadding = cellPadding
+                };
+
+                Add(buttonIcon);
+                Add(buttonText);
+            }
+            else if (IconRelativeOrientation == IconOrientation.Bottom)
+            {
+                Layout = new LinearLayout()
+                {
+                    LinearOrientation = LinearLayout.Orientation.Vertical,
+                    HorizontalAlignment = itemHorizontalAlignment,
+                    VerticalAlignment = itemVerticalAlignment,
+                    CellPadding = cellPadding
+                };
+
+                Add(buttonText);
+                Add(buttonIcon);
+            }
+
+            if (overlayImage != null)
+            {
+                overlayImage.ExcludeLayouting = true;
+                Add(overlayImage);
+            }
         }
 
-        private void OnLayoutDirectionChanged(object sender, LayoutDirectionChangedEventArgs e)
+        private void UpdateSizeAndSpacing()
         {
-            MeasureText();
-            LayoutChild();
+            if (size == null || buttonIcon == null || buttonText == null)
+            {
+                return;
+            }
+
+            LinearLayout layout = Layout as LinearLayout;
+
+            if (layout == null)
+            {
+                return;
+            }
+
+            float lengthWithoutText = 0;
+            Size2D cellPadding = null;
+            Extents iconMargin = buttonIcon.Margin ?? new Extents(0);
+            Extents textMargin = buttonText.Margin ?? new Extents(0);
+
+            if (buttonIcon.Size.Width != 0 && buttonIcon.Size.Height != 0)
+            {
+                lengthWithoutText = buttonIcon.Size.Width;
+
+                if (!String.IsNullOrEmpty(buttonText.Text))
+                {
+                    cellPadding = itemSpacing;
+
+                    if (iconRelativeOrientation == IconOrientation.Left || iconRelativeOrientation == IconOrientation.Right)
+                    {
+                        lengthWithoutText += (itemSpacing?.Width ?? 0) + iconMargin.Start + iconMargin.End + textMargin.Start + textMargin.End + Padding.Start + Padding.End;
+                    }
+                    else
+                    {
+                        lengthWithoutText += (itemSpacing?.Height ?? 0) + iconMargin.Top + iconMargin.Bottom + textMargin.Top + textMargin.Bottom + Padding.Top + Padding.Bottom;
+                    }
+                }
+            }
+
+            layout.CellPadding = cellPadding ?? new Size2D(0, 0);
+
+            // If the button has fixed width and the text is not empty, the text should not exceed button boundary.
+            if (WidthSpecification != LayoutParamPolicies.WrapContent && !String.IsNullOrEmpty(buttonText.Text))
+            {
+                buttonText.MaximumSize = new Size2D((int)Math.Max(size.Width - lengthWithoutText, Math.Max(buttonText.MinimumSize.Width, 1)), (int)size.Height);
+            }
+        }
+
+        private void OnClickedInternal(ClickedEventArgs eventArgs, Touch touch)
+        {
+            // If GrabTouchAfterLeave is true, Up will result in Finished rather than Interrupted even if it is out of the button area.
+            // So, it is necessary to check whether it is Up in the button area.
+            if (GrabTouchAfterLeave == true)
+            {
+                Vector2 localPosition = touch.GetLocalPosition(0);
+                if ((localPosition != null && Size != null &&
+                    0 <= localPosition.X && localPosition.X <= Size.Width &&
+                    0 <= localPosition.Y && localPosition.Y <= Size.Height) == false)
+                {
+                    return;
+                }
+            }
+            OnClickedInternal(eventArgs);
         }
 
         private void OnClickedInternal(ClickedEventArgs eventArgs)
@@ -484,29 +480,5 @@ namespace Tizen.NUI.Components
             ClickEvent?.Invoke(this, nestedEventArgs);
             Clicked?.Invoke(this, eventArgs);
         }
-
-        private void OnIconRelayout(object sender, EventArgs e)
-        {
-            MeasureText();
-            LayoutChild();
-        }
-
-        internal override bool OnAccessibilityActivated()
-        {
-            using (var key = new Key())
-            {
-                key.State = Key.StateType.Down;
-                key.KeyPressedName = "Return";
-
-                // Touch Down
-                OnKey(key);
-
-                // Touch Up
-                key.State = Key.StateType.Up;
-                OnKey(key);
-            }
-
-            return true;
-        }
     }
 }