[NUI] Change the name of Tizen.NUI.CommonUI as Tizen.NUI.Components (#958)
[platform/core/csapi/tizenfx.git] / src / Tizen.NUI.Components / Controls / Progress.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 Tizen.NUI.BaseComponents;
19 using System.ComponentModel;
20
21 namespace Tizen.NUI.Components
22 {
23     /// <summary>
24     /// The Progress class of nui component. It's used to show the ongoing status with a long narrow bar.
25     /// </summary>
26     /// <since_tizen> 6 </since_tizen>
27     /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
28     [EditorBrowsable(EditorBrowsableState.Never)]
29     public class Progress : Control
30     {
31         /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
32         [EditorBrowsable(EditorBrowsableState.Never)]
33         protected ProgressAttributes progressAttrs = null;
34         /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
35         [EditorBrowsable(EditorBrowsableState.Never)]
36         protected ProgressStatusType state = ProgressStatusType.Indeterminate;
37
38         private const float round = 0.5f;
39         private ImageView trackObj = null;
40         private ImageView progressObj = null;
41         private ImageView bufferObj = null;
42         private ImageView loadingObj = null;
43         private float maxValue = 100;
44         private float minValue = 0;
45         private float currentValue = 0;
46         private float bufferValue = 0;
47
48         /// <summary>
49         /// The constructor of Progress
50         /// </summary>
51         /// <since_tizen> 6 </since_tizen>
52         /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
53         [EditorBrowsable(EditorBrowsableState.Never)]
54         public Progress() : base()
55         {
56             Initialize();
57         }
58
59         /// <summary>
60         /// The constructor of the Progress class with specific style.
61         /// </summary>
62         /// <param name="style">style name</param>
63         /// <since_tizen> 6 </since_tizen>
64         /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
65         [EditorBrowsable(EditorBrowsableState.Never)]
66         public Progress(string style) : base(style)
67         {
68             Initialize();
69         }
70
71         /// <summary>
72         /// The constructor of the Progress class with specific Attributes.
73         /// </summary>
74         /// <param name="attributes">The Attributes object to initialize the Progress.</param>
75         /// <since_tizen> 6 </since_tizen>
76         /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
77         [EditorBrowsable(EditorBrowsableState.Never)]
78         public Progress(ProgressAttributes attributes) : base(attributes)
79         {
80             Initialize();
81         }
82
83         /// <summary>
84         /// The status type of the Progress.
85         /// </summary>
86         /// <since_tizen> 6 </since_tizen>
87         /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
88         [EditorBrowsable(EditorBrowsableState.Never)]
89         public enum ProgressStatusType
90         {
91             /// <summary>
92             /// Show TrackImage
93             /// </summary>
94             /// <since_tizen> 6 </since_tizen>
95             /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
96             [EditorBrowsable(EditorBrowsableState.Never)]
97             Buffering,
98
99             /// <summary>
100             /// Show ProgressImage
101             /// </summary>
102             /// <since_tizen> 6 </since_tizen>
103             /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
104             [EditorBrowsable(EditorBrowsableState.Never)]
105             Determinate,
106
107             /// <summary>
108             /// Show LoadingImage
109             /// </summary>
110             /// <since_tizen> 6 </since_tizen>
111             /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
112             [EditorBrowsable(EditorBrowsableState.Never)]
113             Indeterminate
114         }
115
116         /// <summary>
117         /// The property to get/set Track image object URL of the Progress.
118         /// </summary>
119         /// <since_tizen> 6 </since_tizen>
120         /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
121         [EditorBrowsable(EditorBrowsableState.Never)]
122         public string TrackImageURL
123         {
124             get
125             {
126                 return progressAttrs.TrackImageAttributes?.ResourceURL?.All;
127             }
128             set
129             {
130                 CreateTrackImageAttributes();
131                 if (progressAttrs.TrackImageAttributes.ResourceURL == null)
132                 {
133                     progressAttrs.TrackImageAttributes.ResourceURL = new StringSelector();
134                 }
135                 progressAttrs.TrackImageAttributes.ResourceURL.All = value;
136                 RelayoutRequest();
137             }
138         }
139
140         /// <summary>
141         /// The property to get/set Progress object image URL of the Progress.
142         /// </summary>
143         /// <since_tizen> 6 </since_tizen>
144         /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
145         [EditorBrowsable(EditorBrowsableState.Never)]
146         public string ProgressImageURL
147         {
148             get
149             {
150                 return progressAttrs.ProgressImageAttributes?.ResourceURL?.All;
151             }
152             set
153             {
154                 CreateProgressImageAttributes();
155                 if (progressAttrs.ProgressImageAttributes.ResourceURL == null)
156                 {
157                     progressAttrs.ProgressImageAttributes.ResourceURL = new StringSelector();
158                 }
159                 progressAttrs.ProgressImageAttributes.ResourceURL.All = value;
160                 RelayoutRequest();
161             }
162         }
163
164         /// <summary>
165         /// The property to get/set Buffer object image resource URL of the Progress.
166         /// </summary>
167         /// <since_tizen> 6 </since_tizen>
168         /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
169         [EditorBrowsable(EditorBrowsableState.Never)]
170         public string BufferImageURL
171         {
172             get
173             {
174                 return progressAttrs.BufferImageAttributes?.ResourceURL?.All;
175             }
176             set
177             {
178                 CreateBufferImageAttributes();
179                 if (progressAttrs.BufferImageAttributes.ResourceURL == null)
180                 {
181                     progressAttrs.BufferImageAttributes.ResourceURL = new StringSelector();
182                 }
183                 progressAttrs.BufferImageAttributes.ResourceURL.All = value;
184                 RelayoutRequest();
185             }
186         }
187
188         /// <summary>
189         /// The property to get/set Track object color of the Progress.
190         /// </summary>
191         /// <since_tizen> 6 </since_tizen>
192         /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
193         [EditorBrowsable(EditorBrowsableState.Never)]
194         public Color TrackColor
195         {
196             get
197             {
198                 return progressAttrs.TrackImageAttributes?.BackgroundColor?.All;
199             }
200             set
201             {
202                 CreateTrackImageAttributes();
203                 if (progressAttrs.TrackImageAttributes.BackgroundColor == null)
204                 {
205                     progressAttrs.TrackImageAttributes.BackgroundColor = new ColorSelector();
206                 }
207                 progressAttrs.TrackImageAttributes.BackgroundColor.All = value;
208                 RelayoutRequest();
209             }
210         }
211
212         /// <summary>
213         /// The property to get/set Progress object color of the Progress.
214         /// </summary>
215         /// <since_tizen> 6 </since_tizen>
216         /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
217         [EditorBrowsable(EditorBrowsableState.Never)]
218         public Color ProgressColor
219         {
220             get
221             {
222                 return progressAttrs.ProgressImageAttributes?.BackgroundColor?.All;
223             }
224             set
225             {
226                 CreateProgressImageAttributes();
227                 if (null == progressAttrs.ProgressImageAttributes.BackgroundColor)
228                 {
229                     progressAttrs.ProgressImageAttributes.BackgroundColor = new ColorSelector();
230                 }
231                 progressAttrs.ProgressImageAttributes.BackgroundColor.All = value;
232                 RelayoutRequest();
233             }
234         }
235
236         /// <summary>
237         /// The property to get/set Buffer object color of the Progress.
238         /// </summary>
239         /// <since_tizen> 6 </since_tizen>
240         /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
241         [EditorBrowsable(EditorBrowsableState.Never)]
242         public Color BufferColor
243         {
244             get
245             {
246                 return progressAttrs.BufferImageAttributes?.BackgroundColor?.All;
247             }
248             set
249             {
250                 CreateBufferImageAttributes();
251                 if (null == progressAttrs.BufferImageAttributes.BackgroundColor)
252                 {
253                     progressAttrs.BufferImageAttributes.BackgroundColor = new ColorSelector();
254                 }
255                 progressAttrs.BufferImageAttributes.BackgroundColor.All = value;
256                 RelayoutRequest();
257             }
258         }
259
260         /// <summary>
261         /// The property to get/set the maximum value of the Progress.
262         /// </summary>
263         /// <since_tizen> 6 </since_tizen>
264         /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
265         [EditorBrowsable(EditorBrowsableState.Never)]
266         public float MaxValue
267         {
268             get
269             {
270                 return maxValue;
271             }
272             set
273             {
274                 maxValue = value;
275                 UpdateValue();
276             }
277         }
278
279         /// <summary>
280         /// The property to get/set the minim value of the Progress.
281         /// </summary>
282         /// <since_tizen> 6 </since_tizen>
283         /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
284         [EditorBrowsable(EditorBrowsableState.Never)]
285         public float MinValue
286         {
287             get
288             {
289                 return minValue;
290             }
291             set
292             {
293                 minValue = value;
294                 UpdateValue();
295             }
296         }
297
298         /// <summary>
299         /// The property to get/set the current value of the Progress.
300         /// </summary>
301         /// <since_tizen> 6 </since_tizen>
302         /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
303         [EditorBrowsable(EditorBrowsableState.Never)]
304         public float CurrentValue
305         {
306             get
307             {
308                 return currentValue;
309             }
310             set
311             {
312                 if (value > maxValue || value < minValue)
313                 {
314                     return;
315                 }
316                 currentValue = value;
317                 UpdateValue();
318             }
319         }
320
321         /// <summary>
322         /// The property to get/set the buffer value of the Progress.
323         /// </summary>
324         /// <since_tizen> 6 </since_tizen>
325         /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
326         [EditorBrowsable(EditorBrowsableState.Never)]
327         public float BufferValue
328         {
329             get
330             {
331                 return bufferValue;
332             }
333             set
334             {
335                 if (value > maxValue || value < minValue)
336                 {
337                     return;
338                 }
339                 bufferValue = value;
340                 UpdateValue();
341             }
342         }
343
344         /// <summary>
345         /// Gets or sets state of progress.
346         /// </summary>
347         /// <since_tizen> 6 </since_tizen>
348         /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
349         [EditorBrowsable(EditorBrowsableState.Never)]
350         public ProgressStatusType ProgressState
351         {
352             get
353             {
354                 return state;
355             }
356             set
357             {
358                 state = value;
359                 UpdateStates();
360             }
361         }
362
363         /// <summary>
364         /// Dispose Progress and all children on it.
365         /// </summary>
366         /// <param name="type">Dispose type.</param>
367         /// <since_tizen> 6 </since_tizen>
368         /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
369         [EditorBrowsable(EditorBrowsableState.Never)]
370         protected override void Dispose(DisposeTypes type)
371         {
372             if (disposed)
373             {
374                 return;
375             }
376
377             if (type == DisposeTypes.Explicit)
378             {
379                 //Called by User
380                 //Release your own managed resources here.
381                 //You should release all of your own disposable objects here.
382                 Utility.Dispose(trackObj);
383                 Utility.Dispose(progressObj);
384                 Utility.Dispose(bufferObj);
385                 Utility.Dispose(loadingObj);
386             }
387
388             //Release your own unmanaged resources here.
389             //You should not access any managed member here except static instance.
390             //because the execution order of Finalizes is non-deterministic.
391             //Unreference this from if a static instance refer to this. 
392
393             //You must call base.Dispose(type) just before exit.
394             base.Dispose(type);
395         }
396
397         /// <summary>
398         /// The method to update Attributes.
399         /// </summary>
400         /// <since_tizen> 6 </since_tizen>
401         /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
402         [EditorBrowsable(EditorBrowsableState.Never)]
403         protected override void OnUpdate()
404         {
405             ApplyAttributes(this, progressAttrs);
406             ApplyAttributes(trackObj, progressAttrs.TrackImageAttributes);
407             ApplyAttributes(progressObj, progressAttrs.ProgressImageAttributes);
408             ApplyAttributes(loadingObj, progressAttrs.LoadingImageAttributes);
409             ApplyAttributes(bufferObj, progressAttrs.BufferImageAttributes);
410         }
411
412         /// <summary>
413         /// Theme change callback when theme is changed, this callback will be trigger.
414         /// </summary>
415         /// <param name="sender">serder object</param>
416         /// <param name="e">ThemeChangeEventArgs</param>
417         /// <since_tizen> 6 </since_tizen>
418         /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
419         [EditorBrowsable(EditorBrowsableState.Never)]
420         protected override void OnThemeChangedEvent(object sender, StyleManager.ThemeChangeEventArgs e)
421         {
422             ProgressAttributes tempAttributes = StyleManager.Instance.GetAttributes(style) as ProgressAttributes;
423             if (null != tempAttributes)
424             {
425                 attributes = progressAttrs = tempAttributes;
426                 RelayoutRequest();
427             }
428         }
429
430         /// <summary>
431         /// Change Image status. It can be override.
432         /// </summary>
433         /// <since_tizen> 6 </since_tizen>
434         /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
435         [EditorBrowsable(EditorBrowsableState.Never)]
436         protected virtual void UpdateStates()
437         {
438             ChangeImageState(state);
439         }
440
441         /// <summary>
442         /// Update progress value
443         /// </summary>
444         /// <since_tizen> 6 </since_tizen>
445         /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
446         [EditorBrowsable(EditorBrowsableState.Never)]
447         protected virtual void UpdateValue()
448         {
449             if (null == trackObj || null == progressObj)
450             {
451                 return;
452             }
453
454             if (minValue >= maxValue || currentValue < minValue || currentValue > maxValue)
455             {
456                 return;
457             }
458
459             float width = this.SizeWidth;
460             float height = this.SizeHeight;
461             float progressRatio = (float)(currentValue - minValue) / (float)(maxValue - minValue);
462             float progressWidth = width * progressRatio;
463             progressObj.Size2D = new Size2D((int)(progressWidth + round), (int)height); //Add const round to reach Math.Round function.
464             if (null != bufferObj)
465             {
466                 if (bufferValue < minValue || bufferValue > maxValue)
467                 {
468                     return;
469                 }
470
471                 float bufferRatio = (float)(bufferValue - minValue) / (float)(maxValue - minValue);
472                 float bufferWidth = width * bufferRatio;
473                 bufferObj.Size2D = new Size2D((int)(bufferWidth + round), (int)height); //Add const round to reach Math.Round function.
474             }
475         }
476
477         /// <summary>
478         /// Get Progress attribues.
479         /// </summary>
480         /// <since_tizen> 6 </since_tizen>
481         /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
482         [EditorBrowsable(EditorBrowsableState.Never)]
483         protected override Attributes GetAttributes()
484         {
485             return new ProgressAttributes();
486         }
487
488         /// <summary>
489         /// Change Image status
490         /// </summary>
491         /// <since_tizen> 6 </since_tizen>
492         /// <param name="statusType">New status type</param>
493         protected void ChangeImageState(ProgressStatusType statusType)
494         {
495             if (state == ProgressStatusType.Buffering)
496             {
497                 bufferObj.Show();
498                 loadingObj.Hide();
499                 progressObj.Hide();
500             }
501             else if (state == ProgressStatusType.Determinate)
502             {
503                 bufferObj.Hide();
504                 loadingObj.Hide();
505                 progressObj.Show();
506                 UpdateValue();
507             }
508             else
509             {
510                 bufferObj.Hide();
511                 loadingObj.Show();
512                 progressObj.Hide();
513             }
514         }
515
516         private void Initialize()
517         {
518             progressAttrs = attributes as ProgressAttributes;
519             if (null == progressAttrs)
520             {
521                 throw new Exception("Progress attribute parse error.");
522             }
523
524             // create necessary components
525             InitializeTrack();
526             InitializeBuffer();
527             InitializeProgress();
528             InitializeLoading();
529         }
530
531         private void InitializeTrack()
532         {
533             if (null == trackObj)
534             {
535                 trackObj = new ImageView
536                 {
537                     WidthResizePolicy = ResizePolicyType.FillToParent,
538                     HeightResizePolicy = ResizePolicyType.FillToParent,
539                     PositionUsesPivotPoint = true,
540                     ParentOrigin = Tizen.NUI.ParentOrigin.TopLeft,
541                     PivotPoint = Tizen.NUI.PivotPoint.TopLeft
542                 };
543                 Add(trackObj);
544             }
545         }
546
547         private void InitializeProgress()
548         {
549             if (null == progressObj)
550             {
551                 progressObj = new ImageView
552                 {
553                     WidthResizePolicy = ResizePolicyType.FillToParent,
554                     HeightResizePolicy = ResizePolicyType.FillToParent,
555                     PositionUsesPivotPoint = true,
556                     ParentOrigin = Tizen.NUI.ParentOrigin.TopLeft,
557                     PivotPoint = Tizen.NUI.PivotPoint.TopLeft
558                 };
559                 Add(progressObj);
560             }
561         }
562
563         private void InitializeBuffer()
564         {
565             if (null == bufferObj)
566             {
567                 bufferObj = new ImageView
568                 {
569                     WidthResizePolicy = ResizePolicyType.FillToParent,
570                     HeightResizePolicy = ResizePolicyType.FillToParent,
571                     PositionUsesPivotPoint = true,
572                     ParentOrigin = Tizen.NUI.ParentOrigin.TopLeft,
573                     PivotPoint = Tizen.NUI.PivotPoint.TopLeft
574                 };
575                 Add(bufferObj);
576             }
577         }
578
579         private void InitializeLoading()
580         {
581             if (null == loadingObj)
582             {
583                 loadingObj = new ImageView
584                 {
585                     WidthResizePolicy = ResizePolicyType.FillToParent,
586                     HeightResizePolicy = ResizePolicyType.FillToParent,
587                     PositionUsesPivotPoint = true,
588                     ParentOrigin = Tizen.NUI.ParentOrigin.TopLeft,
589                     PivotPoint = Tizen.NUI.PivotPoint.TopLeft
590                 };
591                 Add(loadingObj);
592             }
593         }
594
595         private void CreateTrackImageAttributes()
596         {
597             if (null == progressAttrs.TrackImageAttributes)
598             {
599                 progressAttrs.TrackImageAttributes = new ImageAttributes();
600             }
601         }
602
603         private void CreateProgressImageAttributes()
604         {
605             if (null == progressAttrs.ProgressImageAttributes)
606             {
607                 progressAttrs.ProgressImageAttributes = new ImageAttributes();
608             }
609         }
610
611         private void CreateBufferImageAttributes()
612         {
613             if (null == progressAttrs.BufferImageAttributes)
614             {
615                 progressAttrs.BufferImageAttributes = new ImageAttributes();
616             }
617         }
618     }
619 }