Update java download window style

This commit is contained in:
Glavo 2022-02-06 17:57:36 +08:00 committed by Yuhui Huang
parent 312152de78
commit 2136fddec5

View File

@ -5,32 +5,30 @@
$url = 'https://aka.ms/download-jdk/microsoft-jdk-17-windows-x64.zip' $url = 'https://aka.ms/download-jdk/microsoft-jdk-17-windows-x64.zip'
$chinese = (Get-WinSystemLocale).Name -eq 'zh-CN' $chinese = (Get-WinSystemLocale).Name -eq 'zh-CN'
do {
$tempFileName = "hmcl-java-$(Get-Random).zip"
$script:tempFile = Join-Path ([System.IO.Path]::GetTempPath()) $tempFileName
} while (Test-Path $script:tempFile)
echo $script:tempFile
[Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms") [Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
$result = if ($chinese) { $result = if ($chinese) {
[System.Windows.Forms.MessageBox]::Show('HMCL 需要 Java 运行时环境才能正常运行,是否自动下载安装 Java', '', [System.Windows.Forms.MessageBoxButtons]::YesNo) [System.Windows.Forms.MessageBox]::Show('HMCL 需要 Java 运行时环境才能正常运行,是否自动下载安装 Java', 'HMCL', [System.Windows.Forms.MessageBoxButtons]::YesNo)
} else { } else {
[System.Windows.Forms.MessageBox]::Show('Running HMCL requires a Java runtime environment. Do you want to download and install Java automatically?', '', [System.Windows.Forms.MessageBoxButtons]::YesNo) [System.Windows.Forms.MessageBox]::Show('Running HMCL requires a Java runtime environment. Do you want to download and install Java automatically?', 'HMCL', [System.Windows.Forms.MessageBoxButtons]::YesNo)
} }
if ($result -ne [System.Windows.Forms.DialogResult]::Yes) { if ($result -ne [System.Windows.Forms.DialogResult]::Yes) {
exit 0 exit 0
} }
do {
$tempFileName = "hmcl-java-$(Get-Random).zip"
$script:tempFile = Join-Path ([System.IO.Path]::GetTempPath()) $tempFileName
} while (Test-Path $script:tempFile)
$form = New-Object System.Windows.Forms.Form $form = New-Object System.Windows.Forms.Form
$form.AutoSize = $true $form.AutoSize = $true
$form.AutoSizeMode = [System.Windows.Forms.AutoSizeMode]::GrowAndShrink $form.AutoSizeMode = [System.Windows.Forms.AutoSizeMode]::GrowAndShrink
if ($chinese) { if ($chinese) {
$form.Text = '正在下载 Java' $form.Text = '正在下载 Java。这需要一段时间,请耐心等待。'
} else { } else {
$form.Text = 'Downloading Java' $form.Text = 'Downloading Java. Please wait patiently for the download to complete.'
} }
$tip = New-Object System.Windows.Forms.Label $tip = New-Object System.Windows.Forms.Label
@ -49,12 +47,12 @@ $progressBar = New-Object System.Windows.Forms.ProgressBar
$progressBar.Maximum = 100 $progressBar.Maximum = 100
$label = New-Object System.Windows.Forms.Label $label = New-Object System.Windows.Forms.Label
$label.Anchor = [System.Windows.Forms.AnchorStyles]::Bottom
$box = New-Object System.Windows.Forms.FlowLayoutPanel $box = New-Object System.Windows.Forms.FlowLayoutPanel
$box.AutoSize = $true $box.AutoSize = $true
$box.FlowDirection = [System.Windows.Forms.FlowDirection]::LeftToRight $box.FlowDirection = [System.Windows.Forms.FlowDirection]::LeftToRight
$box.AutoSizeMode = [System.Windows.Forms.AutoSizeMode]::GrowAndShrink $box.AutoSizeMode = [System.Windows.Forms.AutoSizeMode]::GrowAndShrink
$box.Anchor = [System.Windows.Forms.AnchorStyles]::Right
$box.Controls.Add($progressBar) $box.Controls.Add($progressBar)
$box.Controls.Add($label) $box.Controls.Add($label)
@ -69,27 +67,26 @@ if ($chinese) {
$layout.Controls.Add($tip) $layout.Controls.Add($tip)
$layout.Controls.Add($box) $layout.Controls.Add($box)
#$layout.Controls.Add($progressBar) $box.Controls.Add($cancelButton)
$layout.Controls.Add($cancelButton)
$form.Controls.Add($layout) $form.Controls.Add($layout)
#$form.Controls.Add($progressBar)
#$form.Controls.Add($cancelButton)
[System.Net.DownloadProgressChangedEventHandler]$progressChangedEventHandler = { [System.Net.DownloadProgressChangedEventHandler]$progressChangedEventHandler = {
param($sender, [System.Net.DownloadProgressChangedEventArgs]$EventArgs) param($sender, [System.Net.DownloadProgressChangedEventArgs]$ChangedEventArgs)
$bytesReceived = $EventArgs.BytesReceived $bytesReceived = $ChangedEventArgs.BytesReceived
$totalBytes = $EventArgs.TotalBytesToReceive $totalBytes = $ChangedEventArgs.TotalBytesToReceive
$percentage = ([double]$bytesReceived)/([double]$totalBytes) * 100 $percentage = ([double]$bytesReceived)/([double]$totalBytes) * 100
$progressBar.Value = [int][System.Math]::Truncate($percentage) $progressBar.Value = [int][System.Math]::Truncate($percentage)
#$tooltip.SetToolTip($progressBar, [string]::Format("{0:0.00}%", $percentage))
$label.Text = [string]::Format("{0:0.00}%", $percentage) $label.Text = [string]::Format("{0:0.00}%", $percentage)
} }
[System.ComponentModel.AsyncCompletedEventHandler]$downloadFileCompletedEventHandler = { [System.ComponentModel.AsyncCompletedEventHandler]$downloadFileCompletedEventHandler = {
$form.DialogResult = [System.Windows.Forms.DialogResult]::OK param($sender, [System.ComponentModel.AsyncCompletedEventArgs]$CompletedEventArgs)
if (!$form.IsDisposed) {
$form.DialogResult = [System.Windows.Forms.DialogResult]::OK
}
} }
$client = New-Object System.Net.WebClient $client = New-Object System.Net.WebClient
@ -108,10 +105,13 @@ if ($result -eq [System.Windows.Forms.DialogResult]::OK) {
foreach ($item in $items) { foreach ($item in $items) {
$app.NameSpace($JavaDir).copyHere($item) $app.NameSpace($JavaDir).copyHere($item)
} }
} elseif ($result -eq [System.Windows.Forms.DialogResult]::Cancel) {
$client.CancelAsync()
} }
$client.CancelAsync()
if ([System.IO.File]::Exists($script:tempFile)) { if ([System.IO.File]::Exists($script:tempFile)) {
[System.IO.File]::Delete($script:tempFile) try {
[System.IO.File]::Delete($script:tempFile)
} catch {
Write-Error $_
}
} }