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;
57 var newIconOrientation = (IconOrientation?)newValue;
58 if (instance.iconRelativeOrientation != newIconOrientation)
60 instance.iconRelativeOrientation = newIconOrientation;
61 instance.UpdateUIContent();
64 defaultValueCreator: (bindable) => ((Button)bindable).iconRelativeOrientation
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 IsEnabledProperty = BindableProperty.Create(nameof(IsEnabled), typeof(bool), typeof(Button), true, propertyChanged: (bindable, oldValue, newValue) =>
71 var instance = (Button)bindable;
74 bool newEnabled = (bool)newValue;
75 if (instance.isEnabled != newEnabled)
77 instance.isEnabled = newEnabled;
79 if (instance.buttonStyle != null)
81 instance.buttonStyle.IsEnabled = newEnabled;
84 instance.UpdateState();
88 defaultValueCreator: (bindable) => ((Button)bindable).isEnabled);
89 /// This will be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API.
90 [EditorBrowsable(EditorBrowsableState.Never)]
91 public static readonly BindableProperty IsSelectedProperty = BindableProperty.Create(nameof(IsSelected), typeof(bool), typeof(Button), true, propertyChanged: (bindable, oldValue, newValue) =>
93 var instance = (Button)bindable;
96 bool newSelected = (bool)newValue;
97 if (instance.isSelected != newSelected)
99 instance.isSelected = newSelected;
101 if (instance.buttonStyle != null)
103 instance.buttonStyle.IsSelected = newSelected;
106 if (instance.isSelectable)
108 instance.UpdateState();
113 defaultValueCreator: (bindable) =>
115 var instance = (Button)bindable;
116 return instance.isSelectable && instance.isSelected;
118 /// This will be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API.
119 [EditorBrowsable(EditorBrowsableState.Never)]
120 public static readonly BindableProperty IsSelectableProperty = BindableProperty.Create(nameof(IsSelectable), typeof(bool), typeof(Button), true, propertyChanged: (bindable, oldValue, newValue) =>
122 var instance = (Button)bindable;
123 if (newValue != null)
125 bool newSelectable = (bool)newValue;
126 if (instance.isSelectable != newSelectable)
128 instance.isSelectable = newSelectable;
130 if (instance.buttonStyle != null)
132 instance.buttonStyle.IsSelectable = newSelectable;
135 instance.UpdateState();
139 defaultValueCreator: (bindable) => ((Button)bindable).isSelectable);
141 /// This will be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API.
142 [EditorBrowsable(EditorBrowsableState.Never)]
143 public static readonly BindableProperty IconPaddingProperty = BindableProperty.Create(nameof(IconPadding), typeof(Extents), typeof(Button), null, propertyChanged: (bindable, oldValue, newValue) =>
145 var instance = (Button)bindable;
146 instance.iconPadding = (Extents)((Extents)newValue).Clone();
147 instance.UpdateUIContent();
149 defaultValueCreator: (bindable) => ((Button)bindable).iconPadding);
151 /// This will be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API.
152 [EditorBrowsable(EditorBrowsableState.Never)]
153 public static readonly BindableProperty TextPaddingProperty = BindableProperty.Create(nameof(TextPadding), typeof(Extents), typeof(Button), null, propertyChanged: (bindable, oldValue, newValue) =>
155 var instance = (Button)bindable;
156 instance.textPadding = (Extents)((Extents)newValue).Clone();
157 instance.UpdateUIContent();
159 defaultValueCreator: (bindable) => ((Button)bindable).textPadding);
161 private IconOrientation? iconRelativeOrientation;
162 private bool isSelected = false;
163 private bool isSelectable = false;
164 private bool isEnabled = true;
165 private Extents iconPadding;
166 private Extents textPadding;
171 /// Creates a new instance of a Button.
173 /// <since_tizen> 6 </since_tizen>
174 public Button() : base()
180 /// Creates a new instance of a Button with style.
182 /// <param name="style">Create Button by special style defined in UX.</param>
183 /// <since_tizen> 8 </since_tizen>
184 public Button(string style) : base(style)
190 /// Creates a new instance of a Button with style.
192 /// <param name="buttonStyle">Create Button by style customized by user.</param>
193 /// <since_tizen> 8 </since_tizen>
194 public Button(ButtonStyle buttonStyle) : base(buttonStyle)
200 /// An event for the button clicked signal which can be used to subscribe or unsubscribe the event handler provided by the user.<br />
202 /// <since_tizen> 6 </since_tizen>
203 [Obsolete("Deprecated in API8; Will be removed in API10. Please use Clicked event instead.")]
204 public event EventHandler<ClickEventArgs> ClickEvent;
207 /// An event for the button clicked signal which can be used to subscribe or unsubscribe the event handler provided by the user.
209 /// <since_tizen> 8 </since_tizen>
210 public event EventHandler<ClickedEventArgs> Clicked;
213 /// An event for the button state changed signal which can be used to subscribe or unsubscribe the event handler provided by the user.<br />
215 /// <since_tizen> 6 </since_tizen>
216 [Obsolete("Deprecated in API8; Will be removed in API10. Please use View.ControlStateChangedEvent")]
217 public event EventHandler<StateChangedEventArgs> StateChangedEvent
221 stateChangeHandler += value;
225 stateChangeHandler -= value;
230 /// Icon orientation.
232 /// <since_tizen> 6 </since_tizen>
233 public enum IconOrientation
238 /// <since_tizen> 6 </since_tizen>
243 /// <since_tizen> 6 </since_tizen>
248 /// <since_tizen> 6 </since_tizen>
253 /// <since_tizen> 6 </since_tizen>
258 /// Button's icon part.
260 /// <since_tizen> 8 </since_tizen>
261 public ImageView Icon
265 if (null == buttonIcon)
267 buttonIcon = CreateIcon();
268 if (null != Extension)
270 buttonIcon = Extension.OnCreateIcon(this, buttonIcon);
272 if (null != buttonIcon)
275 buttonIcon.Relayout += OnIconRelayout;
287 /// Button's overlay image part.
289 /// <since_tizen> 8 </since_tizen>
290 public ImageView OverlayImage
294 if (null == overlayImage)
296 overlayImage = CreateOverlayImage();
297 if (null != Extension)
299 overlayImage = Extension.OnCreateOverlayImage(this, overlayImage);
301 if (null != overlayImage)
310 overlayImage = value;
315 /// Button's text part.
317 /// <since_tizen> 8 </since_tizen>
318 public TextLabel TextLabel
322 if (null == buttonText)
324 buttonText = CreateText();
325 if (null != Extension)
327 buttonText = Extension.OnCreateText(this, buttonText);
329 if (null != buttonText)
339 AccessibilityManager.Instance.SetAccessibilityAttribute(this, AccessibilityManager.AccessibilityAttribute.Label, buttonText.Text);
344 /// Return a copied Style instance of Button
347 /// It returns copied Style instance and changing it does not effect to the Button.
348 /// Style setting is possible by using constructor or the function of ApplyStyle(ViewStyle viewStyle)
350 /// <since_tizen> 8 </since_tizen>
351 public new ButtonStyle Style
355 var result = (ButtonStyle)ViewStyle.Clone();
356 result.CopyPropertiesFromView(this);
357 result.Text.CopyPropertiesFromView(TextLabel);
358 result.Icon.CopyPropertiesFromView(Icon);
359 result.Overlay.CopyPropertiesFromView(OverlayImage);
365 /// The text of Button.
367 /// <since_tizen> 6 </since_tizen>
372 return TextLabel.Text;
376 TextLabel.Text = value;
381 /// Flag to decide Button can be selected or not.
383 /// <since_tizen> 6 </since_tizen>
384 public bool IsSelectable
388 return (bool)GetValue(IsSelectableProperty);
392 SetValue(IsSelectableProperty, value);
397 /// Translate text string in Button.
399 /// <since_tizen> 6 </since_tizen>
400 public string TranslatableText
404 return TextLabel.TranslatableText;
408 TextLabel.TranslatableText = value;
413 /// Text point size in Button.
415 /// <since_tizen> 6 </since_tizen>
416 public float PointSize
420 return TextLabel.PointSize;
424 TextLabel.PointSize = value;
429 /// Text font family in Button.
431 /// <since_tizen> 6 </since_tizen>
432 public string FontFamily
436 return TextLabel.FontFamily;
440 TextLabel.FontFamily = value;
445 /// Text color in Button.
447 /// <since_tizen> 6 </since_tizen>
448 public Color TextColor
452 return TextLabel.TextColor;
456 TextLabel.TextColor = value;
461 /// Text horizontal alignment in Button.
463 /// <since_tizen> 6 </since_tizen>
464 public HorizontalAlignment TextAlignment
468 return TextLabel.HorizontalAlignment;
472 TextLabel.HorizontalAlignment = value;
477 /// Icon image's resource url in Button.
479 /// <since_tizen> 6 </since_tizen>
480 public string IconURL
484 return Icon.ResourceUrl;
488 Icon.ResourceUrl = value;
493 /// Text string selector in Button.
494 /// Getter returns copied selector value if exist, null otherwise.
495 /// <exception cref="NullReferenceException">Thrown when setting null value.</exception>
497 /// <since_tizen> 6 </since_tizen>
498 public StringSelector TextSelector
500 get => buttonText == null ? null : new StringSelector((Selector<string>)buttonText.GetValue(TextLabel.TextSelectorProperty));
503 if (value == null || buttonText == null)
505 throw new NullReferenceException("Button.TextSelector is null");
509 buttonText.SetValue(TextLabel.TextSelectorProperty, value);
515 /// Translateable text string selector in Button.
516 /// Getter returns copied selector value if exist, null otherwise.
518 /// <exception cref="NullReferenceException">Thrown when setting null value.</exception>
519 /// <since_tizen> 6 </since_tizen>
520 public StringSelector TranslatableTextSelector
522 get => buttonText == null ? null : new StringSelector((Selector<string>)buttonText.GetValue(TextLabel.TranslatableTextSelectorProperty));
525 if (value == null || buttonText == null)
527 throw new NullReferenceException("Button.TranslatableTextSelector is null");
531 buttonText.SetValue(TextLabel.TranslatableTextSelectorProperty, value);
537 /// Text color selector in Button.
538 /// Getter returns copied selector value if exist, null otherwise.
540 /// <exception cref="NullReferenceException">Thrown when setting null value.</exception>
541 /// <since_tizen> 6 </since_tizen>
542 public ColorSelector TextColorSelector
544 get => buttonText == null ? null : new ColorSelector((Selector<Color>)buttonText.GetValue(TextLabel.TextColorSelectorProperty));
547 if (value == null || buttonText == null)
549 throw new NullReferenceException("Button.TextColorSelectorProperty is null");
553 buttonText.SetValue(TextLabel.TextColorSelectorProperty, value);
559 /// Text font size selector in Button.
560 /// Getter returns copied selector value if exist, null otherwise.
562 /// <exception cref="NullReferenceException">Thrown when setting null value.</exception>
563 /// <since_tizen> 6 </since_tizen>
564 public FloatSelector PointSizeSelector
566 get => buttonText == null ? null : new FloatSelector((Selector<float?>)buttonText.GetValue(TextLabel.PointSizeSelectorProperty));
569 if (value == null || buttonText == null)
571 throw new NullReferenceException("Button.PointSizeSelector is null");
575 buttonText.SetValue(TextLabel.PointSizeSelectorProperty, value);
581 /// Icon image's resource url selector in Button.
582 /// Getter returns copied selector value if exist, null otherwise.
584 /// <exception cref="NullReferenceException">Thrown when setting null value.</exception>
585 /// <since_tizen> 6 </since_tizen>
586 public StringSelector IconURLSelector
588 get => buttonIcon == null ? null : new StringSelector((Selector<string>)buttonIcon.GetValue(ImageView.ResourceUrlSelectorProperty));
591 if (value == null || buttonIcon == null)
593 throw new NullReferenceException("Button.IconURLSelector is null");
597 buttonIcon.SetValue(ImageView.ResourceUrlSelectorProperty, value);
603 /// Flag to decide selected state in Button.
605 /// <since_tizen> 6 </since_tizen>
606 public bool IsSelected
610 return (bool)GetValue(IsSelectedProperty);
614 SetValue(IsSelectedProperty, value);
619 /// Flag to decide enable or disable in Button.
621 /// <since_tizen> 6 </since_tizen>
622 public bool IsEnabled
626 return (bool)GetValue(IsEnabledProperty);
630 SetValue(IsEnabledProperty, value);
635 /// Icon relative orientation in Button, work only when show icon and text.
637 /// <since_tizen> 8 </since_tizen>
638 public IconOrientation? IconRelativeOrientation
642 return (IconOrientation?)GetValue(IconRelativeOrientationProperty) ?? IconOrientation.Left;
646 SetValue(IconRelativeOrientationProperty, value);
651 /// Icon padding in Button, work only when show icon and text.
653 /// <since_tizen> 6 </since_tizen>
654 public Extents IconPadding
656 get => (Extents)GetValue(IconPaddingProperty) ?? new Extents();
657 set => SetValue(IconPaddingProperty, value);
661 /// Text padding in Button, work only when show icon and text.
663 /// <since_tizen> 6 </since_tizen>
664 public Extents TextPadding
666 get => (Extents)GetValue(TextPaddingProperty) ?? new Extents();
667 set => SetValue(TextPaddingProperty, value);
670 private ButtonStyle buttonStyle => ViewStyle as ButtonStyle;
673 /// Called after a key event is received by the view that has had its focus set.
675 /// <param name="key">The key event.</param>
676 /// <returns>True if the key event should be consumed.</returns>
677 /// <since_tizen> 6 </since_tizen>
678 public override bool OnKey(Key key)
680 if (!IsEnabled || null == key)
685 if (key.State == Key.StateType.Down)
687 if (key.KeyPressedName == "Return")
693 else if (key.State == Key.StateType.Up)
695 if (key.KeyPressedName == "Return")
697 bool clicked = isPressed && IsEnabled;
703 IsSelected = !IsSelected;
712 ClickedEventArgs eventArgs = new ClickedEventArgs();
713 OnClickedInternal(eventArgs);
717 return base.OnKey(key);
721 /// 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.
723 /// <since_tizen> 8 </since_tizen>
724 public override void OnFocusGained()
726 base.OnFocusGained();
731 /// 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.
733 /// <since_tizen> 8 </since_tizen>
734 public override void OnFocusLost()
741 /// Called after a touch event is received by the owning view.<br />
742 /// CustomViewBehaviour.REQUIRES_TOUCH_EVENTS must be enabled during construction. See CustomView(ViewWrapperImpl.CustomViewBehaviour behaviour).<br />
744 /// <param name="touch">The touch event.</param>
745 /// <returns>True if the event should be consumed.</returns>
746 /// <since_tizen> 8 </since_tizen>
747 [Obsolete("Deprecated in API8; Will be removed in API10. Please use OnClicked instead.")]
748 #pragma warning disable CS0809 // Obsolete member overrides non-obsolete member, It will be removed in API10
749 public override bool OnTouch(Touch touch)
750 #pragma warning restore CS0809 // Obsolete member overrides non-obsolete member, It will be removed in API10
752 return base.OnTouch(touch);
756 /// Apply style to button.
758 /// <param name="viewStyle">The style to apply.</param>
759 /// <since_tizen> 8 </since_tizen>
760 public override void ApplyStyle(ViewStyle viewStyle)
762 styleApplied = false;
764 base.ApplyStyle(viewStyle);
766 if (null != buttonStyle)
768 Extension = buttonStyle.CreateExtension();
769 if (buttonStyle.Overlay != null)
771 OverlayImage?.ApplyStyle(buttonStyle.Overlay);
774 if (buttonStyle.Text != null)
776 TextLabel?.ApplyStyle(buttonStyle.Text);
779 if (buttonStyle.Icon != null)
781 Icon?.ApplyStyle(buttonStyle.Icon);
789 /// ClickEventArgs is a class to record button click event arguments which will sent to user.
791 /// <since_tizen> 6 </since_tizen>
792 [Obsolete("Deprecated in API8; Will be removed in API10. Please use ClickedEventArgs instead.")]
793 [global::System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1034:NestedTypesShouldNotBeVisible")]
794 public class ClickEventArgs : EventArgs
799 /// StateChangeEventArgs is a class to record button state change event arguments which will sent to user.
801 /// <since_tizen> 6 </since_tizen>
802 [Obsolete("Deprecated in API8; Will be removed in API10. Please use View.ControlStateChangedEventArgs")]
803 [global::System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1034:NestedTypesShouldNotBeVisible")]
804 public class StateChangedEventArgs : EventArgs
806 /// <summary> previous state of Button </summary>
807 /// <since_tizen> 6 </since_tizen>
808 /// It will be removed in API10
809 [global::System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1051:Do not declare visible instance fields")]
810 [Obsolete("Deprecated in API8; Will be removed in API10")]
811 public ControlStates PreviousState;
812 /// <summary> current state of Button </summary>
813 /// <since_tizen> 6 </since_tizen>
814 /// It will be removed in API10
815 [global::System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1051:Do not declare visible instance fields")]
816 [Obsolete("Deprecated in API8; Will be removed in API10")]
817 public ControlStates CurrentState;