1
0
mirror of https://github.com/ppy/osu.git synced 2024-12-15 13:22:55 +08:00

Show spinner next to buttons and get rid of EditorCommitButton

This commit is contained in:
ansel 2023-01-09 23:37:36 +03:00
parent 13b00928c8
commit 23e4cfb469
2 changed files with 58 additions and 75 deletions

View File

@ -7,7 +7,6 @@ using osu.Framework.Allocation;
using osu.Framework.Bindables; using osu.Framework.Bindables;
using osu.Framework.Graphics; using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers; using osu.Framework.Graphics.Containers;
using osu.Framework.Graphics.Sprites;
using osu.Framework.Graphics.UserInterface; using osu.Framework.Graphics.UserInterface;
using osu.Framework.Localisation; using osu.Framework.Localisation;
using osu.Framework.Testing; using osu.Framework.Testing;
@ -56,9 +55,9 @@ namespace osu.Game.Tests.Visual.UserInterface
AddStep("press Enter", () => InputManager.Key(Key.Enter)); AddStep("press Enter", () => InputManager.Key(Key.Enter));
AddUntilStep("button is loading", () => commentEditor.ButtonLoading); AddUntilStep("button is loading", () => commentEditor.IsSpinnerShown);
AddAssert("text committed", () => commentEditor.CommittedText == "text"); AddAssert("text committed", () => commentEditor.CommittedText == "text");
AddUntilStep("button is not loading", () => !commentEditor.ButtonLoading); AddUntilStep("button is not loading", () => !commentEditor.IsSpinnerShown);
} }
[Test] [Test]
@ -72,7 +71,7 @@ namespace osu.Game.Tests.Visual.UserInterface
AddStep("press Enter", () => InputManager.Key(Key.Enter)); AddStep("press Enter", () => InputManager.Key(Key.Enter));
AddAssert("button is not loading", () => !commentEditor.ButtonLoading); AddAssert("button is not loading", () => !commentEditor.IsSpinnerShown);
AddAssert("no text committed", () => commentEditor.CommittedText.Length == 0); AddAssert("no text committed", () => commentEditor.CommittedText.Length == 0);
} }
@ -92,9 +91,9 @@ namespace osu.Game.Tests.Visual.UserInterface
InputManager.Click(MouseButton.Left); InputManager.Click(MouseButton.Left);
}); });
AddUntilStep("button is loading", () => commentEditor.ButtonLoading); AddUntilStep("button is loading", () => commentEditor.IsSpinnerShown);
AddAssert("text committed", () => commentEditor.CommittedText == "some other text"); AddAssert("text committed", () => commentEditor.CommittedText == "some other text");
AddUntilStep("button is not loading", () => !commentEditor.ButtonLoading); AddUntilStep("button is not loading", () => !commentEditor.IsSpinnerShown);
} }
[Test] [Test]
@ -116,13 +115,13 @@ namespace osu.Game.Tests.Visual.UserInterface
public string CommittedText { get; private set; } = string.Empty; public string CommittedText { get; private set; } = string.Empty;
public bool ButtonLoading => CommitButton.ChildrenOfType<LoadingSpinner>().Single().IsPresent && !CommitButton.ChildrenOfType<SpriteText>().Single().IsPresent; public bool IsSpinnerShown => this.ChildrenOfType<LoadingSpinner>().Single().IsPresent;
protected override void OnCommit(string value) protected override void OnCommit(string value)
{ {
CommitButton.ShowLoadingSpinner = true; ShowLoadingSpinner = true;
CommittedText = value; CommittedText = value;
Scheduler.AddDelayed(() => CommitButton.ShowLoadingSpinner = false, 1000); Scheduler.AddDelayed(() => ShowLoadingSpinner = false, 1000);
} }
protected override LocalisableString FooterText => @"Footer text. And it is pretty long. Cool."; protected override LocalisableString FooterText => @"Footer text. And it is pretty long. Cool.";

View File

