Skip to content

Commit 1a6733e

Browse files
committed
Improve background wallpaper fetch
1 parent c143aa4 commit 1a6733e

File tree

3 files changed

+40
-44
lines changed

3 files changed

+40
-44
lines changed

Flow.Launcher/Helper/WallpaperPathRetrieval.cs

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
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;
66
using System.Windows.Media;
7+
using System.Windows.Media.Imaging;
78
using Microsoft.Win32;
89
using Windows.Win32;
910
using Windows.Win32.UI.WindowsAndMessaging;
@@ -14,7 +15,40 @@ public static class WallpaperPathRetrieval
1415
{
1516
private static readonly int MAX_PATH = 260;
1617

17-
public static unsafe string GetWallpaperPath()
18+
private static readonly Dictionary<DateTime, BitmapImage> wallpaperCache = new();
19+
20+
public static Brush GetWallpaperBrush()
21+
{
22+
var wallpaper = GetWallpaperPath();
23+
if (wallpaper is not null && File.Exists(wallpaper))
24+
{
25+
// Since the wallpaper file name is the same (TranscodedWallpaper),
26+
// we need to use the last modified date to differentiate them
27+
var dateModified = File.GetLastWriteTime(wallpaper);
28+
wallpaperCache.TryGetValue(dateModified, out var cachedWallpaper);
29+
if (cachedWallpaper != null)
30+
{
31+
return new ImageBrush(cachedWallpaper) { Stretch = Stretch.UniformToFill };
32+
}
33+
34+
// We should not dispose the memory stream since the bitmap is still in use
35+
var memStream = new MemoryStream(File.ReadAllBytes(wallpaper));
36+
var bitmap = new BitmapImage();
37+
bitmap.BeginInit();
38+
bitmap.StreamSource = memStream;
39+
bitmap.DecodePixelWidth = 800;
40+
bitmap.DecodePixelHeight = 600;
41+
bitmap.EndInit();
42+
bitmap.Freeze(); // Make the bitmap thread-safe
43+
wallpaperCache[dateModified] = bitmap;
44+
return new ImageBrush(bitmap) { Stretch = Stretch.UniformToFill };
45+
}
46+
47+
var wallpaperColor = GetWallpaperColor();
48+
return new SolidColorBrush(wallpaperColor);
49+
}
50+
51+
private static unsafe string GetWallpaperPath()
1852
{
1953
var wallpaperPtr = stackalloc char[MAX_PATH];
2054
PInvoke.SystemParametersInfo(SYSTEM_PARAMETERS_INFO_ACTION.SPI_GETDESKWALLPAPER, (uint)MAX_PATH,
@@ -25,7 +59,7 @@ public static unsafe string GetWallpaperPath()
2559
return wallpaper.ToString();
2660
}
2761

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

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

Lines changed: 1 addition & 20 deletions
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

Lines changed: 1 addition & 20 deletions
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)