Private/UI/Show-EwWorkbenchDownloadTray.ps1

Set-StrictMode -Version Latest
$ErrorActionPreference = 'Stop'

function Show-EwWorkbenchDownloadTray {
    [CmdletBinding()]
    param([Parameter(Mandatory)][System.Collections.IDictionary]$State)

    $pending = @($State.Queue | Where-Object Status -eq 'Pending').Count
    $downloading = @($State.Queue | Where-Object Status -eq 'Downloading').Count
    $finished = @($State.Queue | Where-Object Status -in @('Downloaded', 'Failed')).Count
    $activeDownload = $null -ne $State.ActiveJobId -and $State.ActiveJobKind -eq 'Downloads'
    $activeCount = $pending + $downloading

    $State.AppsQueueCountText.Text = [string]$activeCount
    $State.AppsQueueCountBadge.Visibility = if ($activeCount -gt 0) {
        [System.Windows.Visibility]::Visible
    }
    else {
        [System.Windows.Visibility]::Collapsed
    }
    [System.Windows.Automation.AutomationProperties]::SetName(
        $State.NavApps,
        "Apps view, $activeCount pending or running downloads"
    )

    $State.DownloadDestinationText.Text = [string]$State.Settings.OutputPath
    $State.DownloadDestinationText.ToolTip = [string]$State.Settings.OutputPath
    $summary = @(
        if ($pending -gt 0) { "$pending pending" }
        if ($downloading -gt 0) { "$downloading downloading" }
        if ($finished -gt 0) { "$finished completed or failed" }
    ) -join ', '
    if ([string]::IsNullOrWhiteSpace($summary)) {
        $summary = 'empty'
    }
    $isTrayExpanded = [bool]$State.DownloadTrayToggleButton.IsChecked
    $verb = if ($isTrayExpanded) { 'Hide' } else { 'Show' }
    $State.DownloadTrayToggleIcon.Text = if ($isTrayExpanded) { [char]0xED1A } else { [char]0xE890 }
    $State.DownloadTrayToggleLabel.Text = "$verb queue ($summary)"
    if ($State.Queue.Count -gt 0) {
        $State.DownloadTrayPanel.Visibility = [System.Windows.Visibility]::Visible
    }
    else {
        $State.DownloadTrayPanel.Visibility = [System.Windows.Visibility]::Collapsed
        $State.DownloadTrayToggleButton.IsChecked = $false
    }
    $trayExpanded = $State.Queue.Count -gt 0 -and [bool]$State.DownloadTrayToggleButton.IsChecked
    $State.DownloadTrayContent.Visibility = if ($trayExpanded) {
        [System.Windows.Visibility]::Visible
    }
    else {
        [System.Windows.Visibility]::Collapsed
    }
    $State.DownloadTrayGridSplitter.Visibility = if ($trayExpanded) {
        [System.Windows.Visibility]::Visible
    }
    elseif ($State.Queue.Count -gt 0) {
        # Retain the splitter's spacing above the tray header while the table is hidden.
        [System.Windows.Visibility]::Hidden
    }
    else {
        [System.Windows.Visibility]::Collapsed
    }
    if ($State.Queue.Count -eq 0) {
        $State.AppsPanel.RowDefinitions[3].Height = [System.Windows.GridLength]::new(0)
    }
    elseif ($trayExpanded) {
        $trayRow = $State.AppsPanel.RowDefinitions[3]
        if ($trayRow.Height.IsAuto -or $trayRow.Height.Value -le 0) {
            $height = [int]$State.Settings.DownloadTrayHeight
            $trayRow.Height = [System.Windows.GridLength]::new($height)
        }
    }
    else {
        $State.AppsPanel.RowDefinitions[3].Height = [System.Windows.GridLength]::Auto
    }
    $State.DownloadTrayActionLabel.Text = 'Download queue'
    [System.Windows.Automation.AutomationProperties]::SetHelpText(
        $State.DownloadTrayActionButton,
        $(if ($activeDownload) { 'Stop the active download' } else { 'Download pending items in the queue' })
    )
    $State.DownloadTrayActionButton.IsEnabled = $activeDownload -or
        ($null -eq $State.ActiveJobId -and $pending -gt 0)
    $State.DownloadTrayToggleButton.IsEnabled = $State.Queue.Count -gt 0
    $State.DownloadTrayQueueActionsRow.Visibility = if ($State.Queue.Count -gt 0) {
        [System.Windows.Visibility]::Visible
    }
    else {
        [System.Windows.Visibility]::Collapsed
    }
    $State.ClearCompletedQueueButton.IsEnabled = $finished -gt 0
    $State.ClearQueueButton.IsEnabled = $State.Queue.Count -gt 0 -and $downloading -eq 0 -and
        $null -eq $State.ActiveJobId
    $canAddArtifacts = $State.ArtifactsGrid.SelectedItems.Count -gt 0 -and $null -eq $State.ActiveJobId
    $State.QueueDownloadButton.IsEnabled = $canAddArtifacts
    $State.DownloadNowButton.IsEnabled = $canAddArtifacts
    Invoke-EwWorkbenchLibraryRegistrationState -State $State
}