Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 53 additions & 0 deletions RcbExportFilterWindow.VirtualizedSelectionAuthority.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
using System.Runtime.CompilerServices;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using ArIED61850Tester.Models;

namespace ArIED61850Tester;

/// <summary>
/// Keeps the P1 multi-RCB selection model authoritative when the legacy DataGrid virtualizes
/// and recycles checkbox containers. A recycled CheckBox re-applies IsChecked from the bound
/// RcbExportRow.IsSelected value, which raises Checked/Unchecked even though the operator did
/// not change selection. The original single-select instance handlers interpret that visual
/// rehydration as a user action and call SelectOnly, silently clearing the other selected RCBs.
///
/// P1 pointer/keyboard handling already owns real operator selection changes, so these routed
/// state-change events are presentation-only for this grid and must not reach the stale
/// single-select handlers.
/// </summary>
public partial class RcbExportFilterWindow
{
[ModuleInitializer]
internal static void RegisterLegacyRcbVirtualizedSelectionAuthority()
{
EventManager.RegisterClassHandler(
typeof(CheckBox),
ToggleButton.CheckedEvent,
new RoutedEventHandler(SuppressLegacyRcbSingleSelectStateHandler),
handledEventsToo: true);
EventManager.RegisterClassHandler(
typeof(CheckBox),
ToggleButton.UncheckedEvent,
new RoutedEventHandler(SuppressLegacyRcbSingleSelectStateHandler),
handledEventsToo: true);
}

private static void SuppressLegacyRcbSingleSelectStateHandler(object sender, RoutedEventArgs e)
{
if (sender is not CheckBox checkBox ||
checkBox.DataContext is not RcbExportRow ||
Window.GetWindow(checkBox) is not RcbExportFilterWindow window ||
FindP1LegacyRcbAncestor<DataGrid>(checkBox) is not { } grid ||
!ReferenceEquals(grid, window.RcbGrid))
{
return;
}

// Do not allow the original XAML Checked/Unchecked instance handlers to translate
// DataGrid container materialization/recycling into a single-selection mutation.
// RcbExportRow.IsSelected remains the durable selection authority.
e.Handled = true;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Refresh state for checkbox toggles that bypass preview input

When a checkbox is toggled through UI Automation (for example, a screen reader invoking TogglePattern) rather than mouse or Space-key input, the two-way binding still changes RcbExportRow.IsSelected, but this handler now suppresses both legacy callbacks and the P1 preview handlers never run. Consequently SelectedRow, the footer summaries, and ExportButton.IsEnabled remain stale—for example, unticking the only selected row leaves Export enabled and still reports that row as retained. Preserve a path that synchronizes the multi-selection UI for genuine toggles that do not pass through the preview handlers.

Useful? React with 👍 / 👎.

}
}
81 changes: 81 additions & 0 deletions tests/ARSAS.Tests/RcbSelectionVirtualizationRegressionTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
using ArIED61850Tester;
using ArIED61850Tester.Models;

namespace ARSAS.Tests;

public sealed class RcbSelectionVirtualizationRegressionTests
{
[Fact]
public void LegacyRcbGrid_RecycledCheckboxEventsCannotReachSingleSelectHandlers()
{
var xaml = Read("RcbExportFilterWindow.xaml");
var authority = Read("RcbExportFilterWindow.VirtualizedSelectionAuthority.cs");

Assert.Contains(
"IsChecked=\"{Binding IsSelected, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}\"",
xaml,
StringComparison.Ordinal);

// These legacy handlers still exist for the original single-RCB window contract. The
// P1 multi-select authority must stop virtualization-generated Checked/Unchecked events
// before those instance handlers can translate visual rehydration into SelectOnly().
Assert.Contains("Checked=\"RcbCheckBox_Checked\"", xaml, StringComparison.Ordinal);
Assert.Contains("Unchecked=\"RcbCheckBox_Unchecked\"", xaml, StringComparison.Ordinal);
Assert.Contains("ToggleButton.CheckedEvent", authority, StringComparison.Ordinal);
Assert.Contains("ToggleButton.UncheckedEvent", authority, StringComparison.Ordinal);
Assert.Contains("ReferenceEquals(grid, window.RcbGrid)", authority, StringComparison.Ordinal);
Assert.Contains("e.Handled = true", authority, StringComparison.Ordinal);
}

[Fact]
public void MultiRcbSelectionAuthority_KeepsIndependentRowsSelected()
{
var rows = new List<RcbExportRow>
{
NewRow("Buffer"),
NewRow("Unbuffer"),
NewRow("A_URCB"),
NewRow("A_URCB_1"),
NewRow("A_URCB_10")
};
var anchorIndex = -1;
var anchorValue = false;

for (var index = 0; index < 4; index++)
{
MainWindow.ApplyRcbSelectionForTest(
rows,
ref anchorIndex,
ref anchorValue,
index,
extendRange: false);
}

Assert.Equal(4, rows.Count(row => row.IsSelected));
Assert.All(rows.Take(4), row => Assert.True(row.IsSelected));
Assert.False(rows[4].IsSelected);
}

private static RcbExportRow NewRow(string name)
=> new()
{
Name = name,
Reference = $"IED/LLN0.{name}",
SourceSelectionKey = name,
ExportName = name
};

private static string Read(string relativePath)
{
DirectoryInfo? directory = new(AppContext.BaseDirectory);
while (directory != null)
{
var candidate = Path.Combine(directory.FullName, relativePath);
if (File.Exists(candidate))
return File.ReadAllText(candidate);
directory = directory.Parent;
}

throw new FileNotFoundException(relativePath);
}
}
Loading