[NUI] Fix text padding issue
[platform/core/csapi/tizenfx.git] / src / Tizen.NUI / src / public / BaseComponents / ImageViewBindableProperty.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
18 using System.ComponentModel;
19 using System.Runtime.InteropServices;
20 using Tizen.NUI.Binding;
21
22 namespace Tizen.NUI.BaseComponents
23 {
24     public partial class ImageView
25     {
26         /// Intenal used, will never be opened.
27         [EditorBrowsable(EditorBrowsableState.Never)]
28         public static readonly BindableProperty ResourceUrlProperty = BindableProperty.Create(nameof(ImageView.ResourceUrl), typeof(string), typeof(ImageView), string.Empty, propertyChanged: (bindable, oldValue, newValue) =>
29         {
30             var imageView = (ImageView)bindable;
31
32             if (newValue is Selector<string> selector)
33             {
34                 imageView.ResourceUrlSelector = selector;
35             }
36             else
37             {
38                 imageView.resourceUrlSelector?.Reset(imageView);
39                 imageView.SetResourceUrl((string)newValue);
40             }
41         },
42         defaultValueCreator: (BindableProperty.CreateDefaultValueDelegate)((bindable) =>
43         {
44             var imageView = (ImageView)bindable;
45             string ret = "";
46
47             imageView.GetCachedImageVisualProperty(ImageVisualProperty.URL)?.Get(out ret);
48
49             return ret;
50         }));
51
52         /// Intenal used, will never be opened.
53         [EditorBrowsable(EditorBrowsableState.Never)]
54         public static readonly BindableProperty ImageProperty = BindableProperty.Create(nameof(ImageView.Image), typeof(PropertyMap), typeof(ImageView), null, propertyChanged: (BindableProperty.BindingPropertyChangedDelegate)((bindable, oldValue, newValue) =>
55         {
56             var imageView = (ImageView)bindable;
57             if (newValue != null)
58             {
59                 PropertyMap map = (PropertyMap)newValue;
60                 if (imageView.IsCreateByXaml)
61                 {
62                     string url = "", alphaMaskURL = "", auxiliaryImageURL = "";
63                     string resource = Tizen.Applications.Application.Current.DirectoryInfo.Resource;
64                     PropertyValue urlValue = map.Find(NDalic.ImageVisualUrl);
65                     bool ret = false;
66                     if (urlValue != null) ret = urlValue.Get(out url);
67                     PropertyMap mmap = new PropertyMap();
68                     if (ret && url.StartsWith("*Resource*"))
69                     {
70                         url = url.Replace("*Resource*", resource);
71                         mmap.Insert(NDalic.ImageVisualUrl, new PropertyValue(url));
72                     }
73
74                     ret = false;
75                     PropertyValue alphaMaskUrlValue = map.Find(NDalic.ImageVisualAlphaMaskUrl);
76                     if (alphaMaskUrlValue != null) ret = alphaMaskUrlValue.Get(out alphaMaskURL);
77                     if (ret && alphaMaskURL.StartsWith("*Resource*"))
78                     {
79                         alphaMaskURL = alphaMaskURL.Replace("*Resource*", resource);
80                         mmap.Insert(NDalic.ImageVisualUrl, new PropertyValue(alphaMaskURL));
81                     }
82
83                     ret = false;
84                     PropertyValue auxiliaryImageURLValue = map.Find(NDalic.ImageVisualAuxiliaryImageUrl);
85                     if (auxiliaryImageURLValue != null) ret = auxiliaryImageURLValue.Get(out auxiliaryImageURL);
86                     if (ret && auxiliaryImageURL.StartsWith("*Resource*"))
87                     {
88                         auxiliaryImageURL = auxiliaryImageURL.Replace("*Resource*", resource);
89                         mmap.Insert(NDalic.ImageVisualAuxiliaryImageUrl, new PropertyValue(auxiliaryImageURL));
90                     }
91
92                     map.Merge(mmap);
93                 }
94                 if (imageView._border == null)
95                 {
96                     // Image properties are changed hardly. We should ignore lazy UpdateImage
97                     imageView.imagePropertyUpdatedFlag = false;
98                     imageView.cachedImagePropertyMap?.Dispose();
99                     imageView.cachedImagePropertyMap = null;
100                     imageView.MergeCachedImageVisualProperty(map);
101
102                     Tizen.NUI.Object.SetProperty((HandleRef)imageView.SwigCPtr, ImageView.Property.IMAGE, new Tizen.NUI.PropertyValue(map));
103                 }
104             }
105         }),
106         defaultValueCreator: (BindableProperty.CreateDefaultValueDelegate)((bindable) =>
107         {
108             var imageView = (ImageView)bindable;
109             if (imageView._border == null)
110             {
111                 // Sync as current properties
112                 imageView.UpdateImage();
113
114                 // Get current properties force.
115                 PropertyMap returnValue = new PropertyMap();
116                 Tizen.NUI.Object.GetProperty((HandleRef)imageView.SwigCPtr, ImageView.Property.IMAGE).Get(returnValue);
117
118                 // Update cached property map
119                 if (returnValue != null)
120                 {
121                     imageView.MergeCachedImageVisualProperty(returnValue);
122                 }
123                 return returnValue;
124             }
125             else
126             {
127                 return null;
128             }
129         }));
130
131         /// Intenal used, will never be opened.
132         [EditorBrowsable(EditorBrowsableState.Never)]
133         public static readonly BindableProperty PreMultipliedAlphaProperty = BindableProperty.Create(nameof(PreMultipliedAlpha), typeof(bool), typeof(ImageView), false, propertyChanged: (BindableProperty.BindingPropertyChangedDelegate)((bindable, oldValue, newValue) =>
134         {
135             var imageView = (ImageView)bindable;
136             if (newValue != null)
137             {
138                 if (imageView.imagePropertyUpdatedFlag)
139                 {
140                     // If imageView Property still not send to the dali, Append cached property.
141                     imageView.UpdateImage(Visual.Property.PremultipliedAlpha, new PropertyValue((bool)newValue));
142                 }
143                 else
144                 {
145                     // Else, we don't need to re-create view. Get value from current ImageView.
146                     Object.InternalSetPropertyBool(imageView.SwigCPtr, ImageView.Property.PreMultipliedAlpha, (bool)newValue);
147                 }
148             }
149         }),
150         defaultValueCreator: (BindableProperty.CreateDefaultValueDelegate)((bindable) =>
151         {
152             var imageView = (ImageView)bindable;
153             bool temp = false;
154
155             if (imageView.imagePropertyUpdatedFlag)
156             {
157                 // If imageView Property still not send to the dali, just get cached property.
158                 imageView.GetCachedImageVisualProperty(Visual.Property.PremultipliedAlpha)?.Get(out temp);
159             }
160             else
161             {
162                 // Else, PremultipliedAlpha may not setuped in cached property. Get value from current ImageView.
163                 temp = Object.InternalGetPropertyBool(imageView.SwigCPtr, ImageView.Property.PreMultipliedAlpha);
164             }
165             return temp;
166         }));
167
168         /// Intenal used, will never be opened.
169         [EditorBrowsable(EditorBrowsableState.Never)]
170         public static readonly BindableProperty PixelAreaProperty = BindableProperty.Create(nameof(PixelArea), typeof(RelativeVector4), typeof(ImageView), null, propertyChanged: (BindableProperty.BindingPropertyChangedDelegate)((bindable, oldValue, newValue) =>
171         {
172             var imageView = (ImageView)bindable;
173             if (newValue != null)
174             {
175
176                 Object.InternalSetPropertyVector4(imageView.SwigCPtr, ImageView.Property.PixelArea, ((RelativeVector4)newValue).SwigCPtr);
177             }
178         }),
179         defaultValueCreator: (BindableProperty.CreateDefaultValueDelegate)((bindable) =>
180         {
181             var imageView = (ImageView)bindable;
182
183             if (imageView.internalPixelArea == null)
184             {
185                 imageView.internalPixelArea = new RelativeVector4(imageView.OnPixelAreaChanged, 0, 0, 0, 0);
186             }
187             Object.InternalRetrievingPropertyVector4(imageView.SwigCPtr, ImageView.Property.PixelArea, imageView.internalPixelArea.SwigCPtr);
188             return imageView.internalPixelArea;
189         }));
190
191         /// Intenal used, will never be opened.
192         [EditorBrowsable(EditorBrowsableState.Never)]
193         public static readonly BindableProperty BorderProperty = BindableProperty.Create(nameof(Border), typeof(Rectangle), typeof(ImageView), null, propertyChanged: (bindable, oldValue, newValue) =>
194         {
195             var imageView = (ImageView)bindable;
196             imageView.borderSelector?.Reset(imageView);
197
198             if (newValue is Selector<Rectangle> selector)
199             {
200                 if (selector.HasAll()) imageView.SetBorder(selector.All);
201                 else imageView.borderSelector = new TriggerableSelector<Rectangle>(imageView, selector, imageView.SetBorder, true);
202             }
203             else
204             {
205                 imageView.SetBorder((Rectangle)newValue);
206             }
207         },
208         defaultValueCreator: (bindable) =>
209         {
210             var imageView = (ImageView)bindable;
211             return imageView._border;
212         });
213
214         /// Intenal used, will never be opened.
215         [EditorBrowsable(EditorBrowsableState.Never)]
216         public static readonly BindableProperty BorderOnlyProperty = BindableProperty.Create(nameof(BorderOnly), typeof(bool), typeof(ImageView), false, propertyChanged: (bindable, oldValue, newValue) =>
217         {
218             var imageView = (ImageView)bindable;
219             if (newValue != null)
220             {
221                 if (oldValue != null)
222                 {
223                     bool oldBool = (bool)oldValue;
224                     bool newBool = (bool)newValue;
225                     if (oldBool == newBool)
226                     {
227                         return;
228                     }
229                 }
230                 imageView.UpdateImage(NpatchImageVisualProperty.BorderOnly, new PropertyValue((bool)newValue));
231             }
232         },
233         defaultValueCreator: (BindableProperty.CreateDefaultValueDelegate)((bindable) =>
234         {
235             var imageView = (ImageView)bindable;
236             bool ret = false;
237
238             imageView.GetCachedImageVisualProperty(NpatchImageVisualProperty.BorderOnly)?.Get(out ret);
239
240             return ret;
241         }));
242
243         /// Intenal used, will never be opened.
244         [EditorBrowsable(EditorBrowsableState.Never)]
245         public static readonly BindableProperty SynchronosLoadingProperty = BindableProperty.Create(nameof(SynchronousLoading), typeof(bool), typeof(ImageView), false, propertyChanged: (bindable, oldValue, newValue) =>
246         {
247             var imageView = (ImageView)bindable;
248             if (newValue != null)
249             {
250                 if (oldValue != null)
251                 {
252                     bool oldBool = (bool)oldValue;
253                     bool newBool = (bool)newValue;
254                     if (oldBool == newBool)
255                     {
256                         return;
257                     }
258                 }
259                 imageView.UpdateImage(ImageVisualProperty.SynchronousLoading, new PropertyValue((bool)newValue));
260             }
261         },
262         defaultValueCreator: (bindable) =>
263         {
264             var imageView = (ImageView)bindable;
265             bool ret = false;
266
267             imageView.GetCachedImageVisualProperty(ImageVisualProperty.SynchronousLoading)?.Get(out ret);
268
269             return ret;
270         });
271
272         /// This will be public opened in tizen_7.0 after ACR done. Before ACR, need to be hidden as inhouse API.
273         [EditorBrowsable(EditorBrowsableState.Never)]
274         public static readonly BindableProperty SynchronousLoadingProperty = BindableProperty.Create(nameof(SynchronousLoading), typeof(bool), typeof(ImageView), false, propertyChanged: (bindable, oldValue, newValue) =>
275         {
276             var imageView = (ImageView)bindable;
277             if (newValue != null)
278             {
279                 if (oldValue != null)
280                 {
281                     bool oldBool = (bool)oldValue;
282                     bool newBool = (bool)newValue;
283                     if (oldBool == newBool)
284                     {
285                         return;
286                     }
287                 }
288                 imageView.UpdateImage(ImageVisualProperty.SynchronousLoading, new PropertyValue((bool)newValue));
289             }
290         },
291         defaultValueCreator: (bindable) =>
292         {
293             var imageView = (ImageView)bindable;
294             bool ret = false;
295
296             imageView.GetCachedImageVisualProperty(ImageVisualProperty.SynchronousLoading)?.Get(out ret);
297
298             return ret;
299         });
300
301         /// Intenal used, will never be opened.
302         [EditorBrowsable(EditorBrowsableState.Never)]
303         public static readonly BindableProperty OrientationCorrectionProperty = BindableProperty.Create(nameof(OrientationCorrection), typeof(bool), typeof(ImageView), false, propertyChanged: (bindable, oldValue, newValue) =>
304         {
305             var imageView = (ImageView)bindable;
306             if (newValue != null)
307             {
308                 if (oldValue != null)
309                 {
310                     bool oldBool = (bool)oldValue;
311                     bool newBool = (bool)newValue;
312                     if (oldBool == newBool)
313                     {
314                         return;
315                     }
316                 }
317                 imageView.UpdateImage(ImageVisualProperty.OrientationCorrection, new PropertyValue((bool)newValue));
318             }
319         },
320         defaultValueCreator: (BindableProperty.CreateDefaultValueDelegate)((bindable) =>
321         {
322             var imageView = (ImageView)bindable;
323
324             bool ret = false;
325
326             imageView.GetCachedImageVisualProperty(ImageVisualProperty.OrientationCorrection)?.Get(out ret);
327
328             return ret;
329         }));
330
331         /// <summary>
332         /// MaskingModeProperty
333         /// </summary>
334         [EditorBrowsable(EditorBrowsableState.Never)]
335         public static readonly BindableProperty MaskingModeProperty = BindableProperty.Create(nameof(MaskingMode), typeof(MaskingModeType), typeof(ImageView), default(MaskingModeType), propertyChanged: (bindable, oldValue, newValue) =>
336         {
337             var instance = (Tizen.NUI.BaseComponents.ImageView)bindable;
338             if (newValue != null)
339             {
340                 instance.InternalMaskingMode = (ImageView.MaskingModeType)newValue;
341             }
342         },
343         defaultValueCreator: (bindable) =>
344         {
345             var instance = (Tizen.NUI.BaseComponents.ImageView)bindable;
346             return instance.InternalMaskingMode;
347         });
348
349         /// <summary>
350         /// ImageMapProperty
351         /// </summary>
352         [EditorBrowsable(EditorBrowsableState.Never)]
353         public static readonly BindableProperty ImageMapProperty = BindableProperty.Create(nameof(ImageMap), typeof(Tizen.NUI.PropertyMap), typeof(ImageView), null, propertyChanged: (bindable, oldValue, newValue) =>
354         {
355             var instance = (Tizen.NUI.BaseComponents.ImageView)bindable;
356             if (newValue != null)
357             {
358                 instance.InternalImageMap = (Tizen.NUI.PropertyMap)newValue;
359             }
360         },
361         defaultValueCreator: (bindable) =>
362         {
363             var instance = (Tizen.NUI.BaseComponents.ImageView)bindable;
364             return instance.InternalImageMap;
365         });
366
367         /// <summary>
368         /// AlphaMaskURLProperty
369         /// </summary>
370         [EditorBrowsable(EditorBrowsableState.Never)]
371         public static readonly BindableProperty AlphaMaskURLProperty = BindableProperty.Create(nameof(AlphaMaskURL), typeof(string), typeof(ImageView), string.Empty, propertyChanged: (bindable, oldValue, newValue) =>
372         {
373             var instance = (Tizen.NUI.BaseComponents.ImageView)bindable;
374             if (newValue != null)
375             {
376                 instance.InternalAlphaMaskURL = (string)newValue;
377             }
378         },
379         defaultValueCreator: (bindable) =>
380         {
381             var instance = (Tizen.NUI.BaseComponents.ImageView)bindable;
382             return instance.InternalAlphaMaskURL;
383         });
384
385         /// <summary>
386         /// CropToMaskProperty
387         /// </summary>
388         [EditorBrowsable(EditorBrowsableState.Never)]
389         public static readonly BindableProperty CropToMaskProperty = BindableProperty.Create(nameof(CropToMask), typeof(bool), typeof(ImageView), false, propertyChanged: (bindable, oldValue, newValue) =>
390         {
391             var instance = (Tizen.NUI.BaseComponents.ImageView)bindable;
392             if (newValue != null)
393             {
394                 instance.InternalCropToMask = (bool)newValue;
395             }
396         },
397         defaultValueCreator: (bindable) =>
398         {
399             var instance = (Tizen.NUI.BaseComponents.ImageView)bindable;
400             return instance.InternalCropToMask;
401         });
402
403         /// <summary>
404         /// FittingModeProperty
405         /// </summary>
406         [EditorBrowsable(EditorBrowsableState.Never)]
407         public static readonly BindableProperty FittingModeProperty = BindableProperty.Create(nameof(FittingMode), typeof(FittingModeType), typeof(ImageView), default(FittingModeType), propertyChanged: (bindable, oldValue, newValue) =>
408         {
409             var instance = (Tizen.NUI.BaseComponents.ImageView)bindable;
410             if (newValue != null)
411             {
412                 instance.InternalFittingMode = (Tizen.NUI.FittingModeType)newValue;
413             }
414         },
415         defaultValueCreator: (bindable) =>
416         {
417             var instance = (Tizen.NUI.BaseComponents.ImageView)bindable;
418             return instance.InternalFittingMode;
419         });
420
421         /// <summary>
422         /// DesiredWidthProperty
423         /// </summary>
424         [EditorBrowsable(EditorBrowsableState.Never)]
425         public static readonly BindableProperty DesiredWidthProperty = BindableProperty.Create(nameof(DesiredWidth), typeof(int), typeof(ImageView), 0, propertyChanged: (bindable, oldValue, newValue) =>
426         {
427             var instance = (Tizen.NUI.BaseComponents.ImageView)bindable;
428             if (newValue != null)
429             {
430                 instance.InternalDesiredWidth = (int)newValue;
431             }
432         },
433         defaultValueCreator: (bindable) =>
434         {
435             var instance = (Tizen.NUI.BaseComponents.ImageView)bindable;
436             return instance.InternalDesiredWidth;
437         });
438
439         /// <summary>
440         /// DesiredHeightProperty
441         /// </summary>
442         [EditorBrowsable(EditorBrowsableState.Never)]
443         public static readonly BindableProperty DesiredHeightProperty = BindableProperty.Create(nameof(DesiredHeight), typeof(int), typeof(ImageView), 0, propertyChanged: (bindable, oldValue, newValue) =>
444         {
445             var instance = (Tizen.NUI.BaseComponents.ImageView)bindable;
446             if (newValue != null)
447             {
448                 instance.InternalDesiredHeight = (int)newValue;
449             }
450         },
451         defaultValueCreator: (bindable) =>
452         {
453             var instance = (Tizen.NUI.BaseComponents.ImageView)bindable;
454             return instance.InternalDesiredHeight;
455         });
456
457         /// <summary>
458         /// ReleasePolicyProperty
459         /// </summary>
460         [EditorBrowsable(EditorBrowsableState.Never)]
461         public static readonly BindableProperty ReleasePolicyProperty = BindableProperty.Create(nameof(ReleasePolicy), typeof(ReleasePolicyType), typeof(ImageView), default(ReleasePolicyType), propertyChanged: (bindable, oldValue, newValue) =>
462         {
463             var instance = (Tizen.NUI.BaseComponents.ImageView)bindable;
464             if (newValue != null)
465             {
466                 instance.InternalReleasePolicy = (Tizen.NUI.ReleasePolicyType)newValue;
467             }
468         },
469         defaultValueCreator: (bindable) =>
470         {
471             var instance = (Tizen.NUI.BaseComponents.ImageView)bindable;
472             return instance.InternalReleasePolicy;
473         });
474
475         /// <summary>
476         /// WrapModeUProperty
477         /// </summary>
478         [EditorBrowsable(EditorBrowsableState.Never)]
479         public static readonly BindableProperty WrapModeUProperty = BindableProperty.Create(nameof(WrapModeU), typeof(Tizen.NUI.WrapModeType), typeof(ImageView), default(WrapModeType), propertyChanged: (bindable, oldValue, newValue) =>
480         {
481             var instance = (Tizen.NUI.BaseComponents.ImageView)bindable;
482             if (newValue != null)
483             {
484                 instance.InternalWrapModeU = (Tizen.NUI.WrapModeType)newValue;
485             }
486         },
487         defaultValueCreator: (bindable) =>
488         {
489             var instance = (Tizen.NUI.BaseComponents.ImageView)bindable;
490             return instance.InternalWrapModeU;
491         });
492
493         /// <summary>
494         /// WrapModeVProperty
495         /// </summary>
496         [EditorBrowsable(EditorBrowsableState.Never)]
497         public static readonly BindableProperty WrapModeVProperty = BindableProperty.Create(nameof(WrapModeV), typeof(Tizen.NUI.WrapModeType), typeof(ImageView), default(WrapModeType), propertyChanged: (bindable, oldValue, newValue) =>
498         {
499             var instance = (Tizen.NUI.BaseComponents.ImageView)bindable;
500             if (newValue != null)
501             {
502                 instance.InternalWrapModeV = (Tizen.NUI.WrapModeType)newValue;
503             }
504         },
505         defaultValueCreator: (bindable) =>
506         {
507             var instance = (Tizen.NUI.BaseComponents.ImageView)bindable;
508             return instance.InternalWrapModeV;
509         });
510
511         /// <summary>
512         /// AdjustViewSizeProperty
513         /// </summary>
514         [EditorBrowsable(EditorBrowsableState.Never)]
515         public static readonly BindableProperty AdjustViewSizeProperty = BindableProperty.Create(nameof(AdjustViewSize), typeof(bool), typeof(ImageView), false, propertyChanged: (bindable, oldValue, newValue) =>
516         {
517             var instance = (Tizen.NUI.BaseComponents.ImageView)bindable;
518             if (newValue != null)
519             {
520                 instance.adjustViewSize = (bool)newValue;
521             }
522         },
523         defaultValueCreator: (bindable) =>
524         {
525             var instance = (Tizen.NUI.BaseComponents.ImageView)bindable;
526             return instance.adjustViewSize;
527         });
528     }
529 }