Skip to content

Commit 7f42721

Browse files
committed
Add Padding support back
This needed some work to properly measure/arrange with Padding (and strokethickness) in mind. The strokethickness may be unnecessary here if the platform button accounts for it already.
1 parent 86fd2d6 commit 7f42721

File tree

8 files changed

+128
-56
lines changed

8 files changed

+128
-56
lines changed

Maui.ContentButton/Apple/ContentButtonHandler.ios.maccatalyst.cs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ public partial class ContentButtonHandler : ViewHandler<IContentButton, MButton>
1010
{
1111
static readonly UIControlState[] ControlStates = { UIControlState.Normal, UIControlState.Highlighted, UIControlState.Disabled };
1212

13-
//public readonly static Thickness DefaultPadding = new Thickness(12, 7);
13+
public readonly static Thickness DefaultPadding = new Thickness(12, 7);
1414

1515
public const int ContentButtonHandlerContentViewTag = 23123;
1616

@@ -27,6 +27,8 @@ protected override UIButton CreatePlatformView()
2727

2828
return button;
2929
}
30+
31+
3032

3133
public static void MapBackground(IContentButtonHandler handler, IContentButton button)
3234
{
@@ -68,10 +70,10 @@ public static void MapCornerRadius(IContentButtonHandler handler, IButtonStroke
6870
}
6971
}
7072

71-
// public static void MapPadding(IContentButtonHandler handler, IPadding padding)
72-
// {
73-
// handler.PlatformView?.UpdatePadding(padding.Padding, DefaultPadding);
74-
// }
73+
public static void MapPadding(IContentButtonHandler handler, IPadding padding)
74+
{
75+
handler.PlatformView?.UpdatePadding(padding.Padding, DefaultPadding);
76+
}
7577

7678
static void SetControlPropertiesFromProxy(UIButton platformView)
7779
{

Maui.ContentButton/ContentButton.cs

Lines changed: 44 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -17,24 +17,24 @@ public class ContentButton : View, IContentButton, ICrossPlatformLayout
1717
{
1818

1919
/// <summary>Bindable property for <see cref="Content"/>.</summary>
20-
public static readonly BindableProperty ContentProperty
20+
public static readonly BindableProperty ContentProperty
2121
= BindableProperty.Create(nameof(Content), typeof(View), typeof(ContentView), null,
22-
propertyChanged: (bindableObject, oldValue, newValue) =>
23-
{
24-
if (bindableObject is ContentButton contentButton)
22+
propertyChanged: (bindableObject, oldValue, newValue) =>
2523
{
26-
27-
if (oldValue is View oldView)
24+
if (bindableObject is ContentButton contentButton)
2825
{
29-
contentButton.RemoveLogicalChild(oldView);
30-
}
3126

32-
if (newValue is View newView)
33-
{
34-
contentButton.AddLogicalChild(newView);
27+
if (oldValue is View oldView)
28+
{
29+
contentButton.RemoveLogicalChild(oldView);
30+
}
31+
32+
if (newValue is View newView)
33+
{
34+
contentButton.AddLogicalChild(newView);
35+
}
3536
}
36-
}
37-
});
37+
});
3838

