# Create enums Add-Type @" public enum PackageManagers { Winget, Choco } "@ # SPDX-License-Identifier: MIT # Set the maximum number of threads for the RunspacePool to the number of threads on the machine $maxthreads = [int]$env:NUMBER_OF_PROCESSORS # Create a new session state for parsing variables into our runspace $hashVars = New-object System.Management.Automation.Runspaces.SessionStateVariableEntry -ArgumentList 'sync',$sync,$Null $InitialSessionState = [System.Management.Automation.Runspaces.InitialSessionState]::CreateDefault() # Add the variable to the session state $InitialSessionState.Variables.Add($hashVars) # Get every private function and add them to the session state $functions = Get-ChildItem function:\ | Where-Object { $_.Name -imatch 'winutil|Microwin|WPF' } foreach ($function in $functions) { $functionDefinition = Get-Content function:\$($function.name) $functionEntry = New-Object System.Management.Automation.Runspaces.SessionStateFunctionEntry -ArgumentList $($function.name), $functionDefinition $initialSessionState.Commands.Add($functionEntry) } # Create the runspace pool $sync.runspace = [runspacefactory]::CreateRunspacePool( 1, # Minimum thread count $maxthreads, # Maximum thread count $InitialSessionState, # Initial session state $Host # Machine to create runspaces on ) # Open the RunspacePool instance $sync.runspace.Open() # Create classes for different exceptions class WingetFailedInstall : Exception { [string]$additionalData WingetFailedInstall($Message) : base($Message) {} } class ChocoFailedInstall : Exception { [string]$additionalData ChocoFailedInstall($Message) : base($Message) {} } class GenericException : Exception { [string]$additionalData GenericException($Message) : base($Message) {} } $inputXML = $inputXML -replace 'mc:Ignorable="d"', '' -replace "x:N", 'N' -replace '^ [char]0xF08C if (($msg -eq 0x001A) -and $sync.ThemeButton.Content -eq [char]0xF08C) { $currentTime = [datetime]::Now if ($currentTime - $lastThemeChangeTime -gt $debounceInterval) { Invoke-WinutilThemeChange -theme "Auto" $script:lastThemeChangeTime = $currentTime $handled = $true } } return 0 }) }) Invoke-WinutilThemeChange -init $true # Load the configuration files $sync.configs.applicationsHashtable = @{} $sync.configs.applications.PSObject.Properties | ForEach-Object { $sync.configs.applicationsHashtable[$_.Name] = $_.Value } # Now call the function with the final merged config Invoke-WPFUIElements -configVariable $sync.configs.appnavigation -targetGridName "appscategory" -columncount 1 Initialize-WPFUI -targetGridName "appscategory" Initialize-WPFUI -targetGridName "appspanel" Invoke-WPFUIElements -configVariable $sync.configs.tweaks -targetGridName "tweakspanel" -columncount 2 Invoke-WPFUIElements -configVariable $sync.configs.feature -targetGridName "featurespanel" -columncount 2 # Future implementation: Add Windows Version to updates panel #Invoke-WPFUIElements -configVariable $sync.configs.updates -targetGridName "updatespanel" -columncount 1 #=========================================================================== # Store Form Objects In PowerShell #=========================================================================== $xaml.SelectNodes("//*[@Name]") | ForEach-Object {$sync["$("$($psitem.Name)")"] = $sync["Form"].FindName($psitem.Name)} #Persist Package Manager preference across winutil restarts $sync.ChocoRadioButton.Add_Checked({Set-PackageManagerPreference Choco}) $sync.WingetRadioButton.Add_Checked({Set-PackageManagerPreference Winget}) Set-PackageManagerPreference switch ($sync["ManagerPreference"]) { "Choco" {$sync.ChocoRadioButton.IsChecked = $true; break} "Winget" {$sync.WingetRadioButton.IsChecked = $true; break} } $sync.keys | ForEach-Object { if($sync.$psitem) { if($($sync["$psitem"].GetType() | Select-Object -ExpandProperty Name) -eq "ToggleButton") { $sync["$psitem"].Add_Click({ [System.Object]$Sender = $args[0] Invoke-WPFButton $Sender.name }) } if($($sync["$psitem"].GetType() | Select-Object -ExpandProperty Name) -eq "Button") { $sync["$psitem"].Add_Click({ [System.Object]$Sender = $args[0] Invoke-WPFButton $Sender.name }) } if ($($sync["$psitem"].GetType() | Select-Object -ExpandProperty Name) -eq "TextBlock") { if ($sync["$psitem"].Name.EndsWith("Link")) { $sync["$psitem"].Add_MouseUp({ [System.Object]$Sender = $args[0] Start-Process $Sender.ToolTip -ErrorAction Stop Write-Debug "Opening: $($Sender.ToolTip)" }) } } } } #=========================================================================== # Setup background config #=========================================================================== # Load computer information in the background Invoke-WPFRunspace -ScriptBlock { try { $ProgressPreference = "SilentlyContinue" $sync.ConfigLoaded = $False $sync.ComputerInfo = Get-ComputerInfo $sync.ConfigLoaded = $True } finally{ $ProgressPreference = $oldProgressPreference } } | Out-Null #=========================================================================== # Setup and Show the Form #=========================================================================== # Print the logo Show-CTTLogo # Progress bar in taskbaritem > Set-WinUtilProgressbar $sync["Form"].TaskbarItemInfo = New-Object System.Windows.Shell.TaskbarItemInfo Set-WinUtilTaskbaritem -state "None" # Set the titlebar $sync["Form"].title = $sync["Form"].title + " " + $sync.version # Set the commands that will run when the form is closed $sync["Form"].Add_Closing({ $sync.runspace.Dispose() $sync.runspace.Close() [System.GC]::Collect() }) # Attach the event handler to the Click event $sync.SearchBarClearButton.Add_Click({ $sync.SearchBar.Text = "" $sync.SearchBarClearButton.Visibility = "Collapsed" # Focus the search bar after clearing the text $sync.SearchBar.Focus() $sync.SearchBar.SelectAll() }) # add some shortcuts for people that don't like clicking $commonKeyEvents = { # Prevent shortcuts from executing if a process is already running if ($sync.ProcessRunning -eq $true) { return } # Handle key presses of single keys switch ($_.Key) { "Escape" { $sync.SearchBar.Text = "" } } # Handle Alt key combinations for navigation if ($_.KeyboardDevice.Modifiers -eq "Alt") { $keyEventArgs = $_ switch ($_.SystemKey) { "I" { Invoke-WPFButton "WPFTab1BT"; $keyEventArgs.Handled = $true } # Navigate to Install tab and suppress Windows Warning Sound "T" { Invoke-WPFButton "WPFTab2BT"; $keyEventArgs.Handled = $true } # Navigate to Tweaks tab "C" { Invoke-WPFButton "WPFTab3BT"; $keyEventArgs.Handled = $true } # Navigate to Config tab "U" { Invoke-WPFButton "WPFTab4BT"; $keyEventArgs.Handled = $true } # Navigate to Updates tab "M" { Invoke-WPFButton "WPFTab5BT"; $keyEventArgs.Handled = $true } # Navigate to MicroWin tab } } # Handle Ctrl key combinations for specific actions if ($_.KeyboardDevice.Modifiers -eq "Ctrl") { switch ($_.Key) { "F" { $sync.SearchBar.Focus() } # Focus on the search bar "Q" { $this.Close() } # Close the application } } } $sync["Form"].Add_PreViewKeyDown($commonKeyEvents) $sync["Form"].Add_MouseLeftButtonDown({ Invoke-WPFPopup -Action "Hide" -Popups @("Settings", "Theme") $sync["Form"].DragMove() }) $sync["Form"].Add_MouseDoubleClick({ if ($_.OriginalSource.Name -eq "NavDockPanel" -or $_.OriginalSource.Name -eq "GridBesideNavDockPanel") { if ($sync["Form"].WindowState -eq [Windows.WindowState]::Normal) { $sync["Form"].WindowState = [Windows.WindowState]::Maximized } else{ $sync["Form"].WindowState = [Windows.WindowState]::Normal } } }) $sync["Form"].Add_Deactivated({ Write-Debug "WinUtil lost focus" Invoke-WPFPopup -Action "Hide" -Popups @("Settings", "Theme") }) $sync["Form"].Add_ContentRendered({ # Load the Windows Forms assembly Add-Type -AssemblyName System.Windows.Forms $primaryScreen = [System.Windows.Forms.Screen]::PrimaryScreen # Check if the primary screen is found if ($primaryScreen) { # Extract screen width and height for the primary monitor $screenWidth = $primaryScreen.Bounds.Width $screenHeight = $primaryScreen.Bounds.Height # Print the screen size Write-Debug "Primary Monitor Width: $screenWidth pixels" Write-Debug "Primary Monitor Height: $screenHeight pixels" # Compare with the primary monitor size if ($sync.Form.ActualWidth -gt $screenWidth -or $sync.Form.ActualHeight -gt $screenHeight) { Write-Debug "The specified width and/or height is greater than the primary monitor size." $sync.Form.Left = 0 $sync.Form.Top = 0 $sync.Form.Width = $screenWidth $sync.Form.Height = $screenHeight } else { Write-Debug "The specified width and height are within the primary monitor size limits." } } else { Write-Debug "Unable to retrieve information about the primary monitor." } Invoke-WPFTab "WPFTab1BT" $sync["Form"].Focus() # maybe this is not the best place to load and execute config file? # maybe community can help? if ($PARAM_CONFIG) { Invoke-WPFImpex -type "import" -Config $PARAM_CONFIG if ($PARAM_RUN) { while ($sync.ProcessRunning) { Start-Sleep -Seconds 5 } Start-Sleep -Seconds 5 Write-Host "Applying tweaks..." Invoke-WPFtweaksbutton while ($sync.ProcessRunning) { Start-Sleep -Seconds 5 } Start-Sleep -Seconds 5 Write-Host "Installing features..." Invoke-WPFFeatureInstall while ($sync.ProcessRunning) { Start-Sleep -Seconds 5 } Start-Sleep -Seconds 5 Write-Host "Installing applications..." while ($sync.ProcessRunning) { Start-Sleep -Seconds 1 } Invoke-WPFInstall Start-Sleep -Seconds 5 Write-Host "Done." } } }) # Add event handlers for the RadioButtons $sync["ISOdownloader"].add_Checked({ $sync["ISORelease"].Visibility = [System.Windows.Visibility]::Visible $sync["ISOLanguage"].Visibility = [System.Windows.Visibility]::Visible }) $sync["ISOmanual"].add_Checked({ $sync["ISORelease"].Visibility = [System.Windows.Visibility]::Collapsed $sync["ISOLanguage"].Visibility = [System.Windows.Visibility]::Collapsed }) $sync["ISORelease"].Items.Add("24H2") | Out-Null $sync["ISORelease"].SelectedItem = "24H2" $sync["ISOLanguage"].Items.Add("System Language ($(Microwin-GetLangFromCulture -langName $((Get-Culture).Name)))") | Out-Null if ($currentCulture -ne "English International") { $sync["ISOLanguage"].Items.Add("English International") | Out-Null } if ($currentCulture -ne "English") { $sync["ISOLanguage"].Items.Add("English") | Out-Null } if ($sync["ISOLanguage"].Items.Count -eq 1) { $sync["ISOLanguage"].IsEnabled = $false } $sync["ISOLanguage"].SelectedIndex = 0 # The SearchBarTimer is used to delay the search operation until the user has stopped typing for a short period # This prevents the ui from stuttering when the user types quickly as it dosnt need to update the ui for every keystroke $searchBarTimer = New-Object System.Windows.Threading.DispatcherTimer $searchBarTimer.Interval = [TimeSpan]::FromMilliseconds(300) $searchBarTimer.IsEnabled = $false $searchBarTimer.add_Tick({ $searchBarTimer.Stop() switch ($sync.currentTab) { "Install" { Find-AppsByNameOrDescription -SearchString $sync.SearchBar.Text } "Tweaks" { Find-TweaksByNameOrDescription -SearchString $sync.SearchBar.Text } } }) $sync["SearchBar"].Add_TextChanged({ if ($sync.SearchBar.Text -ne "") { $sync.SearchBarClearButton.Visibility = "Visible" } else { $sync.SearchBarClearButton.Visibility = "Collapsed" } if ($searchBarTimer.IsEnabled) { $searchBarTimer.Stop() } $searchBarTimer.Start() }) $sync["Form"].Add_Loaded({ param($e) $sync.Form.MinWidth = "1000" $sync["Form"].MaxWidth = [Double]::PositiveInfinity $sync["Form"].MaxHeight = [Double]::PositiveInfinity }) $NavLogoPanel = $sync["Form"].FindName("NavLogoPanel") $NavLogoPanel.Children.Add((Invoke-WinUtilAssets -Type "logo" -Size 25)) | Out-Null # Initialize the hashtable $winutildir = @{} # Set the path for the winutil directory $winutildir["path"] = "$env:LOCALAPPDATA\winutil\" [System.IO.Directory]::CreateDirectory($winutildir["path"]) | Out-Null $winutildir["logo.ico"] = $winutildir["path"] + "cttlogo.ico" if (Test-Path $winutildir["logo.ico"]) { $sync["logorender"] = $winutildir["logo.ico"] } else { $sync["logorender"] = (Invoke-WinUtilAssets -Type "Logo" -Size 90 -Render) } $sync["checkmarkrender"] = (Invoke-WinUtilAssets -Type "checkmark" -Size 512 -Render) $sync["warningrender"] = (Invoke-WinUtilAssets -Type "warning" -Size 512 -Render) Set-WinUtilTaskbaritem -overlay "logo" $sync["Form"].Add_Activated({ Set-WinUtilTaskbaritem -overlay "logo" }) $sync["ThemeButton"].Add_Click({ Write-Debug "ThemeButton clicked" Invoke-WPFPopup -PopupActionTable @{ "Settings" = "Hide"; "Theme" = "Toggle" } }) $sync["AutoThemeMenuItem"].Add_Click({ Write-Debug "About clicked" Invoke-WPFPopup -Action "Hide" -Popups @("Theme") Invoke-WinutilThemeChange -theme "Auto" }) $sync["DarkThemeMenuItem"].Add_Click({ Write-Debug "Dark Theme clicked" Invoke-WPFPopup -Action "Hide" -Popups @("Theme") Invoke-WinutilThemeChange -theme "Dark" }) $sync["LightThemeMenuItem"].Add_Click({ Write-Debug "Light Theme clicked" Invoke-WPFPopup -Action "Hide" -Popups @("Theme") Invoke-WinutilThemeChange -theme "Light" }) $sync["SettingsButton"].Add_Click({ Write-Debug "SettingsButton clicked" Invoke-WPFPopup -PopupActionTable @{ "Settings" = "Toggle"; "Theme" = "Hide" } }) $sync["ImportMenuItem"].Add_Click({ Write-Debug "Import clicked" Invoke-WPFPopup -Action "Hide" -Popups @("Settings") Invoke-WPFImpex -type "import" }) $sync["ExportMenuItem"].Add_Click({ Write-Debug "Export clicked" Invoke-WPFPopup -Action "Hide" -Popups @("Settings") Invoke-WPFImpex -type "export" }) $sync["AboutMenuItem"].Add_Click({ Write-Debug "About clicked" Invoke-WPFPopup -Action "Hide" -Popups @("Settings") $authorInfo = @" Author : @christitustech UI : @MyDrift-user, @Marterich Runspace : @DeveloperDurp, @Marterich MicroWin : @KonTy, @CodingWonders GitHub : ChrisTitusTech/winutil Version : $($sync.version) "@ Show-CustomDialog -Title "About" -Message $authorInfo }) $sync["SponsorMenuItem"].Add_Click({ Write-Debug "Sponsors clicked" Invoke-WPFPopup -Action "Hide" -Popups @("Settings") $authorInfo = @" Current sponsors for ChrisTitusTech: "@ $authorInfo += "`n" try { $sponsors = Invoke-WinUtilSponsors foreach ($sponsor in $sponsors) { $authorInfo += "$sponsor`n" } } catch { $authorInfo += "An error occurred while fetching or processing the sponsors: $_`n" } Show-CustomDialog -Title "Sponsors" -Message $authorInfo -EnableScroll $true }) $sync["Form"].ShowDialog() | out-null Stop-Transcript