From 80c283a370fa7a9a2cb74778b97e55b2829069a0 Mon Sep 17 00:00:00 2001 From: Jack251970 <1160210343@qq.com> Date: Wed, 17 Sep 2025 19:15:46 +0800 Subject: [PATCH 1/9] Improve code quality --- Flow.Launcher/Helper/WallpaperPathRetrieval.cs | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/Flow.Launcher/Helper/WallpaperPathRetrieval.cs b/Flow.Launcher/Helper/WallpaperPathRetrieval.cs index 93b9a8aaa23..c16e1170d97 100644 --- a/Flow.Launcher/Helper/WallpaperPathRetrieval.cs +++ b/Flow.Launcher/Helper/WallpaperPathRetrieval.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using System.IO; using System.Linq; +using System.Threading; using System.Windows; using System.Windows.Media; using System.Windows.Media.Imaging; @@ -16,7 +17,7 @@ public static class WallpaperPathRetrieval private const int MaxCacheSize = 3; private static readonly Dictionary<(string, DateTime), ImageBrush> WallpaperCache = new(); - private static readonly object CacheLock = new(); + private static readonly Lock CacheLock = new(); public static Brush GetWallpaperBrush() { @@ -56,7 +57,7 @@ public static Brush GetWallpaperBrush() if (originalWidth == 0 || originalHeight == 0) { - App.API.LogInfo(ClassName, $"Failed to load bitmap: Width={originalWidth}, Height={originalHeight}"); + App.API.LogError(ClassName, $"Failed to load bitmap: Width={originalWidth}, Height={originalHeight}"); return new SolidColorBrush(Colors.Transparent); } @@ -104,13 +105,13 @@ public static Brush GetWallpaperBrush() private static Color GetWallpaperColor() { - RegistryKey key = Registry.CurrentUser.OpenSubKey(@"Control Panel\Colors", false); + using var key = Registry.CurrentUser.OpenSubKey(@"Control Panel\Colors", false); var result = key?.GetValue("Background", null); if (result is string strResult) { try { - var parts = strResult.Trim().Split(new[] { ' ' }, 3).Select(byte.Parse).ToList(); + var parts = strResult.Trim().Split([' '], 3).Select(byte.Parse).ToList(); return Color.FromRgb(parts[0], parts[1], parts[2]); } catch (Exception ex) From 5c16b86edfde80d6b07c08da6a59fddc3ee61880 Mon Sep 17 00:00:00 2001 From: Jack251970 <1160210343@qq.com> Date: Wed, 17 Sep 2025 19:16:09 +0800 Subject: [PATCH 2/9] Fix file lock during file stream --- Flow.Launcher/Helper/WallpaperPathRetrieval.cs | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/Flow.Launcher/Helper/WallpaperPathRetrieval.cs b/Flow.Launcher/Helper/WallpaperPathRetrieval.cs index c16e1170d97..be35b8f6959 100644 --- a/Flow.Launcher/Helper/WallpaperPathRetrieval.cs +++ b/Flow.Launcher/Helper/WallpaperPathRetrieval.cs @@ -48,12 +48,15 @@ public static Brush GetWallpaperBrush() return cachedWallpaper; } } - - using var fileStream = File.OpenRead(wallpaperPath); - var decoder = BitmapDecoder.Create(fileStream, BitmapCreateOptions.DelayCreation, BitmapCacheOption.None); - var frame = decoder.Frames[0]; - var originalWidth = frame.PixelWidth; - var originalHeight = frame.PixelHeight; + + int originalWidth, originalHeight; + using (var fileStream = File.OpenRead(wallpaperPath)) + { + var decoder = BitmapDecoder.Create(fileStream, BitmapCreateOptions.DelayCreation, BitmapCacheOption.None); + var frame = decoder.Frames[0]; + originalWidth = frame.PixelWidth; + originalHeight = frame.PixelHeight; + } if (originalWidth == 0 || originalHeight == 0) { From 83f02f5c9144cf49c387a0e408fa92c43e0f7c9f Mon Sep 17 00:00:00 2001 From: Jack251970 <1160210343@qq.com> Date: Wed, 17 Sep 2025 19:16:58 +0800 Subject: [PATCH 3/9] Use log error & Improve returned color --- Flow.Launcher/Helper/WallpaperPathRetrieval.cs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/Flow.Launcher/Helper/WallpaperPathRetrieval.cs b/Flow.Launcher/Helper/WallpaperPathRetrieval.cs index be35b8f6959..aa41c999852 100644 --- a/Flow.Launcher/Helper/WallpaperPathRetrieval.cs +++ b/Flow.Launcher/Helper/WallpaperPathRetrieval.cs @@ -32,7 +32,7 @@ public static Brush GetWallpaperBrush() var wallpaperPath = Win32Helper.GetWallpaperPath(); if (string.IsNullOrEmpty(wallpaperPath) || !File.Exists(wallpaperPath)) { - App.API.LogInfo(ClassName, $"Wallpaper path is invalid: {wallpaperPath}"); + App.API.LogError(ClassName, $"Wallpaper path is invalid: {wallpaperPath}"); var wallpaperColor = GetWallpaperColor(); return new SolidColorBrush(wallpaperColor); } @@ -61,7 +61,8 @@ public static Brush GetWallpaperBrush() if (originalWidth == 0 || originalHeight == 0) { App.API.LogError(ClassName, $"Failed to load bitmap: Width={originalWidth}, Height={originalHeight}"); - return new SolidColorBrush(Colors.Transparent); + var wallpaperColor = GetWallpaperColor(); + return new SolidColorBrush(wallpaperColor); } // Calculate the scaling factor to fit the image within 800x600 while preserving aspect ratio From 60ec9b5c497c7a30e58dc29b0d5d602fb50a8adc Mon Sep 17 00:00:00 2001 From: Jack251970 <1160210343@qq.com> Date: Wed, 17 Sep 2025 19:24:32 +0800 Subject: [PATCH 4/9] Use OnLoaded to ensure the wallpaper file is not locked --- Flow.Launcher/Helper/WallpaperPathRetrieval.cs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Flow.Launcher/Helper/WallpaperPathRetrieval.cs b/Flow.Launcher/Helper/WallpaperPathRetrieval.cs index aa41c999852..57c1bbe8d4a 100644 --- a/Flow.Launcher/Helper/WallpaperPathRetrieval.cs +++ b/Flow.Launcher/Helper/WallpaperPathRetrieval.cs @@ -75,7 +75,9 @@ public static Brush GetWallpaperBrush() // Set DecodePixelWidth and DecodePixelHeight to resize the image while preserving aspect ratio var bitmap = new BitmapImage(); bitmap.BeginInit(); + bitmap.CacheOption = BitmapCacheOption.OnLoad; // Use OnLoaded to ensure the wallpaper file is not locked bitmap.UriSource = new Uri(wallpaperPath); + bitmap.CreateOptions = BitmapCreateOptions.IgnoreColorProfile; bitmap.DecodePixelWidth = decodedPixelWidth; bitmap.DecodePixelHeight = decodedPixelHeight; bitmap.EndInit(); From 354b04bea449091efa5bd61e182d133552367391 Mon Sep 17 00:00:00 2001 From: Jack251970 <1160210343@qq.com> Date: Wed, 17 Sep 2025 19:29:36 +0800 Subject: [PATCH 5/9] Add code comments --- Flow.Launcher/Helper/WallpaperPathRetrieval.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/Flow.Launcher/Helper/WallpaperPathRetrieval.cs b/Flow.Launcher/Helper/WallpaperPathRetrieval.cs index 57c1bbe8d4a..67618d760ef 100644 --- a/Flow.Launcher/Helper/WallpaperPathRetrieval.cs +++ b/Flow.Launcher/Helper/WallpaperPathRetrieval.cs @@ -50,6 +50,7 @@ public static Brush GetWallpaperBrush() } int originalWidth, originalHeight; + // Use `using ()` instead of `using var` sentence here to ensure the wallpaper file is not locked using (var fileStream = File.OpenRead(wallpaperPath)) { var decoder = BitmapDecoder.Create(fileStream, BitmapCreateOptions.DelayCreation, BitmapCacheOption.None); From e50a2772f82392234eb1389e3fad8b01dfae3e3d Mon Sep 17 00:00:00 2001 From: Jack Ye <1160210343@qq.com> Date: Wed, 17 Sep 2025 19:33:46 +0800 Subject: [PATCH 6/9] Update code comments Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --- Flow.Launcher/Helper/WallpaperPathRetrieval.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Flow.Launcher/Helper/WallpaperPathRetrieval.cs b/Flow.Launcher/Helper/WallpaperPathRetrieval.cs index 67618d760ef..fd04b3e88fc 100644 --- a/Flow.Launcher/Helper/WallpaperPathRetrieval.cs +++ b/Flow.Launcher/Helper/WallpaperPathRetrieval.cs @@ -76,7 +76,7 @@ public static Brush GetWallpaperBrush() // Set DecodePixelWidth and DecodePixelHeight to resize the image while preserving aspect ratio var bitmap = new BitmapImage(); bitmap.BeginInit(); - bitmap.CacheOption = BitmapCacheOption.OnLoad; // Use OnLoaded to ensure the wallpaper file is not locked + bitmap.CacheOption = BitmapCacheOption.OnLoad; // Use OnLoad to ensure the wallpaper file is not locked bitmap.UriSource = new Uri(wallpaperPath); bitmap.CreateOptions = BitmapCreateOptions.IgnoreColorProfile; bitmap.DecodePixelWidth = decodedPixelWidth; From fc2e3fec630f98c532ae4525b8a99eddbf3e1314 Mon Sep 17 00:00:00 2001 From: Jack251970 <1160210343@qq.com> Date: Thu, 18 Sep 2025 18:29:45 +0800 Subject: [PATCH 7/9] Improve ImageLoader performance --- Flow.Launcher.Infrastructure/Image/ImageLoader.cs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Flow.Launcher.Infrastructure/Image/ImageLoader.cs b/Flow.Launcher.Infrastructure/Image/ImageLoader.cs index 64d323de6c5..598347fd21f 100644 --- a/Flow.Launcher.Infrastructure/Image/ImageLoader.cs +++ b/Flow.Launcher.Infrastructure/Image/ImageLoader.cs @@ -22,7 +22,7 @@ public static class ImageLoader private static Lock storageLock { get; } = new(); private static BinaryStorage> _storage; private static readonly ConcurrentDictionary GuidToKey = new(); - private static IImageHashGenerator _hashGenerator; + private static ImageHashGenerator _hashGenerator; private static readonly bool EnableImageHash = true; public static ImageSource Image => ImageCache[Constant.ImageIcon, false]; public static ImageSource MissingImage => ImageCache[Constant.MissingImgIcon, false]; @@ -31,7 +31,7 @@ public static class ImageLoader public const int FullIconSize = 256; public const int FullImageSize = 320; - private static readonly string[] ImageExtensions = { ".png", ".jpg", ".jpeg", ".gif", ".bmp", ".tiff", ".ico" }; + private static readonly string[] ImageExtensions = [".png", ".jpg", ".jpeg", ".gif", ".bmp", ".tiff", ".ico"]; private static readonly string SvgExtension = ".svg"; public static async Task InitializeAsync() @@ -327,7 +327,7 @@ public static async ValueTask LoadAsync(string path, bool loadFullI return img; } - private static ImageSource LoadFullImage(string path) + private static BitmapImage LoadFullImage(string path) { BitmapImage image = new BitmapImage(); image.BeginInit(); @@ -364,7 +364,7 @@ private static ImageSource LoadFullImage(string path) return image; } - private static ImageSource LoadSvgImage(string path, bool loadFullImage = false) + private static RenderTargetBitmap LoadSvgImage(string path, bool loadFullImage = false) { // Set up drawing settings var desiredHeight = loadFullImage ? FullImageSize : SmallIconSize; From 9a597f2b4d1e3928c849b28de3d0b1b6cd7d84c8 Mon Sep 17 00:00:00 2001 From: Jack251970 <1160210343@qq.com> Date: Thu, 18 Sep 2025 18:32:13 +0800 Subject: [PATCH 8/9] Disable cache feature --- Flow.Launcher/Helper/WallpaperPathRetrieval.cs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/Flow.Launcher/Helper/WallpaperPathRetrieval.cs b/Flow.Launcher/Helper/WallpaperPathRetrieval.cs index fd04b3e88fc..fe0ff39ad74 100644 --- a/Flow.Launcher/Helper/WallpaperPathRetrieval.cs +++ b/Flow.Launcher/Helper/WallpaperPathRetrieval.cs @@ -15,8 +15,9 @@ public static class WallpaperPathRetrieval { private static readonly string ClassName = nameof(WallpaperPathRetrieval); - private const int MaxCacheSize = 3; - private static readonly Dictionary<(string, DateTime), ImageBrush> WallpaperCache = new(); + // Disable cache feature because some wallpaper applications (like Wallpaper Engine) may change wallpaper frequently + private const int MaxCacheSize = 0;//3; + private static readonly Dictionary<(string, DateTime), ImageBrush> WallpaperCache = []; private static readonly Lock CacheLock = new(); public static Brush GetWallpaperBrush() From bbc12ec04123b7006b902001e860d9c78da4ef0d Mon Sep 17 00:00:00 2001 From: Jack251970 <1160210343@qq.com> Date: Thu, 18 Sep 2025 21:45:24 +0800 Subject: [PATCH 9/9] Revert "Disable cache feature" This reverts commit 9a597f2b4d1e3928c849b28de3d0b1b6cd7d84c8. --- Flow.Launcher/Helper/WallpaperPathRetrieval.cs | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/Flow.Launcher/Helper/WallpaperPathRetrieval.cs b/Flow.Launcher/Helper/WallpaperPathRetrieval.cs index fe0ff39ad74..fd04b3e88fc 100644 --- a/Flow.Launcher/Helper/WallpaperPathRetrieval.cs +++ b/Flow.Launcher/Helper/WallpaperPathRetrieval.cs @@ -15,9 +15,8 @@ public static class WallpaperPathRetrieval { private static readonly string ClassName = nameof(WallpaperPathRetrieval); - // Disable cache feature because some wallpaper applications (like Wallpaper Engine) may change wallpaper frequently - private const int MaxCacheSize = 0;//3; - private static readonly Dictionary<(string, DateTime), ImageBrush> WallpaperCache = []; + private const int MaxCacheSize = 3; + private static readonly Dictionary<(string, DateTime), ImageBrush> WallpaperCache = new(); private static readonly Lock CacheLock = new(); public static Brush GetWallpaperBrush()