Skip to content
Merged
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
18 changes: 18 additions & 0 deletions src/Reactor/Core/V1Protocol/Handlers/GridViewHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,26 @@ public void Update(UpdateContext ctx, GridViewElement o, GridViewElement n, WinU
if (!ReferenceEquals(o.ItemContainerStyle, n.ItemContainerStyle) && n.ItemContainerStyle is not null)
gv.ItemContainerStyle = n.ItemContainerStyle;

// Issue #495 / #464 — rebuild ItemsSource on Items-array change so
// WinUI recycles + re-realizes containers (CCC re-fires
// reconciler.Mount with the new per-item element). The handler has
// `Children = null` and never reconciles realized child controls
// itself, so skipping the rebuild would silently freeze visible items
// when only their content changes
// (see Issue495_GridView_SameLengthContentChange_RefreshesContainers).
//
// WinUI drops SelectedIndex to -1 on ItemsSource reassignment when
// there's an active selection, firing SelectionChanged(-1). Arm
// BeginSuppress immediately before the swap so the trampoline's
// ShouldSuppress gate consumes that transient event. Only arm when
// there's a selection to clear — else the token strands and
// swallows the next real user input. Matches the ListView fix.
if (!ReferenceEquals(o.Items, n.Items))
{
if (gv.SelectedIndex >= 0)
ChangeEchoSuppressor.BeginSuppress(gv);
gv.ItemsSource = Enumerable.Range(0, n.Items.Length).ToList();
}

Reconciler.SetElementTag(gv, n);

Expand Down
57 changes: 53 additions & 4 deletions src/Reactor/Core/V1Protocol/Handlers/ListViewHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,15 @@ public WinUI.ListView Mount(MountContext ctx, ListViewElement lv)
listView.SelectionChanged += (s, _) =>
{
var l = (WinUI.ListView)s!;
// Issue #495 — consume any pending echo-suppress token before
// dispatching to the user callback (mirrors the GridView trampoline
// wired in issue #464). The programmatic SelectedIndex writes
// below in Mount / Update arm the suppressor with BeginSuppress so
// their synthesized SelectionChanged is dropped here instead of
// looping back through OnSelectedIndexChanged → setIndex →
// re-render → … which previously caused a 50+-render storm when
// the callback was bound to UseState.
if (ChangeEchoSuppressor.ShouldSuppress(l)) return;
if (Reconciler.GetElementTag(l) is not ListViewElement el) return;
el.OnSelectedIndexChanged?.Invoke(l.SelectedIndex);
if (el.OnSelectionChanged is { } h)
Expand All @@ -89,7 +98,19 @@ public WinUI.ListView Mount(MountContext ctx, ListViewElement lv)
// Set ItemsSource LAST — triggers container creation which needs the handler above
listView.ItemsSource = Enumerable.Range(0, lv.Items.Length).ToList();

if (lv.SelectedIndex >= 0) listView.SelectedIndex = lv.SelectedIndex;
// Issue #495 — wrap the initial SelectedIndex write so the deferred
// SelectionChanged ListView fires after container realization is
// suppressed instead of leaking into OnSelectedIndexChanged. Only arm
// on real drift to avoid stranding a token for a no-op write — see
// ChangeEchoSuppressor.BeginSuppress / ShouldSuppress in
// src/Reactor/Core/ChangeEchoSuppressor.cs: BeginSuppress always
// increments, ShouldSuppress only consumes on a real event, so an
// unconsumed token would swallow the next real user input.
if (lv.SelectedIndex >= 0 && listView.SelectedIndex != lv.SelectedIndex)
{
ChangeEchoSuppressor.BeginSuppress(listView);
listView.SelectedIndex = lv.SelectedIndex;
}
Reconciler.ApplySetters(lv.Setters, listView);
return listView;
}
Expand All @@ -104,10 +125,30 @@ public void Update(UpdateContext ctx, ListViewElement o, ListViewElement n, WinU
if (!ReferenceEquals(o.ItemContainerStyle, n.ItemContainerStyle) && n.ItemContainerStyle is not null)
lv.ItemContainerStyle = n.ItemContainerStyle;

// Update ItemsSource — ContainerContentChanging re-mounts visible items via Tag.
// Always set a new list when items differ (even same count) so WinUI re-realizes containers.
// Issue #495 — when the Items array changes (idiomatic Reactor authors
// allocate `new Element[] { ... }` literals on every render), rebuild
// ItemsSource so WinUI recycles + re-realizes its containers and
// ContainerContentChanging re-fires `reconciler.Mount` with the new
// per-item element. The handler has `Children = null` and never
// reconciles realized child controls itself, so skipping the rebuild
// would silently freeze visible items when only their content changes
// (see Issue495_ListView_SameLengthContentChange_RefreshesContainers).
//
// WinUI synchronously drops SelectedIndex to -1 on ItemsSource
// reassignment when there's an active selection, and fires
// SelectionChanged(-1). Arm BeginSuppress immediately before the
// swap so that transient event is consumed by the trampoline's
// ShouldSuppress gate instead of looping back through
// OnSelectedIndexChanged → setState → re-render → swap → … (the
// 50+-render storm reported in #495). Only arm when there's actually
// a selection to clear — otherwise the token strands and swallows
// the next real user input.
if (!ReferenceEquals(o.Items, n.Items))
{
if (lv.SelectedIndex >= 0)
ChangeEchoSuppressor.BeginSuppress(lv);
lv.ItemsSource = Enumerable.Range(0, n.Items.Length).ToList();
}

Reconciler.SetElementTag(lv, n);

Expand All @@ -123,7 +164,15 @@ public void Update(UpdateContext ctx, ListViewElement o, ListViewElement n, WinU
(Reconciler.GetElementTag(l) as ListViewElement)?.OnItemClick?.Invoke(idx);
};

if (n.SelectedIndex >= 0) lv.SelectedIndex = n.SelectedIndex;
// Issue #495 — wrap the SelectedIndex write so the SelectionChanged
// ListView fires after the property set doesn't echo back into
// OnSelectedIndexChanged. Only arm on real drift (see Mount comment
// above and the GridView analog wired for issue #464).
if (n.SelectedIndex >= 0 && lv.SelectedIndex != n.SelectedIndex)
{
ChangeEchoSuppressor.BeginSuppress(lv);
lv.SelectedIndex = n.SelectedIndex;
}
Reconciler.ApplySetters(n.Setters, lv);
}

Expand Down
62 changes: 56 additions & 6 deletions src/Reactor/Core/V1Protocol/TemplatedListLifecycle.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,13 @@ internal static WinUI.ListView MountListView(Reconciler reconciler, TemplatedLis
listView.SelectionChanged += (s, _) =>
{
var l = (WinUI.ListView)s!;
// Issue #495 — consume any pending echo-suppress token before
// dispatching to the user callback (mirrors the GridView/ListView
// hand-coded handlers wired in issues #464/#495). Mount/Update
// arm BeginSuppress around the SelectedIndex writes below so the
// synthesized SelectionChanged is dropped here instead of looping
// back through OnSelectedIndexChanged → setState → re-render.
if (ChangeEchoSuppressor.ShouldSuppress(l)) return;
if (Reconciler.GetElementTag(l) is not TemplatedListElementBase tel) return;
tel.InvokeSelectionChanged(l.SelectedIndex);
if (tel.HasMultiSelectionCallback)
Expand Down Expand Up @@ -101,7 +108,14 @@ internal static WinUI.ListView MountListView(Reconciler reconciler, TemplatedLis
listView.ItemsSource = listState.Source;

var selectedIndex = el.GetSelectedIndex();
if (selectedIndex >= 0) listView.SelectedIndex = selectedIndex;
// Issue #495 — wrap initial SelectedIndex write so the deferred
// SelectionChanged (after container realization) is suppressed.
// Drift check avoids stranding a token for a no-op write.
if (selectedIndex >= 0 && listView.SelectedIndex != selectedIndex)
{
ChangeEchoSuppressor.BeginSuppress(listView);
listView.SelectedIndex = selectedIndex;
}
el.ApplyControlSetters(listView);
return listView;
}
Expand All @@ -125,6 +139,8 @@ internal static WinUI.GridView MountGridView(Reconciler reconciler, TemplatedLis
gridView.SelectionChanged += (s, _) =>
{
var g = (WinUI.GridView)s!;
// Issue #495 — see ListView trampoline above.
if (ChangeEchoSuppressor.ShouldSuppress(g)) return;
if (Reconciler.GetElementTag(g) is not TemplatedListElementBase tel) return;
tel.InvokeSelectionChanged(g.SelectedIndex);
if (tel.HasMultiSelectionCallback)
Expand Down Expand Up @@ -156,7 +172,12 @@ internal static WinUI.GridView MountGridView(Reconciler reconciler, TemplatedLis
gridView.ItemsSource = gridState.Source;

var selectedIndex = el.GetSelectedIndex();
if (selectedIndex >= 0) gridView.SelectedIndex = selectedIndex;
// Issue #495 — see MountListView.
if (selectedIndex >= 0 && gridView.SelectedIndex != selectedIndex)
{
ChangeEchoSuppressor.BeginSuppress(gridView);
gridView.SelectedIndex = selectedIndex;
}
el.ApplyControlSetters(gridView);
return gridView;
}
Expand All @@ -179,11 +200,18 @@ internal static WinUI.FlipView MountFlipView(Reconciler reconciler, TemplatedLis
flipView.SelectionChanged += (s, _) =>
{
var f = (WinUI.FlipView)s!;
// Issue #495 — see ListView trampoline above.
if (ChangeEchoSuppressor.ShouldSuppress(f)) return;
(Reconciler.GetElementTag(f) as TemplatedListElementBase)?.InvokeSelectionChanged(f.SelectedIndex);
};

var selectedIndex = el.GetSelectedIndex();
if (selectedIndex >= 0) flipView.SelectedIndex = selectedIndex;
// Issue #495 — see MountListView.
if (selectedIndex >= 0 && flipView.SelectedIndex != selectedIndex)
{
ChangeEchoSuppressor.BeginSuppress(flipView);
flipView.SelectedIndex = selectedIndex;
}
el.ApplyControlSetters(flipView);
return flipView;
}
Expand All @@ -209,7 +237,14 @@ internal static WinUI.FlipView MountFlipView(Reconciler reconciler, TemplatedLis
reconciler.RefreshRealizedContainers(lv, n, requestRerender);

var selectedIndex = n.GetSelectedIndex();
if (selectedIndex >= 0) lv.SelectedIndex = selectedIndex;
// Issue #495 — wrap SelectedIndex write so the deferred
// SelectionChanged doesn't echo into the user callback. Drift check
// avoids stranding a token on a no-op write.
if (selectedIndex >= 0 && lv.SelectedIndex != selectedIndex)
{
ChangeEchoSuppressor.BeginSuppress(lv);
lv.SelectedIndex = selectedIndex;
}
n.ApplyControlSetters(lv);
return null;
}
Expand All @@ -226,7 +261,12 @@ internal static WinUI.FlipView MountFlipView(Reconciler reconciler, TemplatedLis
reconciler.RefreshRealizedContainers(gv, n, requestRerender);

var selectedIndex = n.GetSelectedIndex();
if (selectedIndex >= 0) gv.SelectedIndex = selectedIndex;
// Issue #495 — see UpdateListView.
if (selectedIndex >= 0 && gv.SelectedIndex != selectedIndex)
{
ChangeEchoSuppressor.BeginSuppress(gv);
gv.SelectedIndex = selectedIndex;
}
n.ApplyControlSetters(gv);
return null;
}
Expand Down Expand Up @@ -341,7 +381,17 @@ public IEnumerator<KeyOnly> GetEnumerator()
if (ctrl is not null) fv.Items.Add(ctrl);
}

fv.SelectedIndex = n.GetSelectedIndex();
// Issue #495 — drift-guarded, suppressed write. Previously
// unconditional, which would write -1 when n.GetSelectedIndex() == -1
// (the default for TemplatedFlipViewElement<T> is 0, so the prior
// unconditional write usually matched, but a `with { SelectedIndex = -1 }`
// would clear silently).
var fvSelectedIndex = n.GetSelectedIndex();
if (fv.SelectedIndex != fvSelectedIndex)
{
ChangeEchoSuppressor.BeginSuppress(fv);
fv.SelectedIndex = fvSelectedIndex;
}
Reconciler.SetElementTag(fv, n);
n.ApplyControlSetters(fv);
return null;
Expand Down
Loading
Loading