Skip to content

Commit 9dbc174

Browse files
committed
Add cache size management & Add error handling
1 parent 0fddb84 commit 9dbc174

File tree

1 file changed

+45
-24
lines changed

1 file changed

+45
-24
lines changed

Flow.Launcher/Helper/WallpaperPathRetrieval.cs

Lines changed: 45 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ public static class WallpaperPathRetrieval
1717
private static readonly int MAX_PATH = 260;
1818

1919
private static readonly Dictionary<(string, DateTime), ImageBrush> wallpaperCache = new();
20+
private const int MaxCacheSize = 3;
2021

2122
public static Brush GetWallpaperBrush()
2223
{
@@ -26,35 +27,55 @@ public static Brush GetWallpaperBrush()
2627
return Application.Current.Dispatcher.Invoke(GetWallpaperBrush);
2728
}
2829

29-
var wallpaperPath = GetWallpaperPath();
30-
if (wallpaperPath is not null && File.Exists(wallpaperPath))
30+
try
3131
{
32-
// Since the wallpaper file name can be the same (TranscodedWallpaper),
33-
// we need to add the last modified date to differentiate them
34-
var dateModified = File.GetLastWriteTime(wallpaperPath);
35-
wallpaperCache.TryGetValue((wallpaperPath, dateModified), out var cachedWallpaper);
36-
if (cachedWallpaper != null)
32+
var wallpaperPath = GetWallpaperPath();
33+
if (wallpaperPath is not null && File.Exists(wallpaperPath))
3734
{
38-
return cachedWallpaper;
35+
// Since the wallpaper file name can be the same (TranscodedWallpaper),
36+
// we need to add the last modified date to differentiate them
37+
var dateModified = File.GetLastWriteTime(wallpaperPath);
38+
wallpaperCache.TryGetValue((wallpaperPath, dateModified), out var cachedWallpaper);
39+
if (cachedWallpaper != null)
40+
{
41+
return cachedWallpaper;
42+
}
43+
44+
// We should not dispose the memory stream since the bitmap is still in use
45+
var memStream = new MemoryStream(File.ReadAllBytes(wallpaperPath));
46+
var bitmap = new BitmapImage();
47+
bitmap.BeginInit();
48+
bitmap.StreamSource = memStream;
49+
bitmap.DecodePixelWidth = 800;
50+
bitmap.DecodePixelHeight = 600;
51+
bitmap.EndInit();
52+
bitmap.Freeze(); // Make the bitmap thread-safe
53+
var wallpaperBrush = new ImageBrush(bitmap) { Stretch = Stretch.UniformToFill };
54+
wallpaperBrush.Freeze(); // Make the brush thread-safe
55+
56+
// Manage cache size
57+
if (wallpaperCache.Count >= MaxCacheSize)
58+
{
59+
// Remove the oldest wallpaper from the cache
60+
var oldestCache = wallpaperCache.Keys.OrderBy(k => k.Item2).FirstOrDefault();
61+
if (oldestCache != default)
62+
{
63+
wallpaperCache.Remove(oldestCache);
64+
}
65+
}
66+
67+
wallpaperCache.Add((wallpaperPath, dateModified), wallpaperBrush);
68+
return wallpaperBrush;
3969
}
4070

41-
// We should not dispose the memory stream since the bitmap is still in use
42-
var memStream = new MemoryStream(File.ReadAllBytes(wallpaperPath));
43-
var bitmap = new BitmapImage();
44-
bitmap.BeginInit();
45-
bitmap.StreamSource = memStream;
46-
bitmap.DecodePixelWidth = 800;
47-
bitmap.DecodePixelHeight = 600;
48-
bitmap.EndInit();
49-
bitmap.Freeze(); // Make the bitmap thread-safe
50-
var wallpaperBrush = new ImageBrush(bitmap) { Stretch = Stretch.UniformToFill };
51-
wallpaperBrush.Freeze(); // Make the brush thread-safe
52-
wallpaperCache.Add((wallpaperPath, dateModified), wallpaperBrush);
53-
return wallpaperBrush;
71+
var wallpaperColor = GetWallpaperColor();
72+
return new SolidColorBrush(wallpaperColor);
73+
}
74+
catch (Exception ex)
75+
{
76+
App.API.LogException(nameof(WallpaperPathRetrieval), "Error retrieving wallpaper", ex);
77+
return new SolidColorBrush(Colors.Transparent);
5478
}
55-
56-
var wallpaperColor = GetWallpaperColor();
57-
return new SolidColorBrush(wallpaperColor);
5879
}
5980

6081
private static unsafe string GetWallpaperPath()

0 commit comments

Comments
 (0)