-
Notifications
You must be signed in to change notification settings - Fork 58
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a download progress window to give feedback and allow cancelling …
…downloads
- Loading branch information
Showing
5 changed files
with
151 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
namespace UnityLauncherPro | ||
{ | ||
public readonly struct DownloadProgress | ||
{ | ||
public long TotalRead { get; } | ||
public long TotalBytes { get; } | ||
|
||
public DownloadProgress(long totalRead, long totalBytes) | ||
{ | ||
TotalRead = totalRead; | ||
TotalBytes = totalBytes; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<Window x:Class="UnityLauncherPro.DownloadProgressWindow" | ||
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" | ||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" | ||
Title="Download Progress" Height="70" Width="500" | ||
ResizeMode="NoResize" | ||
WindowStyle="None" | ||
PreviewLostKeyboardFocus="Window_PreviewLostKeyboardFocus" | ||
Background="{DynamicResource ThemeDarkestBackground}"> | ||
<Grid> | ||
<Grid> | ||
<Label Content="{Binding Title, RelativeSource={RelativeSource AncestorType=Window}, FallbackValue=Title}" IsHitTestVisible="False" Margin="5,0,0,-5" Foreground="{DynamicResource ThemeMainTitle}" FontSize="12" HorizontalAlignment="Left" /> | ||
<Button BorderThickness="0" Style="{StaticResource {x:Static ToolBar.ButtonStyleKey}}" HorizontalAlignment="Right" VerticalAlignment="Top" Height="23" Width="23" Background="Transparent" Click="CancelDownloadClick" Padding="0,2" IsTabStop="False"> | ||
<TextBlock Text="❌" FontSize="10" Foreground="{DynamicResource ThemeWindowMinClose}" Padding="5,3,4,4" HorizontalAlignment="Center" /> | ||
</Button> | ||
</Grid> | ||
<Grid VerticalAlignment="Bottom" Background="{DynamicResource ThemeMainBackgroundColor}"> | ||
<ProgressBar x:Name="ProgressBar" Height="23" VerticalAlignment="Center" Margin="10, 10, 70, 10"/> | ||
<TextBlock x:Name="ProgressText" Text="0%" VerticalAlignment="Center" HorizontalAlignment="Center" Margin="10, 10, 70, 10"/> | ||
<Button Style="{StaticResource CustomButton}" Content="Cancel" Width="50" Height="23" HorizontalAlignment="Right" Margin="10" Click="CancelDownloadClick"/> | ||
</Grid> | ||
</Grid> | ||
</Window> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
using System; | ||
using System.Windows; | ||
using System.Windows.Input; | ||
|
||
namespace UnityLauncherPro | ||
{ | ||
public partial class DownloadProgressWindow | ||
{ | ||
private readonly Action _cancelAction; | ||
private readonly string _subjectName; | ||
private static MainWindow MainWindow => Tools.mainWindow; | ||
|
||
public DownloadProgressWindow(string subjectName, Action cancelAction = null) | ||
{ | ||
InitializeComponent(); | ||
_subjectName = subjectName; | ||
Title = subjectName; | ||
_cancelAction = cancelAction; | ||
Topmost = true; | ||
Owner = MainWindow; | ||
WindowStartupLocation = WindowStartupLocation.CenterOwner; | ||
MainWindow.IsEnabled = false; | ||
} | ||
|
||
public void UpdateProgress(DownloadProgress downloadProgress) | ||
{ | ||
Title = $"Downloading {_subjectName} ({downloadProgress.TotalRead / 1024d / 1024d:F1} MB / {downloadProgress.TotalBytes / 1024d / 1024d:F1} MB)"; | ||
var progress = downloadProgress.TotalBytes == 0 ? 0 : downloadProgress.TotalRead * 100d / downloadProgress.TotalBytes; | ||
ProgressBar.Value = progress; | ||
ProgressText.Text = $"{progress / 100:P1}"; | ||
} | ||
|
||
private void CancelDownloadClick(object sender, RoutedEventArgs e) | ||
{ | ||
CancelDownload(); | ||
} | ||
|
||
private void CancelDownload() | ||
{ | ||
_cancelAction?.Invoke(); | ||
} | ||
|
||
private void Window_PreviewLostKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e) | ||
{ | ||
var window = (Window)sender; | ||
window.Topmost = true; | ||
} | ||
|
||
protected override void OnClosed(EventArgs e) | ||
{ | ||
base.OnClosed(e); | ||
_cancelAction?.Invoke(); | ||
MainWindow.IsEnabled = true; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters