2 using System.ComponentModel;
3 using Tizen.NUI.BaseComponents;
4 using Tizen.NUI.Components.Extension;
6 namespace Tizen.NUI.Components
8 public partial class Button
10 private ImageView overlayImage;
11 private TextLabel buttonText;
12 private ImageView buttonIcon;
14 private EventHandler<StateChangedEventArgs> stateChangeHandler;
16 private bool isPressed = false;
17 private bool styleApplied = false;
20 /// The ButtonExtension instance that is injected by ButtonStyle.
22 [EditorBrowsable(EditorBrowsableState.Never)]
23 protected ButtonExtension Extension { get; set; }
26 /// Creates Button's text part.
28 /// <return>The created Button's text part.</return>
29 [EditorBrowsable(EditorBrowsableState.Never)]
30 protected virtual TextLabel CreateText()
32 return new TextLabel();
36 /// Creates Button's icon part.
38 /// <return>The created Button's icon part.</return>
39 [EditorBrowsable(EditorBrowsableState.Never)]
40 protected virtual ImageView CreateIcon()
42 return new ImageView();
46 /// Creates Button's overlay image part.
48 /// <return>The created Button's overlay image part.</return>
49 [EditorBrowsable(EditorBrowsableState.Never)]
50 protected virtual ImageView CreateOverlayImage()
52 return new ImageView();
56 /// Called when the Button is Clicked by a user
58 /// <param name="eventArgs">The click information.</param>
59 [EditorBrowsable(EditorBrowsableState.Never)]
60 protected virtual void OnClicked(ClickedEventArgs eventArgs)
67 /// <returns>The default button style.</returns>
68 /// <since_tizen> 8 </since_tizen>
69 protected override ViewStyle CreateViewStyle()
71 return new ButtonStyle();
74 /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
75 [EditorBrowsable(EditorBrowsableState.Never)]
76 protected override void OnUpdate()
81 Extension?.OnRelayout(this);
85 [EditorBrowsable(EditorBrowsableState.Never)]
86 protected override bool HandleControlStateOnTouch(Touch touch)
88 if (!IsEnabled || null == touch)
93 PointStateType state = touch.GetState(0);
97 case PointStateType.Down:
99 Extension?.SetTouchInfo(touch);
102 case PointStateType.Interrupted:
106 case PointStateType.Up:
108 bool clicked = isPressed && IsEnabled;
114 Extension?.SetTouchInfo(touch);
115 IsSelected = !IsSelected;
119 Extension?.SetTouchInfo(touch);
125 ClickedEventArgs eventArgs = new ClickedEventArgs();
126 OnClickedInternal(eventArgs);
134 return base.HandleControlStateOnTouch(touch);
138 /// Update Button State.
140 /// <param name="touchInfo">The touch information in case the state has changed by touching.</param>
141 /// <since_tizen> 6 </since_tizen>
142 /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
143 [EditorBrowsable(EditorBrowsableState.Never)]
144 protected void UpdateState()
146 if (!styleApplied) return;
148 ControlState sourceState = ControlState;
149 ControlState targetState;
152 targetState = IsEnabled ? ControlState.Normal : ControlState.Disabled;
154 // Selected, DisabledSelected
155 if (IsSelected) targetState += ControlState.Selected;
157 // Pressed, PressedSelected
158 if (isPressed) targetState += ControlState.Pressed;
160 // Focused, FocusedPressed, FocusedPressedSelected, DisabledFocused, DisabledSelectedFocused
161 if (IsFocused) targetState += ControlState.Focused;
163 if (sourceState != targetState)
165 ControlState = targetState;
168 StateChangedEventArgs e = new StateChangedEventArgs
170 PreviousState = ControlStatesExtension.FromControlStateClass(sourceState),
171 CurrentState = ControlStatesExtension.FromControlStateClass(targetState)
173 stateChangeHandler?.Invoke(this, e);
175 Extension?.OnControlStateChanged(this, new ControlStateChangedEventArgs(sourceState, targetState));
180 /// Measure text, it can be override.
182 /// <since_tizen> 6 </since_tizen>
183 /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
184 [EditorBrowsable(EditorBrowsableState.Never)]
185 protected virtual void MeasureText()
187 if (Icon == null || TextLabel == null)
191 TextLabel.WidthResizePolicy = ResizePolicyType.Fixed;
192 TextLabel.HeightResizePolicy = ResizePolicyType.Fixed;
193 int textPaddingStart = buttonStyle.TextPadding.Start;
194 int textPaddingEnd = buttonStyle.TextPadding.End;
195 int textPaddingTop = buttonStyle.TextPadding.Top;
196 int textPaddingBottom = buttonStyle.TextPadding.Bottom;
198 int iconPaddingStart = buttonStyle.IconPadding.Start;
199 int iconPaddingEnd = buttonStyle.IconPadding.End;
200 int iconPaddingTop = buttonStyle.IconPadding.Top;
201 int iconPaddingBottom = buttonStyle.IconPadding.Bottom;
203 if (IconRelativeOrientation == IconOrientation.Top || IconRelativeOrientation == IconOrientation.Bottom)
205 TextLabel.SizeWidth = SizeWidth - textPaddingStart - textPaddingEnd;
206 TextLabel.SizeHeight = SizeHeight - textPaddingTop - textPaddingBottom - iconPaddingTop - iconPaddingBottom - Icon.SizeHeight;
210 TextLabel.SizeWidth = SizeWidth - textPaddingStart - textPaddingEnd - iconPaddingStart - iconPaddingEnd - Icon.SizeWidth;
211 TextLabel.SizeHeight = SizeHeight - textPaddingTop - textPaddingBottom;
216 /// Layout child, it can be override.
218 /// <since_tizen> 6 </since_tizen>
219 /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
220 [EditorBrowsable(EditorBrowsableState.Never)]
221 protected virtual void LayoutChild()
223 if (Icon == null || TextLabel == null)
228 var buttonIcon = Icon;
229 var buttonText = TextLabel;
231 int textPaddingStart = buttonStyle.TextPadding.Start;
232 int textPaddingEnd = buttonStyle.TextPadding.End;
233 int textPaddingTop = buttonStyle.TextPadding.Top;
234 int textPaddingBottom = buttonStyle.TextPadding.Bottom;
236 int iconPaddingStart = buttonStyle.IconPadding.Start;
237 int iconPaddingEnd = buttonStyle.IconPadding.End;
238 int iconPaddingTop = buttonStyle.IconPadding.Top;
239 int iconPaddingBottom = buttonStyle.IconPadding.Bottom;
241 switch (IconRelativeOrientation)
243 case IconOrientation.Top:
244 buttonIcon.PositionUsesPivotPoint = true;
245 buttonIcon.ParentOrigin = NUI.ParentOrigin.TopCenter;
246 buttonIcon.PivotPoint = NUI.PivotPoint.TopCenter;
247 buttonIcon.Position2D = new Position2D(0, iconPaddingTop);
249 buttonText.PositionUsesPivotPoint = true;
250 buttonText.ParentOrigin = NUI.ParentOrigin.BottomCenter;
251 buttonText.PivotPoint = NUI.PivotPoint.BottomCenter;
252 buttonText.Position2D = new Position2D(0, -textPaddingBottom);
254 case IconOrientation.Bottom:
255 buttonIcon.PositionUsesPivotPoint = true;
256 buttonIcon.ParentOrigin = NUI.ParentOrigin.BottomCenter;
257 buttonIcon.PivotPoint = NUI.PivotPoint.BottomCenter;
258 buttonIcon.Position2D = new Position2D(0, -iconPaddingBottom);
260 buttonText.PositionUsesPivotPoint = true;
261 buttonText.ParentOrigin = NUI.ParentOrigin.TopCenter;
262 buttonText.PivotPoint = NUI.PivotPoint.TopCenter;
263 buttonText.Position2D = new Position2D(0, textPaddingTop);
265 case IconOrientation.Left:
266 if (LayoutDirection == ViewLayoutDirectionType.LTR)
268 buttonIcon.PositionUsesPivotPoint = true;
269 buttonIcon.ParentOrigin = NUI.ParentOrigin.CenterLeft;
270 buttonIcon.PivotPoint = NUI.PivotPoint.CenterLeft;
271 buttonIcon.Position2D = new Position2D(iconPaddingStart, 0);
273 buttonText.PositionUsesPivotPoint = true;
274 buttonText.ParentOrigin = NUI.ParentOrigin.CenterRight;
275 buttonText.PivotPoint = NUI.PivotPoint.CenterRight;
276 buttonText.Position2D = new Position2D(-textPaddingEnd, 0);
280 buttonIcon.PositionUsesPivotPoint = true;
281 buttonIcon.ParentOrigin = NUI.ParentOrigin.CenterRight;
282 buttonIcon.PivotPoint = NUI.PivotPoint.CenterRight;
283 buttonIcon.Position2D = new Position2D(-iconPaddingStart, 0);
285 buttonText.PositionUsesPivotPoint = true;
286 buttonText.ParentOrigin = NUI.ParentOrigin.CenterLeft;
287 buttonText.PivotPoint = NUI.PivotPoint.CenterLeft;
288 buttonText.Position2D = new Position2D(textPaddingEnd, 0);
292 case IconOrientation.Right:
293 if (LayoutDirection == ViewLayoutDirectionType.RTL)
295 buttonIcon.PositionUsesPivotPoint = true;
296 buttonIcon.ParentOrigin = NUI.ParentOrigin.CenterLeft;
297 buttonIcon.PivotPoint = NUI.PivotPoint.CenterLeft;
298 buttonIcon.Position2D = new Position2D(iconPaddingEnd, 0);
300 buttonText.PositionUsesPivotPoint = true;
301 buttonText.ParentOrigin = NUI.ParentOrigin.CenterRight;
302 buttonText.PivotPoint = NUI.PivotPoint.CenterRight;
303 buttonText.Position2D = new Position2D(-textPaddingStart, 0);
307 buttonIcon.PositionUsesPivotPoint = true;
308 buttonIcon.ParentOrigin = NUI.ParentOrigin.CenterRight;
309 buttonIcon.PivotPoint = NUI.PivotPoint.CenterRight;
310 buttonIcon.Position2D = new Position2D(-iconPaddingEnd, 0);
312 buttonText.PositionUsesPivotPoint = true;
313 buttonText.ParentOrigin = NUI.ParentOrigin.CenterLeft;
314 buttonText.PivotPoint = NUI.PivotPoint.CenterLeft;
315 buttonText.Position2D = new Position2D(textPaddingStart, 0);
321 if (string.IsNullOrEmpty(buttonText.Text))
323 buttonIcon.ParentOrigin = NUI.ParentOrigin.Center;
324 buttonIcon.PivotPoint = NUI.PivotPoint.Center;
329 /// Theme change callback when theme is changed, this callback will be trigger.
331 /// <param name="sender">The sender</param>
332 /// <param name="e">The event data</param>
333 [EditorBrowsable(EditorBrowsableState.Never)]
334 protected override void OnThemeChangedEvent(object sender, StyleManager.ThemeChangeEventArgs e)
336 ButtonStyle buttonStyle = StyleManager.Instance.GetViewStyle(StyleName) as ButtonStyle;
337 if (buttonStyle != null)
339 ApplyStyle(buttonStyle);
345 /// Dispose Button and all children on it.
347 /// <param name="type">Dispose type.</param>
348 /// <since_tizen> 6 </since_tizen>
349 protected override void Dispose(DisposeTypes type)
356 if (type == DisposeTypes.Explicit)
358 Extension?.OnDispose(this);
362 Utility.Dispose(Icon);
364 if (TextLabel != null)
366 Utility.Dispose(TextLabel);
368 if (OverlayImage != null)
370 Utility.Dispose(OverlayImage);
378 [EditorBrowsable(EditorBrowsableState.Never)]
379 protected override void OnControlStateChanged(ControlStateChangedEventArgs controlStateChangedInfo)
381 base.OnControlStateChanged(controlStateChangedInfo);
383 var stateEnabled = !controlStateChangedInfo.CurrentState.Contains(ControlState.Disabled);
385 if (IsEnabled != stateEnabled)
387 IsEnabled = stateEnabled;
390 var statePressed = controlStateChangedInfo.CurrentState.Contains(ControlState.Pressed);
392 if (isPressed != statePressed)
394 isPressed = statePressed;
399 /// It is hijack by using protected, style copy problem when class inherited from Button.
401 /// <since_tizen> 6 </since_tizen>
402 private void Initialize()
404 EnableControlStatePropagation = true;
406 LayoutDirectionChanged += OnLayoutDirectionChanged;
409 private void UpdateUIContent()
414 Sensitive = IsEnabled;
417 private void OnLayoutDirectionChanged(object sender, LayoutDirectionChangedEventArgs e)
423 private void OnClickedInternal(ClickedEventArgs eventArgs)
425 Command?.Execute(CommandParameter);
426 OnClicked(eventArgs);
427 Extension?.OnClicked(this, eventArgs);
429 ClickEventArgs nestedEventArgs = new ClickEventArgs();
430 ClickEvent?.Invoke(this, nestedEventArgs);
431 Clicked?.Invoke(this, eventArgs);
434 private void OnIconRelayout(object sender, EventArgs e)