From 150c4c01d0652bda4ee26f37bcbbda43bbabc50b Mon Sep 17 00:00:00 2001 From: Steven Kirk Date: Fri, 24 Feb 2023 11:55:52 +0100 Subject: [PATCH] Ensure layout a little earlier. Fixes all integration tests with overlay popups. --- src/Avalonia.Controls/MenuItem.cs | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/src/Avalonia.Controls/MenuItem.cs b/src/Avalonia.Controls/MenuItem.cs index 45fc2ed859..1670e496b4 100644 --- a/src/Avalonia.Controls/MenuItem.cs +++ b/src/Avalonia.Controls/MenuItem.cs @@ -310,12 +310,7 @@ namespace Avalonia.Controls protected override bool IsEnabledCore => base.IsEnabledCore && _commandCanExecute; /// - bool IMenuElement.MoveSelection(NavigationDirection direction, bool wrap) - { - if (Presenter?.Panel is null) - (VisualRoot as ILayoutRoot)?.LayoutManager.ExecuteLayoutPass(); - return MoveSelection(direction, wrap); - } + bool IMenuElement.MoveSelection(NavigationDirection direction, bool wrap) => MoveSelection(direction, wrap); /// IMenuItem? IMenuElement.SelectedItem @@ -686,6 +681,12 @@ namespace Avalonia.Controls /// The event args. private void PopupOpened(object? sender, EventArgs e) { + // If we're using overlay popups, there's a chance we need to do a layout pass before + // the child items are added to the visual tree. If we don't do this here, then + // selection breaks. + if (Presenter?.IsAttachedToVisualTree == false) + UpdateLayout(); + var selected = SelectedIndex; if (selected != -1) @@ -705,6 +706,11 @@ namespace Avalonia.Controls SelectedItem = null; } + private void UpdateLayout() + { + (VisualRoot as ILayoutRoot)?.LayoutManager.ExecuteLayoutPass(); + } + void ICommandSource.CanExecuteChanged(object sender, EventArgs e) => this.CanExecuteChanged(sender, e); void IClickableControl.RaiseClick()