Refine background property of control (#1183)
[platform/core/csapi/tizenfx.git] / src / Tizen.NUI.Components / Attributes / ControlStyle.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     /// ControlStyle is a base class of NUI.Components style.
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 ControlStyle : ViewStyle
31     {
32         /// This will be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API.
33         [EditorBrowsable(EditorBrowsableState.Never)]
34         public static readonly BindableProperty BackgroundImageProperty = BindableProperty.Create("ControlBackgroundImage", typeof(Selector<string>), typeof(ControlStyle), null, propertyChanged: (bindable, oldValue, newValue) =>
35         {
36             var controlStyle = (ControlStyle)bindable;
37             if (null == controlStyle.backgroundImage) controlStyle.backgroundImage = new Selector<string>();
38             controlStyle.backgroundImage.Clone((Selector<string>)newValue);
39         },
40         defaultValueCreator: (bindable) =>
41         {
42             var controlStyle = (ControlStyle)bindable;
43             return controlStyle.backgroundImage;
44         });
45         /// This will be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API.
46         [EditorBrowsable(EditorBrowsableState.Never)]
47         public static readonly BindableProperty BackgroundColorProperty = BindableProperty.Create("ControlBackgroundColor", typeof(Selector<Color>), typeof(ControlStyle), null, propertyChanged: (bindable, oldValue, newValue) =>
48         {
49             var controlStyle = (ControlStyle)bindable;
50             if (null == controlStyle.backgroundColor) controlStyle.backgroundColor = new Selector<Color>();
51             controlStyle.backgroundColor.Clone((Selector<Color>)newValue);
52         },
53         defaultValueCreator: (bindable) =>
54         {
55             var controlStyle = (ControlStyle)bindable;
56             return controlStyle.backgroundColor;
57         });
58         /// This will be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API.
59         [EditorBrowsable(EditorBrowsableState.Never)]
60         public static readonly BindableProperty BackgroundImageBorderProperty = BindableProperty.Create("ControlBackgroundImageBorder", typeof(Selector<Rectangle>), typeof(ControlStyle), null, propertyChanged: (bindable, oldValue, newValue) =>
61         {
62             var controlStyle = (ControlStyle)bindable;
63             if (null == controlStyle.backgroundImageBorder) controlStyle.backgroundImageBorder = new Selector<Rectangle>();
64             controlStyle.backgroundImageBorder.Clone((Selector<Rectangle>)newValue);
65         },
66         defaultValueCreator: (bindable) =>
67         {
68             var controlStyle = (ControlStyle)bindable;
69             return controlStyle.backgroundImageBorder;
70         });
71         private Selector<string> backgroundImage;
72         private Selector<Rectangle> backgroundImageBorder;
73         private Selector<Color> backgroundColor;
74         /// <summary>
75         /// Creates a new instance of a ControlStyle.
76         /// </summary>
77         /// <since_tizen> 6 </since_tizen>
78         /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
79         [EditorBrowsable(EditorBrowsableState.Never)]
80         public ControlStyle() : base()
81         {
82             InitSubstyle();
83         }
84
85         /// <summary>
86         /// Creates a new instance of a ControlStyle with style.
87         /// </summary>
88         /// <param name="style">Create ControlStyle by style customized by user.</param>
89         /// <since_tizen> 6 </since_tizen>
90         /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
91         [EditorBrowsable(EditorBrowsableState.Never)]
92         public ControlStyle(ControlStyle style) : base(style)
93         {
94             if(null == style) return;
95
96             InitSubstyle();
97
98             this.CopyFrom(style);
99         }
100
101         /// <summary>Background image resource.</summary>
102         /// This will be public opened in tizen__6.0 after ACR done. Before ACR, need to be hidden as inhouse API.
103         [EditorBrowsable(EditorBrowsableState.Never)]
104         public new Selector<string> BackgroundImage
105         {
106             get
107             {
108                 Selector<string> image = (Selector<string>)GetValue(BackgroundImageProperty);
109                 return (null != image) ? image : backgroundImage = new Selector<string>();
110             }
111             set => SetValue(BackgroundImageProperty, value);
112         }
113
114         /// <summary>Background image border.</summary>
115         /// This will be public opened in tizen__6.0 after ACR done. Before ACR, need to be hidden as inhouse API.
116         [EditorBrowsable(EditorBrowsableState.Never)]
117         public new Selector<Rectangle> BackgroundImageBorder
118         {
119             get
120             {
121                 Selector<Rectangle> boder = (Selector<Rectangle>)GetValue(BackgroundImageBorderProperty);
122                 return (null != boder) ? boder : backgroundImageBorder = new Selector<Rectangle>();
123             }
124             set => SetValue(BackgroundImageBorderProperty, value);
125         }
126
127         /// <summary>Background image color. </summary>
128         /// This will be public opened in tizen__6.0 after ACR done. Before ACR, need to be hidden as inhouse API.
129         [EditorBrowsable(EditorBrowsableState.Never)]
130         public new Selector<Color> BackgroundColor
131         {
132             get
133             {
134                 Selector<Color> color = (Selector<Color>)GetValue(BackgroundColorProperty);
135                 return (null != color) ? color : backgroundColor = new Selector<Color>();
136             }
137             set => SetValue(BackgroundColorProperty, value);
138         }
139
140         /// <summary>
141         /// Shadow image's Style.
142         /// </summary>
143         /// <since_tizen> 6 </since_tizen>
144         /// This will be public opened in tizen__6.0 after ACR done. Before ACR, need to be hidden as inhouse API.
145         [EditorBrowsable(EditorBrowsableState.Never)]
146         public ImageViewStyle Shadow { get; set; }
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 override void CopyFrom(BindableObject bindableObject)
151         {
152             base.CopyFrom(bindableObject);
153
154             ControlStyle controlStyle = bindableObject as ControlStyle;
155
156             if (null != controlStyle)
157             {
158                 if (null != controlStyle.Shadow)
159                 {
160                     Shadow.CopyFrom(controlStyle.Shadow);
161                 }
162                 if (null != controlStyle.BackgroundImage)
163                 {
164                     if (null == BackgroundImage) BackgroundImage = new Selector<string>();
165                     BackgroundImage.Clone(controlStyle.BackgroundImage);
166                 }
167                 if (null != controlStyle.BackgroundImageBorder)
168                 {
169                     if (null == BackgroundImageBorder) BackgroundImageBorder = new Selector<Rectangle>();
170                     BackgroundImageBorder.Clone(controlStyle.BackgroundImageBorder);
171                 }
172                 if (null != controlStyle.BackgroundColor)
173                 {
174                     if (null == BackgroundColor) BackgroundColor = new Selector<Color>();
175                     BackgroundColor.Clone(controlStyle.BackgroundColor);
176                 }
177             }
178         }
179
180         private void InitSubstyle()
181         {
182             Shadow = new ImageViewStyle()
183             {
184                 PositionUsesPivotPoint = true,
185                 ParentOrigin = Tizen.NUI.ParentOrigin.Center,
186                 PivotPoint = Tizen.NUI.PivotPoint.Center,
187                 WidthResizePolicy = ResizePolicyType.FillToParent,
188                 HeightResizePolicy = ResizePolicyType.FillToParent
189             };
190             Shadow.PropertyChanged += SubStyleCalledEvent;
191         }
192
193         private void SubStyleCalledEvent(object sender, global::System.EventArgs e)
194         {
195             OnPropertyChanged();
196         }
197     }
198 }