[Core,iOS,Android] Color on Slider (Implementation) (#1781) fixes #1684

* Implementation for #1690

* Updated  default(Color) -> Color.Default

* Added UWP + default

* Revert "Merge branch 'Slider' into master"

This reverts commit 0aa3d01b68, reversing
changes made to 8961c28315.

* Update SliderRenderer.cs

* Updated Doc

* Updated Slider

Removed Comment.
Formated Code
now building fine for android.

* Slider Renderer for iOS

Reuploding

* Reuploading SliderRenderer for UWP

* Checking and enabling  For API >19
This commit is contained in:
Ravinder Jangra 2018-03-07 16:27:59 +05:30 коммит произвёл Rui Marinho
Родитель 4a8f78db4c
Коммит 65f4e078ce
5 изменённых файлов: 403 добавлений и 2 удалений

Просмотреть файл

@ -41,6 +41,14 @@ namespace Xamarin.Forms
eh(slider, new ValueChangedEventArgs((double)oldValue, (double)newValue));
});
public static readonly BindableProperty MinimumTrackColorProperty = BindableProperty.Create(nameof(MinimumTrackColor), typeof(Color), typeof(Slider), Color.Default);
public static readonly BindableProperty MaximumTrackColorProperty = BindableProperty.Create(nameof(MaximumTrackColor), typeof(Color), typeof(Slider), Color.Default);
public static readonly BindableProperty ThumbColorProperty = BindableProperty.Create(nameof(ThumbColor), typeof(Color), typeof(Slider), Color.Default);
public static readonly BindableProperty ThumbImageProperty = BindableProperty.Create(nameof(ThumbImage), typeof(FileImageSource), typeof(Slider), default(FileImageSource));
readonly Lazy<PlatformConfigurationRegistry<Slider>> _platformConfigurationRegistry;
public Slider()
@ -66,6 +74,30 @@ namespace Xamarin.Forms
Value = val.Clamp(min, max);
}
public Color MinimumTrackColor
{
get { return (Color)GetValue(MinimumTrackColorProperty); }
set { SetValue(MinimumTrackColorProperty, value); }
}
public Color MaximumTrackColor
{
get { return (Color)GetValue(MaximumTrackColorProperty); }
set { SetValue(MaximumTrackColorProperty, value); }
}
public Color ThumbColor
{
get { return (Color)GetValue(ThumbColorProperty); }
set { SetValue(ThumbColorProperty, value); }
}
public FileImageSource ThumbImage
{
get { return (FileImageSource)GetValue(ThumbImageProperty); }
set { SetValue(ThumbImageProperty, value); }
}
public double Maximum
{
get { return (double)GetValue(MaximumProperty); }

Просмотреть файл

@ -1,6 +1,8 @@
using System;
using System.ComponentModel;
using Android.Content;
using Android.Content.Res;
using Android.Graphics;
using Android.Graphics.Drawables;
using Android.OS;
using Android.Widget;
@ -11,6 +13,10 @@ namespace Xamarin.Forms.Platform.Android
{
double _max, _min;
bool _isTrackingChange;
ColorStateList defaultprogresstintlist, defaultprogressbackgroundtintlist;
ColorFilter defaultthumbcolorfilter;
Drawable defaultthumb;
PorterDuff.Mode defaultprogresstintmode, defaultprogressbackgroundtintmode;
public SliderRenderer(Context context) : base(context)
{
@ -60,14 +66,32 @@ namespace Xamarin.Forms.Platform.Android
SetNativeControl(seekBar);
seekBar.Max = 1000;
seekBar.SetOnSeekBarChangeListener(this);
if (Build.VERSION.SdkInt > BuildVersionCodes.Kitkat)
{
defaultthumbcolorfilter = seekBar.Thumb.ColorFilter;
defaultprogresstintmode = seekBar.ProgressTintMode;
defaultprogressbackgroundtintmode = seekBar.ProgressBackgroundTintMode;
defaultprogresstintlist = seekBar.ProgressTintList;
defaultprogressbackgroundtintlist = seekBar.ProgressBackgroundTintList;
defaultthumb = seekBar.Thumb;
}
}
Slider slider = e.NewElement;
_min = slider.Minimum;
_max = slider.Maximum;
Value = slider.Value;
if (Build.VERSION.SdkInt > BuildVersionCodes.Kitkat)
{
UpdateSliderColors();
}
}
SeekBar NativeSeekbar
{
get { return Control; }
}
protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
@ -88,6 +112,93 @@ namespace Xamarin.Forms.Platform.Android
Value = view.Value;
break;
}
if (Build.VERSION.SdkInt > BuildVersionCodes.Kitkat)
{
if (e.PropertyName == Slider.MinimumTrackColorProperty.PropertyName)
UpdateMinimumTrackColor();
else if (e.PropertyName == Slider.MaximumTrackColorProperty.PropertyName)
UpdateMaximumTrackColor();
else if (e.PropertyName == Slider.ThumbImageProperty.PropertyName)
UpdateThumbImage();
else if (e.PropertyName == Slider.ThumbColorProperty.PropertyName)
UpdateThumbColor();
}
}
private void UpdateSliderColors()
{
UpdateMinimumTrackColor();
UpdateMaximumTrackColor();
if (!string.IsNullOrEmpty(Element.ThumbImage))
{
UpdateThumbImage();
}
else
{
UpdateThumbColor();
}
}
private void UpdateMinimumTrackColor()
{
if (Element != null)
{
if (Element.MinimumTrackColor == Color.Default)
{
Control.ProgressTintList = defaultprogresstintlist;
Control.ProgressTintMode = defaultprogresstintmode;
}
else
{
Control.ProgressTintList = ColorStateList.ValueOf(Element.MinimumTrackColor.ToAndroid());
Control.ProgressTintMode = PorterDuff.Mode.SrcIn;
}
}
}
private void UpdateMaximumTrackColor()
{
if (Element != null)
{
if (Element.MaximumTrackColor == Color.Default)
{
Control.ProgressBackgroundTintList = defaultprogressbackgroundtintlist;
Control.ProgressBackgroundTintMode = defaultprogressbackgroundtintmode;
}
else
{
Control.ProgressBackgroundTintList = ColorStateList.ValueOf(Element.MaximumTrackColor.ToAndroid());
Control.ProgressBackgroundTintMode = PorterDuff.Mode.SrcIn;
}
}
}
private void UpdateThumbColor()
{
if (Element != null)
{
if (Element.ThumbColor == Color.Default)
{
Control.Thumb.SetColorFilter(defaultthumbcolorfilter);
}
else
{
Control.Thumb.SetColorFilter(Element.ThumbColor.ToAndroid(), PorterDuff.Mode.SrcIn);
}
}
}
private void UpdateThumbImage()
{
if (Element != null)
{
if (string.IsNullOrEmpty(Element.ThumbImage))
Control.SetThumb(defaultthumb);
else
Control.SetThumb(Context.GetDrawable(Element.ThumbImage));
}
}
protected override void OnLayout(bool changed, int l, int t, int r, int b)
@ -111,4 +222,4 @@ namespace Xamarin.Forms.Platform.Android
thumb.SetBounds(thumb.Bounds.Left, thumbTop, thumb.Bounds.Left + thumb.IntrinsicWidth, thumbTop + thumb.IntrinsicHeight);
}
}
}
}

