Hyperlink to CustomDialogs

- added ability to add hyperlinks to CustomDialogs
- Added custom Dialog for every item in the About Section
- added custom link to see the commits of the day of the version the script is on
- added hover effect for linked items
This commit is contained in:
MyDrift 2024-06-10 21:07:05 +02:00
parent 3dca1ee43e
commit 333f230ea6
2 changed files with 63 additions and 9 deletions

View File

@ -178,17 +178,67 @@ $cttLogoPath = @"
$winutilTextBlock.Foreground = $foregroundColor $winutilTextBlock.Foreground = $foregroundColor
$winutilTextBlock.Margin = New-Object Windows.Thickness(10, 5, 10, 5) # Add margins around the text block $winutilTextBlock.Margin = New-Object Windows.Thickness(10, 5, 10, 5) # Add margins around the text block
$stackPanel.Children.Add($winutilTextBlock) $stackPanel.Children.Add($winutilTextBlock)
# Add TextBlock for information with text wrapping and margins # Add TextBlock for information with text wrapping and margins
$messageTextBlock = New-Object Windows.Controls.TextBlock $messageTextBlock = New-Object Windows.Controls.TextBlock
$messageTextBlock.Text = $Message
$messageTextBlock.TextWrapping = [Windows.TextWrapping]::Wrap # Enable text wrapping $messageTextBlock.TextWrapping = [Windows.TextWrapping]::Wrap # Enable text wrapping
$messageTextBlock.HorizontalAlignment = [Windows.HorizontalAlignment]::Left $messageTextBlock.HorizontalAlignment = [Windows.HorizontalAlignment]::Left
$messageTextBlock.VerticalAlignment = [Windows.VerticalAlignment]::Top $messageTextBlock.VerticalAlignment = [Windows.VerticalAlignment]::Top
$messageTextBlock.Margin = New-Object Windows.Thickness(10) # Add margins around the text block $messageTextBlock.Margin = New-Object Windows.Thickness(10) # Add margins around the text block
# Define the Regex to find hyperlinks formatted as HTML <a> tags
$regex = [regex]::new('<a href="([^"]+)">([^<]+)</a>')
$lastPos = 0
# Iterate through each match and add regular text and hyperlinks
foreach ($match in $regex.Matches($Message)) {
# Add the text before the hyperlink, if any
$textBefore = $Message.Substring($lastPos, $match.Index - $lastPos)
if ($textBefore.Length -gt 0) {
$messageTextBlock.Inlines.Add((New-Object Windows.Documents.Run($textBefore)))
}
# Create and add the hyperlink
$hyperlink = New-Object Windows.Documents.Hyperlink
$hyperlink.NavigateUri = New-Object System.Uri($match.Groups[1].Value)
$hyperlink.Inlines.Add($match.Groups[2].Value)
$hyperlink.TextDecorations = [Windows.TextDecorations]::None # Remove underline
$hyperlink.Foreground = $foregroundColor
$hyperlink.Add_Click({
param($sender, $args)
[System.Diagnostics.Process]::Start($sender.NavigateUri.AbsoluteUri)
})
$hyperlink.Add_MouseEnter({
param($sender, $args)
$sender.Foreground = [Windows.Media.Brushes]::LightGray
})
$hyperlink.Add_MouseLeave({
param($sender, $args)
$sender.Foreground = $foregroundColor
})
$messageTextBlock.Inlines.Add($hyperlink)
# Update the last position
$lastPos = $match.Index + $match.Length
}
# Add any remaining text after the last hyperlink
if ($lastPos -lt $Message.Length) {
$textAfter = $Message.Substring($lastPos)
$messageTextBlock.Inlines.Add((New-Object Windows.Documents.Run($textAfter)))
}
# If no matches, add the entire message as a run
if ($regex.Matches($Message).Count -eq 0) {
$messageTextBlock.Inlines.Add((New-Object Windows.Documents.Run($Message)))
}
# Add the TextBlock to the Grid
$grid.Children.Add($messageTextBlock) $grid.Children.Add($messageTextBlock)
[Windows.Controls.Grid]::SetRow($messageTextBlock, 1) # Set the row to the second row (0-based index) [Windows.Controls.Grid]::SetRow($messageTextBlock, 1) # Set the row to the second row (0-based index)
# Add OK button # Add OK button
$okButton = New-Object Windows.Controls.Button $okButton = New-Object Windows.Controls.Button
$okButton.Content = "OK" $okButton.Content = "OK"

View File

@ -467,14 +467,18 @@ $sync["AboutMenuItem"].Add_Click({
# Handle Export menu item click # Handle Export menu item click
Write-Debug "About clicked" Write-Debug "About clicked"
$sync["SettingsPopup"].IsOpen = $false $sync["SettingsPopup"].IsOpen = $false
# Example usage # Extract date from version
$versionDate = $sync.version
$date = [datetime]::ParseExact($versionDate, "yy.MM.dd", $null)
$formattedDate = $date.ToString("yyyy-MM-dd")
$commitLink = "https://github.com/ChrisTitusTech/winutil/commits/main/?since=$formattedDate&until=$formattedDate"
$authorInfo = @" $authorInfo = @"
Author : @christitustech Author : <a href="https://github.com/ChrisTitusTech">@christitustech</a>
Runspace : @DeveloperDurp Runspace : <a href="https://github.com/DeveloperDurp">@DeveloperDurp</a>
GUI : @KonTy GUI : <a href="https://github.com/KonTy">@KonTy</a>
MicroWin : @KonTy MicroWin : <a href="https://github.com/KonTy">@KonTy</a>
GitHub : https://github.com/ChrisTitusTech/winutil GitHub : <a href="https://github.com/ChrisTitusTech/winutil">https://github.com/ChrisTitusTech/winutil</a>
Version : $($sync.version) Version : <a href="$commitLink">$($sync.version)</a>
"@ "@
Show-CustomDialog -Message $authorInfo -Width 400 Show-CustomDialog -Message $authorInfo -Width 400
}) })