From 4bc54de8cd46c4dacac277fcf722f3a1c65cebb9 Mon Sep 17 00:00:00 2001 From: MyDrift Date: Tue, 25 Jun 2024 20:54:18 +0200 Subject: [PATCH] Hyperlinks to about section (#2080) * 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 * Fix - change version link to match changes made to release - removed date formatting as it is not needed anymore - Renamed Github Link to "ChrisTitusTech/winutil" because you can't select the text but click on it to open the link directly so it is unnecessary * fix opening link - switched method to open hyperlinks old: [System.Diagnostics.Process]::Start new: Start-Process --- functions/private/Show-CustomDialog.ps1 | 54 ++++++++++++++++++++++++- scripts/main.ps1 | 13 +++--- 2 files changed, 58 insertions(+), 9 deletions(-) diff --git a/functions/private/Show-CustomDialog.ps1 b/functions/private/Show-CustomDialog.ps1 index e102c43e..505c7a8d 100644 --- a/functions/private/Show-CustomDialog.ps1 +++ b/functions/private/Show-CustomDialog.ps1 @@ -178,17 +178,67 @@ $cttLogoPath = @" $winutilTextBlock.Foreground = $foregroundColor $winutilTextBlock.Margin = New-Object Windows.Thickness(10, 5, 10, 5) # Add margins around the text block $stackPanel.Children.Add($winutilTextBlock) - # Add TextBlock for information with text wrapping and margins $messageTextBlock = New-Object Windows.Controls.TextBlock - $messageTextBlock.Text = $Message $messageTextBlock.TextWrapping = [Windows.TextWrapping]::Wrap # Enable text wrapping $messageTextBlock.HorizontalAlignment = [Windows.HorizontalAlignment]::Left $messageTextBlock.VerticalAlignment = [Windows.VerticalAlignment]::Top $messageTextBlock.Margin = New-Object Windows.Thickness(10) # Add margins around the text block + + # Define the Regex to find hyperlinks formatted as HTML tags + $regex = [regex]::new('([^<]+)') + $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) + Start-Process $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) [Windows.Controls.Grid]::SetRow($messageTextBlock, 1) # Set the row to the second row (0-based index) + # Add OK button $okButton = New-Object Windows.Controls.Button $okButton.Content = "OK" diff --git a/scripts/main.ps1 b/scripts/main.ps1 index 23978ab7..6f603e46 100644 --- a/scripts/main.ps1 +++ b/scripts/main.ps1 @@ -467,14 +467,13 @@ $sync["AboutMenuItem"].Add_Click({ # Handle Export menu item click Write-Debug "About clicked" $sync["SettingsPopup"].IsOpen = $false - # Example usage $authorInfo = @" -Author : @christitustech -Runspace : @DeveloperDurp -GUI : @KonTy -MicroWin : @KonTy -GitHub : https://github.com/ChrisTitusTech/winutil -Version : $($sync.version) +Author : @christitustech +Runspace : @DeveloperDurp +GUI : @KonTy +MicroWin : @KonTy +GitHub : ChrisTitusTech/winutil +Version : $($sync.version) "@ Show-CustomDialog -Message $authorInfo -Width 400 })