2 * Copyright(c) 2021 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 using Tizen.NUI.BaseComponents;
19 using Tizen.NUI.Binding;
20 using System.ComponentModel;
21 using System.Diagnostics;
23 namespace Tizen.NUI.Components
26 /// The Progress class is used to show the ongoing status with a long narrow bar.
28 /// <since_tizen> 6 </since_tizen>
29 public class Progress : Control
34 [EditorBrowsable(EditorBrowsableState.Never)]
35 public static readonly BindableProperty MaxValueProperty = BindableProperty.Create(nameof(MaxValue), typeof(float), typeof(Progress), default(float), propertyChanged: (bindable, oldValue, newValue) =>
37 var instance = (Progress)bindable;
40 instance.maxValue = (float)newValue;
41 instance.UpdateValue();
44 defaultValueCreator: (bindable) =>
46 var instance = (Progress)bindable;
47 return instance.maxValue;
53 [EditorBrowsable(EditorBrowsableState.Never)]
54 public static readonly BindableProperty MinValueProperty = BindableProperty.Create(nameof(MinValue), typeof(float), typeof(Progress), default(float), propertyChanged: (bindable, oldValue, newValue) =>
56 var instance = (Progress)bindable;
59 instance.minValue = (float)newValue;
60 instance.UpdateValue();
63 defaultValueCreator: (bindable) =>
65 var instance = (Progress)bindable;
66 return instance.minValue;
70 /// CurrentValueProperty
72 [EditorBrowsable(EditorBrowsableState.Never)]
73 public static readonly BindableProperty CurrentValueProperty = BindableProperty.Create(nameof(CurrentValue), typeof(float), typeof(Progress), default(float), propertyChanged: (bindable, oldValue, newValue) =>
75 var instance = (Progress)bindable;
78 if ((float)newValue > instance.maxValue || (float)newValue < instance.minValue)
82 instance.currentValue = (float)newValue;
83 instance.UpdateValue();
86 defaultValueCreator: (bindable) =>
88 var instance = (Progress)bindable;
89 return instance.currentValue;
93 /// BufferValueProperty
95 [EditorBrowsable(EditorBrowsableState.Never)]
96 public static readonly BindableProperty BufferValueProperty = BindableProperty.Create(nameof(BufferValue), typeof(float), typeof(Progress), default(float), propertyChanged: (bindable, oldValue, newValue) =>
98 var instance = (Progress)bindable;
101 if ((float)newValue > instance.maxValue || (float)newValue < instance.minValue)
105 instance.bufferValue = (float)newValue;
106 instance.UpdateValue();
109 defaultValueCreator: (bindable) =>
111 var instance = (Progress)bindable;
112 return instance.bufferValue;
116 /// ProgressStateProperty
118 [EditorBrowsable(EditorBrowsableState.Never)]
119 public static readonly BindableProperty ProgressStateProperty = BindableProperty.Create(nameof(ProgressState), typeof(ProgressStatusType), typeof(Progress), ProgressStatusType.Indeterminate, propertyChanged: (bindable, oldValue, newValue) =>
121 var instance = (Progress)bindable;
122 if (newValue != null)
124 instance.state = (ProgressStatusType)newValue;
125 instance.UpdateStates();
128 defaultValueCreator: (bindable) =>
130 var instance = (Progress)bindable;
131 return instance.state;
134 /// This needs to be considered more if public-open is necessary.
135 private ProgressStatusType state = ProgressStatusType.Determinate;
137 private const float round = 0.5f;
138 private ImageView trackImage = null;
139 private ImageView progressImage = null;
140 private ImageView bufferImage = null;
141 private ImageVisual indeterminateImage = null;
142 private float maxValue = 100;
143 private float minValue = 0;
144 private float currentValue = 0;
145 private float bufferValue = 0;
146 private Animation indeterminateAnimation = null;
148 static Progress() { }
150 /// The constructor of Progress
152 /// <since_tizen> 6 </since_tizen>
153 public Progress() : base()
158 /// The constructor of the Progress class with specific style.
160 /// <param name="style">style name</param>
161 /// <since_tizen> 8 </since_tizen>
162 public Progress(string style) : base(style)
167 /// The constructor of the Progress class with specific style.
169 /// <param name="progressStyle">The style object to initialize the Progress.</param>
170 /// <since_tizen> 8 </since_tizen>
171 public Progress(ProgressStyle progressStyle) : base(progressStyle)
176 /// The status type of the Progress.
178 /// <since_tizen> 6 </since_tizen>
179 public enum ProgressStatusType
184 /// <since_tizen> 6 </since_tizen>
188 /// Show ProgressImage and BufferImage
190 /// <since_tizen> 6 </since_tizen>
196 /// <since_tizen> 6 </since_tizen>
201 /// Return currently applied style.
204 /// Modifying contents in style may cause unexpected behaviour.
206 /// <since_tizen> 8 </since_tizen>
207 public ProgressStyle Style => (ProgressStyle)(ViewStyle as ProgressStyle)?.Clone();
210 /// The property to get/set Track image object URL of the Progress.
212 /// <since_tizen> 6 </since_tizen>
213 public string TrackImageURL
215 get => trackImage.ResourceUrl;
216 set => trackImage.ResourceUrl = value;
220 /// The property to get/set Progress object image URL of the Progress.
222 /// <since_tizen> 6 </since_tizen>
223 public string ProgressImageURL
225 get => progressImage.ResourceUrl;
226 set => progressImage.ResourceUrl = value;
230 /// The property to get/set Buffer object image resource URL of the Progress.
232 /// <since_tizen> 6 </since_tizen>
233 public string BufferImageURL
235 get => bufferImage.ResourceUrl;
236 set => bufferImage.ResourceUrl = value;
240 /// The property to get/set the indeterminate image.
242 /// <exception cref="NullReferenceException">Thrown when setting null value.</exception>
243 /// <since_tizen> 9 </since_tizen>
244 public string IndeterminateImageUrl
248 if (indeterminateImage == null)
254 return indeterminateImage?.URL;
259 if (value == null || indeterminateImage == null)
261 throw new NullReferenceException("Progress.IndeterminateImage is null");
265 indeterminateImage.URL = value;
271 /// The property to get/set Track object color of the Progress.
273 /// <since_tizen> 6 </since_tizen>
274 public Color TrackColor
276 get => trackImage.BackgroundColor;
277 set => trackImage.BackgroundColor = value;
281 /// The property to get/set Progress object color of the Progress.
283 /// <since_tizen> 6 </since_tizen>
284 public Color ProgressColor
286 get => progressImage.BackgroundColor;
287 set => progressImage.BackgroundColor = value;
291 /// The property to get/set Buffer object color of the Progress.
293 /// <since_tizen> 6 </since_tizen>
294 public Color BufferColor
296 get => bufferImage.BackgroundColor;
297 set => bufferImage.BackgroundColor = value;
301 /// The property to get/set the maximum value of the Progress.
303 /// <since_tizen> 6 </since_tizen>
304 public float MaxValue
308 return (float)GetValue(MaxValueProperty);
312 SetValue(MaxValueProperty, value);
317 /// The property to get/set the minim value of the Progress.
319 /// <since_tizen> 6 </since_tizen>
320 public float MinValue
324 return (float)GetValue(MinValueProperty);
328 SetValue(MinValueProperty, value);
333 /// The property to get/set the current value of the Progress.
335 /// <since_tizen> 6 </since_tizen>
336 public float CurrentValue
340 return (float)GetValue(CurrentValueProperty);
344 SetValue(CurrentValueProperty, value);
349 /// The property to get/set the buffer value of the Progress.
351 /// <since_tizen> 6 </since_tizen>
352 public float BufferValue
356 return (float)GetValue(BufferValueProperty);
360 SetValue(BufferValueProperty, value);
365 /// Gets or sets state of progress.
367 /// <since_tizen> 6 </since_tizen>
368 public ProgressStatusType ProgressState
372 return (ProgressStatusType)GetValue(ProgressStateProperty);
376 SetValue(ProgressStateProperty, value);
381 [EditorBrowsable(EditorBrowsableState.Never)]
382 public override void OnInitialize()
385 // create necessary components
388 InitializeProgress();
389 InitializeIndeterminate();
391 indeterminateAnimation?.Stop();
392 indeterminateAnimation = null;
396 [EditorBrowsable(EditorBrowsableState.Never)]
397 public override void ApplyStyle(ViewStyle style)
399 base.ApplyStyle(style);
401 if (style is ProgressStyle progressStyle)
403 Debug.Assert(trackImage != null);
404 Debug.Assert(progressImage != null);
405 Debug.Assert(bufferImage != null);
407 trackImage.ApplyStyle(progressStyle.Track);
408 progressImage.ApplyStyle(progressStyle.Progress);
409 bufferImage.ApplyStyle(progressStyle.Buffer);
411 if (null != indeterminateImage && null != progressStyle.IndeterminateImageUrl)
413 indeterminateImage.URL = progressStyle.IndeterminateImageUrl;
419 /// Dispose Progress and all children on it.
421 /// <param name="type">Dispose type.</param>
422 /// <since_tizen> 6 </since_tizen>
423 protected override void Dispose(DisposeTypes type)
430 if (type == DisposeTypes.Explicit)
433 //Release your own managed resources here.
434 //You should release all of your own disposable objects here.
435 Utility.Dispose(trackImage);
436 Utility.Dispose(progressImage);
437 Utility.Dispose(bufferImage);
438 indeterminateImage = null;
441 //You must call base.Dispose(type) just before exit.
446 /// Change Image status. It can be override.
448 /// This needs to be considered more if public-open is necessary.
449 [EditorBrowsable(EditorBrowsableState.Never)]
450 private void UpdateStates()
452 ChangeImageState(state);
456 /// Update progress value
458 /// This needs to be considered more if public-open is necessary.
459 [EditorBrowsable(EditorBrowsableState.Never)]
460 private void UpdateValue()
462 if (null == trackImage || null == progressImage)
467 if (minValue >= maxValue || currentValue < minValue || currentValue > maxValue)
472 float width = this.SizeWidth;
473 float height = this.SizeHeight;
474 float progressRatio = (float)(currentValue - minValue) / (float)(maxValue - minValue);
475 float progressWidth = width * progressRatio;
476 progressImage.Size2D = new Size2D((int)(progressWidth + round), (int)height); //Add const round to reach Math.Round function.
477 if (null != bufferImage)
479 if (bufferValue < minValue || bufferValue > maxValue)
484 float bufferRatio = (float)(bufferValue - minValue) / (float)(maxValue - minValue);
485 float bufferWidth = width * bufferRatio;
486 bufferImage.Size2D = new Size2D((int)(bufferWidth + round), (int)height); //Add const round to reach Math.Round function.
490 private Vector4 destinationValue = new Vector4(-1.0f, 0.0f, 10.0f, 1.0f);
491 private Vector4 initialValue = new Vector4(0.0f, 0.0f, 10.0f, 1.0f);
494 /// Update Animation for Indeterminate mode.
496 /// This will be public opened later after ACR done. Before ACR, need to be hidden as inhouse API.
497 [EditorBrowsable(EditorBrowsableState.Never)]
498 private void UpdateIndeterminateAnimation()
500 indeterminateAnimation?.Stop();
502 if (null != indeterminateImage)
504 indeterminateAnimation = AnimateVisual(indeterminateImage, "pixelArea", destinationValue, 0, 1000, AlphaFunction.BuiltinFunctions.Default, initialValue);
505 indeterminateAnimation.Looping = true;
506 indeterminateAnimation.Play();
511 /// Get Progress style.
513 /// <returns>The default progress style.</returns>
514 /// <since_tizen> 8 </since_tizen>
515 protected override ViewStyle CreateViewStyle()
517 return new ProgressStyle();
521 /// Change Image status
523 /// <since_tizen> 6 </since_tizen>
524 /// <param name="statusType">New status type</param>
525 protected void ChangeImageState(ProgressStatusType statusType)
527 if (statusType == ProgressStatusType.Buffering)
529 indeterminateAnimation?.Stop();
530 indeterminateAnimation = null;
532 if (null != indeterminateImage)
534 indeterminateImage.Opacity = 0.0f;
536 progressImage.Hide();
539 else if (statusType == ProgressStatusType.Determinate)
541 indeterminateAnimation?.Stop();
542 indeterminateAnimation = null;
544 if (null != indeterminateImage)
546 indeterminateImage.Opacity = 0.0f;
549 progressImage.Show();
553 else if (statusType == ProgressStatusType.Indeterminate)
556 progressImage.Hide();
557 if (null != indeterminateImage)
559 indeterminateImage.Opacity = 1.0f;
562 UpdateIndeterminateAnimation();
566 private void InitializeTrack()
568 if (null == trackImage)
570 trackImage = new ImageView
572 WidthResizePolicy = ResizePolicyType.FillToParent,
573 HeightResizePolicy = ResizePolicyType.FillToParent,
574 PositionUsesPivotPoint = true,
575 ParentOrigin = NUI.ParentOrigin.TopLeft,
576 PivotPoint = NUI.PivotPoint.TopLeft
582 private void InitializeProgress()
584 if (null == progressImage)
586 progressImage = new ImageView
588 WidthResizePolicy = ResizePolicyType.FillToParent,
589 HeightResizePolicy = ResizePolicyType.FillToParent,
590 PositionUsesPivotPoint = true,
591 ParentOrigin = Tizen.NUI.ParentOrigin.TopLeft,
592 PivotPoint = Tizen.NUI.PivotPoint.TopLeft
598 private void InitializeBuffer()
600 if (null == bufferImage)
602 bufferImage = new ImageView
604 WidthResizePolicy = ResizePolicyType.FillToParent,
605 HeightResizePolicy = ResizePolicyType.FillToParent,
606 PositionUsesPivotPoint = true,
607 ParentOrigin = Tizen.NUI.ParentOrigin.TopLeft,
608 PivotPoint = Tizen.NUI.PivotPoint.TopLeft
614 private void InitializeIndeterminate()
616 indeterminateImage = new ImageVisual
618 PixelArea = new Vector4(0.0f, 0.0f, 10.0f, 1.0f),
619 WrapModeU = WrapModeType.Repeat,
620 SizePolicy = VisualTransformPolicyType.Relative,
621 Origin = Visual.AlignType.Center,
622 AnchorPoint = Visual.AlignType.Center,
624 Size = new Size2D(1, 1),
625 URL = Tizen.Applications.Application.Current.DirectoryInfo.Resource + "nui_component_default_progress_indeterminate.png"
627 AddVisual("Indeterminate", indeterminateImage);
629 if (state == ProgressStatusType.Indeterminate)
631 indeterminateImage.Opacity = 1.0f;