/* * Copyright(c) 2019 Samsung Electronics Co., Ltd. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * */ using Tizen.NUI.BaseComponents; using Tizen.NUI.Binding; using System.ComponentModel; namespace Tizen.NUI.Components { /// /// The Progress class of nui component. It's used to show the ongoing status with a long narrow bar. /// /// 6 public class Progress : Control { /// This will be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API. [EditorBrowsable(EditorBrowsableState.Never)] public static readonly BindableProperty MaxValueProperty = BindableProperty.Create(nameof(MaxValue), typeof(float), typeof(Progress), default(float), propertyChanged: (bindable, oldValue, newValue) => { var instance = (Progress)bindable; if (newValue != null) { instance.maxValue = (float)newValue; instance.UpdateValue(); } }, defaultValueCreator: (bindable) => { var instance = (Progress)bindable; return instance.maxValue; }); /// This will be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API. [EditorBrowsable(EditorBrowsableState.Never)] public static readonly BindableProperty MinValueProperty = BindableProperty.Create(nameof(MinValue), typeof(float), typeof(Progress), default(float), propertyChanged: (bindable, oldValue, newValue) => { var instance = (Progress)bindable; if (newValue != null) { instance.minValue = (float)newValue; instance.UpdateValue(); } }, defaultValueCreator: (bindable) => { var instance = (Progress)bindable; return instance.minValue; }); /// This will be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API. [EditorBrowsable(EditorBrowsableState.Never)] public static readonly BindableProperty CurrentValueProperty = BindableProperty.Create(nameof(CurrentValue), typeof(float), typeof(Progress), default(float), propertyChanged: (bindable, oldValue, newValue) => { var instance = (Progress)bindable; if (newValue != null) { if ((float)newValue > instance.maxValue || (float)newValue < instance.minValue) { return; } instance.currentValue = (float)newValue; instance.UpdateValue(); } }, defaultValueCreator: (bindable) => { var instance = (Progress)bindable; return instance.currentValue; }); /// This will be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API. [EditorBrowsable(EditorBrowsableState.Never)] public static readonly BindableProperty BufferValueProperty = BindableProperty.Create(nameof(BufferValue), typeof(float), typeof(Progress), default(float), propertyChanged: (bindable, oldValue, newValue) => { var instance = (Progress)bindable; if (newValue != null) { if ((float)newValue > instance.maxValue || (float)newValue < instance.minValue) { return; } instance.bufferValue = (float)newValue; instance.UpdateValue(); } }, defaultValueCreator: (bindable) => { var instance = (Progress)bindable; return instance.bufferValue; }); /// This will be public opened in tizen_6.0 after ACR done. Before ACR, need to be hidden as inhouse API. [EditorBrowsable(EditorBrowsableState.Never)] public static readonly BindableProperty ProgressStateProperty = BindableProperty.Create(nameof(ProgressState), typeof(ProgressStatusType), typeof(Progress), ProgressStatusType.Indeterminate, propertyChanged: (bindable, oldValue, newValue) => { var instance = (Progress)bindable; if (newValue != null) { instance.state = (ProgressStatusType)newValue; instance.UpdateStates(); } }, defaultValueCreator: (bindable) => { var instance = (Progress)bindable; return instance.state; }); /// This needs to be considered more if public-open is necessary. private ProgressStatusType state = ProgressStatusType.Determinate; private const float round = 0.5f; private ImageView trackImage = null; private ImageView progressImage = null; private ImageView bufferImage = null; private float maxValue = 100; private float minValue = 0; private float currentValue = 0; private float bufferValue = 0; private ProgressStyle progressStyle => ViewStyle as ProgressStyle; static Progress() { } /// /// The constructor of Progress /// /// 6 public Progress() : base() { Initialize(); } /// /// The constructor of the Progress class with specific style. /// /// style name /// 8 public Progress(string style) : base(style) { Initialize(); } /// /// The constructor of the Progress class with specific style. /// /// The style object to initialize the Progress. /// 8 public Progress(ProgressStyle progressStyle) : base(progressStyle) { Initialize(); } /// /// The status type of the Progress. /// /// 6 public enum ProgressStatusType { /// /// Show BufferImage /// /// 6 Buffering, /// /// Show ProgressImage and BufferImage /// /// 6 Determinate, /// /// Show TrackImage /// /// 6 Indeterminate } /// /// Get style of progress. /// /// 8 public new ProgressStyle Style { get { var result = new ProgressStyle(progressStyle); result.CopyPropertiesFromView(this); result.Track.CopyPropertiesFromView(trackImage); result.Progress.CopyPropertiesFromView(progressImage); result.Buffer.CopyPropertiesFromView(bufferImage); return result; } } /// /// The property to get/set Track image object URL of the Progress. /// /// 6 public string TrackImageURL { get { return progressStyle?.Track?.ResourceUrl?.All; } set { if (null != progressStyle?.Track) { progressStyle.Track.ResourceUrl = value; } } } /// /// The property to get/set Progress object image URL of the Progress. /// /// 6 public string ProgressImageURL { get { return progressStyle?.Progress?.ResourceUrl?.All; } set { if (null != progressStyle?.Progress) { progressStyle.Progress.ResourceUrl = value; } } } /// /// The property to get/set Buffer object image resource URL of the Progress. /// /// 6 public string BufferImageURL { get { return progressStyle?.Buffer?.ResourceUrl?.All; } set { if (null != progressStyle?.Buffer) { progressStyle.Buffer.ResourceUrl = value; RelayoutRequest(); } } } /// /// The property to get/set Track object color of the Progress. /// /// 6 public Color TrackColor { get { return progressStyle?.Track?.BackgroundColor?.All; } set { if (null != progressStyle?.Track) { progressStyle.Track.BackgroundColor = value; } } } /// /// The property to get/set Progress object color of the Progress. /// /// 6 public Color ProgressColor { get { return progressStyle?.Progress?.BackgroundColor?.All; } set { if (null != progressStyle?.Progress) { progressStyle.Progress.BackgroundColor = value; } } } /// /// The property to get/set Buffer object color of the Progress. /// /// 6 public Color BufferColor { get { return progressStyle?.Buffer?.BackgroundColor?.All; } set { if (null != progressStyle?.Buffer) { progressStyle.Buffer.BackgroundColor = value; } } } /// /// The property to get/set the maximum value of the Progress. /// /// 6 public float MaxValue { get { return (float)GetValue(MaxValueProperty); } set { SetValue(MaxValueProperty, value); } } /// /// The property to get/set the minim value of the Progress. /// /// 6 public float MinValue { get { return (float)GetValue(MinValueProperty); } set { SetValue(MinValueProperty, value); } } /// /// The property to get/set the current value of the Progress. /// /// 6 public float CurrentValue { get { return (float)GetValue(CurrentValueProperty); } set { SetValue(CurrentValueProperty, value); } } /// /// The property to get/set the buffer value of the Progress. /// /// 6 public float BufferValue { get { return (float)GetValue(BufferValueProperty); } set { SetValue(BufferValueProperty, value); } } /// /// Gets or sets state of progress. /// /// 6 public ProgressStatusType ProgressState { get { return (ProgressStatusType)GetValue(ProgressStateProperty); } set { SetValue(ProgressStateProperty, value); } } /// /// Dispose Progress and all children on it. /// /// Dispose type. /// 6 protected override void Dispose(DisposeTypes type) { if (disposed) { return; } if (type == DisposeTypes.Explicit) { //Called by User //Release your own managed resources here. //You should release all of your own disposable objects here. Utility.Dispose(trackImage); Utility.Dispose(progressImage); Utility.Dispose(bufferImage); } //You must call base.Dispose(type) just before exit. base.Dispose(type); } /// /// Theme change callback when theme is changed, this callback will be trigger. /// /// The sender /// The event data [EditorBrowsable(EditorBrowsableState.Never)] protected override void OnThemeChangedEvent(object sender, StyleManager.ThemeChangeEventArgs e) { ProgressStyle tempStyle = StyleManager.Instance.GetViewStyle(StyleName) as ProgressStyle; if (null != tempStyle) { progressStyle.CopyFrom(tempStyle); RelayoutRequest(); } } /// /// Change Image status. It can be override. /// /// 6 /// This needs to be considered more if public-open is necessary. [EditorBrowsable(EditorBrowsableState.Never)] private void UpdateStates() { ChangeImageState(state); } /// /// Update progress value /// /// 6 /// This needs to be considered more if public-open is necessary. [EditorBrowsable(EditorBrowsableState.Never)] private void UpdateValue() { if (null == trackImage || null == progressImage) { return; } if (minValue >= maxValue || currentValue < minValue || currentValue > maxValue) { return; } float width = this.SizeWidth; float height = this.SizeHeight; float progressRatio = (float)(currentValue - minValue) / (float)(maxValue - minValue); float progressWidth = width * progressRatio; progressImage.Size2D = new Size2D((int)(progressWidth + round), (int)height); //Add const round to reach Math.Round function. if (null != bufferImage) { if (bufferValue < minValue || bufferValue > maxValue) { return; } float bufferRatio = (float)(bufferValue - minValue) / (float)(maxValue - minValue); float bufferWidth = width * bufferRatio; bufferImage.Size2D = new Size2D((int)(bufferWidth + round), (int)height); //Add const round to reach Math.Round function. } } /// /// Get Progress style. /// /// The default progress style. /// 8 protected override ViewStyle CreateViewStyle() { return new ProgressStyle(); } /// /// Change Image status /// /// 6 /// New status type protected void ChangeImageState(ProgressStatusType statusType) { if (state == ProgressStatusType.Buffering) { bufferImage.Show(); progressImage.Hide(); } else if (state == ProgressStatusType.Determinate) { bufferImage.Show(); progressImage.Show(); UpdateValue(); } else { bufferImage.Hide(); progressImage.Hide(); } } private void Initialize() { // create necessary components InitializeTrack(); InitializeBuffer(); InitializeProgress(); } private void InitializeTrack() { if (null == trackImage) { trackImage = new ImageView { WidthResizePolicy = ResizePolicyType.FillToParent, HeightResizePolicy = ResizePolicyType.FillToParent, PositionUsesPivotPoint = true, ParentOrigin = NUI.ParentOrigin.TopLeft, PivotPoint = NUI.PivotPoint.TopLeft }; Add(trackImage); trackImage.ApplyStyle(progressStyle.Track); } } private void InitializeProgress() { if (null == progressImage) { progressImage = new ImageView { WidthResizePolicy = ResizePolicyType.FillToParent, HeightResizePolicy = ResizePolicyType.FillToParent, PositionUsesPivotPoint = true, ParentOrigin = Tizen.NUI.ParentOrigin.TopLeft, PivotPoint = Tizen.NUI.PivotPoint.TopLeft }; Add(progressImage); progressImage.ApplyStyle(progressStyle.Progress); } } private void InitializeBuffer() { if (null == bufferImage) { bufferImage = new ImageView { WidthResizePolicy = ResizePolicyType.FillToParent, HeightResizePolicy = ResizePolicyType.FillToParent, PositionUsesPivotPoint = true, ParentOrigin = Tizen.NUI.ParentOrigin.TopLeft, PivotPoint = Tizen.NUI.PivotPoint.TopLeft }; Add(bufferImage); bufferImage.ApplyStyle(progressStyle.Buffer); } } } }