Skip to content

Commit c685075

Browse files
committed
Code quality & Lock & Bitmap memory & Registry access improvement
1 parent 9ac9ec8 commit c685075

File tree

1 file changed

+38
-39
lines changed

1 file changed

+38
-39
lines changed

Flow.Launcher/Helper/WallpaperPathRetrieval.cs

Lines changed: 38 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,9 @@ namespace Flow.Launcher.Helper;
1212

1313
public static class WallpaperPathRetrieval
1414
{
15-
private static readonly int MAX_CACHE_SIZE = 3;
16-
private static readonly Dictionary<(string, DateTime), ImageBrush> wallpaperCache = new();
15+
private const int MaxCacheSize = 3;
16+
private static readonly Dictionary<(string, DateTime), ImageBrush> WallpaperCache = new();
17+
private static readonly object CacheLock = new();
1718

1819
public static Brush GetWallpaperBrush()
1920
{
@@ -36,42 +37,38 @@ public static Brush GetWallpaperBrush()
3637
// Since the wallpaper file name can be the same (TranscodedWallpaper),
3738
// we need to add the last modified date to differentiate them
3839
var dateModified = File.GetLastWriteTime(wallpaperPath);
39-
wallpaperCache.TryGetValue((wallpaperPath, dateModified), out var cachedWallpaper);
40-
if (cachedWallpaper != null)
40+
lock (CacheLock)
4141
{
42-
App.API.LogInfo(nameof(WallpaperPathRetrieval), "Using cached wallpaper");
43-
return cachedWallpaper;
42+
WallpaperCache.TryGetValue((wallpaperPath, dateModified), out var cachedWallpaper);
43+
if (cachedWallpaper != null)
44+
{
45+
return cachedWallpaper;
46+
}
4447
}
45-
46-
// We should not dispose the memory stream since the bitmap is still in use
47-
var memStream = new MemoryStream(File.ReadAllBytes(wallpaperPath));
48-
var bitmap = new BitmapImage();
49-
bitmap.BeginInit();
50-
bitmap.StreamSource = memStream;
51-
bitmap.EndInit();
52-
53-
if (bitmap.PixelWidth == 0 || bitmap.PixelHeight == 0)
48+
49+
using var fileStream = File.OpenRead(wallpaperPath);
50+
var decoder = BitmapDecoder.Create(fileStream, BitmapCreateOptions.DelayCreation, BitmapCacheOption.None);
51+
var frame = decoder.Frames[0];
52+
var originalWidth = frame.PixelWidth;
53+
var originalHeight = frame.PixelHeight;
54+
55+
if (originalWidth == 0 || originalHeight == 0)
5456
{
55-
App.API.LogInfo(nameof(WallpaperPathRetrieval), $"Failed to load bitmap: Width={bitmap.PixelWidth}, Height={bitmap.PixelHeight}");
57+
App.API.LogInfo(nameof(WallpaperPathRetrieval), $"Failed to load bitmap: Width={originalWidth}, Height={originalHeight}");
5658
return new SolidColorBrush(Colors.Transparent);
5759
}
5860

59-
var originalWidth = bitmap.PixelWidth;
60-
var originalHeight = bitmap.PixelHeight;
61-
6261
// Calculate the scaling factor to fit the image within 800x600 while preserving aspect ratio
63-
double widthRatio = 800.0 / originalWidth;
64-
double heightRatio = 600.0 / originalHeight;
65-
double scaleFactor = Math.Min(widthRatio, heightRatio);
66-
67-
int decodedPixelWidth = (int)(originalWidth * scaleFactor);
68-
int decodedPixelHeight = (int)(originalHeight * scaleFactor);
62+
var widthRatio = 800.0 / originalWidth;
63+
var heightRatio = 600.0 / originalHeight;
64+
var scaleFactor = Math.Min(widthRatio, heightRatio);
65+
var decodedPixelWidth = (int)(originalWidth * scaleFactor);
66+
var decodedPixelHeight = (int)(originalHeight * scaleFactor);
6967

7068
// Set DecodePixelWidth and DecodePixelHeight to resize the image while preserving aspect ratio
71-
bitmap = new BitmapImage();
69+
var bitmap = new BitmapImage();
7270
bitmap.BeginInit();
73-
memStream.Seek(0, SeekOrigin.Begin); // Reset stream position
74-
bitmap.StreamSource = memStream;
71+
bitmap.UriSource = new Uri(wallpaperPath);
7572
bitmap.DecodePixelWidth = decodedPixelWidth;
7673
bitmap.DecodePixelHeight = decodedPixelHeight;
7774
bitmap.EndInit();
@@ -80,19 +77,21 @@ public static Brush GetWallpaperBrush()
8077
wallpaperBrush.Freeze(); // Make the brush thread-safe
8178

8279
// Manage cache size
83-
if (wallpaperCache.Count >= MAX_CACHE_SIZE)
80+
lock (CacheLock)
8481
{
85-
// Remove the oldest wallpaper from the cache
86-
var oldestCache = wallpaperCache.Keys.OrderBy(k => k.Item2).FirstOrDefault();
87-
if (oldestCache != default)
82+
if (WallpaperCache.Count >= MaxCacheSize)
8883
{
89-
wallpaperCache.Remove(oldestCache);
84+
// Remove the oldest wallpaper from the cache
85+
var oldestCache = WallpaperCache.Keys.OrderBy(k => k.Item2).FirstOrDefault();
86+
if (oldestCache != default)
87+
{
88+
WallpaperCache.Remove(oldestCache);
89+
}
9090
}
91-
}
92-
93-
wallpaperCache.Add((wallpaperPath, dateModified), wallpaperBrush);
94-
return wallpaperBrush;
9591

92+
WallpaperCache.Add((wallpaperPath, dateModified), wallpaperBrush);
93+
return wallpaperBrush;
94+
}
9695
}
9796
catch (Exception ex)
9897
{
@@ -103,7 +102,7 @@ public static Brush GetWallpaperBrush()
103102

104103
private static Color GetWallpaperColor()
105104
{
106-
RegistryKey key = Registry.CurrentUser.OpenSubKey(@"Control Panel\Colors", true);
105+
RegistryKey key = Registry.CurrentUser.OpenSubKey(@"Control Panel\Colors", false);
107106
var result = key?.GetValue("Background", null);
108107
if (result is string strResult)
109108
{
@@ -114,7 +113,7 @@ private static Color GetWallpaperColor()
114113
}
115114
catch (Exception ex)
116115
{
117-
App.API.LogException(nameof(WallpaperPathRetrieval), "Error parsing wallpaper color", ex);
116+
App.API.LogException(nameof(WallpaperPathRetrieval), "Error parsing wallpaper color", ex);
118117
}
119118
}
120119

0 commit comments

Comments
 (0)