Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions functions/private/Get-WinUtilSelectedPackages.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ function Get-WinUtilSelectedPackages
)

if ($PackageList.count -eq 1) {
$sync.form.Dispatcher.Invoke([action]{ Set-WinUtilTaskbaritem -state "Indeterminate" -value 0.01 -overlay "logo" })
Invoke-WPFUIThread -ScriptBlock { Set-WinUtilTaskbaritem -state "Indeterminate" -value 0.01 -overlay "logo" }
} else {
$sync.form.Dispatcher.Invoke([action]{ Set-WinUtilTaskbaritem -state "Normal" -value 0.01 -overlay "logo" })
Invoke-WPFUIThread -ScriptBlock { Set-WinUtilTaskbaritem -state "Normal" -value 0.01 -overlay "logo" }
}

$packages = [System.Collections.Hashtable]::new()
Expand Down
4 changes: 2 additions & 2 deletions functions/private/Hide-WPFInstallAppBusy.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ function Hide-WPFInstallAppBusy {
Hides the busy overlay in the install app area of the WPF form.
This is used to indicate that an install or uninstall has finished.
#>
$sync.form.Dispatcher.Invoke([action]{
Invoke-WPFUIThread -ScriptBlock {
$sync.InstallAppAreaOverlay.Visibility = [Windows.Visibility]::Collapsed
$sync.InstallAppAreaBorder.IsEnabled = $true
$sync.InstallAppAreaScrollViewer.Effect.Radius = 0
})
}
}
4 changes: 2 additions & 2 deletions functions/private/Install-WinUtilProgramChoco.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ function Install-WinUtilProgramChoco {
[int]$totalPrograms
)
$progressState = if ($currentIndex -eq $totalPrograms) { "Normal" } else { "Error" }
$sync.form.Dispatcher.Invoke([action] { Set-WinUtilTaskbaritem -state $progressState -value ($currentIndex / $totalPrograms) })
Invoke-WPFUIThread -ScriptBlock { Set-WinUtilTaskbaritem -state $progressState -value ($currentIndex / $totalPrograms) }
}

