Skip to content

Commit

Permalink
feat(wasm): Support ms-appdata for Image
Browse files Browse the repository at this point in the history
  • Loading branch information
Youssef1313 committed May 23, 2023
1 parent 4e63502 commit 4b48a50
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
using Windows.Foundation;
using Windows.Foundation.Metadata;
using Windows.Graphics.Display;
using Windows.Storage;
using Windows.UI;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
Expand Down Expand Up @@ -434,6 +435,19 @@ void Image_ImageOpened(object sender, RoutedEventArgs e)
Assert.AreEqual("Image_ImageOpened", logs[1]);
}

[TestMethod]
[RunsOnUIThread]
public async Task When_Loaded_From_AppData_LocalFolder()
{
var file = await StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx:///Assets/test_image_100_150.png"));
await file.CopyAsync(ApplicationData.Current.LocalFolder, "newfile.png", NameCollisionOption.ReplaceExisting);
var uri = new Uri($"ms-appdata:///Local/newfile.png");
var bitmapImage = new BitmapImage(uri);
var image = new Image() { Source = bitmapImage };
TestServices.WindowHelper.WindowContent = image;
await WindowHelper.WaitForLoaded(image);
}

private async Task<RawBitmap> TakeScreenshot(FrameworkElement SUT)
{
var renderer = new RenderTargetBitmap();
Expand Down
21 changes: 19 additions & 2 deletions src/Uno.UI/UI/Xaml/Media/ImageSource.wasm.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
#nullable enable

using Uno.Extensions;
using System;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using Windows.Storage.Streams;
using Uno.Extensions;
using Uno.Helpers;
using Uno.UI.Xaml.Media;
using Windows.Storage;
using Windows.Storage.Streams;

#if !IS_UNO
using Uno.Web.Query;
Expand Down Expand Up @@ -57,5 +60,19 @@ internal virtual void ReportImageFailed(string errorMessage)

}

internal virtual string? ContentType { get; }

internal async Task<ImageData> OpenMsAppData(Uri uri, CancellationToken ct)
{
var file = await StorageFile.GetFileFromPathAsync(AppDataUriEvaluator.ToPath(uri));
var stream = await file.OpenAsync(FileAccessMode.Read);

var streamWithContentType = ContentType is { } contentType
? stream.TrySetContentType(ContentType)
: stream.TrySetContentType();

return await OpenFromStream(streamWithContentType, null, ct);
}

}
}
12 changes: 8 additions & 4 deletions src/Uno.UI/UI/Xaml/Media/Imaging/BitmapImage.wasm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public sealed partial class BitmapImage : BitmapSource
{
internal ResolutionScale? ScaleOverride { get; set; }

internal string ContentType { get; set; } = "application/octet-stream";
internal override string ContentType { get; } = "application/octet-stream";

private protected override bool TryOpenSourceAsync(
CancellationToken ct,
Expand All @@ -43,7 +43,7 @@ private protected override bool TryOpenSourceAsync(
_ => uri
};

asyncImage = AssetResolver.ResolveImageAsync(this, newUri, ScaleOverride);
asyncImage = AssetResolver.ResolveImageAsync(this, newUri, ScaleOverride, ct);

return true;
}
Expand Down Expand Up @@ -91,7 +91,7 @@ private static async Task<HashSet<string>> GetAssets()
return new HashSet<string>(Regex.Split(assets, "\r\n|\r|\n"));
}

internal static async Task<ImageData> ResolveImageAsync(ImageSource source, Uri uri, ResolutionScale? scaleOverride)
internal static async Task<ImageData> ResolveImageAsync(ImageSource source, Uri uri, ResolutionScale? scaleOverride, CancellationToken ct)
{
try
{
Expand All @@ -104,7 +104,11 @@ internal static async Task<ImageData> ResolveImageAsync(ImageSource source, Uri
return ImageData.FromUrl(uri, source);
}

// TODO: Implement ms-appdata
if (uri.IsAppData())
{
return await source.OpenMsAppData(uri, ct);
}

return ImageData.Empty;
}

Expand Down
12 changes: 1 addition & 11 deletions src/Uno.UI/UI/Xaml/Media/Imaging/SvgImageSource.wasm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ namespace Windows.UI.Xaml.Media.Imaging
{
partial class SvgImageSource
{
internal string ContentType { get; set; } = "image/svg+xml";
internal override string ContentType { get; } = "image/svg+xml";

partial void InitPartial()
{
Expand Down Expand Up @@ -88,16 +88,6 @@ private protected override bool TryOpenSourceAsync(
return false;
}

private protected async Task<ImageData> OpenMsAppData(Uri uri, CancellationToken ct)
{
var file = await StorageFile.GetFileFromPathAsync(AppDataUriEvaluator.ToPath(uri));
var stream = await file.OpenAsync(FileAccessMode.Read);

var streamWithContentType = stream.TrySetContentType(ContentType);

return await OpenFromStream(streamWithContentType, null, ct);
}

internal override void ReportImageLoaded() => RaiseImageOpened();

internal override void ReportImageFailed(string errorMessage) => RaiseImageFailed(SvgImageSourceLoadStatus.Other);
Expand Down

0 comments on commit 4b48a50

Please sign in to comment.