From 88a1b31fae9538a0e38ffc3aef9e9167e90a3120 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bart=C5=82omiej=20Dach?= Date: Mon, 18 Oct 2021 21:01:43 +0200 Subject: [PATCH] Remove grid usage in `SettingsItem` --- .../Visual/Settings/TestSceneSettingsItem.cs | 21 +++++--- osu.Game/Overlays/Settings/SettingsItem.cs | 54 ++++++------------- osu.Game/Overlays/Settings/SettingsTextBox.cs | 1 - 3 files changed, 32 insertions(+), 44 deletions(-) diff --git a/osu.Game.Tests/Visual/Settings/TestSceneSettingsItem.cs b/osu.Game.Tests/Visual/Settings/TestSceneSettingsItem.cs index 9c3940af4c..83265e13ad 100644 --- a/osu.Game.Tests/Visual/Settings/TestSceneSettingsItem.cs +++ b/osu.Game.Tests/Visual/Settings/TestSceneSettingsItem.cs @@ -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 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().Single(); restoreDefaultValueButton = textBox.ChildrenOfType>().Single(); + control = textBox.ChildrenOfType().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().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)); } /// diff --git a/osu.Game/Overlays/Settings/SettingsItem.cs b/osu.Game/Overlays/Settings/SettingsItem.cs index 7895f6386b..91998f07d8 100644 --- a/osu.Game/Overlays/Settings/SettingsItem.cs +++ b/osu.Game/Overlays/Settings/SettingsItem.cs @@ -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 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 + defaultValueIndicatorContainer.Add(new RestoreDefaultValueButton { 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() diff --git a/osu.Game/Overlays/Settings/SettingsTextBox.cs b/osu.Game/Overlays/Settings/SettingsTextBox.cs index 68562802cf..a724003183 100644 --- a/osu.Game/Overlays/Settings/SettingsTextBox.cs +++ b/osu.Game/Overlays/Settings/SettingsTextBox.cs @@ -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 };