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()
159 /// The constructor of the Progress class with specific style.
161 /// <param name="style">style name</param>
162 /// <since_tizen> 8 </since_tizen>
163 public Progress(string style) : base(style)
169 /// The constructor of the Progress class with specific style.
171 /// <param name="progressStyle">The style object to initialize the Progress.</param>
172 /// <since_tizen> 8 </since_tizen>
173 public Progress(ProgressStyle progressStyle) : base(progressStyle)
179 /// Prevents from showing child widgets in AT-SPI tree.
181 [EditorBrowsable(EditorBrowsableState.Never)]
182 protected override bool AccessibilityShouldReportZeroChildren()
190 [EditorBrowsable(EditorBrowsableState.Never)]
191 protected override double AccessibilityGetMinimum()
193 if (this.ProgressState == Components.Progress.ProgressStatusType.Determinate)
194 return (double)MinValue;
201 [EditorBrowsable(EditorBrowsableState.Never)]
202 protected override double AccessibilityGetCurrent()
204 if (this.ProgressState == Components.Progress.ProgressStatusType.Determinate)
205 return (double)CurrentValue;
212 [EditorBrowsable(EditorBrowsableState.Never)]
213 protected override double AccessibilityGetMaximum()
215 if (this.ProgressState == Components.Progress.ProgressStatusType.Determinate)
216 return (double)MaxValue;
221 /// The status type of the Progress.
223 /// <since_tizen> 6 </since_tizen>
224 public enum ProgressStatusType
229 /// <since_tizen> 6 </since_tizen>
233 /// Show ProgressImage and BufferImage
235 /// <since_tizen> 6 </since_tizen>
241 /// <since_tizen> 6 </since_tizen>
246 /// Return currently applied style.
249 /// Modifying contents in style may cause unexpected behaviour.
251 /// <since_tizen> 8 </since_tizen>
252 public ProgressStyle Style => (ProgressStyle)(ViewStyle as ProgressStyle)?.Clone();
255 /// The property to get/set Track image object URL of the Progress.
257 /// <since_tizen> 6 </since_tizen>
258 public string TrackImageURL
260 get => trackImage.ResourceUrl;
261 set => trackImage.ResourceUrl = value;
265 /// The property to get/set Progress object image URL of the Progress.
267 /// <since_tizen> 6 </since_tizen>
268 public string ProgressImageURL
270 get => progressImage.ResourceUrl;
271 set => progressImage.ResourceUrl = value;
275 /// The property to get/set Buffer object image resource URL of the Progress.
277 /// <since_tizen> 6 </since_tizen>
278 public string BufferImageURL
280 get => bufferImage.ResourceUrl;
281 set => bufferImage.ResourceUrl = value;
285 /// The property to get/set the indeterminate image.
287 /// <exception cref="NullReferenceException">Thrown when setting null value.</exception>
288 /// <since_tizen> 9 </since_tizen>
289 public string IndeterminateImageUrl
293 if (indeterminateImage == null)
299 return indeterminateImage?.URL;
304 if (value == null || indeterminateImage == null)
306 throw new NullReferenceException("Progress.IndeterminateImage is null");
310 indeterminateImage.URL = value;
316 /// The property to get/set Track object color of the Progress.
318 /// <since_tizen> 6 </since_tizen>
319 public Color TrackColor
321 get => trackImage.BackgroundColor;
322 set => trackImage.BackgroundColor = value;
326 /// The property to get/set Progress object color of the Progress.
328 /// <since_tizen> 6 </since_tizen>
329 public Color ProgressColor
331 get => progressImage.BackgroundColor;
332 set => progressImage.BackgroundColor = value;
336 /// The property to get/set Buffer object color of the Progress.
338 /// <since_tizen> 6 </since_tizen>
339 public Color BufferColor
341 get => bufferImage.BackgroundColor;
342 set => bufferImage.BackgroundColor = value;
346 /// The property to get/set the maximum value of the Progress.
348 /// <since_tizen> 6 </since_tizen>
349 public float MaxValue
353 return (float)GetValue(MaxValueProperty);
357 SetValue(MaxValueProperty, value);
362 /// The property to get/set the minim value of the Progress.
364 /// <since_tizen> 6 </since_tizen>
365 public float MinValue
369 return (float)GetValue(MinValueProperty);
373 SetValue(MinValueProperty, value);
378 /// The property to get/set the current value of the Progress.
380 /// <since_tizen> 6 </since_tizen>
381 public float CurrentValue
385 return (float)GetValue(CurrentValueProperty);
389 SetValue(CurrentValueProperty, value);
392 EmitAccessibilityEvent(AccessibilityPropertyChangeEvent.Value);
398 /// The property to get/set the buffer value of the Progress.
400 /// <since_tizen> 6 </since_tizen>
401 public float BufferValue
405 return (float)GetValue(BufferValueProperty);
409 SetValue(BufferValueProperty, value);
414 /// Gets or sets state of progress.
416 /// <since_tizen> 6 </since_tizen>
417 public ProgressStatusType ProgressState
421 return (ProgressStatusType)GetValue(ProgressStateProperty);
425 SetValue(ProgressStateProperty, value);
430 [EditorBrowsable(EditorBrowsableState.Never)]
431 public override void OnInitialize()
434 SetAccessibilityConstructor(Role.ProgressBar, AccessibilityInterface.Value);
435 // create necessary components
438 InitializeProgress();
439 InitializeIndeterminate();
441 indeterminateAnimation?.Stop();
442 indeterminateAnimation = null;
446 [EditorBrowsable(EditorBrowsableState.Never)]
447 public override void ApplyStyle(ViewStyle style)
449 base.ApplyStyle(style);
451 if (style is ProgressStyle progressStyle)
453 Debug.Assert(trackImage != null);
454 Debug.Assert(progressImage != null);
455 Debug.Assert(bufferImage != null);
457 trackImage.ApplyStyle(progressStyle.Track);
458 progressImage.ApplyStyle(progressStyle.Progress);
459 bufferImage.ApplyStyle(progressStyle.Buffer);
461 if (null != indeterminateImage && null != progressStyle.IndeterminateImageUrl)
463 indeterminateImage.URL = progressStyle.IndeterminateImageUrl;
469 /// Dispose Progress and all children on it.
471 /// <param name="type">Dispose type.</param>
472 /// <since_tizen> 6 </since_tizen>
473 protected override void Dispose(DisposeTypes type)
480 if (type == DisposeTypes.Explicit)
483 //Release your own managed resources here.
484 //You should release all of your own disposable objects here.
485 Utility.Dispose(trackImage);
486 Utility.Dispose(progressImage);
487 Utility.Dispose(bufferImage);
488 indeterminateImage = null;
491 //You must call base.Dispose(type) just before exit.
496 /// Change Image status. It can be override.
498 /// This needs to be considered more if public-open is necessary.
499 [EditorBrowsable(EditorBrowsableState.Never)]
500 private void UpdateStates()
502 ChangeImageState(state);
506 /// Update progress value
508 /// This needs to be considered more if public-open is necessary.
509 [EditorBrowsable(EditorBrowsableState.Never)]
510 private void UpdateValue()
512 if (null == trackImage || null == progressImage)
517 if (minValue >= maxValue || currentValue < minValue || currentValue > maxValue)
522 float width = this.SizeWidth;
523 float height = this.SizeHeight;
524 float progressRatio = (float)(currentValue - minValue) / (float)(maxValue - minValue);
525 float progressWidth = width * progressRatio;
526 progressImage.Size2D = new Size2D((int)(progressWidth + round), (int)height); //Add const round to reach Math.Round function.
527 if (null != bufferImage)
529 if (bufferValue < minValue || bufferValue > maxValue)
534 float bufferRatio = (float)(bufferValue - minValue) / (float)(maxValue - minValue);
535 float bufferWidth = width * bufferRatio;
536 bufferImage.Size2D = new Size2D((int)(bufferWidth + round), (int)height); //Add const round to reach Math.Round function.
540 private Vector4 destinationValue = new Vector4(-1.0f, 0.0f, 10.0f, 1.0f);
541 private Vector4 initialValue = new Vector4(0.0f, 0.0f, 10.0f, 1.0f);
544 /// Update Animation for Indeterminate mode.
546 /// This will be public opened later after ACR done. Before ACR, need to be hidden as inhouse API.
547 [EditorBrowsable(EditorBrowsableState.Never)]
548 private void UpdateIndeterminateAnimation()
550 indeterminateAnimation?.Stop();
552 if (null != indeterminateImage)
554 indeterminateAnimation = AnimateVisual(indeterminateImage, "pixelArea", destinationValue, 0, 1000, AlphaFunction.BuiltinFunctions.Default, initialValue);
555 indeterminateAnimation.Looping = true;
556 indeterminateAnimation.Play();
561 /// Get Progress style.
563 /// <returns>The default progress style.</returns>
564 /// <since_tizen> 8 </since_tizen>
565 protected override ViewStyle CreateViewStyle()
567 return new ProgressStyle();
571 /// Change Image status
573 /// <since_tizen> 6 </since_tizen>
574 /// <param name="statusType">New status type</param>
575 protected void ChangeImageState(ProgressStatusType statusType)
577 if (statusType == ProgressStatusType.Buffering)
579 indeterminateAnimation?.Stop();
580 indeterminateAnimation = null;
582 if (null != indeterminateImage)
584 indeterminateImage.Opacity = 0.0f;
586 progressImage.Hide();
589 else if (statusType == ProgressStatusType.Determinate)
591 indeterminateAnimation?.Stop();
592 indeterminateAnimation = null;
594 if (null != indeterminateImage)
596 indeterminateImage.Opacity = 0.0f;
599 progressImage.Show();
603 else if (statusType == ProgressStatusType.Indeterminate)
606 progressImage.Hide();
607 if (null != indeterminateImage)
609 indeterminateImage.Opacity = 1.0f;
612 UpdateIndeterminateAnimation();
616 private void Initialize()
618 AccessibilityHighlightable = true;
621 private void InitializeTrack()
623 if (null == trackImage)
625 trackImage = new ImageView
627 WidthResizePolicy = ResizePolicyType.FillToParent,
628 HeightResizePolicy = ResizePolicyType.FillToParent,
629 PositionUsesPivotPoint = true,
630 ParentOrigin = NUI.ParentOrigin.TopLeft,
631 PivotPoint = NUI.PivotPoint.TopLeft
637 private void InitializeProgress()
639 if (null == progressImage)
641 progressImage = new ImageView
643 WidthResizePolicy = ResizePolicyType.FillToParent,
644 HeightResizePolicy = ResizePolicyType.FillToParent,
645 PositionUsesPivotPoint = true,
646 ParentOrigin = Tizen.NUI.ParentOrigin.TopLeft,
647 PivotPoint = Tizen.NUI.PivotPoint.TopLeft
653 private void InitializeBuffer()
655 if (null == bufferImage)
657 bufferImage = new ImageView
659 WidthResizePolicy = ResizePolicyType.FillToParent,
660 HeightResizePolicy = ResizePolicyType.FillToParent,
661 PositionUsesPivotPoint = true,
662 ParentOrigin = Tizen.NUI.ParentOrigin.TopLeft,
663 PivotPoint = Tizen.NUI.PivotPoint.TopLeft
669 private void InitializeIndeterminate()
671 indeterminateImage = new ImageVisual
673 PixelArea = new Vector4(0.0f, 0.0f, 10.0f, 1.0f),
674 WrapModeU = WrapModeType.Repeat,
675 SizePolicy = VisualTransformPolicyType.Relative,
676 Origin = Visual.AlignType.Center,
677 AnchorPoint = Visual.AlignType.Center,
679 Size = new Size2D(1, 1),
680 URL = Tizen.Applications.Application.Current.DirectoryInfo.Resource + "nui_component_default_progress_indeterminate.png"
682 AddVisual("Indeterminate", indeterminateImage);
684 if (state == ProgressStatusType.Indeterminate)
686 indeterminateImage.Opacity = 1.0f;