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

Fix dropdown/menu components not providing items in correct order #6079

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ public void TestMenuBlocksInputUnderneathIt()
bool itemClicked = false;
bool actionReceived = false;

AddStep("set item action", () => Menus.GetSubMenu(0).Items[0].Items[0].Action.Value = () => itemClicked = true);
AddStep("set item action", () => Menus.GetSubMenu(0).Items.ElementAt(0).Items[0].Action.Value = () => itemClicked = true);
AddStep("add mouse handler", () => Add(new MouseHandlingLayer
{
Action = () => actionReceived = true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,7 @@ private void assertMenuOnScreen(bool expected) => AddAssert($"menu {(expected ?
return result == expected;
});

private void assertMenuItems(int expectedCount) => AddAssert($"menu contains {expectedCount} item(s)", () => contextMenuContainer.CurrentMenu.Items.Count == expectedCount);
private void assertMenuItems(int expectedCount) => AddAssert($"menu contains {expectedCount} item(s)", () => contextMenuContainer.CurrentMenu.Items.Count() == expectedCount);

private partial class BoxWithContextMenu : Box, IHasContextMenu
{
Expand Down
4 changes: 2 additions & 2 deletions osu.Framework.Tests/Visual/UserInterface/TestSceneDropdown.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public void TestSelectByUserInteraction()

AddStep("click item 2", () =>
{
InputManager.MoveMouseTo(testDropdown.Menu.Children[2]);
InputManager.MoveMouseTo(testDropdown.Menu.Children.ElementAt(2));
InputManager.Click(MouseButton.Left);
});

Expand Down Expand Up @@ -176,7 +176,7 @@ public void TestReplaceItems()

AddStep("click item 4", () =>
{
InputManager.MoveMouseTo(testDropdown.Menu.Children[4]);
InputManager.MoveMouseTo(testDropdown.Menu.Children.ElementAt(4));
InputManager.Click(MouseButton.Left);
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ public void TestHoverChange()
IReadOnlyList<MenuItem> currentItems = null;
AddStep("Click item", () => { ClickItem(0, 0); });

AddStep("Get items", () => { currentItems = Menus.GetSubMenu(1).Items; });
AddStep("Get items", () => { currentItems = Menus.GetSubMenu(1).Items.ToList(); });

AddAssert("Check open", () => Menus.GetSubMenu(1).State == MenuState.Open);
AddStep("Hover item", () => InputManager.MoveMouseTo(Menus.GetSubStructure(0).GetMenuItems()[1]));
Expand Down
24 changes: 17 additions & 7 deletions osu.Framework/Graphics/UserInterface/Dropdown.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,14 @@ public bool AllowNonContiguousMatching

protected IEnumerable<DropdownMenuItem<T>> MenuItems => itemMap.Values;

private readonly List<T> orderedItems = new List<T>();

/// <summary>
/// Enumerate all values in the dropdown.
/// </summary>
public IEnumerable<T> Items
{
get => MenuItems.Select(i => i.Value);
get => orderedItems;
set
{
if (boundItemSource != null)
Expand Down Expand Up @@ -127,7 +129,7 @@ private void addDropdownItem(T value, int? position = null)
if (itemMap.ContainsKey(value))
throw new ArgumentException($"The item {value} already exists in this {nameof(Dropdown<T>)}.");

var item = new DropdownMenuItem<T>(value, () =>
var menuItem = new DropdownMenuItem<T>(value, () =>
{
if (!Current.Disabled)
Current.Value = value;
Expand All @@ -137,14 +139,20 @@ private void addDropdownItem(T value, int? position = null)

// inheritors expect that `virtual GenerateItemText` is only called when this dropdown's BDL has run to completion.
if (LoadState >= LoadState.Ready)
item.Text.Value = GenerateItemText(value);
menuItem.Text.Value = GenerateItemText(value);

if (position != null)
Menu.Insert(position.Value, item);
{
Menu.Insert(position.Value, menuItem);
orderedItems.Insert(position.Value, value);
}
else
Menu.Add(item);
{
Menu.Add(menuItem);
orderedItems.Add(value);
}

itemMap[value] = item;
itemMap[value] = menuItem;
}

/// <summary>
Expand All @@ -169,6 +177,7 @@ private bool removeDropdownItem(T value)

Menu.Remove(item);
itemMap.Remove(value);
orderedItems.Remove(value);
return true;
}

Expand Down Expand Up @@ -405,7 +414,7 @@ private void ensureItemSelectionIsValid()
{
if (Current.Value == null || !itemMap.ContainsKey(Current.Value))
{
Current.Value = itemMap.Keys.FirstOrDefault();
Current.Value = orderedItems.FirstOrDefault();
return;
}

Expand Down Expand Up @@ -442,6 +451,7 @@ public void ClearItems()
private void clearItems()
{
itemMap.Clear();
orderedItems.Clear();
Menu.Clear();
}

Expand Down
6 changes: 3 additions & 3 deletions osu.Framework/Graphics/UserInterface/Menu.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public abstract partial class Menu : CompositeDrawable, IStateful<MenuState>
/// <summary>
/// Gets the item representations contained by this <see cref="Menu"/>.
/// </summary>
protected internal IReadOnlyList<DrawableMenuItem> Children => itemsFlow.Children;
protected internal IEnumerable<DrawableMenuItem> Children => itemsFlow.Children.OrderBy(c => itemsFlow.GetLayoutPosition(c));
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In case anyone asks, we cannot use FlowingChildren here as it filters the items by extra properties that are not wanted (life state).


protected readonly Direction Direction;

Expand Down Expand Up @@ -142,9 +142,9 @@ protected override void LoadComplete()
/// <summary>
/// Gets or sets the <see cref="MenuItem"/>s contained within this <see cref="Menu"/>.
/// </summary>
public IReadOnlyList<MenuItem> Items
public IEnumerable<MenuItem> Items
{
get => itemsFlow.Select(r => r.Item).ToList();
get => Children.Select(r => r.Item);
set
{
Clear();
Expand Down
Loading