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

Fixed an issue where SVG icons were flickering when the NavigationView was collapsed. #109

Merged
merged 1 commit into from
Dec 11, 2021
Merged
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
24 changes: 19 additions & 5 deletions src/dev/impl/DevToys/ViewModels/Tools/ToolProviderBase.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#nullable enable

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Numerics;
using System.Threading.Tasks;
Expand All @@ -20,8 +22,11 @@ namespace DevToys.ViewModels.Tools
{
internal abstract class ToolProviderBase
{
private const int IconMinimalSize = 16;
protected const string AssetsFolderPath = "ms-appx:///Assets/";

private static readonly Dictionary<string, string> IconFileNameToSvgMap = new();

protected TaskCompletionNotifier<IconElement> CreatePathIconFromPath(string resourceName)
{
Arguments.NotNullOrWhiteSpace(resourceName, nameof(resourceName));
Expand Down Expand Up @@ -66,13 +71,17 @@ var result
ElementTheme actualTheme = windowFrame.ActualTheme;
return Task.Run(async () =>
{
await TaskScheduler.Default;
if (!IconFileNameToSvgMap.TryGetValue(iconFileName, out string svgFileContent))
{
await TaskScheduler.Default;

StorageFolder installationFolder = Windows.ApplicationModel.Package.Current.InstalledLocation;
StorageFolder installationFolder = Windows.ApplicationModel.Package.Current.InstalledLocation;

IStorageItem file = await installationFolder.TryGetItemAsync("Assets\\Icons\\" + iconFileName);
IStorageItem file = await installationFolder.TryGetItemAsync("Assets\\Icons\\" + iconFileName);

string svgFileContent = File.ReadAllText(file.Path);
svgFileContent = File.ReadAllText(file.Path);
IconFileNameToSvgMap[iconFileName] = svgFileContent;
}

if (actualTheme == ElementTheme.Dark)
{
Expand All @@ -83,7 +92,7 @@ var result
svgFileContent = svgFileContent.Replace("#ffffff", "#000000").Replace("#FFFFFFFF", "#000000");
}

return await ThreadHelper.RunOnUIThreadAsync(ThreadPriority.High, async () =>
return await ThreadHelper.RunOnUIThreadAsync(ThreadPriority.Low, async () =>
{
var svgSource = new SvgImageSource();

Expand All @@ -107,6 +116,11 @@ var result
newSize = parent.ActualSize.ToSize();
}

if (newSize.Width < IconMinimalSize || newSize.Height < IconMinimalSize)
{
return;
}

svgSource = new SvgImageSource();
svgSource.RasterizePixelHeight = newSize.Height;
svgSource.RasterizePixelWidth = newSize.Width;
Expand Down