Skip to content

Commit 4b2fdec

Browse files
Jack251970jjw24
authored andcommitted
Merge pull request #3986 from Flow-Launcher/wallpaper_lock
Fix Wallpaper File Lock
1 parent ed06504 commit 4b2fdec

File tree

2 files changed

+24
-16
lines changed

2 files changed

+24
-16
lines changed

Flow.Launcher.Infrastructure/Image/ImageLoader.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public static class ImageLoader
2222
private static Lock storageLock { get; } = new();
2323
private static BinaryStorage<List<(string, bool)>> _storage;
2424
private static readonly ConcurrentDictionary<string, string> GuidToKey = new();
25-
private static IImageHashGenerator _hashGenerator;
25+
private static ImageHashGenerator _hashGenerator;
2626
private static readonly bool EnableImageHash = true;
2727
public static ImageSource Image => ImageCache[Constant.ImageIcon, false];
2828
public static ImageSource MissingImage => ImageCache[Constant.MissingImgIcon, false];
@@ -31,7 +31,7 @@ public static class ImageLoader
3131
public const int FullIconSize = 256;
3232
public const int FullImageSize = 320;
3333

34-
private static readonly string[] ImageExtensions = { ".png", ".jpg", ".jpeg", ".gif", ".bmp", ".tiff", ".ico" };
34+
private static readonly string[] ImageExtensions = [".png", ".jpg", ".jpeg", ".gif", ".bmp", ".tiff", ".ico"];
3535
private static readonly string SvgExtension = ".svg";
3636

3737
public static async Task InitializeAsync()
@@ -327,7 +327,7 @@ public static async ValueTask<ImageSource> LoadAsync(string path, bool loadFullI
327327
return img;
328328
}
329329

330-
private static ImageSource LoadFullImage(string path)
330+
private static BitmapImage LoadFullImage(string path)
331331
{
332332
BitmapImage image = new BitmapImage();
333333
image.BeginInit();
@@ -364,7 +364,7 @@ private static ImageSource LoadFullImage(string path)
364364
return image;
365365
}
366366

367-
private static ImageSource LoadSvgImage(string path, bool loadFullImage = false)
367+
private static RenderTargetBitmap LoadSvgImage(string path, bool loadFullImage = false)
368368
{
369369
// Set up drawing settings
370370
var desiredHeight = loadFullImage ? FullImageSize : SmallIconSize;

Flow.Launcher/Helper/WallpaperPathRetrieval.cs

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Collections.Generic;
33
using System.IO;
44
using System.Linq;
5+
using System.Threading;
56
using System.Windows;
67
using System.Windows.Media;
78
using System.Windows.Media.Imaging;
@@ -16,7 +17,7 @@ public static class WallpaperPathRetrieval
1617

1718
private const int MaxCacheSize = 3;
1819
private static readonly Dictionary<(string, DateTime), ImageBrush> WallpaperCache = new();
19-
private static readonly object CacheLock = new();
20+
private static readonly Lock CacheLock = new();
2021

2122
public static Brush GetWallpaperBrush()
2223
{
@@ -31,7 +32,7 @@ public static Brush GetWallpaperBrush()
3132
var wallpaperPath = Win32Helper.GetWallpaperPath();
3233
if (string.IsNullOrEmpty(wallpaperPath) || !File.Exists(wallpaperPath))
3334
{
34-
App.API.LogInfo(ClassName, $"Wallpaper path is invalid: {wallpaperPath}");
35+
App.API.LogError(ClassName, $"Wallpaper path is invalid: {wallpaperPath}");
3536
var wallpaperColor = GetWallpaperColor();
3637
return new SolidColorBrush(wallpaperColor);
3738
}
@@ -47,17 +48,22 @@ public static Brush GetWallpaperBrush()
4748
return cachedWallpaper;
4849
}
4950
}
50-
51-
using var fileStream = File.OpenRead(wallpaperPath);
52-
var decoder = BitmapDecoder.Create(fileStream, BitmapCreateOptions.DelayCreation, BitmapCacheOption.None);
53-
var frame = decoder.Frames[0];
54-
var originalWidth = frame.PixelWidth;
55-
var originalHeight = frame.PixelHeight;
51+
52+
int originalWidth, originalHeight;
53+
// Use `using ()` instead of `using var` sentence here to ensure the wallpaper file is not locked
54+
using (var fileStream = File.OpenRead(wallpaperPath))
55+
{
56+
var decoder = BitmapDecoder.Create(fileStream, BitmapCreateOptions.DelayCreation, BitmapCacheOption.None);
57+
var frame = decoder.Frames[0];
58+
originalWidth = frame.PixelWidth;
59+
originalHeight = frame.PixelHeight;
60+
}
5661

5762
if (originalWidth == 0 || originalHeight == 0)
5863
{
59-
App.API.LogInfo(ClassName, $"Failed to load bitmap: Width={originalWidth}, Height={originalHeight}");
60-
return new SolidColorBrush(Colors.Transparent);
64+
App.API.LogError(ClassName, $"Failed to load bitmap: Width={originalWidth}, Height={originalHeight}");
65+
var wallpaperColor = GetWallpaperColor();
66+
return new SolidColorBrush(wallpaperColor);
6167
}
6268

6369
// Calculate the scaling factor to fit the image within 800x600 while preserving aspect ratio
@@ -70,7 +76,9 @@ public static Brush GetWallpaperBrush()
7076
// Set DecodePixelWidth and DecodePixelHeight to resize the image while preserving aspect ratio
7177
var bitmap = new BitmapImage();
7278
bitmap.BeginInit();
79+
bitmap.CacheOption = BitmapCacheOption.OnLoad; // Use OnLoad to ensure the wallpaper file is not locked
7380
bitmap.UriSource = new Uri(wallpaperPath);
81+
bitmap.CreateOptions = BitmapCreateOptions.IgnoreColorProfile;
7482
bitmap.DecodePixelWidth = decodedPixelWidth;
7583
bitmap.DecodePixelHeight = decodedPixelHeight;
7684
bitmap.EndInit();
@@ -104,13 +112,13 @@ public static Brush GetWallpaperBrush()
104112

105113
private static Color GetWallpaperColor()
106114
{
107-
RegistryKey key = Registry.CurrentUser.OpenSubKey(@"Control Panel\Colors", false);
115+
using var key = Registry.CurrentUser.OpenSubKey(@"Control Panel\Colors", false);
108116
var result = key?.GetValue("Background", null);
109117
if (result is string strResult)
110118
{
111119
try
112120
{
113-
var parts = strResult.Trim().Split(new[] { ' ' }, 3).Select(byte.Parse).ToList();
121+
var parts = strResult.Trim().Split([' '], 3).Select(byte.Parse).ToList();
114122
return Color.FromRgb(parts[0], parts[1], parts[2]);
115123
}
116124
catch (Exception ex)

0 commit comments

Comments
 (0)