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)
340 AccessibilityManager.Instance.SetAccessibilityAttribute(this, AccessibilityManager.AccessibilityAttribute.Label, buttonText.Text);
346 /// Return a copied Style instance of Button
349 /// It returns copied Style instance and changing it does not effect to the Button.
350 /// Style setting is possible by using constructor or the function of ApplyStyle(ViewStyle viewStyle)
352 /// <since_tizen> 8 </since_tizen>
353 public new ButtonStyle Style
357 var result = (ButtonStyle)ViewStyle.Clone();
358 result.CopyPropertiesFromView(this);
359 result.Text.CopyPropertiesFromView(TextLabel);
360 result.Icon.CopyPropertiesFromView(Icon);
361 result.Overlay.CopyPropertiesFromView(OverlayImage);
367 /// The text of Button.
369 /// <since_tizen> 6 </since_tizen>
374 return TextLabel.Text;
378 TextLabel.Text = value;
383 /// Flag to decide Button can be selected or not.
385 /// <since_tizen> 6 </since_tizen>
386 public bool IsSelectable
390 return (bool)GetValue(IsSelectableProperty);
394 SetValue(IsSelectableProperty, value);
399 /// Translate text string in Button.
401 /// <since_tizen> 6 </since_tizen>
402 public string TranslatableText
406 return TextLabel.TranslatableText;
410 TextLabel.TranslatableText = value;
415 /// Text point size in Button.
417 /// <since_tizen> 6 </since_tizen>
418 public float PointSize
422 return TextLabel.PointSize;
426 TextLabel.PointSize = value;
431 /// Text font family in Button.
433 /// <since_tizen> 6 </since_tizen>
434 public string FontFamily
438 return TextLabel.FontFamily;
442 TextLabel.FontFamily = value;
447 /// Text color in Button.
449 /// <since_tizen> 6 </since_tizen>
450 public Color TextColor
454 return TextLabel.TextColor;
458 TextLabel.TextColor = value;
463 /// Text horizontal alignment in Button.
465 /// <since_tizen> 6 </since_tizen>
466 public HorizontalAlignment TextAlignment
470 return TextLabel.HorizontalAlignment;
474 TextLabel.HorizontalAlignment = value;
479 /// Icon image's resource url in Button.
481 /// <since_tizen> 6 </since_tizen>
482 public string IconURL
486 return Icon.ResourceUrl;
490 Icon.ResourceUrl = value;
495 /// Text string selector in Button.
496 /// Getter returns copied selector value if exist, null otherwise.
497 /// <exception cref="NullReferenceException">Thrown when setting null value.</exception>
499 /// <since_tizen> 6 </since_tizen>
500 public StringSelector TextSelector
502 get => buttonText == null ? null : new StringSelector((Selector<string>)buttonText.GetValue(TextLabel.TextSelectorProperty));
505 if (value == null || buttonText == null)
507 throw new NullReferenceException("Button.TextSelector is null");
511 buttonText.SetValue(TextLabel.TextSelectorProperty, value);
517 /// Translateable text string selector in Button.
518 /// Getter returns copied selector value if exist, null otherwise.
520 /// <exception cref="NullReferenceException">Thrown when setting null value.</exception>
521 /// <since_tizen> 6 </since_tizen>
522 public StringSelector TranslatableTextSelector
524 get => buttonText == null ? null : new StringSelector((Selector<string>)buttonText.GetValue(TextLabel.TranslatableTextSelectorProperty));
527 if (value == null || buttonText == null)
529 throw new NullReferenceException("Button.TranslatableTextSelector is null");
533 buttonText.SetValue(TextLabel.TranslatableTextSelectorProperty, value);
539 /// Text color selector in Button.
540 /// Getter returns copied selector value if exist, null otherwise.
542 /// <exception cref="NullReferenceException">Thrown when setting null value.</exception>
543 /// <since_tizen> 6 </since_tizen>
544 public ColorSelector TextColorSelector
546 get => buttonText == null ? null : new ColorSelector((Selector<Color>)buttonText.GetValue(TextLabel.TextColorSelectorProperty));
549 if (value == null || buttonText == null)
551 throw new NullReferenceException("Button.TextColorSelectorProperty is null");
555 buttonText.SetValue(TextLabel.TextColorSelectorProperty, value);
561 /// Text font size selector in Button.
562 /// Getter returns copied selector value if exist, null otherwise.
564 /// <exception cref="NullReferenceException">Thrown when setting null value.</exception>
565 /// <since_tizen> 6 </since_tizen>
566 public FloatSelector PointSizeSelector
568 get => buttonText == null ? null : new FloatSelector((Selector<float?>)buttonText.GetValue(TextLabel.PointSizeSelectorProperty));
571 if (value == null || buttonText == null)
573 throw new NullReferenceException("Button.PointSizeSelector is null");
577 buttonText.SetValue(TextLabel.PointSizeSelectorProperty, value);
583 /// Icon image's resource url selector in Button.
584 /// Getter returns copied selector value if exist, null otherwise.
586 /// <exception cref="NullReferenceException">Thrown when setting null value.</exception>
587 /// <since_tizen> 6 </since_tizen>
588 public StringSelector IconURLSelector
590 get => buttonIcon == null ? null : new StringSelector((Selector<string>)buttonIcon.GetValue(ImageView.ResourceUrlSelectorProperty));
593 if (value == null || buttonIcon == null)
595 throw new NullReferenceException("Button.IconURLSelector is null");
599 buttonIcon.SetValue(ImageView.ResourceUrlSelectorProperty, value);
605 /// Flag to decide selected state in Button.
607 /// <since_tizen> 6 </since_tizen>
608 public bool IsSelected
612 return (bool)GetValue(IsSelectedProperty);
616 SetValue(IsSelectedProperty, value);
621 /// Flag to decide enable or disable in Button.
623 /// <since_tizen> 6 </since_tizen>
624 public bool IsEnabled
628 return (bool)GetValue(IsEnabledProperty);
632 SetValue(IsEnabledProperty, value);
637 /// Icon relative orientation in Button, work only when show icon and text.
639 /// <since_tizen> 8 </since_tizen>
640 public IconOrientation? IconRelativeOrientation
644 return (IconOrientation?)GetValue(IconRelativeOrientationProperty) ?? IconOrientation.Left;
648 SetValue(IconRelativeOrientationProperty, value);
653 /// Icon padding in Button, work only when show icon and text.
655 /// <since_tizen> 6 </since_tizen>
656 public Extents IconPadding
658 get => (Extents)GetValue(IconPaddingProperty) ?? new Extents();
659 set => SetValue(IconPaddingProperty, value);
663 /// Text padding in Button, work only when show icon and text.
665 /// <since_tizen> 6 </since_tizen>
666 public Extents TextPadding
668 get => (Extents)GetValue(TextPaddingProperty) ?? new Extents();
669 set => SetValue(TextPaddingProperty, value);
672 private ButtonStyle buttonStyle => ViewStyle as ButtonStyle;
675 /// Called after a key event is received by the view that has had its focus set.
677 /// <param name="key">The key event.</param>
678 /// <returns>True if the key event should be consumed.</returns>
679 /// <since_tizen> 6 </since_tizen>
680 public override bool OnKey(Key key)
682 if (!IsEnabled || null == key)
687 if (key.State == Key.StateType.Down)
689 if (key.KeyPressedName == "Return")
695 else if (key.State == Key.StateType.Up)
697 if (key.KeyPressedName == "Return")
699 bool clicked = isPressed && IsEnabled;
705 IsSelected = !IsSelected;
714 ClickedEventArgs eventArgs = new ClickedEventArgs();
715 OnClickedInternal(eventArgs);
719 return base.OnKey(key);
723 /// 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.
725 /// <since_tizen> 8 </since_tizen>
726 public override void OnFocusGained()
728 base.OnFocusGained();
733 /// 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.
735 /// <since_tizen> 8 </since_tizen>
736 public override void OnFocusLost()
743 /// Called after a touch event is received by the owning view.<br />
744 /// CustomViewBehaviour.REQUIRES_TOUCH_EVENTS must be enabled during construction. See CustomView(ViewWrapperImpl.CustomViewBehaviour behaviour).<br />
746 /// <param name="touch">The touch event.</param>
747 /// <returns>True if the event should be consumed.</returns>
748 /// <since_tizen> 8 </since_tizen>
749 [Obsolete("Deprecated in API8; Will be removed in API10. Please use OnClicked instead.")]
750 public override bool OnTouch(Touch touch)
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 public class ClickEventArgs : EventArgs
798 /// StateChangeEventArgs is a class to record button state change event arguments which will sent to user.
800 /// <since_tizen> 6 </since_tizen>
801 [Obsolete("Deprecated in API8; Will be removed in API10. Please use View.ControlStateChangedEventArgs")]
802 public class StateChangedEventArgs : EventArgs
804 /// <summary> previous state of Button </summary>
805 /// <since_tizen> 6 </since_tizen>
806 [Obsolete("Deprecated in API8; Will be removed in API10")]
807 public ControlStates PreviousState;
808 /// <summary> current state of Button </summary>
809 /// <since_tizen> 6 </since_tizen>
810 [Obsolete("Deprecated in API8; Will be removed in API10")]
811 public ControlStates CurrentState;