Fix Loading sizeproperty not work (#1797)
[platform/core/csapi/tizenfx.git] / src / Tizen.NUI.Components / Controls / Loading.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;
18 using System.Collections.Generic;
19 using System.ComponentModel;
20 using Tizen.NUI.BaseComponents;
21 using Tizen.NUI.Binding;
22
23 namespace Tizen.NUI.Components
24 {
25     /// <summary>
26     /// The Loading class of nui component. It's used to indicate informs users of the ongoing operation.
27     /// </summary>
28     /// <since_tizen> 6 </since_tizen>
29     public class Loading : 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 ImageArrayProperty = BindableProperty.Create(nameof(ImageArray), typeof(string[]), typeof(Loading), null, propertyChanged: (bindable, oldValue, newValue) =>
34         {
35             var instance = (Loading)bindable;
36             if (newValue != null)
37             {
38                 instance.Style.Images = (string[])newValue;
39                 instance.imageVisual.URLS = new List<string>((string[])newValue);
40             }
41         },
42         defaultValueCreator: (bindable) =>
43         {
44             var instance = (Loading)bindable;
45             return instance.Style.Images;
46         });
47         /// This will be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API.
48         [EditorBrowsable(EditorBrowsableState.Never)]
49         public new static readonly BindableProperty SizeProperty = BindableProperty.Create(nameof(Size), typeof(Size), typeof(Loading), new Size(0,0), propertyChanged: (bindable, oldValue, newValue) =>
50         {
51             var instance = (Loading)bindable;
52             if (newValue != null)
53             {
54                 Size size = (Size)newValue;
55                 ((View)bindable).ViewStyle.Size = size;
56                 if (null != instance.imageVisual)
57                 {
58                     instance.imageVisual.Size = new Size2D((int)size.Width, (int)size.Height);
59                 }
60             }
61         },
62         defaultValueCreator: (bindable) =>
63         {
64             var instance = (Loading)bindable;
65             return instance.Style.Size;
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 static readonly BindableProperty FrameRateProperty = BindableProperty.Create(nameof(FrameRate), typeof(int), typeof(Loading), (int)(1000/16.6f), propertyChanged: (bindable, oldValue, newValue) =>
70         {
71             var instance = (Loading)bindable;
72             if (newValue != null)
73             {
74                 int frameRate = (int)newValue;
75                 if (0 != frameRate) //It will crash if 0
76                 {
77                     instance.Style.FrameRate.All = frameRate;
78                     instance.imageVisual.FrameDelay = 1000.0f / frameRate;
79                 }
80             }
81         },
82         defaultValueCreator: (bindable) =>
83         {
84             var instance = (Loading)bindable;
85             return instance.Style.FrameRate?.All ?? (int)(1000/16.6f);
86         });
87
88         private AnimatedImageVisual imageVisual = null;
89
90         static Loading() { }
91
92         /// <summary>
93         /// The constructor of Loading.
94         /// </summary>
95         /// <since_tizen> 6 </since_tizen>
96         public Loading() : base()
97         {
98             Initialize();
99         }
100
101         /// <summary>
102         /// Constructor of the Loading class with special style.
103         /// </summary>
104         /// <param name="style">The string to initialize the Loading.</param>
105         /// <since_tizen> 8 </since_tizen>
106         public Loading(string style) : base(style)
107         {
108             Initialize();
109         }
110
111         /// <summary>
112         /// The constructor of the Loading class with specific style.
113         /// </summary>
114         /// <param name="loadingStyle">The style object to initialize the Loading.</param>
115         /// <since_tizen> 8 </since_tizen>
116         public Loading(LoadingStyle loadingStyle) : base(loadingStyle)
117         {
118             Initialize();
119         }
120
121         /// <summary>
122         /// Get style of loading.
123         /// </summary>
124         /// <since_tizen> 8 </since_tizen>
125         public new LoadingStyle Style => ViewStyle as LoadingStyle;
126
127         /// <summary>
128         /// Gets or sets loading image resource array.
129         /// </summary>
130         /// <since_tizen> 6 </since_tizen>
131         public string[] ImageArray
132         {
133             get
134             {
135                 return (string[])GetValue(ImageArrayProperty);
136             }
137             set
138             {
139                 SetValue(ImageArrayProperty, value);
140             }
141         }
142
143         /// <summary>
144         /// Gets or sets loading size.
145         /// </summary>
146         /// <since_tizen> 6 </since_tizen>
147         public new Size Size
148         {
149             get
150             {
151                 return (Size)GetValue(SizeProperty);
152             }
153             set
154             {
155                 SetValue(SizeProperty, value);
156             }
157         }
158
159         /// <summary>
160         /// Gets or sets frame rate of loading.
161         /// </summary>
162         /// <since_tizen> 6 </since_tizen>
163         public int FrameRate
164         {
165             get
166             {
167                 return (int)GetValue(FrameRateProperty);
168             }
169             set
170             {
171                 SetValue(FrameRateProperty, value);
172             }
173         }
174
175         /// <summary>
176         /// Get Loading style.
177         /// </summary>
178         /// <returns>The default loading style.</returns>
179         /// <since_tizen> 8 </since_tizen>
180         protected override ViewStyle CreateViewStyle()
181         {
182             return new LoadingStyle();
183         }
184
185         /// <summary>
186         /// Dispose Loading.
187         /// </summary>
188         /// <param name="type">Dispose type.</param>
189         /// <since_tizen> 6 </since_tizen>
190         protected override void Dispose(DisposeTypes type)
191         {
192             if (disposed)
193             {
194                 return;
195             }
196
197             if (type == DisposeTypes.Explicit)
198             {
199                 //Called by User
200                 //Release your own managed resources here.
201                 //You should release all of your own disposable objects here.
202                 RemoveVisual("loadingImageVisual");
203             }
204
205             //You must call base.Dispose(type) just before exit.
206             base.Dispose(type);
207         }
208
209         private void Initialize()
210         {
211             imageVisual = new AnimatedImageVisual()
212             {
213                 URLS = new List<string>(),
214                 FrameDelay = 16.6f,
215                 LoopCount = -1,
216                 Position = new Vector2(0, 0),
217                 Origin = Visual.AlignType.Center,
218                 AnchorPoint = Visual.AlignType.Center
219             };
220
221             UpdateVisual();
222
223             this.AddVisual("loadingImageVisual", imageVisual);
224         }
225
226         private void UpdateVisual()
227         {
228             if (null != Style.Images)
229             {
230                 imageVisual.URLS = new List<string>(Style.Images);
231             }
232             if (null != Style.FrameRate?.All && 0 != Style.FrameRate.All.Value)
233             {
234                 imageVisual.FrameDelay = 1000.0f / (float)Style.FrameRate.All.Value;
235             }
236             if (null != Style.LoadingSize)
237             {
238                 this.Size = new Size2D((int)Style.LoadingSize.Width, (int)Style.LoadingSize.Height);
239             }
240         }
241     }
242 }