Repository navigation
Description
Current behavior π
Reopening a CommandBar from its own Closed event handler β a pattern WinUI explicitly documents as supported β makes the CommandBar and its overflow popup oscillate open/closed in a tight loop instead of settling open. Closed fires thousands of times per second; a runtime test I wrote counted 2622 Closed invocations in 1.5 seconds for a single close-then-reopen. The bar does eventually read IsOpen == true, but only after burning CPU in a loop the whole time.
Root cause: CommandBar subscribes to its own overflow Popup's Closed event and unconditionally forces IsOpen = false whenever that popup closes β including when the popup closed because the CommandBar itself just closed normally. Since Popup.Closed is always raised through a deferred dispatch (not synchronously), this queued IsOpen = false lands after an app's Closed handler has already reopened the bar, undoing the reopen and immediately re-triggering the whole cycle.
src/Uno.UI/UI/Xaml/Controls/CommandBar/CommandBar.Partial.cs:
// lines 277-279 (Uno Only)
m_tpOverflowPopup.Closed += OnOverflowPopupClosed;
m_overflowPopupClosedEventHandler.Disposable = Disposable.Create(() => m_tpOverflowPopup.Closed -= OnOverflowPopupClosed);
// lines 339-342 (Uno Only)
private void OnOverflowPopupClosed(object? sender, object e)
{
NativeDispatcher.Main.Enqueue(() => IsOpen = false, NativeDispatcherPriority.Idle);
}CommandBar.OnClosed (lines 1086-1103) already closes the popup itself as part of every normal close:
protected override void OnClosed(object e)
{
SetCompactMode(true);
if (m_tpOverflowPopup is { })
{
m_tpOverflowPopup.IsOpen = false;
}
// We need to call this last, rather than first (the usual pattern),
// because this raises an event that apps can use to set IsOpen = true.
// ...
base.OnClosed(e);
}So every normal close of the CommandBar also closes the popup, which raises the popup's Closed event, which queues a deferred, unconditional IsOpen = false on the CommandBar β even though the bar may have just been reopened by the app's own Closed handler (the exact scenario the comment above says is supported). That queued IsOpen = false fires, closes the bar again (closing the popup again, queuing another deferred IsOpen = false), the app's handler reopens it again, and so on.
Expected behavior π―
WinUI has no such mapping between the overflow popup's close and CommandBar.IsOpen. CommandBar::OnApplyTemplate only sets IsSubMenu on the popup (CommandBar_Partial.cpp#L348-L355); nothing hooks the popup's Closed event to force CommandBar.IsOpen. CommandBar::OnClosedImpl only closes the popup and then raises the Closed event β it explicitly documents that apps can set IsOpen = true from that event (CommandBar_Partial.cpp#L1433-L1453):
_Check_return_ HRESULT
CommandBar::OnClosedImpl(_In_ IInspectable* pArgs)
{
IFC_RETURN(SetCompactMode(true));
if (m_tpOverflowPopup)
{
IFC_RETURN(m_tpOverflowPopup->put_IsOpen(FALSE));
}
// We need to call this last, rather than first (the usual pattern),
// because this raises an event that apps can use to set IsOpen = true.
// If that happened before the above, then we would effectively
// see a sequence of Open -> Open -> Close, instead of
// Open -> Close -> Open, which gets us into a state where
// CompactMode is still set to true even when the CommandBar is open,
// hiding AppBarButton labels and the overflow popup.
IFC_RETURN(__super::OnClosedImpl(pArgs));
return S_OK;
}The CommandBar should settle open immediately, without any looping, when an app reopens it from Closed.
How to reproduce it (as minimally and precisely as possible) π¬
- Create a
CommandBarand subscribe to itsClosedevent; in the handler, setcommandBar.IsOpen = true. - Open the
CommandBar(IsOpen = true) and wait for it to settle. - Close it (
IsOpen = false). - Observe that
Closedkeeps firing repeatedly (thousands of times per second) for well over a second before the bar settles open, instead of reopening once, immediately.
Minimal repro:
var commandBar = new CommandBar();
commandBar.PrimaryCommands.Add(new AppBarButton { Icon = new SymbolIcon(Symbol.Add) });
var closedCount = 0;
commandBar.Closed += (s, e) =>
{
closedCount++;
((CommandBar)s).IsOpen = true;
};
// Host commandBar in a page, e.g. as Page.TopAppBar or Page.BottomAppBar.
commandBar.IsOpen = true;
// ... wait for it to open ...
commandBar.IsOpen = false;
// closedCount climbs into the thousands within ~1.5s instead of settling at 1.I confirmed this with a runtime test built on the existing CanReopenInClosedHandler test in CommandBarIntegrationTests, extended to poll for ~1.5 seconds (15 iterations of WaitForIdle + a 100ms delay) after closing, and count Closed invocations β it logged 2622 invocations for that single window, with cmdBar.IsOpen finally reading true at the end.
Workaround π οΈ
None that avoids the CPU churn. The bar does eventually settle in the IsOpen == true state, so the visible symptom may only be a stutter/CPU spike rather than a permanently broken UI, but any app using the documented reopen-from-Closed pattern pays this cost every time.
Renderer π¨
- Skia
Affected platforms π±π»π₯οΈ
All platforms π (the code lives in the shared, non-suffixed CommandBar.Partial.cs; measured on Desktop (Windows) Skia)
Uno.Sdk version (and other relevant versions) π¦
Uno master @ commit 3950a923f5f457ec5b86cf52d654dad20ded75c6 (7.0-dev), built with .NET SDK 11.0.100-rc.1.26425.128, target net11.0-desktop.
IDE version π§βπ»
N/A (built and run from the CLI)
Anything else we need to know? π¬
Root cause and suggested fix: delete the m_tpOverflowPopup.Closed += OnOverflowPopupClosed subscription and the OnOverflowPopupClosed handler entirely (src/Uno.UI/UI/Xaml/Controls/CommandBar/CommandBar.Partial.cs lines 277-279 and 339-342) β this moves the port closer to the WinUI C++ source, which has no such mapping. At minimum, the handler should be guarded so it only reacts to a popup close the CommandBar did not itself just perform.
The handler's own comment claims it exists because "AppBar/CommandBar will not light-dismiss close unless we listen to the Closed event for the overflow Popup." That no longer appears to be necessary: PopupPanel.OnPointerPressed (src/Uno.UI/UI/Xaml/Controls/Popup/PopupPanel.cs, around lines 212-223) already calls CommandBar.TryDismissInlineAppBarInternal() directly for a press outside the popup. Removing the handler should be verified against the light-dismiss and focus-navigation tests in CommandBarIntegrationTests before merging, since I only confirmed the oscillation itself, not the full blast radius of removing the handler.
Activity