Skip to content
Open
52 changes: 52 additions & 0 deletions functions/private/Close-WinUtilFaviconRunspacePool.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
function Close-WinUtilFaviconRunspacePool {
<#
.SYNOPSIS
Stops favicon work and disposes its timer, operations, circuit breaker, and runspace pool.
#>
if ($null -eq $sync) {
return
}

if ($sync.FaviconCircuitBreaker) {
$sync.FaviconCircuitBreaker.Cancel()
}

if ($sync.FaviconTimer) {
$sync.FaviconTimer.Stop()
$sync.Remove("FaviconTimer")
}

if ($sync.FaviconOperations) {
foreach ($operation in @($sync.FaviconOperations.Values)) {
try {
$operation.PowerShell.Stop()
} catch {
}
try {
$operation.PowerShell.Dispose()
} catch {
}
}
$sync.FaviconOperations.Clear()
}
Comment thread
mewclouds marked this conversation as resolved.

if ($sync.FaviconRunspace) {
try {
if ($sync.FaviconRunspace.RunspacePoolStateInfo.State -notin @(
[System.Management.Automation.Runspaces.RunspacePoolState]::Closed,
[System.Management.Automation.Runspaces.RunspacePoolState]::Closing,
[System.Management.Automation.Runspaces.RunspacePoolState]::Broken
)) {
$sync.FaviconRunspace.Close()
}
} finally {
$sync.FaviconRunspace.Dispose()
$sync.Remove("FaviconRunspace")
}
}

if ($sync.FaviconCircuitBreaker) {
$sync.FaviconCircuitBreaker.Dispose()
$sync.Remove("FaviconCircuitBreaker")
}
}
19 changes: 19 additions & 0 deletions functions/private/Get-WinUtilFaviconUrl.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
function Get-WinUtilFaviconUrl {
<#
.SYNOPSIS
Builds the Google favicon service URL for an application link.
.PARAMETER Link
The application website URL whose favicon should be requested.
#>
param(
[Parameter(Mandatory = $true)]
[string]$Link
)

if ([string]::IsNullOrWhiteSpace($Link)) {
return $null
}

$faviconSize = 64
return "https://www.google.com/s2/favicons?sz=$faviconSize&domain_url=$([uri]::EscapeDataString($Link))"
}
9 changes: 6 additions & 3 deletions functions/private/Initialize-InstallAppEntry.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -71,15 +71,15 @@ function Initialize-InstallAppEntry {
$fallback = New-Object Windows.Controls.TextBlock
$fallback.Text = $app.content.TrimStart(".").Substring(0, 1).ToUpper()
$fallback.FontWeight = "Bold"; $fallback.HorizontalAlignment = "Center"; $fallback.VerticalAlignment = "Center"
if ($app.link) { $fallback.Visibility = "Collapsed" }
$fallback.SetResourceReference([Windows.Controls.TextBlock]::FontSizeProperty, "AppEntryFontSize")
$fallback.SetResourceReference([Windows.Controls.TextBlock]::ForegroundProperty, "ToggleButtonOnColor")
[void]$icon.Children.Add($fallback)
$faviconUrl = $null
if ($app.link) {
$logo = New-Object Windows.Controls.Image
$logo.Stretch = [Windows.Media.Stretch]::Uniform
$logo.Source = "https://www.google.com/s2/favicons?sz=64&domain_url=$([uri]::EscapeDataString($app.link))"
$logo.Add_ImageFailed({ $this.Visibility = "Collapsed"; $this.Parent.Children[0].Visibility = "Visible" })
$logo.Visibility = [Windows.Visibility]::Collapsed
$faviconUrl = Get-WinUtilFaviconUrl -Link $app.link
[void]$icon.Children.Add($logo)
}
[void]$contentPanel.Children.Add($icon)
Expand Down Expand Up @@ -110,5 +110,8 @@ function Initialize-InstallAppEntry {
}
# Add the border to the corresponding Category
$TargetElement.Children.Add($border) | Out-Null
if ($faviconUrl) {
Invoke-WinUtilFaviconFetch -AppKey $appKey -Url $faviconUrl -TargetImage $logo -Fallback $fallback
Comment thread
mewclouds marked this conversation as resolved.
Outdated
}
return $checkbox
}
33 changes: 33 additions & 0 deletions functions/private/Initialize-WinUtilFaviconRunspacePool.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
function Initialize-WinUtilFaviconRunspacePool {
<#
.SYNOPSIS
Creates or returns the dedicated runspace pool used for favicon downloads.
.DESCRIPTION
Uses half the available logical processors while keeping concurrency between
two and eight workers so favicon requests remain responsive without creating
an excessive burst of connections.
#>
if ($sync.FaviconRunspace -and $sync.FaviconRunspace.RunspacePoolStateInfo.State -eq [System.Management.Automation.Runspaces.RunspacePoolState]::Opened) {
return $sync.FaviconRunspace
}

if ($sync.FaviconRunspace) {
Close-WinUtilFaviconRunspacePool
}
Comment thread
coderabbitai[bot] marked this conversation as resolved.

$minimumWorkers = 2
$maximumWorkers = 8
$halfProcessors = [Math]::Floor([Environment]::ProcessorCount / 2)
$maxThreads = [Math]::Max($halfProcessors, $minimumWorkers)
$maxThreads = [Math]::Min($maxThreads, $maximumWorkers)
$initialSessionState = [System.Management.Automation.Runspaces.InitialSessionState]::CreateDefault()

$sync.FaviconRunspace = [runspacefactory]::CreateRunspacePool(
1,
$maxThreads,
$initialSessionState,
$Host
)
$sync.FaviconRunspace.Open()
return $sync.FaviconRunspace
}
Loading