9bb86329f1998cd7d0360690bc28853bcb1b9412
[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         /// <summary>
113         /// Creates a new instance of a ButtonStyle.
114         /// </summary>
115         /// <since_tizen> 6 </since_tizen>
116         /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
117         [EditorBrowsable(EditorBrowsableState.Never)]
118         public ButtonStyle() : base()
119         {
120             InitSubStyle();
121         }
122
123         /// <summary>
124         /// Creates a new instance of a ButtonStyle with style.
125         /// </summary>
126         /// <param name="style">Create ButtonStyle by style customized by user.</param>
127         /// <since_tizen> 6 </since_tizen>
128         /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
129         [EditorBrowsable(EditorBrowsableState.Never)]
130         public ButtonStyle(ButtonStyle style) : base(style)
131         {
132             if(style == null)
133             {
134                 return;
135             }
136
137             InitSubStyle();
138
139             this.CopyFrom(style);
140         }
141         /// <summary>
142         /// Overlay image's Style.
143         /// </summary>
144         /// <since_tizen> 6 </since_tizen>
145         /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
146         [EditorBrowsable(EditorBrowsableState.Never)]
147         public ImageViewStyle Overlay { get; set; }
148
149         /// <summary>
150         /// Text's Style.
151         /// </summary>
152         /// <since_tizen> 6 </since_tizen>
153         /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
154         [EditorBrowsable(EditorBrowsableState.Never)]
155         public TextLabelStyle Text { get; set; }
156
157         /// <summary>
158         /// Icon's Style.
159         /// </summary>
160         /// <since_tizen> 6 </since_tizen>
161         /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
162         [EditorBrowsable(EditorBrowsableState.Never)]
163         public ImageControlStyle Icon { get; set; }
164
165         /// This will be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API.
166         [EditorBrowsable(EditorBrowsableState.Never)]
167         public bool? IsSelectable
168         {
169             get => (bool?)GetValue(IsSelectableProperty);
170             set => SetValue(IsSelectableProperty, value);
171         }
172
173         /// This will be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API.
174         [EditorBrowsable(EditorBrowsableState.Never)]
175         public bool? IsSelected
176         {
177             get => (bool?)GetValue(IsSelectedProperty);
178             set => SetValue(IsSelectedProperty, value);
179         }
180
181         /// <summary>
182         /// Flag to decide button can be selected or not.
183         /// </summary>
184         /// <since_tizen> 6 </since_tizen>
185         /// This will be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API.
186         [EditorBrowsable(EditorBrowsableState.Never)]
187         public bool? IsEnabled
188         {
189             get => (bool?)GetValue(IsEnabledProperty);
190             set => SetValue(IsEnabledProperty, value);
191         }
192
193         /// <summary>
194         /// Icon relative orientation.
195         /// </summary>
196         /// <since_tizen> 6 </since_tizen>
197         /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
198         [EditorBrowsable(EditorBrowsableState.Never)]
199         public Button.IconOrientation? IconRelativeOrientation
200         {
201             get => (Button.IconOrientation?)GetValue(IconRelativeOrientationProperty);
202             set => SetValue(IconRelativeOrientationProperty, value);
203         }
204
205         /// This will be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API.
206         [EditorBrowsable(EditorBrowsableState.Never)]
207         public Extents IconPadding
208         {
209             get
210             {
211                 Extents padding = (Extents)GetValue(IconPaddingProperty);
212                 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);
213             }
214             set => SetValue(IconPaddingProperty, value);
215         }
216
217         /// This will be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API.
218         [EditorBrowsable(EditorBrowsableState.Never)]
219         public Extents TextPadding
220         {
221             get
222             {
223                 Extents padding = (Extents)GetValue(TextPaddingProperty);
224                 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);
225             }
226             set => SetValue(TextPaddingProperty, value);
227         }
228
229         /// This will be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API.
230         [EditorBrowsable(EditorBrowsableState.Never)]
231         public override void CopyFrom(BindableObject bindableObject)
232         {
233             base.CopyFrom(bindableObject);
234
235             ButtonStyle buttonStyle = bindableObject as ButtonStyle;
236
237             if (null != buttonStyle)
238             {
239                 if (null != buttonStyle.Overlay)
240                 {
241                     Overlay.CopyFrom(buttonStyle.Overlay);
242                 }
243
244                 if (null != buttonStyle.Text)
245                 {
246                     Text.CopyFrom(buttonStyle.Text);
247                 }
248
249                 if (null != buttonStyle.Icon)
250                 {
251                     Icon.CopyFrom(buttonStyle.Icon);
252                 }
253
254                 IsSelectable = buttonStyle.IsSelectable;
255                 IconRelativeOrientation = buttonStyle.IconRelativeOrientation;
256             }
257         }
258
259         private void InitSubStyle()
260         {
261             Overlay = new ImageViewStyle()
262             {
263                 PositionUsesPivotPoint = true,
264                 ParentOrigin = Tizen.NUI.ParentOrigin.Center,
265                 PivotPoint = Tizen.NUI.PivotPoint.Center,
266                 WidthResizePolicy = ResizePolicyType.FillToParent,
267                 HeightResizePolicy = ResizePolicyType.FillToParent
268             };
269             Overlay.PropertyChanged += SubStyleCalledEvent;
270
271             Text = new TextLabelStyle()
272             {
273                 PositionUsesPivotPoint = true,
274                 ParentOrigin = Tizen.NUI.ParentOrigin.Center,
275                 PivotPoint = Tizen.NUI.PivotPoint.Center,
276                 WidthResizePolicy = ResizePolicyType.FillToParent,
277                 HeightResizePolicy = ResizePolicyType.FillToParent,
278                 HorizontalAlignment = HorizontalAlignment.Center,
279                 VerticalAlignment = VerticalAlignment.Center
280             };
281             Text.PropertyChanged += SubStyleCalledEvent;
282
283             Icon = new ImageControlStyle()
284             {
285                 PositionUsesPivotPoint = true,
286                 ParentOrigin = Tizen.NUI.ParentOrigin.Center,
287                 PivotPoint = Tizen.NUI.PivotPoint.Center,
288                 WidthResizePolicy = ResizePolicyType.FitToChildren,
289                 HeightResizePolicy = ResizePolicyType.FitToChildren,
290             };
291             Icon.PropertyChanged += SubStyleCalledEvent;
292         }
293
294         private void SubStyleCalledEvent(object sender, global::System.EventArgs e)
295         {
296             OnPropertyChanged();
297         }
298     }
299 }