Skip to content

Commit

Permalink
[SingleProject] Update splash updator (dotnet#353)
Browse files Browse the repository at this point in the history
* [singleproject] update splash updator

* [singleproject] update icon updator

* [singleproject] Fix default icon path
  • Loading branch information
JoonghyunCho authored and rookiejava committed Feb 10, 2022
1 parent 98fcca2 commit bc8a784
Show file tree
Hide file tree
Showing 3 changed files with 121 additions and 45 deletions.
40 changes: 26 additions & 14 deletions src/SingleProject/Resizetizer/src/DpiPath.cs
Original file line number Diff line number Diff line change
Expand Up @@ -244,24 +244,36 @@ public static DpiPath[] AppIcon
};
}

public static DpiPath[] Tizen
=> new[]
{

public static class Tizen
{
public static DpiPath Original => new DpiPath("res", 1.0m);

public static DpiPath[] Image
=> new[]
{
new DpiPath("res/contents/default_All-LDPI", 0.8m),
new DpiPath("res/contents/default_All-MDPI", 1.0m),
new DpiPath("res/contents/default_All-HDPI", 1.5m),
new DpiPath("res/contents/default_All-XHDPI", 2.0m),
new DpiPath("res/contents/default_All-XXHDPI", 3.0m),
};
};

public static DpiPath[] TizenAppIcon
=> new[]
{
new DpiPath("shared/res", 1.0m, null, ".high", new SKSize(78, 89)),
new DpiPath("shared/res", 1.0m, null, ".xhigh", new SKSize(117, 117)),
};
public static DpiPath[] AppIcon
=> new[]
{
new DpiPath("shared/res/hdpi", 1.0m, null, ".high", new SKSize(78, 78)),
new DpiPath("shared/res/xhdpi", 1.0m, null, ".xhigh", new SKSize(117, 117)),
};

public static DpiPath[] SplashScreen
=> new[]
{
new DpiPath("res/contents/default_All-MDPI", 1.0m),
new DpiPath("res/contents/default_All-HDPI", 1.5m),
};

static DpiPath TizenOriginal => new DpiPath("res", 1.0m);
}

public static DpiPath GetOriginal(string platform)
{
Expand All @@ -276,7 +288,7 @@ public static DpiPath GetOriginal(string platform)
case "wpf":
return DpiPath.Wpf.Original;
case "tizen":
return DpiPath.TizenOriginal;
return DpiPath.Tizen.Original;
}

return null;
Expand All @@ -295,7 +307,7 @@ public static DpiPath[] GetDpis(string platform)
case "wpf":
return DpiPath.Wpf.Image;
case "tizen":
return DpiPath.Tizen;
return DpiPath.Tizen.Image;
}

return null;
Expand All @@ -320,7 +332,7 @@ public static DpiPath[] GetAppIconDpis(string platform, string appIconName)
result = DpiPath.Wpf.AppIcon;
break;
case "tizen":
result = DpiPath.TizenAppIcon;
result = DpiPath.Tizen.AppIcon;
break;
}

Expand Down
43 changes: 37 additions & 6 deletions src/SingleProject/Resizetizer/src/TizenIconManifestUpdater.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
using System;
using System.IO;
using System.Linq;
using System.Xml;

