Skip to content

Commit cb48f81

Browse files
authored
Merge branch 'dev' into loading_bar
2 parents c3a8a02 + 79d54d0 commit cb48f81

File tree

9 files changed

+82
-56
lines changed

9 files changed

+82
-56
lines changed

Flow.Launcher.Core/Plugin/PluginManager.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,9 @@ public static class PluginManager
2929
public static readonly HashSet<PluginPair> GlobalPlugins = new();
3030
public static readonly Dictionary<string, PluginPair> NonGlobalPlugins = new();
3131

32-
public static IPublicAPI API { get; private set; } = Ioc.Default.GetRequiredService<IPublicAPI>();
32+
// We should not initialize API in static constructor because it will create another API instance
33+
private static IPublicAPI api = null;
34+
private static IPublicAPI API => api ??= Ioc.Default.GetRequiredService<IPublicAPI>();
3335

3436
private static PluginsSettings Settings;
3537
private static List<PluginMetadata> _metadatas;

Flow.Launcher/Helper/WallpaperPathRetrieval.cs

Lines changed: 68 additions & 4 deletions
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/MainWindow.xaml.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -438,7 +438,7 @@ private void CheckFirstLaunch()
438438
if (_settings.FirstLaunch)
439439
{
440440
_settings.FirstLaunch = false;
441-
PluginManager.API.SaveAppAllSettings();
441+
App.API.SaveAppAllSettings();
442442
OpenWelcomeWindow();
443443
}
444444
}

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/SettingsPaneAboutViewModel.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
using System.Windows;
77
using CommunityToolkit.Mvvm.Input;
88
using Flow.Launcher.Core;
9-
using Flow.Launcher.Core.Plugin;
109
using Flow.Launcher.Core.Resource;
1110
using Flow.Launcher.Infrastructure;
1211
using Flow.Launcher.Infrastructure.UserSettings;
@@ -77,15 +76,15 @@ private void AskClearLogFolderConfirmation()
7776
[RelayCommand]
7877
private void OpenSettingsFolder()
7978
{
80-
PluginManager.API.OpenDirectory(Path.Combine(DataLocation.DataDirectory(), Constant.Settings));
79+
App.API.OpenDirectory(Path.Combine(DataLocation.DataDirectory(), Constant.Settings));
8180
}
8281

8382
[RelayCommand]
8483
private void OpenParentOfSettingsFolder(object parameter)
8584
{
8685
string settingsFolderPath = Path.Combine(DataLocation.DataDirectory(), Constant.Settings);
8786
string parentFolderPath = Path.GetDirectoryName(settingsFolderPath);
88-
PluginManager.API.OpenDirectory(parentFolderPath);
87+
App.API.OpenDirectory(parentFolderPath);
8988
}
9089

9190

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

Flow.Launcher/SettingPages/Views/SettingsPanePluginStore.xaml.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
using System.Windows.Data;
44
using System.Windows.Input;
55
using System.Windows.Navigation;
6-
using Flow.Launcher.Core.Plugin;
76
using Flow.Launcher.SettingPages.ViewModels;
87
using Flow.Launcher.ViewModel;
98

@@ -49,7 +48,7 @@ private void SettingsPanePlugins_OnKeyDown(object sender, KeyEventArgs e)
4948

5049
private void Hyperlink_OnRequestNavigate(object sender, RequestNavigateEventArgs e)
5150
{
52-
PluginManager.API.OpenUrl(e.Uri.AbsoluteUri);
51+
App.API.OpenUrl(e.Uri.AbsoluteUri);
5352
e.Handled = true;
5453
}
5554

Flow.Launcher/ViewModel/MainViewModel.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -442,7 +442,7 @@ private void OpenSetting()
442442
[RelayCommand]
443443
private void SelectHelp()
444444
{
445-
PluginManager.API.OpenUrl("https://www.flowlauncher.com/docs/#/usage-tips");
445+
App.API.OpenUrl("https://www.flowlauncher.com/docs/#/usage-tips");
446446
}
447447

448448
[RelayCommand]

Flow.Launcher/ViewModel/PluginViewModel.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -134,20 +134,20 @@ private void OpenPluginDirectory()
134134
{
135135
var directory = PluginPair.Metadata.PluginDirectory;
136136
if (!string.IsNullOrEmpty(directory))
137-
PluginManager.API.OpenDirectory(directory);
137+
App.API.OpenDirectory(directory);
138138
}
139139

140140
[RelayCommand]
141141
private void OpenSourceCodeLink()
142142
{
143-
PluginManager.API.OpenUrl(PluginPair.Metadata.Website);
143+
App.API.OpenUrl(PluginPair.Metadata.Website);
144144
}
145145

146146
[RelayCommand]
147147
private void OpenDeletePluginWindow()
148148
{
149-
PluginManager.API.ChangeQuery($"{PluginManagerActionKeyword} uninstall {PluginPair.Metadata.Name}".Trim(), true);
150-
PluginManager.API.ShowMainWindow();
149+
App.API.ChangeQuery($"{PluginManagerActionKeyword} uninstall {PluginPair.Metadata.Name}".Trim(), true);
150+
App.API.ShowMainWindow();
151151
}
152152

153153
public static bool IsActionKeywordRegistered(string newActionKeyword) => PluginManager.ActionKeywordRegistered(newActionKeyword);

0 commit comments

Comments
 (0)