From f422ebb281edd47e30968ba8ec7d38ca390fe382 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bart=C5=82omiej=20Dach?= Date: Fri, 15 Oct 2021 22:44:28 +0200 Subject: [PATCH] Adjust `SettingsItem` to accommodate new default value indicator --- .../Visual/Settings/TestSceneSettingsItem.cs | 45 ++++++++++++-- .../Overlays/Settings/SettingsDropdown.cs | 6 -- osu.Game/Overlays/Settings/SettingsItem.cs | 61 +++++++++++++++---- osu.Game/Overlays/Settings/SettingsSlider.cs | 1 - 4 files changed, 90 insertions(+), 23 deletions(-) diff --git a/osu.Game.Tests/Visual/Settings/TestSceneSettingsItem.cs b/osu.Game.Tests/Visual/Settings/TestSceneSettingsItem.cs index d9cce69ee3..9c3940af4c 100644 --- a/osu.Game.Tests/Visual/Settings/TestSceneSettingsItem.cs +++ b/osu.Game.Tests/Visual/Settings/TestSceneSettingsItem.cs @@ -4,6 +4,7 @@ using System.Linq; using NUnit.Framework; using osu.Framework.Bindables; +using osu.Framework.Graphics.Containers; using osu.Framework.Testing; using osu.Game.Overlays.Settings; using osu.Game.Overlays; @@ -29,9 +30,10 @@ namespace osu.Game.Tests.Visual.Settings Value = "test" } }; - - restoreDefaultValueButton = textBox.ChildrenOfType>().Single(); }); + AddUntilStep("wait for loaded", () => textBox.IsLoaded); + AddStep("retrieve restore default button", () => restoreDefaultValueButton = textBox.ChildrenOfType>().Single()); + AddAssert("restore button hidden", () => restoreDefaultValueButton.Alpha == 0); AddStep("change value from default", () => textBox.Current.Value = "non-default"); @@ -41,6 +43,41 @@ namespace osu.Game.Tests.Visual.Settings AddUntilStep("restore button hidden", () => restoreDefaultValueButton.Alpha == 0); } + [Test] + public void TestSetAndClearLabelText() + { + SettingsTextBox textBox = null; + GridContainer settingsItemGrid = null; + RestoreDefaultValueButton restoreDefaultValueButton = null; + + AddStep("create settings item", () => + { + Child = textBox = new SettingsTextBox + { + Current = new Bindable + { + Default = "test", + Value = "test" + } + }; + }); + AddUntilStep("wait for loaded", () => textBox.IsLoaded); + AddStep("retrieve components", () => + { + settingsItemGrid = textBox.ChildrenOfType().Single(); + restoreDefaultValueButton = 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); + + AddStep("set label", () => textBox.LabelText = "label text"); + AddAssert("default value button next to label", () => settingsItemGrid.Content[0][0] == restoreDefaultValueButton); + + AddStep("clear label", () => textBox.LabelText = default); + AddAssert("default value button next to control", () => settingsItemGrid.Content[1][0] == restoreDefaultValueButton); + } + /// /// Ensures that the reset to default button uses the correct implementation of IsDefault to determine whether it should be shown or not. /// Values have been chosen so that after being set, Value != Default (but they are close enough that the difference is negligible compared to Precision). @@ -64,9 +101,9 @@ namespace osu.Game.Tests.Visual.Settings Precision = 0.1f, } }; - - restoreDefaultValueButton = sliderBar.ChildrenOfType>().Single(); }); + AddUntilStep("wait for loaded", () => sliderBar.IsLoaded); + AddStep("retrieve restore default button", () => restoreDefaultValueButton = sliderBar.ChildrenOfType>().Single()); AddAssert("restore button hidden", () => restoreDefaultValueButton.Alpha == 0); diff --git a/osu.Game/Overlays/Settings/SettingsDropdown.cs b/osu.Game/Overlays/Settings/SettingsDropdown.cs index a281d03ee7..1e90222d28 100644 --- a/osu.Game/Overlays/Settings/SettingsDropdown.cs +++ b/osu.Game/Overlays/Settings/SettingsDropdown.cs @@ -6,7 +6,6 @@ using System.Linq; using osu.Framework.Bindables; using osu.Framework.Graphics; using osu.Game.Graphics.UserInterface; -using osuTK; namespace osu.Game.Overlays.Settings { @@ -28,11 +27,6 @@ namespace osu.Game.Overlays.Settings public override IEnumerable FilterTerms => base.FilterTerms.Concat(Control.Items.Select(i => i.ToString())); - public SettingsDropdown() - { - FlowContent.Spacing = new Vector2(0, 10); - } - protected sealed override Drawable CreateControl() => CreateDropdown(); protected virtual OsuDropdown CreateDropdown() => new DropdownControl(); diff --git a/osu.Game/Overlays/Settings/SettingsItem.cs b/osu.Game/Overlays/Settings/SettingsItem.cs index 5282217013..7895f6386b 100644 --- a/osu.Game/Overlays/Settings/SettingsItem.cs +++ b/osu.Game/Overlays/Settings/SettingsItem.cs @@ -3,6 +3,7 @@ using System; using System.Collections.Generic; +using JetBrains.Annotations; using osu.Framework.Allocation; using osu.Framework.Bindables; using osu.Framework.Graphics; @@ -14,6 +15,7 @@ using osu.Framework.Localisation; using osu.Game.Graphics; using osu.Game.Graphics.Sprites; using osu.Game.Graphics.Containers; +using osuTK; namespace osu.Game.Overlays.Settings { @@ -30,6 +32,10 @@ namespace osu.Game.Overlays.Settings protected readonly FillFlowContainer FlowContent; private SpriteText labelText; + private readonly GridContainer gridContainer; + + [CanBeNull] + private RestoreDefaultValueButton defaultValueButton; private OsuTextFlowContainer warningText; @@ -48,12 +54,13 @@ namespace osu.Game.Overlays.Settings if (labelText == null) { // construct lazily for cases where the label is not needed (may be provided by the Control). - FlowContent.Insert(-1, labelText = new OsuSpriteText()); + gridContainer.Content[0][1] = labelText = new OsuSpriteText(); updateDisabled(); } labelText.Text = value; + updateLayout(); } } @@ -106,18 +113,33 @@ namespace osu.Game.Overlays.Settings AutoSizeAxes = Axes.Y; Padding = new MarginPadding { Right = SettingsPanel.CONTENT_MARGINS }; - InternalChildren = new Drawable[] + FlowContent = new FillFlowContainer { - FlowContent = new FillFlowContainer + 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[] { - RelativeSizeAxes = Axes.X, - AutoSizeAxes = Axes.Y, - Padding = new MarginPadding { Left = SettingsPanel.CONTENT_MARGINS }, - Children = new[] - { - Control = CreateControl(), - }, + new Dimension(GridSizeMode.AutoSize), + new Dimension(GridSizeMode.AutoSize) }, + ColumnDimensions = new[] + { + new Dimension(GridSizeMode.Absolute, SettingsPanel.CONTENT_MARGINS), + new Dimension() + }, + Content = new[] + { + new Drawable[2], + new Drawable[] { null, FlowContent } + } }; // IMPORTANT: all bindable logic is in constructor intentionally to support "CreateSettingsControls" being used in a context it is @@ -135,13 +157,28 @@ namespace osu.Game.Overlays.Settings // intentionally done before LoadComplete to avoid overhead. if (ShowsDefaultIndicator) { - AddInternal(new RestoreDefaultValueButton + defaultValueButton = new RestoreDefaultValueButton { Current = controlWithCurrent.Current, - }); + Anchor = Anchor.Centre, + Origin = Anchor.Centre + }; + updateLayout(); } } + private void updateLayout() + { + bool hasLabel = !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 }; + } + private void updateDisabled() { if (labelText != null) diff --git a/osu.Game/Overlays/Settings/SettingsSlider.cs b/osu.Game/Overlays/Settings/SettingsSlider.cs index bb9c0dd4d7..b95b0af11c 100644 --- a/osu.Game/Overlays/Settings/SettingsSlider.cs +++ b/osu.Game/Overlays/Settings/SettingsSlider.cs @@ -19,7 +19,6 @@ namespace osu.Game.Overlays.Settings { protected override Drawable CreateControl() => new TSlider { - Margin = new MarginPadding { Vertical = 10 }, RelativeSizeAxes = Axes.X };