Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MenuItem (Radio) is not allocated in GroupManager properly on first appearance. #17589

Open
rabbitism opened this issue Nov 22, 2024 · 1 comment
Labels

Comments

@rabbitism
Copy link
Contributor

rabbitism commented Nov 22, 2024

Describe the bug

As described in title.

To Reproduce

Add below code in sandbox and click on third menu item. You will see both 1 and 3 are checked.

  <Button Content="Hello World">
    <Button.Flyout>
      <MenuFlyout>
        <MenuItem Header="A" IsChecked="True" ToggleType="Radio" GroupName="A"/>
        <MenuItem Header="B" ToggleType="Radio" GroupName="A"/>
        <MenuItem Header="C" ToggleType="Radio" GroupName="A"/>
      </MenuFlyout>
    </Button.Flyout>
  </Button>

Expected behavior

Only third is checked.

Avalonia version

latest master

OS

Windows

Additional context

Actually this is a test case covered in unit test

public void Radio_MenuItem_In_Same_Group_Is_Unchecked()
{
using var app = Application();
MenuItem menuItem1, menuItem2, menuItem3;
var menu = new Menu
{
Items =
{
(menuItem1 = new MenuItem
{
GroupName = "A", IsChecked = false, ToggleType = MenuItemToggleType.Radio
}),
(menuItem2 = new MenuItem
{
GroupName = "A", IsChecked = true, ToggleType = MenuItemToggleType.Radio
}),
(menuItem3 = new MenuItem
{
GroupName = "A", IsChecked = false, ToggleType = MenuItemToggleType.Radio
})
}
};
var window = new Window { Content = menu };
window.Show();
Assert.False(menuItem1.IsChecked);
Assert.True(menuItem2.IsChecked);
Assert.False(menuItem3.IsChecked);
menuItem3.IsChecked = true;
Assert.False(menuItem1.IsChecked);
Assert.False(menuItem2.IsChecked);
Assert.True(menuItem3.IsChecked);
}

But unfortunately, there is a difference between headless platform and actual running app.
In headless test app, after MenuFlyoutPresenter attached to logical tree, the three menu items are already LogicalChildren of the MenuFlyoutPresenter, but actually no, which leads to the failure of adding them to radio groups.

private static void AddMenuItemToRadioGroup(RadioButtonGroupManager manager, IMenuElement element)
{
// Instead add menu item to the group on attached/detached + ensure checked stated on attached.
if (element is IRadioButton button)
{
manager.Add(button);
}
foreach (var subItem in element.SubItems)
{
AddMenuItemToRadioGroup(manager, subItem);
}
}

@rabbitism rabbitism added the bug label Nov 22, 2024
@rabbitism
Copy link
Contributor Author

In fact, there is a very generic issue, developers do not have any clue when are ItemsControl's items materialized inside PanelRoot.

@rabbitism rabbitism changed the title MenuItem (Radio) is not allocated in GroupManager property on first appearance. MenuItem (Radio) is not allocated in GroupManager properly on first appearance. Nov 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant