Skip to content

Commit

Permalink
Update ImageSourceService (dotnet#454)
Browse files Browse the repository at this point in the history
  • Loading branch information
myroot committed Aug 24, 2022
1 parent 9690c4d commit d91d8b7
Show file tree
Hide file tree
Showing 18 changed files with 174 additions and 93 deletions.
Original file line number Diff line number Diff line change
@@ -1,13 +1,44 @@
using System;
using Microsoft.Maui.Controls.Internals;
using static Microsoft.Maui.Controls.Button;
using TButton = Tizen.UIExtensions.NUI.Button;

namespace Microsoft.Maui.Controls.Platform
{
public static class ButtonExtensions
{
public static void UpdateText(this TButton platformButton, Button button)
{
var text = TextTransformUtilites.GetTransformedText(button.Text, button.TextTransform);
platformButton.Text = text;
}

public static void UpdateContentLayout(this TButton nativeButton, Button button)
{
//TODO : Need to impl
if (button.ImageSource != null)
{
var contentLayout = button.ContentLayout;
nativeButton.IconPadding = (ushort)Math.Max(contentLayout.Spacing.ToScaledPixel(), 0);
switch (contentLayout.Position)
{
case ButtonContentLayout.ImagePosition.Top:
nativeButton.IconRelativeOrientation = TButton.IconOrientation.Top;
break;
case ButtonContentLayout.ImagePosition.Bottom:
nativeButton.IconRelativeOrientation = TButton.IconOrientation.Bottom;
break;
case ButtonContentLayout.ImagePosition.Left:
nativeButton.IconRelativeOrientation = TButton.IconOrientation.Left;
break;
case ButtonContentLayout.ImagePosition.Right:
nativeButton.IconRelativeOrientation = TButton.IconOrientation.Right;
break;
}
}
else
{
nativeButton.IconPadding = 0;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using TButton = Tizen.UIExtensions.NUI.Button;
using TColor = Tizen.UIExtensions.Common.Color;
using TDeviceInfo = Tizen.UIExtensions.Common.DeviceInfo;
using TImage = Tizen.UIExtensions.NUI.Image;
using TMaterialIconButton = Tizen.UIExtensions.NUI.GraphicsView.MaterialIconButton;

namespace Microsoft.Maui.Controls.Platform
Expand Down Expand Up @@ -48,7 +49,13 @@ public static void UpdateTitleIcon(this MauiToolbar platformToolbar, Toolbar too
source.LoadImage(toolbar.Handler.MauiContext, (result) =>
{
if (result?.Value != null)
platformToolbar.Icon = result.Value;
{
var image = new TImage
{
ResourceUrl = result.Value.ResourceUrl,
};
platformToolbar.Icon = image;
}
});
}

Expand Down
3 changes: 1 addition & 2 deletions src/Core/src/Handlers/Button/ButtonHandler.Tizen.cs
Original file line number Diff line number Diff line change
Expand Up @@ -108,12 +108,11 @@ void OnButtonPressed(object? sender, EventArgs e)
VirtualView?.Pressed();
}

void OnSetImageSource(ImageView? image)
void OnSetImageSource(MauiImageSource? image)
{
if (image == null)
return;
PlatformView.Icon.ResourceUrl = image.ResourceUrl;
image.Dispose();
}
}
}
7 changes: 5 additions & 2 deletions src/Core/src/Handlers/Image/ImageHandler.Tizen.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,12 @@ public static void MapSource(IImageHandler handler, IImage image) =>
public static Task MapSourceAsync(IImageHandler handler, IImage image) =>
handler.SourceLoader.UpdateImageSourceAsync();

void OnSetImageSource(Image? obj)
void OnSetImageSource(MauiImageSource? obj)
{
// Empty on purpose
if (obj == null)
return;

PlatformView.ResourceUrl = obj.ResourceUrl;
}
}
}
8 changes: 5 additions & 3 deletions src/Core/src/Handlers/ImageButton/ImageButtonHandler.Tizen.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using Tizen.UIExtensions.NUI;

