1
0
mirror of https://github.com/ppy/osu.git synced 2024-12-14 23:12:56 +08:00

Remove grid usage in SettingsItem

This commit is contained in:
Bartłomiej Dach 2021-10-18 21:01:43 +02:00
parent 6c3637a62a
commit 88a1b31fae
No known key found for this signature in database
GPG Key ID: BCECCD4FA41F6497
3 changed files with 32 additions and 44 deletions

View File

@ -4,8 +4,10 @@
using System.Linq;
using NUnit.Framework;
using osu.Framework.Bindables;
using osu.Framework.Graphics.Containers;
using osu.Framework.Testing;
using osu.Framework.Utils;
using osu.Game.Graphics.Sprites;
using osu.Game.Graphics.UserInterface;
using osu.Game.Overlays.Settings;
using osu.Game.Overlays;
@ -47,8 +49,8 @@ namespace osu.Game.Tests.Visual.Settings
public void TestSetAndClearLabelText()
{
SettingsTextBox textBox = null;
GridContainer settingsItemGrid = null;
RestoreDefaultValueButton<string> restoreDefaultValueButton = null;
OsuTextBox control = null;
AddStep("create settings item", () =>
{
@ -64,18 +66,25 @@ namespace osu.Game.Tests.Visual.Settings
AddUntilStep("wait for loaded", () => textBox.IsLoaded);
AddStep("retrieve components", () =>
{
settingsItemGrid = textBox.ChildrenOfType<GridContainer>().Single();
restoreDefaultValueButton = textBox.ChildrenOfType<RestoreDefaultValueButton<string>>().Single();
control = textBox.ChildrenOfType<OsuTextBox>().Single();
});
AddStep("set non-default value", () => restoreDefaultValueButton.Current.Value = "non-default");
AddAssert("default value button next to control", () => settingsItemGrid.Content[1][0] == restoreDefaultValueButton);
AddAssert("default value button centre aligned to control size", () => Precision.AlmostEquals(restoreDefaultValueButton.Parent.DrawHeight, control.DrawHeight, 1));
AddStep("set label", () => textBox.LabelText = "label text");
AddAssert("default value button next to label", () => settingsItemGrid.Content[0][0] == restoreDefaultValueButton);
AddAssert("default value button centre aligned to label size", () =>
{
var label = textBox.ChildrenOfType<OsuSpriteText>().Single(spriteText => spriteText.Text == "label text");
return Precision.AlmostEquals(restoreDefaultValueButton.Parent.DrawHeight, label.DrawHeight, 1);
});
AddStep("clear label", () => textBox.LabelText = default);
AddAssert("default value button next to control", () => settingsItemGrid.Content[1][0] == restoreDefaultValueButton);
AddAssert("default value button centre aligned to control size", () => Precision.AlmostEquals(restoreDefaultValueButton.Parent.DrawHeight, control.DrawHeight, 1));
AddStep("set warning text", () => textBox.WarningText = "This is some very important warning text! Hopefully it doesn't break the alignment of the default value indicator...");
AddAssert("default value button centre aligned to control size", () => Precision.AlmostEquals(restoreDefaultValueButton.Parent.DrawHeight, control.DrawHeight, 1));
}
/// <summary>

View File

@ -3,7 +3,6 @@
using System;
using System.Collections.Generic;
using JetBrains.Annotations;
using osu.Framework.Allocation;
using osu.Framework.Bindables;
using osu.Framework.Graphics;
@ -32,14 +31,11 @@ namespace osu.Game.Overlays.Settings
protected readonly FillFlowContainer FlowContent;
private SpriteText labelText;
private readonly GridContainer gridContainer;
[CanBeNull]
private RestoreDefaultValueButton<T> defaultValueButton;
private OsuTextFlowContainer warningText;
public bool ShowsDefaultIndicator = true;
private readonly Container defaultValueIndicatorContainer;
public LocalisableString TooltipText { get; set; }
@ -54,7 +50,7 @@ namespace osu.Game.Overlays.Settings
if (labelText == null)
{
// construct lazily for cases where the label is not needed (may be provided by the Control).
gridContainer.Content[0][1] = labelText = new OsuSpriteText();
FlowContent.Insert(-1, labelText = new OsuSpriteText());
updateDisabled();
}
@ -113,32 +109,19 @@ namespace osu.Game.Overlays.Settings
AutoSizeAxes = Axes.Y;
Padding = new MarginPadding { Right = SettingsPanel.CONTENT_MARGINS };
FlowContent = new FillFlowContainer
InternalChildren = new Drawable[]
{
RelativeSizeAxes = Axes.X,
AutoSizeAxes = Axes.Y,
Spacing = new Vector2(0, 10),
Child = Control = CreateControl(),
};
InternalChild = gridContainer = new GridContainer
{
RelativeSizeAxes = Axes.X,
AutoSizeAxes = Axes.Y,
RowDimensions = new[]
defaultValueIndicatorContainer = new Container
{
new Dimension(GridSizeMode.AutoSize),
new Dimension(GridSizeMode.AutoSize)
Width = SettingsPanel.CONTENT_MARGINS,
},
ColumnDimensions = new[]
FlowContent = new FillFlowContainer
{
new Dimension(GridSizeMode.Absolute, SettingsPanel.CONTENT_MARGINS),
new Dimension()
},
Content = new[]
{
new Drawable[2],
new Drawable[] { null, FlowContent }
RelativeSizeAxes = Axes.X,
AutoSizeAxes = Axes.Y,
Padding = new MarginPadding { Left = SettingsPanel.CONTENT_MARGINS },
Spacing = new Vector2(0, 10),
Child = Control = CreateControl(),
}
};
@ -157,26 +140,23 @@ namespace osu.Game.Overlays.Settings
// intentionally done before LoadComplete to avoid overhead.
if (ShowsDefaultIndicator)
{
defaultValueButton = new RestoreDefaultValueButton<T>
defaultValueIndicatorContainer.Add(new RestoreDefaultValueButton<T>
{
Current = controlWithCurrent.Current,
Anchor = Anchor.Centre,
Origin = Anchor.Centre
};
});
updateLayout();
}
}
private void updateLayout()
{
bool hasLabel = !string.IsNullOrEmpty(labelText?.Text.ToString());
bool hasLabel = labelText != null && !string.IsNullOrEmpty(labelText.Text.ToString());
gridContainer.Content[0][0] = null;
gridContainer.Content[1][0] = null;
gridContainer.Content[hasLabel ? 0 : 1][0] = defaultValueButton;
FlowContent.Margin = new MarginPadding { Top = hasLabel ? 10 : 0 };
// if the settings item is providing a label, the default value indicator should be centred vertically to the left of the label.
// otherwise, it should be centred vertically to the left of the main control of the settings item.
defaultValueIndicatorContainer.Height = hasLabel ? labelText.DrawHeight : Control.DrawHeight;
}
private void updateDisabled()

View File

@ -11,7 +11,6 @@ namespace osu.Game.Overlays.Settings
{
protected override Drawable CreateControl() => new OutlinedTextBox
{
Margin = new MarginPadding { Top = 5 },
RelativeSizeAxes = Axes.X,
CommitOnFocusLost = true
};