From 73973d7101b6b6b4e0c5078052b57b675b13d116 Mon Sep 17 00:00:00 2001 From: MyDrift Date: Fri, 12 Jul 2024 08:00:32 +0200 Subject: [PATCH] add function to manage taskbar item changed from manually setting the taskbar overlay, progressvalue and progress state to setting them through a function --- functions/private/Install-WinUtilChoco.ps1 | 8 +-- .../private/Install-WinUtilProgramChoco.ps1 | 2 +- .../private/Install-WinUtilProgramWinget.ps1 | 2 +- functions/private/Install-WinUtilWinget.ps1 | 5 +- .../private/Invoke-WinUtilFeatureInstall.ps1 | 9 ++- functions/private/Set-WinUtilTaskbarItem.ps1 | 63 +++++++++++++++++++ functions/public/Invoke-WPFGetInstalled.ps1 | 4 +- functions/public/Invoke-WPFGetIso.ps1 | 5 +- functions/public/Invoke-WPFInstall.ps1 | 6 +- functions/public/Invoke-WPFMicrowin.ps1 | 5 +- functions/public/Invoke-WPFUnInstall.ps1 | 6 +- scripts/main.ps1 | 41 +++--------- 12 files changed, 99 insertions(+), 57 deletions(-) create mode 100644 functions/private/Set-WinUtilTaskbarItem.ps1 diff --git a/functions/private/Install-WinUtilChoco.ps1 b/functions/private/Install-WinUtilChoco.ps1 index d246a44b..718855eb 100644 --- a/functions/private/Install-WinUtilChoco.ps1 +++ b/functions/private/Install-WinUtilChoco.ps1 @@ -13,20 +13,20 @@ function Install-WinUtilChoco { if((Test-WinUtilPackageManager -choco) -eq "installed") { return } - - $sync["Form"].taskbarItemInfo.ProgressState = "Indeterminate" + + Set-WinUtilTaskbaritem -state "Indeterminate" Write-Host "Seems Chocolatey is not installed, installing now." Set-ExecutionPolicy Bypass -Scope Process -Force; Invoke-Expression ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1')) -ErrorAction Stop powershell choco feature enable -n allowGlobalConfirmation - $sync["Form"].taskbarItemInfo.ProgressState = "None" + Set-WinUtilTaskbaritem -state "None" } Catch { Write-Host "===========================================" -Foregroundcolor Red Write-Host "-- Chocolatey failed to install ---" -Foregroundcolor Red Write-Host "===========================================" -Foregroundcolor Red - $sync["Form"].taskbarItemInfo.ProgressState = "Error" + Set-WinUtilTaskbaritem -state "Error" } } diff --git a/functions/private/Install-WinUtilProgramChoco.ps1 b/functions/private/Install-WinUtilProgramChoco.ps1 index 1f0a8d2a..58381f9d 100644 --- a/functions/private/Install-WinUtilProgramChoco.ps1 +++ b/functions/private/Install-WinUtilProgramChoco.ps1 @@ -80,7 +80,7 @@ function Install-WinUtilProgramChoco { } } $x++ - # $sync["Form"].TaskbarItemInfo.ProgressValue = $x/$count + # Set-WinUtilTaskbaritem -state "Normal" -value $x/$count } Write-Progress -Activity "$manage Applications" -Status "Finished" -Completed diff --git a/functions/private/Install-WinUtilProgramWinget.ps1 b/functions/private/Install-WinUtilProgramWinget.ps1 index 097188f0..a3cddef2 100644 --- a/functions/private/Install-WinUtilProgramWinget.ps1 +++ b/functions/private/Install-WinUtilProgramWinget.ps1 @@ -97,7 +97,7 @@ Function Install-WinUtilProgramWinget { } } $X++ - # $sync["Form"].TaskbarItemInfo.ProgressValue = $x/$count + # Set-WinUtilTaskbaritem -state "Normal" -value $x/$count } Write-Progress -Activity "$manage Applications" -Status "Finished" -Completed return $failedPackages; diff --git a/functions/private/Install-WinUtilWinget.ps1 b/functions/private/Install-WinUtilWinget.ps1 index 655a11ff..3e8aa7b6 100644 --- a/functions/private/Install-WinUtilWinget.ps1 +++ b/functions/private/Install-WinUtilWinget.ps1 @@ -19,7 +19,7 @@ function Install-WinUtilWinget { Write-Host "`nWinget is not Installed. Continuing with install.`r" -ForegroundColor Red } - $sync["Form"].taskbarItemInfo.ProgressState = "Indeterminate" + Set-WinUtilTaskbaritem -state "Indeterminate" # Gets the computer's information if ($null -eq $sync.ComputerInfo){ @@ -65,5 +65,6 @@ function Install-WinUtilWinget { throw [WingetFailedInstall]::new('Failed to install!') } } - $sync["Form"].taskbarItemInfo.ProgressState = "None" + Set-WinUtilTaskbaritem -state "None" + } diff --git a/functions/private/Invoke-WinUtilFeatureInstall.ps1 b/functions/private/Invoke-WinUtilFeatureInstall.ps1 index 8363b368..1784c315 100644 --- a/functions/private/Invoke-WinUtilFeatureInstall.ps1 +++ b/functions/private/Invoke-WinUtilFeatureInstall.ps1 @@ -10,8 +10,7 @@ function Invoke-WinUtilFeatureInstall { $CheckBox ) - $sync["Form"].taskbarItemInfo.ProgressState = "Normal" - $sync["Form"].taskbarItemInfo.ProgressValue = 1 # Get amount of affected features & scripts + Set-WinUtilTaskbaritem -state "Normal" -value 1 $CheckBox | ForEach-Object { if($sync.configs.feature.$psitem.feature){ @@ -26,7 +25,7 @@ function Invoke-WinUtilFeatureInstall { } else{ - $sync["Form"].taskbarItemInfo.ProgressState = "Error" + Set-WinUtilTaskbaritem -state "Error" Write-Warning "Unable to Install $feature due to unhandled exception" Write-Warning $psitem.Exception.StackTrace } @@ -47,7 +46,7 @@ function Invoke-WinUtilFeatureInstall { } else{ - $sync["Form"].taskbarItemInfo.ProgressState = "Error" + Set-WinUtilTaskbaritem -state "Error" Write-Warning "Unable to Install $feature due to unhandled exception" Write-Warning $psitem.Exception.StackTrace } @@ -56,6 +55,6 @@ function Invoke-WinUtilFeatureInstall { } } if ($sync["Form"].taskbarItemInfo.ProgressState -ne "Error"){ - $sync["Form"].taskbarItemInfo.ProgressState = "None" + Set-WinUtilTaskbaritem -state "None" } } diff --git a/functions/private/Set-WinUtilTaskbarItem.ps1 b/functions/private/Set-WinUtilTaskbarItem.ps1 new file mode 100644 index 00000000..22940783 --- /dev/null +++ b/functions/private/Set-WinUtilTaskbarItem.ps1 @@ -0,0 +1,63 @@ + <# + + .SYNOPSIS + Modifies the Taskbaritem of the WPF Form + + .PARAMETER state & value + Value can be between 0 and 1, 0 being no progress done yet and 1 being fully completed + State can be 'None' > No progress, 'Indeterminate' > Without value, 'Normal' > when using value, 'Error' > Red (when using value), 'Paused' > Yellow (when using value) + + .PARAMETER overlay + Overlay icon to display on the taskbar item + + .EXAMPLE + Set-WinUtilTaskbaritem -value 0.5 -state "Normal" + Set-WinUtilTaskbaritem -state "Error" + Set-WinUtilTaskbaritem -state "None" + Set-WinUtilTaskbaritem -state "Indeterminate" + Set-WinUtilTaskbaritem -overlay "C:\path\to\icon.ico" + + #> + + +function Set-WinUtilTaskbaritem { + param ( + [double]$value, + $state, + $overlay + #[string]$description + ) + + if ($value) { + $sync["Form"].taskbarItemInfo.ProgressValue = $value + } + + if ($state) { + $sync["Form"].taskbarItemInfo.ProgressState = $state + } + + if ($overlay -and (Test-Path $overlay)) { + # Read the image file as a byte array + $imageBytes = [System.IO.File]::ReadAllBytes($overlay) + + # Convert the byte array to a Base64 string + [System.Convert]::ToBase64String($imageBytes) + + # Load the image file as a bitmap + $bitmap = [System.Drawing.Bitmap]::new($overlay) + + # Create a streaming image by streaming the bitmap to a memory stream + $memoryStream = [System.IO.MemoryStream]::new() + $bitmap.Save($memoryStream, [System.Drawing.Imaging.ImageFormat]::Png) + $memoryStream.Position = 0 + + # Create a bitmap image from the memory stream + $bitmapImage = [System.Windows.Media.Imaging.BitmapImage]::new() + $bitmapImage.BeginInit() + $bitmapImage.StreamSource = $memoryStream + $bitmapImage.EndInit() + $bitmapImage.Freeze() + + $sync["Form"].taskbarItemInfo.Overlay = $bitmapImage + } +} \ No newline at end of file diff --git a/functions/public/Invoke-WPFGetInstalled.ps1 b/functions/public/Invoke-WPFGetInstalled.ps1 index e91cd2a1..76aaa47d 100644 --- a/functions/public/Invoke-WPFGetInstalled.ps1 +++ b/functions/public/Invoke-WPFGetInstalled.ps1 @@ -24,7 +24,7 @@ function Invoke-WPFGetInstalled { param($checkbox, $DebugPreference) $sync.ProcessRunning = $true - # $sync["Form"].taskbarItemInfo.ProgressState = "Indeterminate" + Set-WinUtilTaskbaritem -state "Indeterminate" if($checkbox -eq "winget"){ Write-Host "Getting Installed Programs..." @@ -43,6 +43,6 @@ function Invoke-WPFGetInstalled { Write-Host "Done..." $sync.ProcessRunning = $false - # $sync["Form"].taskbarItemInfo.ProgressState = "None" + Set-WinUtilTaskbaritem -state "None" } } diff --git a/functions/public/Invoke-WPFGetIso.ps1 b/functions/public/Invoke-WPFGetIso.ps1 index 6b33537a..e7356c19 100644 --- a/functions/public/Invoke-WPFGetIso.ps1 +++ b/functions/public/Invoke-WPFGetIso.ps1 @@ -15,7 +15,6 @@ function Invoke-WPFGetIso { $sync.BusyMessage.Visibility="Visible" $sync.BusyText.Text="N Busy" - $sync["Form"].taskbarItemInfo.ProgressState = "Indeterminate" Write-Host " _ __ __ _ " @@ -90,6 +89,8 @@ function Invoke-WPFGetIso { return } + Set-WinUtilTaskbaritem -state "Indeterminate" + # Detect the file size of the ISO and compare it with the free space of the system drive $isoSize = (Get-Item -Path $filePath).Length Write-Debug "Size of ISO file: $($isoSize) bytes" @@ -241,7 +242,7 @@ function Invoke-WPFGetIso { $sync.BusyMessage.Visibility="Hidden" $sync.ProcessRunning = $false - $sync["Form"].taskbarItemInfo.ProgressState = "None" + Set-WinUtilTaskbaritem -state "None" } diff --git a/functions/public/Invoke-WPFInstall.ps1 b/functions/public/Invoke-WPFInstall.ps1 index b0cb4904..b3c969ca 100644 --- a/functions/public/Invoke-WPFInstall.ps1 +++ b/functions/public/Invoke-WPFInstall.ps1 @@ -20,10 +20,10 @@ function Invoke-WPFInstall { return } - $sync["Form"].taskbarItemInfo.ProgressState = "Normal" Invoke-WPFRunspace -ArgumentList $PackagesToInstall -DebugPreference $DebugPreference -ScriptBlock { param($PackagesToInstall, $DebugPreference) + Set-WinUtilTaskbaritem -state "Normal" -value 1/$PackagesToInstall.Count $packagesWinget, $packagesChoco = { $packagesWinget = [System.Collections.Generic.List`1[System.Object]]::new() $packagesChoco = [System.Collections.Generic.List`1[System.Object]]::new() @@ -54,13 +54,13 @@ function Invoke-WPFInstall { Write-Host "===========================================" Write-Host "-- Installs have finished ---" Write-Host "===========================================" - # $sync["Form"].taskbarItemInfo.ProgressState = "Indeterminate" + # Set-WinUtilTaskbaritem -state "None" } Catch { Write-Host "===========================================" Write-Host "Error: $_" Write-Host "===========================================" - # $sync["Form"].taskbarItemInfo.ProgressState = "Error" + # Set-WinUtilTaskbaritem -state "Error" } Start-Sleep -Seconds 5 $sync.ProcessRunning = $False diff --git a/functions/public/Invoke-WPFMicrowin.ps1 b/functions/public/Invoke-WPFMicrowin.ps1 index 4c01ea5d..252341b5 100644 --- a/functions/public/Invoke-WPFMicrowin.ps1 +++ b/functions/public/Invoke-WPFMicrowin.ps1 @@ -4,7 +4,6 @@ function Invoke-WPFMicrowin { Invoke MicroWin routines... #> - $sync["Form"].taskbarItemInfo.ProgressState = "Indeterminate" if($sync.ProcessRunning) { $msg = "GetIso process is currently running." @@ -44,6 +43,8 @@ public class PowerManagement { return } + Set-WinUtilTaskbaritem -state "Indeterminate" + Write-Host "Target ISO location: $($SaveDialog.FileName)" $index = $sync.MicrowinWindowsFlavors.SelectedValue.Split(":")[0].Trim() @@ -479,6 +480,6 @@ public class PowerManagement { # Allow the machine to sleep again (optional) [PowerManagement]::SetThreadExecutionState(0) $sync.ProcessRunning = $false - $sync["Form"].taskbarItemInfo.ProgressState = "None" + Set-WinUtilTaskbaritem -state "None" } } \ No newline at end of file diff --git a/functions/public/Invoke-WPFUnInstall.ps1 b/functions/public/Invoke-WPFUnInstall.ps1 index 873e9eaf..800af84a 100644 --- a/functions/public/Invoke-WPFUnInstall.ps1 +++ b/functions/public/Invoke-WPFUnInstall.ps1 @@ -29,7 +29,7 @@ function Invoke-WPFUnInstall { if($confirm -eq "No"){return} - $sync["Form"].taskbarItemInfo.ProgressState = "Normal" + Set-WinUtilTaskbaritem -state "Normal" -value 1/$PackagesToInstall.Count Invoke-WPFRunspace -ArgumentList $PackagesToInstall -DebugPreference $DebugPreference -ScriptBlock { param($PackagesToInstall, $DebugPreference) @@ -68,13 +68,13 @@ function Invoke-WPFUnInstall { Write-Host "===========================================" Write-Host "-- Uninstalls have finished ---" Write-Host "===========================================" - # $sync["Form"].taskbarItemInfo.ProgressState = "Indeterminate" + # Set-WinUtilTaskbaritem -state "None" } Catch { Write-Host "===========================================" Write-Host "Error: $_" Write-Host "===========================================" - # $sync["Form"].taskbarItemInfo.ProgressState = "Error" + # Set-WinUtilTaskbaritem -state "Error" } $sync.ProcessRunning = $False } diff --git a/scripts/main.ps1 b/scripts/main.ps1 index 4e6f0fc1..657c6428 100644 --- a/scripts/main.ps1 +++ b/scripts/main.ps1 @@ -153,9 +153,15 @@ Invoke-WPFRunspace -ScriptBlock { # Print the logo Invoke-WPFFormVariables -# https://learn.microsoft.com/en-us/dotnet/api/system.windows.shell.taskbariteminfo?view=windowsdesktop-8.0 +# download the logo +$logoUrl = "https://christitus.com/images/logo-full.png" +# Download the image +$logoPath = "$env:TEMP\cttlogo.png" +Invoke-WebRequest -Uri $logoUrl -OutFile $logoPath + +# Progress bar in taskbaritem > Set-WinUtilProgressbar $sync["Form"].TaskbarItemInfo = New-Object System.Windows.Shell.TaskbarItemInfo -$sync["Form"].taskbarItemInfo.ProgressState = "None" +Set-WinUtilTaskbaritem -state "None" # Set the titlebar $sync["Form"].title = $sync["Form"].title + " " + $sync.version @@ -291,37 +297,8 @@ Add-Type @" } } - - # Using a TaskbarItem Overlay until someone figures out how to replace the icon correctly - - # URL of the image - $imageUrl = "https://christitus.com/images/logo-full.png" - - # Download the image - $imagePath = "$env:TEMP\logo-full.png" - Invoke-WebRequest -Uri $imageUrl -OutFile $imagePath - - # Read the image file as a byte array - $imageBytes = [System.IO.File]::ReadAllBytes($imagePath) - - # Convert the byte array to a Base64 string - $base64String = [System.Convert]::ToBase64String($imageBytes) - - # Create a streaming image by streaming the base64 string to a bitmap streamsource - $bitmap = New-Object System.Windows.Media.Imaging.BitmapImage - $bitmap.BeginInit() - $bitmap.StreamSource = [System.IO.MemoryStream][System.Convert]::FromBase64String($base64String) - $bitmap.EndInit() - $bitmap.Freeze() - - # Ensure TaskbarItemInfo is created if not already - if (-not $sync["Form"].TaskbarItemInfo) { - $sync["Form"].TaskbarItemInfo = New-Object System.Windows.Shell.TaskbarItemInfo - } - # Set the overlay icon for the taskbar - $sync["Form"].TaskbarItemInfo.Overlay = $bitmap - + Set-WinUtilTaskbaritem -overlay $logoPath $rect = New-Object RECT [Window]::GetWindowRect($windowHandle, [ref]$rect)