-using System;
+/*
+ * 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.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+
+using System;
using System.ComponentModel;
+using System.Diagnostics;
using Tizen.NUI.BaseComponents;
using Tizen.NUI.Components.Extension;
using Tizen.NUI.Accessibility; // To use AccessibilityManager
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()
}
/// <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)]
[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,
};
}
[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,
};
}
PivotPoint = NUI.PivotPoint.Center,
WidthResizePolicy = ResizePolicyType.FillToParent,
HeightResizePolicy = ResizePolicyType.FillToParent,
- AccessibilityHighlightable = false
+ AccessibilityHidden = true,
};
}
protected override void OnUpdate()
{
base.OnUpdate();
- UpdateUIContent();
-
Extension?.OnRelayout(this);
}
return true;
case PointStateType.Up:
{
- bool clicked = isPressed && IsEnabled;
+ if (!isPressed)
+ {
+ return false;
+ }
isPressed = false;
UpdateState();
}
- if (clicked)
- {
- ClickedEventArgs eventArgs = new ClickedEventArgs();
- OnClickedInternal(eventArgs);
- }
+ ClickedEventArgs eventArgs = new ClickedEventArgs();
+ OnClickedInternal(eventArgs, touch);
return true;
}
return base.HandleControlStateOnTouch(touch);
}
+ /// <inheritdoc/>
+ [EditorBrowsable(EditorBrowsableState.Never)]
+ protected override void OnEnabled(bool enabled)
+ {
+ base.OnEnabled(enabled);
+ UpdateState();
+ }
+
/// <summary>
/// Update Button State.
/// </summary>
[EditorBrowsable(EditorBrowsableState.Never)]
protected void UpdateState()
{
- if (!styleApplied) return;
+ if (styleApplying > 0) return;
ControlState sourceState = ControlState;
ControlState targetState;
}
/// <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>
}
/// <summary>
- /// Initilizes AT-SPI object.
+ /// Initializes AT-SPI object.
/// </summary>
[EditorBrowsable(EditorBrowsableState.Never)]
public override void OnInitialize()
{
base.OnInitialize();
- SetAccessibilityConstructor(Role.PushButton);
+
+ AccessibilityRole = Role.PushButton;
+ AccessibilityHighlightable = true;
+ EnableControlStatePropagation = true;
+
+ AccessibilityManager.Instance.SetAccessibilityAttribute(this, AccessibilityManager.AccessibilityAttribute.Trait, "Button");
+
+ buttonText = CreateText();
+ buttonIcon = CreateIcon();
+ LayoutItems();
+
+ Feedback = true;
+ }
+
+ /// <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/>
{
isPressed = statePressed;
}
+
+ if (IsSelectable)
+ {
+ var stateSelected = controlStateChangedInfo.CurrentState.Contains(ControlState.Selected);
+
+ if (IsSelected != stateSelected)
+ {
+ IsSelected = stateSelected;
+ }
+ }
}
/// <summary>
- /// It is hijack by using protected, style copy problem when class inherited from Button.
+ /// Put sub items (e.g. buttonText, buttonIcon) to the right place.
/// </summary>
- /// <since_tizen> 6 </since_tizen>
- private void Initialize()
+ [EditorBrowsable(EditorBrowsableState.Never)]
+ protected virtual void LayoutItems()
{
- AccessibilityHighlightable = true;
- EnableControlStatePropagation = true;
- UpdateState();
- LayoutDirectionChanged += OnLayoutDirectionChanged;
+ if (buttonIcon == null || buttonText == null)
+ {
+ return;
+ }
- AccessibilityManager.Instance.SetAccessibilityAttribute(this, AccessibilityManager.AccessibilityAttribute.Trait, "Button");
+ buttonIcon.Unparent();
+ buttonText.Unparent();
+ overlayImage?.Unparent();
- #if PROFILE_MOBILE
- Feedback = true;
- #endif
- }
+#pragma warning disable CA2000
+ Size2D cellPadding = String.IsNullOrEmpty(buttonText.Text) ? new Size2D(0, 0) : itemSpacing;
+#pragma warning restore CA2000
- private void UpdateUIContent()
- {
- MeasureText();
- LayoutChild();
+ if (IconRelativeOrientation == IconOrientation.Left)
+ {
+ Layout = new LinearLayout()
+ {
+ LinearOrientation = LinearLayout.Orientation.Horizontal,
+ HorizontalAlignment = itemHorizontalAlignment,
+ VerticalAlignment = itemVerticalAlignment,
+ CellPadding = cellPadding
+ };
- Sensitive = IsEnabled;
- }
+ Add(buttonIcon);
+ Add(buttonText);
+ }
+ else if (IconRelativeOrientation == IconOrientation.Right)
+ {
+ Layout = new LinearLayout()
+ {
+ LinearOrientation = LinearLayout.Orientation.Horizontal,
+ HorizontalAlignment = itemHorizontalAlignment,
+ VerticalAlignment = itemVerticalAlignment,
+ CellPadding = cellPadding
+ };
- private void OnLayoutDirectionChanged(object sender, LayoutDirectionChangedEventArgs e)
- {
- MeasureText();
- LayoutChild();
- }
+ Add(buttonText);
+ Add(buttonIcon);
+ }
+ else if (IconRelativeOrientation == IconOrientation.Top)
+ {
+ Layout = new LinearLayout()
+ {
+ LinearOrientation = LinearLayout.Orientation.Vertical,
+ HorizontalAlignment = itemHorizontalAlignment,
+ VerticalAlignment = itemVerticalAlignment,
+ CellPadding = cellPadding
+ };
- private void OnClickedInternal(ClickedEventArgs eventArgs)
- {
- Command?.Execute(CommandParameter);
- OnClicked(eventArgs);
- Extension?.OnClicked(this, eventArgs);
+ Add(buttonIcon);
+ Add(buttonText);
+ }
+ else if (IconRelativeOrientation == IconOrientation.Bottom)
+ {
+ Layout = new LinearLayout()
+ {
+ LinearOrientation = LinearLayout.Orientation.Vertical,
+ HorizontalAlignment = itemHorizontalAlignment,
+ VerticalAlignment = itemVerticalAlignment,
+ CellPadding = cellPadding
+ };
- ClickEventArgs nestedEventArgs = new ClickEventArgs();
- ClickEvent?.Invoke(this, nestedEventArgs);
- Clicked?.Invoke(this, eventArgs);
- }
+ Add(buttonText);
+ Add(buttonIcon);
+ }
- private void OnIconRelayout(object sender, EventArgs e)
- {
- MeasureText();
- LayoutChild();
+ if (overlayImage != null)
+ {
+ overlayImage.ExcludeLayouting = true;
+ Add(overlayImage);
+ }
}
- internal override bool OnAccessibilityActivated()
+ private void UpdateSizeAndSpacing()
{
- if (!IsEnabled)
+ if (size == null || buttonIcon == null || buttonText == null)
{
- return false;
+ return;
}
- // Touch Down
- isPressed = true;
- UpdateState();
+ LinearLayout layout = Layout as LinearLayout;
- // Touch Up
- bool clicked = isPressed && IsEnabled;
- isPressed = false;
+ if (layout == null)
+ {
+ return;
+ }
- if (IsSelectable)
+ 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)
{
- IsSelected = !IsSelected;
+ 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;
+ }
+ }
}
- else
+
+ 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))
{
- UpdateState();
+ buttonText.MaximumSize = new Size2D((int)Math.Max(size.Width - lengthWithoutText, Math.Max(buttonText.MinimumSize.Width, 1)), (int)size.Height);
}
+ }
- if (clicked)
+ 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)
{
- ClickedEventArgs eventArgs = new ClickedEventArgs();
- OnClickedInternal(eventArgs);
+ 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;
+ }
}
- return true;
+ OnClickedInternal(eventArgs);
}
+ private void OnClickedInternal(ClickedEventArgs eventArgs)
+ {
+ Command?.Execute(CommandParameter);
+ OnClicked(eventArgs);
+ Extension?.OnClicked(this, eventArgs);
+
+ ClickEventArgs nestedEventArgs = new ClickEventArgs();
+ ClickEvent?.Invoke(this, nestedEventArgs);
+ Clicked?.Invoke(this, eventArgs);
+ }
}
}