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

Add callback for when a submenu is opened #6439

Merged
merged 1 commit into from
Dec 4, 2024
Merged
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
12 changes: 12 additions & 0 deletions osu.Framework/Graphics/UserInterface/Menu.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@ public abstract partial class Menu : CompositeDrawable, IStateful<MenuState>
[CanBeNull]
public event Action<MenuState> StateChanged;

/// <summary>
/// Invoked when a Sub-<see cref="Menu"/> is opened.
/// </summary>
[CanBeNull]
public event Action<Menu> OnSubmenuOpen;

/// <summary>
/// Gets or sets the delay before opening sub-<see cref="Menu"/>s when menu items are hovered.
/// </summary>
Expand Down Expand Up @@ -563,6 +569,8 @@ private void openSubmenuFor(DrawableMenuItem item)
submenu.StateChanged += submenuStateChanged;
}

bool submenuChanged = submenu.triggeringItem != item;

submenu.triggeringItem = item;
submenu.positionLayout.Invalidate();

Expand All @@ -574,6 +582,10 @@ private void openSubmenuFor(DrawableMenuItem item)
Schedule(delegate { GetContainingFocusManager().AsNonNull().ChangeFocus(submenu); });
else
submenu.Open();

// Check if submenu has changed before firing, to prevent extraneous callbacks (e.g. re-hovering the triggeringItem of an already open submenu)
if (submenuChanged)
OnSubmenuOpen?.Invoke(submenu);
Comment on lines +586 to +588
Copy link
Member

Choose a reason for hiding this comment

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

Sounds like you want to be triggering this event inside the conditional above in the else branch where the submenu was closed and becomes open?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The problem is that submenu gets reused (assumedly as an optimisation?), so in some cases the state is already set to 'open' and the item list just gets updated (e.g. switching between menus in EditorMenuBar)

}
else
submenu.Close();
Expand Down
Loading