From 1a6733e86d0c1eb772c6f5eec4542435373bc42f Mon Sep 17 00:00:00 2001 From: Jack251970 <1160210343@qq.com> Date: Tue, 25 Feb 2025 18:06:23 +0800 Subject: [PATCH] Improve background wallpaper fetch --- .../Helper/WallpaperPathRetrieval.cs | 42 +++++++++++++++++-- .../Resources/Pages/WelcomePage2.xaml.cs | 21 +--------- .../ViewModels/SettingsPaneThemeViewModel.cs | 21 +--------- 3 files changed, 40 insertions(+), 44 deletions(-) diff --git a/Flow.Launcher/Helper/WallpaperPathRetrieval.cs b/Flow.Launcher/Helper/WallpaperPathRetrieval.cs index 8a42d480f..f79fea288 100644 --- a/Flow.Launcher/Helper/WallpaperPathRetrieval.cs +++ b/Flow.Launcher/Helper/WallpaperPathRetrieval.cs @@ -1,9 +1,10 @@ using System; +using System.Collections.Generic; +using System.IO; using System.Linq; using System.Runtime.InteropServices; -using System.Text; -using System.Windows.Documents; using System.Windows.Media; +using System.Windows.Media.Imaging; using Microsoft.Win32; using Windows.Win32; using Windows.Win32.UI.WindowsAndMessaging; @@ -14,7 +15,40 @@ public static class WallpaperPathRetrieval { private static readonly int MAX_PATH = 260; - public static unsafe string GetWallpaperPath() + private static readonly Dictionary wallpaperCache = new(); + + public static Brush GetWallpaperBrush() + { + var wallpaper = GetWallpaperPath(); + if (wallpaper is not null && File.Exists(wallpaper)) + { + // Since the wallpaper file name is the same (TranscodedWallpaper), + // we need to use the last modified date to differentiate them + var dateModified = File.GetLastWriteTime(wallpaper); + wallpaperCache.TryGetValue(dateModified, out var cachedWallpaper); + if (cachedWallpaper != null) + { + return new ImageBrush(cachedWallpaper) { Stretch = Stretch.UniformToFill }; + } + + // We should not dispose the memory stream since the bitmap is still in use + var memStream = new MemoryStream(File.ReadAllBytes(wallpaper)); + var bitmap = new BitmapImage(); + bitmap.BeginInit(); + bitmap.StreamSource = memStream; + bitmap.DecodePixelWidth = 800; + bitmap.DecodePixelHeight = 600; + bitmap.EndInit(); + bitmap.Freeze(); // Make the bitmap thread-safe + wallpaperCache[dateModified] = bitmap; + return new ImageBrush(bitmap) { Stretch = Stretch.UniformToFill }; + } + + var wallpaperColor = GetWallpaperColor(); + return new SolidColorBrush(wallpaperColor); + } + + private static unsafe string GetWallpaperPath() { var wallpaperPtr = stackalloc char[MAX_PATH]; PInvoke.SystemParametersInfo(SYSTEM_PARAMETERS_INFO_ACTION.SPI_GETDESKWALLPAPER, (uint)MAX_PATH, @@ -25,7 +59,7 @@ public static class WallpaperPathRetrieval return wallpaper.ToString(); } - public static Color GetWallpaperColor() + private static Color GetWallpaperColor() { RegistryKey key = Registry.CurrentUser.OpenSubKey(@"Control Panel\Colors", true); var result = key?.GetValue("Background", null); diff --git a/Flow.Launcher/Resources/Pages/WelcomePage2.xaml.cs b/Flow.Launcher/Resources/Pages/WelcomePage2.xaml.cs index 004e4d6d2..1ed5747cd 100644 --- a/Flow.Launcher/Resources/Pages/WelcomePage2.xaml.cs +++ b/Flow.Launcher/Resources/Pages/WelcomePage2.xaml.cs @@ -5,8 +5,6 @@ using System; using System.Windows.Navigation; using CommunityToolkit.Mvvm.Input; using Flow.Launcher.ViewModel; -using System.IO; -using System.Windows.Media.Imaging; using System.Windows.Media; namespace Flow.Launcher.Resources.Pages @@ -33,24 +31,7 @@ namespace Flow.Launcher.Resources.Pages public Brush PreviewBackground { - get - { - var wallpaper = WallpaperPathRetrieval.GetWallpaperPath(); - if (wallpaper is not null && File.Exists(wallpaper)) - { - var memStream = new MemoryStream(File.ReadAllBytes(wallpaper)); - var bitmap = new BitmapImage(); - bitmap.BeginInit(); - bitmap.StreamSource = memStream; - bitmap.DecodePixelWidth = 800; - bitmap.DecodePixelHeight = 600; - bitmap.EndInit(); - return new ImageBrush(bitmap) { Stretch = Stretch.UniformToFill }; - } - - var wallpaperColor = WallpaperPathRetrieval.GetWallpaperColor(); - return new SolidColorBrush(wallpaperColor); - } + get => WallpaperPathRetrieval.GetWallpaperBrush(); } } } diff --git a/Flow.Launcher/SettingPages/ViewModels/SettingsPaneThemeViewModel.cs b/Flow.Launcher/SettingPages/ViewModels/SettingsPaneThemeViewModel.cs index 980b2a811..ed933678d 100644 --- a/Flow.Launcher/SettingPages/ViewModels/SettingsPaneThemeViewModel.cs +++ b/Flow.Launcher/SettingPages/ViewModels/SettingsPaneThemeViewModel.cs @@ -5,7 +5,6 @@ using System.Globalization; using System.IO; using System.Linq; using System.Windows.Media; -using System.Windows.Media.Imaging; using CommunityToolkit.Mvvm.Input; using Flow.Launcher.Core.Resource; using Flow.Launcher.Helper; @@ -14,7 +13,6 @@ using Flow.Launcher.Infrastructure.UserSettings; using Flow.Launcher.Plugin; using Flow.Launcher.ViewModel; using ModernWpf; -using Flow.Launcher.Core; using ThemeManager = Flow.Launcher.Core.Resource.ThemeManager; using ThemeManagerForColorSchemeSwitch = ModernWpf.ThemeManager; @@ -212,24 +210,7 @@ public partial class SettingsPaneThemeViewModel : BaseModel public Brush PreviewBackground { - get - { - var wallpaper = WallpaperPathRetrieval.GetWallpaperPath(); - if (wallpaper is not null && File.Exists(wallpaper)) - { - var memStream = new MemoryStream(File.ReadAllBytes(wallpaper)); - var bitmap = new BitmapImage(); - bitmap.BeginInit(); - bitmap.StreamSource = memStream; - bitmap.DecodePixelWidth = 800; - bitmap.DecodePixelHeight = 600; - bitmap.EndInit(); - return new ImageBrush(bitmap) { Stretch = Stretch.UniformToFill }; - } - - var wallpaperColor = WallpaperPathRetrieval.GetWallpaperColor(); - return new SolidColorBrush(wallpaperColor); - } + get => WallpaperPathRetrieval.GetWallpaperBrush(); } public ResultsViewModel PreviewResults