Use public api for ProgressBoxEx & Add support for force close event

This commit is contained in:
Jack251970 2025-01-05 00:08:56 +08:00
parent 562b233c15
commit 54f02e04e6
5 changed files with 72 additions and 13 deletions

View file

@ -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();
}
}
}

View file

@ -0,0 +1,20 @@
namespace Flow.Launcher.Plugin;
/// <summary>
/// Interface for progress box
/// </summary>
public interface IProgressBoxEx
{
/// <summary>
/// Show progress box
/// </summary>
/// <param name="progress">
/// Progress value. Should be between 0 and 100. When progress is 100, the progress box will be closed.
/// </param>
public void ReportProgress(double progress);
/// <summary>
/// Close progress box.
/// </summary>
public void Close();
}

View file

@ -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
/// <param name="defaultResult">Specifies the default result of the message box.</param>
/// <returns>Specifies which message box button is clicked by the user.</returns>
public MessageBoxResult ShowMsgBox(string messageBoxText, string caption = "", MessageBoxButton button = MessageBoxButton.OK, MessageBoxImage icon = MessageBoxImage.None, MessageBoxResult defaultResult = MessageBoxResult.OK);
/// <summary>
/// Displays a standardised Flow message box.
/// If there is issue when showing the message box, it will return null.
/// </summary>
/// <param name="caption">The caption of the message box.</param>
/// <param name="forceClosed">When user closes the progress box manually by button or esc key, this action will be called.</param>
/// <returns>A progress box interface.</returns>
public IProgressBoxEx ShowProgressBox(string caption, Action forceClosed = null);
}
}

View file

@ -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

View file

@ -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);