[NUI] Remove bindings between Style and View (#1788)
[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.loadingStyle.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.loadingStyle.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 = (View)bindable;
65             return instance.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.loadingStyle.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.loadingStyle.FrameRate?.All ?? (int)(1000/16.6f);
86         });
87
88         private AnimatedImageVisual imageVisual = null;
89         private LoadingStyle loadingStyle => ViewStyle as LoadingStyle;
90
91         static Loading() { }
92
93         /// <summary>
94         /// The constructor of Loading.
95         /// </summary>
96         /// <since_tizen> 6 </since_tizen>
97         public Loading() : base()
98         {
99             Initialize();
100         }
101
102         /// <summary>
103         /// Constructor of the Loading class with special style.
104         /// </summary>
105         /// <param name="style">The string to initialize the Loading.</param>
106         /// <since_tizen> 8 </since_tizen>
107         public Loading(string style) : base(style)
108         {
109             Initialize();
110         }
111
112         /// <summary>
113         /// The constructor of the Loading class with specific style.
114         /// </summary>
115         /// <param name="loadingStyle">The style object to initialize the Loading.</param>
116         /// <since_tizen> 8 </since_tizen>
117         public Loading(LoadingStyle loadingStyle) : base(loadingStyle)
118         {
119             Initialize();
120         }
121
122         /// <summary>
123         /// Get style of loading.
124         /// Return a copied Style instance of Loading
125         /// </summary>
126         /// <remarks>
127         /// It returns copied Style instance and changing it does not effect to the Loading.
128         /// Style setting is possible by using constructor or the function of ApplyStyle(ViewStyle viewStyle)
129         /// </remarks>>
130         /// <since_tizen> 8 </since_tizen>
131         public new LoadingStyle Style
132         {
133             get
134             {
135                 var result = new LoadingStyle(loadingStyle);
136                 result.CopyPropertiesFromView(this);
137                 return result;
138             }
139         }
140
141         /// <summary>
142         /// Gets or sets loading image resource array.
143         /// </summary>
144         /// <since_tizen> 6 </since_tizen>
145         public string[] ImageArray
146         {
147             get
148             {
149                 return (string[])GetValue(ImageArrayProperty);
150             }
151             set
152             {
153                 SetValue(ImageArrayProperty, value);
154             }
155         }
156
157         /// <summary>
158         /// Gets or sets loading size.
159         /// </summary>
160         /// <since_tizen> 6 </since_tizen>
161         public new Size Size
162         {
163             get
164             {
165                 return (Size)GetValue(SizeProperty);
166             }
167             set
168             {
169                 SetValue(SizeProperty, value);
170             }
171         }
172
173         /// <summary>
174         /// Gets or sets frame rate of loading.
175         /// </summary>
176         /// <since_tizen> 6 </since_tizen>
177         public int FrameRate
178         {
179             get
180             {
181                 return (int)GetValue(FrameRateProperty);
182             }
183             set
184             {
185                 SetValue(FrameRateProperty, value);
186             }
187         }
188
189         /// <summary>
190         /// Get Loading style.
191         /// </summary>
192         /// <returns>The default loading style.</returns>
193         /// <since_tizen> 8 </since_tizen>
194         protected override ViewStyle CreateViewStyle()
195         {
196             return new LoadingStyle();
197         }
198
199         /// <summary>
200         /// Dispose Loading.
201         /// </summary>
202         /// <param name="type">Dispose type.</param>
203         /// <since_tizen> 6 </since_tizen>
204         protected override void Dispose(DisposeTypes type)
205         {
206             if (disposed)
207             {
208                 return;
209             }
210
211             if (type == DisposeTypes.Explicit)
212             {
213                 //Called by User
214                 //Release your own managed resources here.
215                 //You should release all of your own disposable objects here.
216                 RemoveVisual("loadingImageVisual");
217             }
218
219             //You must call base.Dispose(type) just before exit.
220             base.Dispose(type);
221         }
222
223         private void Initialize()
224         {
225             imageVisual = new AnimatedImageVisual()
226             {
227                 URLS = new List<string>(),
228                 FrameDelay = 16.6f,
229                 LoopCount = -1,
230                 Position = new Vector2(0, 0),
231                 Origin = Visual.AlignType.Center,
232                 AnchorPoint = Visual.AlignType.Center
233             };
234
235             UpdateVisual();
236
237             this.AddVisual("loadingImageVisual", imageVisual);
238         }
239
240         private void UpdateVisual()
241         {
242             if (null != loadingStyle.Images)
243             {
244                 imageVisual.URLS = new List<string>(loadingStyle.Images);
245             }
246             if (null != loadingStyle.FrameRate?.All && 0 != loadingStyle.FrameRate.All.Value)
247             {
248                 imageVisual.FrameDelay = 1000.0f / (float)loadingStyle.FrameRate.All.Value;
249             }
250             if (null != loadingStyle.LoadingSize)
251             {
252                 this.Size = new Size2D((int)loadingStyle.LoadingSize.Width, (int)loadingStyle.LoadingSize.Height);
253             }
254         }
255     }
256 }