namespace Microsoft.Maui.Resizetizer
{
internal class TizenIconManifestUpdater
{
const string namespaceURI = "http://tizen.org/ns/packages";
const string tizenManifestFile = "tizen-manifest.xml";
const string resourcePath = "shared/res/";
const string defaultDpiType = "xhdpi";

public TizenIconManifestUpdater(string appIconName, DpiPath[] dpis, ILogger logger)
{
Expand All @@ -23,7 +28,7 @@ public TizenIconManifestUpdater(string appIconName, DpiPath[] dpis, ILogger logg
public void Update()
{
XmlDocument doc = new XmlDocument();
var xmlPath = Environment.CurrentDirectory + "\\tizen-manifest.xml";
var xmlPath = Path.Combine(Environment.CurrentDirectory, "platforms", "Tizen", tizenManifestFile);
try
{
doc.Load(xmlPath);
Expand All @@ -42,13 +47,39 @@ public void Update()
Logger.Log($"Failed to find <ui-application>");
return;
}
var IconNode = doc.SelectSingleNode("//manifest:icon", nsmgr);
if (IconNode == null)

var iconNodes = doc.SelectNodes("//manifest:icon", nsmgr);
foreach (XmlElement node in iconNodes)
{
if (node.HasAttribute("dpi") == false)
{
uiApplicationNode.RemoveChild(node);
}
else
{
foreach (var dpi in Dpis)
{
var dpiType = dpi.Path.Replace(resourcePath, "");
if (node.Attributes["dpi"].Value == dpiType)
{
uiApplicationNode.RemoveChild(node);
}
}
}
}

foreach (var dpi in Dpis)
{
IconNode = doc.CreateElement("icon", namespaceURI);
uiApplicationNode.AppendChild(IconNode);
var dpiType = dpi.Path.Replace(resourcePath, "");
var iconNode = doc.CreateElement("icon", namespaceURI);
iconNode.SetAttribute("dpi", dpiType);
iconNode.InnerText = dpiType + "/" + AppIconName + dpi.FileSuffix + ".png";
uiApplicationNode.PrependChild(iconNode);
}
IconNode.InnerText = AppIconName + Dpis[1].FileSuffix + ".png";
var defaultIconNode = doc.CreateElement("icon", namespaceURI);
var defaultDpi = Dpis.Where(n => n.Path.EndsWith(defaultDpiType)).FirstOrDefault();
defaultIconNode.InnerText = defaultDpiType + "/" + AppIconName + defaultDpi.FileSuffix + ".png";
uiApplicationNode.PrependChild(defaultIconNode);

doc.Save(xmlPath);
}
Expand Down
83 changes: 58 additions & 25 deletions src/SingleProject/Resizetizer/src/TizenSplashUpdater.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Xml;
using Microsoft.Build.Framework;
Expand Down Expand Up @@ -30,7 +31,9 @@ public override bool Execute()
const string namespaceURI = "http://tizen.org/ns/packages";
const string splashDirectoryName = "splash";
List<string> orientations = new List<string>() { "portrait", "landscape" };
Dictionary<string, string> splashDpiMap = new Dictionary<string, string>();
Size hdSize = new Size(720, 1080);
Size fhdSize = new Size(1080, 1920);
Dictionary<(string Resolution, string Orientation), string> splashDpiMap = new Dictionary<(string, string), string>();

public bool UpdateSplashImage()
{
Expand All @@ -45,16 +48,19 @@ public bool UpdateSplashImage()
}
Directory.CreateDirectory(splashFullPath);

foreach (var dpi in DpiPath.Tizen)
foreach (var dpi in DpiPath.Tizen.SplashScreen)
{
var imageOutputPath = Path.GetFullPath(Path.Combine(IntermediateOutputPath, dpi.Path));
var imageFullPath = Path.Combine(imageOutputPath, image);
if (File.Exists(imageFullPath))
{
var resolution = dpi.Path.Split('-')[1].ToLower();
var newImage = Path.GetFileNameWithoutExtension(splash.ItemSpec) + "." + resolution + ".png";
splashDpiMap.Add(resolution, $"{splashDirectoryName}/{ newImage }");
UpdateColorAndMoveFile(imageFullPath, Path.Combine(splashFullPath, newImage));
foreach (var orientation in orientations)
{
var newImage = Path.GetFileNameWithoutExtension(splash.ItemSpec) + "." + resolution + "." + orientation + ".png";
splashDpiMap.Add((resolution, orientation), $"{splashDirectoryName}/{ newImage }");
UpdateColorAndMoveFile(GetScreenSize(resolution, orientation), imageFullPath, Path.Combine(splashFullPath, newImage));
}
}
else
{
Expand All @@ -65,7 +71,19 @@ public bool UpdateSplashImage()
return true;
}

public void UpdateColorAndMoveFile(string sourceFilePath, string destFilePath)
Size GetScreenSize(string resolution, string orientation)
{
if (resolution == "mdpi")
{
return orientation == "portrait" ? hdSize : new Size(hdSize.Height, hdSize.Width);
}
else
{
return orientation == "portrait" ? fhdSize : new Size(fhdSize.Height, fhdSize.Width);
}
}

public void UpdateColorAndMoveFile(Size screenSize, string sourceFilePath, string destFilePath)
{
var splash = MauiSplashScreen[0];
var colorMetadata = splash.GetMetadata("Color");
Expand All @@ -80,8 +98,8 @@ public void UpdateColorAndMoveFile(string sourceFilePath, string destFilePath)
}

using (SKBitmap bmp = SKBitmap.Decode(sourceFilePath))
{
SKImageInfo info = new SKImageInfo(bmp.Width, bmp.Height);
{
SKImageInfo info = new SKImageInfo(screenSize.Width, screenSize.Height);
using (SKSurface surface = SKSurface.Create(info))
{
SKCanvas canvas = surface.Canvas;
Expand All @@ -91,7 +109,12 @@ public void UpdateColorAndMoveFile(string sourceFilePath, string destFilePath)
IsAntialias = true,
FilterQuality = SKFilterQuality.High
};
canvas.DrawBitmap(bmp, info.Rect, paint);

var left = screenSize.Width <= bmp.Width ? 0 : (screenSize.Width - bmp.Width) / 2;
var top = screenSize.Height <= bmp.Height ? 0 : (screenSize.Height - bmp.Height) / 2;
var right = screenSize.Width <= bmp.Width ? left + screenSize.Width : left + bmp.Width;
var bottom = screenSize.Height <= bmp.Height ? top + screenSize.Height : top + bmp.Height;
canvas.DrawBitmap(bmp, new SKRect(left, top, right, bottom), paint);
canvas.Flush();

var updatedsplash = surface.Snapshot();
Expand Down Expand Up @@ -129,29 +152,39 @@ public void UpdateManifest()
return;
}
var splashScreensNodeList = doc.SelectNodes("//manifest:splash-screens", nsmgr);
if (splashScreensNodeList != null)
XmlNode splashScreensNode;
if (splashScreensNodeList.Count == 0)
{
splashScreensNode = doc.CreateElement("splash-screens", namespaceURI);
uiApplicationNode.AppendChild(splashScreensNode);
}
else
{
foreach (XmlNode node in splashScreensNodeList)
splashScreensNode = splashScreensNodeList[0];
List<XmlNode> nodesToRemove = new List<XmlNode>();
foreach (XmlNode splashScreenNode in splashScreensNode.ChildNodes)
{
var dpiValue = splashScreenNode.Attributes.GetNamedItem("dpi")?.Value;
if (dpiValue == "mdpi" || dpiValue == "hdpi")
{
nodesToRemove.Add(splashScreenNode);
}
}
foreach(XmlNode node in nodesToRemove)
{
uiApplicationNode.RemoveChild(node);
splashScreensNode.RemoveChild(node);
}
}

var splashScreensNode = doc.CreateElement("splash-screens", namespaceURI);
uiApplicationNode.AppendChild(splashScreensNode);

foreach(var image in splashDpiMap)
{
foreach (var orientation in orientations)
{
var splashScreenNode = doc.CreateElement("splash-screen", namespaceURI);
splashScreenNode.SetAttribute("src", image.Value);
splashScreenNode.SetAttribute("type", "img");
splashScreenNode.SetAttribute("dpi", image.Key);
splashScreenNode.SetAttribute("orientation", orientation);
splashScreenNode.SetAttribute("indicator-display", "false");
splashScreensNode.AppendChild(splashScreenNode);
}
var splashScreenNode = doc.CreateElement("splash-screen", namespaceURI);
splashScreenNode.SetAttribute("src", image.Value);
splashScreenNode.SetAttribute("type", "img");
splashScreenNode.SetAttribute("dpi", image.Key.Resolution);
splashScreenNode.SetAttribute("orientation", image.Key.Orientation);
splashScreenNode.SetAttribute("indicator-display", "false");
splashScreensNode.AppendChild(splashScreenNode);
}

doc.Save(xmlPath);
Expand Down

0 comments on commit bc8a784

Please sign in to comment.