Просмотреть файл

@ -2,11 +2,15 @@
using System.ComponentModel;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Media;
namespace Xamarin.Forms.Platform.UWP
{
public class SliderRenderer : ViewRenderer<Slider, Windows.UI.Xaml.Controls.Slider>
{
Brush defaultforegroundcolor;
Brush defaultbackgroundcolor;
protected override void OnElementChanged(ElementChangedEventArgs<Slider> e)
{
base.OnElementChanged(e);
@ -24,6 +28,9 @@ namespace Xamarin.Forms.Platform.UWP
slider.ValueChanged += OnNativeValueChanged;
defaultforegroundcolor = slider.Foreground;
defaultbackgroundcolor = slider.Background;
// Even when using Center/CenterAndExpand, a Slider has an oddity where it looks
// off-center in its layout by a smidge. The default templates are slightly different
// between 8.1/UWP; the 8.1 rows are 17/Auto/32 and UWP are 18/Auto/18. The value of
@ -45,6 +52,35 @@ namespace Xamarin.Forms.Platform.UWP
Control.StepFrequency = stepping;
Control.SmallChange = stepping;
UpdateFlowDirection();
UpdateSliderColors();
}
}
private void UpdateSliderColors()
{
UpdateMinimumTrackColor();
UpdateMaximumTrackColor();
}
private void UpdateMinimumTrackColor()
{
if (Control != null)
{
if (Element.MinimumTrackColor == Color.Default)
Control.Foreground = defaultforegroundcolor;
else
Control.Foreground = Element.MinimumTrackColor.ToBrush();
}
}
private void UpdateMaximumTrackColor()
{
if (Control != null)
{
if (Element.MaximumTrackColor == Color.Default)
Control.Background = defaultbackgroundcolor;
else
Control.Background = Element.MaximumTrackColor.ToBrush();
}
}
@ -63,6 +99,11 @@ namespace Xamarin.Forms.Platform.UWP
}
else if (e.PropertyName == VisualElement.FlowDirectionProperty.PropertyName)
UpdateFlowDirection();
else if (e.PropertyName == Slider.MinimumTrackColorProperty.PropertyName)
UpdateMinimumTrackColor();
else if (e.PropertyName == Slider.MaximumTrackColorProperty.PropertyName)
UpdateMaximumTrackColor();
}
protected override void UpdateBackgroundColor()

