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> stateChangeHander;
16 private bool isSelected = false;
17 private bool isEnabled = true;
18 private bool isPressed = false;
20 private StringSelector textSelector = new StringSelector();
21 private StringSelector translatableTextSelector = new StringSelector();
22 private ColorSelector textColorSelector = new ColorSelector();
23 private FloatSelector pointSizeSelector = new FloatSelector();
25 private StringSelector iconURLSelector = new StringSelector();
28 /// The last touch information triggering selected state change.
30 [EditorBrowsable(EditorBrowsableState.Never)]
31 protected Touch SelectionChangedByTouch { get; set; }
34 /// The ButtonExtension instance that is injected by ButtonStyle.
36 [EditorBrowsable(EditorBrowsableState.Never)]
37 protected ButtonExtension Extension { get; set; }
40 /// Creates Button's text part.
42 /// <return>The created Button's text part.</return>
43 [EditorBrowsable(EditorBrowsableState.Never)]
44 protected virtual TextLabel CreateText()
46 return new TextLabel();
50 /// Creates Button's icon part.
52 /// <return>The created Button's icon part.</return>
53 [EditorBrowsable(EditorBrowsableState.Never)]
54 protected virtual ImageView CreateIcon()
56 return new ImageView();
60 /// Creates Button's overlay image part.
62 /// <return>The created Button's overlay image part.</return>
63 [EditorBrowsable(EditorBrowsableState.Never)]
64 protected virtual ImageView CreateOverlayImage()
66 return new ImageView();
70 /// Called when the Button is Clicked by a user
72 /// <param name="eventArgs">The click information.</param>
73 [EditorBrowsable(EditorBrowsableState.Never)]
74 protected virtual void OnClick(ClickEventArgs eventArgs)
81 /// <returns>The default button style.</returns>
82 /// <since_tizen> 8 </since_tizen>
83 protected override ViewStyle CreateViewStyle()
85 return new ButtonStyle();
88 /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
89 [EditorBrowsable(EditorBrowsableState.Never)]
90 protected override void OnUpdate()
95 Extension?.OnRelayout(this);
99 /// Update Button State.
101 /// <param name="touchInfo">The touch information in case the state has changed by touching.</param>
102 /// <since_tizen> 6 </since_tizen>
103 /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
104 [EditorBrowsable(EditorBrowsableState.Never)]
105 protected void UpdateState()
107 ControlStates sourceState = ControlState;
108 ControlStates targetState;
115 targetState = ControlStates.Pressed;
120 targetState = ControlStates.Normal;
123 targetState |= (IsSelected ? ControlStates.Selected : 0);
125 // Focused, SelectedFocused
126 targetState |= (IsFocused ? ControlStates.Focused : 0);
132 targetState = ControlStates.Disabled;
134 // DisabledSelected, DisabledFocused
135 targetState |= (IsSelected ? ControlStates.Selected : (IsFocused ? ControlStates.Focused : 0));
138 if (sourceState != targetState)
140 ControlState = targetState;
144 StateChangedEventArgs e = new StateChangedEventArgs
146 PreviousState = sourceState,
147 CurrentState = targetState
149 stateChangeHander?.Invoke(this, e);
151 Extension?.OnControlStateChanged(this, new ControlStateChangedEventArgs(sourceState, targetState));
156 /// Measure text, it can be override.
158 /// <since_tizen> 6 </since_tizen>
159 /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
160 [EditorBrowsable(EditorBrowsableState.Never)]
161 protected virtual void MeasureText()
163 if (Style.IconRelativeOrientation == null || Icon == null || TextLabel == null)
167 TextLabel.WidthResizePolicy = ResizePolicyType.Fixed;
168 TextLabel.HeightResizePolicy = ResizePolicyType.Fixed;
169 int textPaddingStart = Style.TextPadding.Start;
170 int textPaddingEnd = Style.TextPadding.End;
171 int textPaddingTop = Style.TextPadding.Top;
172 int textPaddingBottom = Style.TextPadding.Bottom;
174 int iconPaddingStart = Style.IconPadding.Start;
175 int iconPaddingEnd = Style.IconPadding.End;
176 int iconPaddingTop = Style.IconPadding.Top;
177 int iconPaddingBottom = Style.IconPadding.Bottom;
179 if (IconRelativeOrientation == IconOrientation.Top || IconRelativeOrientation == IconOrientation.Bottom)
181 TextLabel.SizeWidth = SizeWidth - textPaddingStart - textPaddingEnd;
182 TextLabel.SizeHeight = SizeHeight - textPaddingTop - textPaddingBottom - iconPaddingTop - iconPaddingBottom - Icon.SizeHeight;
186 TextLabel.SizeWidth = SizeWidth - textPaddingStart - textPaddingEnd - iconPaddingStart - iconPaddingEnd - Icon.SizeWidth;
187 TextLabel.SizeHeight = SizeHeight - textPaddingTop - textPaddingBottom;
192 /// Layout child, it can be override.
194 /// <since_tizen> 6 </since_tizen>
195 /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
196 [EditorBrowsable(EditorBrowsableState.Never)]
197 protected virtual void LayoutChild()
199 if (Style.IconRelativeOrientation == null || Icon == null || TextLabel == null)
204 var buttonIcon = Icon;
205 var buttonText = TextLabel;
207 int textPaddingStart = Style.TextPadding.Start;
208 int textPaddingEnd = Style.TextPadding.End;
209 int textPaddingTop = Style.TextPadding.Top;
210 int textPaddingBottom = Style.TextPadding.Bottom;
212 int iconPaddingStart = Style.IconPadding.Start;
213 int iconPaddingEnd = Style.IconPadding.End;
214 int iconPaddingTop = Style.IconPadding.Top;
215 int iconPaddingBottom = Style.IconPadding.Bottom;
217 switch (IconRelativeOrientation)
219 case IconOrientation.Top:
220 buttonIcon.PositionUsesPivotPoint = true;
221 buttonIcon.ParentOrigin = NUI.ParentOrigin.TopCenter;
222 buttonIcon.PivotPoint = NUI.PivotPoint.TopCenter;
223 buttonIcon.Position2D = new Position2D(0, iconPaddingTop);
225 buttonText.PositionUsesPivotPoint = true;
226 buttonText.ParentOrigin = NUI.ParentOrigin.BottomCenter;
227 buttonText.PivotPoint = NUI.PivotPoint.BottomCenter;
228 buttonText.Position2D = new Position2D(0, -textPaddingBottom);
230 case IconOrientation.Bottom:
231 buttonIcon.PositionUsesPivotPoint = true;
232 buttonIcon.ParentOrigin = NUI.ParentOrigin.BottomCenter;
233 buttonIcon.PivotPoint = NUI.PivotPoint.BottomCenter;
234 buttonIcon.Position2D = new Position2D(0, -iconPaddingBottom);
236 buttonText.PositionUsesPivotPoint = true;
237 buttonText.ParentOrigin = NUI.ParentOrigin.TopCenter;
238 buttonText.PivotPoint = NUI.PivotPoint.TopCenter;
239 buttonText.Position2D = new Position2D(0, textPaddingTop);
241 case IconOrientation.Left:
242 if (LayoutDirection == ViewLayoutDirectionType.LTR)
244 buttonIcon.PositionUsesPivotPoint = true;
245 buttonIcon.ParentOrigin = NUI.ParentOrigin.CenterLeft;
246 buttonIcon.PivotPoint = NUI.PivotPoint.CenterLeft;
247 buttonIcon.Position2D = new Position2D(iconPaddingStart, 0);
249 buttonText.PositionUsesPivotPoint = true;
250 buttonText.ParentOrigin = NUI.ParentOrigin.CenterRight;
251 buttonText.PivotPoint = NUI.PivotPoint.CenterRight;
252 buttonText.Position2D = new Position2D(-textPaddingEnd, 0);
256 buttonIcon.PositionUsesPivotPoint = true;
257 buttonIcon.ParentOrigin = NUI.ParentOrigin.CenterRight;
258 buttonIcon.PivotPoint = NUI.PivotPoint.CenterRight;
259 buttonIcon.Position2D = new Position2D(-iconPaddingStart, 0);
261 buttonText.PositionUsesPivotPoint = true;
262 buttonText.ParentOrigin = NUI.ParentOrigin.CenterLeft;
263 buttonText.PivotPoint = NUI.PivotPoint.CenterLeft;
264 buttonText.Position2D = new Position2D(textPaddingEnd, 0);
268 case IconOrientation.Right:
269 if (LayoutDirection == ViewLayoutDirectionType.RTL)
271 buttonIcon.PositionUsesPivotPoint = true;
272 buttonIcon.ParentOrigin = NUI.ParentOrigin.CenterLeft;
273 buttonIcon.PivotPoint = NUI.PivotPoint.CenterLeft;
274 buttonIcon.Position2D = new Position2D(iconPaddingEnd, 0);
276 buttonText.PositionUsesPivotPoint = true;
277 buttonText.ParentOrigin = NUI.ParentOrigin.CenterRight;
278 buttonText.PivotPoint = NUI.PivotPoint.CenterRight;
279 buttonText.Position2D = new Position2D(-textPaddingStart, 0);
283 buttonIcon.PositionUsesPivotPoint = true;
284 buttonIcon.ParentOrigin = NUI.ParentOrigin.CenterRight;
285 buttonIcon.PivotPoint = NUI.PivotPoint.CenterRight;
286 buttonIcon.Position2D = new Position2D(-iconPaddingEnd, 0);
288 buttonText.PositionUsesPivotPoint = true;
289 buttonText.ParentOrigin = NUI.ParentOrigin.CenterLeft;
290 buttonText.PivotPoint = NUI.PivotPoint.CenterLeft;
291 buttonText.Position2D = new Position2D(textPaddingStart, 0);
297 if (string.IsNullOrEmpty(buttonText.Text))
299 buttonIcon.ParentOrigin = NUI.ParentOrigin.Center;
300 buttonIcon.PivotPoint = NUI.PivotPoint.Center;
305 /// Theme change callback when theme is changed, this callback will be trigger.
307 /// <param name="sender">The sender</param>
308 /// <param name="e">The event data</param>
309 /// <since_tizen> 8 </since_tizen>
310 protected override void OnThemeChangedEvent(object sender, StyleManager.ThemeChangeEventArgs e)
312 ButtonStyle buttonStyle = StyleManager.Instance.GetViewStyle(StyleName) as ButtonStyle;
313 if (buttonStyle != null)
315 Style.CopyFrom(buttonStyle);
321 /// Dispose Button and all children on it.
323 /// <param name="type">Dispose type.</param>
324 /// <since_tizen> 6 </since_tizen>
325 protected override void Dispose(DisposeTypes type)
332 if (type == DisposeTypes.Explicit)
334 Extension?.OnDispose(this);
338 Utility.Dispose(Icon);
340 if (TextLabel != null)
342 Utility.Dispose(TextLabel);
344 if (OverlayImage != null)
346 Utility.Dispose(OverlayImage);
354 [EditorBrowsable(EditorBrowsableState.Never)]
355 protected override void OnControlStateChanged(ControlStateChangedEventArgs controlStateChangedInfo)
357 base.OnControlStateChanged(controlStateChangedInfo);
359 var stateEnabled = !((controlStateChangedInfo.CurrentState & ControlStates.Disabled) == ControlStates.Disabled);
361 if (isEnabled != stateEnabled)
363 isEnabled = stateEnabled;
366 var stateSelected = (controlStateChangedInfo.CurrentState & ControlStates.Selected) == ControlStates.Selected;
368 if (isSelected != stateSelected)
370 isSelected = stateSelected;
373 var statePressed = (controlStateChangedInfo.CurrentState & ControlStates.Pressed) == ControlStates.Pressed;
375 if (isPressed != statePressed)
377 isPressed = statePressed;
382 /// It is hijack by using protected, style copy problem when class inherited from Button.
384 /// <since_tizen> 6 </since_tizen>
385 private void Initialize()
387 var style = (ButtonStyle)Style;
388 EnableControlStatePropagation = true;
390 LayoutDirectionChanged += OnLayoutDirectionChanged;
393 private void UpdateUIContent()
398 Sensitive = isEnabled;
401 private void OnLayoutDirectionChanged(object sender, LayoutDirectionChangedEventArgs e)
407 private void OnClickInternal(ClickEventArgs eventArgs)
409 Command?.Execute(CommandParameter);
411 Extension?.OnClick(this, eventArgs);
412 ClickEvent?.Invoke(this, eventArgs);
415 private void OnIconRelayout(object sender, EventArgs e)