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 ControlState sourceState = ControlState;
108 ControlState targetState;
115 targetState = ControlState.Pressed;
120 targetState = ControlState.Normal;
123 if (IsSelected) targetState += ControlState.Selected;
125 // Focused, SelectedFocused
126 if (IsFocused) targetState += ControlState.Focused;
132 targetState = ControlState.Disabled;
135 if (IsSelected) targetState += ControlState.Selected;
137 else if (IsFocused) targetState += ControlState.Focused;
140 if (sourceState != targetState)
142 ControlState = targetState;
145 StateChangedEventArgs e = new StateChangedEventArgs
147 PreviousState = ControlStatesExtension.FromControlStateClass(sourceState),
148 CurrentState = ControlStatesExtension.FromControlStateClass(targetState)
150 stateChangeHander?.Invoke(this, e);
152 Extension?.OnControlStateChanged(this, new ControlStateChangedEventArgs(sourceState, targetState));
157 /// Measure text, it can be override.
159 /// <since_tizen> 6 </since_tizen>
160 /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
161 [EditorBrowsable(EditorBrowsableState.Never)]
162 protected virtual void MeasureText()
164 if (Style.IconRelativeOrientation == null || Icon == null || TextLabel == null)
168 TextLabel.WidthResizePolicy = ResizePolicyType.Fixed;
169 TextLabel.HeightResizePolicy = ResizePolicyType.Fixed;
170 int textPaddingStart = Style.TextPadding.Start;
171 int textPaddingEnd = Style.TextPadding.End;
172 int textPaddingTop = Style.TextPadding.Top;
173 int textPaddingBottom = Style.TextPadding.Bottom;
175 int iconPaddingStart = Style.IconPadding.Start;
176 int iconPaddingEnd = Style.IconPadding.End;
177 int iconPaddingTop = Style.IconPadding.Top;
178 int iconPaddingBottom = Style.IconPadding.Bottom;
180 if (IconRelativeOrientation == IconOrientation.Top || IconRelativeOrientation == IconOrientation.Bottom)
182 TextLabel.SizeWidth = SizeWidth - textPaddingStart - textPaddingEnd;
183 TextLabel.SizeHeight = SizeHeight - textPaddingTop - textPaddingBottom - iconPaddingTop - iconPaddingBottom - Icon.SizeHeight;
187 TextLabel.SizeWidth = SizeWidth - textPaddingStart - textPaddingEnd - iconPaddingStart - iconPaddingEnd - Icon.SizeWidth;
188 TextLabel.SizeHeight = SizeHeight - textPaddingTop - textPaddingBottom;
193 /// Layout child, it can be override.
195 /// <since_tizen> 6 </since_tizen>
196 /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
197 [EditorBrowsable(EditorBrowsableState.Never)]
198 protected virtual void LayoutChild()
200 if (Style.IconRelativeOrientation == null || Icon == null || TextLabel == null)
205 var buttonIcon = Icon;
206 var buttonText = TextLabel;
208 int textPaddingStart = Style.TextPadding.Start;
209 int textPaddingEnd = Style.TextPadding.End;
210 int textPaddingTop = Style.TextPadding.Top;
211 int textPaddingBottom = Style.TextPadding.Bottom;
213 int iconPaddingStart = Style.IconPadding.Start;
214 int iconPaddingEnd = Style.IconPadding.End;
215 int iconPaddingTop = Style.IconPadding.Top;
216 int iconPaddingBottom = Style.IconPadding.Bottom;
218 switch (IconRelativeOrientation)
220 case IconOrientation.Top:
221 buttonIcon.PositionUsesPivotPoint = true;
222 buttonIcon.ParentOrigin = NUI.ParentOrigin.TopCenter;
223 buttonIcon.PivotPoint = NUI.PivotPoint.TopCenter;
224 buttonIcon.Position2D = new Position2D(0, iconPaddingTop);
226 buttonText.PositionUsesPivotPoint = true;
227 buttonText.ParentOrigin = NUI.ParentOrigin.BottomCenter;
228 buttonText.PivotPoint = NUI.PivotPoint.BottomCenter;
229 buttonText.Position2D = new Position2D(0, -textPaddingBottom);
231 case IconOrientation.Bottom:
232 buttonIcon.PositionUsesPivotPoint = true;
233 buttonIcon.ParentOrigin = NUI.ParentOrigin.BottomCenter;
234 buttonIcon.PivotPoint = NUI.PivotPoint.BottomCenter;
235 buttonIcon.Position2D = new Position2D(0, -iconPaddingBottom);
237 buttonText.PositionUsesPivotPoint = true;
238 buttonText.ParentOrigin = NUI.ParentOrigin.TopCenter;
239 buttonText.PivotPoint = NUI.PivotPoint.TopCenter;
240 buttonText.Position2D = new Position2D(0, textPaddingTop);
242 case IconOrientation.Left:
243 if (LayoutDirection == ViewLayoutDirectionType.LTR)
245 buttonIcon.PositionUsesPivotPoint = true;
246 buttonIcon.ParentOrigin = NUI.ParentOrigin.CenterLeft;
247 buttonIcon.PivotPoint = NUI.PivotPoint.CenterLeft;
248 buttonIcon.Position2D = new Position2D(iconPaddingStart, 0);
250 buttonText.PositionUsesPivotPoint = true;
251 buttonText.ParentOrigin = NUI.ParentOrigin.CenterRight;
252 buttonText.PivotPoint = NUI.PivotPoint.CenterRight;
253 buttonText.Position2D = new Position2D(-textPaddingEnd, 0);
257 buttonIcon.PositionUsesPivotPoint = true;
258 buttonIcon.ParentOrigin = NUI.ParentOrigin.CenterRight;
259 buttonIcon.PivotPoint = NUI.PivotPoint.CenterRight;
260 buttonIcon.Position2D = new Position2D(-iconPaddingStart, 0);
262 buttonText.PositionUsesPivotPoint = true;
263 buttonText.ParentOrigin = NUI.ParentOrigin.CenterLeft;
264 buttonText.PivotPoint = NUI.PivotPoint.CenterLeft;
265 buttonText.Position2D = new Position2D(textPaddingEnd, 0);
269 case IconOrientation.Right:
270 if (LayoutDirection == ViewLayoutDirectionType.RTL)
272 buttonIcon.PositionUsesPivotPoint = true;
273 buttonIcon.ParentOrigin = NUI.ParentOrigin.CenterLeft;
274 buttonIcon.PivotPoint = NUI.PivotPoint.CenterLeft;
275 buttonIcon.Position2D = new Position2D(iconPaddingEnd, 0);
277 buttonText.PositionUsesPivotPoint = true;
278 buttonText.ParentOrigin = NUI.ParentOrigin.CenterRight;
279 buttonText.PivotPoint = NUI.PivotPoint.CenterRight;
280 buttonText.Position2D = new Position2D(-textPaddingStart, 0);
284 buttonIcon.PositionUsesPivotPoint = true;
285 buttonIcon.ParentOrigin = NUI.ParentOrigin.CenterRight;
286 buttonIcon.PivotPoint = NUI.PivotPoint.CenterRight;
287 buttonIcon.Position2D = new Position2D(-iconPaddingEnd, 0);
289 buttonText.PositionUsesPivotPoint = true;
290 buttonText.ParentOrigin = NUI.ParentOrigin.CenterLeft;
291 buttonText.PivotPoint = NUI.PivotPoint.CenterLeft;
292 buttonText.Position2D = new Position2D(textPaddingStart, 0);
298 if (string.IsNullOrEmpty(buttonText.Text))
300 buttonIcon.ParentOrigin = NUI.ParentOrigin.Center;
301 buttonIcon.PivotPoint = NUI.PivotPoint.Center;
306 /// Theme change callback when theme is changed, this callback will be trigger.
308 /// <param name="sender">The sender</param>
309 /// <param name="e">The event data</param>
310 [EditorBrowsable(EditorBrowsableState.Never)]
311 protected override void OnThemeChangedEvent(object sender, StyleManager.ThemeChangeEventArgs e)
313 ButtonStyle buttonStyle = StyleManager.Instance.GetViewStyle(StyleName) as ButtonStyle;
314 if (buttonStyle != null)
316 Style.CopyFrom(buttonStyle);
322 /// Dispose Button and all children on it.
324 /// <param name="type">Dispose type.</param>
325 /// <since_tizen> 6 </since_tizen>
326 protected override void Dispose(DisposeTypes type)
333 if (type == DisposeTypes.Explicit)
335 Extension?.OnDispose(this);
339 Utility.Dispose(Icon);
341 if (TextLabel != null)
343 Utility.Dispose(TextLabel);
345 if (OverlayImage != null)
347 Utility.Dispose(OverlayImage);
355 [EditorBrowsable(EditorBrowsableState.Never)]
356 protected override void OnControlStateChanged(ControlStateChangedEventArgs controlStateChangedInfo)
358 base.OnControlStateChanged(controlStateChangedInfo);
360 var stateEnabled = !controlStateChangedInfo.CurrentState.Contains(ControlState.Disabled);
362 if (isEnabled != stateEnabled)
364 isEnabled = stateEnabled;
367 var statePressed = controlStateChangedInfo.CurrentState.Contains(ControlState.Pressed);
369 if (isPressed != statePressed)
371 isPressed = statePressed;
376 /// It is hijack by using protected, style copy problem when class inherited from Button.
378 /// <since_tizen> 6 </since_tizen>
379 private void Initialize()
381 var style = (ButtonStyle)Style;
382 EnableControlStatePropagation = true;
384 LayoutDirectionChanged += OnLayoutDirectionChanged;
387 private void UpdateUIContent()
392 Sensitive = isEnabled;
395 private void OnLayoutDirectionChanged(object sender, LayoutDirectionChangedEventArgs e)
401 private void OnClickInternal(ClickEventArgs eventArgs)
403 Command?.Execute(CommandParameter);
405 Extension?.OnClick(this, eventArgs);
406 ClickEvent?.Invoke(this, eventArgs);
409 private void OnIconRelayout(object sender, EventArgs e)