-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
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>
+ /// Gets accessibility name.
+ /// </summary>
+ [EditorBrowsable(EditorBrowsableState.Never)]
+ protected override string AccessibilityGetName()
+ {
+ return Text;
+ }
/// <summary>
/// The ButtonExtension instance that is injected by ButtonStyle.
[EditorBrowsable(EditorBrowsableState.Never)]
protected virtual TextLabel CreateText()
{
- return new TextLabel();
+ return new TextLabel(new TextLabelStyle())
+ {
+ HorizontalAlignment = HorizontalAlignment.Center,
+ VerticalAlignment = VerticalAlignment.Center,
+ AccessibilityHidden = true,
+ };
}
/// <summary>
[EditorBrowsable(EditorBrowsableState.Never)]
protected virtual ImageView CreateIcon()
{
- return new ImageView();
+ return new ImageView()
+ {
+ AccessibilityHidden = true,
+ };
}
/// <summary>
[EditorBrowsable(EditorBrowsableState.Never)]
protected virtual ImageView CreateOverlayImage()
{
- return new ImageView();
+ return new ImageView
+ {
+ PositionUsesPivotPoint = true,
+ ParentOrigin = NUI.ParentOrigin.Center,
+ PivotPoint = NUI.PivotPoint.Center,
+ WidthResizePolicy = ResizePolicyType.FillToParent,
+ HeightResizePolicy = ResizePolicyType.FillToParent,
+ AccessibilityHidden = true,
+ };
}
/// <summary>
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>
- /// <param name="touchInfo">The touch information in case the state has changed by touching.</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)]
protected void UpdateState()
{
- if (!styleApplied) return;
+ if (styleApplying > 0) return;
ControlState sourceState = ControlState;
ControlState targetState;
}
/// <summary>
- /// Measure text, it can be override.
+ /// Dispose Button and all children on it.
/// </summary>
+ /// <param name="type">Dispose type.</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)]
- protected virtual void MeasureText()
+ protected override void Dispose(DisposeTypes type)
{
- if (Icon == null || TextLabel == null)
+ if (disposed)
{
return;
}
- TextLabel.WidthResizePolicy = ResizePolicyType.Fixed;
- TextLabel.HeightResizePolicy = ResizePolicyType.Fixed;
- int textPaddingStart = buttonStyle.TextPadding.Start;
- int textPaddingEnd = buttonStyle.TextPadding.End;
- int textPaddingTop = buttonStyle.TextPadding.Top;
- int textPaddingBottom = buttonStyle.TextPadding.Bottom;
-
- int iconPaddingStart = buttonStyle.IconPadding.Start;
- int iconPaddingEnd = buttonStyle.IconPadding.End;
- int iconPaddingTop = buttonStyle.IconPadding.Top;
- int iconPaddingBottom = buttonStyle.IconPadding.Bottom;
-
- if (IconRelativeOrientation == IconOrientation.Top || IconRelativeOrientation == IconOrientation.Bottom)
- {
- TextLabel.SizeWidth = SizeWidth - textPaddingStart - textPaddingEnd;
- TextLabel.SizeHeight = SizeHeight - textPaddingTop - textPaddingBottom - iconPaddingTop - iconPaddingBottom - Icon.SizeHeight;
- }
- else
+
+ if (type == DisposeTypes.Explicit)
{
- TextLabel.SizeWidth = SizeWidth - textPaddingStart - textPaddingEnd - iconPaddingStart - iconPaddingEnd - Icon.SizeWidth;
- TextLabel.SizeHeight = SizeHeight - textPaddingTop - textPaddingBottom;
+ Extension?.OnDispose(this);
+
+ if (buttonIcon != null)
+ {
+ Utility.Dispose(buttonIcon);
+ }
+ if (buttonText != null)
+ {
+ Utility.Dispose(buttonText);
+ }
+ if (overlayImage != null)
+ {
+ Utility.Dispose(overlayImage);
+ }
}
+
+ base.Dispose(type);
}
/// <summary>
- /// Layout child, it can be override.
+ /// Initializes AT-SPI object.
/// </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()
+ public override void OnInitialize()
{
- if (Icon == null || TextLabel == null)
- {
- return;
- }
-
- var buttonIcon = Icon;
- var buttonText = TextLabel;
+ base.OnInitialize();
- int textPaddingStart = buttonStyle.TextPadding.Start;
- int textPaddingEnd = buttonStyle.TextPadding.End;
- int textPaddingTop = buttonStyle.TextPadding.Top;
- int textPaddingBottom = buttonStyle.TextPadding.Bottom;
+ AccessibilityRole = Role.PushButton;
+ AccessibilityHighlightable = true;
+ EnableControlStatePropagation = true;
- int iconPaddingStart = buttonStyle.IconPadding.Start;
- int iconPaddingEnd = buttonStyle.IconPadding.End;
- int iconPaddingTop = buttonStyle.IconPadding.Top;
- int iconPaddingBottom = buttonStyle.IconPadding.Bottom;
+ AccessibilityManager.Instance.SetAccessibilityAttribute(this, AccessibilityManager.AccessibilityAttribute.Trait, "Button");
- 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);
- }
+ buttonText = CreateText();
+ buttonIcon = CreateIcon();
+ LayoutItems();
- 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;
- }
+ Feedback = true;
}
- /// <summary>
- /// Theme change callback when theme is changed, this callback will be trigger.
- /// </summary>
- /// <param name="sender">The sender</param>
- /// <param name="e">The event data</param>
+ /// <inheritdoc/>
[EditorBrowsable(EditorBrowsableState.Never)]
- protected override void OnThemeChangedEvent(object sender, StyleManager.ThemeChangeEventArgs e)
+ public override void OnRelayout(Vector2 size, RelayoutContainer container)
{
- ButtonStyle buttonStyle = StyleManager.Instance.GetViewStyle(StyleName) as ButtonStyle;
- if (buttonStyle != null)
- {
- ApplyStyle(buttonStyle);
- UpdateUIContent();
- }
- }
+ if (size == null) return;
- /// <summary>
- /// Dispose Button and all children on it.
- /// </summary>
- /// <param name="type">Dispose type.</param>
- /// <since_tizen> 6 </since_tizen>
- protected override void Dispose(DisposeTypes type)
- {
- if (disposed)
+ if (size.Equals(this.size))
{
return;
}
- if (type == DisposeTypes.Explicit)
- {
- Extension?.OnDispose(this);
+ this.size = new Vector2(size);
- if (Icon != null)
- {
- Utility.Dispose(Icon);
- }
- if (TextLabel != null)
- {
- Utility.Dispose(TextLabel);
- }
- if (OverlayImage != null)
- {
- Utility.Dispose(OverlayImage);
- }
- }
-
- base.Dispose(type);
+ 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()
{
- EnableControlStatePropagation = true;
- UpdateState();
- LayoutDirectionChanged += OnLayoutDirectionChanged;
- }
+ if (buttonIcon == null || buttonText == null)
+ {
+ return;
+ }
- private void UpdateUIContent()
- {
- MeasureText();
- LayoutChild();
+ buttonIcon.Unparent();
+ buttonText.Unparent();
+ overlayImage?.Unparent();
- Sensitive = IsEnabled;
- }
+#pragma warning disable CA2000
+ Size2D cellPadding = String.IsNullOrEmpty(buttonText.Text) ? new Size2D(0, 0) : itemSpacing;
+#pragma warning restore CA2000
- private void OnLayoutDirectionChanged(object sender, LayoutDirectionChangedEventArgs e)
- {
- MeasureText();
- LayoutChild();
- }
+ if (IconRelativeOrientation == IconOrientation.Left)
+ {
+ Layout = new LinearLayout()
+ {
+ LinearOrientation = LinearLayout.Orientation.Horizontal,
+ 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.Right)
+ {
+ Layout = new LinearLayout()
+ {
+ LinearOrientation = LinearLayout.Orientation.Horizontal,
+ HorizontalAlignment = itemHorizontalAlignment,
+ VerticalAlignment = itemVerticalAlignment,
+ CellPadding = cellPadding
+ };
- ClickEventArgs nestedEventArgs = new ClickEventArgs();
- ClickEvent?.Invoke(this, nestedEventArgs);
- Clicked?.Invoke(this, eventArgs);
- }
+ Add(buttonText);
+ Add(buttonIcon);
+ }
+ else if (IconRelativeOrientation == IconOrientation.Top)
+ {
+ Layout = new LinearLayout()
+ {
+ LinearOrientation = LinearLayout.Orientation.Vertical,
+ HorizontalAlignment = itemHorizontalAlignment,
+ VerticalAlignment = itemVerticalAlignment,
+ CellPadding = cellPadding
+ };
- private void OnIconRelayout(object sender, EventArgs e)
- {
- MeasureText();
- LayoutChild();
+ 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);
+ }
}
- 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);
+ }
}
}