Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Apply ForegroundScale per platform #112

Merged
merged 9 commits into from
Apr 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion doc/toc.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
items:
- name: Using Uno Resizetizer
href: using-uno-resizetizer.md
href: using-uno-resizetizer.md
- name: Properties avaliable on Uno Resizetizer
href: uno-resizetizer-properties.md
53 changes: 53 additions & 0 deletions doc/uno-resizetizer-properties.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
---
uid: Overview.Uno.Resizetizer.Properties
---

# Resizetizer Properties

Resizetizer has support for three items they are:

* UnoIcon
* UnoImage
* UnoSplashScreen

Each of these items have properties that allow you to influence the behavior of the generated asset, at this page you can find all available properties for each item their meaning.

## Global Properties

Properties that can be used across all items

| Property Name | Description |
| ------------- | ----------------------------------------------------------------------------------------------------------------------------------- |
| BaseSize | Used to specify the size that will be used as a basement for the resize operations. e.g.: `BaseSize="48,48"` |
| Link | Used to specify a custom path for your image, this path should be used inside your application when you want to reference the image |
| Resize | Boolean value to say if the asset should Resized or not. By default just vectors asset are resized by default |
| TintColor | Color that will be used to tint the image during the resize phase. You can use a Hex value or a named value like `Fuchsia` |
| Color | Color that will be used as a background color |

## UnoIcon

| Property Name | Description |
| ---------------------- | ------------------------------------------------------------------------------------------------------------------------------------- |
| Include | Used to insert the path of the Background image. |
| ForegroundFile | Used to insert the path of the Foreground image |
| ForegroundScale | Used to rescale the Foreground image, in order to fit on the app icon, it's a percentage value so `0.33` will be translatated as 33%. |
| AndroidForegroundScale | The same as ForegroundScale, but the value will be applied just for Android. |
| WasmForegroundScale | The same as ForegroundScale, but the value will be applied just for Wasm |
| WindowsForegroundScale | The same as ForegroundScale, but the value will be applied just for Windows |
| IOSForegroundScale | The same as ForegroundScale, but the value will be applied just for iOS |
| SkiaForegroundScale | The same as ForegroundScale, but the value will be applied just for Skia targets |

> [!NOTE]
> The PlatformsForegroundScale (AndroidForegroundScale, WasmForegroundScale, etc) will override the global ForegroundScale value.

## UnoImage

| Property Name | Description |
| ------------- | ----------- |
| Include | Used to insert the path of the image asset, could be a `png` or `svg` |

## UnoSplashScreen

| Property Name | Description |
| ------------- | ----------- |
| Include | Used to insert the path of the image asset, could be a `png` or `svg` |
62 changes: 50 additions & 12 deletions src/Resizetizer/src/ResizeImageInfo.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
#nullable enable
using Microsoft.Build.Framework;
using SkiaSharp;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using Microsoft.Build.Framework;
using SkiaSharp;

namespace Uno.Resizetizer
{
Expand Down Expand Up @@ -91,9 +91,9 @@ public static List<ResizeImageInfo> Parse(IEnumerable<ITaskItem>? images)
}
}

info.BaseSize = Utils.ParseSizeString(image.GetMetadata("BaseSize"));
info.BaseSize = Utils.ParseSizeString(image.GetMetadata(nameof(BaseSize)));

if (bool.TryParse(image.GetMetadata("Resize"), out var rz))
if (bool.TryParse(image.GetMetadata(nameof(Resize)), out var rz))
{
info.Resize = rz;
}
Expand All @@ -103,20 +103,20 @@ public static List<ResizeImageInfo> Parse(IEnumerable<ITaskItem>? images)
info.Resize = false;
}

var tintColor = image.GetMetadata("TintColor");
var tintColor = image.GetMetadata(nameof(TintColor));
info.TintColor = Utils.ParseColorString(tintColor);
if (info.TintColor is null && !string.IsNullOrEmpty(tintColor))
throw new InvalidDataException($"Unable to parse color value '{tintColor}' for '{info.Filename}'.");

var color = image.GetMetadata("Color");
var color = image.GetMetadata(nameof(Color));
info.Color = Utils.ParseColorString(color);
if (info.Color is null && !string.IsNullOrEmpty(color))
throw new InvalidDataException($"Unable to parse color value '{color}' for '{info.Filename}'.");

if (bool.TryParse(image.GetMetadata("IsAppIcon"), out var iai))
if (bool.TryParse(image.GetMetadata(nameof(IsAppIcon)), out var iai))
info.IsAppIcon = iai;

if (float.TryParse(image.GetMetadata("ForegroundScale"),NumberStyles.Number, CultureInfo.InvariantCulture, out var fsc))
if (float.TryParse(image.GetMetadata(nameof(ForegroundScale)), NumberStyles.Number, CultureInfo.InvariantCulture, out var fsc))
info.ForegroundScale = fsc;

var fgFile = image.GetMetadata("ForegroundFile");
Expand All @@ -129,11 +129,16 @@ public static List<ResizeImageInfo> Parse(IEnumerable<ITaskItem>? images)
info.ForegroundFilename = fgFileInfo.FullName;
}

// make sure the image is a foreground if this is an icon
if (info.IsAppIcon && string.IsNullOrEmpty(info.ForegroundFilename))
if (info.IsAppIcon)
{
info.ForegroundFilename = info.Filename;
info.Filename = null;
// make sure the image is a foreground if this is an icon
if (string.IsNullOrEmpty(info.ForegroundFilename))
{
info.ForegroundFilename = info.Filename;
info.Filename = null;
}

ApplyPlatformForegroundScale(image, info);
}

// TODO:
Expand All @@ -144,5 +149,38 @@ public static List<ResizeImageInfo> Parse(IEnumerable<ITaskItem>? images)

return r;
}

static void SetPlatformForegroundScale(ITaskItem image, string property, ResizeImageInfo info)
{
if (double.TryParse(image.GetMetadata(property), NumberStyles.Number,
CultureInfo.InvariantCulture, out var result))
{
info.ForegroundScale = result;
}
}


static void ApplyPlatformForegroundScale(ITaskItem image, ResizeImageInfo info)
{
switch (ResizetizeImages_v0.TargetPlatform)
{
case "android":
SetPlatformForegroundScale(image, "AndroidForegroundScale", info);
break;
case "ios" :
SetPlatformForegroundScale(image, "IOSForegroundScale", info);
break;
case "uwp":
SetPlatformForegroundScale(image, "WindowsForegroundScale", info);
break;
case "wasm":
SetPlatformForegroundScale(image, "WasmForegroundScale", info);
break;
//skia
case "netstandard" or "wpf":
SetPlatformForegroundScale(image, "SkiaForegroundScale", info);
break;
}
}
}
}
3 changes: 3 additions & 0 deletions src/Resizetizer/src/ResizetizeImages.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ public class ResizetizeImages_v0 : UnoAsyncTask, ILogger

public string InputsFile { get; set; }

internal static string TargetPlatform { get; private set; }

public ITaskItem[] Images { get; set; }

[Output]
Expand All @@ -45,6 +47,7 @@ public class ResizetizeImages_v0 : UnoAsyncTask, ILogger

public override System.Threading.Tasks.Task ExecuteAsync()
{
TargetPlatform = PlatformType;
var images = ResizeImageInfo.Parse(Images);

var dpis = DpiPath.GetDpis();
Expand Down