2018-11-04 01:45:28 +03:00
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.ComponentModel;
|
|
|
|
using System.Reflection;
|
|
|
|
using System.Windows.Input;
|
|
|
|
using Xamarin.Forms.Internals;
|
|
|
|
using Xamarin.Forms.Platform;
|
|
|
|
|
|
|
|
namespace Xamarin.Forms
|
|
|
|
{
|
|
|
|
[RenderWith(typeof(_ImageButtonRenderer))]
|
2018-11-10 22:25:01 +03:00
|
|
|
public class ImageButton : View, IImageController, IElementConfiguration<ImageButton>, IBorderElement, IButtonController, IViewController, IPaddingElement, IButtonElement, IImageElement
|
2018-11-04 01:45:28 +03:00
|
|
|
{
|
|
|
|
const int DefaultCornerRadius = -1;
|
|
|
|
|
2018-11-10 22:25:01 +03:00
|
|
|
public static readonly BindableProperty CommandProperty = ButtonElement.CommandProperty;
|
2018-11-04 01:45:28 +03:00
|
|
|
|
2018-11-10 22:25:01 +03:00
|
|
|
public static readonly BindableProperty CommandParameterProperty = ButtonElement.CommandParameterProperty;
|
2018-11-04 01:45:28 +03:00
|
|
|
|
2018-11-10 22:25:01 +03:00
|
|
|
public static readonly BindableProperty CornerRadiusProperty = BorderElement.CornerRadiusProperty;
|
2018-11-04 01:45:28 +03:00
|
|
|
|
2018-11-10 22:25:01 +03:00
|
|
|
public static readonly BindableProperty BorderWidthProperty = BorderElement.BorderWidthProperty;
|
2018-11-04 01:45:28 +03:00
|
|
|
|
|
|
|
public static readonly BindableProperty BorderColorProperty = BorderElement.BorderColorProperty;
|
|
|
|
|
2018-11-10 22:25:01 +03:00
|
|
|
public static readonly BindableProperty SourceProperty = ImageElement.SourceProperty;
|
2018-11-04 01:45:28 +03:00
|
|
|
|
2018-11-10 22:25:01 +03:00
|
|
|
public static readonly BindableProperty AspectProperty = ImageElement.AspectProperty;
|
2018-11-04 01:45:28 +03:00
|
|
|
|
2018-11-10 22:25:01 +03:00
|
|
|
public static readonly BindableProperty IsOpaqueProperty = ImageElement.IsOpaqueProperty;
|
2018-11-04 01:45:28 +03:00
|
|
|
|
|
|
|
internal static readonly BindablePropertyKey IsLoadingPropertyKey = BindableProperty.CreateReadOnly(nameof(IsLoading), typeof(bool), typeof(ImageButton), default(bool));
|
|
|
|
|
|
|
|
public static readonly BindableProperty IsLoadingProperty = IsLoadingPropertyKey.BindableProperty;
|
|
|
|
|
|
|
|
internal static readonly BindablePropertyKey IsPressedPropertyKey = BindableProperty.CreateReadOnly(nameof(IsPressed), typeof(bool), typeof(ImageButton), default(bool));
|
|
|
|
|
|
|
|
public static readonly BindableProperty IsPressedProperty = IsPressedPropertyKey.BindableProperty;
|
|
|
|
|
|
|
|
public static readonly BindableProperty PaddingProperty = PaddingElement.PaddingProperty;
|
|
|
|
public event EventHandler Clicked;
|
|
|
|
public event EventHandler Pressed;
|
|
|
|
public event EventHandler Released;
|
|
|
|
|
|
|
|
readonly Lazy<PlatformConfigurationRegistry<ImageButton>> _platformConfigurationRegistry;
|
|
|
|
|
|
|
|
|
|
|
|
public ImageButton()
|
|
|
|
{
|
|
|
|
_platformConfigurationRegistry = new Lazy<PlatformConfigurationRegistry<ImageButton>>(() => new PlatformConfigurationRegistry<ImageButton>(this));
|
|
|
|
}
|
|
|
|
|
|
|
|
public Color BorderColor
|
|
|
|
{
|
|
|
|
get { return (Color)GetValue(BorderElement.BorderColorProperty); }
|
|
|
|
set { SetValue(BorderElement.BorderColorProperty, value); }
|
|
|
|
}
|
|
|
|
|
|
|
|
public int CornerRadius
|
|
|
|
{
|
|
|
|
get { return (int)GetValue(CornerRadiusProperty); }
|
|
|
|
set { SetValue(CornerRadiusProperty, value); }
|
|
|
|
}
|
|
|
|
|
|
|
|
public double BorderWidth
|
|
|
|
{
|
|
|
|
get { return (double)GetValue(BorderWidthProperty); }
|
|
|
|
set { SetValue(BorderWidthProperty, value); }
|
|
|
|
}
|
|
|
|
|
|
|
|
public Aspect Aspect
|
|
|
|
{
|
|
|
|
get { return (Aspect)GetValue(AspectProperty); }
|
|
|
|
set { SetValue(AspectProperty, value); }
|
|
|
|
}
|
|
|
|
|
|
|
|
public bool IsLoading => (bool)GetValue(IsLoadingProperty);
|
|
|
|
|
|
|
|
public bool IsPressed => (bool)GetValue(IsPressedProperty);
|
|
|
|
|
|
|
|
public bool IsOpaque
|
|
|
|
{
|
|
|
|
get { return (bool)GetValue(IsOpaqueProperty); }
|
|
|
|
set { SetValue(IsOpaqueProperty, value); }
|
|
|
|
}
|
|
|
|
public ICommand Command
|
|
|
|
{
|
|
|
|
get { return (ICommand)GetValue(CommandProperty); }
|
|
|
|
set { SetValue(CommandProperty, value); }
|
|
|
|
}
|
|
|
|
|
|
|
|
public object CommandParameter
|
|
|
|
{
|
|
|
|
get { return GetValue(CommandParameterProperty); }
|
|
|
|
set { SetValue(CommandParameterProperty, value); }
|
|
|
|
}
|
|
|
|
|
|
|
|
[TypeConverter(typeof(ImageSourceConverter))]
|
|
|
|
public ImageSource Source
|
|
|
|
{
|
|
|
|
get { return (ImageSource)GetValue(SourceProperty); }
|
|
|
|
set { SetValue(SourceProperty, value); }
|
|
|
|
}
|
|
|
|
|
2018-11-10 22:25:01 +03:00
|
|
|
bool IButtonElement.IsEnabledCore
|
2018-11-04 01:45:28 +03:00
|
|
|
{
|
|
|
|
set { SetValueCore(IsEnabledProperty, value); }
|
|
|
|
}
|
|
|
|
|
|
|
|
protected override void OnBindingContextChanged()
|
|
|
|
{
|
2018-11-10 22:25:01 +03:00
|
|
|
ImageElement.OnBindingContextChanged(this, this);
|
2018-11-04 01:45:28 +03:00
|
|
|
base.OnBindingContextChanged();
|
|
|
|
}
|
|
|
|
|
|
|
|
protected internal override void ChangeVisualState()
|
|
|
|
{
|
|
|
|
if (IsEnabled && IsPressed)
|
|
|
|
{
|
2018-11-10 22:25:01 +03:00
|
|
|
VisualStateManager.GoToState(this, ButtonElement.PressedVisualState);
|
2018-11-04 01:45:28 +03:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
base.ChangeVisualState();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
protected override SizeRequest OnMeasure(double widthConstraint, double heightConstraint)
|
|
|
|
{
|
|
|
|
SizeRequest desiredSize = base.OnMeasure(double.PositiveInfinity, double.PositiveInfinity);
|
2018-11-10 22:25:01 +03:00
|
|
|
return ImageElement.Measure(this, desiredSize, widthConstraint, heightConstraint);
|
2018-11-04 01:45:28 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public IPlatformElementConfiguration<T, ImageButton> On<T>() where T : IConfigPlatform => _platformConfigurationRegistry.Value.On<T>();
|
|
|
|
|
2018-11-10 22:25:01 +03:00
|
|
|
int IBorderElement.CornerRadiusDefaultValue => (int)CornerRadiusProperty.DefaultValue;
|
|
|
|
|
|
|
|
Color IBorderElement.BorderColorDefaultValue => (Color)BorderColorProperty.DefaultValue;
|
|
|
|
|
|
|
|
double IBorderElement.BorderWidthDefaultValue => (double)BorderWidthProperty.DefaultValue;
|
|
|
|
|
2018-11-04 01:45:28 +03:00
|
|
|
void IBorderElement.OnBorderColorPropertyChanged(Color oldValue, Color newValue)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
[EditorBrowsable(EditorBrowsableState.Never)]
|
|
|
|
public void SetIsLoading(bool isLoading) => SetValue(IsLoadingPropertyKey, isLoading);
|
|
|
|
|
|
|
|
[EditorBrowsable(EditorBrowsableState.Never)]
|
|
|
|
public void SetIsPressed(bool isPressed) =>
|
|
|
|
SetValue(IsPressedPropertyKey, isPressed);
|
|
|
|
|
|
|
|
[EditorBrowsable(EditorBrowsableState.Never)]
|
|
|
|
public void SendClicked() =>
|
2018-11-10 22:25:01 +03:00
|
|
|
ButtonElement.ElementClicked(this, this);
|
2018-11-04 01:45:28 +03:00
|
|
|
|
|
|
|
[EditorBrowsable(EditorBrowsableState.Never)]
|
|
|
|
public void SendPressed() =>
|
2018-11-10 22:25:01 +03:00
|
|
|
ButtonElement.ElementPressed(this, this);
|
2018-11-04 01:45:28 +03:00
|
|
|
|
|
|
|
[EditorBrowsable(EditorBrowsableState.Never)]
|
|
|
|
public void SendReleased() =>
|
2018-11-10 22:25:01 +03:00
|
|
|
ButtonElement.ElementReleased(this, this);
|
2018-11-04 01:45:28 +03:00
|
|
|
|
|
|
|
public void PropagateUpClicked() =>
|
|
|
|
Clicked?.Invoke(this, EventArgs.Empty);
|
|
|
|
|
|
|
|
public void PropagateUpPressed() =>
|
|
|
|
Pressed?.Invoke(this, EventArgs.Empty);
|
|
|
|
|
|
|
|
public void PropagateUpReleased() =>
|
|
|
|
Released?.Invoke(this, EventArgs.Empty);
|
|
|
|
|
|
|
|
public void RaiseImageSourcePropertyChanged() =>
|
|
|
|
OnPropertyChanged(nameof(Source));
|
|
|
|
|
|
|
|
public Thickness Padding
|
|
|
|
{
|
|
|
|
get { return (Thickness)GetValue(PaddingElement.PaddingProperty); }
|
|
|
|
set { SetValue(PaddingElement.PaddingProperty, value); }
|
|
|
|
}
|
|
|
|
|
|
|
|
Thickness IPaddingElement.PaddingDefaultValueCreator()
|
|
|
|
{
|
|
|
|
return default(Thickness);
|
|
|
|
}
|
|
|
|
|
|
|
|
void IPaddingElement.OnPaddingPropertyChanged(Thickness oldValue, Thickness newValue)
|
|
|
|
{
|
|
|
|
InvalidateMeasureInternal(InvalidationTrigger.MeasureChanged);
|
|
|
|
}
|
|
|
|
|
2018-11-10 22:25:01 +03:00
|
|
|
void IImageElement.OnImageSourcesSourceChanged(object sender, EventArgs e) =>
|
|
|
|
ImageElement.ImageSourcesSourceChanged(this, e);
|
2018-11-04 01:45:28 +03:00
|
|
|
|
2018-11-10 22:25:01 +03:00
|
|
|
void IButtonElement.OnCommandCanExecuteChanged(object sender, EventArgs e) =>
|
|
|
|
ButtonElement.CommandCanExecuteChanged(this, EventArgs.Empty);
|
2018-11-04 01:45:28 +03:00
|
|
|
|
|
|
|
|
2018-11-10 22:25:01 +03:00
|
|
|
bool IBorderElement.IsCornerRadiusSet() => IsSet(CornerRadiusProperty);
|
|
|
|
bool IBorderElement.IsBackgroundColorSet() => IsSet(BackgroundColorProperty);
|
|
|
|
bool IBorderElement.IsBorderColorSet() => IsSet(BorderColorProperty);
|
|
|
|
bool IBorderElement.IsBorderWidthSet() => IsSet(BorderWidthProperty);
|
2018-11-04 01:45:28 +03:00
|
|
|
}
|
|
|
|
}
|