diff --git a/Xamarin.Forms.Controls/CoreGalleryPages/ProgressBarCoreGalleryPage.cs b/Xamarin.Forms.Controls/CoreGalleryPages/ProgressBarCoreGalleryPage.cs index 1cc51ae8f80..1d653486137 100644 --- a/Xamarin.Forms.Controls/CoreGalleryPages/ProgressBarCoreGalleryPage.cs +++ b/Xamarin.Forms.Controls/CoreGalleryPages/ProgressBarCoreGalleryPage.cs @@ -20,8 +20,11 @@ protected override void Build (StackLayout stackLayout) { base.Build (stackLayout); - var progressContainer = new ViewContainer (Test.ProgressBar.Progress, new ProgressBar { Progress = 0.5 }); + var progressContainer = new ViewContainer(Test.ProgressBar.Progress, new ProgressBar { Progress = 0.5 }); + var colorContainer = new ViewContainer(Test.ProgressBar.ProgressColor, new ProgressBar { ProgressColor = Color.Lime, Progress = 0.5 }); + Add (progressContainer); + Add(colorContainer); } } } \ No newline at end of file diff --git a/Xamarin.Forms.Core.UnitTests/NotifiedPropertiesTests.cs b/Xamarin.Forms.Core.UnitTests/NotifiedPropertiesTests.cs index 2fc64204fa5..bd47c0be6ce 100644 --- a/Xamarin.Forms.Core.UnitTests/NotifiedPropertiesTests.cs +++ b/Xamarin.Forms.Core.UnitTests/NotifiedPropertiesTests.cs @@ -124,6 +124,7 @@ public override INotifyPropertyChanged CreateView () new PropertyTestCase ("Title", v=>v.Title, (v, o) =>v.Title = o, () => null, "FooBar"), new PropertyTestCase ("SelectedIndex", v=>v.SelectedIndex, (v, o) =>v.SelectedIndex = o, () => -1, 2, ()=>new Picker{Items= {"Foo", "Bar", "Baz", "Qux"}}), new PropertyTestCase ("Progress", v => v.Progress, (v, o) => v.Progress = o, () => 0, .5), + new PropertyTestCase ("ProgressColor", v => v.ProgressColor, (v, o) => v.ProgressColor = o, () => Color.Default, new Color (0, 1, 0)), new PropertyTestCase ("Placeholder", v => v.Placeholder, (v, o) => v.Placeholder = o, () => null, "Foo"), new PropertyTestCase ("Text", v => v.Text, (v, o) => v.Text = o, () => null, "Foo"), new PropertyTestCase ("Minimum", v => v.Minimum, (v, o) => v.Minimum = o, () => 0, .5), diff --git a/Xamarin.Forms.Core/ProgressBar.cs b/Xamarin.Forms.Core/ProgressBar.cs index ffeb5ab2856..69cbe79e4da 100644 --- a/Xamarin.Forms.Core/ProgressBar.cs +++ b/Xamarin.Forms.Core/ProgressBar.cs @@ -8,7 +8,9 @@ namespace Xamarin.Forms [RenderWith(typeof(_ProgressBarRenderer))] public class ProgressBar : View, IElementConfiguration { - public static readonly BindableProperty ProgressProperty = BindableProperty.Create("Progress", typeof(double), typeof(ProgressBar), 0d, coerceValue: (bo, v) => ((double)v).Clamp(0, 1)); + public static readonly BindableProperty ProgressColorProperty = BindableProperty.Create(nameof(ProgressColor), typeof(Color), typeof(ProgressBar), Color.Default); + + public static readonly BindableProperty ProgressProperty = BindableProperty.Create(nameof(Progress), typeof(double), typeof(ProgressBar), 0d, coerceValue: (bo, v) => ((double)v).Clamp(0, 1)); readonly Lazy> _platformConfigurationRegistry; @@ -17,6 +19,12 @@ public ProgressBar() _platformConfigurationRegistry = new Lazy>(() => new PlatformConfigurationRegistry(this)); } + public Color ProgressColor + { + get { return (Color)GetValue(ProgressColorProperty); } + set { SetValue(ProgressColorProperty, value); } + } + public double Progress { get { return (double)GetValue(ProgressProperty); } diff --git a/Xamarin.Forms.CustomAttributes/TestAttributes.cs b/Xamarin.Forms.CustomAttributes/TestAttributes.cs index 595e4638f35..b65209a1b9c 100644 --- a/Xamarin.Forms.CustomAttributes/TestAttributes.cs +++ b/Xamarin.Forms.CustomAttributes/TestAttributes.cs @@ -609,7 +609,8 @@ public enum OpenGlView { } public enum ProgressBar { - Progress + Progress, + ProgressColor } public enum RelativeLayout { diff --git a/Xamarin.Forms.Platform.Android/Renderers/ProgressBarRenderer.cs b/Xamarin.Forms.Platform.Android/Renderers/ProgressBarRenderer.cs index f0b5b006979..d4a9140e725 100644 --- a/Xamarin.Forms.Platform.Android/Renderers/ProgressBarRenderer.cs +++ b/Xamarin.Forms.Platform.Android/Renderers/ProgressBarRenderer.cs @@ -1,6 +1,9 @@ using System; using System.ComponentModel; using Android.Content; +using Android.Content.Res; +using Android.Graphics; +using Android.OS; using AProgressBar = Android.Widget.ProgressBar; namespace Xamarin.Forms.Platform.Android @@ -35,6 +38,8 @@ protected override void OnElementChanged(ElementChangedEventArgs e) SetNativeControl(progressBar); } + + UpdateProgressColor(); UpdateProgress(); } } @@ -45,6 +50,38 @@ protected override void OnElementPropertyChanged(object sender, PropertyChangedE if (e.PropertyName == ProgressBar.ProgressProperty.PropertyName) UpdateProgress(); + else if (e.PropertyName == ProgressBar.ProgressColorProperty.PropertyName) + UpdateProgressColor(); + } + + void UpdateProgressColor() + { + if (Element == null || Control == null) + return; + + Color color = Element.ProgressColor; + + if (color.IsDefault) + { + (Control.Indeterminate ? Control.IndeterminateDrawable : + Control.ProgressDrawable).ClearColorFilter(); + } + else + { + if (Build.VERSION.SdkInt < BuildVersionCodes.Lollipop) + { + (Control.Indeterminate ? Control.IndeterminateDrawable : + Control.ProgressDrawable).SetColorFilter(color.ToAndroid(), PorterDuff.Mode.SrcIn); + } + else + { + var tintList = ColorStateList.ValueOf(color.ToAndroid()); + if (Control.Indeterminate) + Control.IndeterminateTintList = tintList; + else + Control.ProgressTintList = tintList; + } + } } void UpdateProgress() diff --git a/Xamarin.Forms.Platform.MacOS/Renderers/ProgressBarRenderer.cs b/Xamarin.Forms.Platform.MacOS/Renderers/ProgressBarRenderer.cs index 97718981744..159b115bbca 100644 --- a/Xamarin.Forms.Platform.MacOS/Renderers/ProgressBarRenderer.cs +++ b/Xamarin.Forms.Platform.MacOS/Renderers/ProgressBarRenderer.cs @@ -21,6 +21,7 @@ protected override void OnElementChanged(ElementChangedEventArgs e) MinValue = 0, MaxValue = 1 }); + UpdateProgressColor(); UpdateProgress(); } @@ -28,22 +29,34 @@ protected override void OnElementPropertyChanged(object sender, PropertyChangedE { base.OnElementPropertyChanged(sender, e); - if (e.PropertyName == ProgressBar.ProgressProperty.PropertyName) + if (e.PropertyName == ProgressBar.ProgressColorProperty.PropertyName) + UpdateProgressColor(); + else if (e.PropertyName == ProgressBar.ProgressProperty.PropertyName) UpdateProgress(); } + void UpdateProgressColor() + { + SetBackgroundColor(Element.ProgressColor); + } + protected override void SetBackgroundColor(Color color) { if (Control == null) return; - if (s_currentColorFilter == null && color.IsDefault) - return; - if (color.IsDefault) - Control.ContentFilters = new CIFilter[0]; + { + if (s_currentColorFilter != null && Element.BackgroundColor.IsDefault && Element.ProgressColor.IsDefault) + { + Control.ContentFilters = new CIFilter[0]; + s_currentColor = null; + } + + return; + } - var newColor = Element.BackgroundColor.ToNSColor(); + var newColor = color.ToNSColor(); if (Equals(s_currentColor, newColor)) return; diff --git a/Xamarin.Forms.Platform.Tizen/Renderers/ProgressBarRenderer.cs b/Xamarin.Forms.Platform.Tizen/Renderers/ProgressBarRenderer.cs index 30d37224baa..f67de8a7668 100644 --- a/Xamarin.Forms.Platform.Tizen/Renderers/ProgressBarRenderer.cs +++ b/Xamarin.Forms.Platform.Tizen/Renderers/ProgressBarRenderer.cs @@ -3,11 +3,19 @@ using SpecificVE = Xamarin.Forms.PlatformConfiguration.TizenSpecific.VisualElement; using Specific = Xamarin.Forms.PlatformConfiguration.TizenSpecific.ProgressBar; using EProgressBar = ElmSharp.ProgressBar; +using EColor = ElmSharp.Color; namespace Xamarin.Forms.Platform.Tizen { public class ProgressBarRenderer : ViewRenderer { + static readonly EColor s_defaultColor = new EColor(129, 198, 255); + + public ProgressBarRenderer() + { + RegisterPropertyHandler(ProgressBar.ProgressColorProperty, UpdateProgressColor); + } + protected override void OnElementChanged(ElementChangedEventArgs e) { if (Control == null) @@ -60,6 +68,14 @@ void UpdateAll() } } + void UpdateProgressColor(bool initialize) + { + if (initialize && Element.ProgressColor.IsDefault) + return; + + Control.Color = Element.ProgressColor == Color.Default ? s_defaultColor : Element.ProgressColor.ToNative(); + } + void UpdateProgress() { Control.Value = Element.Progress; diff --git a/Xamarin.Forms.Platform.UAP/ProgressBarRenderer.cs b/Xamarin.Forms.Platform.UAP/ProgressBarRenderer.cs index 354b0dc7607..925149590e5 100644 --- a/Xamarin.Forms.Platform.UAP/ProgressBarRenderer.cs +++ b/Xamarin.Forms.Platform.UAP/ProgressBarRenderer.cs @@ -1,4 +1,5 @@ using System.ComponentModel; +using Windows.UI.Xaml; using Windows.UI.Xaml.Controls.Primitives; using Xamarin.Forms.Internals; @@ -6,6 +7,8 @@ namespace Xamarin.Forms.Platform.UWP { public class ProgressBarRenderer : ViewRenderer { + object _foregroundDefault; + protected override void Dispose(bool disposing) { if (disposing) @@ -13,6 +16,7 @@ protected override void Dispose(bool disposing) if (Control != null) { Control.ValueChanged -= ProgressBarOnValueChanged; + Control.Loaded -= OnControlLoaded; } } @@ -23,6 +27,14 @@ protected override void OnElementChanged(ElementChangedEventArgs e) { base.OnElementChanged(e); + if (e.OldElement != null) + { + if (Control != null) + { + Control.Loaded -= OnControlLoaded; + } + } + if (e.NewElement != null) { if (Control == null) @@ -32,6 +44,8 @@ protected override void OnElementChanged(ElementChangedEventArgs e) progressBar.ValueChanged += ProgressBarOnValueChanged; SetNativeControl(progressBar); + + Control.Loaded += OnControlLoaded; } Control.Value = e.NewElement.Progress; @@ -47,6 +61,28 @@ protected override void OnElementPropertyChanged(object sender, PropertyChangedE Control.Value = Element.Progress; else if (e.PropertyName == VisualElement.FlowDirectionProperty.PropertyName) UpdateFlowDirection(); + else if (e.PropertyName == ProgressBar.ProgressColorProperty.PropertyName) + UpdateProgressColor(); + } + + void OnControlLoaded(object sender, RoutedEventArgs routedEventArgs) + { + _foregroundDefault = Control.GetForegroundCache(); + UpdateProgressColor(); + } + + void UpdateProgressColor() + { + Color color = Element.ProgressColor; + + if (color.IsDefault) + { + Control.RestoreForegroundCache(_foregroundDefault); + } + else + { + Control.Foreground = color.ToBrush(); + } } void ProgressBarOnValueChanged(object sender, RangeBaseValueChangedEventArgs rangeBaseValueChangedEventArgs) diff --git a/Xamarin.Forms.Platform.WPF/Renderers/ProgressBarRenderer.cs b/Xamarin.Forms.Platform.WPF/Renderers/ProgressBarRenderer.cs index 175867d7dbe..ec3b6a0b18d 100644 --- a/Xamarin.Forms.Platform.WPF/Renderers/ProgressBarRenderer.cs +++ b/Xamarin.Forms.Platform.WPF/Renderers/ProgressBarRenderer.cs @@ -1,11 +1,5 @@ -using System; -using System.Collections.Generic; -using System.ComponentModel; -using System.Linq; -using System.Text; -using System.Threading.Tasks; +using System.ComponentModel; using System.Windows; -using System.Windows.Media; using Xamarin.Forms.Internals; using WProgressBar = System.Windows.Controls.ProgressBar; @@ -19,12 +13,13 @@ protected override void OnElementChanged(ElementChangedEventArgs e) { if (Control == null) // construct and SetNativeControl and suscribe control event { - SetNativeControl(new WProgressBar { Minimum = 0, Maximum = 1, Foreground = Brushes.DeepSkyBlue }); + SetNativeControl(new WProgressBar { Minimum = 0, Maximum = 1 }); Control.ValueChanged += HandleValueChanged; } // Update control property UpdateProgress(); + UpdateProgressColor(); } base.OnElementChanged(e); @@ -36,8 +31,15 @@ protected override void OnElementPropertyChanged(object sender, PropertyChangedE if (e.PropertyName == ProgressBar.ProgressProperty.PropertyName) UpdateProgress(); + else if (e.PropertyName == ProgressBar.ProgressColorProperty.PropertyName) + UpdateProgressColor(); } - + + void UpdateProgressColor() + { + Control.UpdateDependencyColor(WProgressBar.ForegroundProperty, Element.ProgressColor.IsDefault ? Color.DeepSkyBlue : Element.ProgressColor); + } + void UpdateProgress() { Control.Value = Element.Progress; diff --git a/Xamarin.Forms.Platform.iOS/Renderers/ProgressBarRenderer.cs b/Xamarin.Forms.Platform.iOS/Renderers/ProgressBarRenderer.cs index 234f812820b..c5bcdb7553b 100644 --- a/Xamarin.Forms.Platform.iOS/Renderers/ProgressBarRenderer.cs +++ b/Xamarin.Forms.Platform.iOS/Renderers/ProgressBarRenderer.cs @@ -21,6 +21,7 @@ protected override void OnElementChanged(ElementChangedEventArgs e) if (Control == null) SetNativeControl(new UIProgressView(UIProgressViewStyle.Default)); + UpdateProgressColor(); UpdateProgress(); } @@ -31,7 +32,9 @@ protected override void OnElementPropertyChanged(object sender, PropertyChangedE { base.OnElementPropertyChanged(sender, e); - if (e.PropertyName == ProgressBar.ProgressProperty.PropertyName) + if (e.PropertyName == ProgressBar.ProgressColorProperty.PropertyName) + UpdateProgressColor(); + else if (e.PropertyName == ProgressBar.ProgressProperty.PropertyName) UpdateProgress(); } @@ -45,6 +48,11 @@ protected override void SetBackgroundColor(Color color) Control.TrackTintColor = color != Color.Default ? color.ToUIColor() : null; } + void UpdateProgressColor() + { + Control.ProgressTintColor = Element.ProgressColor == Color.Default ? null : Element.ProgressColor.ToUIColor(); + } + void UpdateProgress() { Control.Progress = (float)Element.Progress; diff --git a/docs/Xamarin.Forms.Core/Xamarin.Forms/ProgressBar.xml b/docs/Xamarin.Forms.Core/Xamarin.Forms/ProgressBar.xml index e4c3e4a1e2c..e84c52b98a1 100644 --- a/docs/Xamarin.Forms.Core/Xamarin.Forms/ProgressBar.xml +++ b/docs/Xamarin.Forms.Core/Xamarin.Forms/ProgressBar.xml @@ -51,6 +51,12 @@ Debug.WriteLine ("Animation completed"); Property Value + + Color + + A color specification, with or without the prefix, "Color". For example, "Color.Red" and "Red" both specify the color red. + + Progress @@ -128,6 +134,39 @@ Debug.WriteLine ("Animation completed"); Values less than 0 or larger than 1 will be clamped to the range [0-1]. + + + + Property + + 2.0.0.0 + + + Xamarin.Forms.Color + + + Gets or sets the of the ProgressBar line. This is a bindable property. + A used to display the ProgressBar line. Default is . + + + + + + + + Field + + 2.0.0.0 + + + Xamarin.Forms.BindableProperty + + + Identifies the ProgressColor bindable property. + + + +