Skip to content

Commit 54f02e0

Browse files
committed
Use public api for ProgressBoxEx & Add support for force close event
1 parent 562b233 commit 54f02e0

File tree

5 files changed

+72
-13
lines changed

5 files changed

+72
-13
lines changed

Flow.Launcher.Core/ProgressBoxEx.xaml.cs

Lines changed: 37 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,31 @@
22
using System.Windows;
33
using System.Windows.Input;
44
using Flow.Launcher.Infrastructure.Logger;
5+
using Flow.Launcher.Plugin;
56

67
namespace Flow.Launcher.Core
78
{
8-
public partial class ProgressBoxEx : Window
9+
public partial class ProgressBoxEx : Window, IProgressBoxEx
910
{
10-
private ProgressBoxEx()
11+
private readonly Action _forceClosed;
12+
private bool _isClosed;
13+
14+
private ProgressBoxEx(Action forceClosed)
1115
{
16+
_forceClosed = forceClosed;
1217
InitializeComponent();
1318
}
1419

15-
public static ProgressBoxEx Show(string caption)
20+
public static IProgressBoxEx Show(string caption, Action forceClosed)
1621
{
1722
if (!Application.Current.Dispatcher.CheckAccess())
1823
{
19-
return Application.Current.Dispatcher.Invoke(() => Show(caption));
24+
return Application.Current.Dispatcher.Invoke(() => Show(caption, forceClosed));
2025
}
2126

2227
try
2328
{
24-
var prgBox = new ProgressBoxEx
29+
var prgBox = new ProgressBoxEx(forceClosed)
2530
{
2631
Title = caption
2732
};
@@ -51,26 +56,50 @@ public void ReportProgress(double progress)
5156
else if (progress >= 100)
5257
{
5358
ProgressBar.Value = 100;
59+
Close();
5460
}
5561
else
5662
{
5763
ProgressBar.Value = progress;
5864
}
5965
}
6066

67+
private new void Close()
68+
{
69+
if (_isClosed)
70+
{
71+
return;
72+
}
73+
74+
base.Close();
75+
_isClosed = true;
76+
}
77+
6178
private void KeyEsc_OnPress(object sender, ExecutedRoutedEventArgs e)
6279
{
63-
Close();
80+
ForceClose();
6481
}
6582

6683
private void Button_Click(object sender, RoutedEventArgs e)
6784
{
68-
Close();
85+
ForceClose();
6986
}
7087

7188
private void Button_Cancel(object sender, RoutedEventArgs e)
7289
{
73-
Close();
90+
ForceClose();
91+
}
92+
93+
private void ForceClose()
94+
{
95+
if (_isClosed)
96+
{
97+
return;
98+
}
99+
100+
base.Close();
101+
_isClosed = true;
102+
_forceClosed?.Invoke();
74103
}
75104
}
76105
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
namespace Flow.Launcher.Plugin;
2+
3+
/// <summary>
4+
/// Interface for progress box
5+
/// </summary>
6+
public interface IProgressBoxEx
7+
{
8+
/// <summary>
9+
/// Show progress box
10+
/// </summary>
11+
/// <param name="progress">
12+
/// Progress value. Should be between 0 and 100. When progress is 100, the progress box will be closed.
13+
/// </param>
14+
public void ReportProgress(double progress);
15+
16+
/// <summary>
17+
/// Close progress box.
18+
/// </summary>
19+
public void Close();
20+
}

Flow.Launcher.Plugin/Interfaces/IPublicAPI.cs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using Flow.Launcher.Plugin.SharedModels;
1+
using Flow.Launcher.Plugin.SharedModels;
22
using JetBrains.Annotations;
33
using System;
44
using System.Collections.Generic;
@@ -316,5 +316,14 @@ public interface IPublicAPI
316316
/// <param name="defaultResult">Specifies the default result of the message box.</param>
317317
/// <returns>Specifies which message box button is clicked by the user.</returns>
318318
public MessageBoxResult ShowMsgBox(string messageBoxText, string caption = "", MessageBoxButton button = MessageBoxButton.OK, MessageBoxImage icon = MessageBoxImage.None, MessageBoxResult defaultResult = MessageBoxResult.OK);
319+
320+
/// <summary>
321+
/// Displays a standardised Flow message box.
322+
/// If there is issue when showing the message box, it will return null.
323+
/// </summary>
324+
/// <param name="caption">The caption of the message box.</param>
325+
/// <param name="forceClosed">When user closes the progress box manually by button or esc key, this action will be called.</param>
326+
/// <returns>A progress box interface.</returns>
327+
public IProgressBoxEx ShowProgressBox(string caption, Action forceClosed = null);
319328
}
320329
}

Flow.Launcher/PublicAPIInstance.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,8 @@ public bool IsGameModeOn()
324324
public MessageBoxResult ShowMsgBox(string messageBoxText, string caption = "", MessageBoxButton button = MessageBoxButton.OK, MessageBoxImage icon = MessageBoxImage.None, MessageBoxResult defaultResult = MessageBoxResult.OK) =>
325325
MessageBoxEx.Show(messageBoxText, caption, button, icon, defaultResult);
326326

327+
public IProgressBoxEx ShowProgressBox(string caption, Action forceClosed = null) => ProgressBoxEx.Show(caption, forceClosed);
328+
327329
#endregion
328330

329331
#region Private Methods

Plugins/Flow.Launcher.Plugin.PluginsManager/PluginsManager.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
using Flow.Launcher.Core;
2-
using Flow.Launcher.Core.ExternalPlugins;
1+
using Flow.Launcher.Core.ExternalPlugins;
32
using Flow.Launcher.Core.Plugin;
43
using Flow.Launcher.Infrastructure;
54
using Flow.Launcher.Infrastructure.Http;
@@ -143,7 +142,7 @@ internal async Task InstallOrUpdateAsync(UserPlugin plugin)
143142

144143
var filePath = Path.Combine(Path.GetTempPath(), downloadFilename);
145144

146-
ProgressBoxEx prgBox = null;
145+
IProgressBoxEx prgBox = null;
147146
try
148147
{
149148
if (!plugin.IsFromLocalInstallPath)
@@ -159,7 +158,7 @@ internal async Task InstallOrUpdateAsync(UserPlugin plugin)
159158
var totalBytes = response.Content.Headers.ContentLength ?? -1L;
160159
var canReportProgress = totalBytes != -1;
161160

162-
if (canReportProgress && (prgBox = ProgressBoxEx.Show("Download plugin...")) != null)
161+
if (canReportProgress && (prgBox = Context.API.ShowProgressBox("Download plugin...")) != null)
163162
{
164163
await using var contentStream = await response.Content.ReadAsStreamAsync().ConfigureAwait(false);
165164
await using var fileStream = new FileStream(filePath, FileMode.Create, FileAccess.Write, FileShare.None, 8192, true);

0 commit comments

Comments
 (0)