Просмотреть файл

@ -8,6 +8,7 @@ namespace Xamarin.Forms.Platform.iOS
public class SliderRenderer : ViewRenderer<Slider, UISlider>
{
SizeF _fitSize;
UIColor defaultmintrackcolor, defaultmaxtrackcolor, defaultthumbcolor;
public override SizeF SizeThatFits(SizeF size)
{
@ -36,6 +37,10 @@ namespace Xamarin.Forms.Platform.iOS
Control.SizeToFit();
_fitSize = Control.Bounds.Size;
defaultmintrackcolor = Control.MinimumTrackTintColor;
defaultmaxtrackcolor = Control.MaximumTrackTintColor;
defaultthumbcolor = Control.ThumbTintColor;
// except if your not running iOS 7... then it fails...
if (_fitSize.Width <= 0 || _fitSize.Height <= 0)
_fitSize = new SizeF(22, 22); // Per the glorious documentation known as the SDK docs
@ -44,11 +49,91 @@ namespace Xamarin.Forms.Platform.iOS
UpdateMaximum();
UpdateMinimum();
UpdateValue();
UpdateSliderColors();
}
base.OnElementChanged(e);
}
private void UpdateSliderColors()
{
UpdateMinimumTrackColor();
UpdateMaximumTrackColor();
if (!string.IsNullOrEmpty(Element.ThumbImage))
{
UpdateThumbImage();
}
else
{
UpdateThumbColor();
}
}
private void UpdateMinimumTrackColor()
{
if (Element != null)
{
if (Element.MinimumTrackColor == Color.Default)
Control.MinimumTrackTintColor = defaultmintrackcolor;
else
Control.MinimumTrackTintColor = Element.MinimumTrackColor.ToUIColor();
}
}
private void UpdateMaximumTrackColor()
{
if (Element != null)
{
if (Element.MaximumTrackColor == Color.Default)
Control.MaximumTrackTintColor = defaultmaxtrackcolor;
else
Control.MaximumTrackTintColor = Element.MaximumTrackColor.ToUIColor();
}
}
private void UpdateThumbColor()
{
if (Element != null)
{
if (Element.ThumbColor == Color.Default)
Control.ThumbTintColor = defaultthumbcolor;
else
Control.ThumbTintColor = Element.ThumbColor.ToUIColor();
}
}
async void UpdateThumbImage()
{
IImageSourceHandler handler;
FileImageSource source = Element.ThumbImage;
if (source != null && (handler = Internals.Registrar.Registered.GetHandlerForObject<IImageSourceHandler>(source)) != null)
{
UIImage uiimage;
try
{
uiimage = await handler.LoadImageAsync(source, scale: (float)UIScreen.MainScreen.Scale);
}
catch (OperationCanceledException)
{
uiimage = null;
}
UISlider slider = Control;
if (slider != null && uiimage != null)
{
slider.SetThumbImage(uiimage, UIControlState.Normal);
}
}
else
{
UISlider slider = Control;
if (slider != null)
{
slider.SetThumbImage(null, UIControlState.Normal);
}
}
((IVisualElementController)Element).NativeSizeChanged();
}
protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
{
base.OnElementPropertyChanged(sender, e);
@ -59,6 +144,14 @@ namespace Xamarin.Forms.Platform.iOS
UpdateMinimum();
else if (e.PropertyName == Slider.ValueProperty.PropertyName)
UpdateValue();
else if (e.PropertyName == Slider.MinimumTrackColorProperty.PropertyName)
UpdateMinimumTrackColor();
else if (e.PropertyName == Slider.MaximumTrackColorProperty.PropertyName)
UpdateMaximumTrackColor();
else if (e.PropertyName == Slider.ThumbImageProperty.PropertyName)
UpdateThumbImage();
else if (e.PropertyName == Slider.ThumbColorProperty.PropertyName)
UpdateThumbColor();
}
void OnControlValueChanged(object sender, EventArgs eventArgs)

