From 9e868e7e3fec02ac340b79cc1f9d505d33616176 Mon Sep 17 00:00:00 2001 From: Jack251970 <1160210343@qq.com> Date: Sun, 29 Jun 2025 22:35:14 +0800 Subject: [PATCH] Move plugin installer location --- .../Plugin/PluginInstaller.cs | 100 +++++++++--------- .../SettingsPanePluginStoreViewModel.cs | 4 +- .../ViewModel/PluginStoreItemViewModel.cs | 6 +- Flow.Launcher/ViewModel/PluginViewModel.cs | 3 +- .../ViewModel/SelectBrowserViewModel.cs | 1 - 5 files changed, 58 insertions(+), 56 deletions(-) rename Flow.Launcher/Helper/PluginInstallationHelper.cs => Flow.Launcher.Core/Plugin/PluginInstaller.cs (71%) diff --git a/Flow.Launcher/Helper/PluginInstallationHelper.cs b/Flow.Launcher.Core/Plugin/PluginInstaller.cs similarity index 71% rename from Flow.Launcher/Helper/PluginInstallationHelper.cs rename to Flow.Launcher.Core/Plugin/PluginInstaller.cs index ea8195e57..a69ab322e 100644 --- a/Flow.Launcher/Helper/PluginInstallationHelper.cs +++ b/Flow.Launcher.Core/Plugin/PluginInstaller.cs @@ -10,24 +10,28 @@ using CommunityToolkit.Mvvm.DependencyInjection; using Flow.Launcher.Infrastructure.UserSettings; using Flow.Launcher.Plugin; -namespace Flow.Launcher.Helper; +namespace Flow.Launcher.Core.Plugin; /// /// Helper class for installing, updating, and uninstalling plugins. /// -public static class PluginInstallationHelper +public static class PluginInstaller { - private static readonly string ClassName = nameof(PluginInstallationHelper); + private static readonly string ClassName = nameof(PluginInstaller); private static readonly Settings Settings = 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(); + public static async Task InstallPluginAndCheckRestartAsync(UserPlugin newPlugin) { - if (App.API.ShowMsgBox( + if (API.ShowMsgBox( string.Format( - App.API.GetTranslation("InstallPromptSubtitle"), + API.GetTranslation("InstallPromptSubtitle"), newPlugin.Name, newPlugin.Author, Environment.NewLine), - App.API.GetTranslation("InstallPromptTitle"), + API.GetTranslation("InstallPromptTitle"), button: MessageBoxButton.YesNo) != MessageBoxResult.Yes) return; try @@ -44,7 +48,7 @@ public static class PluginInstallationHelper if (!newPlugin.IsFromLocalInstallPath) { await DownloadFileAsync( - $"{App.API.GetTranslation("DownloadingPlugin")} {newPlugin.Name}", + $"{API.GetTranslation("DownloadingPlugin")} {newPlugin.Name}", newPlugin.UrlDownload, filePath, cts); } else @@ -63,7 +67,7 @@ public static class PluginInstallationHelper throw new FileNotFoundException($"Plugin {newPlugin.ID} zip file not found at {filePath}", filePath); } - App.API.InstallPlugin(newPlugin, filePath); + API.InstallPlugin(newPlugin, filePath); if (!newPlugin.IsFromLocalInstallPath) { @@ -72,21 +76,21 @@ public static class PluginInstallationHelper } catch (Exception e) { - App.API.LogException(ClassName, "Failed to install plugin", e); - App.API.ShowMsgError(App.API.GetTranslation("ErrorInstallingPlugin")); + API.LogException(ClassName, "Failed to install plugin", e); + API.ShowMsgError(API.GetTranslation("ErrorInstallingPlugin")); return; // don’t restart on failure } if (Settings.AutoRestartAfterChanging) { - App.API.RestartApp(); + API.RestartApp(); } else { - App.API.ShowMsg( - App.API.GetTranslation("installbtn"), + API.ShowMsg( + API.GetTranslation("installbtn"), string.Format( - App.API.GetTranslation( + API.GetTranslation( "InstallSuccessNoRestart"), newPlugin.Name)); } @@ -108,17 +112,17 @@ public static class PluginInstallationHelper } catch (Exception e) { - App.API.LogException(ClassName, "Failed to validate zip file", e); - App.API.ShowMsgError(App.API.GetTranslation("ZipFileNotHavePluginJson")); + API.LogException(ClassName, "Failed to validate zip file", e); + API.ShowMsgError(API.GetTranslation("ZipFileNotHavePluginJson")); return; } if (Settings.ShowUnknownSourceWarning) { if (!InstallSourceKnown(plugin.Website) - && App.API.ShowMsgBox(string.Format( - App.API.GetTranslation("InstallFromUnknownSourceSubtitle"), Environment.NewLine), - App.API.GetTranslation("InstallFromUnknownSourceTitle"), + && API.ShowMsgBox(string.Format( + API.GetTranslation("InstallFromUnknownSourceSubtitle"), Environment.NewLine), + API.GetTranslation("InstallFromUnknownSourceTitle"), MessageBoxButton.YesNo) == MessageBoxResult.No) return; } @@ -128,39 +132,39 @@ public static class PluginInstallationHelper public static async Task UninstallPluginAndCheckRestartAsync(PluginMetadata oldPlugin) { - if (App.API.ShowMsgBox( + if (API.ShowMsgBox( string.Format( - App.API.GetTranslation("UninstallPromptSubtitle"), + API.GetTranslation("UninstallPromptSubtitle"), oldPlugin.Name, oldPlugin.Author, Environment.NewLine), - App.API.GetTranslation("UninstallPromptTitle"), + API.GetTranslation("UninstallPromptTitle"), button: MessageBoxButton.YesNo) != MessageBoxResult.Yes) return; - var removePluginSettings = App.API.ShowMsgBox( - App.API.GetTranslation("KeepPluginSettingsSubtitle"), - App.API.GetTranslation("KeepPluginSettingsTitle"), + var removePluginSettings = API.ShowMsgBox( + API.GetTranslation("KeepPluginSettingsSubtitle"), + API.GetTranslation("KeepPluginSettingsTitle"), button: MessageBoxButton.YesNo) == MessageBoxResult.No; try { - await App.API.UninstallPluginAsync(oldPlugin, removePluginSettings); + await API.UninstallPluginAsync(oldPlugin, removePluginSettings); } catch (Exception e) { - App.API.LogException(ClassName, "Failed to uninstall plugin", e); - App.API.ShowMsgError(App.API.GetTranslation("ErrorUninstallingPlugin")); + API.LogException(ClassName, "Failed to uninstall plugin", e); + API.ShowMsgError(API.GetTranslation("ErrorUninstallingPlugin")); return; // don’t restart on failure } if (Settings.AutoRestartAfterChanging) { - App.API.RestartApp(); + API.RestartApp(); } else { - App.API.ShowMsg( - App.API.GetTranslation("uninstallbtn"), + API.ShowMsg( + API.GetTranslation("uninstallbtn"), string.Format( - App.API.GetTranslation( + API.GetTranslation( "UninstallSuccessNoRestart"), oldPlugin.Name)); } @@ -168,11 +172,11 @@ public static class PluginInstallationHelper public static async Task UpdatePluginAndCheckRestartAsync(UserPlugin newPlugin, PluginMetadata oldPlugin) { - if (App.API.ShowMsgBox( + if (API.ShowMsgBox( string.Format( - App.API.GetTranslation("UpdatePromptSubtitle"), + API.GetTranslation("UpdatePromptSubtitle"), oldPlugin.Name, oldPlugin.Author, Environment.NewLine), - App.API.GetTranslation("UpdatePromptTitle"), + API.GetTranslation("UpdatePromptTitle"), button: MessageBoxButton.YesNo) != MessageBoxResult.Yes) return; try @@ -184,7 +188,7 @@ public static class PluginInstallationHelper if (!newPlugin.IsFromLocalInstallPath) { await DownloadFileAsync( - $"{App.API.GetTranslation("DownloadingPlugin")} {newPlugin.Name}", + $"{API.GetTranslation("DownloadingPlugin")} {newPlugin.Name}", newPlugin.UrlDownload, filePath, cts); } else @@ -198,25 +202,25 @@ public static class PluginInstallationHelper return; } - await App.API.UpdatePluginAsync(oldPlugin, newPlugin, filePath); + await API.UpdatePluginAsync(oldPlugin, newPlugin, filePath); } catch (Exception e) { - App.API.LogException(ClassName, "Failed to update plugin", e); - App.API.ShowMsgError(App.API.GetTranslation("ErrorUpdatingPlugin")); + API.LogException(ClassName, "Failed to update plugin", e); + API.ShowMsgError(API.GetTranslation("ErrorUpdatingPlugin")); return; // don’t restart on failure } if (Settings.AutoRestartAfterChanging) { - App.API.RestartApp(); + API.RestartApp(); } else { - App.API.ShowMsg( - App.API.GetTranslation("updatebtn"), + API.ShowMsg( + API.GetTranslation("updatebtn"), string.Format( - App.API.GetTranslation( + API.GetTranslation( "UpdateSuccessNoRestart"), newPlugin.Name)); } @@ -230,7 +234,7 @@ public static class PluginInstallationHelper if (showProgress) { var exceptionHappened = false; - await App.API.ShowProgressBoxAsync(prgBoxTitle, + await API.ShowProgressBoxAsync(prgBoxTitle, async (reportProgress) => { if (reportProgress == null) @@ -242,18 +246,18 @@ public static class PluginInstallationHelper } else { - await App.API.HttpDownloadAsync(downloadUrl, filePath, reportProgress, cts.Token).ConfigureAwait(false); + await API.HttpDownloadAsync(downloadUrl, filePath, reportProgress, cts.Token).ConfigureAwait(false); } }, cts.Cancel); // if exception happened while downloading and user does not cancel downloading, // we need to redownload the plugin if (exceptionHappened && (!cts.IsCancellationRequested)) - await App.API.HttpDownloadAsync(downloadUrl, filePath, token: cts.Token).ConfigureAwait(false); + await API.HttpDownloadAsync(downloadUrl, filePath, token: cts.Token).ConfigureAwait(false); } else { - await App.API.HttpDownloadAsync(downloadUrl, filePath, token: cts.Token).ConfigureAwait(false); + await API.HttpDownloadAsync(downloadUrl, filePath, token: cts.Token).ConfigureAwait(false); } } @@ -275,7 +279,7 @@ public static class PluginInstallationHelper if (!Uri.TryCreate(url, UriKind.Absolute, out var uri) || uri.Host != acceptedHost) return false; - return App.API.GetAllPlugins().Any(x => + return API.GetAllPlugins().Any(x => !string.IsNullOrEmpty(x.Metadata.Website) && x.Metadata.Website.StartsWith(constructedUrlPart) ); diff --git a/Flow.Launcher/SettingPages/ViewModels/SettingsPanePluginStoreViewModel.cs b/Flow.Launcher/SettingPages/ViewModels/SettingsPanePluginStoreViewModel.cs index 2b12ba70f..efe67d016 100644 --- a/Flow.Launcher/SettingPages/ViewModels/SettingsPanePluginStoreViewModel.cs +++ b/Flow.Launcher/SettingPages/ViewModels/SettingsPanePluginStoreViewModel.cs @@ -3,7 +3,7 @@ using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using CommunityToolkit.Mvvm.Input; -using Flow.Launcher.Helper; +using Flow.Launcher.Core.Plugin; using Flow.Launcher.Plugin; using Flow.Launcher.ViewModel; @@ -106,7 +106,7 @@ public partial class SettingsPanePluginStoreViewModel : BaseModel $"{App.API.GetTranslation("ZipFiles")} (*.zip)|*.zip"); if (!string.IsNullOrEmpty(file)) - await PluginInstallationHelper.InstallPluginAndCheckRestartAsync(file); + await PluginInstaller.InstallPluginAndCheckRestartAsync(file); } private static string GetFileFromDialog(string title, string filter = "") diff --git a/Flow.Launcher/ViewModel/PluginStoreItemViewModel.cs b/Flow.Launcher/ViewModel/PluginStoreItemViewModel.cs index f03d2740e..a985ca7ff 100644 --- a/Flow.Launcher/ViewModel/PluginStoreItemViewModel.cs +++ b/Flow.Launcher/ViewModel/PluginStoreItemViewModel.cs @@ -66,13 +66,13 @@ namespace Flow.Launcher.ViewModel switch (action) { case "install": - await PluginInstallationHelper.InstallPluginAndCheckRestartAsync(_newPlugin); + await PluginInstaller.InstallPluginAndCheckRestartAsync(_newPlugin); break; case "uninstall": - await PluginInstallationHelper.UninstallPluginAndCheckRestartAsync(_oldPluginPair.Metadata); + await PluginInstaller.UninstallPluginAndCheckRestartAsync(_oldPluginPair.Metadata); break; case "update": - await PluginInstallationHelper.UpdatePluginAndCheckRestartAsync(_newPlugin, _oldPluginPair.Metadata); + await PluginInstaller.UpdatePluginAndCheckRestartAsync(_newPlugin, _oldPluginPair.Metadata); break; default: break; diff --git a/Flow.Launcher/ViewModel/PluginViewModel.cs b/Flow.Launcher/ViewModel/PluginViewModel.cs index 131972e85..ea222d023 100644 --- a/Flow.Launcher/ViewModel/PluginViewModel.cs +++ b/Flow.Launcher/ViewModel/PluginViewModel.cs @@ -5,7 +5,6 @@ using System.Windows.Media; using CommunityToolkit.Mvvm.DependencyInjection; using CommunityToolkit.Mvvm.Input; using Flow.Launcher.Core.Plugin; -using Flow.Launcher.Helper; using Flow.Launcher.Infrastructure.Image; using Flow.Launcher.Infrastructure.UserSettings; using Flow.Launcher.Plugin; @@ -173,7 +172,7 @@ namespace Flow.Launcher.ViewModel [RelayCommand] private async Task OpenDeletePluginWindowAsync() { - await PluginInstallationHelper.UninstallPluginAndCheckRestartAsync(PluginPair.Metadata); + await PluginInstaller.UninstallPluginAndCheckRestartAsync(PluginPair.Metadata); } [RelayCommand] diff --git a/Flow.Launcher/ViewModel/SelectBrowserViewModel.cs b/Flow.Launcher/ViewModel/SelectBrowserViewModel.cs index 1eee6dba5..67bbbd930 100644 --- a/Flow.Launcher/ViewModel/SelectBrowserViewModel.cs +++ b/Flow.Launcher/ViewModel/SelectBrowserViewModel.cs @@ -1,6 +1,5 @@ using System.Collections.ObjectModel; using System.Linq; -using System.Windows; using CommunityToolkit.Mvvm.Input; using Flow.Launcher.Infrastructure.UserSettings; using Flow.Launcher.Plugin;