From c06042f96bee6f65fe1b4b761643a2e2bd495978 Mon Sep 17 00:00:00 2001 From: Jack251970 <1160210343@qq.com> Date: Fri, 28 Feb 2025 17:03:53 +0800 Subject: [PATCH] Fix API instance create twice issue & Make PluginManager.API private --- Flow.Launcher.Core/Plugin/PluginManager.cs | 4 +++- Flow.Launcher/MainWindow.xaml.cs | 2 +- .../SettingPages/ViewModels/SettingsPaneAboutViewModel.cs | 5 ++--- .../SettingPages/Views/SettingsPanePluginStore.xaml.cs | 3 +-- Flow.Launcher/ViewModel/MainViewModel.cs | 2 +- Flow.Launcher/ViewModel/PluginViewModel.cs | 8 ++++---- 6 files changed, 12 insertions(+), 12 deletions(-) diff --git a/Flow.Launcher.Core/Plugin/PluginManager.cs b/Flow.Launcher.Core/Plugin/PluginManager.cs index 6e7b5ec6002..09711051e24 100644 --- a/Flow.Launcher.Core/Plugin/PluginManager.cs +++ b/Flow.Launcher.Core/Plugin/PluginManager.cs @@ -29,7 +29,9 @@ public static class PluginManager public static readonly HashSet GlobalPlugins = new(); public static readonly Dictionary NonGlobalPlugins = new(); - public static IPublicAPI API { get; private set; } = Ioc.Default.GetRequiredService(); + // We should not initialize API in static constructor because it will create another API instance + private static IPublicAPI api = null; + private static IPublicAPI API => api ??= Ioc.Default.GetRequiredService(); private static PluginsSettings Settings; private static List _metadatas; diff --git a/Flow.Launcher/MainWindow.xaml.cs b/Flow.Launcher/MainWindow.xaml.cs index 41dc68fd924..3f1bae090a3 100644 --- a/Flow.Launcher/MainWindow.xaml.cs +++ b/Flow.Launcher/MainWindow.xaml.cs @@ -438,7 +438,7 @@ private void CheckFirstLaunch() if (_settings.FirstLaunch) { _settings.FirstLaunch = false; - PluginManager.API.SaveAppAllSettings(); + App.API.SaveAppAllSettings(); OpenWelcomeWindow(); } } diff --git a/Flow.Launcher/SettingPages/ViewModels/SettingsPaneAboutViewModel.cs b/Flow.Launcher/SettingPages/ViewModels/SettingsPaneAboutViewModel.cs index cb434f399f0..ade65028472 100644 --- a/Flow.Launcher/SettingPages/ViewModels/SettingsPaneAboutViewModel.cs +++ b/Flow.Launcher/SettingPages/ViewModels/SettingsPaneAboutViewModel.cs @@ -6,7 +6,6 @@ using System.Windows; using CommunityToolkit.Mvvm.Input; using Flow.Launcher.Core; -using Flow.Launcher.Core.Plugin; using Flow.Launcher.Core.Resource; using Flow.Launcher.Infrastructure; using Flow.Launcher.Infrastructure.UserSettings; @@ -77,7 +76,7 @@ private void AskClearLogFolderConfirmation() [RelayCommand] private void OpenSettingsFolder() { - PluginManager.API.OpenDirectory(Path.Combine(DataLocation.DataDirectory(), Constant.Settings)); + App.API.OpenDirectory(Path.Combine(DataLocation.DataDirectory(), Constant.Settings)); } [RelayCommand] @@ -85,7 +84,7 @@ private void OpenParentOfSettingsFolder(object parameter) { string settingsFolderPath = Path.Combine(DataLocation.DataDirectory(), Constant.Settings); string parentFolderPath = Path.GetDirectoryName(settingsFolderPath); - PluginManager.API.OpenDirectory(parentFolderPath); + App.API.OpenDirectory(parentFolderPath); } diff --git a/Flow.Launcher/SettingPages/Views/SettingsPanePluginStore.xaml.cs b/Flow.Launcher/SettingPages/Views/SettingsPanePluginStore.xaml.cs index dfb4a7eaf6c..db476331908 100644 --- a/Flow.Launcher/SettingPages/Views/SettingsPanePluginStore.xaml.cs +++ b/Flow.Launcher/SettingPages/Views/SettingsPanePluginStore.xaml.cs @@ -3,7 +3,6 @@ using System.Windows.Data; using System.Windows.Input; using System.Windows.Navigation; -using Flow.Launcher.Core.Plugin; using Flow.Launcher.SettingPages.ViewModels; using Flow.Launcher.ViewModel; @@ -49,7 +48,7 @@ private void SettingsPanePlugins_OnKeyDown(object sender, KeyEventArgs e) private void Hyperlink_OnRequestNavigate(object sender, RequestNavigateEventArgs e) { - PluginManager.API.OpenUrl(e.Uri.AbsoluteUri); + App.API.OpenUrl(e.Uri.AbsoluteUri); e.Handled = true; } diff --git a/Flow.Launcher/ViewModel/MainViewModel.cs b/Flow.Launcher/ViewModel/MainViewModel.cs index 650a276109d..6b0144a0384 100644 --- a/Flow.Launcher/ViewModel/MainViewModel.cs +++ b/Flow.Launcher/ViewModel/MainViewModel.cs @@ -442,7 +442,7 @@ private void OpenSetting() [RelayCommand] private void SelectHelp() { - PluginManager.API.OpenUrl("https://www.flowlauncher.com/docs/#/usage-tips"); + App.API.OpenUrl("https://www.flowlauncher.com/docs/#/usage-tips"); } [RelayCommand] diff --git a/Flow.Launcher/ViewModel/PluginViewModel.cs b/Flow.Launcher/ViewModel/PluginViewModel.cs index bae9292bff7..46f8e00a222 100644 --- a/Flow.Launcher/ViewModel/PluginViewModel.cs +++ b/Flow.Launcher/ViewModel/PluginViewModel.cs @@ -134,20 +134,20 @@ private void OpenPluginDirectory() { var directory = PluginPair.Metadata.PluginDirectory; if (!string.IsNullOrEmpty(directory)) - PluginManager.API.OpenDirectory(directory); + App.API.OpenDirectory(directory); } [RelayCommand] private void OpenSourceCodeLink() { - PluginManager.API.OpenUrl(PluginPair.Metadata.Website); + App.API.OpenUrl(PluginPair.Metadata.Website); } [RelayCommand] private void OpenDeletePluginWindow() { - PluginManager.API.ChangeQuery($"{PluginManagerActionKeyword} uninstall {PluginPair.Metadata.Name}".Trim(), true); - PluginManager.API.ShowMainWindow(); + App.API.ChangeQuery($"{PluginManagerActionKeyword} uninstall {PluginPair.Metadata.Name}".Trim(), true); + App.API.ShowMainWindow(); } public static bool IsActionKeywordRegistered(string newActionKeyword) => PluginManager.ActionKeywordRegistered(newActionKeyword);