Просмотреть файл

@ -208,6 +208,37 @@ namespace FormsGallery
</remarks>
</Docs>
</Member>
<Member MemberName="MaximumTrackColor">
<MemberSignature Language="C#" Value="public Xamarin.Forms.Color MaximumTrackColor { get; set; }" />
<MemberSignature Language="ILAsm" Value=".property instance valuetype Xamarin.Forms.Color MaximumTrackColor" />
<MemberType>Property</MemberType>
<AssemblyInfo>
<AssemblyVersion>2.0.0.0</AssemblyVersion>
</AssemblyInfo>
<ReturnValue>
<ReturnType>Xamarin.Forms.Color</ReturnType>
</ReturnValue>
<Docs>
<summary>To be added.</summary>
<value>To be added.</value>
<remarks>To be added.</remarks>
</Docs>
</Member>
<Member MemberName="MaximumTrackColorProperty">
<MemberSignature Language="C#" Value="public static readonly Xamarin.Forms.BindableProperty MaximumTrackColorProperty;" />
<MemberSignature Language="ILAsm" Value=".field public static initonly class Xamarin.Forms.BindableProperty MaximumTrackColorProperty" />
<MemberType>Field</MemberType>
<AssemblyInfo>
<AssemblyVersion>2.0.0.0</AssemblyVersion>
</AssemblyInfo>
<ReturnValue>
<ReturnType>Xamarin.Forms.BindableProperty</ReturnType>
</ReturnValue>
<Docs>
<summary>To be added.</summary>
<remarks>To be added.</remarks>
</Docs>
</Member>
<Member MemberName="Minimum">
<MemberSignature Language="C#" Value="public double Minimum { get; set; }" />
<MemberSignature Language="ILAsm" Value=".property instance float64 Minimum" />
@ -253,6 +284,37 @@ namespace FormsGallery
</remarks>
</Docs>
</Member>
<Member MemberName="MinimumTrackColor">
<MemberSignature Language="C#" Value="public Xamarin.Forms.Color MinimumTrackColor { get; set; }" />
<MemberSignature Language="ILAsm" Value=".property instance valuetype Xamarin.Forms.Color MinimumTrackColor" />
<MemberType>Property</MemberType>
<AssemblyInfo>
<AssemblyVersion>2.0.0.0</AssemblyVersion>
</AssemblyInfo>
<ReturnValue>
<ReturnType>Xamarin.Forms.Color</ReturnType>
</ReturnValue>
<Docs>
<summary>To be added.</summary>
<value>To be added.</value>
<remarks>To be added.</remarks>
</Docs>
</Member>
<Member MemberName="MinimumTrackColorProperty">
<MemberSignature Language="C#" Value="public static readonly Xamarin.Forms.BindableProperty MinimumTrackColorProperty;" />
<MemberSignature Language="ILAsm" Value=".field public static initonly class Xamarin.Forms.BindableProperty MinimumTrackColorProperty" />
<MemberType>Field</MemberType>
<AssemblyInfo>
<AssemblyVersion>2.0.0.0</AssemblyVersion>
</AssemblyInfo>
<ReturnValue>
<ReturnType>Xamarin.Forms.BindableProperty</ReturnType>
</ReturnValue>
<Docs>
<summary>To be added.</summary>
<remarks>To be added.</remarks>
</Docs>
</Member>
<Member MemberName="On&lt;T&gt;">
<MemberSignature Language="C#" Value="public Xamarin.Forms.IPlatformElementConfiguration&lt;T,Xamarin.Forms.Slider&gt; On&lt;T&gt; () where T : Xamarin.Forms.IConfigPlatform;" />
<MemberSignature Language="ILAsm" Value=".method public hidebysig newslot virtual instance class Xamarin.Forms.IPlatformElementConfiguration`2&lt;!!T, class Xamarin.Forms.Slider&gt; On&lt;(class Xamarin.Forms.IConfigPlatform) T&gt;() cil managed" />
@ -278,6 +340,68 @@ namespace FormsGallery
<remarks>To be added.</remarks>
</Docs>
</Member>
<Member MemberName="ThumbColor">
<MemberSignature Language="C#" Value="public Xamarin.Forms.Color ThumbColor { get; set; }" />
<MemberSignature Language="ILAsm" Value=".property instance valuetype Xamarin.Forms.Color ThumbColor" />
<MemberType>Property</MemberType>
<AssemblyInfo>
<AssemblyVersion>2.0.0.0</AssemblyVersion>
</AssemblyInfo>
<ReturnValue>
<ReturnType>Xamarin.Forms.Color</ReturnType>
</ReturnValue>
<Docs>
<summary>To be added.</summary>
<value>To be added.</value>
<remarks>To be added.</remarks>
</Docs>
</Member>
<Member MemberName="ThumbColorProperty">
<MemberSignature Language="C#" Value="public static readonly Xamarin.Forms.BindableProperty ThumbColorProperty;" />
<MemberSignature Language="ILAsm" Value=".field public static initonly class Xamarin.Forms.BindableProperty ThumbColorProperty" />
<MemberType>Field</MemberType>
<AssemblyInfo>
<AssemblyVersion>2.0.0.0</AssemblyVersion>
</AssemblyInfo>
<ReturnValue>
<ReturnType>Xamarin.Forms.BindableProperty</ReturnType>
</ReturnValue>
<Docs>
<summary>To be added.</summary>
<remarks>To be added.</remarks>
</Docs>
</Member>
<Member MemberName="ThumbImage">
<MemberSignature Language="C#" Value="public Xamarin.Forms.FileImageSource ThumbImage { get; set; }" />
<MemberSignature Language="ILAsm" Value=".property instance class Xamarin.Forms.FileImageSource ThumbImage" />
<MemberType>Property</MemberType>
<AssemblyInfo>
<AssemblyVersion>2.0.0.0</AssemblyVersion>
</AssemblyInfo>
<ReturnValue>
<ReturnType>Xamarin.Forms.FileImageSource</ReturnType>
</ReturnValue>
<Docs>
<summary>To be added.</summary>
<value>To be added.</value>
<remarks>To be added.</remarks>
</Docs>
</Member>
<Member MemberName="ThumbImageProperty">
<MemberSignature Language="C#" Value="public static readonly Xamarin.Forms.BindableProperty ThumbImageProperty;" />
<MemberSignature Language="ILAsm" Value=".field public static initonly class Xamarin.Forms.BindableProperty ThumbImageProperty" />
<MemberType>Field</MemberType>
<AssemblyInfo>
<AssemblyVersion>2.0.0.0</AssemblyVersion>
</AssemblyInfo>
<ReturnValue>
<ReturnType>Xamarin.Forms.BindableProperty</ReturnType>
</ReturnValue>
<Docs>
<summary>To be added.</summary>
<remarks>To be added.</remarks>
</Docs>
</Member>
<Member MemberName="Value">
<MemberSignature Language="C#" Value="public double Value { get; set; }" />
<MemberSignature Language="ILAsm" Value=".property instance float64 Value" />