3939
public View Content
4040
{
@@ -60,23 +60,21 @@ protected override void OnBindingContextChanged()
6060
IView IContentButton.PresentedContent => Content;
6161

6262

63-
// public static readonly BindableProperty PaddingProperty =
64-
// BindableProperty.Create(nameof(Padding), typeof(Thickness), typeof(ContentButton), new Thickness(),
65-
// propertyChanged: (bindable, oldValue, newValue) =>
66-
// {
67-
// if (bindable is ContentButton contentButton)
68-
// {
69-
// contentButton.InvalidateMeasure();
70-
// }
71-
// });
72-
73-
// public Thickness Padding
74-
// {
75-
// get => (Thickness)GetValue(PaddingProperty);
76-
// set => SetValue(PaddingProperty, value);
77-
// }
78-
63+
public static readonly BindableProperty PaddingProperty =
64+
BindableProperty.Create(nameof(Padding), typeof(Thickness), typeof(ContentButton), new Thickness(),
65+
propertyChanged: (bindable, oldValue, newValue) =>
66+
{
67+
if (bindable is ContentButton contentButton)
68+
{
69+
contentButton.InvalidateMeasure();
70+
}
71+
});
7972

73+
public Thickness Padding
74+
{
75+
get => (Thickness)GetValue(PaddingProperty);
76+
set => SetValue(PaddingProperty, value);
77+
}
8078

8179
public const int DefaultCornerRadius = -1;
8280

@@ -89,7 +87,8 @@ protected override void OnBindingContextChanged()
8987

9088
/// <summary>Bindable property for <see cref="IBorderElement.CornerRadius"/>.</summary>
9189
public static readonly BindableProperty CornerRadiusProperty =
92-
BindableProperty.Create(nameof(IBorderElement.CornerRadius), typeof(int), typeof(IBorderElement), defaultValue: DefaultCornerRadius);
90+
BindableProperty.Create(nameof(IBorderElement.CornerRadius), typeof(int), typeof(IBorderElement),
91+
defaultValue: DefaultCornerRadius);
9392

9493

9594

@@ -111,7 +110,8 @@ public int CornerRadius
111110
set => SetValue(CornerRadiusProperty, value);
112111
}
113112

114-
static readonly BindablePropertyKey IsPressedPropertyKey = BindableProperty.CreateReadOnly(nameof(IsPressed), typeof(bool), typeof(ContentButton), default(bool));
113+
static readonly BindablePropertyKey IsPressedPropertyKey =
114+
BindableProperty.CreateReadOnly(nameof(IsPressed), typeof(bool), typeof(ContentButton), default(bool));
115115

116116
public static readonly BindableProperty IsPressedProperty = IsPressedPropertyKey.BindableProperty;
117117

@@ -189,4 +189,16 @@ public object CommandParameter
189189
set => SetValue(CommandParameterProperty, value);
190190
}
191191

192-
}
192+
public Size CrossPlatformArrange(Rect bounds)
193+
{
194+
var inset = bounds.Inset(StrokeThickness);
195+
this.ArrangeContent(inset);
196+
return bounds.Size;
197+
}
198+
199+
public Size CrossPlatformMeasure(double widthConstraint, double heightConstraint)
200+
{
201+
var inset = Padding + StrokeThickness;
202+
return this.MeasureContent( inset, widthConstraint, heightConstraint);
203+
}
204+
}

