Skip to content

Commit

Permalink
feat: [Skia] Add scaled images support
Browse files Browse the repository at this point in the history
  • Loading branch information
jeromelaban committed Jul 31, 2020
1 parent 740ba70 commit 9117af9
Showing 1 changed file with 54 additions and 4 deletions.
58 changes: 54 additions & 4 deletions src/Uno.UI/UI/Xaml/Media/Imaging/BitmapImage.skia.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
using System;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Runtime.InteropServices;
using System.Threading.Tasks;
using System.Transactions;
using Windows.Foundation;
using Windows.Foundation.Metadata;
using Windows.Graphics.Display;
using Windows.UI.Composition;

namespace Windows.UI.Xaml.Media.Imaging
Expand Down Expand Up @@ -40,10 +42,7 @@ private async Task<ImageData> TryOpenSourceAsync(int? targetWidth, int? targetHe
{
var path = UriSource.PathAndQuery;

var filePath = global::System.IO.Path.Combine(
Windows.Application­Model.Package.Current.Installed­Location.Path,
path.TrimStart('/').Replace('/', global::System.IO.Path.DirectorySeparatorChar)
);
var filePath = GetScaledPath(path);

using var fileStream = File.OpenRead(filePath);

Expand Down Expand Up @@ -89,5 +88,56 @@ private protected override bool TryOpenSourceSync(int? targetWidth, int? targetH

return base.TryOpenSourceSync(targetWidth, targetHeight, out image);
}

private static int[] KnownScales =
{
(int)ResolutionScale.Scale100Percent,
(int)ResolutionScale.Scale120Percent,
(int)ResolutionScale.Scale125Percent,
(int)ResolutionScale.Scale140Percent,
(int)ResolutionScale.Scale150Percent,
(int)ResolutionScale.Scale160Percent,
(int)ResolutionScale.Scale175Percent,
(int)ResolutionScale.Scale180Percent,
(int)ResolutionScale.Scale200Percent,
(int)ResolutionScale.Scale225Percent,
(int)ResolutionScale.Scale250Percent,
(int)ResolutionScale.Scale300Percent,
(int)ResolutionScale.Scale350Percent,
(int)ResolutionScale.Scale400Percent,
(int)ResolutionScale.Scale450Percent,
(int)ResolutionScale.Scale500Percent
};

private static string GetScaledPath(string rawPath)
{
var originalLocalPath =
Path.Combine(Windows.Application­Model.Package.Current.Installed­Location.Path,
rawPath.TrimStart('/').Replace('/', global::System.IO.Path.DirectorySeparatorChar)
);

var resolutionScale = (int)DisplayInformation.GetForCurrentView().ResolutionScale;

var baseDirectory = Path.GetDirectoryName(originalLocalPath);
var baseFileName = Path.GetFileNameWithoutExtension(originalLocalPath);
var baseExtension = Path.GetExtension(originalLocalPath);

for (var i = KnownScales.Length - 1; i >= 0; i--)
{
var probeScale = KnownScales[i];

if (resolutionScale >= probeScale)
{
var filePath = Path.Combine(baseDirectory, $"{baseFileName}.scale-{probeScale}{baseExtension}");

if (File.Exists(filePath))
{
return filePath;
}
}
}

return originalLocalPath;
}
}
}

0 comments on commit 9117af9

Please sign in to comment.