Skip to content

Commit 724a5a6

Browse files
committed
fix: Windows Integration progress bar
1 parent 7112231 commit 724a5a6

3 files changed

Lines changed: 73 additions & 22 deletions

File tree

bin/index.js

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,13 @@ const instance = windows ? new JSSC() : {
286286
compressLargeToBase64, compressToBase64, decompressFromBase64
287287
};
288288

289+
function getInpName(inp) {
290+
let inpPath = path.parse(inp);
291+
let inpName = inpPath.name + inpPath.ext;
292+
if (inpName.length > 27) inpName = inpName.slice(0, 24) + '...';
293+
return inpName;
294+
}
295+
289296
(async (inp, out, cfg) => {
290297
const inpF = await collectFiles(inp);
291298
const isFile = !str ? inpF != null : !str;
@@ -387,11 +394,15 @@ const instance = windows ? new JSSC() : {
387394

388395
let total = 0;
389396
let all = 0;
397+
let last = 0;
390398
if (windows) {
391-
WinUIWait = winUIWait('Compressing "' + path.parse(inp).name + '"...');
399+
WinUIWait = winUIWait('Compressing "' + getInpName(inp) + '"...');
392400
instance.events.onCompressProgress = (percentage) => {
393-
WinUIWait.stdin.write(((total + percentage / 100) / all) * 100 + "\n");
401+
const p = Math.max(Math.floor(((total + percentage / 100) / all) * 100), last);
402+
last = p;
403+
if (WinUIWait) WinUIWait.stdin.write(p + "\n");
394404
};
405+
setInterval(()=>{if(WinUIWait){WinUIWait.stdin.write(last + "\n")}}, 50); /* ping ui to avoid ui lag */
395406
}
396407

397408
config.stringify = undefined;
@@ -470,7 +481,7 @@ const instance = windows ? new JSSC() : {
470481
exit(1, e);
471482
}
472483
if (windows) {
473-
WinUIWait = winUIWait('Decompressing "' + path.parse(inp).name + '"...');
484+
WinUIWait = winUIWait('Decompressing "' + getInpName(inp) + '"...');
474485
}
475486

476487
const raw = isFile ? fs.readFileSync(input[0]) : input[0];

bin/windows/ui/wait.ps1

Lines changed: 58 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,48 +6,88 @@ param (
66

77
Add-Type -AssemblyName System.Windows.Forms
88
Add-Type -AssemblyName System.Drawing
9+
Add-Type -AssemblyName PresentationFramework
10+
Add-Type -AssemblyName WindowsFormsIntegration
911

1012
[Windows.Forms.Application]::EnableVisualStyles()
1113

1214
$form = New-Object Windows.Forms.Form
1315
$form.Text = $Name
14-
$form.Size = New-Object Drawing.Size(400, 120)
16+
$form.Size = New-Object Drawing.Size(400, 100)
1517
$form.StartPosition = "CenterScreen"
1618
$form.FormBorderStyle = "FixedDialog"
1719
$form.MaximizeBox = $false
1820
$form.MinimizeBox = $false
1921
$form.ControlBox = $false
2022
$form.TopMost = $true
2123

24+
$Icon = Join-Path $PSScriptRoot "..\..\..\..\jssc.ico"
25+
$Icon = [System.IO.Path]::GetFullPath($Icon)
26+
Add-Type -Path "$PSScriptRoot\process.cs"
27+
if (Test-Path $Icon) {
28+
[Taskbar]::SetCurrentProcessExplicitAppUserModelID("JSSC.Compress") | Out-Null
29+
$form.Icon = New-Object System.Drawing.Icon($Icon)
30+
}
31+
2232
$label = New-Object Windows.Forms.Label
2333
$label.Text = "$Text ($Progress%)"
24-
$label.Location = New-Object Drawing.Point(20, 15)
34+
$label.Location = New-Object Drawing.Point(20, 5)
2535
$label.AutoSize = $true
2636
$label.Font = New-Object System.Drawing.Font('Microsoft JhengHei',10)
2737

28-
$progressBar = New-Object Windows.Forms.ProgressBar
29-
$progressBar.Location = New-Object Drawing.Point(20, 50)
30-
$progressBar.Size = New-Object Drawing.Size(340, 20)
31-
$progressBar.Minimum = 0
32-
$progressBar.Maximum = 100
33-
$progressBar.Value = $Progress
38+
$wpfProgress = New-Object System.Windows.Controls.ProgressBar
39+
$wpfProgress.Minimum = 0
40+
$wpfProgress.Maximum = 100
41+
$wpfProgress.Value = $Progress
42+
$wpfProgress.Height = 20
43+
$wpfProgress.Width = 340
44+
$wpfProgress.Margin = '0'
45+
$wpfProgress.Resources["ProgressBarCornerRadius"] = 10
46+
$wpfProgress.IsIndeterminate = $true
47+
$wpfProgress.Foreground = [System.Windows.Media.SolidColorBrush]::new(
48+
[System.Windows.Media.Color]::FromArgb(150, 81, 90, 218)
49+
)
50+
$wpfProgress.Background = [System.Windows.Media.SolidColorBrush]::new(
51+
[System.Windows.Media.Color]::FromArgb(150, 239, 213, 255)
52+
)
3453

3554
$form.Controls.Add($label)
36-
$form.Controls.Add($progressBar)
55+
56+
$elhost = New-Object System.Windows.Forms.Integration.ElementHost
57+
$elhost.Location = New-Object Drawing.Point(20, 30)
58+
$elhost.Size = New-Object Drawing.Size(340, 20)
59+
$elhost.Child = $wpfProgress
60+
$elhost.BackColor = [System.Drawing.Color]::Transparent
61+
62+
$form.Controls.Add($elhost)
3763

3864
$timer = New-Object Windows.Forms.Timer
3965
$timer.Interval = 100
4066

41-
$timer.Add_Tick({
42-
while ([Console]::In.Peek() -ne -1) {
43-
$line = [Console]::In.ReadLine()
44-
if ($line -ne $null) {
45-
$value = [int]$line
46-
if ($value -ge 0 -and $value -le 100) {
47-
$progressBar.Value = $value
48-
$label.Text = "$Text ($value%)"
49-
}
67+
$idleTicks = 0
68+
function Idle {
69+
$idleTicks++
70+
$timeout = New-Object Windows.Forms.Timer
71+
$timeout.Interval = 3000
72+
$timeout.Add_Tick({
73+
if ($idleTicks -ge 0) {
74+
$wpfProgress.IsIndeterminate = $true
75+
} else {
76+
$timeout.Stop()
5077
}
78+
})
79+
$timeout.Start()
80+
}
81+
82+
$timer.Add_Tick({
83+
Idle
84+
$line = [Console]::In.ReadLine()
85+
$newValue = [int]$line
86+
if ($newValue -ne $wpfProgress.Value) {
87+
$idleTicks = 0
88+
$wpfProgress.Value = $newValue
89+
$wpfProgress.IsIndeterminate = $false
90+
$label.Text = "$Text ($newValue%)"
5191
}
5292
})
5393

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "strc",
3-
"version": "2.1.1-f",
3+
"version": "2.1.1-g",
44
"description": "JavaScript String Compressor - lossless string compression algorithm",
55
"main": "dist/jssc.cjs",
66
"repository": {

0 commit comments

Comments
 (0)