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

Limit maximum tooltip width to prevent overflow #29210

Merged
Merged
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
86 changes: 86 additions & 0 deletions osu.Game.Tests/Visual/UserInterface/TestSceneOsuTooltip.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text.

using System.Linq;
using NUnit.Framework;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Framework.Graphics.Cursor;
using osu.Framework.Graphics.Shapes;
using osu.Framework.Localisation;
using osu.Framework.Testing;
using osu.Game.Graphics;
using osu.Game.Graphics.Cursor;
using osu.Game.Graphics.Sprites;
using osuTK;

namespace osu.Game.Tests.Visual.UserInterface
{
[TestFixture]
public partial class TestSceneOsuTooltip : OsuManualInputManagerTestScene
{
private TestTooltipContainer container = null!;

private static readonly string[] test_case_tooltip_string =
[
"Hello!!",
string.Concat(Enumerable.Repeat("Hello ", 100)),

//TODO: o!f issue: https://github.com/ppy/osu-framework/issues/5007
//Enable after o!f fixed
// $"H{new string('e', 500)}llo",
];

[SetUp]
public void SetUp() => Schedule(() =>
{
Child = new Container
{
RelativeSizeAxes = Axes.Both,
Padding = new MarginPadding(100),
Children = new Drawable[]
{
new Box
{
Colour = Colour4.Red.Opacity(0.5f),
RelativeSizeAxes = Axes.Both,
},
container = new TestTooltipContainer
{
RelativeSizeAxes = Axes.Both,
Child = new OsuSpriteText
{
Text = "Hover me!",
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
Font = OsuFont.GetFont(size: 50)
}
},
},
};
});

[TestCaseSource(nameof(test_case_tooltip_string))]
public void TestTooltipBasic(string text)
{
AddStep("Set tooltip content", () => container.TooltipText = text);

AddStep("Move mouse to container", () => InputManager.MoveMouseTo(new Vector2(InputManager.ScreenSpaceDrawQuad.Centre.X, InputManager.ScreenSpaceDrawQuad.Centre.Y)));

OsuTooltipContainer.OsuTooltip? tooltip = null!;

AddUntilStep("Wait for the tooltip shown", () =>
{
tooltip = container.FindClosestParent<OsuTooltipContainer>().ChildrenOfType<OsuTooltipContainer.OsuTooltip>().FirstOrDefault();
return tooltip != null && tooltip.Alpha == 1;
});

AddAssert("Check tooltip is under width limit", () => tooltip != null && tooltip.Width <= 500);
}

internal sealed partial class TestTooltipContainer : Container, IHasTooltip
{
public LocalisableString TooltipText { get; set; }
}
}
}
27 changes: 19 additions & 8 deletions osu.Game/Graphics/Cursor/OsuTooltipContainer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
using osu.Framework.Graphics.Effects;
using osu.Framework.Graphics.Shapes;
using osu.Framework.Localisation;
using osu.Game.Graphics.Sprites;
using osu.Framework.Graphics.Containers;

namespace osu.Game.Graphics.Cursor
{
Expand All @@ -27,15 +27,20 @@ public OsuTooltipContainer(CursorContainer cursor)

public partial class OsuTooltip : Tooltip
{
private const float max_width = 500;

private readonly Box background;
private readonly OsuSpriteText text;
private readonly TextFlowContainer text;
private bool instantMovement = true;

public override void SetContent(LocalisableString contentString)
private LocalisableString lastContent;

public override void SetContent(LocalisableString content)
{
if (contentString == text.Text) return;
if (content.Equals(lastContent))
return;

text.Text = contentString;
text.Text = content;

if (IsPresent)
{
Expand All @@ -44,6 +49,8 @@ public override void SetContent(LocalisableString contentString)
}
else
AutoSizeDuration = 0;

lastContent = content;
}

public OsuTooltip()
Expand All @@ -65,10 +72,14 @@ public OsuTooltip()
RelativeSizeAxes = Axes.Both,
Alpha = 0.9f,
},
text = new OsuSpriteText
text = new TextFlowContainer(f =>
{
f.Font = OsuFont.GetFont(weight: FontWeight.Regular);
})
{
Padding = new MarginPadding(5),
Font = OsuFont.GetFont(weight: FontWeight.Regular)
Margin = new MarginPadding(5),
AutoSizeAxes = Axes.Both,
MaximumSize = new Vector2(max_width, float.PositiveInfinity),
}
};
}
Expand Down
Loading