Skip to content

Commit dbf51a3

Browse files
authored
Merge pull request #3279 from Jack251970/wallpaper__retrieval
Improve Wallpaper Retrieval
2 parents 65f85cf + d8c547f commit dbf51a3

File tree

3 files changed

+70
-44
lines changed

3 files changed

+70
-44
lines changed

Flow.Launcher/Helper/WallpaperPathRetrieval.cs

+68-4
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
using System;
2+
using System.Collections.Generic;
3+
using System.IO;
24
using System.Linq;
35
using System.Runtime.InteropServices;
4-
using System.Text;
5-
using System.Windows.Documents;
6+
using System.Windows;
67
using System.Windows.Media;
8+
using System.Windows.Media.Imaging;
79
using Microsoft.Win32;
810
using Windows.Win32;
911
using Windows.Win32.UI.WindowsAndMessaging;
@@ -13,8 +15,70 @@ namespace Flow.Launcher.Helper;
1315
public static class WallpaperPathRetrieval
1416
{
1517
private static readonly int MAX_PATH = 260;
18+
private static readonly int MAX_CACHE_SIZE = 3;
1619

17-
public static unsafe string GetWallpaperPath()
20+
private static readonly Dictionary<(string, DateTime), ImageBrush> wallpaperCache = new();
21+
22+
public static Brush GetWallpaperBrush()
23+
{
24+
// Invoke the method on the UI thread
25+
if (!Application.Current.Dispatcher.CheckAccess())
26+
{
27+
return Application.Current.Dispatcher.Invoke(GetWallpaperBrush);
28+
}
29+
30+
try
31+
{
32+
var wallpaperPath = GetWallpaperPath();
33+
if (wallpaperPath is not null && File.Exists(wallpaperPath))
34+
{
35+
// Since the wallpaper file name can be the same (TranscodedWallpaper),
36+
// we need to add the last modified date to differentiate them
37+
var dateModified = File.GetLastWriteTime(wallpaperPath);
38+
wallpaperCache.TryGetValue((wallpaperPath, dateModified), out var cachedWallpaper);
39+
if (cachedWallpaper != null)
40+
{
41+
return cachedWallpaper;
42+
}
43+
44+
// We should not dispose the memory stream since the bitmap is still in use
45+
var memStream = new MemoryStream(File.ReadAllBytes(wallpaperPath));
46+
var bitmap = new BitmapImage();
47+
bitmap.BeginInit();
48+
bitmap.StreamSource = memStream;
49+
bitmap.DecodePixelWidth = 800;
50+
bitmap.DecodePixelHeight = 600;
51+
bitmap.EndInit();
52+
bitmap.Freeze(); // Make the bitmap thread-safe
53+
var wallpaperBrush = new ImageBrush(bitmap) { Stretch = Stretch.UniformToFill };
54+
wallpaperBrush.Freeze(); // Make the brush thread-safe
55+
56+
// Manage cache size
57+
if (wallpaperCache.Count >= MAX_CACHE_SIZE)
58+
{
59+
// Remove the oldest wallpaper from the cache
60+
var oldestCache = wallpaperCache.Keys.OrderBy(k => k.Item2).FirstOrDefault();
61+
if (oldestCache != default)
62+
{
63+
wallpaperCache.Remove(oldestCache);
64+
}
65+
}
66+
67+
wallpaperCache.Add((wallpaperPath, dateModified), wallpaperBrush);
68+
return wallpaperBrush;
69+
}
70+
71+
var wallpaperColor = GetWallpaperColor();
72+
return new SolidColorBrush(wallpaperColor);
73+
}
74+
catch (Exception ex)
75+
{
76+
App.API.LogException(nameof(WallpaperPathRetrieval), "Error retrieving wallpaper", ex);
77+
return new SolidColorBrush(Colors.Transparent);
78+
}
79+
}
80+
81+
private static unsafe string GetWallpaperPath()
1882
{
1983
var wallpaperPtr = stackalloc char[MAX_PATH];
2084
PInvoke.SystemParametersInfo(SYSTEM_PARAMETERS_INFO_ACTION.SPI_GETDESKWALLPAPER, (uint)MAX_PATH,
@@ -25,7 +89,7 @@ public static unsafe string GetWallpaperPath()
2589
return wallpaper.ToString();
2690
}
2791

28-
public static Color GetWallpaperColor()
92+
private static Color GetWallpaperColor()
2993
{
3094
RegistryKey key = Registry.CurrentUser.OpenSubKey(@"Control Panel\Colors", true);
3195
var result = key?.GetValue("Background", null);

Flow.Launcher/Resources/Pages/WelcomePage2.xaml.cs

+1-20
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55
using System.Windows.Navigation;
66
using CommunityToolkit.Mvvm.Input;
77
using Flow.Launcher.ViewModel;
8-
using System.IO;
9-
using System.Windows.Media.Imaging;
108
using System.Windows.Media;
119

1210
namespace Flow.Launcher.Resources.Pages
@@ -33,24 +31,7 @@ private static void SetTogglingHotkey(HotkeyModel hotkey)
3331

3432
public Brush PreviewBackground
3533
{
36-
get
37-
{
38-
var wallpaper = WallpaperPathRetrieval.GetWallpaperPath();
39-
if (wallpaper is not null && File.Exists(wallpaper))
40-
{
41-
var memStream = new MemoryStream(File.ReadAllBytes(wallpaper));
42-
var bitmap = new BitmapImage();
43-
bitmap.BeginInit();
44-
bitmap.StreamSource = memStream;
45-
bitmap.DecodePixelWidth = 800;
46-
bitmap.DecodePixelHeight = 600;
47-
bitmap.EndInit();
48-
return new ImageBrush(bitmap) { Stretch = Stretch.UniformToFill };
49-
}
50-
51-
var wallpaperColor = WallpaperPathRetrieval.GetWallpaperColor();
52-
return new SolidColorBrush(wallpaperColor);
53-
}
34+
get => WallpaperPathRetrieval.GetWallpaperBrush();
5435
}
5536
}
5637
}

Flow.Launcher/SettingPages/ViewModels/SettingsPaneThemeViewModel.cs

+1-20
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
using System.IO;
66
using System.Linq;
77
using System.Windows.Media;
8-
using System.Windows.Media.Imaging;
98
using CommunityToolkit.Mvvm.Input;
109
using Flow.Launcher.Core.Resource;
1110
using Flow.Launcher.Helper;
@@ -14,7 +13,6 @@
1413
using Flow.Launcher.Plugin;
1514
using Flow.Launcher.ViewModel;
1615
using ModernWpf;
17-
using Flow.Launcher.Core;
1816
using ThemeManager = Flow.Launcher.Core.Resource.ThemeManager;
1917
using ThemeManagerForColorSchemeSwitch = ModernWpf.ThemeManager;
2018

@@ -212,24 +210,7 @@ public bool UseDate
212210

213211
public Brush PreviewBackground
214212
{
215-
get
216-
{
217-
var wallpaper = WallpaperPathRetrieval.GetWallpaperPath();
218-
if (wallpaper is not null && File.Exists(wallpaper))
219-
{
220-
var memStream = new MemoryStream(File.ReadAllBytes(wallpaper));
221-
var bitmap = new BitmapImage();
222-
bitmap.BeginInit();
223-
bitmap.StreamSource = memStream;
224-
bitmap.DecodePixelWidth = 800;
225-
bitmap.DecodePixelHeight = 600;
226-
bitmap.EndInit();
227-
return new ImageBrush(bitmap) { Stretch = Stretch.UniformToFill };
228-
}
229-
230-
var wallpaperColor = WallpaperPathRetrieval.GetWallpaperColor();
231-
return new SolidColorBrush(wallpaperColor);
232-
}
213+
get => WallpaperPathRetrieval.GetWallpaperBrush();
233214
}
234215

235216
public ResultsViewModel PreviewResults

0 commit comments

Comments
 (0)