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;
135 /// IsEnabledProperty
137 [EditorBrowsable(EditorBrowsableState.Never)]
138 public static readonly BindableProperty IsEnabledProperty = BindableProperty.Create(nameof(IsEnabled), typeof(bool), typeof(Progress), true, propertyChanged: (bindable, oldValue, newValue) =>
140 var instance = (Progress)bindable;
141 if (newValue != null)
143 bool newEnabled = (bool)newValue;
144 if (instance.isEnabled != newEnabled)
146 instance.isEnabled = newEnabled;
147 instance.Sensitive = newEnabled;
148 instance.UpdateStates();
152 defaultValueCreator: (bindable) => ((Progress)bindable).isEnabled);
154 /// This needs to be considered more if public-open is necessary.
155 private ProgressStatusType state = ProgressStatusType.Determinate;
157 private Vector2 size = null;
158 private const float round = 0.5f;
159 private ImageView trackImage = null;
160 private ImageView progressImage = null;
161 private ImageView bufferImage = null;
162 private ImageVisual indeterminateImage = null;
163 private float maxValue = 100;
164 private float minValue = 0;
165 private float currentValue = 0;
166 private float bufferValue = 0;
167 private Animation indeterminateAnimation = null;
168 bool isEnabled = true;
170 static Progress() { }
172 /// The constructor of Progress
174 /// <since_tizen> 6 </since_tizen>
175 public Progress() : base()
181 /// The constructor of the Progress class with specific style.
183 /// <param name="style">style name</param>
184 /// <since_tizen> 8 </since_tizen>
185 public Progress(string style) : base(style)
191 /// The constructor of the Progress class with specific style.
193 /// <param name="progressStyle">The style object to initialize the Progress.</param>
194 /// <since_tizen> 8 </since_tizen>
195 public Progress(ProgressStyle progressStyle) : base(progressStyle)
201 /// The status type of the Progress.
203 /// <since_tizen> 6 </since_tizen>
204 public enum ProgressStatusType
209 /// <since_tizen> 6 </since_tizen>
213 /// Show ProgressImage and BufferImage
215 /// <since_tizen> 6 </since_tizen>
221 /// <since_tizen> 6 </since_tizen>
226 /// Return currently applied style.
229 /// Modifying contents in style may cause unexpected behaviour.
231 /// <since_tizen> 8 </since_tizen>
232 public ProgressStyle Style => (ProgressStyle)(ViewStyle as ProgressStyle)?.Clone();
235 /// The property to get/set Track image object URL of the Progress.
237 /// <since_tizen> 6 </since_tizen>
238 public string TrackImageURL
240 get => trackImage.ResourceUrl;
241 set => trackImage.ResourceUrl = value;
245 /// The property to get/set Progress object image URL of the Progress.
247 /// <since_tizen> 6 </since_tizen>
248 public string ProgressImageURL
250 get => progressImage.ResourceUrl;
251 set => progressImage.ResourceUrl = value;
255 /// The property to get/set Buffer object image resource URL of the Progress.
257 /// <since_tizen> 6 </since_tizen>
258 public string BufferImageURL
260 get => bufferImage.ResourceUrl;
261 set => bufferImage.ResourceUrl = value;
265 /// The property to get/set the indeterminate image.
267 /// <exception cref="NullReferenceException">Thrown when setting null value.</exception>
268 /// <since_tizen> 9 </since_tizen>
269 public string IndeterminateImageUrl
273 if (indeterminateImage == null)
279 return indeterminateImage?.URL;
284 if (value == null || indeterminateImage == null)
286 throw new NullReferenceException("Progress.IndeterminateImage is null");
290 indeterminateImage.URL = value;
296 /// The property to get/set Track object color of the Progress.
298 /// <since_tizen> 6 </since_tizen>
299 public Color TrackColor
301 get => trackImage.BackgroundColor;
302 set => trackImage.BackgroundColor = value;
306 /// The property to get/set Progress object color of the Progress.
308 /// <since_tizen> 6 </since_tizen>
309 public Color ProgressColor
311 get => progressImage.BackgroundColor;
312 set => progressImage.BackgroundColor = value;
316 /// The property to get/set Buffer object color of the Progress.
318 /// <since_tizen> 6 </since_tizen>
319 public Color BufferColor
321 get => bufferImage.BackgroundColor;
322 set => bufferImage.BackgroundColor = value;
326 /// The property to get/set the maximum value of the Progress.
328 /// <since_tizen> 6 </since_tizen>
329 public float MaxValue
333 return (float)GetValue(MaxValueProperty);
337 SetValue(MaxValueProperty, value);
342 /// The property to get/set the minim value of the Progress.
344 /// <since_tizen> 6 </since_tizen>
345 public float MinValue
349 return (float)GetValue(MinValueProperty);
353 SetValue(MinValueProperty, value);
358 /// The property to get/set the current value of the Progress.
360 /// <since_tizen> 6 </since_tizen>
361 public float CurrentValue
365 return (float)GetValue(CurrentValueProperty);
369 SetValue(CurrentValueProperty, value);
370 if (Accessibility.Accessibility.Enabled && IsHighlighted)
372 EmitAccessibilityEvent(AccessibilityPropertyChangeEvent.Value);
378 /// The property to get/set the buffer value of the Progress.
380 /// <since_tizen> 6 </since_tizen>
381 public float BufferValue
385 return (float)GetValue(BufferValueProperty);
389 SetValue(BufferValueProperty, value);
394 /// Gets or sets state of progress.
396 /// <since_tizen> 6 </since_tizen>
397 public ProgressStatusType ProgressState
401 return (ProgressStatusType)GetValue(ProgressStateProperty);
405 SetValue(ProgressStateProperty, value);
410 /// Flag to decide enable or disable in Progress.
412 [EditorBrowsable(EditorBrowsableState.Never)]
413 public bool IsEnabled
417 return (bool)GetValue(IsEnabledProperty);
421 SetValue(IsEnabledProperty, value);
426 [EditorBrowsable(EditorBrowsableState.Never)]
427 public override void OnInitialize()
430 SetAccessibilityConstructor(Role.ProgressBar, AccessibilityInterface.Value);
431 // create necessary components
434 InitializeProgress();
435 InitializeIndeterminate();
437 indeterminateAnimation?.Stop();
438 indeterminateAnimation = null;
442 [EditorBrowsable(EditorBrowsableState.Never)]
443 public override void ApplyStyle(ViewStyle style)
445 base.ApplyStyle(style);
447 if (style is ProgressStyle progressStyle)
449 Debug.Assert(trackImage != null);
450 Debug.Assert(progressImage != null);
451 Debug.Assert(bufferImage != null);
453 trackImage.ApplyStyle(progressStyle.Track);
454 progressImage.ApplyStyle(progressStyle.Progress);
455 bufferImage.ApplyStyle(progressStyle.Buffer);
457 if (null != indeterminateImage && null != progressStyle.IndeterminateImageUrl)
459 indeterminateImage.URL = progressStyle.IndeterminateImageUrl;
465 /// Prevents from showing child widgets in AT-SPI tree.
467 [EditorBrowsable(EditorBrowsableState.Never)]
468 protected override bool AccessibilityShouldReportZeroChildren()
474 /// Gets minimum value for Accessibility.
476 [EditorBrowsable(EditorBrowsableState.Never)]
477 protected override double AccessibilityGetMinimum()
479 if (this.ProgressState == Progress.ProgressStatusType.Determinate)
481 return (double)MinValue;
490 /// Gets the current value for Accessibility.
492 [EditorBrowsable(EditorBrowsableState.Never)]
493 protected override double AccessibilityGetCurrent()
495 if (this.ProgressState == Progress.ProgressStatusType.Determinate)
497 return (double)CurrentValue;
506 /// Gets maximum value for Accessibility.
508 [EditorBrowsable(EditorBrowsableState.Never)]
509 protected override double AccessibilityGetMaximum()
511 if (this.ProgressState == Progress.ProgressStatusType.Determinate)
513 return (double)MaxValue;
522 /// Dispose Progress and all children on it.
524 /// <param name="type">Dispose type.</param>
525 /// <since_tizen> 6 </since_tizen>
526 protected override void Dispose(DisposeTypes type)
533 if (type == DisposeTypes.Explicit)
536 //Release your own managed resources here.
537 //You should release all of your own disposable objects here.
538 Utility.Dispose(trackImage);
539 Utility.Dispose(progressImage);
540 Utility.Dispose(bufferImage);
541 indeterminateImage = null;
544 //You must call base.Dispose(type) just before exit.
549 /// Change Image status. It can be override.
551 /// This needs to be considered more if public-open is necessary.
552 [EditorBrowsable(EditorBrowsableState.Never)]
553 private void UpdateStates()
555 ChangeImageState(state);
559 [EditorBrowsable(EditorBrowsableState.Never)]
560 public override void OnRelayout(Vector2 size, RelayoutContainer container)
562 if (size == null) return;
564 if (size.Equals(this.size))
569 this.size = new Vector2(size);
574 /// Update progress value
576 /// This needs to be considered more if public-open is necessary.
577 [EditorBrowsable(EditorBrowsableState.Never)]
578 private void UpdateValue()
580 if (null == trackImage || null == progressImage)
585 if (minValue >= maxValue || currentValue < minValue || currentValue > maxValue)
590 float width = this.SizeWidth;
591 float height = this.SizeHeight;
592 float progressRatio = (float)(currentValue - minValue) / (float)(maxValue - minValue);
593 float progressWidth = width * progressRatio;
594 progressImage.Size2D = new Size2D((int)(progressWidth + round), (int)height); //Add const round to reach Math.Round function.
595 if (null != bufferImage)
597 if (bufferValue < minValue || bufferValue > maxValue)
602 float bufferRatio = (float)(bufferValue - minValue) / (float)(maxValue - minValue);
603 float bufferWidth = width * bufferRatio;
604 bufferImage.Size2D = new Size2D((int)(bufferWidth + round), (int)height); //Add const round to reach Math.Round function.
608 private Vector4 destinationValue = new Vector4(-1.0f, 0.0f, 10.0f, 1.0f);
609 private Vector4 initialValue = new Vector4(0.0f, 0.0f, 10.0f, 1.0f);
612 /// Update Animation for Indeterminate mode.
614 /// This will be public opened later after ACR done. Before ACR, need to be hidden as inhouse API.
615 [EditorBrowsable(EditorBrowsableState.Never)]
616 private void UpdateIndeterminateAnimation()
618 indeterminateAnimation?.Stop();
620 if (null != indeterminateImage)
622 indeterminateAnimation = AnimateVisual(indeterminateImage, "pixelArea", destinationValue, 0, 1000, AlphaFunction.BuiltinFunctions.Default, initialValue);
623 indeterminateAnimation.Looping = true;
624 indeterminateAnimation.Play();
629 /// Get Progress style.
631 /// <returns>The default progress style.</returns>
632 /// <since_tizen> 8 </since_tizen>
633 protected override ViewStyle CreateViewStyle()
635 return new ProgressStyle();
639 /// Change Image status
641 /// <since_tizen> 6 </since_tizen>
642 /// <param name="statusType">New status type</param>
643 protected void ChangeImageState(ProgressStatusType statusType)
647 ControlState = ControlState.Disabled;
649 indeterminateAnimation?.Stop();
650 indeterminateAnimation = null;
652 if (null != indeterminateImage)
654 indeterminateImage.Opacity = 0.0f;
656 progressImage.Hide();
661 if (statusType == ProgressStatusType.Buffering)
663 indeterminateAnimation?.Stop();
664 indeterminateAnimation = null;
666 if (null != indeterminateImage)
668 indeterminateImage.Opacity = 0.0f;
670 progressImage.Hide();
673 else if (statusType == ProgressStatusType.Determinate)
675 indeterminateAnimation?.Stop();
676 indeterminateAnimation = null;
678 if (null != indeterminateImage)
680 indeterminateImage.Opacity = 0.0f;
683 progressImage.Show();
687 else if (statusType == ProgressStatusType.Indeterminate)
690 progressImage.Hide();
691 if (null != indeterminateImage)
693 indeterminateImage.Opacity = 1.0f;
696 UpdateIndeterminateAnimation();
700 private void Initialize()
702 AccessibilityHighlightable = true;
705 private void InitializeTrack()
707 if (null == trackImage)
709 trackImage = new ImageView
711 WidthResizePolicy = ResizePolicyType.FillToParent,
712 HeightResizePolicy = ResizePolicyType.FillToParent,
713 PositionUsesPivotPoint = true,
714 ParentOrigin = NUI.ParentOrigin.TopLeft,
715 PivotPoint = NUI.PivotPoint.TopLeft
721 private void InitializeProgress()
723 if (null == progressImage)
725 progressImage = new ImageView
727 WidthResizePolicy = ResizePolicyType.FillToParent,
728 HeightResizePolicy = ResizePolicyType.FillToParent,
729 PositionUsesPivotPoint = true,
730 ParentOrigin = Tizen.NUI.ParentOrigin.TopLeft,
731 PivotPoint = Tizen.NUI.PivotPoint.TopLeft
737 private void InitializeBuffer()
739 if (null == bufferImage)
741 bufferImage = new ImageView
743 WidthResizePolicy = ResizePolicyType.FillToParent,
744 HeightResizePolicy = ResizePolicyType.FillToParent,
745 PositionUsesPivotPoint = true,
746 ParentOrigin = Tizen.NUI.ParentOrigin.TopLeft,
747 PivotPoint = Tizen.NUI.PivotPoint.TopLeft
750 bufferImage.Hide(); // At first, buffer image does not show.
754 private void InitializeIndeterminate()
756 indeterminateImage = new ImageVisual
758 PixelArea = new Vector4(0.0f, 0.0f, 10.0f, 1.0f),
759 WrapModeU = WrapModeType.Repeat,
760 SizePolicy = VisualTransformPolicyType.Relative,
761 Origin = Visual.AlignType.Center,
762 AnchorPoint = Visual.AlignType.Center,
764 Size = new Size2D(1, 1),
765 URL = Tizen.Applications.Application.Current.DirectoryInfo.Resource + "nui_component_default_progress_indeterminate.png"
767 AddVisual("Indeterminate", indeterminateImage);
769 if (state == ProgressStatusType.Indeterminate)
771 indeterminateImage.Opacity = 1.0f;