Skip to content
This repository has been archived by the owner on May 1, 2024. It is now read-only.

Commit

Permalink
merge tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
PureWeen committed Apr 29, 2019
1 parent b3927b6 commit 37c22eb
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 129 deletions.
119 changes: 0 additions & 119 deletions Xamarin.Forms.Platform.MacOS/Extensions/NSImageExtensions.cs
Expand Up @@ -22,124 +22,5 @@ public static NSImage ResizeTo(this NSImage self, CoreGraphics.CGSize newSize)
resizedImage.UnlockFocus();
return resizedImage;
}

internal static async Task<NSImage> GetNativeImageAsync(this ImageSource source, CancellationToken cancellationToken = default(CancellationToken))
{
if (source == null || source.IsEmpty)
return null;

var handler = Internals.Registrar.Registered.GetHandlerForObject<IImageSourceHandler>(source);
if (handler == null)
return null;

try
{
return await handler.LoadImageAsync(source, scale: (float)NSScreen.MainScreen.BackingScaleFactor, cancelationToken: cancellationToken);
}
catch (OperationCanceledException)
{
Log.Warning("Image loading", "Image load cancelled");
}
catch (Exception ex)
{
Log.Warning("Image loading", $"Image load failed: {ex}");
}

return null;
}

internal static Task ApplyNativeImageAsync(this IVisualElementRenderer renderer, BindableProperty imageSourceProperty, Action<NSImage> onSet, Action<bool> onLoading = null, CancellationToken cancellationToken = default(CancellationToken))
{
return renderer.ApplyNativeImageAsync(null, imageSourceProperty, onSet, onLoading, cancellationToken);
}

internal static async Task ApplyNativeImageAsync(this IVisualElementRenderer renderer, BindableObject bindable, BindableProperty imageSourceProperty, Action<NSImage> onSet, Action<bool> onLoading = null, CancellationToken cancellationToken = default(CancellationToken))
{
_ = renderer ?? throw new ArgumentNullException(nameof(renderer));
_ = imageSourceProperty ?? throw new ArgumentNullException(nameof(imageSourceProperty));
_ = onSet ?? throw new ArgumentNullException(nameof(onSet));

// TODO: it might be good to make sure the renderer has not been disposed

// makse sure things are good before we start
var element = bindable ?? renderer.Element;

var nativeRenderer = renderer as IVisualNativeElementRenderer;

if (element == null || renderer.NativeView == null || (nativeRenderer != null && nativeRenderer.Control == null))
return;

onLoading?.Invoke(true);
if (element.GetValue(imageSourceProperty) is ImageSource initialSource && !initialSource.IsEmpty)
{
try
{
using (var drawable = await initialSource.GetNativeImageAsync(cancellationToken))
{
// TODO: it might be good to make sure the renderer has not been disposed

// we are back, so update the working element
element = bindable ?? renderer.Element;

// makse sure things are good now that we are back
if (element == null || renderer.NativeView == null || (nativeRenderer != null && nativeRenderer.Control == null))
return;

// only set if we are still on the same image
if (element.GetValue(imageSourceProperty) == initialSource)
onSet(drawable);
}
}
finally
{
if (element != null && onLoading != null)
{
// only mark as finished if we are still on the same image
if (element.GetValue(imageSourceProperty) == initialSource)
onLoading.Invoke(false);
}
}
}
else
{
onSet(null);
onLoading?.Invoke(false);
}
}

internal static async Task ApplyNativeImageAsync(this BindableObject bindable, BindableProperty imageSourceProperty, Action<NSImage> onSet, Action<bool> onLoading = null, CancellationToken cancellationToken = default(CancellationToken))
{
_ = bindable ?? throw new ArgumentNullException(nameof(bindable));
_ = imageSourceProperty ?? throw new ArgumentNullException(nameof(imageSourceProperty));
_ = onSet ?? throw new ArgumentNullException(nameof(onSet));

onLoading?.Invoke(true);
if (bindable.GetValue(imageSourceProperty) is ImageSource initialSource)
{
try
{
using (var nsimage = await initialSource.GetNativeImageAsync(cancellationToken))
{
// only set if we are still on the same image
if (bindable.GetValue(imageSourceProperty) == initialSource)
onSet(nsimage);
}
}
finally
{
if (onLoading != null)
{
// only mark as finished if we are still on the same image
if (bindable.GetValue(imageSourceProperty) == initialSource)
onLoading.Invoke(false);
}
}
}
else
{
onSet(null);
onLoading?.Invoke(false);
}
}
}
}
4 changes: 2 additions & 2 deletions Xamarin.Forms.Platform.Tizen/Shell/ShellRenderer.cs
Expand Up @@ -156,9 +156,9 @@ void BuildMenu()
else if (flyoutGroup[j] is MenuItem menuItem)
{
title = menuItem.Text;
if (menuItem.Icon != null)
if (menuItem.IconImageSource is FileImageSource source)
{
icon = menuItem.Icon.File;
icon = source.File;
}
}

