Skip to content
This repository has been archived by the owner on May 1, 2024. It is now read-only.

Commit

Permalink
Add ProgressColor on ProgressBar (#1861)
Browse files Browse the repository at this point in the history
  • Loading branch information
kiok85 authored and rmarinho committed Feb 15, 2018
1 parent 1233e05 commit 58d56d0
Show file tree
Hide file tree
Showing 11 changed files with 183 additions and 19 deletions.
Expand Up @@ -20,8 +20,11 @@ protected override void Build (StackLayout stackLayout)
{
base.Build (stackLayout);

var progressContainer = new ViewContainer<ProgressBar> (Test.ProgressBar.Progress, new ProgressBar { Progress = 0.5 });
var progressContainer = new ViewContainer<ProgressBar>(Test.ProgressBar.Progress, new ProgressBar { Progress = 0.5 });
var colorContainer = new ViewContainer<ProgressBar>(Test.ProgressBar.ProgressColor, new ProgressBar { ProgressColor = Color.Lime, Progress = 0.5 });

Add (progressContainer);
Add(colorContainer);
}
}
}
1 change: 1 addition & 0 deletions Xamarin.Forms.Core.UnitTests/NotifiedPropertiesTests.cs
Expand Up @@ -124,6 +124,7 @@ public override INotifyPropertyChanged CreateView ()
new PropertyTestCase<Picker, string> ("Title", v=>v.Title, (v, o) =>v.Title = o, () => null, "FooBar"),
new PropertyTestCase<Picker, int> ("SelectedIndex", v=>v.SelectedIndex, (v, o) =>v.SelectedIndex = o, () => -1, 2, ()=>new Picker{Items= {"Foo", "Bar", "Baz", "Qux"}}),
new PropertyTestCase<ProgressBar, double> ("Progress", v => v.Progress, (v, o) => v.Progress = o, () => 0, .5),
new PropertyTestCase<ProgressBar, Color> ("ProgressColor", v => v.ProgressColor, (v, o) => v.ProgressColor = o, () => Color.Default, new Color (0, 1, 0)),
new PropertyTestCase<SearchBar, string> ("Placeholder", v => v.Placeholder, (v, o) => v.Placeholder = o, () => null, "Foo"),
new PropertyTestCase<SearchBar, string> ("Text", v => v.Text, (v, o) => v.Text = o, () => null, "Foo"),
new PropertyTestCase<Slider, double> ("Minimum", v => v.Minimum, (v, o) => v.Minimum = o, () => 0, .5),
Expand Down
10 changes: 9 additions & 1 deletion Xamarin.Forms.Core/ProgressBar.cs
Expand Up @@ -8,7 +8,9 @@ namespace Xamarin.Forms
[RenderWith(typeof(_ProgressBarRenderer))]
public class ProgressBar : View, IElementConfiguration<ProgressBar>
{
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<ProgressBar>> _platformConfigurationRegistry;

Expand All @@ -17,6 +19,12 @@ public ProgressBar()
_platformConfigurationRegistry = new Lazy<PlatformConfigurationRegistry<ProgressBar>>(() => new PlatformConfigurationRegistry<ProgressBar>(this));
}

public Color ProgressColor
{
get { return (Color)GetValue(ProgressColorProperty); }
set { SetValue(ProgressColorProperty, value); }
}

public double Progress
{
get { return (double)GetValue(ProgressProperty); }
Expand Down
3 changes: 2 additions & 1 deletion Xamarin.Forms.CustomAttributes/TestAttributes.cs
Expand Up @@ -609,7 +609,8 @@ public enum OpenGlView {
}

public enum ProgressBar {
Progress
Progress,
ProgressColor
}

public enum RelativeLayout {
Expand Down
37 changes: 37 additions & 0 deletions 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
Expand Down Expand Up @@ -35,6 +38,8 @@ protected override void OnElementChanged(ElementChangedEventArgs<ProgressBar> e)

SetNativeControl(progressBar);
}

UpdateProgressColor();
UpdateProgress();
}
}
Expand All @@ -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()
Expand Down
25 changes: 19 additions & 6 deletions Xamarin.Forms.Platform.MacOS/Renderers/ProgressBarRenderer.cs
Expand Up @@ -21,29 +21,42 @@ protected override void OnElementChanged(ElementChangedEventArgs<ProgressBar> e)
MinValue = 0,
MaxValue = 1
});
UpdateProgressColor();
UpdateProgress();
}

protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
{
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;

Expand Down
16 changes: 16 additions & 0 deletions Xamarin.Forms.Platform.Tizen/Renderers/ProgressBarRenderer.cs
Expand Up @@ -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<ProgressBar, EProgressBar>
{
static readonly EColor s_defaultColor = new EColor(129, 198, 255);

public ProgressBarRenderer()
{
RegisterPropertyHandler(ProgressBar.ProgressColorProperty, UpdateProgressColor);
}

protected override void OnElementChanged(ElementChangedEventArgs<ProgressBar> e)
{
if (Control == null)
Expand Down Expand Up @@ -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;
Expand Down
36 changes: 36 additions & 0 deletions Xamarin.Forms.Platform.UAP/ProgressBarRenderer.cs
@@ -1,18 +1,22 @@
using System.ComponentModel;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls.Primitives;
using Xamarin.Forms.Internals;

namespace Xamarin.Forms.Platform.UWP
{
public class ProgressBarRenderer : ViewRenderer<ProgressBar, Windows.UI.Xaml.Controls.ProgressBar>
{
object _foregroundDefault;

protected override void Dispose(bool disposing)
{
if (disposing)
{
if (Control != null)
{
Control.ValueChanged -= ProgressBarOnValueChanged;
Control.Loaded -= OnControlLoaded;
}
}

Expand All @@ -23,6 +27,14 @@ protected override void OnElementChanged(ElementChangedEventArgs<ProgressBar> e)
{
base.OnElementChanged(e);

if (e.OldElement != null)
{
if (Control != null)
{
Control.Loaded -= OnControlLoaded;
}
}

if (e.NewElement != null)
{
if (Control == null)
Expand All @@ -32,6 +44,8 @@ protected override void OnElementChanged(ElementChangedEventArgs<ProgressBar> e)
progressBar.ValueChanged += ProgressBarOnValueChanged;

SetNativeControl(progressBar);

Control.Loaded += OnControlLoaded;
}

Control.Value = e.NewElement.Progress;
Expand All @@ -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)
Expand Down
20 changes: 11 additions & 9 deletions 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;

Expand All @@ -19,12 +13,13 @@ protected override void OnElementChanged(ElementChangedEventArgs<ProgressBar> 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);
Expand All @@ -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;
Expand Down
10 changes: 9 additions & 1 deletion Xamarin.Forms.Platform.iOS/Renderers/ProgressBarRenderer.cs
Expand Up @@ -21,6 +21,7 @@ protected override void OnElementChanged(ElementChangedEventArgs<ProgressBar> e)
if (Control == null)
SetNativeControl(new UIProgressView(UIProgressViewStyle.Default));

UpdateProgressColor();
UpdateProgress();
}

Expand All @@ -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();
}

Expand All @@ -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;
Expand Down

0 comments on commit 58d56d0

Please sign in to comment.