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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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