From f703c5f03879f68a8996e7e30959f4cf0fa4f97f Mon Sep 17 00:00:00 2001 From: Dean Herbert Date: Thu, 6 Jan 2022 23:38:54 +0900 Subject: [PATCH] Add comment and reduce how often `ChildrenOfType` is invoked in `ExpandingButtonContainer` --- osu.Game/Overlays/ExpandingButtonContainer.cs | 26 ++++++++++--------- 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/osu.Game/Overlays/ExpandingButtonContainer.cs b/osu.Game/Overlays/ExpandingButtonContainer.cs index 5964f7f263..4eb8c47a1f 100644 --- a/osu.Game/Overlays/ExpandingButtonContainer.cs +++ b/osu.Game/Overlays/ExpandingButtonContainer.cs @@ -65,7 +65,7 @@ namespace osu.Game.Overlays protected override void OnHoverLost(HoverLostEvent e) { expandEvent?.Cancel(); - lastHoveredButton = null; + hoveredButton = null; State = ExpandedState.Contracted; base.OnHoverLost(e); @@ -112,22 +112,24 @@ namespace osu.Game.Overlays } } - private Drawable lastHoveredButton; - - private Drawable hoveredButton => FillFlow.ChildrenOfType().FirstOrDefault(c => c.IsHovered); + private Drawable hoveredButton; private void queueExpandIfHovering() { - // only expand when we hover a different button. - if (lastHoveredButton == hoveredButton) return; + // if the same button is hovered, let the scheduled expand play out.. + if (hoveredButton?.IsHovered == true) + return; - if (State != ExpandedState.Expanded) - { - expandEvent?.Cancel(); + // ..otherwise check whether a new button is hovered, and if so, queue a new hover operation. + + // usually we wouldn't use ChildrenOfType in implementations, but this is the simplest way + // to handle cases like the editor where the buttons may be nested within a child hierarchy. + hoveredButton = FillFlow.ChildrenOfType().FirstOrDefault(c => c.IsHovered); + + expandEvent?.Cancel(); + + if (hoveredButton?.IsHovered == true && State != ExpandedState.Expanded) expandEvent = Scheduler.AddDelayed(() => State = ExpandedState.Expanded, 750); - } - - lastHoveredButton = hoveredButton; } }