From 54f02e04e6d67d46152e2480d553ec7628ed9980 Mon Sep 17 00:00:00 2001 From: Jack251970 <1160210343@qq.com> Date: Sun, 5 Jan 2025 00:08:56 +0800 Subject: [PATCH] Use public api for ProgressBoxEx & Add support for force close event --- Flow.Launcher.Core/ProgressBoxEx.xaml.cs | 45 +++++++++++++++---- Flow.Launcher.Plugin/IProgressBoxEx.cs | 20 +++++++++ Flow.Launcher.Plugin/Interfaces/IPublicAPI.cs | 11 ++++- Flow.Launcher/PublicAPIInstance.cs | 2 + .../PluginsManager.cs | 7 ++- 5 files changed, 72 insertions(+), 13 deletions(-) create mode 100644 Flow.Launcher.Plugin/IProgressBoxEx.cs diff --git a/Flow.Launcher.Core/ProgressBoxEx.xaml.cs b/Flow.Launcher.Core/ProgressBoxEx.xaml.cs index 61ba397de..b6801d2c1 100644 --- a/Flow.Launcher.Core/ProgressBoxEx.xaml.cs +++ b/Flow.Launcher.Core/ProgressBoxEx.xaml.cs @@ -2,26 +2,31 @@ using System.Windows; using System.Windows.Input; using Flow.Launcher.Infrastructure.Logger; +using Flow.Launcher.Plugin; namespace Flow.Launcher.Core { - public partial class ProgressBoxEx : Window + public partial class ProgressBoxEx : Window, IProgressBoxEx { - private ProgressBoxEx() + private readonly Action _forceClosed; + private bool _isClosed; + + private ProgressBoxEx(Action forceClosed) { + _forceClosed = forceClosed; InitializeComponent(); } - public static ProgressBoxEx Show(string caption) + public static IProgressBoxEx Show(string caption, Action forceClosed) { if (!Application.Current.Dispatcher.CheckAccess()) { - return Application.Current.Dispatcher.Invoke(() => Show(caption)); + return Application.Current.Dispatcher.Invoke(() => Show(caption, forceClosed)); } try { - var prgBox = new ProgressBoxEx + var prgBox = new ProgressBoxEx(forceClosed) { Title = caption }; @@ -51,6 +56,7 @@ namespace Flow.Launcher.Core else if (progress >= 100) { ProgressBar.Value = 100; + Close(); } else { @@ -58,19 +64,42 @@ namespace Flow.Launcher.Core } } + private new void Close() + { + if (_isClosed) + { + return; + } + + base.Close(); + _isClosed = true; + } + private void KeyEsc_OnPress(object sender, ExecutedRoutedEventArgs e) { - Close(); + ForceClose(); } private void Button_Click(object sender, RoutedEventArgs e) { - Close(); + ForceClose(); } private void Button_Cancel(object sender, RoutedEventArgs e) { - Close(); + ForceClose(); + } + + private void ForceClose() + { + if (_isClosed) + { + return; + } + + base.Close(); + _isClosed = true; + _forceClosed?.Invoke(); } } } diff --git a/Flow.Launcher.Plugin/IProgressBoxEx.cs b/Flow.Launcher.Plugin/IProgressBoxEx.cs new file mode 100644 index 000000000..0b245411f --- /dev/null +++ b/Flow.Launcher.Plugin/IProgressBoxEx.cs @@ -0,0 +1,20 @@ +namespace Flow.Launcher.Plugin; + +/// +/// Interface for progress box +/// +public interface IProgressBoxEx +{ + /// + /// Show progress box + /// + /// + /// Progress value. Should be between 0 and 100. When progress is 100, the progress box will be closed. + /// + public void ReportProgress(double progress); + + /// + /// Close progress box. + /// + public void Close(); +} diff --git a/Flow.Launcher.Plugin/Interfaces/IPublicAPI.cs b/Flow.Launcher.Plugin/Interfaces/IPublicAPI.cs index a0186b7a2..9cd45a1d3 100644 --- a/Flow.Launcher.Plugin/Interfaces/IPublicAPI.cs +++ b/Flow.Launcher.Plugin/Interfaces/IPublicAPI.cs @@ -1,4 +1,4 @@ -using Flow.Launcher.Plugin.SharedModels; +using Flow.Launcher.Plugin.SharedModels; using JetBrains.Annotations; using System; using System.Collections.Generic; @@ -316,5 +316,14 @@ namespace Flow.Launcher.Plugin /// Specifies the default result of the message box. /// Specifies which message box button is clicked by the user. public MessageBoxResult ShowMsgBox(string messageBoxText, string caption = "", MessageBoxButton button = MessageBoxButton.OK, MessageBoxImage icon = MessageBoxImage.None, MessageBoxResult defaultResult = MessageBoxResult.OK); + + /// + /// Displays a standardised Flow message box. + /// If there is issue when showing the message box, it will return null. + /// + /// The caption of the message box. + /// When user closes the progress box manually by button or esc key, this action will be called. + /// A progress box interface. + public IProgressBoxEx ShowProgressBox(string caption, Action forceClosed = null); } } diff --git a/Flow.Launcher/PublicAPIInstance.cs b/Flow.Launcher/PublicAPIInstance.cs index f0295cf24..b403d6046 100644 --- a/Flow.Launcher/PublicAPIInstance.cs +++ b/Flow.Launcher/PublicAPIInstance.cs @@ -324,6 +324,8 @@ namespace Flow.Launcher public MessageBoxResult ShowMsgBox(string messageBoxText, string caption = "", MessageBoxButton button = MessageBoxButton.OK, MessageBoxImage icon = MessageBoxImage.None, MessageBoxResult defaultResult = MessageBoxResult.OK) => MessageBoxEx.Show(messageBoxText, caption, button, icon, defaultResult); + public IProgressBoxEx ShowProgressBox(string caption, Action forceClosed = null) => ProgressBoxEx.Show(caption, forceClosed); + #endregion #region Private Methods diff --git a/Plugins/Flow.Launcher.Plugin.PluginsManager/PluginsManager.cs b/Plugins/Flow.Launcher.Plugin.PluginsManager/PluginsManager.cs index 39390ae10..f7383ff2f 100644 --- a/Plugins/Flow.Launcher.Plugin.PluginsManager/PluginsManager.cs +++ b/Plugins/Flow.Launcher.Plugin.PluginsManager/PluginsManager.cs @@ -1,5 +1,4 @@ -using Flow.Launcher.Core; -using Flow.Launcher.Core.ExternalPlugins; +using Flow.Launcher.Core.ExternalPlugins; using Flow.Launcher.Core.Plugin; using Flow.Launcher.Infrastructure; using Flow.Launcher.Infrastructure.Http; @@ -143,7 +142,7 @@ namespace Flow.Launcher.Plugin.PluginsManager var filePath = Path.Combine(Path.GetTempPath(), downloadFilename); - ProgressBoxEx prgBox = null; + IProgressBoxEx prgBox = null; try { if (!plugin.IsFromLocalInstallPath) @@ -159,7 +158,7 @@ namespace Flow.Launcher.Plugin.PluginsManager var totalBytes = response.Content.Headers.ContentLength ?? -1L; var canReportProgress = totalBytes != -1; - if (canReportProgress && (prgBox = ProgressBoxEx.Show("Download plugin...")) != null) + if (canReportProgress && (prgBox = Context.API.ShowProgressBox("Download plugin...")) != null) { await using var contentStream = await response.Content.ReadAsStreamAsync().ConfigureAwait(false); await using var fileStream = new FileStream(filePath, FileMode.Create, FileAccess.Write, FileShare.None, 8192, true);