8924dd7a7cdd270338f1a8c8f551355094e78724
[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 ImagesProperty = BindableProperty.Create(nameof(Images), 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).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 Images;
136             }
137             set
138             {
139                 Images = value;
140             }
141         }
142
143         /// <summary>
144         /// Gets or sets loading image resources.
145         /// </summary>
146         /// <since_tizen> 6 </since_tizen>
147         /// This will be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API.
148         [EditorBrowsable(EditorBrowsableState.Never)]
149         public string[] Images
150         {
151             get
152             {
153                 return (string[])GetValue(ImagesProperty);
154             }
155             set
156             {
157                 SetValue(ImagesProperty, value);
158             }
159         }
160
161         /// <summary>
162         /// Gets or sets loading size.
163         /// </summary>
164         /// <since_tizen> 6 </since_tizen>
165         public new Size Size
166         {
167             get
168             {
169                 return (Size)GetValue(SizeProperty);
170             }
171             set
172             {
173                 SetValue(SizeProperty, value);
174             }
175         }
176
177         /// <summary>
178         /// Gets or sets frame rate of loading.
179         /// </summary>
180         /// <since_tizen> 6 </since_tizen>
181         public int FrameRate
182         {
183             get
184             {
185                 return (int)GetValue(FrameRateProperty);
186             }
187             set
188             {
189                 SetValue(FrameRateProperty, value);
190             }
191         }
192
193         /// <summary>
194         /// Get Loading style.
195         /// </summary>
196         /// <returns>The default loading style.</returns>
197         /// <since_tizen> 8 </since_tizen>
198         protected override ViewStyle CreateViewStyle()
199         {
200             return new LoadingStyle();
201         }
202
203         /// <summary>
204         /// Dispose Loading.
205         /// </summary>
206         /// <param name="type">Dispose type.</param>
207         /// <since_tizen> 6 </since_tizen>
208         protected override void Dispose(DisposeTypes type)
209         {
210             if (disposed)
211             {
212                 return;
213             }
214
215             if (type == DisposeTypes.Explicit)
216             {
217                 //Called by User
218                 //Release your own managed resources here.
219                 //You should release all of your own disposable objects here.
220                 RemoveVisual("loadingImageVisual");
221             }
222
223             //You must call base.Dispose(type) just before exit.
224             base.Dispose(type);
225         }
226
227         private void Initialize()
228         {
229             imageVisual = new AnimatedImageVisual()
230             {
231                 URLS = new List<string>(),
232                 FrameDelay = 16.6f,
233                 LoopCount = -1,
234                 Position = new Vector2(0, 0),
235                 Origin = Visual.AlignType.Center,
236                 AnchorPoint = Visual.AlignType.Center
237             };
238
239             UpdateVisual();
240
241             this.AddVisual("loadingImageVisual", imageVisual);
242         }
243
244         private void UpdateVisual()
245         {
246             if (null != Style.Images)
247             {
248                 imageVisual.URLS = new List<string>(Style.Images);
249             }
250             if (null != Style.FrameRate?.All && 0 != Style.FrameRate.All.Value)
251             {
252                 imageVisual.FrameDelay = 1000.0f / (float)Style.FrameRate.All.Value;
253             }
254             if (null != Style.LoadingSize)
255             {
256                 this.Size = new Size2D((int)Style.LoadingSize.Width, (int)Style.LoadingSize.Height);
257             }
258         }
259     }
260 }