Maui.ContentButton/ContentButtonHandler.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public partial class ContentButtonHandler : IContentButtonHandler
2020
{
2121
public static IPropertyMapper<IContentButton, IContentButtonHandler> Mapper = new PropertyMapper<IContentButton, IContentButtonHandler>(ViewMapper, ViewHandler.ViewMapper)
2222
{
23-
//[nameof(IPadding.Padding)] = MapPadding,
23+
[nameof(IPadding.Padding)] = MapPadding,
2424
[nameof(IButtonStroke.StrokeThickness)] = MapStrokeThickness,
2525
[nameof(IButtonStroke.StrokeColor)] = MapStrokeColor,
2626
[nameof(IButtonStroke.CornerRadius)] = MapCornerRadius,
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
using Microsoft.Maui.Primitives;
2+
3+
namespace MauiContentButton;
4+
5+
// These are mirrors of the LayoutExtensions in MAUI Core but those take IContentView so aren't usable here
6+
public static class ContentButtonLayoutExtensions
7+
{
8+
public static Size MeasureContent(this IContentButton contentButton, Thickness inset, double widthConstraint, double heightConstraint)
9+
{
10+
var content = contentButton.PresentedContent;
11+
12+
if (Dimension.IsExplicitSet(contentButton.Width))
13+
{
14+
widthConstraint = contentButton.Width;
15+
}
16+
17+
if (Dimension.IsExplicitSet(contentButton.Height))
18+
{
19+
heightConstraint = contentButton.Height;
20+
}
21+
22+
var contentSize = Size.Zero;
23+
24+
if (content != null)
25+
{
26+
contentSize = content.Measure(widthConstraint - inset.HorizontalThickness,
27+
heightConstraint - inset.VerticalThickness);
28+
}
29+
30+
return new Size(contentSize.Width + inset.HorizontalThickness, contentSize.Height + inset.VerticalThickness);
31+
}
32+
33+
public static void ArrangeContent(this IContentButton contentButton, Rect bounds)
34+
{
35+
if (contentButton.PresentedContent == null)
36+
{
37+
return;
38+
}
39+
40+
var padding = contentButton.Padding;
41+
42+
var targetBounds = new Rect(bounds.Left + padding.Left, bounds.Top + padding.Top,
43+
bounds.Width - padding.HorizontalThickness, bounds.Height - padding.VerticalThickness);
44+
45+
_ = contentButton.PresentedContent.Arrange(targetBounds);
46+
}
47+
48+
public static Rect Inset(this Rect rectangle, double inset)
49+
{
50+
if (inset == 0)
51+
{
52+
return rectangle;
53+
}
54+
55+
return new Rect(rectangle.Left + inset, rectangle.Top + inset,
56+
rectangle.Width - (2 * inset), rectangle.Height - (2 * inset));
57+
}
58+
}

Maui.ContentButton/IContentButton.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
namespace MauiContentButton;
1111

12-
public interface IContentButton : IView, IButtonStroke, ICrossPlatformLayout //, IPadding
12+
public interface IContentButton : IView, IButtonStroke, ICrossPlatformLayout, IPadding
1313
{
1414
/// <summary>
1515
/// Occurs when the button is pressed.

Maui.ContentButton/Platforms/Android/ContentButtonHandler.android.cs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -115,18 +115,18 @@ public static void MapCornerRadius(IContentButtonHandler handler, IButtonStroke
115115
}
116116
}
117117

118-
// public static void MapPadding(IContentButtonHandler handler, IPadding padding)
119-
// {
120-
// if (handler.PlatformView is not null)
121-
// {
122-
// var density = (handler.PlatformView.Resources?.DisplayMetrics?.Density ?? 1f);
123-
// handler.PlatformView.SetContentPadding(
124-
// (int)(padding.Padding.Left * density),
125-
// (int)(padding.Padding.Top * density),
126-
// (int)(padding.Padding.Right * density),
127-
// (int)(padding.Padding.Bottom * density));
128-
// }
129-
// }
118+
public static void MapPadding(IContentButtonHandler handler, IPadding padding)
119+
{
120+
if (handler.PlatformView is not null)
121+
{
122+
var density = (handler.PlatformView.Resources?.DisplayMetrics?.Density ?? 1f);
123+
handler.PlatformView.SetContentPadding(
124+
(int)(padding.Padding.Left * density),
125+
(int)(padding.Padding.Top * density),
126+
(int)(padding.Padding.Right * density),
127+
(int)(padding.Padding.Bottom * density));
128+
}
129+
}
130130

131131
static bool OnTouch(IContentButton? button, AView? v, MotionEvent? e)
132132
{

Maui.ContentButton/Platforms/Android/MauiMaterialCardView.android.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ protected override void OnMeasure(int widthMeasureSpec, int heightMeasureSpec)
6363
var heightMode = MeasureSpec.GetMode(heightMeasureSpec);
6464

6565
var measure = CrossPlatformMeasure(deviceIndependentWidth, deviceIndependentHeight);
66-
66+
6767
// If the measure spec was exact, we should return the explicit size value, even if the content
6868
// measure came out to a different size
6969
var width = widthMode == MeasureSpecMode.Exactly ? deviceIndependentWidth : measure.Width;

Maui.ContentButton/Platforms/Windows/ContentButtonHandler.windows.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -105,10 +105,10 @@ public static void MapCornerRadius(IContentButtonHandler handler, IButtonStroke
105105
handler.PlatformView?.UpdateCornerRadius(buttonStroke);
106106
}
107107

108-
// public static void MapPadding(IContentButtonHandler handler, IPadding padding)
109-
// {
110-
// handler.PlatformView?.UpdatePadding(padding, new Microsoft.UI.Xaml.Thickness(0));
111-
// }
108+
public static void MapPadding(IContentButtonHandler handler, IPadding padding)
109+
{
110+
handler.PlatformView?.UpdatePadding(padding, new Microsoft.UI.Xaml.Thickness(0));
111+
}
112112

113113
void OnClick(object sender, RoutedEventArgs e)
114114
{

0 commit comments

Comments
 (0)