Refactor: Centralize Install Apps checkbox logic

Consolidates the state management for the "Install Apps" checkbox into a single, reusable function.

Previously, the logic to automatically check and disable "Install Apps" when selecting an update or installing Office was duplicated and scattered across multiple event handlers and files.

This change introduces a new core function that centralizes this behavior. A single event handler is now used for all relevant checkboxes (Updates and Office), simplifying the UI code, reducing redundancy, and making the logic more robust and maintainable. The initial state is also set correctly on startup.
This commit is contained in:
rbalsleyMSFT
2025-06-18 13:07:07 -07:00
parent af5d5206f8
commit fd39b0008e
4 changed files with 66 additions and 70 deletions
+37
View File
@@ -188,6 +188,43 @@ function Get-USBDrives {
}
}
# Function to manage the state of the main "Install Apps" checkbox based on selections in Updates/Office
function Update-InstallAppsState {
param([PSCustomObject]$State)
$installAppsChk = $State.Controls.chkInstallApps
$installOfficeChk = $State.Controls.chkInstallOffice
# Determine if any checkbox that forces "Install Apps" is checked
$anyUpdateChecked = $State.Controls.chkUpdateLatestDefender.IsChecked -or `
$State.Controls.chkUpdateEdge.IsChecked -or `
$State.Controls.chkUpdateOneDrive.IsChecked -or `
$State.Controls.chkUpdateLatestMSRT.IsChecked
$isForced = $anyUpdateChecked -or $installOfficeChk.IsChecked
if ($isForced) {
# If InstallApps is not already forced (i.e., it's enabled), save its current state.
if ($installAppsChk.IsEnabled) {
$State.Flags.prevInstallAppsState = $installAppsChk.IsChecked
}
$installAppsChk.IsChecked = $true
$installAppsChk.IsEnabled = $false
}
else {
# No longer forced. Restore the previous state if it was saved.
if ($State.Flags.ContainsKey('prevInstallAppsState')) {
$installAppsChk.IsChecked = $State.Flags.prevInstallAppsState
$State.Flags.Remove('prevInstallAppsState') # Use the saved state only once
}
else {
# If no state was saved (e.g., it was never forced), ensure it's unchecked.
$installAppsChk.IsChecked = $false
}
$installAppsChk.IsEnabled = $true
}
}
# --------------------------------------------------------------------------
# SECTION: Module Export
# --------------------------------------------------------------------------