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