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
-54
View File
@@ -188,60 +188,6 @@ $window.Add_Loaded({
$script:uiState.Controls.OfficeConfigurationXMLFileGrid.Visibility = 'Collapsed'
}
# Updates/InstallApps interplay (Keep existing logic)
$script:uiState.Flags.installAppsForcedByUpdates = $false
$script:uiState.Flags.prevInstallAppsStateBeforeUpdates = $null
# Define the scriptblock within the Loaded event and assign it to the state object
$script:uiState.Controls.UpdateInstallAppsBasedOnUpdates = {
param($State) # Pass state object to avoid using $script: scope inside
$anyUpdateChecked = $State.Controls.chkUpdateLatestDefender.IsChecked -or $State.Controls.chkUpdateEdge.IsChecked -or $State.Controls.chkUpdateOneDrive.IsChecked -or $State.Controls.chkUpdateLatestMSRT.IsChecked
if ($anyUpdateChecked) {
if (-not $State.Flags.installAppsForcedByUpdates) {
$State.Flags.prevInstallAppsStateBeforeUpdates = $State.Controls.chkInstallApps.IsChecked
$State.Flags.installAppsForcedByUpdates = $true
}
$State.Controls.chkInstallApps.IsChecked = $true
$State.Controls.chkInstallApps.IsEnabled = $false
}
else {
if ($State.Flags.installAppsForcedByUpdates) {
$State.Controls.chkInstallApps.IsChecked = $State.Flags.prevInstallAppsStateBeforeUpdates
$State.Flags.installAppsForcedByUpdates = $false
$State.Flags.prevInstallAppsStateBeforeUpdates = $null
}
# Only re-enable InstallApps if not forced by Office
if (-not $State.Controls.chkInstallOffice.IsChecked) {
$State.Controls.chkInstallApps.IsEnabled = $true
}
}
}
$script:uiState.Controls.chkUpdateLatestDefender.Add_Checked({
& $script:uiState.Controls.UpdateInstallAppsBasedOnUpdates -State $script:uiState
})
$script:uiState.Controls.chkUpdateLatestDefender.Add_Unchecked({
& $script:uiState.Controls.UpdateInstallAppsBasedOnUpdates -State $script:uiState
})
$script:uiState.Controls.chkUpdateEdge.Add_Checked({
& $script:uiState.Controls.UpdateInstallAppsBasedOnUpdates -State $script:uiState
})
$script:uiState.Controls.chkUpdateEdge.Add_Unchecked({
& $script:uiState.Controls.UpdateInstallAppsBasedOnUpdates -State $script:uiState
})
$script:uiState.Controls.chkUpdateOneDrive.Add_Checked({
& $script:uiState.Controls.UpdateInstallAppsBasedOnUpdates -State $script:uiState
})
$script:uiState.Controls.chkUpdateOneDrive.Add_Unchecked({
& $script:uiState.Controls.UpdateInstallAppsBasedOnUpdates -State $script:uiState
})
$script:uiState.Controls.chkUpdateLatestMSRT.Add_Checked({
& $script:uiState.Controls.UpdateInstallAppsBasedOnUpdates -State $script:uiState
})
$script:uiState.Controls.chkUpdateLatestMSRT.Add_Unchecked({
& $script:uiState.Controls.UpdateInstallAppsBasedOnUpdates -State $script:uiState
})
# Initial check for Updates/InstallApps state
& $script:uiState.Controls.UpdateInstallAppsBasedOnUpdates -State $script:uiState
# CU interplay (Keep existing logic)
$script:uiState.Controls.chkLatestCU.Add_Checked({
$script:uiState.Controls.chkPreviewCU.IsEnabled = $false