diff --git a/scripts/Invoke-ReplaySave.ps1 b/scripts/Invoke-ReplaySave.ps1 index 4c25307..c1fe50e 100644 --- a/scripts/Invoke-ReplaySave.ps1 +++ b/scripts/Invoke-ReplaySave.ps1 @@ -93,17 +93,7 @@ try { # --- Notify user that the replay was saved --- $notifyScript = Join-Path $PSScriptRoot 'Show-Notification.ps1' - Start-Process -FilePath 'powershell.exe' -ArgumentList @( - '-STA' - '-ExecutionPolicy', 'Bypass' - '-NonInteractive' - '-WindowStyle', 'Hidden' - '-File', $notifyScript - '-Type', 'ReplaySaved' - '-ContactNumber', $config.ITContactNumber - '-FileName', $savedFile.Name - '-FileSizeMB', $fileSizeMB - ) -WindowStyle Hidden + Start-Process -FilePath 'powershell.exe' -ArgumentList "-STA -ExecutionPolicy Bypass -NonInteractive -WindowStyle Hidden -File `"$notifyScript`" -Type ReplaySaved -ContactNumber `"$($config.ITContactNumber)`" -FileName `"$($savedFile.Name)`" -FileSizeMB $fileSizeMB" -WindowStyle Hidden Write-Host "$(Get-Date -Format 'yyyy-MM-dd HH:mm:ss') [INFO] User notification launched." return $true diff --git a/scripts/Show-Notification.ps1 b/scripts/Show-Notification.ps1 index 977c00c..f86dced 100644 --- a/scripts/Show-Notification.ps1 +++ b/scripts/Show-Notification.ps1 @@ -20,21 +20,40 @@ param( [double]$FileSizeMB = 0 ) +$ErrorActionPreference = 'Stop' + Add-Type -AssemblyName PresentationFramework Add-Type -AssemblyName WindowsBase Add-Type -AssemblyName System.Drawing -$hBitmap = [System.Drawing.SystemIcons]::Shield.ToBitmap().GetHbitmap() -$wpfImage = [System.Windows.Interop.Imaging]::CreateBitmapSourceFromHBitmap( +Add-Type -TypeDefinition @" +using System; +using System.Runtime.InteropServices; +public class GdiHelper { + [DllImport("gdi32.dll")] + public static extern bool DeleteObject(IntPtr hObject); +} +"@ + +$hBitmap = [System.Drawing.SystemIcons]::Shield.ToBitmap().GetHbitmap() +$wpfShield = [System.Windows.Interop.Imaging]::CreateBitmapSourceFromHBitmap( $hBitmap, [IntPtr]::Zero, [System.Windows.Int32Rect]::Empty, [System.Windows.Media.Imaging.BitmapSizeOptions]::FromEmptyOptions() ) -[System.Runtime.InteropServices.Marshal]::DeleteObject($hBitmap) +[GdiHelper]::DeleteObject($hBitmap) | Out-Null + +$hBitmap = [System.Drawing.SystemIcons]::Warning.ToBitmap().GetHbitmap() +$wpfWarning = [System.Windows.Interop.Imaging]::CreateBitmapSourceFromHBitmap( + $hBitmap, [IntPtr]::Zero, [System.Windows.Int32Rect]::Empty, + [System.Windows.Media.Imaging.BitmapSizeOptions]::FromEmptyOptions() +) +[GdiHelper]::DeleteObject($hBitmap) | Out-Null if ($Type -eq 'BufferStarted') { [xml]$xaml = @" @@ -69,8 +88,8 @@ if ($Type -eq 'BufferStarted') { $txtContact = $window.FindName('TxtContact') $btnOK = $window.FindName('BtnOK') - $imgHeader.Source = $wpfImage - $imgTray.Source = $wpfImage + $imgHeader.Source = $wpfWarning + $imgTray.Source = $wpfShield $txtContact.Text = "Questions? Contact IT at $ContactNumber" $txtBody.Inlines.Add("Your screen is being recorded into a rolling $BufferMinutes-minute replay buffer. Nothing is saved until you request it.`n`nTo save a clip, ") @@ -86,7 +105,8 @@ if ($Type -eq 'BufferStarted') { } else { [xml]$xaml = @" @@ -113,7 +133,7 @@ if ($Type -eq 'BufferStarted') { $txtContact = $window.FindName('TxtContact') $btnOK = $window.FindName('BtnOK') - $imgHeader.Source = $wpfImage + $imgHeader.Source = $wpfShield $txtContact.Text = "Questions? Contact IT at $ContactNumber" $txtBody.Inlines.Add("Your replay has been saved ($FileSizeMB MB):`n") @@ -124,18 +144,19 @@ if ($Type -eq 'BufferStarted') { } # Countdown timer — auto-dismisses after 30 seconds -$script:tick = 30 -$btnOK.Content = "OK (30)" +# Use a hashtable for shared state so the closure captures it by reference correctly +$state = @{ Tick = 30 } +$btnOK.Content = 'OK (30)' $timer = New-Object System.Windows.Threading.DispatcherTimer $timer.Interval = [TimeSpan]::FromSeconds(1) $timer.Add_Tick({ - $script:tick-- - $btnOK.Content = "OK ($script:tick)" - if ($script:tick -le 0) { + $state.Tick-- + $btnOK.Content = "OK ($($state.Tick))" + if ($state.Tick -le 0) { $timer.Stop() $window.Close() } -}) +}.GetNewClosure()) $timer.Start() -$btnOK.Add_Click({ $timer.Stop(); $window.Close() }) +$btnOK.Add_Click({ $timer.Stop(); $window.Close() }.GetNewClosure()) $window.ShowDialog() | Out-Null diff --git a/scripts/Start-OBSReplayBuffer.ps1 b/scripts/Start-OBSReplayBuffer.ps1 index 948aad9..f238a80 100644 --- a/scripts/Start-OBSReplayBuffer.ps1 +++ b/scripts/Start-OBSReplayBuffer.ps1 @@ -241,16 +241,7 @@ Write-Host "$(Get-Date -Format 'yyyy-MM-dd HH:mm:ss') [INFO] Replay Buffer confi # --- Notify user that the replay buffer is active --- $notifyScript = Join-Path $PSScriptRoot 'Show-Notification.ps1' $bufferMinutes = [math]::Round($config.BufferSeconds / 60) -Start-Process -FilePath 'powershell.exe' -ArgumentList @( - '-STA' - '-ExecutionPolicy', 'Bypass' - '-NonInteractive' - '-WindowStyle', 'Hidden' - '-File', $notifyScript - '-Type', 'BufferStarted' - '-ContactNumber', $config.ITContactNumber - '-BufferMinutes', $bufferMinutes -) -WindowStyle Hidden +Start-Process -FilePath 'powershell.exe' -ArgumentList "-STA -ExecutionPolicy Bypass -NonInteractive -WindowStyle Hidden -File `"$notifyScript`" -Type BufferStarted -ContactNumber `"$($config.ITContactNumber)`" -BufferMinutes $bufferMinutes" -WindowStyle Hidden Write-Host "$(Get-Date -Format 'yyyy-MM-dd HH:mm:ss') [INFO] User notification launched." # --- Launch tray icon as a separate background process ---