Merge pull request #19538 from bdach/mod-overlay/sheared-button-broken-autosize

Fix broken sheared button autosizing logic
This commit is contained in:
Dean Herbert 2022-08-02 11:51:14 +09:00 committed by GitHub
commit 5501195ed7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 17 additions and 1 deletions

View File

@ -3,9 +3,13 @@
#nullable disable
using System.Linq;
using NUnit.Framework;
using osu.Framework.Allocation;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Sprites;
using osu.Framework.Testing;
using osu.Framework.Utils;
using osu.Game.Graphics.UserInterface;
using osu.Game.Overlays;
using osuTK.Input;
@ -99,7 +103,10 @@ namespace osu.Game.Tests.Visual.UserInterface
Origin = Anchor.Centre,
Text = "Fixed width"
});
AddAssert("draw width is 200", () => toggleButton.DrawWidth, () => Is.EqualTo(200).Within(Precision.FLOAT_EPSILON));
AddStep("change text", () => toggleButton.Text = "New text");
AddAssert("draw width is 200", () => toggleButton.DrawWidth, () => Is.EqualTo(200).Within(Precision.FLOAT_EPSILON));
AddStep("create auto-sizing button", () => Child = toggleButton = new ShearedToggleButton
{
@ -107,7 +114,14 @@ namespace osu.Game.Tests.Visual.UserInterface
Origin = Anchor.Centre,
Text = "This button autosizes to its text!"
});
AddAssert("button is wider than text", () => toggleButton.DrawWidth, () => Is.GreaterThan(toggleButton.ChildrenOfType<SpriteText>().Single().DrawWidth));
float originalDrawWidth = 0;
AddStep("store button width", () => originalDrawWidth = toggleButton.DrawWidth);
AddStep("change text", () => toggleButton.Text = "New text");
AddAssert("button is wider than text", () => toggleButton.DrawWidth, () => Is.GreaterThan(toggleButton.ChildrenOfType<SpriteText>().Single().DrawWidth));
AddAssert("button width decreased", () => toggleButton.DrawWidth, () => Is.LessThan(originalDrawWidth));
}
[Test]

View File

@ -97,7 +97,7 @@ namespace osu.Game.Graphics.UserInterface
{
backgroundLayer = new Container
{
RelativeSizeAxes = Axes.Both,
RelativeSizeAxes = Axes.Y,
CornerRadius = corner_radius,
Masking = true,
BorderThickness = 2,
@ -128,10 +128,12 @@ namespace osu.Game.Graphics.UserInterface
if (width != null)
{
Width = width.Value;
backgroundLayer.RelativeSizeAxes = Axes.Both;
}
else
{
AutoSizeAxes = Axes.X;
backgroundLayer.AutoSizeAxes = Axes.X;
text.Margin = new MarginPadding { Horizontal = 15 };
}
}