namespace Microsoft.Maui.Handlers
{
Expand Down Expand Up @@ -53,9 +52,12 @@ void OnClicked(object? sender, EventArgs e)
VirtualView?.Clicked();
}

void OnSetImageSource(Image? img)
void OnSetImageSource(MauiImageSource? img)
{
//Empty on purpose
if (img == null)
return;

PlatformView.ResourceUrl = img.ResourceUrl;
}
}
}
2 changes: 1 addition & 1 deletion src/Core/src/Handlers/ImageButton/ImageButtonHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
using PlatformImageView = Microsoft.UI.Xaml.Controls.Image;
using PlatformView = Microsoft.UI.Xaml.Controls.Button;
#elif TIZEN
using PlatformImage = Tizen.UIExtensions.NUI.Image;
using PlatformImage = Microsoft.Maui.Platform.MauiImageSource;
using PlatformImageView = Tizen.UIExtensions.NUI.Image;
using PlatformView = Microsoft.Maui.Platform.MauiImageButton;
#elif (NETSTANDARD || !PLATFORM) || (NET6_0_OR_GREATER && !IOS && !ANDROID && !TIZEN)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public static void MapBackground(ISwipeItemMenuItemHandler handler, ISwipeItemMe

public static void MapVisibility(ISwipeItemMenuItemHandler handler, ISwipeItemMenuItem view) { }

