Skip to content

Fix ItemsView multi-select checkmark flicker on window resize (#383) - #648

Merged
Alexandre Zollinger Chohfi (azchohfi) merged 6 commits into
mainfrom
azchohfi-fix-itemsview-checkmark-flicker
Jun 24, 2026
Merged

Alexandre Zollinger Chohfi (azchohfi) merged 6 commits into
mainfrom
azchohfi-fix-itemsview-checkmark-flicker

Conversation

@azchohfi

@azchohfi Alexandre Zollinger Chohfi (azchohfi) commented Jun 23, 2026 •

Copy link
Copy Markdown
Collaborator

Closes #383.

Symptom

In an ItemsView with SelectionMode=Multiple (e.g. samples/Reactor.TestApp → ItemsView tab), the per-item selection checkmark visibly fades out/in on every realized row while the user drag-resizes the window.

Root cause (proven, not surgically fixable in layout)

The fade itself is intrinsic WinUI behavior: on every ItemsRepeater recycle round-trip, ItemsView flips each realized ItemContainer's internal MultiSelectMode (Auto ↔ Auto|Multiple), and each flip runs the MultiSelectStates.Multiple opacity storyboard (PART_SelectionCheckbox.Opacity 0→1) with useTransitions: true.

What makes it visible in Reactor specifically is frequency: during a window resize Reactor's host re-runs a full layout pass on every tick, which drives the inner ItemsRepeater to recycle/realize its working set dozens of times per gesture — even though nothing the ItemsView owns actually moved.

I instrumented this exhaustively (headless zero-reconcile repro + per-panel measure/arrange counters + inner ScrollView/repeater geometry logging):

  • The churn reproduces with zero reconcile (pure native-style resize of the host content) → it is pure layout, not a reconcile artifact.
  • During the gesture the ItemsView branch (VStack → ItemsView → ScrollView → ItemsRepeater) is never re-measured or re-arranged by FlexPanel — only the outer FlexColumn re-runs — and the ScrollView geometry is 100% constant (viewport=500, extent=124, offset=0; repeater 700×124), yet the repeater fully re-realizes ~43 items/tick.

So the recycle is intrinsic ItemsRepeater viewport-manager behavior firing on every ancestor arrange pass, not something FlexPanel does to the ItemsView. I tried two layout-side suppressions (skip the grow-child re-measure; measure the main axis at ∞) — both stopped the churn but broke the ItemsView width (700 → 2696), because the grow child's finite re-measure is load-bearing for cross-axis width propagation. Eliminating the spurious recycle is therefore not possible without regressing layout.

Known upstream behavior

This recycle storm is upstream WinUI behavior, not a Reactor layout bug — it matches microsoft/microsoft-ui-xaml#11122 (ItemsRepeater re-realizes its working set on every ancestor arrange pass even when its own viewport/extent is unchanged). This PR is a cosmetic mitigation of the visible fade, not a fix for the recycle itself; if/when the upstream issue is resolved the guard becomes a no-op and can be removed.

Fix (the brief's endorsed mitigation: stop the checkmark re-animating)

ItemContainer.MultiSelectMode and ItemContainerMultiSelectMode are [MUX_INTERNAL], so the property is unreachable from Reactor. Instead, the new ItemContainerSelectionFlickerGuard reaches into the public MultiSelectStates VisualStateGroup once the container's template is applied, finds the Multiple state's opacity storyboard, and collapses its keyframe times to zero. WinUI keeps calling GoToState("Multiple", useTransitions: true) exactly as before, but a zero-duration storyboard snaps the checkmark to full opacity in the same UI tick instead of fading it — so the recycle storm is no longer visible. Selection behavior, the recycle, and final checkmark visibility are unchanged.

This is a per-instance, one-time mutation of the container's own template storyboard. It never calls GoToState (which is not re-entrancy-safe from inside WinUI's own state transitions — an earlier CurrentStateChanged + GoToState(useTransitions:false) approach deadlocked realization), and it touches no shared resource.

Pool rent/return safety

The guard is armed idempotently from ElementFactory<T>.GetElement when the realized control is an ItemContainer. There is no persistent event subscription to leak across pool reuse — the only handler is a transient, self-removing Loaded hook used when a freshly-mounted container hasn't applied its template yet. State is tracked in a ConditionalWeakTable<ItemContainer, Holder> (the same once-per-control wiring pattern the pooling layer already uses for poolable controls): a re-rented container short-circuits on holder.Neutralized so it is never double-processed, and the collapsed storyboard persists across rent/return because it lives on the container's own template instance.

Before / after

  • Recycle/realize cycle count per resize gesture: unchanged — it is intrinsic WinUI behavior and deliberately not touched. Measured churn under a standardized resize gesture (via the [EF] ElementFactory diagnostic): ~3870 Get / 3870 Recycle over the gesture, ~43 items re-realized/tick across ~75–90 ticks (zero-reconcile repro: 3225–3913 depending on scenario). A plain Border ancestor instead of FlexPanel = 0 — confirming it is the repeater's own viewport manager, not Reactor layout.
  • Visible checkmark fade: eliminated. The regression selftest drives the exact Single → Multiple animated transition WinUI fires on a recycle and asserts PART_SelectionCheckbox.Opacity == 1.000 immediately (snapped). The same test proves the un-guarded path genuinely fades (opacity 0.000 mid-transition with a restored non-zero keyframe) — so the snap assertion is load-bearing, not green-by-construction.

Tests

  • New selftest ItemsView_MultiSelect_CheckmarkDoesNotFlicker, now load-bearing on a real realized ItemContainer from a live SelectionMode=Multiple ItemsView, with three checks:
    1. Production wiring — without calling the guard directly, the GetElement-armed guard has already collapsed the storyboard → transition snaps (opacity=1.000).
    2. Un-guarded fade proof — restore a non-zero keyframe (reproduce the un-guarded WinUI default) → identical transition fades (opacity=0.000). A regressed/absent guard would land here.
    3. Re-collapse proof — re-zero the keyframe (the guard's exact operation) → snaps again (opacity=1.000).
  • Full ItemsView selftest suite green.
  • dotnet test tests/Reactor.Tests: 9517 passed, 0 failed (64 skipped).
  • Full selftest suite: only unrelated pre-existing flake WindowLevel_RuntimeFlip_Topmost (passes in isolation; cross-fixture window-topmost interference, untouched by this change).
  • Core lib builds AOT-clean (no new trim/AOT warnings).

Scope / overlap note

Surgical: 1 new core file + a 5-line GetElement wiring + a selftest fixture & registration. No FlexPanel/layout changes. The only shared-file touch is src/Reactor/Core/ElementFactory.cs — additive, near the end of GetElement. A sibling effort (#326 / PR #635, RefreshRealizedItems / TryAdoptRealizedReplacement) also touches ElementFactory<T>; this change is additive and localized, but flagging for merge ordering in case of a trivial textual conflict in that file.

Co-authored-by: Copilot [email protected]

In a SelectionMode=Multiple ItemsView the per-item selection checkmark
visibly faded out/in on every realized row during a window drag-resize.

Root cause: WinUI's ItemsView flips each realized ItemContainer's internal
MultiSelectMode (Auto <-> Auto|Multiple) on every recycle round-trip, each
flip running the MultiSelectStates.Multiple opacity storyboard with
useTransitions:true. Reactor's host re-runs a full layout pass on every
resize tick, which drives the inner ItemsRepeater to recycle/realize its
working set dozens of times per gesture even though nothing the ItemsView
owns actually moved (the ItemsView/ScrollView/repeater geometry is constant
across the gesture). The recycle storm is intrinsic WinUI ItemsRepeater
viewport-manager behavior and cannot be suppressed in FlexPanel without
regressing ItemsView width; only the useTransitions:true fade is visible.

Mitigation: ItemContainerSelectionFlickerGuard collapses the Multiple
state's opacity storyboard to zero duration on each realized container, so
WinUI's animated GoToState snaps the checkmark to full opacity in the same
UI tick instead of fading it. MultiSelectMode and its enum are [MUX_INTERNAL]
so the guard works purely through the public MultiSelectStates
VisualStateGroup, mutating only the container's own per-instance template
storyboard (no GoToState re-entrancy, no shared resource).

Armed idempotently from ElementFactory.GetElement when a realized control is
an ItemContainer. Adds a selftest regression fixture that drives the exact
Single->Multiple animated transition and asserts the checkmark opacity is
snapped to 1.0 rather than left mid-fade.

Co-authored-by: Copilot <[email protected]>
Anchor the regression fixture on a real realized ItemContainer from a live SelectionMode=Multiple ItemsView and add an un-guarded fade proof: restore a non-zero keyframe on the Multiple storyboard and assert the identical Single->Multiple transition fades (opacity 0.000), then re-collapse and assert it snaps (1.000). This guarantees the snap assertion is not green-by-construction and would catch a regressed/absent guard.

Co-authored-by: Copilot <[email protected]>
Per PR-review triage (M3, must-fix): the regression fixture now covers BOTH the guard mechanism AND its production activation, so a silent break in either is caught.

Part A (mechanism): realize an ItemContainer via a raw WinUI ItemsView (never through Reactor's ElementFactory, so un-armed), prove the Single->Multiple transition fades (opacity 0.000), then call ItemContainerSelectionFlickerGuard.Ensure directly and prove it snaps (1.000).

Part B (wiring): realize an ItemContainer via a Reactor ItemsView and assert the GetElement-armed guard already collapsed the storyboard so the transition snaps (1.000) with no direct Ensure call. If the GetElement->Ensure wiring breaks, Part B fails while Part A still passes.

Also: comment at the GetElement arming site explaining the ItemContainer scope is intentional (LazyVStack/HStack use ItemsRepeater not ItemContainer; the Multiple storyboard only runs for multi-select containers), and an XML-doc note on Holder.Neutralized re re-templated storyboards. No production behavior change.

Co-authored-by: Copilot <[email protected]>
…L1/L3/L4)

Final PR-review triage round for #383.

L4 (correctness): TryNeutralize now sets Neutralized=true before the group-absent return, symmetric with the no-Multiple-state branch, so an unusual ItemContainer template without a MultiSelectStates group no longer retains the Loaded handler or re-walks the visual tree on every recycle.

L1 (test): Part B now polls (Harness.WaitFor) until the guard has actually collapsed the Multiple storyboard before asserting, instead of relying on a fixed two-render-pass budget — removes a potential CI flake on slow runners.

L3 (test): new Part C forces a real recycle round-trip (toggle the ItemsView between two disjoint keyed item sets via a state button, so the inner ItemsRepeater clears + re-prepares its containers — the production OnItemsRepeaterElementClearing/Prepared trigger) and asserts a realized container still snaps afterward. Closes the gap between 'guard collapses the storyboard' and 'guard survives the recycle that causes the flicker'.

L2/L5 skipped per triage. No production behavior change beyond the L4 edge-case give-up.

Co-authored-by: Copilot <[email protected]>
…ryboardCollapsed

Refactor the nested foreach in the IsMultipleStoryboardCollapsed test helper to filter explicitly with .Where(...) / .OfType<>() instead of in-loop guard clauses, per the github-code-quality review. No behavior change — flicker fixture still 12/12.

Co-authored-by: Copilot <[email protected]>
…detach, docs

Addresses the meaningful findings from the re-run pr-review of PR #648 (#383):

M1 (test-coverage): Part C of ItemsView_MultiSelect_CheckmarkDoesNotFlicker no longer relies on a fixed 2-render-pass budget that could pass vacuously. It now polls (WaitFor) and asserts the realized dataset genuinely swapped each way: an AltCatalog-only item (Xigua) becomes realized and the bound ItemsView.ItemsSource count changes (5<->3). Reading the source count is pool-proof - WinUI keeps cleared containers parented in its recycle pool with stale text, so a tree-wide FindText would still match the old set.

L3 (correctness): ItemContainerSelectionFlickerGuard.Ensure now detaches a previously attached Loaded handler whenever TryNeutralize succeeds (synchronously or via the deferred hook), so the one-time handler cannot linger on the container until the next Loaded fires. Adds a DetachLoaded helper and a Holder.OnLoaded field.

L2 (alternative-solution): documented why the guard deliberately uses a private ConditionalWeakTable instead of ReactorAttached.StateProperty (must be safe on any ItemContainer, including raw-WinUI ones with no ReactorState).

L1 (docs): added a CHANGELOG entry for the #383 fix.

Co-authored-by: Copilot <[email protected]>
@azchohfi
Alexandre Zollinger Chohfi (azchohfi) merged commit 6aeb00a into main Jun 24, 2026
28 of 29 checks passed
@azchohfi
Alexandre Zollinger Chohfi (azchohfi) deleted the azchohfi-fix-itemsview-checkmark-flicker branch June 24, 2026 00:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ItemsView multi-select checkmark flickers during window resize

1 participant