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;
216 var textPadding = TextPadding;
217 int textPaddingStart = textPadding.Start;
218 int textPaddingEnd = textPadding.End;
219 int textPaddingTop = textPadding.Top;
220 int textPaddingBottom = textPadding.Bottom;
222 var iconPadding = IconPadding;
223 int iconPaddingStart = iconPadding.Start;
224 int iconPaddingEnd = iconPadding.End;
225 int iconPaddingTop = iconPadding.Top;
226 int iconPaddingBottom = iconPadding.Bottom;
228 if (IconRelativeOrientation == IconOrientation.Top || IconRelativeOrientation == IconOrientation.Bottom)
230 buttonText.SizeWidth = SizeWidth - textPaddingStart - textPaddingEnd;
231 buttonText.SizeHeight = SizeHeight - textPaddingTop - textPaddingBottom - iconPaddingTop - iconPaddingBottom - buttonIcon.SizeHeight;
235 buttonText.SizeWidth = SizeWidth - textPaddingStart - textPaddingEnd - iconPaddingStart - iconPaddingEnd - buttonIcon.SizeWidth;
236 buttonText.SizeHeight = SizeHeight - textPaddingTop - textPaddingBottom;
241 /// Layout child, it can be override.
243 /// <since_tizen> 6 </since_tizen>
244 /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
245 [EditorBrowsable(EditorBrowsableState.Never)]
246 protected virtual void LayoutChild()
248 if (buttonIcon == null || buttonText == null)
253 var textPadding = TextPadding;
254 int textPaddingStart = textPadding.Start;
255 int textPaddingEnd = textPadding.End;
256 int textPaddingTop = textPadding.Top;
257 int textPaddingBottom = textPadding.Bottom;
259 var iconPadding = IconPadding;
260 int iconPaddingStart = iconPadding.Start;
261 int iconPaddingEnd = iconPadding.End;
262 int iconPaddingTop = iconPadding.Top;
263 int iconPaddingBottom = iconPadding.Bottom;
265 switch (IconRelativeOrientation)
267 case IconOrientation.Top:
268 buttonIcon.PositionUsesPivotPoint = true;
269 buttonIcon.ParentOrigin = NUI.ParentOrigin.TopCenter;
270 buttonIcon.PivotPoint = NUI.PivotPoint.TopCenter;
271 buttonIcon.Position2D = new Position2D(0, iconPaddingTop);
273 buttonText.PositionUsesPivotPoint = true;
274 buttonText.ParentOrigin = NUI.ParentOrigin.BottomCenter;
275 buttonText.PivotPoint = NUI.PivotPoint.BottomCenter;
276 buttonText.Position2D = new Position2D(0, -textPaddingBottom);
278 case IconOrientation.Bottom:
279 buttonIcon.PositionUsesPivotPoint = true;
280 buttonIcon.ParentOrigin = NUI.ParentOrigin.BottomCenter;
281 buttonIcon.PivotPoint = NUI.PivotPoint.BottomCenter;
282 buttonIcon.Position2D = new Position2D(0, -iconPaddingBottom);
284 buttonText.PositionUsesPivotPoint = true;
285 buttonText.ParentOrigin = NUI.ParentOrigin.TopCenter;
286 buttonText.PivotPoint = NUI.PivotPoint.TopCenter;
287 buttonText.Position2D = new Position2D(0, textPaddingTop);
289 case IconOrientation.Left:
290 if (LayoutDirection == ViewLayoutDirectionType.LTR)
292 buttonIcon.PositionUsesPivotPoint = true;
293 buttonIcon.ParentOrigin = NUI.ParentOrigin.CenterLeft;
294 buttonIcon.PivotPoint = NUI.PivotPoint.CenterLeft;
295 buttonIcon.Position2D = new Position2D(iconPaddingStart, 0);
297 buttonText.PositionUsesPivotPoint = true;
298 buttonText.ParentOrigin = NUI.ParentOrigin.CenterRight;
299 buttonText.PivotPoint = NUI.PivotPoint.CenterRight;
300 buttonText.Position2D = new Position2D(-textPaddingEnd, 0);
304 buttonIcon.PositionUsesPivotPoint = true;
305 buttonIcon.ParentOrigin = NUI.ParentOrigin.CenterRight;
306 buttonIcon.PivotPoint = NUI.PivotPoint.CenterRight;
307 buttonIcon.Position2D = new Position2D(-iconPaddingStart, 0);
309 buttonText.PositionUsesPivotPoint = true;
310 buttonText.ParentOrigin = NUI.ParentOrigin.CenterLeft;
311 buttonText.PivotPoint = NUI.PivotPoint.CenterLeft;
312 buttonText.Position2D = new Position2D(textPaddingEnd, 0);
316 case IconOrientation.Right:
317 if (LayoutDirection == ViewLayoutDirectionType.RTL)
319 buttonIcon.PositionUsesPivotPoint = true;
320 buttonIcon.ParentOrigin = NUI.ParentOrigin.CenterLeft;
321 buttonIcon.PivotPoint = NUI.PivotPoint.CenterLeft;
322 buttonIcon.Position2D = new Position2D(iconPaddingEnd, 0);
324 buttonText.PositionUsesPivotPoint = true;
325 buttonText.ParentOrigin = NUI.ParentOrigin.CenterRight;
326 buttonText.PivotPoint = NUI.PivotPoint.CenterRight;
327 buttonText.Position2D = new Position2D(-textPaddingStart, 0);
331 buttonIcon.PositionUsesPivotPoint = true;
332 buttonIcon.ParentOrigin = NUI.ParentOrigin.CenterRight;
333 buttonIcon.PivotPoint = NUI.PivotPoint.CenterRight;
334 buttonIcon.Position2D = new Position2D(-iconPaddingEnd, 0);
336 buttonText.PositionUsesPivotPoint = true;
337 buttonText.ParentOrigin = NUI.ParentOrigin.CenterLeft;
338 buttonText.PivotPoint = NUI.PivotPoint.CenterLeft;
339 buttonText.Position2D = new Position2D(textPaddingStart, 0);
345 if (string.IsNullOrEmpty(buttonText.Text))
347 buttonIcon.ParentOrigin = NUI.ParentOrigin.Center;
348 buttonIcon.PivotPoint = NUI.PivotPoint.Center;
353 /// Dispose Button and all children on it.
355 /// <param name="type">Dispose type.</param>
356 /// <since_tizen> 6 </since_tizen>
357 protected override void Dispose(DisposeTypes type)
364 if (type == DisposeTypes.Explicit)
366 Extension?.OnDispose(this);
368 if (buttonIcon != null)
370 Utility.Dispose(buttonIcon);
372 if (buttonText != null)
374 Utility.Dispose(buttonText);
376 if (overlayImage != null)
378 Utility.Dispose(overlayImage);
386 [EditorBrowsable(EditorBrowsableState.Never)]
387 protected override void OnControlStateChanged(ControlStateChangedEventArgs controlStateChangedInfo)
389 base.OnControlStateChanged(controlStateChangedInfo);
391 var stateEnabled = !controlStateChangedInfo.CurrentState.Contains(ControlState.Disabled);
393 if (IsEnabled != stateEnabled)
395 IsEnabled = stateEnabled;
398 var statePressed = controlStateChangedInfo.CurrentState.Contains(ControlState.Pressed);
400 if (isPressed != statePressed)
402 isPressed = statePressed;
407 /// It is hijack by using protected, style copy problem when class inherited from Button.
409 /// <since_tizen> 6 </since_tizen>
410 private void Initialize()
412 EnableControlStatePropagation = true;
414 LayoutDirectionChanged += OnLayoutDirectionChanged;
416 AccessibilityManager.Instance.SetAccessibilityAttribute(this, AccessibilityManager.AccessibilityAttribute.Trait, "Button");
419 private void UpdateUIContent()
424 Sensitive = IsEnabled;
427 private void OnLayoutDirectionChanged(object sender, LayoutDirectionChangedEventArgs e)
433 private void OnClickedInternal(ClickedEventArgs eventArgs)
435 Command?.Execute(CommandParameter);
436 OnClicked(eventArgs);
437 Extension?.OnClicked(this, eventArgs);
439 ClickEventArgs nestedEventArgs = new ClickEventArgs();
440 ClickEvent?.Invoke(this, nestedEventArgs);
441 Clicked?.Invoke(this, eventArgs);
444 private void OnIconRelayout(object sender, EventArgs e)
450 internal override bool OnAccessibilityActivated()
462 bool clicked = isPressed && IsEnabled;
467 IsSelected = !IsSelected;
476 ClickedEventArgs eventArgs = new ClickedEventArgs();
477 OnClickedInternal(eventArgs);