@@ -13,7 +13,6 @@ namespace Flow.Launcher.Helper;
13
13
public static class WallpaperPathRetrieval
14
14
{
15
15
private static readonly int MAX_CACHE_SIZE = 3 ;
16
-
17
16
private static readonly Dictionary < ( string , DateTime ) , ImageBrush > wallpaperCache = new ( ) ;
18
17
19
18
public static Brush GetWallpaperBrush ( )
@@ -27,46 +26,73 @@ public static Brush GetWallpaperBrush()
27
26
try
28
27
{
29
28
var wallpaperPath = Win32Helper . GetWallpaperPath ( ) ;
30
- if ( wallpaperPath is not null && File . Exists ( wallpaperPath ) )
29
+ if ( string . IsNullOrEmpty ( wallpaperPath ) || ! File . Exists ( wallpaperPath ) )
31
30
{
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 )
37
- {
38
- return cachedWallpaper ;
39
- }
31
+ App . API . LogInfo ( nameof ( WallpaperPathRetrieval ) , $ "Wallpaper path is invalid: { wallpaperPath } ") ;
32
+ var wallpaperColor = GetWallpaperColor ( ) ;
33
+ return new SolidColorBrush ( wallpaperColor ) ;
34
+ }
35
+
36
+ // Since the wallpaper file name can be the same (TranscodedWallpaper),
37
+ // we need to add the last modified date to differentiate them
38
+ var dateModified = File . GetLastWriteTime ( wallpaperPath ) ;
39
+ wallpaperCache . TryGetValue ( ( wallpaperPath , dateModified ) , out var cachedWallpaper ) ;
40
+ if ( cachedWallpaper != null )
41
+ {
42
+ App . API . LogInfo ( nameof ( WallpaperPathRetrieval ) , "Using cached wallpaper" ) ;
43
+ return cachedWallpaper ;
44
+ }
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 )
54
+ {
55
+ App . API . LogInfo ( nameof ( WallpaperPathRetrieval ) , $ "Failed to load bitmap: Width={ bitmap . PixelWidth } , Height={ bitmap . PixelHeight } ") ;
56
+ return new SolidColorBrush ( Colors . Transparent ) ;
57
+ }
58
+
59
+ var originalWidth = bitmap . PixelWidth ;
60
+ var originalHeight = bitmap . PixelHeight ;
61
+
62
+ // 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 ) ;
40
69
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
-
53
- // Manage cache size
54
- if ( wallpaperCache . Count >= MAX_CACHE_SIZE )
70
+ // Set DecodePixelWidth and DecodePixelHeight to resize the image while preserving aspect ratio
71
+ bitmap = new BitmapImage ( ) ;
72
+ bitmap . BeginInit ( ) ;
73
+ memStream . Seek ( 0 , SeekOrigin . Begin ) ; // Reset stream position
74
+ bitmap . StreamSource = memStream ;
75
+ bitmap . DecodePixelWidth = decodedPixelWidth ;
76
+ bitmap . DecodePixelHeight = decodedPixelHeight ;
77
+ bitmap . EndInit ( ) ;
78
+ bitmap . Freeze ( ) ; // Make the bitmap thread-safe
79
+ var wallpaperBrush = new ImageBrush ( bitmap ) { Stretch = Stretch . UniformToFill } ;
80
+ wallpaperBrush . Freeze ( ) ; // Make the brush thread-safe
81
+
82
+ // Manage cache size
83
+ if ( wallpaperCache . Count >= MAX_CACHE_SIZE )
84
+ {
85
+ // Remove the oldest wallpaper from the cache
86
+ var oldestCache = wallpaperCache . Keys . OrderBy ( k => k . Item2 ) . FirstOrDefault ( ) ;
87
+ if ( oldestCache != default )
55
88
{
56
- // Remove the oldest wallpaper from the cache
57
- var oldestCache = wallpaperCache . Keys . OrderBy ( k => k . Item2 ) . FirstOrDefault ( ) ;
58
- if ( oldestCache != default )
59
- {
60
- wallpaperCache . Remove ( oldestCache ) ;
61
- }
89
+ wallpaperCache . Remove ( oldestCache ) ;
62
90
}
63
-
64
- wallpaperCache . Add ( ( wallpaperPath , dateModified ) , wallpaperBrush ) ;
65
- return wallpaperBrush ;
66
91
}
67
92
68
- var wallpaperColor = GetWallpaperColor ( ) ;
69
- return new SolidColorBrush ( wallpaperColor ) ;
93
+ wallpaperCache . Add ( ( wallpaperPath , dateModified ) , wallpaperBrush ) ;
94
+ return wallpaperBrush ;
95
+
70
96
}
71
97
catch ( Exception ex )
72
98
{
@@ -86,8 +112,9 @@ private static Color GetWallpaperColor()
86
112
var parts = strResult . Trim ( ) . Split ( new [ ] { ' ' } , 3 ) . Select ( byte . Parse ) . ToList ( ) ;
87
113
return Color . FromRgb ( parts [ 0 ] , parts [ 1 ] , parts [ 2 ] ) ;
88
114
}
89
- catch
115
+ catch ( Exception ex )
90
116
{
117
+ App . API . LogException ( nameof ( WallpaperPathRetrieval ) , "Error parsing wallpaper color" , ex ) ;
91
118
}
92
119
}
93
120
0 commit comments