void OnSetImageSource(NView? obj)
void OnSetImageSource(MauiImageSource? obj)
{
throw new NotImplementedException();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,37 +4,31 @@
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using Tizen.UIExtensions.NUI;
using AppFW = Tizen.Applications;

namespace Microsoft.Maui
{
public partial class FileImageSourceService
{
public override Task<IImageSourceServiceResult<Image>?> GetImageAsync(IImageSource imageSource, Image image, CancellationToken cancellationToken = default) =>
GetImageAsync((IFileImageSource)imageSource, image, cancellationToken);
public override Task<IImageSourceServiceResult<MauiImageSource>?> GetImageAsync(IImageSource imageSource, CancellationToken cancellationToken = default) =>
GetImageAsync((IFileImageSource)imageSource, cancellationToken);

public async Task<IImageSourceServiceResult<Image>?> GetImageAsync(IFileImageSource imageSource, Image image, CancellationToken cancellationToken = default)
public Task<IImageSourceServiceResult<MauiImageSource>?> GetImageAsync(IFileImageSource imageSource, CancellationToken cancellationToken = default)
{
if (imageSource.IsEmpty)
return null;
return FromResult(null);

var filename = imageSource.File;
try
{
if (!string.IsNullOrEmpty(filename))
{
var isLoadComplated = await image.LoadAsync(GetPath(filename));

if (!isLoadComplated)
var image = new MauiImageSource
{
//If it fails, call the Load function to remove the previous image.
image.ResourceUrl = null;
throw new InvalidOperationException("Unable to load image file.");
}

var result = new ImageSourceServiceResult(image);
return result;
ResourceUrl = GetPath(filename)
};
var result = new ImageSourceServiceResult(image, () => image.Dispose());
return FromResult(result);
}
else
{
Expand All @@ -48,6 +42,9 @@ public partial class FileImageSourceService
}
}

static Task<IImageSourceServiceResult<MauiImageSource>?> FromResult(IImageSourceServiceResult<MauiImageSource>? result) =>
Task.FromResult(result);

static string GetPath(string res)
{
if (Path.IsPathRooted(res))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,40 +3,33 @@
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using Tizen.UIExtensions.NUI;
using Tizen.NUI.BaseComponents;

namespace Microsoft.Maui
{
public partial class FontImageSourceService
{
public override Task<IImageSourceServiceResult<Image>?> GetImageAsync(IImageSource imageSource, Image image, CancellationToken cancellationToken = default) =>
GetImageAsync((IFontImageSource)imageSource, image, cancellationToken);
public override Task<IImageSourceServiceResult<MauiImageSource>?> GetImageAsync(IImageSource imageSource, CancellationToken cancellationToken = default) =>
GetImageAsync((IFontImageSource)imageSource, cancellationToken);

public async Task<IImageSourceServiceResult<Image>?> GetImageAsync(IFontImageSource imageSource, Image image, CancellationToken cancellationToken = default)
public Task<IImageSourceServiceResult<MauiImageSource>?> GetImageAsync(IFontImageSource imageSource, CancellationToken cancellationToken = default)
{
if (imageSource.IsEmpty)
return null;
return FromResult(null);

try
{
//TODO : Fix me correctly later.
var isLoadComplated = await image.LoadAsync(string.Empty);

if (!isLoadComplated)
{
throw new InvalidOperationException("Unable to load image file.");
}

var result = new ImageSourceServiceResult(image);

return result;
//TODO : Font Image not support
var image = new MauiImageSource();
return FromResult(new ImageSourceServiceResult(image, () => image.Dispose()));
}
catch (Exception ex)
{
Logger?.LogWarning(ex, "Unable to generate font image '{Glyph}'.", imageSource.Glyph);
throw;
}
}

static Task<IImageSourceServiceResult<MauiImageSource>?> FromResult(IImageSourceServiceResult<MauiImageSource>? result) =>
Task.FromResult(result);
}
}
3 changes: 1 addition & 2 deletions src/Core/src/ImageSources/IImageSourceService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,8 @@ public interface IImageSourceService
float scale = 1,
CancellationToken cancellationToken = default);
#elif TIZEN || __TIZEN__
Task<IImageSourceServiceResult<Tizen.UIExtensions.NUI.Image>?> GetImageAsync(
Task<IImageSourceServiceResult<MauiImageSource>?> GetImageAsync(
IImageSource imageSource,
Tizen.UIExtensions.NUI.Image image,
CancellationToken cancellationToken = default);
#elif WINDOWS
Task<IImageSourceServiceResult<UI.Xaml.Media.ImageSource>?> GetImageSourceAsync(
Expand Down
5 changes: 2 additions & 3 deletions src/Core/src/ImageSources/ImageSourceExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#elif WINDOWS
using PlatformImage = Microsoft.UI.Xaml.Media.ImageSource;
#elif TIZEN
using PlatformImage = Tizen.UIExtensions.NUI.Image;
using PlatformImage = Microsoft.Maui.Platform.MauiImageSource;
#elif (NETSTANDARD || !PLATFORM) || (NET6_0_OR_GREATER && !IOS && !ANDROID && !TIZEN)
using PlatformImage = System.Object;
#endif
Expand Down Expand Up @@ -55,8 +55,7 @@ static async Task LoadImageResult(Task<IImageSourceServiceResult<PlatformImage>?
#elif WINDOWS
return imageSourceService.GetImageSourceAsync(imageSource);
#elif TIZEN
var platformImage = new PlatformImage();
return imageSourceService.GetImageAsync(imageSource, platformImage);
return imageSourceService.GetImageAsync(imageSource);
#else
throw new NotImplementedException();
#endif
Expand Down
3 changes: 1 addition & 2 deletions src/Core/src/ImageSources/ImageSourceService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,8 @@ public ImageSourceService(ILogger? logger = null)
float scale = 1,
CancellationToken cancellationToken = default);
#elif TIZEN || __TIZEN__
public abstract Task<IImageSourceServiceResult<Tizen.UIExtensions.NUI.Image>?> GetImageAsync(
public abstract Task<IImageSourceServiceResult<MauiImageSource>?> GetImageAsync(
IImageSource imageSource,
Tizen.UIExtensions.NUI.Image image,
CancellationToken cancellationToken = default);
#elif WINDOWS
public abstract Task<IImageSourceServiceResult<UI.Xaml.Media.ImageSource>?> GetImageSourceAsync(
Expand Down
2 changes: 1 addition & 1 deletion src/Core/src/ImageSources/ImageSourceServiceResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#elif WINDOWS
using PlatformView = Microsoft.UI.Xaml.Media.ImageSource;
#elif TIZEN
using PlatformView = Tizen.UIExtensions.NUI.Image;
using PlatformView = Microsoft.Maui.Platform.MauiImageSource;
#elif (NETSTANDARD || !PLATFORM) || (NET6_0_OR_GREATER && !IOS && !ANDROID && !TIZEN)
using PlatformView = System.Object;
#endif
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,27 +9,22 @@ namespace Microsoft.Maui
{
public partial class StreamImageSourceService
{
public override Task<IImageSourceServiceResult<Image>?> GetImageAsync(IImageSource imageSource, Image image, CancellationToken cancellationToken = default) =>
GetImageAsync((IStreamImageSource)imageSource, image, cancellationToken);
public override Task<IImageSourceServiceResult<MauiImageSource>?> GetImageAsync(IImageSource imageSource, CancellationToken cancellationToken = default) =>
GetImageAsync((IStreamImageSource)imageSource, cancellationToken);

public async Task<IImageSourceServiceResult<Image>?> GetImageAsync(IStreamImageSource imageSource, Image image, CancellationToken cancellationToken = default)
public async Task<IImageSourceServiceResult<MauiImageSource>?> GetImageAsync(IStreamImageSource imageSource, CancellationToken cancellationToken = default)
{
if (imageSource.IsEmpty)
return null;

try
{
var stream = await imageSource.GetStreamAsync(cancellationToken);
var image = new MauiImageSource();

if (stream == null)
throw new InvalidOperationException("Unable to load image stream.");
await image.LoadSource(stream);

var isLoadComplated = await image.LoadAsync(stream);

if (!isLoadComplated)
throw new InvalidOperationException("Unable to decode image from stream.");

return new ImageSourceServiceResult(image);
return new ImageSourceServiceResult(image, image.Dispose);
}
catch (Exception ex)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,30 +9,33 @@ namespace Microsoft.Maui
{
public partial class UriImageSourceService
{
public override Task<IImageSourceServiceResult<Image>?> GetImageAsync(IImageSource imageSource, Image image, CancellationToken cancellationToken = default) =>
GetImageAsync((IUriImageSource)imageSource, image, cancellationToken);
public override Task<IImageSourceServiceResult<MauiImageSource>?> GetImageAsync(IImageSource imageSource, CancellationToken cancellationToken = default) =>
GetImageAsync((IUriImageSource)imageSource, cancellationToken);

public async Task<IImageSourceServiceResult<Image>?> GetImageAsync(IUriImageSource imageSource, Image image, CancellationToken cancellationToken = default)
public Task<IImageSourceServiceResult<MauiImageSource>?> GetImageAsync(IUriImageSource imageSource, CancellationToken cancellationToken = default)
{
if (imageSource.IsEmpty)
return null;
return FromResult(null);

var uri = imageSource.Uri;

try
{
var isLoadComplated = await image.LoadAsync(uri);

if (!isLoadComplated)
throw new InvalidOperationException($"Unable to load image URI '{uri}'.");
var image = new MauiImageSource
{
ResourceUrl = uri.AbsoluteUri
};

return new ImageSourceServiceResult(image);
var result = new ImageSourceServiceResult(image, image.Dispose);
return FromResult(result);
}
catch (Exception ex)
{
Logger?.LogWarning(ex, "Unable to load image URI '{Uri}'.", uri);
throw;
}
}
static Task<IImageSourceServiceResult<MauiImageSource>?> FromResult(IImageSourceServiceResult<MauiImageSource>? result) =>
Task.FromResult(result);
}
}
Loading

0 comments on commit d91d8b7

Please sign in to comment.