function Install-ChocoPackage {
Expand Down Expand Up @@ -234,7 +234,7 @@ function Install-WinUtilProgramChoco {
for ($currentIndex = 0; $currentIndex -lt $totalPrograms; $currentIndex++) {
$Program = $Programs[$currentIndex]
Set-WinUtilProgressBar -label "$Action $($Program)" -percent ($currentIndex / $totalPrograms * 100)
$sync.form.Dispatcher.Invoke([action]{ Set-WinUtilTaskbaritem -value ($currentIndex / $totalPrograms)})
Invoke-WPFUIThread -ScriptBlock { Set-WinUtilTaskbaritem -value ($currentIndex / $totalPrograms)}

switch ($Action) {
"Install" {
Expand Down
2 changes: 1 addition & 1 deletion functions/private/Install-WinUtilProgramWinget.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ Function Install-WinUtilProgramWinget {
$Program = $Programs[$i]
$result = $false
Set-WinUtilProgressBar -label "$Action $($Program)" -percent ($i / $count * 100)
$sync.form.Dispatcher.Invoke([action]{ Set-WinUtilTaskbaritem -value ($i / $count)})
Invoke-WPFUIThread -ScriptBlock{ Set-WinUtilTaskbaritem -value ($i / $count)}

$result = switch ($Action) {
"Install" {Invoke-Install -Program $Program}
Expand Down
2 changes: 1 addition & 1 deletion functions/private/Invoke-WinUtilExplorerUpdate.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ function Invoke-WinUtilExplorerUpdate {
)

if ($action -eq "refresh") {
Invoke-WPFRunspace -DebugPreference $DebugPreference -ScriptBlock {
Invoke-WPFRunspace -ScriptBlock {
# Define the Win32 type only if it doesn't exist
if (-not ([System.Management.Automation.PSTypeName]'Win32').Type) {
Add-Type -TypeDefinition @"
Expand Down
60 changes: 27 additions & 33 deletions functions/private/Invoke-WinUtilFeatureInstall.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -10,46 +10,40 @@ function Invoke-WinUtilFeatureInstall {
$CheckBox
)

$x = 0
if($sync.configs.feature.$CheckBox.feature) {
Foreach( $feature in $sync.configs.feature.$CheckBox.feature ) {
try {
Write-Host "Installing $feature"
Enable-WindowsOptionalFeature -Online -FeatureName $feature -All -NoRestart
} catch {
if ($CheckBox.Exception.Message -like "*requires elevation*") {
Write-Warning "Unable to Install $feature due to permissions. Are you running as admin?"
Invoke-WPFUIThread -ScriptBlock { Set-WinUtilTaskbaritem -state "Error" }
} else {

$CheckBox | ForEach-Object {
if($sync.configs.feature.$psitem.feature) {
Foreach( $feature in $sync.configs.feature.$psitem.feature ) {
try {
Write-Host "Installing $feature"
Enable-WindowsOptionalFeature -Online -FeatureName $feature -All -NoRestart
} catch {
if ($psitem.Exception.Message -like "*requires elevation*") {
Write-Warning "Unable to Install $feature due to permissions. Are you running as admin?"
$sync.form.Dispatcher.Invoke([action]{ Set-WinUtilTaskbaritem -state "Error" })
} else {

Write-Warning "Unable to Install $feature due to unhandled exception"
Write-Warning $psitem.Exception.StackTrace
}
Write-Warning "Unable to Install $feature due to unhandled exception"
Write-Warning $CheckBox.Exception.StackTrace
}
}
}
if($sync.configs.feature.$psitem.InvokeScript) {
Foreach( $script in $sync.configs.feature.$psitem.InvokeScript ) {
try {
$Scriptblock = [scriptblock]::Create($script)
}
if($sync.configs.feature.$CheckBox.InvokeScript) {
Foreach( $script in $sync.configs.feature.$CheckBox.InvokeScript ) {
try {
$Scriptblock = [scriptblock]::Create($script)

Write-Host "Running Script for $psitem"
Invoke-Command $scriptblock -ErrorAction stop
} catch {
if ($psitem.Exception.Message -like "*requires elevation*") {
Write-Warning "Unable to Install $feature due to permissions. Are you running as admin?"
$sync.form.Dispatcher.Invoke([action]{ Set-WinUtilTaskbaritem -state "Error" })
} else {
$sync.form.Dispatcher.Invoke([action]{ Set-WinUtilTaskbaritem -state "Error" })
Write-Warning "Unable to Install $feature due to unhandled exception"
Write-Warning $psitem.Exception.StackTrace
}
Write-Host "Running Script for $CheckBox"
Invoke-Command $scriptblock -ErrorAction stop
} catch {
if ($CheckBox.Exception.Message -like "*requires elevation*") {
Write-Warning "Unable to Install $feature due to permissions. Are you running as admin?"
Invoke-WPFUIThread -ScriptBlock { Set-WinUtilTaskbaritem -state "Error" }
} else {
Invoke-WPFUIThread -ScriptBlock { Set-WinUtilTaskbaritem -state "Error" }
Write-Warning "Unable to Install $feature due to unhandled exception"
Write-Warning $CheckBox.Exception.StackTrace
}
}
}
$X++
$sync.form.Dispatcher.Invoke([action]{ Set-WinUtilTaskbaritem -value ($x/$CheckBox.Count) })
}
}
10 changes: 7 additions & 3 deletions functions/private/Set-WinUtilProgressbar.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,15 @@ function Set-WinUtilProgressbar{
[int]$Percent
)

$sync.form.Dispatcher.Invoke([action]{$sync.progressBarTextBlock.Text = $label})
$sync.form.Dispatcher.Invoke([action]{$sync.progressBarTextBlock.ToolTip = $label})
if($PARAM_NOUI) {
return;
}

Invoke-WPFUIThread -ScriptBlock {$sync.progressBarTextBlock.Text = $label}
Invoke-WPFUIThread -ScriptBlock {$sync.progressBarTextBlock.ToolTip = $label}
if ($percent -lt 5 ) {
$percent = 5 # Ensure the progress bar is not empty, as it looks weird
}
$sync.form.Dispatcher.Invoke([action]{ $sync.ProgressBar.Value = $percent})
Invoke-WPFUIThread -ScriptBlock { $sync.ProgressBar.Value = $percent}

}
4 changes: 2 additions & 2 deletions functions/private/Show-WPFInstallAppBusy.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ function Show-WPFInstallAppBusy {
param (
$text = "Installing apps..."
)
$sync.form.Dispatcher.Invoke([action]{
Invoke-WPFUIThread -ScriptBlock {
$sync.InstallAppAreaOverlay.Visibility = [Windows.Visibility]::Visible
$sync.InstallAppAreaOverlay.Width = $($sync.InstallAppAreaScrollViewer.ActualWidth * 0.4)
$sync.InstallAppAreaOverlay.Height = $($sync.InstallAppAreaScrollViewer.ActualWidth * 0.4)
$sync.InstallAppAreaOverlayText.Text = $text
$sync.InstallAppAreaBorder.IsEnabled = $false
$sync.InstallAppAreaScrollViewer.Effect.Radius = 5
})
}
}
20 changes: 12 additions & 8 deletions functions/public/Invoke-WPFFeatureInstall.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,25 @@ function Invoke-WPFFeatureInstall {
return
}

$Features = $sync.selectedFeatures

Invoke-WPFRunspace -ArgumentList $Features -DebugPreference $DebugPreference -ScriptBlock {
param($Features, $DebugPreference)
$handle = Invoke-WPFRunspace -ScriptBlock {
$Features = $sync.selectedFeatures
$sync.ProcessRunning = $true
if ($Features.count -eq 1) {
$sync.form.Dispatcher.Invoke([action]{ Set-WinUtilTaskbaritem -state "Indeterminate" -value 0.01 -overlay "logo" })
Invoke-WPFUIThread -ScriptBlock { Set-WinUtilTaskbaritem -state "Indeterminate" -value 0.01 -overlay "logo" }
} else {
$sync.form.Dispatcher.Invoke([action]{ Set-WinUtilTaskbaritem -state "Normal" -value 0.01 -overlay "logo" })
Invoke-WPFUIThread -ScriptBlock { Set-WinUtilTaskbaritem -state "Normal" -value 0.01 -overlay "logo" }
}

Invoke-WinUtilFeatureInstall $Features
$x = 0

$Features | ForEach-Object {
Invoke-WinUtilFeatureInstall $Feature
$X++
Invoke-WPFUIThread -ScriptBlock { Set-WinUtilTaskbaritem -value ($x/$CheckBox.Count) }
}

$sync.ProcessRunning = $false
$sync.form.Dispatcher.Invoke([action]{ Set-WinUtilTaskbaritem -state "None" -overlay "checkmark" })
Invoke-WPFUIThread -ScriptBlock { Set-WinUtilTaskbaritem -state "None" -overlay "checkmark" }

Write-Host "==================================="
Write-Host "--- Features are Installed ---"
Expand Down
6 changes: 3 additions & 3 deletions functions/public/Invoke-WPFGetInstalled.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@ function Invoke-WPFGetInstalled {
}
$managerPreference = $sync["ManagerPreference"]

Invoke-WPFRunspace -ParameterList @(("managerPreference", $managerPreference),("checkbox", $checkbox)) -DebugPreference $DebugPreference -ScriptBlock {
Invoke-WPFRunspace -ParameterList @(("managerPreference", $managerPreference),("checkbox", $checkbox)) -ScriptBlock {
param (
[string]$checkbox,
[PackageManagers]$managerPreference
)
$sync.ProcessRunning = $true
$sync.form.Dispatcher.Invoke([action] { Set-WinUtilTaskbaritem -state "Indeterminate" })
Invoke-WPFUIThread -ScriptBlock { Set-WinUtilTaskbaritem -state "Indeterminate" }

if ($checkbox -eq "winget") {
Write-Host "Getting Installed Programs..."
Expand All @@ -48,6 +48,6 @@ function Invoke-WPFGetInstalled {

Write-Host "Done..."
$sync.ProcessRunning = $false
$sync.form.Dispatcher.Invoke([action] { Set-WinUtilTaskbaritem -state "None" })
Invoke-WPFUIThread -ScriptBlock { Set-WinUtilTaskbaritem -state "None" }
}
}
6 changes: 4 additions & 2 deletions functions/public/Invoke-WPFImpex.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,10 @@ function Invoke-WPFImpex {
# $flattenedJson = $jsonFile.PSObject.Properties.Where({ $_.Name -ne "Install" }).ForEach({ $_.Value })
$flattenedJson = $jsonFile
Update-WinUtilSelections -flatJson $flattenedJson
# TODO test with toggles
Reset-WPFCheckBoxes -doToggles $true

if (!$PARAM_NOUI) {
Reset-WPFCheckBoxes -doToggles $true
}
}
} catch {
Write-Error "An error occurred while importing: $_"
Expand Down
15 changes: 7 additions & 8 deletions functions/public/Invoke-WPFInstall.ps1
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
function Invoke-WPFInstall {
param (
[Parameter(Mandatory=$false)]
[PSObject[]]$PackagesToInstall = $($sync.selectedApps | Foreach-Object { $sync.configs.applicationsHashtable.$_ })
)
<#
.SYNOPSIS
Installs the selected programs using winget, if one or more of the selected programs are already installed on the system, winget will try and perform an upgrade if there's a newer version to install.
#>

$PackagesToInstall = $sync.selectedApps | Foreach-Object { $sync.configs.applicationsHashtable.$_ }


if($sync.ProcessRunning) {
$msg = "[Invoke-WPFInstall] An Install process is currently running."
[System.Windows.MessageBox]::Show($msg, "Winutil", [System.Windows.MessageBoxButton]::OK, [System.Windows.MessageBoxImage]::Warning)
Expand All @@ -22,8 +21,8 @@ function Invoke-WPFInstall {

$ManagerPreference = $sync["ManagerPreference"]

Invoke-WPFRunspace -ParameterList @(("PackagesToInstall", $PackagesToInstall),("ManagerPreference", $ManagerPreference)) -DebugPreference $DebugPreference -ScriptBlock {
param($PackagesToInstall, $ManagerPreference, $DebugPreference)
$handle = Invoke-WPFRunspace -ParameterList @(("PackagesToInstall", $PackagesToInstall),("ManagerPreference", $ManagerPreference)) -ScriptBlock {
param($PackagesToInstall, $ManagerPreference)

$packagesSorted = Get-WinUtilSelectedPackages -PackageList $PackagesToInstall -Preference $ManagerPreference

Expand All @@ -45,12 +44,12 @@ function Invoke-WPFInstall {
Write-Host "==========================================="
Write-Host "-- Installs have finished ---"
Write-Host "==========================================="
$sync.form.Dispatcher.Invoke([action]{ Set-WinUtilTaskbaritem -state "None" -overlay "checkmark" })
Invoke-WPFUIThread -ScriptBlock { Set-WinUtilTaskbaritem -state "None" -overlay "checkmark" }
} catch {
Write-Host "==========================================="
Write-Host "Error: $_"
Write-Host "==========================================="
$sync.form.Dispatcher.Invoke([action]{ Set-WinUtilTaskbaritem -state "Error" -overlay "warning" })
Invoke-WPFUIThread -ScriptBlock { Set-WinUtilTaskbaritem -state "Error" -overlay "warning" }
}
$sync.ProcessRunning = $False
}
Expand Down
5 changes: 2 additions & 3 deletions functions/public/Invoke-WPFRunspace.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@ function Invoke-WPFRunspace {
Param (
$ScriptBlock,
$ArgumentList,
$ParameterList,
$DebugPreference
$ParameterList
)

# Create a PowerShell instance
Expand All @@ -41,7 +40,7 @@ function Invoke-WPFRunspace {
foreach ($parameter in $ParameterList) {
$script:powershell.AddParameter($parameter[0], $parameter[1])
}
$script:powershell.AddArgument($DebugPreference) # Pass DebugPreference to the script block

$script:powershell.RunspacePool = $sync.runspace

# Execute the RunspacePool
Expand Down
2 changes: 1 addition & 1 deletion functions/public/Invoke-WPFSSHServer.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ function Invoke-WPFSSHServer {

#>

Invoke-WPFRunspace -DebugPreference $DebugPreference -ScriptBlock {
Invoke-WPFRunspace -ScriptBlock {

Invoke-WinUtilSSHServer

Expand Down
21 changes: 21 additions & 0 deletions functions/public/Invoke-WPFUIThread.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
function Invoke-WPFUIThread {
<#
.SYNOPSIS
Creates and runs a task on Winutil's WPF Forms thread.
.PARAMETER ScriptBlock
The scriptblock to invoke in the thread
#>

[CmdletBinding()]
Param (
$ScriptBlock
)

if ($PARAM_NOUI) {
return;
}

$sync.form.Dispatcher.Invoke([action]$ScriptBlock)
}
8 changes: 4 additions & 4 deletions functions/public/Invoke-WPFUnInstall.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ function Invoke-WPFUnInstall {

$ManagerPreference = $sync["ManagerPreference"]

Invoke-WPFRunspace -ParameterList @(("PackagesToUninstall", $PackagesToUninstall),("ManagerPreference", $ManagerPreference)) -DebugPreference $DebugPreference -ScriptBlock {
param($PackagesToUninstall, $ManagerPreference, $DebugPreference)
Invoke-WPFRunspace -ParameterList @(("PackagesToUninstall", $PackagesToUninstall),("ManagerPreference", $ManagerPreference)) -ScriptBlock {
param($PackagesToUninstall, $ManagerPreference)

$packagesSorted = Get-WinUtilSelectedPackages -PackageList $PackagesToUninstall -Preference $ManagerPreference
$packagesWinget = $packagesSorted[[PackageManagers]::Winget]
Expand All @@ -54,12 +54,12 @@ function Invoke-WPFUnInstall {
Write-Host "==========================================="
Write-Host "-- Uninstalls have finished ---"
Write-Host "==========================================="
$sync.form.Dispatcher.Invoke([action]{ Set-WinUtilTaskbaritem -state "None" -overlay "checkmark" })
Invoke-WPFUIThread -ScriptBlock { Set-WinUtilTaskbaritem -state "None" -overlay "checkmark" }
} catch {
Write-Host "==========================================="
Write-Host "Error: $_"
Write-Host "==========================================="
$sync.form.Dispatcher.Invoke([action]{ Set-WinUtilTaskbaritem -state "Error" -overlay "warning" })
Invoke-WPFUIThread -ScriptBlock { Set-WinUtilTaskbaritem -state "Error" -overlay "warning" }
}
$sync.ProcessRunning = $False

Expand Down
15 changes: 6 additions & 9 deletions functions/public/Invoke-WPFtweaksbutton.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -25,30 +25,27 @@ function Invoke-WPFtweaksbutton {
Write-Debug "Number of tweaks to process: $($Tweaks.Count)"

# The leading "," in the ParameterList is necessary because we only provide one argument and powershell cannot be convinced that we want a nested loop with only one argument otherwise
Invoke-WPFRunspace -ParameterList @(,("tweaks",$tweaks)) -DebugPreference $DebugPreference -ScriptBlock {
param(
$tweaks,
$DebugPreference
)
$handle = Invoke-WPFRunspace -ParameterList @(,("tweaks",$tweaks)) -ScriptBlock {
param($tweaks)
Write-Debug "Inside Number of tweaks to process: $($Tweaks.Count)"

$sync.ProcessRunning = $true

if ($Tweaks.count -eq 1) {
$sync.form.Dispatcher.Invoke([action]{ Set-WinUtilTaskbaritem -state "Indeterminate" -value 0.01 -overlay "logo" })
Invoke-WPFUIThread -ScriptBlock { Set-WinUtilTaskbaritem -state "Indeterminate" -value 0.01 -overlay "logo" }
} else {
$sync.form.Dispatcher.Invoke([action]{ Set-WinUtilTaskbaritem -state "Normal" -value 0.01 -overlay "logo" })
Invoke-WPFUIThread -ScriptBlock{ Set-WinUtilTaskbaritem -state "Normal" -value 0.01 -overlay "logo" }
}
# Execute other selected tweaks

for ($i = 0; $i -lt $Tweaks.Count; $i++) {
Set-WinUtilProgressBar -Label "Applying $($tweaks[$i])" -Percent ($i / $tweaks.Count * 100)
Invoke-WinUtilTweaks $tweaks[$i]
$sync.form.Dispatcher.Invoke([action]{ Set-WinUtilTaskbaritem -value ($i/$Tweaks.Count) })
Invoke-WPFUIThread -ScriptBlock { Set-WinUtilTaskbaritem -value ($i/$Tweaks.Count) }
}
Set-WinUtilProgressBar -Label "Tweaks finished" -Percent 100
$sync.ProcessRunning = $false
$sync.form.Dispatcher.Invoke([action]{ Set-WinUtilTaskbaritem -state "None" -overlay "checkmark" })
Invoke-WPFUIThread -ScriptBlock { Set-WinUtilTaskbaritem -state "None" -overlay "checkmark" }
Write-Host "================================="
Write-Host "-- Tweaks are Finished ---"
Write-Host "================================="
Expand Down
Loading
Loading