Skip to content

Commit a382be9

Browse files
Implement [BindableProperty] for AvatarView
1 parent 4060d68 commit a382be9

File tree

1 file changed

+28
-58
lines changed

1 file changed

+28
-58
lines changed

src/CommunityToolkit.Maui/Views/AvatarView.shared.cs

Lines changed: 28 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,6 @@ namespace CommunityToolkit.Maui.Views;
99
/// <summary>AvatarView control.</summary>
1010
public partial class AvatarView : Border, IAvatarView, IBorderElement, IFontElement, ITextElement, IImageElement, ITextAlignmentElement, ILineHeightElement, ICornerElement
1111
{
12-
/// <summary>The backing store for the <see cref="BorderColor" /> bindable property.</summary>
13-
public static readonly BindableProperty BorderColorProperty = BindableProperty.Create(nameof(BorderColor), typeof(Color), typeof(IAvatarView), defaultValue: AvatarViewDefaults.DefaultBorderColor, propertyChanged: OnBorderColorPropertyChanged);
14-
15-
/// <summary>The backing store for the <see cref="BorderWidth" /> bindable property.</summary>
16-
public static readonly BindableProperty BorderWidthProperty = BindableProperty.Create(nameof(BorderWidth), typeof(double), typeof(IAvatarView), defaultValue: AvatarViewDefaults.DefaultBorderWidth, propertyChanged: OnBorderWidthPropertyChanged);
17-
18-
/// <summary>The backing store for the <see cref="CornerRadius" /> bindable property.</summary>
19-
public static readonly BindableProperty CornerRadiusProperty = BindableProperty.Create(nameof(CornerRadius), typeof(CornerRadius), typeof(ICornerElement), defaultValue: AvatarViewDefaults.DefaultCornerRadius, propertyChanged: OnCornerRadiusPropertyChanged);
20-
2112
/// <summary>The backing store for the <see cref="IFontElement.FontAttributes" /> bindable property.</summary>
2213
public static readonly BindableProperty FontAttributesProperty = FontElement.FontAttributesProperty;
2314

@@ -30,15 +21,9 @@ public partial class AvatarView : Border, IAvatarView, IBorderElement, IFontElem
3021
/// <summary>The backing store for the <see cref="IFontElement.FontSize" /> bindable property.</summary>
3122
public static readonly BindableProperty FontSizeProperty = FontElement.FontSizeProperty;
3223

33-
/// <summary>The backing store for the <see cref="ImageSource" /> bindable property.</summary>
34-
public static readonly BindableProperty ImageSourceProperty = BindableProperty.Create(nameof(ImageSource), typeof(ImageSource), typeof(IImageElement), default(ImageSource), propertyChanged: OnImageSourcePropertyChanged);
35-
3624
/// <summary>The backing store for the <see cref="ITextStyle.TextColor" /> bindable property.</summary>
3725
public static readonly BindableProperty TextColorProperty = TextElement.TextColorProperty;
3826

39-
/// <summary>The backing store for the <see cref="Text" /> bindable property.</summary>
40-
public static readonly BindableProperty TextProperty = BindableProperty.Create(nameof(Text), typeof(string), typeof(AvatarView), defaultValue: AvatarViewDefaults.DefaultText, propertyChanged: OnTextPropertyChanged);
41-
4227
/// <summary>The backing store for the <see cref="TextTransform" /> bindable property.</summary>
4328
public static readonly BindableProperty TextTransformProperty = TextElement.TextTransformProperty;
4429

@@ -80,34 +65,13 @@ public AvatarView()
8065
/// <summary>Gets or sets the control font.</summary>
8166
public Microsoft.Maui.Font Font { get; set; } = Microsoft.Maui.Font.SystemFontOfSize((double)FontElement.FontSizeProperty.DefaultValue);
8267

83-
/// <summary>Gets or sets a value of the control border colour.</summary>
84-
public Color BorderColor
85-
{
86-
get => (Color)GetValue(BorderColorProperty);
87-
set => SetValue(BorderColorProperty, value);
88-
}
89-
90-
/// <summary>Gets or sets a value of the control border width.</summary>
91-
public double BorderWidth
92-
{
93-
get => (double)GetValue(BorderWidthProperty);
94-
set => SetValue(BorderWidthProperty, value);
95-
}
96-
9768
/// <summary>Gets or sets a value of the control text character spacing property.</summary>
9869
public double CharacterSpacing
9970
{
10071
get => (double)GetValue(TextElement.CharacterSpacingProperty);
10172
set => SetValue(TextElement.CharacterSpacingProperty, value);
10273
}
10374

104-
/// <summary>Gets or sets a value of the control corner radius property.</summary>
105-
public CornerRadius CornerRadius
106-
{
107-
get => (CornerRadius)GetValue(CornerRadiusProperty);
108-
set => SetValue(CornerRadiusProperty, value);
109-
}
110-
11175
/// <summary>Gets or sets a value of the control font attributes property.</summary>
11276
public FontAttributes FontAttributes
11377
{
@@ -137,20 +101,26 @@ public double FontSize
137101
set => SetValue(FontElement.FontSizeProperty, value);
138102
}
139103

104+
/// <summary>Gets or sets a value of the control border colour.</summary>
105+
[BindableProperty(PropertyChangedMethodName = nameof(OnBorderColorPropertyChanged))]
106+
public partial Color BorderColor { get; set; } = AvatarViewDefaults.DefaultBorderColor;
107+
108+
/// <summary>Gets or sets a value of the control border width.</summary>
109+
[BindableProperty(PropertyChangedMethodName = nameof(OnBorderWidthPropertyChanged))]
110+
public partial double BorderWidth { get; set; } = AvatarViewDefaults.DefaultBorderWidth;
111+
112+
/// <summary>Gets or sets a value of the control corner radius property.</summary>
113+
[BindableProperty(PropertyChangedMethodName = nameof(OnCornerRadiusPropertyChanged))]
114+
public partial CornerRadius CornerRadius { get; set; } = AvatarViewDefaults.DefaultCornerRadius;
115+
140116
/// <summary>Gets or sets a value of the control image source property.</summary>
141117
[TypeConverter(typeof(ImageSourceConverter))]
142-
public ImageSource ImageSource
143-
{
144-
get => (ImageSource)GetValue(ImageSourceProperty);
145-
set => SetValue(ImageSourceProperty, value);
146-
}
118+
[BindableProperty(PropertyChangedMethodName = nameof(OnImageSourcePropertyChanged))]
119+
public partial ImageSource ImageSource { get; set; }
147120

148121
/// <summary>Gets or sets a value of the control text property.</summary>
149-
public string Text
150-
{
151-
get => (string)GetValue(TextProperty);
152-
set => SetValue(TextProperty, value);
153-
}
122+
[BindableProperty(PropertyChangedMethodName = nameof(OnTextPropertyChanged))]
123+
public partial string Text { get; set; } = AvatarViewDefaults.DefaultText;
154124

155125
/// <summary>Gets or sets a value of the control text colour property.</summary>
156126
public Color TextColor
@@ -170,13 +140,13 @@ public TextTransform TextTransform
170140

171141
Aspect IImageElement.Aspect => avatarImage.Aspect;
172142

173-
Color IBorderElement.BorderColorDefaultValue => (Color)BorderColorProperty.DefaultValue;
143+
Color IBorderElement.BorderColorDefaultValue => AvatarViewDefaults.DefaultBorderColor;
174144

175-
double IBorderElement.BorderWidthDefaultValue => (double)BorderWidthProperty.DefaultValue;
145+
double IBorderElement.BorderWidthDefaultValue => AvatarViewDefaults.DefaultBorderWidth;
176146

177147
int IBorderElement.CornerRadius => (int)GetAverageCorderRadius(CornerRadius);
178148

179-
int IBorderElement.CornerRadiusDefaultValue => (int)GetAverageCorderRadius((CornerRadius)CornerRadiusProperty.DefaultValue);
149+
int IBorderElement.CornerRadiusDefaultValue => (int)GetAverageCorderRadius(AvatarViewDefaults.DefaultCornerRadius);
180150

181151
TextAlignment ITextAlignment.HorizontalTextAlignment => ((ITextAlignmentElement)this).HorizontalTextAlignment;
182152

@@ -344,15 +314,15 @@ void HandlePropertyChanged(object? sender, PropertyChangedEventArgs e)
344314
{
345315
// Ensure avatarImage is clipped to the bounds of the AvatarView whenever its Height, Width, CornerRadius and Padding properties change
346316
if ((e.PropertyName == HeightProperty.PropertyName
347-
|| e.PropertyName == WidthProperty.PropertyName
348-
|| e.PropertyName == PaddingProperty.PropertyName
349-
|| e.PropertyName == ImageSourceProperty.PropertyName
350-
|| e.PropertyName == BorderWidthProperty.PropertyName
351-
|| e.PropertyName == CornerRadiusProperty.PropertyName
352-
|| e.PropertyName == StrokeThicknessProperty.PropertyName)
353-
&& Height >= 0 // The default value of Height (before the view is drawn onto the page) is -1
354-
&& Width >= 0 // The default value of Y (before the view is drawn onto the page) is -1
355-
&& avatarImage.Source is not null)
317+
|| e.PropertyName == WidthProperty.PropertyName
318+
|| e.PropertyName == PaddingProperty.PropertyName
319+
|| e.PropertyName == ImageSourceProperty.PropertyName
320+
|| e.PropertyName == BorderWidthProperty.PropertyName
321+
|| e.PropertyName == CornerRadiusProperty.PropertyName
322+
|| e.PropertyName == StrokeThicknessProperty.PropertyName)
323+
&& Height >= 0 // The default value of Height (before the view is drawn onto the page) is -1
324+
&& Width >= 0 // The default value of Y (before the view is drawn onto the page) is -1
325+
&& avatarImage.Source is not null)
356326

357327
{
358328
Geometry? avatarImageClipGeometry = null;

0 commit comments

Comments
 (0)