@@ -12,8 +12,9 @@ namespace Flow.Launcher.Helper;
12
12
13
13
public static class WallpaperPathRetrieval
14
14
{
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 ( ) ;
17
18
18
19
public static Brush GetWallpaperBrush ( )
19
20
{
@@ -36,42 +37,38 @@ public static Brush GetWallpaperBrush()
36
37
// Since the wallpaper file name can be the same (TranscodedWallpaper),
37
38
// we need to add the last modified date to differentiate them
38
39
var dateModified = File . GetLastWriteTime ( wallpaperPath ) ;
39
- wallpaperCache . TryGetValue ( ( wallpaperPath , dateModified ) , out var cachedWallpaper ) ;
40
- if ( cachedWallpaper != null )
40
+ lock ( CacheLock )
41
41
{
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
+ }
44
47
}
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 )
54
56
{
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 } ") ;
56
58
return new SolidColorBrush ( Colors . Transparent ) ;
57
59
}
58
60
59
- var originalWidth = bitmap . PixelWidth ;
60
- var originalHeight = bitmap . PixelHeight ;
61
-
62
61
// 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 ) ;
69
67
70
68
// Set DecodePixelWidth and DecodePixelHeight to resize the image while preserving aspect ratio
71
- bitmap = new BitmapImage ( ) ;
69
+ var bitmap = new BitmapImage ( ) ;
72
70
bitmap . BeginInit ( ) ;
73
- memStream . Seek ( 0 , SeekOrigin . Begin ) ; // Reset stream position
74
- bitmap . StreamSource = memStream ;
71
+ bitmap . UriSource = new Uri ( wallpaperPath ) ;
75
72
bitmap . DecodePixelWidth = decodedPixelWidth ;
76
73
bitmap . DecodePixelHeight = decodedPixelHeight ;
77
74
bitmap . EndInit ( ) ;
@@ -80,19 +77,21 @@ public static Brush GetWallpaperBrush()
80
77
wallpaperBrush . Freeze ( ) ; // Make the brush thread-safe
81
78
82
79
// Manage cache size
83
- if ( wallpaperCache . Count >= MAX_CACHE_SIZE )
80
+ lock ( CacheLock )
84
81
{
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 )
88
83
{
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
+ }
90
90
}
91
- }
92
-
93
- wallpaperCache . Add ( ( wallpaperPath , dateModified ) , wallpaperBrush ) ;
94
- return wallpaperBrush ;
95
91
92
+ WallpaperCache . Add ( ( wallpaperPath , dateModified ) , wallpaperBrush ) ;
93
+ return wallpaperBrush ;
94
+ }
96
95
}
97
96
catch ( Exception ex )
98
97
{
@@ -103,7 +102,7 @@ public static Brush GetWallpaperBrush()
103
102
104
103
private static Color GetWallpaperColor ( )
105
104
{
106
- RegistryKey key = Registry . CurrentUser . OpenSubKey ( @"Control Panel\Colors" , true ) ;
105
+ RegistryKey key = Registry . CurrentUser . OpenSubKey ( @"Control Panel\Colors" , false ) ;
107
106
var result = key ? . GetValue ( "Background" , null ) ;
108
107
if ( result is string strResult )
109
108
{
@@ -114,7 +113,7 @@ private static Color GetWallpaperColor()
114
113
}
115
114
catch ( Exception ex )
116
115
{
117
- App . API . LogException ( nameof ( WallpaperPathRetrieval ) , "Error parsing wallpaper color" , ex ) ;
116
+ App . API . LogException ( nameof ( WallpaperPathRetrieval ) , "Error parsing wallpaper color" , ex ) ;
118
117
}
119
118
}
120
119
0 commit comments