2 using System.ComponentModel;
3 using Tizen.NUI.BaseComponents;
4 using Tizen.NUI.Components.Extension;
5 using Tizen.NUI.Accessibility; // To use AccessibilityManager
7 namespace Tizen.NUI.Components
9 public partial class Button
11 private ImageView overlayImage;
12 private TextLabel buttonText;
13 private ImageView buttonIcon;
15 private EventHandler<StateChangedEventArgs> stateChangeHandler;
17 private bool isPressed = false;
18 private bool styleApplied = false;
21 /// The ButtonExtension instance that is injected by ButtonStyle.
23 [EditorBrowsable(EditorBrowsableState.Never)]
24 protected ButtonExtension Extension { get; set; }
27 /// Creates Button's text part.
29 /// <return>The created Button's text part.</return>
30 [EditorBrowsable(EditorBrowsableState.Never)]
31 protected virtual TextLabel CreateText()
35 PositionUsesPivotPoint = true,
36 ParentOrigin = NUI.ParentOrigin.Center,
37 PivotPoint = NUI.PivotPoint.Center,
38 WidthResizePolicy = ResizePolicyType.FillToParent,
39 HeightResizePolicy = ResizePolicyType.FillToParent,
40 HorizontalAlignment = HorizontalAlignment.Center,
41 VerticalAlignment = VerticalAlignment.Center
46 /// Creates Button's icon part.
48 /// <return>The created Button's icon part.</return>
49 [EditorBrowsable(EditorBrowsableState.Never)]
50 protected virtual ImageView CreateIcon()
54 PositionUsesPivotPoint = true,
55 ParentOrigin = NUI.ParentOrigin.Center,
56 PivotPoint = NUI.PivotPoint.Center
61 /// Creates Button's overlay image part.
63 /// <return>The created Button's overlay image part.</return>
64 [EditorBrowsable(EditorBrowsableState.Never)]
65 protected virtual ImageView CreateOverlayImage()
69 PositionUsesPivotPoint = true,
70 ParentOrigin = NUI.ParentOrigin.Center,
71 PivotPoint = NUI.PivotPoint.Center,
72 WidthResizePolicy = ResizePolicyType.FillToParent,
73 HeightResizePolicy = ResizePolicyType.FillToParent
78 /// Called when the Button is Clicked by a user
80 /// <param name="eventArgs">The click information.</param>
81 [EditorBrowsable(EditorBrowsableState.Never)]
82 protected virtual void OnClicked(ClickedEventArgs eventArgs)
89 /// <returns>The default button style.</returns>
90 /// <since_tizen> 8 </since_tizen>
91 protected override ViewStyle CreateViewStyle()
93 return new ButtonStyle();
96 /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
97 [EditorBrowsable(EditorBrowsableState.Never)]
98 protected override void OnUpdate()
103 Extension?.OnRelayout(this);
107 [EditorBrowsable(EditorBrowsableState.Never)]
108 protected override bool HandleControlStateOnTouch(Touch touch)
110 if (!IsEnabled || null == touch)
115 PointStateType state = touch.GetState(0);
119 case PointStateType.Down:
121 Extension?.SetTouchInfo(touch);
124 case PointStateType.Interrupted:
128 case PointStateType.Up:
130 bool clicked = isPressed && IsEnabled;
136 Extension?.SetTouchInfo(touch);
137 IsSelected = !IsSelected;
141 Extension?.SetTouchInfo(touch);
147 ClickedEventArgs eventArgs = new ClickedEventArgs();
148 OnClickedInternal(eventArgs);
156 return base.HandleControlStateOnTouch(touch);
160 /// Update Button State.
162 /// <since_tizen> 6 </since_tizen>
163 /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
164 [EditorBrowsable(EditorBrowsableState.Never)]
165 protected void UpdateState()
167 if (!styleApplied) return;
169 ControlState sourceState = ControlState;
170 ControlState targetState;
173 targetState = IsEnabled ? ControlState.Normal : ControlState.Disabled;
175 // Selected, DisabledSelected
176 if (IsSelected) targetState += ControlState.Selected;
178 // Pressed, PressedSelected
179 if (isPressed) targetState += ControlState.Pressed;
181 // Focused, FocusedPressed, FocusedPressedSelected, DisabledFocused, DisabledSelectedFocused
182 if (IsFocused) targetState += ControlState.Focused;
184 if (sourceState != targetState)
186 ControlState = targetState;
189 StateChangedEventArgs e = new StateChangedEventArgs
191 PreviousState = ControlStatesExtension.FromControlStateClass(sourceState),
192 CurrentState = ControlStatesExtension.FromControlStateClass(targetState)
194 stateChangeHandler?.Invoke(this, e);
196 Extension?.OnControlStateChanged(this, new ControlStateChangedEventArgs(sourceState, targetState));
201 /// Measure text, it can be override.
203 /// <since_tizen> 6 </since_tizen>
204 /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
205 [EditorBrowsable(EditorBrowsableState.Never)]
206 protected virtual void MeasureText()
208 if (buttonIcon == null || buttonText == null)
212 buttonText.WidthResizePolicy = ResizePolicyType.Fixed;
213 buttonText.HeightResizePolicy = ResizePolicyType.Fixed;
215 var textPadding = TextPadding;
216 int textPaddingStart = textPadding.Start;
217 int textPaddingEnd = textPadding.End;
218 int textPaddingTop = textPadding.Top;
219 int textPaddingBottom = textPadding.Bottom;
221 var iconPadding = IconPadding;
222 int iconPaddingStart = iconPadding.Start;
223 int iconPaddingEnd = iconPadding.End;
224 int iconPaddingTop = iconPadding.Top;
225 int iconPaddingBottom = iconPadding.Bottom;
227 if (IconRelativeOrientation == IconOrientation.Top || IconRelativeOrientation == IconOrientation.Bottom)
229 buttonText.SizeWidth = SizeWidth - textPaddingStart - textPaddingEnd;
230 buttonText.SizeHeight = SizeHeight - textPaddingTop - textPaddingBottom - iconPaddingTop - iconPaddingBottom - buttonIcon.SizeHeight;
234 buttonText.SizeWidth = SizeWidth - textPaddingStart - textPaddingEnd - iconPaddingStart - iconPaddingEnd - buttonIcon.SizeWidth;
235 buttonText.SizeHeight = SizeHeight - textPaddingTop - textPaddingBottom;
240 /// Layout child, it can be override.
242 /// <since_tizen> 6 </since_tizen>
243 /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
244 [EditorBrowsable(EditorBrowsableState.Never)]
245 protected virtual void LayoutChild()
247 if (buttonIcon == null || buttonText == null)
252 var textPadding = TextPadding;
253 int textPaddingStart = textPadding.Start;
254 int textPaddingEnd = textPadding.End;
255 int textPaddingTop = textPadding.Top;
256 int textPaddingBottom = textPadding.Bottom;
258 var iconPadding = IconPadding;
259 int iconPaddingStart = iconPadding.Start;
260 int iconPaddingEnd = iconPadding.End;
261 int iconPaddingTop = iconPadding.Top;
262 int iconPaddingBottom = iconPadding.Bottom;
264 switch (IconRelativeOrientation)
266 case IconOrientation.Top:
267 buttonIcon.PositionUsesPivotPoint = true;
268 buttonIcon.ParentOrigin = NUI.ParentOrigin.TopCenter;
269 buttonIcon.PivotPoint = NUI.PivotPoint.TopCenter;
270 buttonIcon.Position2D = new Position2D(0, iconPaddingTop);
272 buttonText.PositionUsesPivotPoint = true;
273 buttonText.ParentOrigin = NUI.ParentOrigin.BottomCenter;
274 buttonText.PivotPoint = NUI.PivotPoint.BottomCenter;
275 buttonText.Position2D = new Position2D(0, -textPaddingBottom);
277 case IconOrientation.Bottom:
278 buttonIcon.PositionUsesPivotPoint = true;
279 buttonIcon.ParentOrigin = NUI.ParentOrigin.BottomCenter;
280 buttonIcon.PivotPoint = NUI.PivotPoint.BottomCenter;
281 buttonIcon.Position2D = new Position2D(0, -iconPaddingBottom);
283 buttonText.PositionUsesPivotPoint = true;
284 buttonText.ParentOrigin = NUI.ParentOrigin.TopCenter;
285 buttonText.PivotPoint = NUI.PivotPoint.TopCenter;
286 buttonText.Position2D = new Position2D(0, textPaddingTop);
288 case IconOrientation.Left:
289 if (LayoutDirection == ViewLayoutDirectionType.LTR)
291 buttonIcon.PositionUsesPivotPoint = true;
292 buttonIcon.ParentOrigin = NUI.ParentOrigin.CenterLeft;
293 buttonIcon.PivotPoint = NUI.PivotPoint.CenterLeft;
294 buttonIcon.Position2D = new Position2D(iconPaddingStart, 0);
296 buttonText.PositionUsesPivotPoint = true;
297 buttonText.ParentOrigin = NUI.ParentOrigin.CenterRight;
298 buttonText.PivotPoint = NUI.PivotPoint.CenterRight;
299 buttonText.Position2D = new Position2D(-textPaddingEnd, 0);
303 buttonIcon.PositionUsesPivotPoint = true;
304 buttonIcon.ParentOrigin = NUI.ParentOrigin.CenterRight;
305 buttonIcon.PivotPoint = NUI.PivotPoint.CenterRight;
306 buttonIcon.Position2D = new Position2D(-iconPaddingStart, 0);
308 buttonText.PositionUsesPivotPoint = true;
309 buttonText.ParentOrigin = NUI.ParentOrigin.CenterLeft;
310 buttonText.PivotPoint = NUI.PivotPoint.CenterLeft;
311 buttonText.Position2D = new Position2D(textPaddingEnd, 0);
315 case IconOrientation.Right:
316 if (LayoutDirection == ViewLayoutDirectionType.RTL)
318 buttonIcon.PositionUsesPivotPoint = true;
319 buttonIcon.ParentOrigin = NUI.ParentOrigin.CenterLeft;
320 buttonIcon.PivotPoint = NUI.PivotPoint.CenterLeft;
321 buttonIcon.Position2D = new Position2D(iconPaddingEnd, 0);
323 buttonText.PositionUsesPivotPoint = true;
324 buttonText.ParentOrigin = NUI.ParentOrigin.CenterRight;
325 buttonText.PivotPoint = NUI.PivotPoint.CenterRight;
326 buttonText.Position2D = new Position2D(-textPaddingStart, 0);
330 buttonIcon.PositionUsesPivotPoint = true;
331 buttonIcon.ParentOrigin = NUI.ParentOrigin.CenterRight;
332 buttonIcon.PivotPoint = NUI.PivotPoint.CenterRight;
333 buttonIcon.Position2D = new Position2D(-iconPaddingEnd, 0);
335 buttonText.PositionUsesPivotPoint = true;
336 buttonText.ParentOrigin = NUI.ParentOrigin.CenterLeft;
337 buttonText.PivotPoint = NUI.PivotPoint.CenterLeft;
338 buttonText.Position2D = new Position2D(textPaddingStart, 0);
344 if (string.IsNullOrEmpty(buttonText.Text))
346 buttonIcon.ParentOrigin = NUI.ParentOrigin.Center;
347 buttonIcon.PivotPoint = NUI.PivotPoint.Center;
352 /// Dispose Button and all children on it.
354 /// <param name="type">Dispose type.</param>
355 /// <since_tizen> 6 </since_tizen>
356 protected override void Dispose(DisposeTypes type)
363 if (type == DisposeTypes.Explicit)
366 AccessibilityManager.Instance.DeleteAccessibilityAttribute(this);
368 Extension?.OnDispose(this);
370 if (buttonIcon != null)
372 Utility.Dispose(buttonIcon);
374 if (buttonText != null)
376 Utility.Dispose(buttonText);
378 if (overlayImage != null)
380 Utility.Dispose(overlayImage);
388 [EditorBrowsable(EditorBrowsableState.Never)]
389 protected override void OnControlStateChanged(ControlStateChangedEventArgs controlStateChangedInfo)
391 base.OnControlStateChanged(controlStateChangedInfo);
393 var stateEnabled = !controlStateChangedInfo.CurrentState.Contains(ControlState.Disabled);
395 if (IsEnabled != stateEnabled)
397 IsEnabled = stateEnabled;
400 var statePressed = controlStateChangedInfo.CurrentState.Contains(ControlState.Pressed);
402 if (isPressed != statePressed)
404 isPressed = statePressed;
409 /// It is hijack by using protected, style copy problem when class inherited from Button.
411 /// <since_tizen> 6 </since_tizen>
412 private void Initialize()
414 EnableControlStatePropagation = true;
416 LayoutDirectionChanged += OnLayoutDirectionChanged;
418 AccessibilityManager.Instance.SetAccessibilityAttribute(this, AccessibilityManager.AccessibilityAttribute.Trait, "Button");
423 private void UpdateUIContent()
428 Sensitive = IsEnabled;
431 private void OnLayoutDirectionChanged(object sender, LayoutDirectionChangedEventArgs e)
437 private void OnClickedInternal(ClickedEventArgs eventArgs)
439 Command?.Execute(CommandParameter);
440 OnClicked(eventArgs);
441 Extension?.OnClicked(this, eventArgs);
443 ClickEventArgs nestedEventArgs = new ClickEventArgs();
444 ClickEvent?.Invoke(this, nestedEventArgs);
445 Clicked?.Invoke(this, eventArgs);
448 private void OnIconRelayout(object sender, EventArgs e)
454 internal override bool OnAccessibilityActivated()
466 bool clicked = isPressed && IsEnabled;
471 IsSelected = !IsSelected;
480 ClickedEventArgs eventArgs = new ClickedEventArgs();
481 OnClickedInternal(eventArgs);