From ca84f4dfead09ec23ca5ee663af2cc3bb49cc3b4 Mon Sep 17 00:00:00 2001 From: rbalsleyMSFT <53497092+rbalsleyMSFT@users.noreply.github.com> Date: Mon, 14 Jul 2025 17:56:51 -0700 Subject: [PATCH] Implement conditional autoscroll in log monitor Improves the user experience in the monitor tab by making the log output autoscroll conditional. Autoscrolling is now disabled when the user selects a log entry other than the last one, allowing them to inspect previous output without interruption. Selecting the last item in the list re-enables autoscrolling. --- FFUDevelopment/BuildFFUVM_UI.ps1 | 17 +++++++++++-- .../FFUUI.Core/FFUUI.Core.Handlers.psm1 | 25 +++++++++++++++++++ 2 files changed, 40 insertions(+), 2 deletions(-) diff --git a/FFUDevelopment/BuildFFUVM_UI.ps1 b/FFUDevelopment/BuildFFUVM_UI.ps1 index 08f70ae..dfa01b4 100644 --- a/FFUDevelopment/BuildFFUVM_UI.ps1 +++ b/FFUDevelopment/BuildFFUVM_UI.ps1 @@ -120,9 +120,10 @@ $script:uiState.Controls.btnRun.Add_Click({ # Switch to Monitor Tab $script:uiState.Controls.MainTabControl.SelectedItem = $script:uiState.Controls.MonitorTab - # Clear previous log data + # Clear previous log data and reset autoscroll if ($null -ne $script:uiState.Data.logData) { $script:uiState.Data.logData.Clear() + $script:uiState.Flags.autoScrollLog = $true } $progressBar = $script:uiState.Controls.pbOverallProgress @@ -201,7 +202,10 @@ $script:uiState.Controls.btnRun.Add_Click({ while ($null -ne ($line = $script:uiState.Data.logStreamReader.ReadLine())) { # Add the full line to the log view first to maintain consistency $script:uiState.Data.logData.Add($line) - $script:uiState.Controls.lstLogOutput.ScrollIntoView($line) + if ($script:uiState.Flags.autoScrollLog) { + $script:uiState.Controls.lstLogOutput.ScrollIntoView($line) + $script:uiState.Controls.lstLogOutput.SelectedIndex = $script:uiState.Controls.lstLogOutput.Items.Count - 1 + } # Now, check if it's a progress line and update the UI accordingly if ($line -match '\[PROGRESS\] (\d{1,3}) \| (.*)') { @@ -234,9 +238,11 @@ $script:uiState.Controls.btnRun.Add_Click({ # Final read of the log stream if ($null -ne $script:uiState.Data.logStreamReader) { + $lastLine = $null while ($null -ne ($line = $script:uiState.Data.logStreamReader.ReadLine())) { # Add the full line to the log view first $script:uiState.Data.logData.Add($line) + $lastLine = $line # Now, check if it's a progress line and update the UI accordingly if ($line -match '\[PROGRESS\] (\d{1,3}) \| (.*)') { @@ -247,6 +253,13 @@ $script:uiState.Controls.btnRun.Add_Click({ $script:uiState.Controls.txtStatus.Text = $message } } + + # After the final read, scroll to the last line if autoscroll is enabled + if ($script:uiState.Flags.autoScrollLog -and $null -ne $lastLine) { + $script:uiState.Controls.lstLogOutput.ScrollIntoView($lastLine) + $script:uiState.Controls.lstLogOutput.SelectedIndex = $script:uiState.Controls.lstLogOutput.Items.Count - 1 + } + $script:uiState.Data.logStreamReader.Close() $script:uiState.Data.logStreamReader.Dispose() $script:uiState.Data.logStreamReader = $null diff --git a/FFUDevelopment/FFUUI.Core/FFUUI.Core.Handlers.psm1 b/FFUDevelopment/FFUUI.Core/FFUUI.Core.Handlers.psm1 index 86d9d6e..718aefd 100644 --- a/FFUDevelopment/FFUUI.Core/FFUUI.Core.Handlers.psm1 +++ b/FFUDevelopment/FFUUI.Core/FFUUI.Core.Handlers.psm1 @@ -778,5 +778,30 @@ function Register-EventHandlers { $keyEventArgs.Handled = $true } }) + + $State.Controls.lstLogOutput.Add_SelectionChanged({ + param($eventSource, $selectionChangedEventArgs) + $listBox = $eventSource + $window = [System.Windows.Window]::GetWindow($listBox) + if ($null -eq $window) { return } + $localState = $window.Tag + + # If nothing is selected or the list is empty, do nothing. + if ($listBox.SelectedIndex -eq -1 -or $listBox.Items.Count -eq 0) { + return + } + + # Check if the last item is selected + $isLastItemSelected = ($listBox.SelectedIndex -eq ($listBox.Items.Count - 1)) + + # Update the flag + $localState.Flags.autoScrollLog = $isLastItemSelected + if ($isLastItemSelected) { + WriteLog "Monitor tab autoscroll enabled (last item selected)." + } + else { + WriteLog "Monitor tab autoscroll disabled (user selected item #$($listBox.SelectedIndex))." + } + }) } Export-ModuleMember -Function * \ No newline at end of file