Merge pull request #1492 from VictoriousRaptor/FixProgramIcons

Fix UWP icon missing issue
This commit is contained in:
VictoriousRaptor 2022-11-11 14:00:11 +08:00 committed by GitHub
commit a2c2da037f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -601,92 +601,97 @@ namespace Flow.Launcher.Plugin.Program.Programs
// windows 8.1 https://msdn.microsoft.com/en-us/library/windows/apps/hh965372.aspx#target_size // windows 8.1 https://msdn.microsoft.com/en-us/library/windows/apps/hh965372.aspx#target_size
// windows 8 https://msdn.microsoft.com/en-us/library/windows/apps/br211475.aspx // windows 8 https://msdn.microsoft.com/en-us/library/windows/apps/br211475.aspx
string path; string path = Path.Combine(Package.Location, uri);
if (uri.Contains("\\"))
{ var logoPath = TryToFindLogo(uri, path);
path = Path.Combine(Package.Location, uri); if (String.IsNullOrEmpty(logoPath))
}
else
{ {
// TODO: Don't know why, just keep it at the moment
// Maybe on older version of Windows 10?
// for C:\Windows\MiracastView etc // for C:\Windows\MiracastView etc
path = Path.Combine(Package.Location, "Assets", uri); return TryToFindLogo(uri, Path.Combine(Package.Location, "Assets", uri));
} }
return logoPath;
var extension = Path.GetExtension(path); string TryToFindLogo(string uri, string path)
if (extension != null)
{ {
var end = path.Length - extension.Length; var extension = Path.GetExtension(path);
var prefix = path.Substring(0, end); if (extension != null)
var paths = new List<string>
{ {
path //if (File.Exists(path))
}; //{
// return path; // shortcut, avoid enumerating files
//}
var scaleFactors = new Dictionary<PackageVersion, List<int>> var logoNamePrefix = Path.GetFileNameWithoutExtension(uri); // e.g Square44x44
{ var logoDir = Path.GetDirectoryName(path); // e.g ..\..\Assets
// scale factors on win10: https://docs.microsoft.com/en-us/windows/uwp/controls-and-patterns/tiles-and-notifications-app-assets#asset-size-tables, if (String.IsNullOrEmpty(logoNamePrefix) || String.IsNullOrEmpty(logoDir) || !Directory.Exists(logoDir))
{ {
PackageVersion.Windows10, new List<int> // Known issue: Edge always triggers it since logo is not at uri
{ ProgramLogger.LogException($"|UWP|LogoPathFromUri|{Package.Location}" +
100, $"|{UserModelId} can't find logo uri for {uri} in package location (logo name or directory not found): {Package.Location}", new FileNotFoundException());
125, return string.Empty;
150, }
200,
400 var files = Directory.EnumerateFiles(logoDir);
}
}, // Currently we don't care which one to choose
// Just ignore all qualifiers
// select like logo.[xxx_yyy].png
// https://learn.microsoft.com/en-us/windows/uwp/app-resources/tailor-resources-lang-scale-contrast
var logos = files.Where(file =>
Path.GetFileName(file)?.StartsWith(logoNamePrefix, StringComparison.OrdinalIgnoreCase) ?? false
&& extension.Equals(Path.GetExtension(file), StringComparison.OrdinalIgnoreCase)
);
var selected = logos.FirstOrDefault();
var closest = selected;
int min = int.MaxValue;
foreach(var logo in logos)
{ {
PackageVersion.Windows81, new List<int>
var imageStream = File.OpenRead(logo);
var decoder = BitmapDecoder.Create(imageStream, BitmapCreateOptions.IgnoreColorProfile, BitmapCacheOption.None);
var height = decoder.Frames[0].PixelHeight;
var width = decoder.Frames[0].PixelWidth;
int pixelCountDiff = Math.Abs(height * width - 1936); // 44*44=1936
if(pixelCountDiff < min)
{ {
100, // try to find the closest to 44x44 logo
120, closest = logo;
140, if (pixelCountDiff == 0)
160, break; // found 44x44
180 min = pixelCountDiff;
}
},
{
PackageVersion.Windows8, new List<int>
{
100
} }
} }
};
if (scaleFactors.ContainsKey(Package.Version)) selected = closest;
{ if (!string.IsNullOrEmpty(selected))
foreach (var factor in scaleFactors[Package.Version])
{ {
paths.Add($"{prefix}.scale-{factor}{extension}"); return selected;
}
else
{
ProgramLogger.LogException($"|UWP|LogoPathFromUri|{Package.Location}" +
$"|{UserModelId} can't find logo uri for {uri} in package location (can't find specified logo): {Package.Location}", new FileNotFoundException());
return string.Empty;
} }
}
var selected = paths.FirstOrDefault(File.Exists);
if (!string.IsNullOrEmpty(selected))
{
return selected;
} }
else else
{ {
ProgramLogger.LogException($"|UWP|LogoPathFromUri|{Package.Location}" + ProgramLogger.LogException($"|UWP|LogoPathFromUri|{Package.Location}" +
$"|{UserModelId} can't find logo uri for {uri} in package location: {Package.Location}", new FileNotFoundException()); $"|Unable to find extension from {uri} for {UserModelId} " +
$"in package location {Package.Location}", new FileNotFoundException());
return string.Empty; return string.Empty;
} }
} }
else
{
ProgramLogger.LogException($"|UWP|LogoPathFromUri|{Package.Location}" +
$"|Unable to find extension from {uri} for {UserModelId} " +
$"in package location {Package.Location}", new FileNotFoundException());
return string.Empty;
}
} }
public ImageSource Logo() public ImageSource Logo()
{ {
var logo = ImageFromPath(LogoPath); var logo = ImageFromPath(LogoPath);
var plated = PlatedImage(logo); var plated = PlatedImage(logo); // TODO: maybe get plated directly from app package?
// todo magic! temp fix for cross thread object // todo magic! temp fix for cross thread object
plated.Freeze(); plated.Freeze();