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;
22 using Tizen.NUI.Accessibility;
24 namespace Tizen.NUI.Components
27 /// ClickedEventArgs is a class to record button click event arguments which will sent to user.
29 /// <since_tizen> 8 </since_tizen>
30 public class ClickedEventArgs : EventArgs
35 /// SelectedChangedEventArgs is a class to record item selected arguments which will sent to user.
37 /// <since_tizen> 8 </since_tizen>
38 public class SelectedChangedEventArgs : EventArgs
40 /// <summary> Selected state </summary>
41 /// <since_tizen> 8 </since_tizen>
42 public bool IsSelected { get; set; }
46 /// Button is one kind of common component, a button clearly describes what action will occur when the user selects it.
47 /// Button may contain text or an icon.
49 /// <since_tizen> 6 </since_tizen>
50 public partial class Button : Control
52 /// This will be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API.
53 [EditorBrowsable(EditorBrowsableState.Never)]
54 public static readonly BindableProperty IconRelativeOrientationProperty = BindableProperty.Create(nameof(IconRelativeOrientation), typeof(IconOrientation?), typeof(Button), null, propertyChanged: (bindable, oldValue, newValue) =>
56 var instance = (Button)bindable;
59 if (instance.buttonStyle != null && instance.buttonStyle.IconRelativeOrientation != (IconOrientation?)newValue)
61 instance.buttonStyle.IconRelativeOrientation = (IconOrientation?)newValue;
62 instance.UpdateUIContent();
66 defaultValueCreator: (bindable) =>
68 var instance = (Button)bindable;
69 return instance.buttonStyle?.IconRelativeOrientation;
71 /// This will be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API.
72 [EditorBrowsable(EditorBrowsableState.Never)]
73 public static readonly BindableProperty IsEnabledProperty = BindableProperty.Create(nameof(IsEnabled), typeof(bool), typeof(Button), true, propertyChanged: (bindable, oldValue, newValue) =>
75 var instance = (Button)bindable;
78 if (instance.buttonStyle != null && (!instance.styleApplied || instance.buttonStyle.IsEnabled != (bool)newValue))
80 instance.buttonStyle.IsEnabled = (bool)newValue;
81 instance.UpdateState();
85 defaultValueCreator: (bindable) =>
87 var instance = (Button)bindable;
88 return instance.buttonStyle?.IsEnabled ?? true;
90 /// This will be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API.
91 [EditorBrowsable(EditorBrowsableState.Never)]
92 public static readonly BindableProperty IsSelectedProperty = BindableProperty.Create(nameof(IsSelected), typeof(bool), typeof(Button), true, propertyChanged: (bindable, oldValue, newValue) =>
94 var instance = (Button)bindable;
97 if (instance.buttonStyle != null && instance.IsSelectable && (!instance.styleApplied || instance.buttonStyle.IsSelected != (bool)newValue))
99 instance.buttonStyle.IsSelected = (bool)newValue;
100 instance.UpdateState();
104 defaultValueCreator: (bindable) =>
106 var instance = (Button)bindable;
107 return instance.IsSelectable && (instance.buttonStyle.IsSelected ?? false);
109 /// This will be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API.
110 [EditorBrowsable(EditorBrowsableState.Never)]
111 public static readonly BindableProperty IsSelectableProperty = BindableProperty.Create(nameof(IsSelectable), typeof(bool), typeof(Button), true, propertyChanged: (bindable, oldValue, newValue) =>
113 var instance = (Button)bindable;
114 if (newValue != null)
116 if (instance.buttonStyle != null && (!instance.styleApplied || instance.buttonStyle.IsSelectable != (bool)newValue))
118 instance.buttonStyle.IsSelectable = (bool)newValue;
122 defaultValueCreator: (bindable) =>
124 var instance = (Button)bindable;
125 return instance.buttonStyle?.IsSelectable ?? false;
127 /// This will be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API.
128 [EditorBrowsable(EditorBrowsableState.Never)]
129 public static readonly BindableProperty IconPaddingProperty = BindableProperty.Create(nameof(IconPadding), typeof(Extents), typeof(Button), null, propertyChanged: (bindable, oldValue, newValue) =>
131 var instance = (Button)bindable;
132 if (null != newValue && null != instance.buttonStyle?.IconPadding)
134 instance.buttonStyle.IconPadding.CopyFrom((Extents)newValue);
135 instance.UpdateUIContent();
138 defaultValueCreator: (bindable) =>
140 var instance = (Button)bindable;
141 return instance.buttonStyle?.IconPadding;
143 /// This will be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API.
144 [EditorBrowsable(EditorBrowsableState.Never)]
145 public static readonly BindableProperty TextPaddingProperty = BindableProperty.Create(nameof(TextPadding), typeof(Extents), typeof(Button), null, propertyChanged: (bindable, oldValue, newValue) =>
147 var instance = (Button)bindable;
148 if (null != newValue && null != instance.buttonStyle?.TextPadding)
150 instance.buttonStyle.TextPadding.CopyFrom((Extents)newValue);
151 instance.UpdateUIContent();
154 defaultValueCreator: (bindable) =>
156 var instance = (Button)bindable;
157 return instance.buttonStyle?.TextPadding;
163 /// Creates a new instance of a Button.
165 /// <since_tizen> 6 </since_tizen>
166 public Button() : base()
172 /// Creates a new instance of a Button with style.
174 /// <param name="style">Create Button by special style defined in UX.</param>
175 /// <since_tizen> 8 </since_tizen>
176 public Button(string style) : base(style)
182 /// Creates a new instance of a Button with style.
184 /// <param name="buttonStyle">Create Button by style customized by user.</param>
185 /// <since_tizen> 8 </since_tizen>
186 public Button(ButtonStyle buttonStyle) : base(buttonStyle)
192 /// An event for the button clicked signal which can be used to subscribe or unsubscribe the event handler provided by the user.<br />
194 /// <since_tizen> 6 </since_tizen>
195 [Obsolete("Deprecated in API8; Will be removed in API10. Please use Clicked event instead.")]
196 public event EventHandler<ClickEventArgs> ClickEvent;
199 /// An event for the button clicked signal which can be used to subscribe or unsubscribe the event handler provided by the user.
201 /// <since_tizen> 8 </since_tizen>
202 public event EventHandler<ClickedEventArgs> Clicked;
205 /// An event for the button state changed signal which can be used to subscribe or unsubscribe the event handler provided by the user.<br />
207 /// <since_tizen> 6 </since_tizen>
208 [Obsolete("Deprecated in API8; Will be removed in API10. Please use View.ControlStateChangedEvent")]
209 public event EventHandler<StateChangedEventArgs> StateChangedEvent
213 stateChangeHandler += value;
217 stateChangeHandler -= value;
222 /// Icon orientation.
224 /// <since_tizen> 6 </since_tizen>
225 public enum IconOrientation
230 /// <since_tizen> 6 </since_tizen>
235 /// <since_tizen> 6 </since_tizen>
240 /// <since_tizen> 6 </since_tizen>
245 /// <since_tizen> 6 </since_tizen>
250 /// Button's icon part.
252 /// <since_tizen> 8 </since_tizen>
253 public ImageView Icon
257 if (null == buttonIcon)
259 buttonIcon = CreateIcon();
260 if (null != Extension)
262 buttonIcon = Extension.OnCreateIcon(this, buttonIcon);
265 buttonIcon.Relayout += OnIconRelayout;
276 /// Button's overlay image part.
278 /// <since_tizen> 8 </since_tizen>
279 public ImageView OverlayImage
283 if (null == overlayImage)
285 overlayImage = CreateOverlayImage();
286 if (null != Extension)
288 overlayImage = Extension.OnCreateOverlayImage(this, overlayImage);
290 overlayImage.WidthResizePolicy = ResizePolicyType.FillToParent;
291 overlayImage.HeightResizePolicy = ResizePolicyType.FillToParent;
298 overlayImage = value;
303 /// Button's text part.
305 /// <since_tizen> 8 </since_tizen>
306 public TextLabel TextLabel
310 if (null == buttonText)
312 buttonText = CreateText();
313 if (null != Extension)
315 buttonText = Extension.OnCreateText(this, buttonText);
317 buttonText.HorizontalAlignment = HorizontalAlignment.Center;
318 buttonText.VerticalAlignment = VerticalAlignment.Center;
326 AccessibilityManager.Instance.SetAccessibilityAttribute(this, AccessibilityManager.AccessibilityAttribute.Label, buttonText.Text);
331 /// Return a copied Style instance of Button
334 /// It returns copied Style instance and changing it does not effect to the Button.
335 /// Style setting is possible by using constructor or the function of ApplyStyle(ViewStyle viewStyle)
337 /// <since_tizen> 8 </since_tizen>
338 public new ButtonStyle Style
342 var result = (ButtonStyle)ViewStyle.Clone();
343 result.CopyPropertiesFromView(this);
344 result.Text.CopyPropertiesFromView(TextLabel);
345 result.Icon.CopyPropertiesFromView(Icon);
346 result.Overlay.CopyPropertiesFromView(OverlayImage);
352 /// The text of Button.
354 /// <since_tizen> 6 </since_tizen>
359 return TextLabel.Text;
363 TextLabel.Text = value;
368 /// Flag to decide Button can be selected or not.
370 /// <since_tizen> 6 </since_tizen>
371 public bool IsSelectable
375 return (bool)GetValue(IsSelectableProperty);
379 SetValue(IsSelectableProperty, value);
384 /// Translate text string in Button.
386 /// <since_tizen> 6 </since_tizen>
387 public string TranslatableText
391 return TextLabel.TranslatableText;
395 TextLabel.TranslatableText = value;
400 /// Text point size in Button.
402 /// <since_tizen> 6 </since_tizen>
403 public float PointSize
407 return TextLabel.PointSize;
411 TextLabel.PointSize = value;
416 /// Text font family in Button.
418 /// <since_tizen> 6 </since_tizen>
419 public string FontFamily
423 return TextLabel.FontFamily;
427 TextLabel.FontFamily = value;
432 /// Text color in Button.
434 /// <since_tizen> 6 </since_tizen>
435 public Color TextColor
439 return TextLabel.TextColor;
443 TextLabel.TextColor = value;
448 /// Text horizontal alignment in Button.
450 /// <since_tizen> 6 </since_tizen>
451 public HorizontalAlignment TextAlignment
455 return TextLabel.HorizontalAlignment;
459 TextLabel.HorizontalAlignment = value;
464 /// Icon image's resource url in Button.
466 /// <since_tizen> 6 </since_tizen>
467 public string IconURL
471 return Icon.ResourceUrl;
475 Icon.ResourceUrl = value;
480 /// Text string selector in Button.
481 /// Getter returns copied selector value if exist, null otherwise.
482 /// <exception cref="NullReferenceException">Thrown when setting null value.</exception>
484 /// <since_tizen> 6 </since_tizen>
485 public StringSelector TextSelector
487 get => buttonText == null ? null : new StringSelector((Selector<string>)buttonText.GetValue(TextLabel.TextSelectorProperty));
490 if (value == null || buttonText == null)
492 throw new NullReferenceException("Button.TextSelector is null");
496 buttonText.SetValue(TextLabel.TextSelectorProperty, value);
502 /// Translateable text string selector in Button.
503 /// Getter returns copied selector value if exist, null otherwise.
505 /// <exception cref="NullReferenceException">Thrown when setting null value.</exception>
506 /// <since_tizen> 6 </since_tizen>
507 public StringSelector TranslatableTextSelector
509 get => buttonText == null ? null : new StringSelector((Selector<string>)buttonText.GetValue(TextLabel.TranslatableTextSelectorProperty));
512 if (value == null || buttonText == null)
514 throw new NullReferenceException("Button.TranslatableTextSelector is null");
518 buttonText.SetValue(TextLabel.TranslatableTextSelectorProperty, value);
524 /// Text color selector in Button.
525 /// Getter returns copied selector value if exist, null otherwise.
527 /// <exception cref="NullReferenceException">Thrown when setting null value.</exception>
528 /// <since_tizen> 6 </since_tizen>
529 public ColorSelector TextColorSelector
531 get => buttonText == null ? null : new ColorSelector((Selector<Color>)buttonText.GetValue(TextLabel.TextColorSelectorProperty));
534 if (value == null || buttonText == null)
536 throw new NullReferenceException("Button.TextColorSelectorProperty is null");
540 buttonText.SetValue(TextLabel.TextColorSelectorProperty, value);
546 /// Text font size selector in Button.
547 /// Getter returns copied selector value if exist, null otherwise.
549 /// <exception cref="NullReferenceException">Thrown when setting null value.</exception>
550 /// <since_tizen> 6 </since_tizen>
551 public FloatSelector PointSizeSelector
553 get => buttonText == null ? null : new FloatSelector((Selector<float?>)buttonText.GetValue(TextLabel.PointSizeSelectorProperty));
556 if (value == null || buttonText == null)
558 throw new NullReferenceException("Button.PointSizeSelector is null");
562 buttonText.SetValue(TextLabel.PointSizeSelectorProperty, value);
568 /// Icon image's resource url selector in Button.
569 /// Getter returns copied selector value if exist, null otherwise.
571 /// <exception cref="NullReferenceException">Thrown when setting null value.</exception>
572 /// <since_tizen> 6 </since_tizen>
573 public StringSelector IconURLSelector
575 get => buttonIcon == null ? null : new StringSelector((Selector<string>)buttonIcon.GetValue(ImageView.ResourceUrlSelectorProperty));
578 if (value == null || buttonIcon == null)
580 throw new NullReferenceException("Button.IconURLSelector is null");
584 buttonIcon.SetValue(ImageView.ResourceUrlSelectorProperty, value);
590 /// Flag to decide selected state in Button.
592 /// <since_tizen> 6 </since_tizen>
593 public bool IsSelected
597 return (bool)GetValue(IsSelectedProperty);
601 SetValue(IsSelectedProperty, value);
606 /// Flag to decide enable or disable in Button.
608 /// <since_tizen> 6 </since_tizen>
609 public bool IsEnabled
613 return (bool)GetValue(IsEnabledProperty);
617 SetValue(IsEnabledProperty, value);
622 /// Icon relative orientation in Button, work only when show icon and text.
624 /// <since_tizen> 8 </since_tizen>
625 public IconOrientation? IconRelativeOrientation
629 return (IconOrientation?)GetValue(IconRelativeOrientationProperty);
633 SetValue(IconRelativeOrientationProperty, value);
638 /// Icon padding in Button, work only when show icon and text.
640 /// <since_tizen> 6 </since_tizen>
641 public Extents IconPadding
643 get => (Extents)GetValue(IconPaddingProperty);
644 set => SetValue(IconPaddingProperty, value);
648 /// Text padding in Button, work only when show icon and text.
650 /// <since_tizen> 6 </since_tizen>
651 public Extents TextPadding
653 get => (Extents)GetValue(TextPaddingProperty);
654 set => SetValue(TextPaddingProperty, value);
657 private ButtonStyle buttonStyle => ViewStyle as ButtonStyle;
660 /// Called after a key event is received by the view that has had its focus set.
662 /// <param name="key">The key event.</param>
663 /// <returns>True if the key event should be consumed.</returns>
664 /// <since_tizen> 6 </since_tizen>
665 public override bool OnKey(Key key)
667 if (!IsEnabled || null == key)
672 if (key.State == Key.StateType.Down)
674 if (key.KeyPressedName == "Return")
680 else if (key.State == Key.StateType.Up)
682 if (key.KeyPressedName == "Return")
684 bool clicked = isPressed && IsEnabled;
690 IsSelected = !IsSelected;
699 ClickedEventArgs eventArgs = new ClickedEventArgs();
700 OnClickedInternal(eventArgs);
704 return base.OnKey(key);
708 /// 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.
710 /// <since_tizen> 8 </since_tizen>
711 public override void OnFocusGained()
713 base.OnFocusGained();
718 /// 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.
720 /// <since_tizen> 8 </since_tizen>
721 public override void OnFocusLost()
728 /// Called after a touch event is received by the owning view.<br />
729 /// CustomViewBehaviour.REQUIRES_TOUCH_EVENTS must be enabled during construction. See CustomView(ViewWrapperImpl.CustomViewBehaviour behaviour).<br />
731 /// <param name="touch">The touch event.</param>
732 /// <returns>True if the event should be consumed.</returns>
733 /// <since_tizen> 8 </since_tizen>
734 [Obsolete("Deprecated in API8; Will be removed in API10. Please use OnClicked instead.")]
735 public override bool OnTouch(Touch touch)
737 return base.OnTouch(touch);
741 /// Apply style to button.
743 /// <param name="viewStyle">The style to apply.</param>
744 /// <since_tizen> 8 </since_tizen>
745 public override void ApplyStyle(ViewStyle viewStyle)
747 styleApplied = false;
749 base.ApplyStyle(viewStyle);
751 if (null != buttonStyle)
753 Extension = buttonStyle.CreateExtension();
754 if (buttonStyle.Overlay != null)
756 OverlayImage?.ApplyStyle(buttonStyle.Overlay);
759 if (buttonStyle.Text != null)
761 TextLabel?.ApplyStyle(buttonStyle.Text);
764 if (buttonStyle.Icon != null)
766 Icon?.ApplyStyle(buttonStyle.Icon);
774 /// ClickEventArgs is a class to record button click event arguments which will sent to user.
776 /// <since_tizen> 6 </since_tizen>
777 [Obsolete("Deprecated in API8; Will be removed in API10. Please use ClickedEventArgs instead.")]
778 public class ClickEventArgs : EventArgs
783 /// StateChangeEventArgs is a class to record button state change event arguments which will sent to user.
785 /// <since_tizen> 6 </since_tizen>
786 [Obsolete("Deprecated in API8; Will be removed in API10. Please use View.ControlStateChangedEventArgs")]
787 public class StateChangedEventArgs : EventArgs
789 /// <summary> previous state of Button </summary>
790 /// <since_tizen> 6 </since_tizen>
791 [Obsolete("Deprecated in API8; Will be removed in API10")]
792 public ControlStates PreviousState;
793 /// <summary> current state of Button </summary>
794 /// <since_tizen> 6 </since_tizen>
795 [Obsolete("Deprecated in API8; Will be removed in API10")]
796 public ControlStates CurrentState;
800 /// Get current text part to the attached ButtonExtension.
803 /// It returns null if the passed extension is invalid.
805 /// <param name="extension">The extension instance that is currently attached to this Button.</param>
806 /// <return>The button's text part.</return>
807 [EditorBrowsable(EditorBrowsableState.Never)]
808 public TextLabel GetCurrentText(ButtonExtension extension)
810 return (extension == Extension) ? TextLabel : null;
814 /// Get current icon part to the attached ButtonExtension.
817 /// It returns null if the passed extension is invalid.
819 /// <param name="extension">The extension instance that is currently attached to this Button.</param>
820 /// <return>The button's icon part.</return>
821 [EditorBrowsable(EditorBrowsableState.Never)]
822 public ImageView GetCurrentIcon(ButtonExtension extension)
824 return (extension == Extension) ? Icon : null;
828 /// Get current overlay image part to the attached ButtonExtension.
831 /// It returns null if the passed extension is invalid.
833 /// <param name="extension">The extension instance that is currently attached to this Button.</param>
834 /// <return>The button's overlay image part.</return>
835 [EditorBrowsable(EditorBrowsableState.Never)]
836 public ImageView GetCurrentOverlayImage(ButtonExtension extension)
838 return (extension == Extension) ? OverlayImage : null;