2 * Copyright(c) 2021 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.Accessibility;
23 namespace Tizen.NUI.Components
26 /// ClickedEventArgs is a class to record button click event arguments which will sent to user.
28 /// <since_tizen> 8 </since_tizen>
29 public class ClickedEventArgs : EventArgs
34 /// SelectedChangedEventArgs is a class to record item selected arguments which will sent to user.
36 /// <since_tizen> 8 </since_tizen>
37 public class SelectedChangedEventArgs : EventArgs
39 /// <summary> Selected state </summary>
40 /// <since_tizen> 8 </since_tizen>
41 public bool IsSelected { get; set; }
45 /// Button is one kind of common component, a button clearly describes what action will occur when the user selects it.
46 /// Button may contain text or an icon.
48 /// <since_tizen> 6 </since_tizen>
49 public partial class Button : Control
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 IconRelativeOrientationProperty = BindableProperty.Create(nameof(IconRelativeOrientation), typeof(IconOrientation?), typeof(Button), null, propertyChanged: (bindable, oldValue, newValue) =>
55 var instance = (Button)bindable;
56 var newIconOrientation = (IconOrientation?)newValue;
57 if (instance.iconRelativeOrientation != newIconOrientation)
59 instance.iconRelativeOrientation = newIconOrientation;
60 instance.UpdateUIContent();
63 defaultValueCreator: (bindable) => ((Button)bindable).iconRelativeOrientation
66 /// This will be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API.
67 [EditorBrowsable(EditorBrowsableState.Never)]
68 public static readonly BindableProperty IsEnabledProperty = BindableProperty.Create(nameof(IsEnabled), typeof(bool), typeof(Button), true, propertyChanged: (bindable, oldValue, newValue) =>
70 var instance = (Button)bindable;
73 bool newEnabled = (bool)newValue;
74 if (instance.isEnabled != newEnabled)
76 instance.isEnabled = newEnabled;
77 instance.UpdateState();
81 defaultValueCreator: (bindable) => ((Button)bindable).isEnabled);
82 /// This will be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API.
83 [EditorBrowsable(EditorBrowsableState.Never)]
84 public static readonly BindableProperty IsSelectedProperty = BindableProperty.Create(nameof(IsSelected), typeof(bool), typeof(Button), true, propertyChanged: (bindable, oldValue, newValue) =>
86 var instance = (Button)bindable;
89 bool newSelected = (bool)newValue;
90 if (instance.isSelected != newSelected)
92 instance.isSelected = newSelected;
94 if (instance.isSelectable)
96 instance.UpdateState();
101 defaultValueCreator: (bindable) =>
103 var instance = (Button)bindable;
104 return instance.isSelectable && instance.isSelected;
106 /// This will be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API.
107 [EditorBrowsable(EditorBrowsableState.Never)]
108 public static readonly BindableProperty IsSelectableProperty = BindableProperty.Create(nameof(IsSelectable), typeof(bool), typeof(Button), true, propertyChanged: (bindable, oldValue, newValue) =>
110 var instance = (Button)bindable;
111 if (newValue != null)
113 bool newSelectable = (bool)newValue;
114 if (instance.isSelectable != newSelectable)
116 instance.isSelectable = newSelectable;
117 instance.UpdateState();
121 defaultValueCreator: (bindable) => ((Button)bindable).isSelectable);
123 /// This will be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API.
124 [EditorBrowsable(EditorBrowsableState.Never)]
125 public static readonly BindableProperty IconPaddingProperty = BindableProperty.Create(nameof(IconPadding), typeof(Extents), typeof(Button), null, propertyChanged: (bindable, oldValue, newValue) =>
127 var instance = (Button)bindable;
128 instance.iconPadding = (Extents)((Extents)newValue).Clone();
129 instance.UpdateUIContent();
131 defaultValueCreator: (bindable) => ((Button)bindable).iconPadding);
133 /// This will be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API.
134 [EditorBrowsable(EditorBrowsableState.Never)]
135 public static readonly BindableProperty TextPaddingProperty = BindableProperty.Create(nameof(TextPadding), typeof(Extents), typeof(Button), null, propertyChanged: (bindable, oldValue, newValue) =>
137 var instance = (Button)bindable;
138 instance.textPadding = (Extents)((Extents)newValue).Clone();
139 instance.UpdateUIContent();
141 defaultValueCreator: (bindable) => ((Button)bindable).textPadding);
143 private IconOrientation? iconRelativeOrientation;
144 private bool isSelected = false;
145 private bool isSelectable = false;
146 private bool isEnabled = true;
147 private Extents iconPadding;
148 private Extents textPadding;
153 /// Creates a new instance of a Button.
155 /// <since_tizen> 6 </since_tizen>
156 public Button() : base()
161 /// Creates a new instance of a Button with style.
163 /// <param name="style">Create Button by special style defined in UX.</param>
164 /// <since_tizen> 8 </since_tizen>
165 public Button(string style) : base(style)
170 /// Creates a new instance of a Button with style.
172 /// <param name="buttonStyle">Create Button by style customized by user.</param>
173 /// <since_tizen> 8 </since_tizen>
174 public Button(ButtonStyle buttonStyle) : base(buttonStyle)
179 /// Calculates current states for the button<br />
181 [EditorBrowsable(EditorBrowsableState.Never)]
182 protected override AccessibilityStates AccessibilityCalculateStates()
184 var states = base.AccessibilityCalculateStates();
185 states.Set(AccessibilityState.Enabled, this.IsEnabled);
190 /// An event for the button clicked signal which can be used to subscribe or unsubscribe the event handler provided by the user.<br />
192 /// <since_tizen> 6 </since_tizen>
193 [Obsolete("Deprecated in API8; Will be removed in API10. Please use Clicked event instead.")]
194 public event EventHandler<ClickEventArgs> ClickEvent;
197 /// An event for the button clicked signal which can be used to subscribe or unsubscribe the event handler provided by the user.
199 /// <since_tizen> 8 </since_tizen>
200 public event EventHandler<ClickedEventArgs> Clicked;
203 /// An event for the button state changed signal which can be used to subscribe or unsubscribe the event handler provided by the user.<br />
205 /// <since_tizen> 6 </since_tizen>
206 [Obsolete("Deprecated in API8; Will be removed in API10. Please use View.ControlStateChangedEvent")]
207 public event EventHandler<StateChangedEventArgs> StateChangedEvent
211 stateChangeHandler += value;
215 stateChangeHandler -= value;
220 /// Icon orientation.
222 /// <since_tizen> 6 </since_tizen>
223 public enum IconOrientation
228 /// <since_tizen> 6 </since_tizen>
233 /// <since_tizen> 6 </since_tizen>
238 /// <since_tizen> 6 </since_tizen>
243 /// <since_tizen> 6 </since_tizen>
248 /// Button's icon part.
250 /// <since_tizen> 8 </since_tizen>
251 public ImageView Icon
255 if (null == buttonIcon)
257 buttonIcon = CreateIcon();
258 if (null != Extension)
260 buttonIcon = Extension.OnCreateIcon(this, buttonIcon);
262 if (null != buttonIcon)
265 buttonIcon.Relayout += OnIconRelayout;
277 /// Button's overlay image part.
279 /// <since_tizen> 8 </since_tizen>
280 public ImageView OverlayImage
284 if (null == overlayImage)
286 overlayImage = CreateOverlayImage();
287 if (null != Extension)
289 overlayImage = Extension.OnCreateOverlayImage(this, overlayImage);
291 if (null != overlayImage)
300 overlayImage = value;
305 /// Button's text part.
307 /// <since_tizen> 8 </since_tizen>
308 public TextLabel TextLabel
312 if (null == buttonText)
314 buttonText = CreateText();
315 if (null != Extension)
317 buttonText = Extension.OnCreateText(this, buttonText);
319 if (null != buttonText)
329 AccessibilityManager.Instance.SetAccessibilityAttribute(this, AccessibilityManager.AccessibilityAttribute.Label, buttonText.Text);
334 /// Return currently applied style.
337 /// Modifying contents in style may cause unexpected behaviour.
339 /// <since_tizen> 8 </since_tizen>
340 public ButtonStyle Style => (ButtonStyle)(ViewStyle as ButtonStyle)?.Clone();
343 /// The text of Button.
345 /// <since_tizen> 6 </since_tizen>
350 return TextLabel.Text;
354 TextLabel.Text = value;
356 if (IsHighlighted && String.IsNullOrEmpty(AccessibilityName) && GetAccessibilityNameSignal().Empty())
358 EmitAccessibilityEvent(ObjectPropertyChangeEvent.Name);
364 /// Flag to decide Button can be selected or not.
366 /// <since_tizen> 6 </since_tizen>
367 public bool IsSelectable
371 return (bool)GetValue(IsSelectableProperty);
375 SetValue(IsSelectableProperty, value);
380 /// Translate text string in Button.
382 /// <since_tizen> 6 </since_tizen>
383 public string TranslatableText
387 return TextLabel.TranslatableText;
391 TextLabel.TranslatableText = value;
396 /// Text point size in Button.
398 /// <since_tizen> 6 </since_tizen>
399 public float PointSize
403 return TextLabel.PointSize;
407 TextLabel.PointSize = value;
412 /// Text font family in Button.
414 /// <since_tizen> 6 </since_tizen>
415 public string FontFamily
419 return TextLabel.FontFamily;
423 TextLabel.FontFamily = value;
428 /// Text color in Button.
430 /// <since_tizen> 6 </since_tizen>
431 public Color TextColor
435 return TextLabel.TextColor;
439 TextLabel.TextColor = value;
444 /// Text horizontal alignment in Button.
446 /// <since_tizen> 6 </since_tizen>
447 public HorizontalAlignment TextAlignment
451 return TextLabel.HorizontalAlignment;
455 TextLabel.HorizontalAlignment = value;
460 /// Icon image's resource url in Button.
462 /// <since_tizen> 6 </since_tizen>
463 public string IconURL
467 return Icon.ResourceUrl;
471 Icon.ResourceUrl = value;
476 /// Text string selector in Button.
477 /// Getter returns copied selector value if exist, null otherwise.
478 /// <exception cref="NullReferenceException">Thrown when setting null value.</exception>
480 /// <since_tizen> 6 </since_tizen>
481 public StringSelector TextSelector
483 get => buttonText == null ? null : new StringSelector((Selector<string>)buttonText.GetValue(TextLabel.TextSelectorProperty));
486 if (value == null || buttonText == null)
488 throw new NullReferenceException("Button.TextSelector is null");
492 buttonText.SetValue(TextLabel.TextSelectorProperty, value);
498 /// Translatable text string selector in Button.
499 /// Getter returns copied selector value if exist, null otherwise.
501 /// <exception cref="NullReferenceException">Thrown when setting null value.</exception>
502 /// <since_tizen> 6 </since_tizen>
503 public StringSelector TranslatableTextSelector
505 get => buttonText == null ? null : new StringSelector((Selector<string>)buttonText.GetValue(TextLabel.TranslatableTextSelectorProperty));
508 if (value == null || buttonText == null)
510 throw new NullReferenceException("Button.TranslatableTextSelector is null");
514 buttonText.SetValue(TextLabel.TranslatableTextSelectorProperty, value);
520 /// Text color selector in Button.
521 /// Getter returns copied selector value if exist, null otherwise.
523 /// <exception cref="NullReferenceException">Thrown when setting null value.</exception>
524 /// <since_tizen> 6 </since_tizen>
525 public ColorSelector TextColorSelector
527 get => buttonText == null ? null : new ColorSelector((Selector<Color>)buttonText.GetValue(TextLabel.TextColorSelectorProperty));
530 if (value == null || buttonText == null)
532 throw new NullReferenceException("Button.TextColorSelectorProperty is null");
536 buttonText.SetValue(TextLabel.TextColorSelectorProperty, value);
542 /// Text font size selector in Button.
543 /// Getter returns copied selector value if exist, null otherwise.
545 /// <exception cref="NullReferenceException">Thrown when setting null value.</exception>
546 /// <since_tizen> 6 </since_tizen>
547 public FloatSelector PointSizeSelector
549 get => buttonText == null ? null : new FloatSelector((Selector<float?>)buttonText.GetValue(TextLabel.PointSizeSelectorProperty));
552 if (value == null || buttonText == null)
554 throw new NullReferenceException("Button.PointSizeSelector is null");
558 buttonText.SetValue(TextLabel.PointSizeSelectorProperty, value);
564 /// Icon image's resource url selector in Button.
565 /// Getter returns copied selector value if exist, null otherwise.
567 /// <exception cref="NullReferenceException">Thrown when setting null value.</exception>
568 /// <since_tizen> 6 </since_tizen>
569 public StringSelector IconURLSelector
571 get => buttonIcon == null ? null : new StringSelector((Selector<string>)buttonIcon.GetValue(ImageView.ResourceUrlSelectorProperty));
574 if (value == null || buttonIcon == null)
576 throw new NullReferenceException("Button.IconURLSelector is null");
580 buttonIcon.SetValue(ImageView.ResourceUrlSelectorProperty, value);
586 /// Flag to decide selected state in Button.
588 /// <since_tizen> 6 </since_tizen>
589 public bool IsSelected
593 return (bool)GetValue(IsSelectedProperty);
597 SetValue(IsSelectedProperty, value);
602 /// Flag to decide enable or disable in Button.
604 /// <since_tizen> 6 </since_tizen>
605 public bool IsEnabled
609 return (bool)GetValue(IsEnabledProperty);
613 SetValue(IsEnabledProperty, value);
618 /// Icon relative orientation in Button, work only when show icon and text.
620 /// <since_tizen> 8 </since_tizen>
621 public IconOrientation? IconRelativeOrientation
625 return (IconOrientation?)GetValue(IconRelativeOrientationProperty) ?? IconOrientation.Left;
629 SetValue(IconRelativeOrientationProperty, value);
634 /// Icon padding in Button, work only when show icon and text.
636 /// <since_tizen> 6 </since_tizen>
637 public Extents IconPadding
639 get => (Extents)GetValue(IconPaddingProperty) ?? new Extents();
640 set => SetValue(IconPaddingProperty, value);
644 /// Text padding in Button, work only when show icon and text.
646 /// <since_tizen> 6 </since_tizen>
647 public Extents TextPadding
649 get => (Extents)GetValue(TextPaddingProperty) ?? new Extents();
650 set => SetValue(TextPaddingProperty, value);
654 /// Called after a key event is received by the view that has had its focus set.
656 /// <param name="key">The key event.</param>
657 /// <returns>True if the key event should be consumed.</returns>
658 /// <since_tizen> 6 </since_tizen>
659 public override bool OnKey(Key key)
661 if (!IsEnabled || null == key)
666 if (key.State == Key.StateType.Down)
668 if (key.KeyPressedName == "Return")
674 else if (key.State == Key.StateType.Up)
676 if (key.KeyPressedName == "Return")
678 bool clicked = isPressed && IsEnabled;
684 IsSelected = !IsSelected;
693 ClickedEventArgs eventArgs = new ClickedEventArgs();
694 OnClickedInternal(eventArgs);
698 return base.OnKey(key);
702 /// 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.
704 /// <since_tizen> 8 </since_tizen>
705 public override void OnFocusGained()
707 base.OnFocusGained();
712 /// 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.
714 /// <since_tizen> 8 </since_tizen>
715 public override void OnFocusLost()
722 /// Called after a touch event is received by the owning view.<br />
723 /// CustomViewBehaviour.REQUIRES_TOUCH_EVENTS must be enabled during construction. See CustomView(ViewWrapperImpl.CustomViewBehaviour behaviour).<br />
725 /// <param name="touch">The touch event.</param>
726 /// <returns>True if the event should be consumed.</returns>
727 /// <since_tizen> 8 </since_tizen>
728 [Obsolete("Deprecated in API8; Will be removed in API10. Please use OnClicked instead.")]
729 #pragma warning disable CS0809 // Obsolete member overrides non-obsolete member, It will be removed in API10
730 public override bool OnTouch(Touch touch)
731 #pragma warning restore CS0809 // Obsolete member overrides non-obsolete member, It will be removed in API10
733 return base.OnTouch(touch);
737 /// Apply style to button.
739 /// <param name="viewStyle">The style to apply.</param>
740 /// <since_tizen> 8 </since_tizen>
741 public override void ApplyStyle(ViewStyle viewStyle)
743 styleApplied = false;
745 base.ApplyStyle(viewStyle);
747 if (viewStyle is ButtonStyle buttonStyle)
749 Extension = buttonStyle.CreateExtension();
750 if (buttonStyle.Overlay != null)
752 OverlayImage?.ApplyStyle(buttonStyle.Overlay);
755 if (buttonStyle.Text != null)
757 TextLabel?.ApplyStyle(buttonStyle.Text);
760 if (buttonStyle.Icon != null)
762 Icon?.ApplyStyle(buttonStyle.Icon);
771 /// ClickEventArgs is a class to record button click event arguments which will sent to user.
773 /// <since_tizen> 6 </since_tizen>
774 [Obsolete("Deprecated in API8; Will be removed in API10. Please use ClickedEventArgs instead.")]
775 [global::System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1034:NestedTypesShouldNotBeVisible")]
776 public class ClickEventArgs : EventArgs
781 /// StateChangeEventArgs is a class to record button state change event arguments which will sent to user.
783 /// <since_tizen> 6 </since_tizen>
784 [Obsolete("Deprecated in API8; Will be removed in API10. Please use View.ControlStateChangedEventArgs")]
785 [global::System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1034:NestedTypesShouldNotBeVisible")]
786 public class StateChangedEventArgs : EventArgs
788 /// <summary> previous state of Button </summary>
789 /// <since_tizen> 6 </since_tizen>
790 /// It will be removed in API10
791 [global::System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1051:Do not declare visible instance fields")]
792 [Obsolete("Deprecated in API8; Will be removed in API10")]
793 public ControlStates PreviousState;
794 /// <summary> current state of Button </summary>
795 /// <since_tizen> 6 </since_tizen>
796 /// It will be removed in API10
797 [global::System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1051:Do not declare visible instance fields")]
798 [Obsolete("Deprecated in API8; Will be removed in API10")]
799 public ControlStates CurrentState;