2dc528db8df70a951eb5f8ed07b5432d938e1f72
[platform/core/csapi/tizenfx.git] / src / Tizen.NUI.Components / Style / ButtonStyle.cs
1 /*
2  * Copyright(c) 2019 Samsung Electronics Co., Ltd.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  *
16  */
17 using System.Collections.Generic;
18 using System.ComponentModel;
19 using Tizen.NUI.BaseComponents;
20 using Tizen.NUI.Binding;
21
22 namespace Tizen.NUI.Components
23 {
24     /// <summary>
25     /// ButtonAttributes is a class which saves Button's ux data.
26     /// </summary>
27     /// <since_tizen> 6 </since_tizen>
28     /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
29     [EditorBrowsable(EditorBrowsableState.Never)]
30     public class ButtonStyle : ControlStyle
31     {
32         private bool? isSelectable;
33         private bool? isSelected;
34         private bool? isEnabled;
35         private Button.IconOrientation? iconRelativeOrientation;
36         private Extents iconPadding;
37         private Extents textPadding;
38
39         /// This will be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API.
40         [EditorBrowsable(EditorBrowsableState.Never)]
41         public static readonly BindableProperty IsSelectableProperty = BindableProperty.Create(nameof(IsSelectable), typeof(bool?), typeof(ButtonStyle), null, propertyChanged: (bindable, oldValue, newValue) =>
42         {
43             var buttonStyle = (ButtonStyle)bindable;
44             buttonStyle.isSelectable = (bool?)newValue;
45         },
46         defaultValueCreator: (bindable) =>
47         {
48             var buttonStyle = (ButtonStyle)bindable;
49             return buttonStyle.isSelectable;
50         });
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 IsSelectedProperty = BindableProperty.Create(nameof(IsSelected), typeof(bool?), typeof(ButtonStyle), null, propertyChanged: (bindable, oldValue, newValue) =>
54         {
55             var buttonStyle = (ButtonStyle)bindable;
56             buttonStyle.isSelected = (bool?)newValue;
57         },
58         defaultValueCreator: (bindable) =>
59         {
60             var buttonStyle = (ButtonStyle)bindable;
61             return buttonStyle.isSelected;
62         });
63         /// This will be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API.
64         [EditorBrowsable(EditorBrowsableState.Never)]
65         public static readonly BindableProperty IsEnabledProperty = BindableProperty.Create(nameof(IsEnabled), typeof(bool?), typeof(ButtonStyle), null, propertyChanged: (bindable, oldValue, newValue) =>
66         {
67             var buttonStyle = (ButtonStyle)bindable;
68             buttonStyle.isEnabled = (bool?)newValue;
69         },
70         defaultValueCreator: (bindable) =>
71         {
72             var buttonStyle = (ButtonStyle)bindable;
73             return buttonStyle.isEnabled;
74         });
75         /// This will be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API.
76         [EditorBrowsable(EditorBrowsableState.Never)]
77         public static readonly BindableProperty IconRelativeOrientationProperty = BindableProperty.Create(nameof(IconRelativeOrientation), typeof(Button.IconOrientation?), typeof(ButtonStyle), null, propertyChanged: (bindable, oldValue, newValue) =>
78         {
79             var buttonStyle = (ButtonStyle)bindable;
80             buttonStyle.iconRelativeOrientation = (Button.IconOrientation?)newValue;
81         },
82         defaultValueCreator: (bindable) =>
83         {
84             var buttonStyle = (ButtonStyle)bindable;
85             return buttonStyle.iconRelativeOrientation;
86         });
87         /// This will be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API.
88         [EditorBrowsable(EditorBrowsableState.Never)]
89         public static readonly BindableProperty IconPaddingProperty = BindableProperty.Create(nameof(IconPadding), typeof(Extents), typeof(ButtonStyle), null, propertyChanged: (bindable, oldValue, newValue) =>
90         {
91             var buttonStyle = (ButtonStyle)bindable;
92             buttonStyle.iconPadding = (Extents)newValue;
93         },
94         defaultValueCreator: (bindable) =>
95         {
96             var buttonStyle = (ButtonStyle)bindable;
97             return buttonStyle.iconPadding;
98         });
99         /// This will be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API.
100         [EditorBrowsable(EditorBrowsableState.Never)]
101         public static readonly BindableProperty TextPaddingProperty = BindableProperty.Create(nameof(TextPadding), typeof(Extents), typeof(ButtonStyle), null, propertyChanged: (bindable, oldValue, newValue) =>
102         {
103             var buttonStyle = (ButtonStyle)bindable;
104             buttonStyle.textPadding = (Extents)newValue;
105         },
106         defaultValueCreator: (bindable) =>
107         {
108             var buttonStyle = (ButtonStyle)bindable;
109             return buttonStyle.textPadding;
110         });
111
112         static ButtonStyle() { }
113
114         /// <summary>
115         /// Creates a new instance of a ButtonStyle.
116         /// </summary>
117         /// <since_tizen> 6 </since_tizen>
118         /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
119         [EditorBrowsable(EditorBrowsableState.Never)]
120         public ButtonStyle() : base()
121         {
122             InitSubStyle();
123         }
124
125         /// <summary>
126         /// Creates a new instance of a ButtonStyle with style.
127         /// </summary>
128         /// <param name="style">Create ButtonStyle by style customized by user.</param>
129         /// <since_tizen> 6 </since_tizen>
130         /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
131         [EditorBrowsable(EditorBrowsableState.Never)]
132         public ButtonStyle(ButtonStyle style) : base(style)
133         {
134             if(style == null)
135             {
136                 return;
137             }
138
139             InitSubStyle();
140
141             this.CopyFrom(style);
142         }
143         /// <summary>
144         /// Overlay image's Style.
145         /// </summary>
146         /// <since_tizen> 6 </since_tizen>
147         /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
148         [EditorBrowsable(EditorBrowsableState.Never)]
149         public ImageViewStyle Overlay { get; set; }
150
151         /// <summary>
152         /// Text's Style.
153         /// </summary>
154         /// <since_tizen> 6 </since_tizen>
155         /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
156         [EditorBrowsable(EditorBrowsableState.Never)]
157         public TextLabelStyle Text { get; set; }
158
159         /// <summary>
160         /// Icon's Style.
161         /// </summary>
162         /// <since_tizen> 6 </since_tizen>
163         /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
164         [EditorBrowsable(EditorBrowsableState.Never)]
165         public ImageControlStyle Icon { get; set; }
166
167         /// This will be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API.
168         [EditorBrowsable(EditorBrowsableState.Never)]
169         public bool? IsSelectable
170         {
171             get => (bool?)GetValue(IsSelectableProperty);
172             set => SetValue(IsSelectableProperty, value);
173         }
174
175         /// This will be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API.
176         [EditorBrowsable(EditorBrowsableState.Never)]
177         public bool? IsSelected
178         {
179             get => (bool?)GetValue(IsSelectedProperty);
180             set => SetValue(IsSelectedProperty, value);
181         }
182
183         /// <summary>
184         /// Flag to decide button can be selected or not.
185         /// </summary>
186         /// <since_tizen> 6 </since_tizen>
187         /// This will be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API.
188         [EditorBrowsable(EditorBrowsableState.Never)]
189         public bool? IsEnabled
190         {
191             get => (bool?)GetValue(IsEnabledProperty);
192             set => SetValue(IsEnabledProperty, value);
193         }
194
195         /// <summary>
196         /// Icon relative orientation.
197         /// </summary>
198         /// <since_tizen> 6 </since_tizen>
199         /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
200         [EditorBrowsable(EditorBrowsableState.Never)]
201         public Button.IconOrientation? IconRelativeOrientation
202         {
203             get => (Button.IconOrientation?)GetValue(IconRelativeOrientationProperty);
204             set => SetValue(IconRelativeOrientationProperty, value);
205         }
206
207         /// This will be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API.
208         [EditorBrowsable(EditorBrowsableState.Never)]
209         public Extents IconPadding
210         {
211             get
212             {
213                 Extents padding = (Extents)GetValue(IconPaddingProperty);
214                 return (null != padding) ? padding : new Extents((ushort start, ushort end, ushort top, ushort bottom) => { IconPadding = new Extents(start, end, top, bottom); }, 0, 0, 0, 0);
215             }
216             set => SetValue(IconPaddingProperty, value);
217         }
218
219         /// This will be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API.
220         [EditorBrowsable(EditorBrowsableState.Never)]
221         public Extents TextPadding
222         {
223             get
224             {
225                 Extents padding = (Extents)GetValue(TextPaddingProperty);
226                 return (null != padding) ? padding : new Extents((ushort start, ushort end, ushort top, ushort bottom) => { TextPadding = new Extents(start, end, top, bottom); }, 0, 0, 0, 0);
227             }
228             set => SetValue(TextPaddingProperty, value);
229         }
230
231         /// This will be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API.
232         [EditorBrowsable(EditorBrowsableState.Never)]
233         public override void CopyFrom(BindableObject bindableObject)
234         {
235             base.CopyFrom(bindableObject);
236
237             ButtonStyle buttonStyle = bindableObject as ButtonStyle;
238
239             if (null != buttonStyle)
240             {
241                 if (null != buttonStyle.Overlay)
242                 {
243                     Overlay.CopyFrom(buttonStyle.Overlay);
244                 }
245
246                 if (null != buttonStyle.Text)
247                 {
248                     Text.CopyFrom(buttonStyle.Text);
249                 }
250
251                 if (null != buttonStyle.Icon)
252                 {
253                     Icon.CopyFrom(buttonStyle.Icon);
254                 }
255
256                 IsSelectable = buttonStyle.IsSelectable;
257                 IconRelativeOrientation = buttonStyle.IconRelativeOrientation;
258             }
259         }
260
261         private void InitSubStyle()
262         {
263             Overlay = new ImageViewStyle()
264             {
265                 PositionUsesPivotPoint = true,
266                 ParentOrigin = Tizen.NUI.ParentOrigin.Center,
267                 PivotPoint = Tizen.NUI.PivotPoint.Center,
268                 WidthResizePolicy = ResizePolicyType.FillToParent,
269                 HeightResizePolicy = ResizePolicyType.FillToParent
270             };
271             Overlay.PropertyChanged += SubStyleCalledEvent;
272
273             Text = new TextLabelStyle()
274             {
275                 PositionUsesPivotPoint = true,
276                 ParentOrigin = Tizen.NUI.ParentOrigin.Center,
277                 PivotPoint = Tizen.NUI.PivotPoint.Center,
278                 WidthResizePolicy = ResizePolicyType.FillToParent,
279                 HeightResizePolicy = ResizePolicyType.FillToParent,
280                 HorizontalAlignment = HorizontalAlignment.Center,
281                 VerticalAlignment = VerticalAlignment.Center
282             };
283             Text.PropertyChanged += SubStyleCalledEvent;
284
285             Icon = new ImageControlStyle()
286             {
287                 PositionUsesPivotPoint = true,
288                 ParentOrigin = Tizen.NUI.ParentOrigin.Center,
289                 PivotPoint = Tizen.NUI.PivotPoint.Center,
290                 WidthResizePolicy = ResizePolicyType.FitToChildren,
291                 HeightResizePolicy = ResizePolicyType.FitToChildren,
292             };
293             Icon.PropertyChanged += SubStyleCalledEvent;
294         }
295
296         private void SubStyleCalledEvent(object sender, global::System.EventArgs e)
297         {
298             OnPropertyChanged();
299         }
300     }
301 }