1
0
mirror of https://github.com/ppy/osu.git synced 2024-12-15 01:52:55 +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 System.Linq;
using NUnit.Framework; using NUnit.Framework;
using osu.Framework.Bindables; using osu.Framework.Bindables;
using osu.Framework.Graphics.Containers;
using osu.Framework.Testing; 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.Settings;
using osu.Game.Overlays; using osu.Game.Overlays;
@ -47,8 +49,8 @@ namespace osu.Game.Tests.Visual.Settings
public void TestSetAndClearLabelText() public void TestSetAndClearLabelText()
{ {
SettingsTextBox textBox = null; SettingsTextBox textBox = null;
GridContainer settingsItemGrid = null;
RestoreDefaultValueButton<string> restoreDefaultValueButton = null; RestoreDefaultValueButton<string> restoreDefaultValueButton = null;
OsuTextBox control = null;
AddStep("create settings item", () => AddStep("create settings item", () =>
{ {
@ -64,18 +66,25 @@ namespace osu.Game.Tests.Visual.Settings
AddUntilStep("wait for loaded", () => textBox.IsLoaded); AddUntilStep("wait for loaded", () => textBox.IsLoaded);
AddStep("retrieve components", () => AddStep("retrieve components", () =>
{ {
settingsItemGrid = textBox.ChildrenOfType<GridContainer>().Single();
restoreDefaultValueButton = textBox.ChildrenOfType<RestoreDefaultValueButton<string>>().Single(); restoreDefaultValueButton = textBox.ChildrenOfType<RestoreDefaultValueButton<string>>().Single();
control = textBox.ChildrenOfType<OsuTextBox>().Single();
}); });
AddStep("set non-default value", () => restoreDefaultValueButton.Current.Value = "non-default"); 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"); 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); 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> /// <summary>

View File

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

View File

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