mirror of
https://github.com/Flow-Launcher/Flow.Launcher.git
synced 2026-03-11 08:54:32 +00:00
Add support for cancelling download
This commit is contained in:
parent
f69dd0f155
commit
c06ba595b4
1 changed files with 21 additions and 3 deletions
|
|
@ -143,6 +143,7 @@ namespace Flow.Launcher.Plugin.PluginsManager
|
|||
var filePath = Path.Combine(Path.GetTempPath(), downloadFilename);
|
||||
|
||||
IProgressBoxEx prgBox = null;
|
||||
var downloadCancelled = false;
|
||||
try
|
||||
{
|
||||
if (!plugin.IsFromLocalInstallPath)
|
||||
|
|
@ -158,7 +159,13 @@ namespace Flow.Launcher.Plugin.PluginsManager
|
|||
var totalBytes = response.Content.Headers.ContentLength ?? -1L;
|
||||
var canReportProgress = totalBytes != -1;
|
||||
|
||||
if (canReportProgress && (prgBox = Context.API.ShowProgressBox($"Download {plugin.Name}...")) != null)
|
||||
if (canReportProgress &&
|
||||
(prgBox = Context.API.ShowProgressBox($"Download {plugin.Name}...", () =>
|
||||
{
|
||||
httpClient.CancelPendingRequests();
|
||||
downloadCancelled = true;
|
||||
prgBox = null;
|
||||
})) != 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);
|
||||
|
|
@ -173,27 +180,38 @@ namespace Flow.Launcher.Plugin.PluginsManager
|
|||
totalRead += read;
|
||||
|
||||
var progressValue = totalRead * 100 / totalBytes;
|
||||
|
||||
// check if user cancelled download before reporting progress
|
||||
if (downloadCancelled)
|
||||
return;
|
||||
|
||||
prgBox.ReportProgress(progressValue);
|
||||
}
|
||||
|
||||
// check if user cancelled download before closing progress box
|
||||
if (downloadCancelled)
|
||||
return;
|
||||
|
||||
Application.Current.Dispatcher.Invoke(() =>
|
||||
{
|
||||
prgBox.Close();
|
||||
prgBox = null;
|
||||
});
|
||||
|
||||
Install(plugin, filePath);
|
||||
}
|
||||
else
|
||||
{
|
||||
await Http.DownloadAsync(plugin.UrlDownload, filePath).ConfigureAwait(false);
|
||||
Install(plugin, filePath);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
filePath = plugin.LocalInstallPath;
|
||||
}
|
||||
|
||||
Install(plugin, filePath);
|
||||
}
|
||||
}
|
||||
catch (HttpRequestException e)
|
||||
{
|
||||
Context.API.ShowMsgError(
|
||||
|
|
|
|||
Loading…
Reference in a new issue