Statically load not found and loading icons to slightly increase performance

This commit is contained in:
Peter Kirmeier 2023-09-23 18:02:45 +02:00
parent 2987bed7cc
commit 425bd91160
2 changed files with 7 additions and 5 deletions

View file

@ -245,7 +245,7 @@ namespace SystemTrayMenu.DataClasses
if (!cacheHit) if (!cacheHit)
{ {
IconLoading = true; IconLoading = true;
ColumnIcon = (ImageSource?)Application.Current.Resources["LoadingIconImage"]; // TODO: Maybe add rotation animation like for the loading Menu icon? (See: pictureBoxLoading, LoadingRotation) ColumnIcon = IconReader.LoadingImage; // TODO: Maybe add rotation animation like for the loading Menu icon? (See: pictureBoxLoading, LoadingRotation)
} }
} }
@ -389,7 +389,7 @@ namespace SystemTrayMenu.DataClasses
{ {
if (icon == null) if (icon == null)
{ {
icon = (BitmapSource)Application.Current.Resources["NotFoundIconImage"]; icon = IconReader.NotFoundImage;
} }
else if (HiddenEntry) else if (HiddenEntry)
{ {

View file

@ -23,7 +23,9 @@ namespace SystemTrayMenu.Utilities
/// </summary> /// </summary>
internal static class IconReader internal static class IconReader
{ {
private static readonly BitmapSource? OverlayImage = (BitmapSource?)Application.Current.Resources["LinkArrowIconImage"]; internal static readonly BitmapSource LoadingImage = (BitmapSource)Application.Current.Resources["LoadingIconImage"];
internal static readonly BitmapSource NotFoundImage = (BitmapSource)Application.Current.Resources["NotFoundIconImage"];
private static readonly BitmapSource OverlayImage = (BitmapSource)Application.Current.Resources["LinkArrowIconImage"];
private static readonly ConcurrentDictionary<string, BitmapSource> IconDictPersistent = new(); private static readonly ConcurrentDictionary<string, BitmapSource> IconDictPersistent = new();
private static readonly ConcurrentDictionary<string, BitmapSource> IconDictCache = new(); private static readonly ConcurrentDictionary<string, BitmapSource> IconDictCache = new();
@ -212,7 +214,7 @@ namespace SystemTrayMenu.Utilities
{ {
result = TryCreateBitmapSourceFromIcon(resolvedPath, icon); result = TryCreateBitmapSourceFromIcon(resolvedPath, icon);
icon.Dispose(); icon.Dispose();
if (result != null && linkOverlay && OverlayImage != null) if (result != null && linkOverlay)
{ {
result = ImagingHelper.CreateIconWithOverlay(result, OverlayImage); result = ImagingHelper.CreateIconWithOverlay(result, OverlayImage);
} }
@ -265,7 +267,7 @@ namespace SystemTrayMenu.Utilities
private static BitmapSource GetIconAsBitmapSourceSTA(string path, string resolvedPath, bool linkOverlay, bool isFolder) private static BitmapSource GetIconAsBitmapSourceSTA(string path, string resolvedPath, bool linkOverlay, bool isFolder)
{ {
BitmapSource? bitmapSource = TryGetIconAsBitmapSourceSTA(path, resolvedPath, linkOverlay, isFolder); BitmapSource? bitmapSource = TryGetIconAsBitmapSourceSTA(path, resolvedPath, linkOverlay, isFolder);
bitmapSource ??= (BitmapSource)Application.Current.Resources["NotFoundIconImage"]; bitmapSource ??= NotFoundImage;
bitmapSource.Freeze(); // Make it accessible for any thread bitmapSource.Freeze(); // Make it accessible for any thread
return bitmapSource; return bitmapSource;
} }