summaryrefslogtreecommitdiff
path: root/Xamarin.Forms.Platform.MacOS/Renderers/ImageRenderer.cs
diff options
context:
space:
mode:
Diffstat (limited to 'Xamarin.Forms.Platform.MacOS/Renderers/ImageRenderer.cs')
-rw-r--r--Xamarin.Forms.Platform.MacOS/Renderers/ImageRenderer.cs117
1 files changed, 117 insertions, 0 deletions
diff --git a/Xamarin.Forms.Platform.MacOS/Renderers/ImageRenderer.cs b/Xamarin.Forms.Platform.MacOS/Renderers/ImageRenderer.cs
new file mode 100644
index 0000000..090df9a
--- /dev/null
+++ b/Xamarin.Forms.Platform.MacOS/Renderers/ImageRenderer.cs
@@ -0,0 +1,117 @@
+´╗┐using System;
+using System.ComponentModel;
+using AppKit;
+
+namespace Xamarin.Forms.Platform.MacOS
+{
+ public class ImageRenderer : ViewRenderer<Image, NSImageView>
+ {
+ bool _isDisposed;
+
+ protected override void Dispose(bool disposing)
+ {
+ if (_isDisposed)
+ return;
+
+ if (disposing)
+ {
+ NSImage oldUIImage;
+ if (Control != null && (oldUIImage = Control.Image) != null)
+ {
+ oldUIImage.Dispose();
+ }
+ }
+
+ _isDisposed = true;
+
+ base.Dispose(disposing);
+ }
+
+ protected override void OnElementChanged(ElementChangedEventArgs<Image> e)
+ {
+ if (Control == null)
+ {
+ var imageView = new FormsNSImageView();
+ SetNativeControl(imageView);
+ }
+
+ if (e.NewElement != null)
+ {
+ SetAspect();
+ SetImage(e.OldElement);
+ SetOpacity();
+ }
+
+ base.OnElementChanged(e);
+ }
+
+ protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
+ {
+ base.OnElementPropertyChanged(sender, e);
+ if (e.PropertyName == Image.SourceProperty.PropertyName)
+ SetImage();
+ else if (e.PropertyName == Image.IsOpaqueProperty.PropertyName)
+ SetOpacity();
+ else if (e.PropertyName == Image.AspectProperty.PropertyName)
+ SetAspect();
+ }
+
+ void SetAspect()
+ {
+ //TODO: Implement set Image Aspect
+ //Control.ContentMode = Element.Aspect.ToUIViewContentMode();
+ }
+
+ async void SetImage(Image oldElement = null)
+ {
+ var source = Element.Source;
+
+ if (oldElement != null)
+ {
+ var oldSource = oldElement.Source;
+ if (Equals(oldSource, source))
+ return;
+
+ var imageSource = oldSource as FileImageSource;
+ if (imageSource != null && source is FileImageSource && imageSource.File == ((FileImageSource)source).File)
+ return;
+
+ Control.Image = null;
+ }
+
+ IImageSourceHandler handler;
+
+ ((IImageController)Element).SetIsLoading(true);
+
+ if (source != null && (handler = Registrar.Registered.GetHandler<IImageSourceHandler>(source.GetType())) != null)
+ {
+ NSImage uiimage;
+ try
+ {
+ uiimage = await handler.LoadImageAsync(source, scale: (float)NSScreen.MainScreen.BackingScaleFactor);
+ }
+ catch (OperationCanceledException)
+ {
+ uiimage = null;
+ }
+
+ var imageView = Control;
+ if (imageView != null)
+ imageView.Image = uiimage;
+
+ if (!_isDisposed)
+ ((IVisualElementController)Element).NativeSizeChanged();
+ }
+ else
+ Control.Image = null;
+
+ if (!_isDisposed)
+ ((IImageController)Element).SetIsLoading(false);
+ }
+
+ void SetOpacity()
+ {
+ (Control as FormsNSImageView)?.SetIsOpaque(Element.IsOpaque);
+ }
+ }
+} \ No newline at end of file