Merge pull request #21937 from bdach/fix-toolbar-gradient

Fix toolbar gradient not showing when mouse is hovered over buttons
This commit is contained in:
Dean Herbert 2022-12-31 03:07:59 +08:00 committed by GitHub
commit ceedc3f934
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 45 additions and 5 deletions

View File

@ -207,7 +207,7 @@ namespace osu.Game.Tests.Visual.Menus
AddStep("set toolbar width", () =>
{
toolbar.RelativeSizeAxes = Axes.None;
toolbar.Width = 0;
toolbar.Width = 400;
});
AddStep("move mouse over news toggle button", () =>
{
@ -215,6 +215,7 @@ namespace osu.Game.Tests.Visual.Menus
InputManager.MoveMouseTo(button);
});
AddAssert("no ruleset toggle buttons hovered", () => !toolbar.ChildrenOfType<ToolbarRulesetTabButton>().Any(button => button.IsHovered));
AddUntilStep("toolbar gradient visible", () => toolbar.ChildrenOfType<Toolbar.ToolbarBackground>().Single().Children.All(d => d.Alpha > 0));
}
public partial class TestToolbar : Toolbar

View File

@ -65,9 +65,12 @@ namespace osu.Game.Overlays.Toolbar
[BackgroundDependencyLoader(true)]
private void load(OsuGame osuGame)
{
ToolbarBackground background;
HoverInterceptor interceptor;
Children = new Drawable[]
{
new ToolbarBackground(),
background = new ToolbarBackground(),
new GridContainer
{
RelativeSizeAxes = Axes.Both,
@ -180,9 +183,15 @@ namespace osu.Game.Overlays.Toolbar
},
},
}
},
interceptor = new HoverInterceptor
{
RelativeSizeAxes = Axes.Both
}
};
((IBindable<bool>)background.ShowGradient).BindTo(interceptor.ReceivedHover);
if (osuGame != null)
OverlayActivationMode.BindTo(osuGame.OverlayActivationMode);
}
@ -196,6 +205,8 @@ namespace osu.Game.Overlays.Toolbar
public partial class ToolbarBackground : Container
{
public Bindable<bool> ShowGradient { get; } = new BindableBool();
private readonly Box gradientBackground;
public ToolbarBackground()
@ -220,15 +231,43 @@ namespace osu.Game.Overlays.Toolbar
};
}
protected override void LoadComplete()
{
base.LoadComplete();
ShowGradient.BindValueChanged(_ => updateState(), true);
}
private void updateState()
{
if (ShowGradient.Value)
gradientBackground.FadeIn(transition_time, Easing.OutQuint);
else
gradientBackground.FadeOut(transition_time, Easing.OutQuint);
}
}
/// <summary>
/// Whenever the mouse cursor is within the bounds of the toolbar, we want the background gradient to show, for toolbar button descriptions to be legible.
/// Unfortunately we also need to ensure that the toolbar buttons handle hover, to prevent the possibility of multiple descriptions being shown
/// due to hover events passing through multiple buttons.
/// This drawable is a workaround, that when placed front-most in the toolbar, allows to see whether hover events have been propagated through it without handling them.
/// </summary>
private partial class HoverInterceptor : Drawable
{
public IBindable<bool> ReceivedHover => receivedHover;
private readonly Bindable<bool> receivedHover = new BindableBool();
protected override bool OnHover(HoverEvent e)
{
gradientBackground.FadeIn(transition_time, Easing.OutQuint);
return true;
receivedHover.Value = true;
return base.OnHover(e);
}
protected override void OnHoverLost(HoverLostEvent e)
{
gradientBackground.FadeOut(transition_time, Easing.OutQuint);
receivedHover.Value = false;
base.OnHoverLost(e);
}
}