[NUI] Add ImageList property (#2449)
[platform/core/csapi/tizenfx.git] / src / Tizen.NUI.Components / Controls / Loading.cs
1 /*
2  * Copyright(c) 2020 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 using Tizen.NUI.Accessibility;
23
24 namespace Tizen.NUI.Components
25 {
26     /// <summary>
27     /// The Loading class of nui component. It's used to indicate informs users of the ongoing operation.
28     /// </summary>
29     /// <since_tizen> 6 </since_tizen>
30     public class Loading : Control
31     {
32         /// <summary>The ImageArray bindable property.</summary>
33         [EditorBrowsable(EditorBrowsableState.Never)]
34         public static readonly BindableProperty ImageArrayProperty = BindableProperty.Create(nameof(ImageArray), typeof(string[]), typeof(Loading), null, propertyChanged: (bindable, oldValue, newValue) =>
35         {
36             var instance = (Loading)bindable;
37             if (newValue != null)
38             {
39                 instance.loadingStyle.Images = (string[])newValue;
40                 instance.imageVisual.URLS = instance.loadingStyle.ImageList as List<string>;
41             }
42         },
43         defaultValueCreator: (bindable) =>
44         {
45             var instance = (Loading)bindable;
46             return instance.loadingStyle.Images;
47         });
48         /// <summary>The ImageList bindable property.</summary>
49         [EditorBrowsable(EditorBrowsableState.Never)]
50         public static readonly BindableProperty ImageListProperty = BindableProperty.Create(nameof(ImageList), typeof(IList<string>), typeof(Loading), null, propertyChanged: (bindable, oldValue, newValue) =>
51         {
52             var instance = (Loading)bindable;
53             if (newValue != null)
54             {
55                 var newValueList = newValue as List<string>;
56                 instance.loadingStyle.ImageList = newValueList;
57                 instance.imageVisual.URLS = newValueList;
58             }
59         },
60         defaultValueCreator: (bindable) =>
61         {
62             return ((Loading)bindable).loadingStyle.ImageList;
63         });
64         /// <summary>The Size bindable property.</summary>
65         [EditorBrowsable(EditorBrowsableState.Never)]
66         public new static readonly BindableProperty SizeProperty = BindableProperty.Create(nameof(Size), typeof(Size), typeof(Loading), new Size(0, 0), propertyChanged: (bindable, oldValue, newValue) =>
67          {
68              var instance = (Loading)bindable;
69              if (newValue != null)
70              {
71                  Size size = (Size)newValue;
72                  ((View)bindable).Size = size;
73                  instance.loadingStyle.LoadingSize = size;
74              }
75          },
76         defaultValueCreator: (bindable) =>
77         {
78             var instance = (View)bindable;
79             return instance.Size;
80         });
81         /// <summary>The FrameRate bindable property.</summary>
82         [EditorBrowsable(EditorBrowsableState.Never)]
83         public static readonly BindableProperty FrameRateProperty = BindableProperty.Create(nameof(FrameRate), typeof(int), typeof(Loading), (int)(1000 / 16.6f), propertyChanged: (bindable, oldValue, newValue) =>
84           {
85               var instance = (Loading)bindable;
86               if (newValue != null)
87               {
88                   int frameRate = (int)newValue;
89                   if (0 != frameRate) //It will crash if 0
90                 {
91                       instance.loadingStyle.FrameRate = frameRate;
92                       instance.imageVisual.FrameDelay = 1000.0f / frameRate;
93                   }
94               }
95           },
96         defaultValueCreator: (bindable) =>
97         {
98             var instance = (Loading)bindable;
99             return instance.loadingStyle.FrameRate?.All ?? (int)(1000 / 16.6f);
100         });
101
102         private AnimatedImageVisual imageVisual = null;
103         private LoadingStyle loadingStyle => ViewStyle as LoadingStyle;
104
105         internal new class Property
106         {
107             internal static readonly int ActionPlay = Interop.ImageView.ImageVisualActionPlayGet();
108             internal static readonly int ActionPause = Interop.ImageView.ImageVisualActionPauseGet();
109             internal static readonly int ActionStop = Interop.ImageView.ImageVisualActionStopGet();
110         }
111
112         static Loading() { }
113
114         /// <summary>
115         /// The constructor of Loading.
116         /// </summary>
117         /// <since_tizen> 6 </since_tizen>
118         public Loading() : base()
119         {
120             Initialize();
121         }
122
123         /// <summary>
124         /// Constructor of the Loading class with special style.
125         /// </summary>
126         /// <param name="style">The string to initialize the Loading.</param>
127         /// <since_tizen> 8 </since_tizen>
128         public Loading(string style) : base(style)
129         {
130             Initialize();
131         }
132
133         /// <summary>
134         /// The constructor of the Loading class with specific style.
135         /// </summary>
136         /// <param name="loadingStyle">The style object to initialize the Loading.</param>
137         /// <since_tizen> 8 </since_tizen>
138         public Loading(LoadingStyle loadingStyle) : base(loadingStyle)
139         {
140             Initialize();
141         }
142
143         /// <summary>
144         /// Get style of loading.
145         /// Return a copied Style instance of Loading
146         /// </summary>
147         /// <remarks>
148         /// It returns copied Style instance and changing it does not effect to the Loading.
149         /// Style setting is possible by using constructor or the function of ApplyStyle(ViewStyle viewStyle)
150         /// </remarks>>
151         /// <since_tizen> 8 </since_tizen>
152         public new LoadingStyle Style
153         {
154             get
155             {
156                 var result = new LoadingStyle(loadingStyle);
157                 result.CopyPropertiesFromView(this);
158                 return result;
159             }
160         }
161
162         /// <summary>
163         /// Gets or sets loading image resource array.
164         /// </summary>
165         /// <since_tizen> 6 </since_tizen>
166         public string[] ImageArray
167         {
168             get
169             {
170                 return (string[])GetValue(ImageArrayProperty);
171             }
172             set
173             {
174                 SetValue(ImageArrayProperty, value);
175             }
176         }
177
178         /// <summary>
179         /// Gets loading image resource array.
180         /// </summary>
181         [EditorBrowsable(EditorBrowsableState.Never)]
182         public IList<string> ImageList
183         {
184             get
185             {
186                 return GetValue(ImageListProperty) as List<string>;
187             }
188         }
189
190         /// <summary>
191         /// Gets or sets loading size.
192         /// </summary>
193         /// <since_tizen> 6 </since_tizen>
194         public new Size Size
195         {
196             get
197             {
198                 return (Size)GetValue(SizeProperty);
199             }
200             set
201             {
202                 SetValue(SizeProperty, value);
203             }
204         }
205
206         /// <summary>
207         /// Gets or sets frame rate of loading.
208         /// </summary>
209         /// <since_tizen> 6 </since_tizen>
210         public int FrameRate
211         {
212             get
213             {
214                 return (int)GetValue(FrameRateProperty);
215             }
216             set
217             {
218                 SetValue(FrameRateProperty, value);
219             }
220         }
221
222         /// <summary>
223         /// Get Loading style.
224         /// </summary>
225         /// <returns>The default loading style.</returns>
226         /// <since_tizen> 8 </since_tizen>
227         protected override ViewStyle CreateViewStyle()
228         {
229             return new LoadingStyle();
230         }
231
232         /// <summary>
233         /// Dispose Loading.
234         /// </summary>
235         /// <param name="type">Dispose type.</param>
236         /// <since_tizen> 6 </since_tizen>
237         protected override void Dispose(DisposeTypes type)
238         {
239             if (disposed)
240             {
241                 return;
242             }
243
244             if (type == DisposeTypes.Explicit)
245             {
246                 //Called by User
247                 //Release your own managed resources here.
248                 //You should release all of your own disposable objects here.
249                 RemoveVisual("loadingImageVisual");
250             }
251
252             //You must call base.Dispose(type) just before exit.
253             base.Dispose(type);
254         }
255
256         private void Initialize()
257         {
258             imageVisual = new AnimatedImageVisual()
259             {
260                 URLS = new List<string>(),
261                 FrameDelay = 16.6f,
262                 LoopCount = -1,
263                 Position = new Vector2(0, 0),
264                 Origin = Visual.AlignType.Center,
265                 AnchorPoint = Visual.AlignType.Center,
266                 SizePolicy = VisualTransformPolicyType.Relative,
267                 Size = new Size2D(1, 1)
268             };
269
270             UpdateVisual();
271
272             this.AddVisual("loadingImageVisual", imageVisual);
273
274             AccessibilityManager.Instance.SetAccessibilityAttribute(this, AccessibilityManager.AccessibilityAttribute.Trait, "Loading");
275         }
276
277         private void UpdateVisual()
278         {
279             if (null != loadingStyle.Images)
280             {
281                 loadingStyle.ImageList = new List<string>(loadingStyle.Images);
282                 imageVisual.URLS = loadingStyle.ImageList as List<string>;
283             }
284             if (null != loadingStyle.FrameRate?.All && 0 != loadingStyle.FrameRate.All.Value)
285             {
286                 imageVisual.FrameDelay = 1000.0f / (float)loadingStyle.FrameRate.All.Value;
287             }
288         }
289
290         /// <summary>
291         /// Play Loading Animation.
292         /// </summary>
293         /// This may be public opened in tizen_6.5 after ACR done. Before ACR, need to be hidden as inhouse API.
294         [EditorBrowsable(EditorBrowsableState.Never)]
295         public void Play()
296         {
297             PropertyValue attributes = new PropertyValue(0);
298             this.DoAction(imageVisual.VisualIndex, Property.ActionPlay, attributes);
299             attributes.Dispose();
300         }
301
302         /// <summary>
303         /// Pause Loading Animation.
304         /// </summary>
305         /// This may be public opened in tizen_6.5 after ACR done. Before ACR, need to be hidden as inhouse API.
306         [EditorBrowsable(EditorBrowsableState.Never)]
307         public void Pause()
308         {
309             PropertyValue attributes = new PropertyValue(0);
310             this.DoAction(imageVisual.VisualIndex, Property.ActionPause, attributes);
311             attributes.Dispose();
312         }
313
314         /// <summary>
315         /// Stop Loading Animation.
316         /// </summary>
317         /// This may be public opened in tizen_6.5 after ACR done. Before ACR, need to be hidden as inhouse API.
318         [EditorBrowsable(EditorBrowsableState.Never)]
319         public void Stop()
320         {
321             PropertyValue attributes = new PropertyValue(0);
322             this.DoAction(imageVisual.VisualIndex, Property.ActionStop, attributes);
323             attributes.Dispose();
324         }
325     }
326 }