Expand Down
18 changes: 10 additions & 8 deletions Xamarin.Forms.Platform.iOS/Renderers/ImageElementManager.cs
Expand Up @@ -3,7 +3,6 @@
using System.Threading;
using System.Threading.Tasks;
using Foundation;
using UIKit;
using Xamarin.Forms.Internals;

#if __MOBILE__
Expand Down Expand Up @@ -165,12 +164,14 @@ internal static async Task<NativeImage> GetNativeImageAsync(this ImageSource sou
try
{


#if __MOBILE__
float scale = (float)UIScreen.MainScreen.Scale;
float scale = (float)UIScreen.MainScreen.Scale;
#else
float scale = (float)NSScreen.MainScreen.BackingScaleFactor;
float scale = (float)NSScreen.MainScreen.BackingScaleFactor;
#endif
return await handler.LoadImageAsync(source, scale: (float)UIScreen.MainScreen.Scale, cancelationToken: cancellationToken);

return await handler.LoadImageAsync(source, scale: scale, cancelationToken: cancellationToken);
}
catch (OperationCanceledException)
{
Expand All @@ -184,20 +185,21 @@ internal static async Task<NativeImage> GetNativeImageAsync(this ImageSource sou
return null;
}

#if __MOBILE__
internal static Task ApplyNativeImageAsync(this IShellContext shellContext, BindableObject bindable, BindableProperty imageSourceProperty, Action<UIImage> onSet, Action<bool> onLoading = null, CancellationToken cancellationToken = default(CancellationToken))
{
_ = shellContext ?? throw new ArgumentNullException(nameof(shellContext));
var renderer = shellContext as IVisualElementRenderer ?? throw new InvalidOperationException($"The shell context {shellContext.GetType()} must be a {typeof(IVisualElementRenderer)}.");

return renderer.ApplyNativeImageAsync(bindable, imageSourceProperty, onSet, onLoading, cancellationToken);
}

internal static Task ApplyNativeImageAsync(this IVisualElementRenderer renderer, BindableProperty imageSourceProperty, Action<UIImage> onSet, Action<bool> onLoading = null, CancellationToken cancellationToken = default(CancellationToken))
#endif
internal static Task ApplyNativeImageAsync(this IVisualElementRenderer renderer, BindableProperty imageSourceProperty, Action<NativeImage> onSet, Action<bool> onLoading = null, CancellationToken cancellationToken = default(CancellationToken))
{
return renderer.ApplyNativeImageAsync(null, imageSourceProperty, onSet, onLoading, cancellationToken);
}

internal static async Task ApplyNativeImageAsync(this IVisualElementRenderer renderer, BindableObject bindable, BindableProperty imageSourceProperty, Action<UIImage> onSet, Action<bool> onLoading = null, CancellationToken cancellationToken = default(CancellationToken))
internal static async Task ApplyNativeImageAsync(this IVisualElementRenderer renderer, BindableObject bindable, BindableProperty imageSourceProperty, Action<NativeImage> onSet, Action<bool> onLoading = null, CancellationToken cancellationToken = default(CancellationToken))
{
_ = renderer ?? throw new ArgumentNullException(nameof(renderer));
_ = imageSourceProperty ?? throw new ArgumentNullException(nameof(imageSourceProperty));
Expand Down Expand Up @@ -251,7 +253,7 @@ internal static async Task ApplyNativeImageAsync(this IVisualElementRenderer ren
}
}

internal static async Task ApplyNativeImageAsync(this BindableObject bindable, BindableProperty imageSourceProperty, Action<UIImage> onSet, Action<bool> onLoading = null, CancellationToken cancellationToken = default(CancellationToken))
internal static async Task ApplyNativeImageAsync(this BindableObject bindable, BindableProperty imageSourceProperty, Action<NativeImage> onSet, Action<bool> onLoading = null, CancellationToken cancellationToken = default(CancellationToken))
{
_ = bindable ?? throw new ArgumentNullException(nameof(bindable));
_ = imageSourceProperty ?? throw new ArgumentNullException(nameof(imageSourceProperty));
Expand Down

0 comments on commit 37c22eb

Please sign in to comment.