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 OnClick(ClickEventArgs 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 /// Update Button State.
87 /// <param name="touchInfo">The touch information in case the state has changed by touching.</param>
88 /// <since_tizen> 6 </since_tizen>
89 /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
90 [EditorBrowsable(EditorBrowsableState.Never)]
91 protected void UpdateState()
93 if (!styleApplied) return;
95 ControlState sourceState = ControlState;
96 ControlState targetState;
99 targetState = IsEnabled ? ControlState.Normal : ControlState.Disabled;
101 // Selected, DisabledSelected
102 if (IsSelected) targetState += ControlState.Selected;
104 // Pressed, PressedSelected
105 if (isPressed) targetState += ControlState.Pressed;
107 // Focused, FocusedPressed, FocusedPressedSelected, DisabledFocused, DisabledSelectedFocused
108 if (IsFocused) targetState += ControlState.Focused;
110 if (sourceState != targetState)
112 ControlState = targetState;
115 StateChangedEventArgs e = new StateChangedEventArgs
117 PreviousState = ControlStatesExtension.FromControlStateClass(sourceState),
118 CurrentState = ControlStatesExtension.FromControlStateClass(targetState)
120 stateChangeHandler?.Invoke(this, e);
122 Extension?.OnControlStateChanged(this, new ControlStateChangedEventArgs(sourceState, targetState));
127 /// Measure text, it can be override.
129 /// <since_tizen> 6 </since_tizen>
130 /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
131 [EditorBrowsable(EditorBrowsableState.Never)]
132 protected virtual void MeasureText()
134 if (Icon == null || TextLabel == null)
138 TextLabel.WidthResizePolicy = ResizePolicyType.Fixed;
139 TextLabel.HeightResizePolicy = ResizePolicyType.Fixed;
140 int textPaddingStart = buttonStyle.TextPadding.Start;
141 int textPaddingEnd = buttonStyle.TextPadding.End;
142 int textPaddingTop = buttonStyle.TextPadding.Top;
143 int textPaddingBottom = buttonStyle.TextPadding.Bottom;
145 int iconPaddingStart = buttonStyle.IconPadding.Start;
146 int iconPaddingEnd = buttonStyle.IconPadding.End;
147 int iconPaddingTop = buttonStyle.IconPadding.Top;
148 int iconPaddingBottom = buttonStyle.IconPadding.Bottom;
150 if (IconRelativeOrientation == IconOrientation.Top || IconRelativeOrientation == IconOrientation.Bottom)
152 TextLabel.SizeWidth = SizeWidth - textPaddingStart - textPaddingEnd;
153 TextLabel.SizeHeight = SizeHeight - textPaddingTop - textPaddingBottom - iconPaddingTop - iconPaddingBottom - Icon.SizeHeight;
157 TextLabel.SizeWidth = SizeWidth - textPaddingStart - textPaddingEnd - iconPaddingStart - iconPaddingEnd - Icon.SizeWidth;
158 TextLabel.SizeHeight = SizeHeight - textPaddingTop - textPaddingBottom;
163 /// Layout child, it can be override.
165 /// <since_tizen> 6 </since_tizen>
166 /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
167 [EditorBrowsable(EditorBrowsableState.Never)]
168 protected virtual void LayoutChild()
170 if (Icon == null || TextLabel == null)
175 var buttonIcon = Icon;
176 var buttonText = TextLabel;
178 int textPaddingStart = buttonStyle.TextPadding.Start;
179 int textPaddingEnd = buttonStyle.TextPadding.End;
180 int textPaddingTop = buttonStyle.TextPadding.Top;
181 int textPaddingBottom = buttonStyle.TextPadding.Bottom;
183 int iconPaddingStart = buttonStyle.IconPadding.Start;
184 int iconPaddingEnd = buttonStyle.IconPadding.End;
185 int iconPaddingTop = buttonStyle.IconPadding.Top;
186 int iconPaddingBottom = buttonStyle.IconPadding.Bottom;
188 switch (IconRelativeOrientation)
190 case IconOrientation.Top:
191 buttonIcon.PositionUsesPivotPoint = true;
192 buttonIcon.ParentOrigin = NUI.ParentOrigin.TopCenter;
193 buttonIcon.PivotPoint = NUI.PivotPoint.TopCenter;
194 buttonIcon.Position2D = new Position2D(0, iconPaddingTop);
196 buttonText.PositionUsesPivotPoint = true;
197 buttonText.ParentOrigin = NUI.ParentOrigin.BottomCenter;
198 buttonText.PivotPoint = NUI.PivotPoint.BottomCenter;
199 buttonText.Position2D = new Position2D(0, -textPaddingBottom);
201 case IconOrientation.Bottom:
202 buttonIcon.PositionUsesPivotPoint = true;
203 buttonIcon.ParentOrigin = NUI.ParentOrigin.BottomCenter;
204 buttonIcon.PivotPoint = NUI.PivotPoint.BottomCenter;
205 buttonIcon.Position2D = new Position2D(0, -iconPaddingBottom);
207 buttonText.PositionUsesPivotPoint = true;
208 buttonText.ParentOrigin = NUI.ParentOrigin.TopCenter;
209 buttonText.PivotPoint = NUI.PivotPoint.TopCenter;
210 buttonText.Position2D = new Position2D(0, textPaddingTop);
212 case IconOrientation.Left:
213 if (LayoutDirection == ViewLayoutDirectionType.LTR)
215 buttonIcon.PositionUsesPivotPoint = true;
216 buttonIcon.ParentOrigin = NUI.ParentOrigin.CenterLeft;
217 buttonIcon.PivotPoint = NUI.PivotPoint.CenterLeft;
218 buttonIcon.Position2D = new Position2D(iconPaddingStart, 0);
220 buttonText.PositionUsesPivotPoint = true;
221 buttonText.ParentOrigin = NUI.ParentOrigin.CenterRight;
222 buttonText.PivotPoint = NUI.PivotPoint.CenterRight;
223 buttonText.Position2D = new Position2D(-textPaddingEnd, 0);
227 buttonIcon.PositionUsesPivotPoint = true;
228 buttonIcon.ParentOrigin = NUI.ParentOrigin.CenterRight;
229 buttonIcon.PivotPoint = NUI.PivotPoint.CenterRight;
230 buttonIcon.Position2D = new Position2D(-iconPaddingStart, 0);
232 buttonText.PositionUsesPivotPoint = true;
233 buttonText.ParentOrigin = NUI.ParentOrigin.CenterLeft;
234 buttonText.PivotPoint = NUI.PivotPoint.CenterLeft;
235 buttonText.Position2D = new Position2D(textPaddingEnd, 0);
239 case IconOrientation.Right:
240 if (LayoutDirection == ViewLayoutDirectionType.RTL)
242 buttonIcon.PositionUsesPivotPoint = true;
243 buttonIcon.ParentOrigin = NUI.ParentOrigin.CenterLeft;
244 buttonIcon.PivotPoint = NUI.PivotPoint.CenterLeft;
245 buttonIcon.Position2D = new Position2D(iconPaddingEnd, 0);
247 buttonText.PositionUsesPivotPoint = true;
248 buttonText.ParentOrigin = NUI.ParentOrigin.CenterRight;
249 buttonText.PivotPoint = NUI.PivotPoint.CenterRight;
250 buttonText.Position2D = new Position2D(-textPaddingStart, 0);
254 buttonIcon.PositionUsesPivotPoint = true;
255 buttonIcon.ParentOrigin = NUI.ParentOrigin.CenterRight;
256 buttonIcon.PivotPoint = NUI.PivotPoint.CenterRight;
257 buttonIcon.Position2D = new Position2D(-iconPaddingEnd, 0);
259 buttonText.PositionUsesPivotPoint = true;
260 buttonText.ParentOrigin = NUI.ParentOrigin.CenterLeft;
261 buttonText.PivotPoint = NUI.PivotPoint.CenterLeft;
262 buttonText.Position2D = new Position2D(textPaddingStart, 0);
268 if (string.IsNullOrEmpty(buttonText.Text))
270 buttonIcon.ParentOrigin = NUI.ParentOrigin.Center;
271 buttonIcon.PivotPoint = NUI.PivotPoint.Center;
276 /// Theme change callback when theme is changed, this callback will be trigger.
278 /// <param name="sender">The sender</param>
279 /// <param name="e">The event data</param>
280 [EditorBrowsable(EditorBrowsableState.Never)]
281 protected override void OnThemeChangedEvent(object sender, StyleManager.ThemeChangeEventArgs e)
283 ButtonStyle buttonStyle = StyleManager.Instance.GetViewStyle(StyleName) as ButtonStyle;
284 if (buttonStyle != null)
286 ApplyStyle(buttonStyle);
292 /// Dispose Button and all children on it.
294 /// <param name="type">Dispose type.</param>
295 /// <since_tizen> 6 </since_tizen>
296 protected override void Dispose(DisposeTypes type)
303 if (type == DisposeTypes.Explicit)
305 Extension?.OnDispose(this);
309 Utility.Dispose(Icon);
311 if (TextLabel != null)
313 Utility.Dispose(TextLabel);
315 if (OverlayImage != null)
317 Utility.Dispose(OverlayImage);
325 [EditorBrowsable(EditorBrowsableState.Never)]
326 protected override void OnControlStateChanged(ControlStateChangedEventArgs controlStateChangedInfo)
328 base.OnControlStateChanged(controlStateChangedInfo);
330 var stateEnabled = !controlStateChangedInfo.CurrentState.Contains(ControlState.Disabled);
332 if (IsEnabled != stateEnabled)
334 IsEnabled = stateEnabled;
337 var statePressed = controlStateChangedInfo.CurrentState.Contains(ControlState.Pressed);
339 if (isPressed != statePressed)
341 isPressed = statePressed;
346 /// It is hijack by using protected, style copy problem when class inherited from Button.
348 /// <since_tizen> 6 </since_tizen>
349 private void Initialize()
351 EnableControlStatePropagation = true;
353 LayoutDirectionChanged += OnLayoutDirectionChanged;
356 private void UpdateUIContent()
361 Sensitive = IsEnabled;
364 private void OnLayoutDirectionChanged(object sender, LayoutDirectionChangedEventArgs e)
370 private void OnClickInternal(ClickEventArgs eventArgs)
372 Command?.Execute(CommandParameter);
374 Extension?.OnClick(this, eventArgs);
375 ClickEvent?.Invoke(this, eventArgs);
378 private void OnIconRelayout(object sender, EventArgs e)