Implement Icon priorities.

With this, a library can set a "default" icon, to be used only if the
user doesn't provide a better one.

Additionally, this treats SVGs as "near" matches rather than exact,
meaning that a perfect-size PNG will take precedence over an SVG. Why
would the PNG even exist if you didn't want to use it in that one case?

And finally, I took the liberty of cleaning up `findNearestMatch()`. It
seemed unnecessarily confusing for what it did.
This commit is contained in:
Joseph Cloutier
2021-12-14 13:13:55 -05:00
parent 7bfaa441f2
commit e848002f93
3 changed files with 20 additions and 25 deletions

View File

@@ -6,11 +6,13 @@ class Icon
public var path:String;
public var size:Int;
public var width:Int;
public var priority:Int;
public function new(path:String, size:Int = 0)
public function new(path:String, size:Int = 0, priority:Int = 0)
{
this.path = path;
this.size = height = width = size;
this.priority = 0;
}
public function clone():Icon

View File

@@ -281,7 +281,7 @@ class IconHelper
for (icon in icons)
{
if ((icon.width == 0 && icon.height == 0) || (icon.width == width && icon.height == height))
if (icon.width == width && icon.height == height && (match == null || match.priority < icon.priority))
{
match = icon;
}
@@ -292,33 +292,21 @@ class IconHelper
public static function findNearestMatch(icons:Array<Icon>, width:Int, height:Int):Icon
{
var match = null;
var match:Icon = null;
var matchDifference = Math.POSITIVE_INFINITY;
for (icon in icons)
{
if (icon.width > width / 2 && icon.height > height / 2)
var iconDifference = Math.abs(icon.width - width) + Math.abs(icon.height - height);
if (Path.extension(icon.path) == "svg")
{
if (match == null)
{
match = icon;
}
else
{
if (icon.width > match.width && icon.height > match.height)
{
if (match.width < width || match.height < height)
{
match = icon;
}
}
else
{
if (icon.width > width && icon.height > height)
{
match = icon;
}
}
}
iconDifference = 0;
}
if (iconDifference < matchDifference || iconDifference == matchDifference && icon.priority > match.priority)
{
match = icon;
matchDifference = iconDifference;
}
}

View File

@@ -1478,6 +1478,11 @@ class ProjectXMLParser extends HXProject
icon.height = Std.parseInt(substitute(element.att.height));
}
if (element.has.priority)
{
icon.priority = Std.parseInt(substitute(element.att.priority));
}
icons.push(icon);
case "source", "classpath":