Refine background property of control (#1183)
[platform/core/csapi/tizenfx.git] / src / Tizen.NUI.Components / Controls / ImageControl.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.ComponentModel;
18 using Tizen.NUI.BaseComponents;
19 using Tizen.NUI.Binding;
20
21 namespace Tizen.NUI.Components
22 {
23     /// <summary>
24     /// The control component is class of image. Temporarily for SelectButton.
25     /// </summary>
26     /// <since_tizen> 6 </since_tizen>
27     /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
28     [EditorBrowsable(EditorBrowsableState.Never)]
29     public class ImageControl : Control
30     {
31         /// This will be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API.
32         [EditorBrowsable(EditorBrowsableState.Never)]
33         public static readonly BindableProperty ResourceUrlProperty = BindableProperty.Create("ImageControlResourceUrl", typeof(Selector<string>), typeof(ImageControl), null, propertyChanged: (bindable, oldValue, newValue) =>
34         {
35             var imageControl = (ImageControl)bindable;
36             if (null != newValue)
37             {
38                 imageControl.ResourceUrlSelector.Clone((Selector<string>)newValue);
39             }
40         },
41         defaultValueCreator: (bindable) =>
42         {
43             var imageControl = (ImageControl)bindable;
44             return imageControl.ResourceUrlSelector;
45         });
46         /// This will be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API.
47         [EditorBrowsable(EditorBrowsableState.Never)]
48         public static readonly BindableProperty BorderProperty = BindableProperty.Create("ImageControlBorder", typeof(Selector<Rectangle>), typeof(ImageControl), null, propertyChanged: (bindable, oldValue, newValue) =>
49         {
50             var imageControl = (ImageControl)bindable;
51             if (null == newValue)
52             {
53                 imageControl.BorderSelector.Clone((Selector<Rectangle>)newValue);
54             }
55         },
56         defaultValueCreator: (bindable) =>
57         {
58             var imageControl = (ImageControl)bindable;
59             return imageControl.BorderSelector;
60         });
61
62         /// <summary>
63         /// Control style.
64         /// </summary>
65         /// <since_tizen> 6 </since_tizen>
66         /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
67         [EditorBrowsable(EditorBrowsableState.Never)]
68         private ImageControlStyle imageControlStyle;
69
70         /// This will be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API.
71         [EditorBrowsable(EditorBrowsableState.Never)]
72         public new ImageControlStyle Style => imageControlStyle;
73
74         internal ImageView imageView;
75
76         /// <summary>
77         /// Construct an empty Control.
78         /// </summary>
79         /// <since_tizen> 6 </since_tizen>
80         /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
81         [EditorBrowsable(EditorBrowsableState.Never)]
82         public ImageControl() : base()
83         {
84             Initialize(null);
85         }
86
87         /// <summary>
88         /// Construct with style.
89         /// </summary>
90         /// <param name="style">Create style customized by user</param>
91         /// <since_tizen> 6 </since_tizen>
92         /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
93         [EditorBrowsable(EditorBrowsableState.Never)]
94         public ImageControl(ImageControlStyle style) : base(style)
95         {
96             Initialize(null);
97         }
98
99         /// <summary>
100         /// Construct with style
101         /// </summary>
102         /// <param name="style">Style to be applied</param>
103         /// <since_tizen> 6 </since_tizen>
104         /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
105         [EditorBrowsable(EditorBrowsableState.Never)]
106         public ImageControl(string style) : base(style)
107         {
108             Initialize(style);
109         }
110
111         /// <summary>
112         /// Override view's BackgroundImage.
113         /// </summary>
114         /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
115         [EditorBrowsable(EditorBrowsableState.Never)]
116         public Selector<string> ResourceUrl
117         {
118             get => (Selector<string>)GetValue(ResourceUrlProperty);
119             set => SetValue(ResourceUrlProperty, value);
120         }
121
122         /// <summary>
123         /// Override view's BackgroundImageBorder.
124         /// </summary>
125         /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
126         [EditorBrowsable(EditorBrowsableState.Never)]
127         public Selector<Rectangle> Border
128         {
129             get => (Selector<Rectangle>)GetValue(BorderProperty);
130             set => SetValue(BorderProperty, value);
131         }
132
133         private TriggerableSelector<string> _resourceUrlSelector;
134         private TriggerableSelector<string> ResourceUrlSelector
135         {
136             get
137             {
138                 if (null == _resourceUrlSelector)
139                 {
140                     _resourceUrlSelector = new TriggerableSelector<string>(imageView, ImageView.ResourceUrlProperty);
141                 }
142                 return _resourceUrlSelector;
143             }
144         }
145         private TriggerableSelector<Rectangle> _borderSelector;
146         private TriggerableSelector<Rectangle> BorderSelector
147         {
148             get
149             {
150                 if (null == _borderSelector)
151                 {
152                     _borderSelector = new TriggerableSelector<Rectangle>(imageView, ImageView.BorderProperty);
153                 }
154                 return _borderSelector;
155             }
156         }
157
158         /// This will be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API.
159         [EditorBrowsable(EditorBrowsableState.Never)]
160         public override void ApplyStyle(ViewStyle viewStyle)
161         {
162             base.ApplyStyle(viewStyle);
163
164             ImageControlStyle imageControlStyle = viewStyle as ImageControlStyle;
165             if (null != imageControlStyle)
166             {
167                 if (null == imageView)
168                 {
169                     imageView = new ImageView()
170                     {
171                         PositionUsesPivotPoint = true,
172                         ParentOrigin = Tizen.NUI.ParentOrigin.Center,
173                         PivotPoint = Tizen.NUI.PivotPoint.Center,
174                         WidthResizePolicy = ResizePolicyType.UseNaturalSize,
175                         HeightResizePolicy = ResizePolicyType.UseNaturalSize,
176                     };
177                     this.Add(imageView);
178                 }
179             }
180         }
181
182         /// <summary>
183         /// Dispose Control and all children on it.
184         /// </summary>
185         /// <param name="type">Dispose type.</param>
186         /// <since_tizen> 6 </since_tizen>
187         /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
188         [EditorBrowsable(EditorBrowsableState.Never)]
189         protected override void Dispose(DisposeTypes type)
190         {
191             if (disposed)
192             {
193                 return;
194             }
195
196             if (imageView != null)
197             {
198                 Utility.Dispose(imageView);
199             }
200
201             base.Dispose(type);
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         protected override ViewStyle GetViewStyle()
207         {
208             imageControlStyle = new ImageControlStyle();
209             return imageControlStyle;
210         }
211
212         private void Initialize(string style)
213         {
214             if (null == imageView)
215             {
216                 imageView = new ImageView()
217                 {
218                     PositionUsesPivotPoint = true,
219                     ParentOrigin = Tizen.NUI.ParentOrigin.Center,
220                     PivotPoint = Tizen.NUI.PivotPoint.Center,
221                     WidthResizePolicy = ResizePolicyType.UseNaturalSize,
222                     HeightResizePolicy = ResizePolicyType.UseNaturalSize,
223                 };
224                 this.Add(imageView);
225             }
226         }
227     }
228 }