summaryrefslogtreecommitdiff
path: root/Xamarin.Forms.Platform.MacOS/Renderers/ButtonRenderer.cs
diff options
context:
space:
mode:
Diffstat (limited to 'Xamarin.Forms.Platform.MacOS/Renderers/ButtonRenderer.cs')
-rw-r--r--Xamarin.Forms.Platform.MacOS/Renderers/ButtonRenderer.cs131
1 files changed, 131 insertions, 0 deletions
diff --git a/Xamarin.Forms.Platform.MacOS/Renderers/ButtonRenderer.cs b/Xamarin.Forms.Platform.MacOS/Renderers/ButtonRenderer.cs
new file mode 100644
index 0000000..05b87fa
--- /dev/null
+++ b/Xamarin.Forms.Platform.MacOS/Renderers/ButtonRenderer.cs
@@ -0,0 +1,131 @@
+´╗┐using System;
+using System.ComponentModel;
+using AppKit;
+using Foundation;
+using SizeF = CoreGraphics.CGSize;
+
+namespace Xamarin.Forms.Platform.MacOS
+{
+ public class ButtonRenderer : ViewRenderer<Button, NSButton>
+ {
+ protected override void Dispose(bool disposing)
+ {
+ if (Control != null)
+ Control.Activated -= OnButtonActivated;
+
+ base.Dispose(disposing);
+ }
+
+ protected override void OnElementChanged(ElementChangedEventArgs<Button> e)
+ {
+ base.OnElementChanged(e);
+
+ if (e.NewElement != null)
+ {
+ if (Control == null)
+ {
+ var btn = new NSButton();
+ btn.SetButtonType(NSButtonType.MomentaryPushIn);
+ SetNativeControl(btn);
+
+ Control.Activated += OnButtonActivated;
+ }
+
+ UpdateText();
+ UpdateFont();
+ UpdateBorder();
+ UpdateImage();
+ }
+ }
+
+ protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
+ {
+ base.OnElementPropertyChanged(sender, e);
+
+ if (e.PropertyName == Button.TextProperty.PropertyName || e.PropertyName == Button.TextColorProperty.PropertyName)
+ UpdateText();
+ else if (e.PropertyName == Button.FontProperty.PropertyName)
+ UpdateFont();
+ else if (e.PropertyName == Button.BorderWidthProperty.PropertyName ||
+ e.PropertyName == Button.BorderRadiusProperty.PropertyName ||
+ e.PropertyName == Button.BorderColorProperty.PropertyName)
+ UpdateBorder();
+ else if (e.PropertyName == VisualElement.BackgroundColorProperty.PropertyName)
+ UpdateBackgroundVisibility();
+ else if (e.PropertyName == Button.ImageProperty.PropertyName)
+ UpdateImage();
+ }
+
+ void OnButtonActivated(object sender, EventArgs eventArgs)
+ {
+ ((IButtonController)Element)?.SendClicked();
+ }
+
+ void UpdateBackgroundVisibility()
+ {
+ var model = Element;
+ var shouldDrawImage = model.BackgroundColor == Color.Default;
+ if (!shouldDrawImage)
+ Control.Cell.BackgroundColor = model.BackgroundColor.ToNSColor();
+ }
+
+ void UpdateBorder()
+ {
+ var uiButton = Control;
+ var button = Element;
+
+ if (button.BorderColor != Color.Default)
+ uiButton.Layer.BorderColor = button.BorderColor.ToCGColor();
+
+ uiButton.Layer.BorderWidth = (float)button.BorderWidth;
+ uiButton.Layer.CornerRadius = button.BorderRadius;
+
+ UpdateBackgroundVisibility();
+ }
+
+ void UpdateFont()
+ {
+ Control.Font = Element.Font.ToNSFont();
+ }
+
+ async void UpdateImage()
+ {
+ IImageSourceHandler handler;
+ FileImageSource source = Element.Image;
+ if (source != null && (handler = Registrar.Registered.GetHandler<IImageSourceHandler>(source.GetType())) != null)
+ {
+ NSImage uiimage;
+ try
+ {
+ uiimage = await handler.LoadImageAsync(source);
+ }
+ catch (OperationCanceledException)
+ {
+ uiimage = null;
+ }
+ NSButton button = Control;
+ if (button != null && uiimage != null)
+ {
+ button.Image = uiimage;
+ if (!string.IsNullOrEmpty(button.Title))
+ button.ImagePosition = Element.ToNSCellImagePosition();
+ }
+ }
+ ((IVisualElementController)Element).NativeSizeChanged();
+ }
+
+ void UpdateText()
+ {
+ var color = Element.TextColor;
+ if (color == Color.Default)
+ {
+ Control.Title = Element.Text ?? "";
+ }
+ else
+ {
+ var textWithColor = new NSAttributedString(Element.Text ?? "", foregroundColor: color.ToNSColor());
+ Control.AttributedTitle = textWithColor;
+ }
+ }
+ }
+} \ No newline at end of file