Skip to content

Commit 08f375a

Browse files
committed
Update tailscale-control.ps1
1 parent b3804eb commit 08f375a

1 file changed

Lines changed: 119 additions & 19 deletions

File tree

tailscale-control.ps1

Lines changed: 119 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6975,57 +6975,108 @@ function Add-TrayNetworkOwnerMenu {
69756975

69766976
function Update-TrayNetworkDevicesMenuState {
69776977
param($Snapshot)
6978+
69786979
if ($null -eq $script:TrayMenuNetworkDevices) { return }
6980+
69796981
try {
69806982
$menu = $script:TrayMenuNetworkDevices
69816983
try { if ($menu.DropDown.Visible) { return } } catch { }
6984+
69826985
$snap = if ($null -ne $Snapshot) { $Snapshot } else { $script:Snapshot }
6983-
$signature = Get-TrayNetworkDevicesSignature -Snapshot $snap
6984-
if ($script:TrayNetworkDevicesSignature -eq $signature -and $menu.DropDownItems.Count -gt 0 -and [string]$menu.DropDownItems[0].Text -ne 'Loading...') { return }
6985-
Clear-TrayDropDownItemsSafe -MenuItem $menu
6986-
$script:TrayNetworkDevicesSignature = $signature
6986+
69876987
$menu.Text = 'Network Devices'
6988-
Set-TrayMenuItemFixedSize -Item $menu -BaseWidth 300 -BaseHeight 28
6988+
Set-TrayMenuItemFixedSize -Item $menu -BaseWidth 340 -BaseHeight 28
69896989
Set-TrayDropDownRenderer -Item $menu
69906990
Register-TraySubmenuArrowGlyph -Item $menu
6991-
if ($null -eq $snap -or -not [bool](Get-ObjectPropertyOrDefault $snap 'Found' $false)) {
6991+
try { $menu.DropDownDirection = [System.Windows.Forms.ToolStripDropDownDirection]::Right } catch { }
6992+
6993+
if ($null -eq $snap) {
6994+
$signature = '__loading__'
6995+
if ($script:TrayNetworkDevicesSignature -eq $signature -and $menu.DropDownItems.Count -gt 0) { return }
6996+
6997+
Clear-TrayDropDownItemsSafe -MenuItem $menu
6998+
$script:TrayNetworkDevicesSignature = $signature
6999+
$menu.Enabled = $true
7000+
7001+
$loading = $menu.DropDownItems.Add('Loading...')
7002+
Set-TrayMenuItemFixedSize -Item $loading -BaseWidth 420 -BaseHeight 28
7003+
$loading.Enabled = $false
7004+
$loading.TextAlign = [System.Drawing.ContentAlignment]::MiddleLeft
7005+
return
7006+
}
7007+
7008+
$signature = Get-TrayNetworkDevicesSignature -Snapshot $snap
7009+
if ($script:TrayNetworkDevicesSignature -eq $signature -and $menu.DropDownItems.Count -gt 0) { return }
7010+
7011+
Clear-TrayDropDownItemsSafe -MenuItem $menu
7012+
$script:TrayNetworkDevicesSignature = $signature
7013+
7014+
if (-not [bool](Get-ObjectPropertyOrDefault $snap 'Found' $false)) {
69927015
$menu.Enabled = $true
7016+
69937017
$empty = $menu.DropDownItems.Add('Tailscale not detected')
69947018
Set-TrayMenuItemFixedSize -Item $empty -BaseWidth 420 -BaseHeight 28
69957019
$empty.Enabled = $false
7020+
$empty.TextAlign = [System.Drawing.ContentAlignment]::MiddleLeft
69967021
return
69977022
}
7023+
69987024
$machines = @(Convert-ToObjectArray (Get-ObjectPropertyOrDefault $snap 'Machines' @()))
7025+
69997026
if ($machines.Count -le 0) {
70007027
$menu.Enabled = $true
7028+
70017029
$empty = $menu.DropDownItems.Add('No devices detected')
70027030
Set-TrayMenuItemFixedSize -Item $empty -BaseWidth 420 -BaseHeight 28
70037031
$empty.Enabled = $false
7032+
$empty.TextAlign = [System.Drawing.ContentAlignment]::MiddleLeft
70047033
return
70057034
}
7035+
70067036
$menu.Enabled = $true
7037+
70077038
$selfOwner = ConvertTo-DiagnosticText -Text ([string](Get-ObjectPropertyOrDefault $snap 'User' ''))
70087039
$selfEmail = ConvertTo-DiagnosticText -Text ([string](Get-ObjectPropertyOrDefault $snap 'UserEmail' ''))
7040+
70097041
$myDevices = @($machines | Where-Object {
70107042
$owner = ConvertTo-DiagnosticText -Text ([string](Get-ObjectPropertyOrDefault $_ 'Owner' ''))
70117043
[bool](Get-ObjectPropertyOrDefault $_ 'IsLocal' $false) -or
70127044
(-not [string]::IsNullOrWhiteSpace($selfOwner) -and [string]::Equals($owner, $selfOwner, [System.StringComparison]::OrdinalIgnoreCase)) -or
70137045
(-not [string]::IsNullOrWhiteSpace($selfEmail) -and [string]::Equals($owner, $selfEmail, [System.StringComparison]::OrdinalIgnoreCase))
70147046
})
7047+
70157048
Add-TrayNetworkOwnerMenu -ParentMenu $menu -Owner 'My Devices' -Machines $myDevices
7049+
70167050
$otherMachines = @($machines | Where-Object {
70177051
$candidate = $_
70187052
-not (@($myDevices | Where-Object { $_ -eq $candidate }).Count -gt 0)
70197053
})
7020-
if ($otherMachines.Count -gt 0) { [void]$menu.DropDownItems.Add((New-Object System.Windows.Forms.ToolStripSeparator)) }
7021-
$owners = @($otherMachines | ForEach-Object { ConvertTo-DiagnosticText -Text ([string](Get-ObjectPropertyOrDefault $_ 'Owner' 'Unknown Owner')) } | Where-Object { -not [string]::IsNullOrWhiteSpace([string]$_) } | Sort-Object -Unique)
7054+
7055+
if ($otherMachines.Count -gt 0) {
7056+
[void]$menu.DropDownItems.Add((New-Object System.Windows.Forms.ToolStripSeparator))
7057+
}
7058+
7059+
$owners = @($otherMachines | ForEach-Object {
7060+
ConvertTo-DiagnosticText -Text ([string](Get-ObjectPropertyOrDefault $_ 'Owner' 'Unknown Owner'))
7061+
} | Where-Object {
7062+
-not [string]::IsNullOrWhiteSpace([string]$_)
7063+
} | Sort-Object -Unique)
7064+
70227065
foreach ($owner in $owners) {
7023-
$ownerDevices = @($otherMachines | Where-Object { [string]::Equals((ConvertTo-DiagnosticText -Text ([string](Get-ObjectPropertyOrDefault $_ 'Owner' 'Unknown Owner'))), [string]$owner, [System.StringComparison]::OrdinalIgnoreCase) })
7066+
$ownerDevices = @($otherMachines | Where-Object {
7067+
[string]::Equals((ConvertTo-DiagnosticText -Text ([string](Get-ObjectPropertyOrDefault $_ 'Owner' 'Unknown Owner'))), [string]$owner, [System.StringComparison]::OrdinalIgnoreCase)
7068+
})
7069+
70247070
Add-TrayNetworkOwnerMenu -ParentMenu $menu -Owner $owner -Machines $ownerDevices
70257071
}
7026-
if ($owners.Count -le 0 -and $otherMachines.Count -gt 0) { Add-TrayNetworkOwnerMenu -ParentMenu $menu -Owner 'Other Devices' -Machines $otherMachines }
7072+
7073+
if ($owners.Count -le 0 -and $otherMachines.Count -gt 0) {
7074+
Add-TrayNetworkOwnerMenu -ParentMenu $menu -Owner 'Other Devices' -Machines $otherMachines
7075+
}
7076+
}
7077+
catch {
7078+
Write-LogException -Context 'Update tray network devices menu state' -ErrorRecord $_
70277079
}
7028-
catch { Write-LogException -Context 'Update tray network devices menu state' -ErrorRecord $_ }
70297080
}
70307081

70317082
function Update-TraySelectAccountMenuState {
@@ -7087,57 +7138,103 @@ function Update-TraySelectAccountMenuState {
70877138

70887139
function Update-TrayExitNodeMenuState {
70897140
param($Snapshot)
7141+
70907142
if ($null -eq $script:TrayMenuChooseExitNode) { return }
7143+
70917144
try {
70927145
$menu = $script:TrayMenuChooseExitNode
70937146
try { if ($menu.DropDown.Visible) { return } } catch { }
7147+
70947148
$snap = if ($null -ne $Snapshot) { $Snapshot } else { $script:Snapshot }
7149+
7150+
$menu.Text = 'Preferred Exit Node'
7151+
Set-TrayMenuItemFixedSize -Item $menu -BaseWidth 340 -BaseHeight 28
7152+
Register-TraySubmenuArrowGlyph -Item $menu
7153+
Set-TrayDropDownRenderer -Item $menu
7154+
try { $menu.DropDownDirection = [System.Windows.Forms.ToolStripDropDownDirection]::Right } catch { }
7155+
7156+
if ($null -eq $snap) {
7157+
$signature = '__loading__'
7158+
if ($script:TrayExitNodeSignature -eq $signature -and $menu.DropDownItems.Count -gt 0) { return }
7159+
7160+
Clear-TrayDropDownItemsSafe -MenuItem $menu
7161+
$script:TrayExitNodeSignature = $signature
7162+
$menu.Enabled = $true
7163+
7164+
$loading = $menu.DropDownItems.Add('Loading...')
7165+
Set-TrayMenuItemFixedSize -Item $loading -BaseWidth 340 -BaseHeight 28
7166+
$loading.Enabled = $false
7167+
$loading.TextAlign = [System.Drawing.ContentAlignment]::MiddleLeft
7168+
return
7169+
}
7170+
70957171
$signature = Get-TrayExitNodeSignature -Snapshot $snap
70967172
if ($script:TrayExitNodeSignature -eq $signature -and $menu.DropDownItems.Count -gt 0) { return }
7173+
70977174
Clear-TrayDropDownItemsSafe -MenuItem $menu
70987175
$script:TrayExitNodeSignature = $signature
7099-
$menu.Text = 'Preferred Exit Node'
7100-
Set-TrayMenuItemFixedSize -Item $menu -BaseWidth 300 -BaseHeight 28
7101-
Register-TraySubmenuArrowGlyph -Item $menu
7102-
if ($null -eq $snap -or -not [bool](Get-ObjectPropertyOrDefault $snap 'Found' $false)) {
7176+
7177+
if (-not [bool](Get-ObjectPropertyOrDefault $snap 'Found' $false)) {
71037178
$menu.Enabled = $false
7104-
[void]$menu.DropDownItems.Add('Tailscale not detected')
7179+
7180+
$item = $menu.DropDownItems.Add('Tailscale not detected')
7181+
Set-TrayMenuItemFixedSize -Item $item -BaseWidth 340 -BaseHeight 28
7182+
$item.Enabled = $false
7183+
$item.TextAlign = [System.Drawing.ContentAlignment]::MiddleLeft
71057184
return
71067185
}
7186+
71077187
$nodes = @(Convert-ToObjectArray $snap.ExitNodes)
7188+
71087189
if ($nodes.Count -eq 0) {
71097190
$menu.Enabled = $false
7110-
[void]$menu.DropDownItems.Add('No exit nodes detected')
7191+
7192+
$item = $menu.DropDownItems.Add('No exit nodes detected')
7193+
Set-TrayMenuItemFixedSize -Item $item -BaseWidth 340 -BaseHeight 28
7194+
$item.Enabled = $false
7195+
$item.TextAlign = [System.Drawing.ContentAlignment]::MiddleLeft
71117196
return
71127197
}
7198+
71137199
$menu.Enabled = $true
71147200
$preferred = Get-PreferredExitNodeLabel
7201+
71157202
foreach ($node in $nodes) {
71167203
if ($null -eq $node) { continue }
7204+
71177205
$label = ConvertTo-DnsName ([string](Get-PropertyValue $node @('DNSName','Name','Node')))
71187206
if ([string]::IsNullOrWhiteSpace($label)) { continue }
7207+
71197208
$labelForClick = $label
71207209
$item = $menu.DropDownItems.Add($label)
71217210
Set-TrayMenuItemFixedSize -Item $item -BaseWidth 340 -BaseHeight 28
71227211
$item.TextAlign = [System.Drawing.ContentAlignment]::MiddleLeft
7212+
71237213
$nodeName = ConvertTo-DnsName ([string](Get-PropertyValue $node @('Name')))
71247214
$nodeDns = ConvertTo-DnsName ([string](Get-PropertyValue $node @('DNSName')))
71257215
$nodeIp = ConvertTo-DnsName ([string](Get-PropertyValue $node @('IPv4')))
7216+
71267217
$item.Checked = (-not [string]::IsNullOrWhiteSpace($preferred) -and ($preferred -eq $label -or $preferred -eq $nodeName -or $preferred -eq $nodeDns -or $preferred -eq $nodeIp))
71277218
$item.add_Click({ Set-TrayExitNodeSelection -Label $labelForClick }.GetNewClosure())
71287219
}
7220+
71297221
[void]$menu.DropDownItems.Add((New-Object System.Windows.Forms.ToolStripSeparator))
7222+
71307223
$allowLan = $menu.DropDownItems.Add('Allow local network access')
71317224
$script:TrayMenuAllowLanAccess = $allowLan
7132-
Set-TrayMenuItemFixedSize -Item $allowLan -BaseWidth 300 -BaseHeight 28
7225+
Set-TrayMenuItemFixedSize -Item $allowLan -BaseWidth 340 -BaseHeight 28
7226+
$allowLan.TextAlign = [System.Drawing.ContentAlignment]::MiddleLeft
71337227
$allowLan.CheckOnClick = $false
71347228
$allowLan.Checked = [bool](Get-Config).allow_lan_on_exit
7229+
71357230
$allowLan.add_Click({
71367231
try { Set-AllowLanAccessPreferenceFromTray -Enabled:(!$script:TrayMenuAllowLanAccess.Checked) }
71377232
catch { Show-Overlay -Title 'Update local network access failed' -Message $_.Exception.Message -ErrorStyle }
71387233
})
71397234
}
7140-
catch { Write-LogException -Context 'Update tray preferred exit node menu state' -ErrorRecord $_ }
7235+
catch {
7236+
Write-LogException -Context 'Update tray preferred exit node menu state' -ErrorRecord $_
7237+
}
71417238
}
71427239

71437240
function Update-TrayMenuState {
@@ -14502,6 +14599,9 @@ $script:TrayMenuChooseExitNode = New-Object System.Windows.Forms.ToolStripMenuIt
1450214599
$script:TrayMenuChooseExitNode.Text = 'Preferred Exit Node'
1450314600
Set-TrayMenuItemFixedSize -Item $script:TrayMenuChooseExitNode -BaseWidth 340 -BaseHeight 28
1450414601
Register-TraySubmenuArrowGlyph -Item $script:TrayMenuChooseExitNode
14602+
try { $script:TrayMenuChooseExitNode.DropDownDirection = [System.Windows.Forms.ToolStripDropDownDirection]::Right } catch { }
14603+
[void]$script:TrayMenuChooseExitNode.DropDownItems.Add('Loading...')
14604+
Set-TrayDropDownRenderer -Item $script:TrayMenuChooseExitNode
1450514605
[void]$contextMenu.Items.Add($script:TrayMenuChooseExitNode)
1450614606
[void]$contextMenu.Items.Add((New-Object System.Windows.Forms.ToolStripSeparator))
1450714607
$script:TrayMenuExit = $contextMenu.Items.Add('Exit')

0 commit comments

Comments
 (0)