This repository has been archived by the owner on May 1, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
Copy pathImageButtonRenderer.cs
282 lines (231 loc) · 7.95 KB
/
ImageButtonRenderer.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
using System;
using System.ComponentModel;
using System.Threading.Tasks;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Media;
using Xamarin.Forms.Internals;
using Windows.UI.Xaml.Input;
using WBrush = Windows.UI.Xaml.Media.Brush;
using WImage = Windows.UI.Xaml.Controls.Image;
using WStretch = Windows.UI.Xaml.Media.Stretch;
using WThickness = Windows.UI.Xaml.Thickness;
namespace Xamarin.Forms.Platform.UWP
{
public class ImageButtonRenderer : ViewRenderer<ImageButton, FormsButton>, IImageVisualElementRenderer
{
bool _measured;
bool _disposed;
WImage _image;
FormsButton _formsButton;
public ImageButtonRenderer() : base()
{
ImageElementManager.Init(this);
}
protected override void Dispose(bool disposing)
{
if (_disposed)
{
return;
}
_disposed = true;
if (disposing)
{
ImageElementManager.Dispose(this);
if (Control != null)
{
_image.ImageOpened -= OnImageOpened;
_image.ImageFailed -= OnImageFailed;
}
}
base.Dispose(disposing);
}
public override SizeRequest GetDesiredSize(double widthConstraint, double heightConstraint)
{
if (_image?.Source == null)
return new SizeRequest();
_measured = true;
// The size needs to be the entire size needed for the button (including padding, borders, etc.)
// Not just the size of the image.
var btn = Control;
btn.Measure(new Windows.Foundation.Size(widthConstraint, heightConstraint));
var size = new Size(Math.Ceiling(btn.DesiredSize.Width), Math.Ceiling(btn.DesiredSize.Height));
return new SizeRequest(size);
}
protected async override void OnElementChanged(ElementChangedEventArgs<ImageButton> e)
{
base.OnElementChanged(e);
if (e.NewElement != null)
{
if (Control == null)
{
_formsButton = new FormsButton();
_formsButton.Padding = new WThickness(0);
_formsButton.BorderThickness = new WThickness(0);
_formsButton.Background = null;
_image = new Windows.UI.Xaml.Controls.Image()
{
VerticalAlignment = VerticalAlignment.Center,
HorizontalAlignment = HorizontalAlignment.Center,
Stretch = WStretch.Uniform,
};
_image.ImageOpened += OnImageOpened;
_image.ImageFailed += OnImageFailed;
_formsButton.Content = _image;
_formsButton.Click += OnButtonClick;
_formsButton.AddHandler(PointerPressedEvent, new PointerEventHandler(OnPointerPressed), true);
_formsButton.Loaded += ButtonOnLoaded;
SetNativeControl(_formsButton);
}
else
{
WireUpFormsVsm();
}
//TODO: We may want to revisit this strategy later. If a user wants to reset any of these to the default, the UI won't update.
if (Element.IsSet(VisualElement.BackgroundColorProperty) && Element.BackgroundColor != (Color)VisualElement.BackgroundColorProperty.DefaultValue)
UpdateImageButtonBackground();
if (Element.IsSet(VisualElement.BackgroundProperty) && Element.Background != (Brush)VisualElement.BackgroundProperty.DefaultValue)
UpdateImageButtonBackground();
if (Element.IsSet(ImageButton.BorderColorProperty) && Element.BorderColor != (Color)ImageButton.BorderColorProperty.DefaultValue)
UpdateBorderColor();
if (Element.IsSet(ImageButton.BorderWidthProperty) && Element.BorderWidth != (double)ImageButton.BorderWidthProperty.DefaultValue)
UpdateBorderWidth();
if (Element.IsSet(ImageButton.CornerRadiusProperty) && Element.CornerRadius != (int)ImageButton.CornerRadiusProperty.DefaultValue)
UpdateBorderRadius();
// By default Button loads width padding 8, 4, 8 ,4
if (Element.IsSet(Button.PaddingProperty))
UpdatePadding();
await TryUpdateSource().ConfigureAwait(false);
}
}
protected virtual async Task TryUpdateSource()
{
// By default we'll just catch and log any exceptions thrown by UpdateSource so we don't bring down
// the application; a custom renderer can override this method and handle exceptions from
// UpdateSource differently if it wants to
try
{
await UpdateSource().ConfigureAwait(false);
}
catch (Exception ex)
{
Log.Warning(nameof(ImageRenderer), "Error loading image: {0}", ex);
}
finally
{
((IImageController)Element)?.SetIsLoading(false);
}
}
protected async Task UpdateSource()
{
await ImageElementManager.UpdateSource(this).ConfigureAwait(false);
}
void OnImageOpened(object sender, RoutedEventArgs routedEventArgs)
{
if (_measured)
{
ImageElementManager.RefreshImage(this);
}
Element?.SetIsLoading(false);
}
protected virtual void OnImageFailed(object sender, ExceptionRoutedEventArgs exceptionRoutedEventArgs)
{
Log.Warning("Image Loading", $"Image failed to load: {exceptionRoutedEventArgs.ErrorMessage}");
Element?.SetIsLoading(false);
}
void ButtonOnLoaded(object o, RoutedEventArgs routedEventArgs)
{
WireUpFormsVsm();
}
void WireUpFormsVsm()
{
if (Element.UseFormsVsm())
{
InterceptVisualStateManager.Hook(Control.GetFirstDescendant<Windows.UI.Xaml.Controls.Grid>(), Control, Element);
}
}
protected override async void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
{
base.OnElementPropertyChanged(sender, e);
if (e.PropertyName == VisualElement.BackgroundColorProperty.PropertyName || e.PropertyName == VisualElement.BackgroundProperty.PropertyName)
{
UpdateImageButtonBackground();
}
else if (e.PropertyName == ImageButton.BorderColorProperty.PropertyName)
{
UpdateBorderColor();
}
else if (e.PropertyName == ImageButton.BorderWidthProperty.PropertyName)
{
UpdateBorderWidth();
}
else if (e.PropertyName == ImageButton.CornerRadiusProperty.PropertyName)
{
UpdateBorderRadius();
}
else if (e.PropertyName == ImageButton.PaddingProperty.PropertyName)
{
UpdatePadding();
}
else if (e.PropertyName == ImageButton.SourceProperty.PropertyName)
await TryUpdateSource().ConfigureAwait(false);
}
void UpdatePadding()
{
_image.Margin = new WThickness(0);
// Apply the padding to the containing button, not the image
_formsButton.Padding = new WThickness(
Element.Padding.Left,
Element.Padding.Top,
Element.Padding.Right,
Element.Padding.Bottom
);
}
protected override void UpdateBackgroundColor()
{
// Button is a special case; we don't want to set the Control's background
// because it goes outside the bounds of the Border/ContentPresenter,
// which is where we might change the BorderRadius to create a rounded shape.
return;
}
protected override void UpdateBackground()
{
return;
}
protected override bool PreventGestureBubbling { get; set; } = true;
bool IImageVisualElementRenderer.IsDisposed => _disposed;
void OnButtonClick(object sender, RoutedEventArgs e)
{
((IButtonController)Element)?.SendReleased();
((IButtonController)Element)?.SendClicked();
}
void OnPointerPressed(object sender, RoutedEventArgs e)
{
((IButtonController)Element)?.SendPressed();
}
void UpdateImageButtonBackground()
{
if (Brush.IsNullOrEmpty(Element.Background))
Control.BackgroundColor = Element.BackgroundColor != Color.Default ? Element.BackgroundColor.ToBrush() : (WBrush)Windows.UI.Xaml.Application.Current.Resources["ButtonBackgroundThemeBrush"];
else
Control.BackgroundColor = Element.Background.ToBrush();
}
void UpdateBorderColor()
{
Control.BorderBrush = Element.BorderColor != Color.Default ? Element.BorderColor.ToBrush() : (WBrush)Windows.UI.Xaml.Application.Current.Resources["ButtonBorderThemeBrush"];
}
void UpdateBorderRadius()
{
Control.BorderRadius = Element.CornerRadius;
}
void UpdateBorderWidth()
{
Control.BorderThickness = Element.BorderWidth == (double)ImageButton.BorderWidthProperty.DefaultValue ? new WThickness(3) : new WThickness(Element.BorderWidth);
}
void IImageVisualElementRenderer.SetImage(Windows.UI.Xaml.Media.ImageSource image)
{
_image.Source = image;
}
WImage IImageVisualElementRenderer.GetImage() =>
Control?.Content as WImage;
}
}