summaryrefslogtreecommitdiff
path: root/Xamarin.Forms.Core
diff options
context:
space:
mode:
authorE.Z. Hart <hartez@gmail.com>2016-03-22 17:16:53 -0600
committerE.Z. Hart <hartez@gmail.com>2016-03-30 16:22:35 -0600
commit17e2a4b94d5105b53c5875d9416d043f14313305 (patch)
tree6f03e5d6f562f230ed79d2e820327fe0bddc5069 /Xamarin.Forms.Core
parentb3a64fadb3951d4e5e65b5f000a5f930772c01ed (diff)
downloadxamarin-forms-17e2a4b94d5105b53c5875d9416d043f14313305.tar.gz
xamarin-forms-17e2a4b94d5105b53c5875d9416d043f14313305.tar.bz2
xamarin-forms-17e2a4b94d5105b53c5875d9416d043f14313305.zip
Add options for specifying layout of button text/image content
Also make the layout and layout defaults consistent across platforms
Diffstat (limited to 'Xamarin.Forms.Core')
-rw-r--r--Xamarin.Forms.Core/Button.cs80
1 files changed, 80 insertions, 0 deletions
diff --git a/Xamarin.Forms.Core/Button.cs b/Xamarin.Forms.Core/Button.cs
index 4662105f..9c746c9e 100644
--- a/Xamarin.Forms.Core/Button.cs
+++ b/Xamarin.Forms.Core/Button.cs
@@ -1,4 +1,6 @@
using System;
+using System.Diagnostics;
+using System.Globalization;
using System.Windows.Input;
using Xamarin.Forms.Platform;
@@ -12,6 +14,9 @@ namespace Xamarin.Forms
public static readonly BindableProperty CommandParameterProperty = BindableProperty.Create("CommandParameter", typeof(object), typeof(Button), null,
propertyChanged: (bindable, oldvalue, newvalue) => ((Button)bindable).CommandCanExecuteChanged(bindable, EventArgs.Empty));
+ public static readonly BindableProperty ContentLayoutProperty =
+ BindableProperty.Create("ContentLayout", typeof(ButtonContentLayout), typeof(Button), new ButtonContentLayout(ButtonContentLayout.ImagePosition.Left, DefaultSpacing));
+
public static readonly BindableProperty TextProperty = BindableProperty.Create("Text", typeof(string), typeof(Button), null,
propertyChanged: (bindable, oldVal, newVal) => ((Button)bindable).InvalidateMeasure(InvalidationTrigger.MeasureChanged));
@@ -39,6 +44,8 @@ namespace Xamarin.Forms
bool _cancelEvents;
+ const double DefaultSpacing = 10;
+
public Color BorderColor
{
get { return (Color)GetValue(BorderColorProperty); }
@@ -57,6 +64,12 @@ namespace Xamarin.Forms
set { SetValue(BorderWidthProperty, value); }
}
+ public ButtonContentLayout ContentLayout
+ {
+ get { return (ButtonContentLayout)GetValue(ContentLayoutProperty); }
+ set { SetValue(ContentLayoutProperty, value); }
+ }
+
public ICommand Command
{
get { return (ICommand)GetValue(CommandProperty); }
@@ -247,5 +260,72 @@ namespace Xamarin.Forms
button._cancelEvents = false;
}
+
+ [DebuggerDisplay("Image Position = {Position}, Spacing = {Spacing}")]
+ [TypeConverter(typeof(ButtonContentTypeConverter))]
+ public sealed class ButtonContentLayout
+ {
+ public enum ImagePosition
+ {
+ Left,
+ Top,
+ Right,
+ Bottom
+ }
+
+ public ButtonContentLayout(ImagePosition position, double spacing)
+ {
+ Position = position;
+ Spacing = spacing;
+ }
+
+ public ImagePosition Position { get; }
+
+ public double Spacing { get; }
+
+ public override string ToString()
+ {
+ return $"Image Position = {Position}, Spacing = {Spacing}";
+ }
+ }
+
+ public sealed class ButtonContentTypeConverter : TypeConverter
+ {
+ public override object ConvertFromInvariantString(string value)
+ {
+ if (value == null)
+ {
+ throw new InvalidOperationException($"Cannot convert null into {typeof(ButtonContentLayout)}");
+ }
+
+ string[] parts = value.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
+
+ if (parts.Length != 1 && parts.Length != 2)
+ {
+ throw new InvalidOperationException($"Cannot convert \"{value}\" into {typeof(ButtonContentLayout)}");
+ }
+
+ double spacing = DefaultSpacing;
+ var position = ButtonContentLayout.ImagePosition.Left;
+
+ var spacingFirst = char.IsDigit(parts[0][0]);
+
+ int positionIndex = spacingFirst ? (parts.Length == 2 ? 1 : -1) : 0;
+ int spacingIndex = spacingFirst ? 0 : (parts.Length == 2 ? 1 : -1);
+
+ if (spacingIndex > -1)
+ {
+ spacing = double.Parse(parts[spacingIndex]);
+ }
+
+ if (positionIndex > -1)
+ {
+ position =
+ (ButtonContentLayout.ImagePosition)Enum.Parse(typeof(ButtonContentLayout.ImagePosition), parts[positionIndex], true);
+ }
+
+ return new ButtonContentLayout(position, spacing);
+ }
+ }
}
} \ No newline at end of file