add function to manage taskbar item

changed from manually setting the taskbar overlay, progressvalue and progress state to setting them through a function
This commit is contained in:
MyDrift
2024-07-12 08:00:32 +02:00
parent a86b07a826
commit 73973d7101
12 changed files with 99 additions and 57 deletions

View File

@ -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"
}
}

View File

@ -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

View File

@ -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;

View File

@ -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"
}

View File

@ -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"
}
}

View File

@ -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
}
}