@ -32,7 +32,24 @@ namespace osu.Game.Overlays.Comments
protected readonly Bindable<string> Current = new Bindable<string>(); protected readonly Bindable<string> Current = new Bindable<string>();
protected EditorCommitButton CommitButton = null!; private RoundedButton commitButton = null!;
private LoadingSpinner loadingSpinner = null!;
private bool showLoadingSpinner;
protected bool ShowLoadingSpinner
{
set
{
commitButton.Enabled.Value = !value && !string.IsNullOrEmpty(Current.Value);
showLoadingSpinner = value;
if (value)
loadingSpinner.Show();
else
loadingSpinner.Hide();
}
}
[BackgroundDependencyLoader] [BackgroundDependencyLoader]
private void load(OverlayColourProvider colourProvider) private void load(OverlayColourProvider colourProvider)
@ -82,6 +99,15 @@ namespace osu.Game.Overlays.Comments
Font = OsuFont.GetFont(size: 14, weight: FontWeight.SemiBold), Font = OsuFont.GetFont(size: 14, weight: FontWeight.SemiBold),
Text = FooterText Text = FooterText
}, },
new FillFlowContainer
{
Anchor = Anchor.CentreRight,
Origin = Anchor.CentreRight,
AutoSizeAxes = Axes.Both,
Direction = FillDirection.Horizontal,
Spacing = new Vector2(5, 0),
Children = new Drawable[]
{
ButtonsContainer = new FillFlowContainer ButtonsContainer = new FillFlowContainer
{ {
Name = @"Buttons", Name = @"Buttons",
@ -90,28 +116,40 @@ namespace osu.Game.Overlays.Comments
AutoSizeAxes = Axes.Both, AutoSizeAxes = Axes.Both,
Direction = FillDirection.Horizontal, Direction = FillDirection.Horizontal,
Spacing = new Vector2(5, 0), Spacing = new Vector2(5, 0),
Child = CommitButton = new EditorCommitButton Child = commitButton = new RoundedButton
{ {
Width = 100,
Height = 30,
Text = CommitButtonText, Text = CommitButtonText,
Anchor = Anchor.CentreRight, Anchor = Anchor.CentreRight,
Origin = Anchor.CentreRight, Origin = Anchor.CentreRight,
Action = () => OnCommit(Current.Value) Action = () => OnCommit(Current.Value)
} }
},
loadingSpinner = new LoadingSpinner
{
Anchor = Anchor.CentreRight,
Origin = Anchor.CentreRight,
Size = new Vector2(18),
},
} }
},
} }
} }
} }
} }
}); });
textBox.OnCommit += (_, _) => CommitButton.TriggerClick(); textBox.OnCommit += (_, _) => commitButton.TriggerClick();
} }
protected override void LoadComplete() protected override void LoadComplete()
{ {
base.LoadComplete(); base.LoadComplete();
Current.BindValueChanged(text =>
Current.BindValueChanged(text => CommitButton.IsBlocked.Value = string.IsNullOrEmpty(text.NewValue), true); {
commitButton.Enabled.Value = !showLoadingSpinner && !string.IsNullOrEmpty(text.NewValue);
}, true);
} }
protected abstract void OnCommit(string text); protected abstract void OnCommit(string text);
@ -149,59 +187,5 @@ namespace osu.Game.Overlays.Comments
Child = new OsuSpriteText { Text = c.ToString(), Font = OsuFont.GetFont(size: CalculatedTextSize) } Child = new OsuSpriteText { Text = c.ToString(), Font = OsuFont.GetFont(size: CalculatedTextSize) }
}; };
} }
protected sealed partial class EditorCommitButton : RoundedButton
{
private const int duration = 200;
private readonly LoadingSpinner spinner;
private SpriteText text = null!;
public readonly BindableBool IsBlocked = new BindableBool();
private bool showLoadingSpinner;
/// <summary>
/// Whether loading spinner shown.
/// </summary>
public bool ShowLoadingSpinner
{
get => showLoadingSpinner;
set
{
showLoadingSpinner = value;
Enabled.Value = !value && !IsBlocked.Value;
spinner.FadeTo(value ? 1f : 0f, duration, Easing.OutQuint);
text.FadeTo(value ? 0f : 1f, duration, Easing.OutQuint);
}
}
public EditorCommitButton()
{
Width = 100;
Height = 30;
Add(spinner = new LoadingSpinner
{
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
Size = new Vector2(16),
Depth = -2,
});
}
protected override void LoadComplete()
{
base.LoadComplete();
IsBlocked.BindValueChanged(e =>
{
Enabled.Value = !ShowLoadingSpinner && !e.NewValue;
}, true);
}
protected override SpriteText CreateText()
{
return text = base.CreateText();
}
}
} }
} }