[NUI] Improve performance : Remove rarely used properties from the Style. (#2695)
[platform/core/csapi/tizenfx.git] / src / Tizen.NUI / src / public / BaseComponents / Style / ViewStyle.cs
1 /*
2  * Copyright(c) 2021 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;
18 using System.ComponentModel;
19 using Tizen.NUI.Binding;
20
21 namespace Tizen.NUI.BaseComponents
22 {
23     /// <summary>
24     /// The base class for Children attributes in Components.
25     /// </summary>
26     /// This will be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API.
27     [EditorBrowsable(EditorBrowsableState.Never)]
28     public partial class ViewStyle : BindableObject, IDisposable
29     {
30         private bool disposed = false;
31         private bool? focusable;
32         private bool? positionUsesPivotPoint;
33         private Position parentOrigin;
34         private Position pivotPoint;
35         private Position position;
36         private Rotation orientation;
37         private DrawModeType? drawMode;
38         private Vector3 sizeModeFactor;
39         private ResizePolicyType? widthResizePolicy;
40         private ResizePolicyType? heightResizePolicy;
41         private bool? widthForHeight;
42         private bool? heightForWidth;
43         private Extents padding;
44         private Size2D minimumSize;
45         private Size2D maximumSize;
46         private ClippingModeType? clippingMode;
47         private Size size;
48         private Extents margin;
49         private bool? themeChangeSensitive;
50         private float? cornerRadius;
51
52         private Selector<ImageShadow> imageShadow;
53         private Selector<Shadow> boxShadow;
54         private Selector<string> backgroundImageSelector;
55         private Selector<float?> opacitySelector;
56         private Selector<Color> backgroundColorSelector;
57         private Selector<Rectangle> backgroundImageBorderSelector;
58         private Selector<Color> colorSelector;
59         private VisualTransformPolicyType? cornerRadiusPolicy;
60
61         static ViewStyle() { }
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 ViewStyle() { }
66
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 ViewStyle(ViewStyle viewAttributes)
70         {
71             CopyFrom(viewAttributes);
72         }
73
74         /// This will be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API.
75         [EditorBrowsable(EditorBrowsableState.Never)]
76         public Selector<string> BackgroundImage
77         {
78             get
79             {
80                 Selector<string> image = (Selector<string>)GetValue(BackgroundImageSelectorProperty);
81                 return (null != image) ? image : backgroundImageSelector = new Selector<string>();
82             }
83             set => SetValue(BackgroundImageSelectorProperty, value);
84         }
85
86         /// This will be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API.
87         [EditorBrowsable(EditorBrowsableState.Never)]
88         public bool? Focusable
89         {
90             get => (bool?)GetValue(FocusableProperty);
91             set => SetValue(FocusableProperty, value);
92         }
93
94         /// This will be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API.
95         [Obsolete("Deprecated. Please use Size instead.")]
96         [EditorBrowsable(EditorBrowsableState.Never)]
97         public Size2D Size2D
98         {
99             get => (Size2D)GetValue(Size2DProperty);
100             set => SetValue(Size2DProperty, value);
101         }
102
103         /// This will be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API.
104         [EditorBrowsable(EditorBrowsableState.Never)]
105         public Selector<float?> Opacity
106         {
107             get
108             {
109                 Selector<float?> opacity = (Selector<float?>)GetValue(OpacitySelectorProperty);
110                 return (null != opacity) ? opacity : opacitySelector = new Selector<float?>();
111             }
112             set => SetValue(OpacitySelectorProperty, value);
113         }
114
115         /// This will be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API.
116         [Obsolete("Deprecated. Please use Position instead.")]
117         [EditorBrowsable(EditorBrowsableState.Never)]
118         public Position2D Position2D
119         {
120             get => (Position2D)GetValue(Position2DProperty);
121             set => SetValue(Position2DProperty, value);
122         }
123
124         /// This will be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API.
125         [EditorBrowsable(EditorBrowsableState.Never)]
126         public bool? PositionUsesPivotPoint
127         {
128             get => (bool?)GetValue(PositionUsesPivotPointProperty);
129             set => SetValue(PositionUsesPivotPointProperty, value);
130         }
131
132         /// This will be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API.
133         [EditorBrowsable(EditorBrowsableState.Never)]
134         public Position ParentOrigin
135         {
136             get => (Position)GetValue(ParentOriginProperty);
137             set => SetValue(ParentOriginProperty, value);
138         }
139
140         /// This will be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API.
141         [EditorBrowsable(EditorBrowsableState.Never)]
142         public Position PivotPoint
143         {
144             get => (Position)GetValue(PivotPointProperty);
145             set => SetValue(PivotPointProperty, value);
146         }
147
148         /// This will be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API.
149         [EditorBrowsable(EditorBrowsableState.Never)]
150         public float? SizeWidth
151         {
152             get => (float?)GetValue(SizeWidthProperty);
153             set => SetValue(SizeWidthProperty, value);
154         }
155
156         /// This will be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API.
157         [EditorBrowsable(EditorBrowsableState.Never)]
158         public float? SizeHeight
159         {
160             get => (float?)GetValue(SizeHeightProperty);
161             set => SetValue(SizeHeightProperty, value);
162         }
163
164         /// This will be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API.
165         [EditorBrowsable(EditorBrowsableState.Never)]
166         public Position Position
167         {
168             get => (Position)GetValue(PositionProperty);
169             set => SetValue(PositionProperty, value);
170         }
171
172         /// This will be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API.
173         [EditorBrowsable(EditorBrowsableState.Never)]
174         public float? PositionX
175         {
176             get => (float?)GetValue(PositionXProperty);
177             set => SetValue(PositionXProperty, value);
178         }
179
180         /// This will be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API.
181         [EditorBrowsable(EditorBrowsableState.Never)]
182         public float? PositionY
183         {
184             get => (float?)GetValue(PositionYProperty);
185             set => SetValue(PositionYProperty, value);
186         }
187
188         /// This will be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API.
189         [EditorBrowsable(EditorBrowsableState.Never)]
190         public Rotation Orientation
191         {
192             get => (Rotation)GetValue(OrientationProperty);
193             set => SetValue(OrientationProperty, value);
194         }
195
196         /// This will be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API.
197         [EditorBrowsable(EditorBrowsableState.Never)]
198         public DrawModeType? DrawMode
199         {
200             get => (DrawModeType?)GetValue(DrawModeProperty);
201             set => SetValue(DrawModeProperty, value);
202         }
203
204         /// This will be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API.
205         [EditorBrowsable(EditorBrowsableState.Never)]
206         public Vector3 SizeModeFactor
207         {
208             get => (Vector3)GetValue(SizeModeFactorProperty);
209             set => SetValue(SizeModeFactorProperty, value);
210         }
211
212         /// This will be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API.
213         [EditorBrowsable(EditorBrowsableState.Never)]
214         public ResizePolicyType? WidthResizePolicy
215         {
216             get => (ResizePolicyType?)GetValue(WidthResizePolicyProperty);
217             set => SetValue(WidthResizePolicyProperty, value);
218         }
219
220         /// This will be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API.
221         [EditorBrowsable(EditorBrowsableState.Never)]
222         public ResizePolicyType? HeightResizePolicy
223         {
224             get => (ResizePolicyType?)GetValue(HeightResizePolicyProperty);
225             set => SetValue(HeightResizePolicyProperty, value);
226         }
227
228         /// This will be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API.
229         [EditorBrowsable(EditorBrowsableState.Never)]
230         public bool? WidthForHeight
231         {
232             get => (bool?)GetValue(WidthForHeightProperty);
233             set => SetValue(WidthForHeightProperty, value);
234         }
235
236         /// This will be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API.
237         [EditorBrowsable(EditorBrowsableState.Never)]
238         public bool? HeightForWidth
239         {
240             get => (bool?)GetValue(HeightForWidthProperty);
241             set => SetValue(HeightForWidthProperty, value);
242         }
243
244         /// This will be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API.
245         [EditorBrowsable(EditorBrowsableState.Never)]
246         public Extents Padding
247         {
248             get => (Extents)GetValue(PaddingProperty) ?? (padding = new Extents());
249             set => SetValue(PaddingProperty, value);
250         }
251
252         /// This will be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API.
253         [EditorBrowsable(EditorBrowsableState.Never)]
254         public Size2D MinimumSize
255         {
256             get => (Size2D)GetValue(MinimumSizeProperty);
257             set => SetValue(MinimumSizeProperty, value);
258         }
259
260         /// This will be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API.
261         [EditorBrowsable(EditorBrowsableState.Never)]
262         public Size2D MaximumSize
263         {
264             get => (Size2D)GetValue(MaximumSizeProperty);
265             set => SetValue(MaximumSizeProperty, value);
266         }
267
268         /// This will be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API.
269         [EditorBrowsable(EditorBrowsableState.Never)]
270         public ClippingModeType? ClippingMode
271         {
272             get => (ClippingModeType?)GetValue(ClippingModeProperty);
273             set => SetValue(ClippingModeProperty, value);
274         }
275
276         /// This will be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API.
277         [EditorBrowsable(EditorBrowsableState.Never)]
278         public Size Size
279         {
280             get => (Size)GetValue(SizeProperty);
281             set => SetValue(SizeProperty, value);
282         }
283
284         /// This will be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API.
285         [EditorBrowsable(EditorBrowsableState.Never)]
286         public Extents Margin
287         {
288             get => (Extents)GetValue(MarginProperty) ?? (margin = new Extents());
289             set => SetValue(MarginProperty, value);
290         }
291
292         /// <summary> View BackgroundColor </summary>
293         /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
294         [EditorBrowsable(EditorBrowsableState.Never)]
295         public Selector<Color> BackgroundColor
296         {
297             get
298             {
299                 Selector<Color> color = (Selector<Color>)GetValue(BackgroundColorSelectorProperty);
300                 return (null != color) ? color : backgroundColorSelector = new Selector<Color>();
301             }
302             set => SetValue(BackgroundColorSelectorProperty, value);
303         }
304
305         /// <summary>
306         /// Color
307         /// </summary>
308         [EditorBrowsable(EditorBrowsableState.Never)]
309         public Selector<Color> Color
310         {
311             get => (Selector<Color>)GetValue(ColorSelectorProperty) ?? (colorSelector = new Selector<Color>());
312             set => SetValue(ColorSelectorProperty, value);
313         }
314
315         /// <summary>View BackgroundBorder</summary>
316         /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
317         [EditorBrowsable(EditorBrowsableState.Never)]
318         public Selector<Rectangle> BackgroundImageBorder
319         {
320             get
321             {
322                 Selector<Rectangle> border = (Selector<Rectangle>)GetValue(BackgroundImageBorderSelectorProperty);
323                 return (null != border) ? border : backgroundImageBorderSelector = new Selector<Rectangle>();
324             }
325             set => SetValue(BackgroundImageBorderSelectorProperty, value);
326         }
327
328         /// <summary>
329         /// Describes a shadow as an image for a View.
330         /// It is null by default.
331         /// </summary>
332         /// <remarks>
333         /// If BoxShadow is not null, the ImageShadow value will be ignored.
334         /// </remarks>
335         [EditorBrowsable(EditorBrowsableState.Never)]
336         public Selector<ImageShadow> ImageShadow
337         {
338             get => (Selector<ImageShadow>)GetValue(ImageShadowSelectorProperty);
339             set => SetValue(ImageShadowSelectorProperty, value);
340         }
341
342         /// <summary>
343         /// Describes a box shaped shadow drawing for a View.
344         /// It is null by default.
345         /// </summary>
346         [EditorBrowsable(EditorBrowsableState.Never)]
347         public Selector<Shadow> BoxShadow
348         {
349             get => (Selector<Shadow>)GetValue(BoxShadowSelectorProperty);
350             set => SetValue(BoxShadowSelectorProperty, value);
351         }
352
353         /// <summary>
354         /// The radius for the rounded corners of the View
355         /// </summary>
356         [EditorBrowsable(EditorBrowsableState.Never)]
357         public float? CornerRadius
358         {
359             get => (float?)GetValue(CornerRadiusProperty);
360             set => SetValue(CornerRadiusProperty, value);
361         }
362
363         /// <summary>
364         /// Whether the CornerRadius property value is relative (percentage [0.0f to 1.0f] of the view size) or absolute (in world units).
365         /// It is absolute by default.
366         /// When the policy is relative, the corner radius is relative to the smaller of the view's width and height.
367         /// </summary>
368         [EditorBrowsable(EditorBrowsableState.Never)]
369         public VisualTransformPolicyType? CornerRadiusPolicy
370         {
371             get => (VisualTransformPolicyType?)GetValue(CornerRadiusPolicyProperty);
372             set => SetValue(CornerRadiusPolicyProperty, value);
373         }
374
375         /// <summary>
376         /// The ThemeChangeSensitive value of the View.
377         /// </summary>
378         [EditorBrowsable(EditorBrowsableState.Never)]
379         public bool? ThemeChangeSensitive
380         {
381             get => (bool?)GetValue(ThemeChangeSensitiveProperty);
382             set => SetValue(ThemeChangeSensitiveProperty, value);
383         }
384
385
386         /// <summary>
387         /// Allow null properties when merging it into other Theme.
388         /// If the value is true, the null properties reset target properties of the other ViewStyle with same key when merge.
389         /// It is used in <seealso cref="Theme.Merge(string)"/>, <seealso cref="Theme.Merge(Theme)"/>.
390         /// It is also used in <seealso cref="Theme.GetStyle(string)"/> when the Theme has a parent and needs to merge.
391         /// Please note that it is false by default.
392         /// </summary>
393         [EditorBrowsable(EditorBrowsableState.Never)]
394         public bool SolidNull { get; set; } = false;
395
396         /// <summary>
397         /// Set style's bindable properties from the view.
398         /// </summary>
399         /// <param name="view">The view that includes property data.</param>
400         [EditorBrowsable(EditorBrowsableState.Never)]
401         public virtual void CopyPropertiesFromView(View view)
402         {
403             if (view == null) return;
404
405             BindableProperty.GetBindablePropertysOfType(GetType(), out var styleProperties);
406             BindableProperty.GetBindablePropertysOfType(view.GetType(), out var viewProperties);
407
408
409             if (styleProperties == null || viewProperties == null) return;
410
411             foreach (var stylePropertyItem in styleProperties)
412             {
413                 viewProperties.TryGetValue(stylePropertyItem.Key, out var viewProperty);
414
415                 if (viewProperty == null) continue;
416
417                 SetValue(stylePropertyItem.Value, view.GetValue(viewProperty));
418             }
419         }
420
421         /// <summary>Create a cloned ViewStyle.</summary>
422         [EditorBrowsable(EditorBrowsableState.Never)]
423         public ViewStyle Clone()
424         {
425             var cloned = CreateInstance();
426             cloned.CopyFrom(this);
427
428             return cloned;
429         }
430
431         /// <summary>Create a cloned ViewStyle.</summary>
432         [EditorBrowsable(EditorBrowsableState.Never)]
433         public void Merge(ViewStyle other)
434         {
435             AllowNullCopy = other?.SolidNull ?? false;
436             CopyFrom(other);
437             AllowNullCopy = false;
438         }
439
440         /// <summary>
441         /// Release instance.
442         /// </summary>
443         [EditorBrowsable(EditorBrowsableState.Never)]
444         public void Dispose()
445         {
446             Dispose(true);
447             global::System.GC.SuppressFinalize(this);
448         }
449
450         /// <summary>
451         /// Release instance.
452         /// </summary>
453         [EditorBrowsable(EditorBrowsableState.Never)]
454         protected virtual void Dispose(bool disposing)
455         {
456             if (disposed)
457             {
458                 return;
459             }
460
461             if (disposing)
462             {
463                 // Dispose managed state (managed objects).
464                 margin?.Dispose();
465                 maximumSize?.Dispose();
466                 minimumSize?.Dispose();
467                 orientation?.Dispose();
468                 padding?.Dispose();
469                 parentOrigin?.Dispose();
470                 pivotPoint?.Dispose();
471                 position?.Dispose();
472                 size?.Dispose();
473                 sizeModeFactor?.Dispose();
474             }
475
476             disposed = true;
477         }
478
479         internal ViewStyle CreateInstance()
480         {
481             return (ViewStyle)Activator.CreateInstance(GetType());
482         }
483     }
484 }