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 /// <param name="touchInfo">The touch information in case the state has changed by touching.</param>
163 /// <since_tizen> 6 </since_tizen>
164 /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
165 [EditorBrowsable(EditorBrowsableState.Never)]
166 protected void UpdateState()
168 if (!styleApplied) return;
170 ControlState sourceState = ControlState;
171 ControlState targetState;
174 targetState = IsEnabled ? ControlState.Normal : ControlState.Disabled;
176 // Selected, DisabledSelected
177 if (IsSelected) targetState += ControlState.Selected;
179 // Pressed, PressedSelected
180 if (isPressed) targetState += ControlState.Pressed;
182 // Focused, FocusedPressed, FocusedPressedSelected, DisabledFocused, DisabledSelectedFocused
183 if (IsFocused) targetState += ControlState.Focused;
185 if (sourceState != targetState)
187 ControlState = targetState;
190 StateChangedEventArgs e = new StateChangedEventArgs
192 PreviousState = ControlStatesExtension.FromControlStateClass(sourceState),
193 CurrentState = ControlStatesExtension.FromControlStateClass(targetState)
195 stateChangeHandler?.Invoke(this, e);
197 Extension?.OnControlStateChanged(this, new ControlStateChangedEventArgs(sourceState, targetState));
202 /// Measure text, it can be override.
204 /// <since_tizen> 6 </since_tizen>
205 /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
206 [EditorBrowsable(EditorBrowsableState.Never)]
207 protected virtual void MeasureText()
209 if (buttonIcon == null || buttonText == null)
213 buttonText.WidthResizePolicy = ResizePolicyType.Fixed;
214 buttonText.HeightResizePolicy = ResizePolicyType.Fixed;
215 int textPaddingStart = buttonStyle.TextPadding.Start;
216 int textPaddingEnd = buttonStyle.TextPadding.End;
217 int textPaddingTop = buttonStyle.TextPadding.Top;
218 int textPaddingBottom = buttonStyle.TextPadding.Bottom;
220 int iconPaddingStart = buttonStyle.IconPadding.Start;
221 int iconPaddingEnd = buttonStyle.IconPadding.End;
222 int iconPaddingTop = buttonStyle.IconPadding.Top;
223 int iconPaddingBottom = buttonStyle.IconPadding.Bottom;
225 if (IconRelativeOrientation == IconOrientation.Top || IconRelativeOrientation == IconOrientation.Bottom)
227 buttonText.SizeWidth = SizeWidth - textPaddingStart - textPaddingEnd;
228 buttonText.SizeHeight = SizeHeight - textPaddingTop - textPaddingBottom - iconPaddingTop - iconPaddingBottom - buttonIcon.SizeHeight;
232 buttonText.SizeWidth = SizeWidth - textPaddingStart - textPaddingEnd - iconPaddingStart - iconPaddingEnd - buttonIcon.SizeWidth;
233 buttonText.SizeHeight = SizeHeight - textPaddingTop - textPaddingBottom;
238 /// Layout child, it can be override.
240 /// <since_tizen> 6 </since_tizen>
241 /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
242 [EditorBrowsable(EditorBrowsableState.Never)]
243 protected virtual void LayoutChild()
245 if (buttonIcon == null || buttonText == null)
250 int textPaddingStart = buttonStyle.TextPadding.Start;
251 int textPaddingEnd = buttonStyle.TextPadding.End;
252 int textPaddingTop = buttonStyle.TextPadding.Top;
253 int textPaddingBottom = buttonStyle.TextPadding.Bottom;
255 int iconPaddingStart = buttonStyle.IconPadding.Start;
256 int iconPaddingEnd = buttonStyle.IconPadding.End;
257 int iconPaddingTop = buttonStyle.IconPadding.Top;
258 int iconPaddingBottom = buttonStyle.IconPadding.Bottom;
260 switch (IconRelativeOrientation)
262 case IconOrientation.Top:
263 buttonIcon.PositionUsesPivotPoint = true;
264 buttonIcon.ParentOrigin = NUI.ParentOrigin.TopCenter;
265 buttonIcon.PivotPoint = NUI.PivotPoint.TopCenter;
266 buttonIcon.Position2D = new Position2D(0, iconPaddingTop);
268 buttonText.PositionUsesPivotPoint = true;
269 buttonText.ParentOrigin = NUI.ParentOrigin.BottomCenter;
270 buttonText.PivotPoint = NUI.PivotPoint.BottomCenter;
271 buttonText.Position2D = new Position2D(0, -textPaddingBottom);
273 case IconOrientation.Bottom:
274 buttonIcon.PositionUsesPivotPoint = true;
275 buttonIcon.ParentOrigin = NUI.ParentOrigin.BottomCenter;
276 buttonIcon.PivotPoint = NUI.PivotPoint.BottomCenter;
277 buttonIcon.Position2D = new Position2D(0, -iconPaddingBottom);
279 buttonText.PositionUsesPivotPoint = true;
280 buttonText.ParentOrigin = NUI.ParentOrigin.TopCenter;
281 buttonText.PivotPoint = NUI.PivotPoint.TopCenter;
282 buttonText.Position2D = new Position2D(0, textPaddingTop);
284 case IconOrientation.Left:
285 if (LayoutDirection == ViewLayoutDirectionType.LTR)
287 buttonIcon.PositionUsesPivotPoint = true;
288 buttonIcon.ParentOrigin = NUI.ParentOrigin.CenterLeft;
289 buttonIcon.PivotPoint = NUI.PivotPoint.CenterLeft;
290 buttonIcon.Position2D = new Position2D(iconPaddingStart, 0);
292 buttonText.PositionUsesPivotPoint = true;
293 buttonText.ParentOrigin = NUI.ParentOrigin.CenterRight;
294 buttonText.PivotPoint = NUI.PivotPoint.CenterRight;
295 buttonText.Position2D = new Position2D(-textPaddingEnd, 0);
299 buttonIcon.PositionUsesPivotPoint = true;
300 buttonIcon.ParentOrigin = NUI.ParentOrigin.CenterRight;
301 buttonIcon.PivotPoint = NUI.PivotPoint.CenterRight;
302 buttonIcon.Position2D = new Position2D(-iconPaddingStart, 0);
304 buttonText.PositionUsesPivotPoint = true;
305 buttonText.ParentOrigin = NUI.ParentOrigin.CenterLeft;
306 buttonText.PivotPoint = NUI.PivotPoint.CenterLeft;
307 buttonText.Position2D = new Position2D(textPaddingEnd, 0);
311 case IconOrientation.Right:
312 if (LayoutDirection == ViewLayoutDirectionType.RTL)
314 buttonIcon.PositionUsesPivotPoint = true;
315 buttonIcon.ParentOrigin = NUI.ParentOrigin.CenterLeft;
316 buttonIcon.PivotPoint = NUI.PivotPoint.CenterLeft;
317 buttonIcon.Position2D = new Position2D(iconPaddingEnd, 0);
319 buttonText.PositionUsesPivotPoint = true;
320 buttonText.ParentOrigin = NUI.ParentOrigin.CenterRight;
321 buttonText.PivotPoint = NUI.PivotPoint.CenterRight;
322 buttonText.Position2D = new Position2D(-textPaddingStart, 0);
326 buttonIcon.PositionUsesPivotPoint = true;
327 buttonIcon.ParentOrigin = NUI.ParentOrigin.CenterRight;
328 buttonIcon.PivotPoint = NUI.PivotPoint.CenterRight;
329 buttonIcon.Position2D = new Position2D(-iconPaddingEnd, 0);
331 buttonText.PositionUsesPivotPoint = true;
332 buttonText.ParentOrigin = NUI.ParentOrigin.CenterLeft;
333 buttonText.PivotPoint = NUI.PivotPoint.CenterLeft;
334 buttonText.Position2D = new Position2D(textPaddingStart, 0);
340 if (string.IsNullOrEmpty(buttonText.Text))
342 buttonIcon.ParentOrigin = NUI.ParentOrigin.Center;
343 buttonIcon.PivotPoint = NUI.PivotPoint.Center;
348 /// Dispose Button and all children on it.
350 /// <param name="type">Dispose type.</param>
351 /// <since_tizen> 6 </since_tizen>
352 protected override void Dispose(DisposeTypes type)
359 if (type == DisposeTypes.Explicit)
361 Extension?.OnDispose(this);
363 if (buttonIcon != null)
365 Utility.Dispose(buttonIcon);
367 if (buttonText != null)
369 Utility.Dispose(buttonText);
371 if (overlayImage != null)
373 Utility.Dispose(overlayImage);
381 [EditorBrowsable(EditorBrowsableState.Never)]
382 protected override void OnControlStateChanged(ControlStateChangedEventArgs controlStateChangedInfo)
384 base.OnControlStateChanged(controlStateChangedInfo);
386 var stateEnabled = !controlStateChangedInfo.CurrentState.Contains(ControlState.Disabled);
388 if (IsEnabled != stateEnabled)
390 IsEnabled = stateEnabled;
393 var statePressed = controlStateChangedInfo.CurrentState.Contains(ControlState.Pressed);
395 if (isPressed != statePressed)
397 isPressed = statePressed;
402 /// It is hijack by using protected, style copy problem when class inherited from Button.
404 /// <since_tizen> 6 </since_tizen>
405 private void Initialize()
407 EnableControlStatePropagation = true;
409 LayoutDirectionChanged += OnLayoutDirectionChanged;
411 AccessibilityManager.Instance.SetAccessibilityAttribute(this, AccessibilityManager.AccessibilityAttribute.Trait, "Button");
414 private void UpdateUIContent()
419 Sensitive = IsEnabled;
422 private void OnLayoutDirectionChanged(object sender, LayoutDirectionChangedEventArgs e)
428 private void OnClickedInternal(ClickedEventArgs eventArgs)
430 Command?.Execute(CommandParameter);
431 OnClicked(eventArgs);
432 Extension?.OnClicked(this, eventArgs);
434 ClickEventArgs nestedEventArgs = new ClickEventArgs();
435 ClickEvent?.Invoke(this, nestedEventArgs);
436 Clicked?.Invoke(this, eventArgs);
439 private void OnIconRelayout(object sender, EventArgs e)
445 internal override bool OnAccessibilityActivated()
457 bool clicked = isPressed && IsEnabled;
462 IsSelected = !IsSelected;
471 ClickedEventArgs eventArgs = new ClickedEventArgs();
472 OnClickedInternal(eventArgs);