2 * Copyright(c) 2020 Samsung Electronics Co., Ltd.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 using System.ComponentModel;
19 using Tizen.NUI.BaseComponents;
20 using Tizen.NUI.Binding;
21 using Tizen.NUI.Components.Extension;
23 namespace Tizen.NUI.Components
26 /// Button is one kind of common component, a button clearly describes what action will occur when the user selects it.
27 /// Button may contain text or an icon.
29 /// <since_tizen> 6 </since_tizen>
30 public partial class Button : Control
32 /// This will be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API.
33 [EditorBrowsable(EditorBrowsableState.Never)]
34 public static readonly BindableProperty IconRelativeOrientationProperty = BindableProperty.Create(nameof(IconRelativeOrientation), typeof(IconOrientation?), typeof(Button), null, propertyChanged: (bindable, oldValue, newValue) =>
36 var instance = (Button)bindable;
39 if (instance.Style != null && instance.Style.IconRelativeOrientation != (IconOrientation?)newValue)
41 instance.Style.IconRelativeOrientation = (IconOrientation?)newValue;
42 instance.UpdateUIContent();
46 defaultValueCreator: (bindable) =>
48 var instance = (Button)bindable;
49 return instance.Style?.IconRelativeOrientation;
51 /// This will be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API.
52 [EditorBrowsable(EditorBrowsableState.Never)]
53 public static readonly BindableProperty IsEnabledProperty = BindableProperty.Create(nameof(IsEnabled), typeof(bool), typeof(Button), true, propertyChanged: (bindable, oldValue, newValue) =>
55 var instance = (Button)bindable;
58 instance.isEnabled = (bool)newValue;
59 instance.UpdateState();
62 defaultValueCreator: (bindable) =>
64 var instance = (Button)bindable;
65 return instance.isEnabled;
67 /// This will be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API.
68 [EditorBrowsable(EditorBrowsableState.Never)]
69 public static readonly BindableProperty IsSelectedProperty = BindableProperty.Create(nameof(IsSelected), typeof(bool), typeof(Button), true, propertyChanged: (bindable, oldValue, newValue) =>
71 var instance = (Button)bindable;
74 instance.isSelected = (bool)newValue;
75 instance.UpdateState();
78 defaultValueCreator: (bindable) =>
80 var instance = (Button)bindable;
81 return instance.isSelected;
83 /// This will be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API.
84 [EditorBrowsable(EditorBrowsableState.Never)]
85 public static readonly BindableProperty IsSelectableProperty = BindableProperty.Create(nameof(IsSelectable), typeof(bool), typeof(Button), true, propertyChanged: (bindable, oldValue, newValue) =>
87 var instance = (Button)bindable;
90 instance.Style.IsSelectable = (bool)newValue;
93 defaultValueCreator: (bindable) =>
95 var instance = (Button)bindable;
96 return instance.Style?.IsSelectable ?? false;
98 /// This will be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API.
99 [EditorBrowsable(EditorBrowsableState.Never)]
100 public static readonly BindableProperty IconPaddingProperty = BindableProperty.Create(nameof(IconPadding), typeof(Extents), typeof(Button), null, propertyChanged: (bindable, oldValue, newValue) =>
102 var instance = (Button)bindable;
103 if (null != newValue && null != instance.Style?.IconPadding)
105 instance.Style.IconPadding.CopyFrom((Extents)newValue);
106 instance.UpdateUIContent();
109 defaultValueCreator: (bindable) =>
111 var instance = (Button)bindable;
112 return instance.Style?.IconPadding;
114 /// This will be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API.
115 [EditorBrowsable(EditorBrowsableState.Never)]
116 public static readonly BindableProperty TextPaddingProperty = BindableProperty.Create(nameof(TextPadding), typeof(Extents), typeof(Button), null, propertyChanged: (bindable, oldValue, newValue) =>
118 var instance = (Button)bindable;
119 if (null != newValue && null != instance.Style?.TextPadding)
121 instance.Style.TextPadding.CopyFrom((Extents)newValue);
122 instance.UpdateUIContent();
125 defaultValueCreator: (bindable) =>
127 var instance = (Button)bindable;
128 return instance.Style?.TextPadding;
134 /// Creates a new instance of a Button.
136 /// <since_tizen> 6 </since_tizen>
137 public Button() : base()
143 /// Creates a new instance of a Button with style.
145 /// <param name="style">Create Button by special style defined in UX.</param>
146 /// <since_tizen> 8 </since_tizen>
147 public Button(string style) : base(style)
153 /// Creates a new instance of a Button with style.
155 /// <param name="buttonStyle">Create Button by style customized by user.</param>
156 /// <since_tizen> 8 </since_tizen>
157 public Button(ButtonStyle buttonStyle) : base(buttonStyle)
163 /// An event for the button clicked signal which can be used to subscribe or unsubscribe the event handler provided by the user.<br />
165 /// <since_tizen> 6 </since_tizen>
166 public event EventHandler<ClickEventArgs> ClickEvent;
169 /// An event for the button state changed signal which can be used to subscribe or unsubscribe the event handler provided by the user.<br />
171 /// <since_tizen> 6 </since_tizen>
172 [Obsolete("Deprecated in API8; Will be removed in API10. Please use View.ControlStateChangedEvent")]
173 public event EventHandler<StateChangedEventArgs> StateChangedEvent
177 stateChangeHander += value;
181 stateChangeHander -= value;
186 /// Icon orientation.
188 /// <since_tizen> 6 </since_tizen>
189 public enum IconOrientation
194 /// <since_tizen> 6 </since_tizen>
199 /// <since_tizen> 6 </since_tizen>
204 /// <since_tizen> 6 </since_tizen>
209 /// <since_tizen> 6 </since_tizen>
214 /// Button's icon part.
216 /// <since_tizen> 8 </since_tizen>
217 public ImageView Icon
221 if (null == buttonIcon)
223 buttonIcon = CreateIcon();
224 if (null != Extension)
226 buttonIcon = Extension.OnCreateIcon(this, buttonIcon);
229 buttonIcon.Relayout += OnIconRelayout;
240 /// Button's overlay image part.
242 /// <since_tizen> 8 </since_tizen>
243 public ImageView OverlayImage
247 if (null == overlayImage)
249 overlayImage = CreateOverlayImage();
250 if (null != Extension)
252 overlayImage = Extension.OnCreateOverlayImage(this, overlayImage);
254 overlayImage.WidthResizePolicy = ResizePolicyType.FillToParent;
255 overlayImage.HeightResizePolicy = ResizePolicyType.FillToParent;
262 overlayImage = value;
267 /// Button's text part.
269 /// <since_tizen> 8 </since_tizen>
270 public TextLabel TextLabel
274 if (null == buttonText)
276 buttonText = CreateText();
277 if (null != Extension)
279 buttonText = Extension.OnCreateText(this, buttonText);
281 buttonText.HorizontalAlignment = HorizontalAlignment.Center;
282 buttonText.VerticalAlignment = VerticalAlignment.Center;
294 /// Return a copied Style instance of Button
297 /// It returns copied Style instance and changing it does not effect to the Button.
298 /// Style setting is possible by using constructor or the function of ApplyStyle(ViewStyle viewStyle)
300 /// <since_tizen> 8 </since_tizen>
301 public new ButtonStyle Style => ViewStyle as ButtonStyle;
304 /// The text of Button.
306 /// <since_tizen> 6 </since_tizen>
311 return Style?.Text?.Text?.GetValue(ControlState);
315 if (null != Style?.Text)
317 Style.Text.Text = value;
323 /// Flag to decide Button can be selected or not.
325 /// <since_tizen> 6 </since_tizen>
326 public bool IsSelectable
330 return (bool)GetValue(IsSelectableProperty);
334 SetValue(IsSelectableProperty, value);
339 /// Translate text string in Button.
341 /// <since_tizen> 6 </since_tizen>
342 public string TranslatableText
346 return Style?.Text?.TranslatableText?.All;
350 if (null != Style?.Text)
352 Style.Text.TranslatableText = value;
358 /// Text point size in Button.
360 /// <since_tizen> 6 </since_tizen>
361 public float PointSize
365 return Style?.Text?.PointSize?.All ?? 0;
369 if (null != Style?.Text)
371 Style.Text.PointSize = value;
377 /// Text font family in Button.
379 /// <since_tizen> 6 </since_tizen>
380 public string FontFamily
384 return Style?.Text?.FontFamily.All;
388 if (null != Style?.Text)
390 Style.Text.FontFamily = value;
396 /// Text color in Button.
398 /// <since_tizen> 6 </since_tizen>
399 public Color TextColor
403 return Style?.Text?.TextColor?.All;
407 if (null != Style?.Text)
409 Style.Text.TextColor = value;
415 /// Text horizontal alignment in Button.
417 /// <since_tizen> 6 </since_tizen>
418 public HorizontalAlignment TextAlignment
422 return Style?.Text?.HorizontalAlignment ?? HorizontalAlignment.Center;
426 if (null != Style?.Text)
428 Style.Text.HorizontalAlignment = value;
434 /// Icon image's resource url in Button.
436 /// <since_tizen> 6 </since_tizen>
437 public string IconURL
441 return Style?.Icon?.ResourceUrl?.All;
445 if (null != Style?.Icon)
447 Style.Icon.ResourceUrl = value;
453 /// Text string selector in Button.
455 /// <since_tizen> 6 </since_tizen>
456 public StringSelector TextSelector
464 if (value == null || textSelector == null)
466 Tizen.Log.Fatal("NUI", "[Exception] Button.TextSelector is null");
467 throw new NullReferenceException("Button.TextSelector is null");
471 textSelector.Clone(value);
477 /// Translateable text string selector in Button.
479 /// <since_tizen> 6 </since_tizen>
480 public StringSelector TranslatableTextSelector
484 return translatableTextSelector;
488 if (value == null || translatableTextSelector == null)
490 Tizen.Log.Fatal("NUI", "[Exception] Button.TranslatableTextSelector is null");
491 throw new NullReferenceException("Button.TranslatableTextSelector is null");
495 translatableTextSelector.Clone(value);
501 /// Text color selector in Button.
503 /// <since_tizen> 6 </since_tizen>
504 public ColorSelector TextColorSelector
508 return textColorSelector;
512 if (value == null || textColorSelector == null)
514 Tizen.Log.Fatal("NUI", "[Exception] Button.textColorSelector is null");
515 throw new NullReferenceException("Button.textColorSelector is null");
519 textColorSelector.Clone(value);
525 /// Text font size selector in Button.
527 /// <since_tizen> 6 </since_tizen>
528 public FloatSelector PointSizeSelector
532 return pointSizeSelector;
536 if (value == null || pointSizeSelector == null)
538 Tizen.Log.Fatal("NUI", "[Exception] Button.pointSizeSelector is null");
539 throw new NullReferenceException("Button.pointSizeSelector is null");
543 pointSizeSelector.Clone(value);
549 /// Icon image's resource url selector in Button.
551 /// <since_tizen> 6 </since_tizen>
552 public StringSelector IconURLSelector
556 return iconURLSelector;
560 if (value == null || iconURLSelector == null)
562 Tizen.Log.Fatal("NUI", "[Exception] Button.iconURLSelector is null");
563 throw new NullReferenceException("Button.iconURLSelector is null");
567 iconURLSelector.Clone(value);
573 /// Flag to decide selected state in Button.
575 /// <since_tizen> 6 </since_tizen>
576 public bool IsSelected
580 return (bool)GetValue(IsSelectedProperty);
584 SetValue(IsSelectedProperty, value);
589 /// Flag to decide enable or disable in Button.
591 /// <since_tizen> 6 </since_tizen>
592 public bool IsEnabled
596 return (bool)GetValue(IsEnabledProperty);
600 SetValue(IsEnabledProperty, value);
605 /// Icon relative orientation in Button, work only when show icon and text.
607 /// <since_tizen> 8 </since_tizen>
608 public IconOrientation? IconRelativeOrientation
612 return (IconOrientation?)GetValue(IconRelativeOrientationProperty);
616 SetValue(IconRelativeOrientationProperty, value);
621 /// Icon padding in Button, work only when show icon and text.
623 /// <since_tizen> 6 </since_tizen>
624 public Extents IconPadding
626 get => (Extents)GetValue(IconPaddingProperty);
627 set => SetValue(IconPaddingProperty, value);
631 /// Text padding in Button, work only when show icon and text.
633 /// <since_tizen> 6 </since_tizen>
634 public Extents TextPadding
636 get => (Extents)GetValue(TextPaddingProperty);
637 set => SetValue(TextPaddingProperty, value);
641 /// Called after a key event is received by the view that has had its focus set.
643 /// <param name="key">The key event.</param>
644 /// <returns>True if the key event should be consumed.</returns>
645 /// <since_tizen> 6 </since_tizen>
646 public override bool OnKey(Key key)
648 if (!IsEnabled || null == key)
653 if (key.State == Key.StateType.Down)
655 if (key.KeyPressedName == "Return")
661 else if (key.State == Key.StateType.Up)
663 if (key.KeyPressedName == "Return")
665 bool clicked = isPressed && isEnabled;
669 if (Style.IsSelectable != null && Style.IsSelectable == true)
671 IsSelected = !IsSelected;
680 ClickEventArgs eventArgs = new ClickEventArgs();
681 OnClickInternal(eventArgs);
685 return base.OnKey(key);
689 /// Called when the control gain key input focus. Should be overridden by derived classes if they need to customize what happens when the focus is gained.
691 /// <since_tizen> 8 </since_tizen>
692 public override void OnFocusGained()
694 base.OnFocusGained();
699 /// Called when the control loses key input focus. Should be overridden by derived classes if they need to customize what happens when the focus is lost.
701 /// <since_tizen> 8 </since_tizen>
702 public override void OnFocusLost()
709 /// Called after a touch event is received by the owning view.<br />
710 /// CustomViewBehaviour.REQUIRES_TOUCH_EVENTS must be enabled during construction. See CustomView(ViewWrapperImpl.CustomViewBehaviour behaviour).<br />
712 /// <param name="touch">The touch event.</param>
713 /// <returns>True if the event should be consumed.</returns>
714 /// <since_tizen> 8 </since_tizen>
715 public override bool OnTouch(Touch touch)
717 if (!IsEnabled || null == touch)
722 PointStateType state = touch.GetState(0);
726 case PointStateType.Down:
728 Extension?.SetTouchInfo(touch);
731 case PointStateType.Interrupted:
735 case PointStateType.Up:
737 bool clicked = isPressed && isEnabled;
741 if (Style.IsSelectable != null && Style.IsSelectable == true)
743 Extension?.SetTouchInfo(touch);
744 IsSelected = !IsSelected;
748 Extension?.SetTouchInfo(touch);
754 ClickEventArgs eventArgs = new ClickEventArgs();
755 OnClickInternal(eventArgs);
763 return base.OnTouch(touch);
767 /// Apply style to button.
769 /// <param name="viewStyle">The style to apply.</param>
770 /// <since_tizen> 8 </since_tizen>
771 public override void ApplyStyle(ViewStyle viewStyle)
773 base.ApplyStyle(viewStyle);
775 ButtonStyle buttonStyle = viewStyle as ButtonStyle;
776 if (null != buttonStyle)
778 Extension = buttonStyle.CreateExtension();
779 if (buttonStyle.Overlay != null)
781 OverlayImage?.ApplyStyle(buttonStyle.Overlay);
784 if (buttonStyle.Text != null)
786 TextLabel?.ApplyStyle(buttonStyle.Text);
789 if (buttonStyle.Icon != null)
791 Icon?.ApplyStyle(buttonStyle.Icon);
797 /// ClickEventArgs is a class to record button click event arguments which will sent to user.
799 /// <since_tizen> 6 </since_tizen>
800 public class ClickEventArgs : EventArgs
805 /// StateChangeEventArgs is a class to record button state change event arguments which will sent to user.
807 /// <since_tizen> 6 </since_tizen>
808 [Obsolete("Deprecated in API8; Will be removed in API10. Please use View.ControlStateChangedEventArgs")]
809 public class StateChangedEventArgs : EventArgs
811 /// <summary> previous state of Button </summary>
812 /// <since_tizen> 6 </since_tizen>
813 [Obsolete("Deprecated in API8; Will be removed in API10")]
814 public ControlStates PreviousState;
815 /// <summary> current state of Button </summary>
816 /// <since_tizen> 6 </since_tizen>
817 [Obsolete("Deprecated in API8; Will be removed in API10")]
818 public ControlStates CurrentState;
822 /// Get current text part to the attached ButtonExtension.
825 /// It returns null if the passed extension is invaild.
827 /// <param name="extension">The extension instance that is currently attached to this Button.</param>
828 /// <return>The button's text part.</return>
829 [EditorBrowsable(EditorBrowsableState.Never)]
830 public TextLabel GetCurrentText(ButtonExtension extension)
832 return (extension == Extension) ? TextLabel : null;
836 /// Get current icon part to the attached ButtonExtension.
839 /// It returns null if the passed extension is invaild.
841 /// <param name="extension">The extension instance that is currently attached to this Button.</param>
842 /// <return>The button's icon part.</return>
843 [EditorBrowsable(EditorBrowsableState.Never)]
844 public ImageView GetCurrentIcon(ButtonExtension extension)
846 return (extension == Extension) ? Icon : null;
850 /// Get current overlay image part to the attached ButtonExtension.
853 /// It returns null if the passed extension is invaild.
855 /// <param name="extension">The extension instance that is currently attached to this Button.</param>
856 /// <return>The button's overlay image part.</return>
857 [EditorBrowsable(EditorBrowsableState.Never)]
858 public ImageView GetCurrentOverlayImage(ButtonExtension extension)
860 return (extension == Extension) ? OverlayImage : null;