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

Commit

Permalink
Bring ImageButton API additions in line with current code (#4328)
Browse files Browse the repository at this point in the history
* Bring ImageButton API additions in line with current code

* [Core] putting things in better places

* [Core] first round of changes

* fix issue with checking if bindable properties are set
  • Loading branch information
PureWeen committed Nov 10, 2018
1 parent f3c5405 commit 2f416f2
Show file tree
Hide file tree
Showing 22 changed files with 477 additions and 455 deletions.
12 changes: 10 additions & 2 deletions Xamarin.Forms.Core/BorderElement.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
namespace Xamarin.Forms
using System;

namespace Xamarin.Forms
{
static class BorderElement
{
public const int DefaultCornerRadius = -1;

public static readonly BindableProperty BorderColorProperty =
BindableProperty.Create("BorderColor", typeof(Color), typeof(IBorderElement), Color.Default,
BindableProperty.Create(nameof(IBorderElement.BorderColor), typeof(Color), typeof(IBorderElement), Color.Default,
propertyChanged: OnBorderColorPropertyChanged);

public static readonly BindableProperty BorderWidthProperty = BindableProperty.Create(nameof(IBorderElement.BorderWidth), typeof(double), typeof(IBorderElement), -1d);

public static readonly BindableProperty CornerRadiusProperty = BindableProperty.Create(nameof(IBorderElement.CornerRadius), typeof(int), typeof(IBorderElement), defaultValue: DefaultCornerRadius);

static void OnBorderColorPropertyChanged(BindableObject bindable, object oldValue, object newValue)
{
((IBorderElement)bindable).OnBorderColorPropertyChanged((Color)oldValue, (Color)newValue);
Expand Down
112 changes: 34 additions & 78 deletions Xamarin.Forms.Core/Button.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,14 @@
namespace Xamarin.Forms
{
[RenderWith(typeof(_ButtonRenderer))]
public class Button : View, IFontElement, ITextElement, IBorderElement, IButtonController, IElementConfiguration<Button>, IPaddingElement, IBorderController, IImageController, IViewController
public class Button : View, IFontElement, ITextElement, IBorderElement, IButtonController, IElementConfiguration<Button>, IPaddingElement, IImageController, IViewController, IButtonElement, IImageElement
{
const double DefaultSpacing = 10;
const int DefaultBorderRadius = 5;
const int DefaultCornerRadius = -1;
const double DefaultSpacing = 10;

public static readonly BindableProperty CommandProperty = BindableProperty.Create(nameof(Command), typeof(ICommand), typeof(Button), null, propertyChanging: OnCommandChanging, propertyChanged: OnCommandChanged);
public static readonly BindableProperty CommandProperty = ButtonElement.CommandProperty;

public static readonly BindableProperty CommandParameterProperty = BindableProperty.Create(nameof(CommandParameter), typeof(object), typeof(Button), null,
propertyChanged: (bindable, oldvalue, newvalue) => ButtonElementManager.CommandCanExecuteChanged(bindable, EventArgs.Empty));
public static readonly BindableProperty CommandParameterProperty = ButtonElement.CommandParameterProperty;

public static readonly BindableProperty ContentLayoutProperty =
BindableProperty.Create("ContentLayout", typeof(ButtonContentLayout), typeof(Button), new ButtonContentLayout(ButtonContentLayout.ImagePosition.Left, DefaultSpacing));
Expand All @@ -43,12 +41,10 @@ public class Button : View, IFontElement, ITextElement, IBorderElement, IButtonC
public static readonly BindableProperty BorderRadiusProperty = BindableProperty.Create("BorderRadius", typeof(int), typeof(Button), defaultValue: DefaultBorderRadius,
propertyChanged: BorderRadiusPropertyChanged);

public static readonly BindableProperty CornerRadiusProperty = BindableProperty.Create("CornerRadius", typeof(int), typeof(Button), defaultValue: DefaultCornerRadius,
public static readonly BindableProperty CornerRadiusProperty = BindableProperty.Create("CornerRadius", typeof(int), typeof(Button), defaultValue: BorderElement.DefaultCornerRadius,
propertyChanged: CornerRadiusPropertyChanged);

public static readonly BindableProperty ImageProperty = BindableProperty.Create(nameof(Image), typeof(FileImageSource), typeof(Button), default(FileImageSource),
propertyChanging: OnImageSourceChanging,
propertyChanged: OnImageSourceChanged);
public static readonly BindableProperty ImageProperty = ImageElement.FileImageProperty;


public static readonly BindableProperty PaddingProperty = PaddingElement.PaddingProperty;
Expand Down Expand Up @@ -143,33 +139,33 @@ public Color TextColor
set { SetValue(TextElement.TextColorProperty, value); }
}

bool IButtonController.IsEnabledCore
bool IButtonElement.IsEnabledCore
{
set { SetValueCore(IsEnabledProperty, value); }
}

[EditorBrowsable(EditorBrowsableState.Never)]
public void SendClicked() => ButtonElementManager.ElementClicked(this, this);
public void SendClicked() => ButtonElement.ElementClicked(this, this);

public bool IsPressed => (bool)GetValue(IsPressedProperty);

[EditorBrowsable(EditorBrowsableState.Never)]
void IButtonController.SetIsPressed(bool isPressed) => SetValue(IsPressedPropertyKey, isPressed);
void IButtonElement.SetIsPressed(bool isPressed) => SetValue(IsPressedPropertyKey, isPressed);

[EditorBrowsable(EditorBrowsableState.Never)]
public void SendPressed() => ButtonElementManager.ElementPressed(this, this);
public void SendPressed() => ButtonElement.ElementPressed(this, this);

[EditorBrowsable(EditorBrowsableState.Never)]
public void SendReleased() => ButtonElementManager.ElementReleased(this, this);
public void SendReleased() => ButtonElement.ElementReleased(this, this);

[EditorBrowsable(EditorBrowsableState.Never)]
void IButtonController.PropagateUpClicked() => Clicked?.Invoke(this, EventArgs.Empty);
void IButtonElement.PropagateUpClicked() => Clicked?.Invoke(this, EventArgs.Empty);

[EditorBrowsable(EditorBrowsableState.Never)]
void IButtonController.PropagateUpPressed() => Pressed?.Invoke(this, EventArgs.Empty);
void IButtonElement.PropagateUpPressed() => Pressed?.Invoke(this, EventArgs.Empty);

[EditorBrowsable(EditorBrowsableState.Never)]
void IButtonController.PropagateUpReleased() => Released?.Invoke(this, EventArgs.Empty);
void IButtonElement.PropagateUpReleased() => Released?.Invoke(this, EventArgs.Empty);

public FontAttributes FontAttributes
{
Expand All @@ -191,10 +187,6 @@ public double FontSize
}

public event EventHandler Clicked;
BindableProperty IBorderController.CornerRadiusProperty => Button.CornerRadiusProperty;
BindableProperty IBorderController.BorderColorProperty => Button.BorderColorProperty;
BindableProperty IBorderController.BorderWidthProperty => Button.BorderWidthProperty;

public event EventHandler Pressed;

public event EventHandler Released;
Expand All @@ -213,7 +205,7 @@ protected internal override void ChangeVisualState()
{
if (IsEnabled && IsPressed)
{
VisualStateManager.GoToState(this, ButtonElementManager.PressedVisualState);
VisualStateManager.GoToState(this, ButtonElement.PressedVisualState);
}
else
{
Expand Down Expand Up @@ -245,17 +237,18 @@ void IFontElement.OnFontAttributesChanged(FontAttributes oldValue, FontAttribute
void IFontElement.OnFontChanged(Font oldValue, Font newValue) =>
InvalidateMeasureInternal(InvalidationTrigger.MeasureChanged);

Aspect IImageController.Aspect => Aspect.AspectFit;
ImageSource IImageController.Source => Image;
bool IImageController.IsOpaque => false;
Aspect IImageElement.Aspect => Aspect.AspectFit;
ImageSource IImageElement.Source => Image;
bool IImageElement.IsOpaque => false;


BindableProperty IImageController.SourceProperty => ImageProperty;
BindableProperty IImageController.AspectProperty => null;
BindableProperty IImageController.IsOpaqueProperty => null;
void IImageElement.RaiseImageSourcePropertyChanged() => OnPropertyChanged(ImageProperty.PropertyName);

int IBorderElement.CornerRadiusDefaultValue => (int)CornerRadiusProperty.DefaultValue;

void IImageController.RaiseImageSourcePropertyChanged() => OnPropertyChanged(ImageProperty.PropertyName);
Color IBorderElement.BorderColorDefaultValue => (Color)BorderColorProperty.DefaultValue;

double IBorderElement.BorderWidthDefaultValue => (double)BorderWidthProperty.DefaultValue;

/// <summary>
/// Flag to prevent overwriting the value of CornerRadius
Expand All @@ -270,7 +263,7 @@ static void BorderRadiusPropertyChanged(BindableObject bindable, object oldvalue
var button = (Button)bindable;
var val = (int)newvalue;
if (val == DefaultBorderRadius && !button.cornerOrBorderRadiusSetting)
val = DefaultCornerRadius;
val = BorderElement.DefaultCornerRadius;

var oldVal = (int)bindable.GetValue(Button.CornerRadiusProperty);

Expand All @@ -289,7 +282,7 @@ static void CornerRadiusPropertyChanged(BindableObject bindable, object oldvalue

var button = (Button)bindable;
var val = (int)newvalue;
if (val == DefaultCornerRadius && !button.cornerOrBorderRadiusSetting)
if (val == BorderElement.DefaultCornerRadius && !button.cornerOrBorderRadiusSetting)
val = DefaultBorderRadius;

#pragma warning disable 0618 // retain until BorderRadiusProperty removed
Expand All @@ -314,58 +307,21 @@ void IBorderElement.OnBorderColorPropertyChanged(Color oldValue, Color newValue)
{
}

void OnImageSourcesSourceChanged(object sender, EventArgs e) =>
ImageElementManager.ImageSourcesSourceChanged(this, EventArgs.Empty);

static void OnImageSourceChanged(BindableObject bindable, object oldValue, object newValue)
{
ImageSource newSource = (ImageSource)newValue;
Button button = (Button)bindable;
if (newSource != null)
{
newSource.SourceChanged += button.OnImageSourcesSourceChanged;
}
ImageElementManager.ImageSourceChanged(bindable, newSource);
}

static void OnImageSourceChanging(BindableObject bindable, object oldValue, object newValue)
{
ImageSource oldSource = (ImageSource)oldValue;
Button button = (Button)bindable;

if (oldSource != null)
{
oldSource.SourceChanged -= button.OnImageSourcesSourceChanged;
}
ImageElementManager.ImageSourceChanging(oldSource);
}


void OnCommandCanExecuteChanged(object sender, EventArgs e) =>
ButtonElementManager.CommandCanExecuteChanged(this, EventArgs.Empty);
void IImageElement.OnImageSourcesSourceChanged(object sender, EventArgs e) =>
ImageElement.ImageSourcesSourceChanged(this, e);

static void OnCommandChanged(BindableObject bo, object o, object n)
{
var button = (Button)bo;
if (n is ICommand newCommand)
newCommand.CanExecuteChanged += button.OnCommandCanExecuteChanged;

ButtonElementManager.CommandChanged(button);
}

static void OnCommandChanging(BindableObject bo, object o, object n)
{
var button = (Button)bo;
if (o != null)
{
(o as ICommand).CanExecuteChanged -= button.OnCommandCanExecuteChanged;
}
}
void IButtonElement.OnCommandCanExecuteChanged(object sender, EventArgs e) =>
ButtonElement.CommandCanExecuteChanged(this, EventArgs.Empty);

void IImageController.SetIsLoading(bool isLoading)
{
}

bool IBorderElement.IsCornerRadiusSet() => IsSet(CornerRadiusProperty);
bool IBorderElement.IsBackgroundColorSet() => IsSet(BackgroundColorProperty);
bool IBorderElement.IsBorderColorSet() => IsSet(BorderColorProperty);
bool IBorderElement.IsBorderWidthSet() => IsSet(BorderWidthProperty);

[DebuggerDisplay("Image Position = {Position}, Spacing = {Spacing}")]
[TypeConverter(typeof(ButtonContentTypeConverter))]
public sealed class ButtonContentLayout
Expand Down
86 changes: 86 additions & 0 deletions Xamarin.Forms.Core/ButtonElement.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
using System;
using System.Windows.Input;
using Xamarin.Forms.Internals;

namespace Xamarin.Forms
{
static class ButtonElement
{
public static readonly BindableProperty CommandProperty = BindableProperty.Create(nameof(IButtonElement.Command), typeof(ICommand), typeof(IButtonElement), null, propertyChanging: OnCommandChanging, propertyChanged: OnCommandChanged);

public static readonly BindableProperty CommandParameterProperty = BindableProperty.Create(nameof(IButtonElement.CommandParameter), typeof(object), typeof(IButtonElement), null,
propertyChanged: (bindable, oldvalue, newvalue) => CommandCanExecuteChanged(bindable, EventArgs.Empty));

static void OnCommandChanged(BindableObject bo, object o, object n)
{
IButtonElement button = (IButtonElement)bo;
if (n is ICommand newCommand)
newCommand.CanExecuteChanged += button.OnCommandCanExecuteChanged;

CommandChanged(button);
}

static void OnCommandChanging(BindableObject bo, object o, object n)
{
IButtonElement button = (IButtonElement)bo;
if (o != null)
{
(o as ICommand).CanExecuteChanged -= button.OnCommandCanExecuteChanged;
}
}

public const string PressedVisualState = "Pressed";

public static void CommandChanged(IButtonElement sender)
{
if (sender.Command != null)
{
CommandCanExecuteChanged(sender, EventArgs.Empty);
}
else
{
sender.IsEnabledCore = true;
}
}

public static void CommandCanExecuteChanged(object sender, EventArgs e)
{
IButtonElement ButtonElementManager = (IButtonElement)sender;
ICommand cmd = ButtonElementManager.Command;
if (cmd != null)
{
ButtonElementManager.IsEnabledCore = cmd.CanExecute(ButtonElementManager.CommandParameter);
}
}

public static void ElementClicked(VisualElement visualElement, IButtonElement ButtonElementManager)
{
if (visualElement.IsEnabled == true)
{
ButtonElementManager.Command?.Execute(ButtonElementManager.CommandParameter);
ButtonElementManager.PropagateUpClicked();
}
}

public static void ElementPressed(VisualElement visualElement, IButtonElement ButtonElementManager)
{
if (visualElement.IsEnabled == true)
{
ButtonElementManager.SetIsPressed(true);
visualElement.ChangeVisualStateInternal();
ButtonElementManager.PropagateUpPressed();
}
}

public static void ElementReleased(VisualElement visualElement, IButtonElement ButtonElementManager)
{
if (visualElement.IsEnabled == true)
{
IButtonController buttonController = ButtonElementManager as IButtonController;
ButtonElementManager.SetIsPressed(false);
visualElement.ChangeVisualStateInternal();
ButtonElementManager.PropagateUpReleased();
}
}
}
}
66 changes: 0 additions & 66 deletions Xamarin.Forms.Core/ButtonElementManager.cs

This file was deleted.

Loading

0 comments on commit 2f416f2

Please sign in to comment.