Fix ManageCollectionsDialog and remove weird placeholder logic

This commit is contained in:
Dean Herbert
2022-07-27 19:35:25 +09:00
parent 67c7f324ee
commit 34a2d1a6e1
6 changed files with 103 additions and 93 deletions

View File

@ -3,12 +3,12 @@
using System;
using osu.Framework.Allocation;
using osu.Framework.Bindables;
using osu.Framework.Extensions.Color4Extensions;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Framework.Graphics.Shapes;
using osu.Framework.Graphics.Sprites;
using osu.Framework.Graphics.UserInterface;
using osu.Framework.Input.Events;
using osu.Game.Database;
using osu.Game.Graphics;
@ -23,52 +23,37 @@ namespace osu.Game.Collections
/// <summary>
/// Visualises a <see cref="BeatmapCollection"/> inside a <see cref="DrawableCollectionList"/>.
/// </summary>
public class DrawableCollectionListItem : OsuRearrangeableListItem<BeatmapCollection>
public class DrawableCollectionListItem : OsuRearrangeableListItem<Live<BeatmapCollection>>
{
private const float item_height = 35;
private const float button_width = item_height * 0.75f;
/// <summary>
/// Whether the <see cref="BeatmapCollection"/> currently exists inside realm.
/// </summary>
public IBindable<bool> IsCreated => isCreated;
private readonly Bindable<bool> isCreated = new Bindable<bool>();
/// <summary>
/// Creates a new <see cref="DrawableCollectionListItem"/>.
/// </summary>
/// <param name="item">The <see cref="BeatmapCollection"/>.</param>
/// <param name="isCreated">Whether <paramref name="item"/> currently exists inside realm.</param>
public DrawableCollectionListItem(BeatmapCollection item, bool isCreated)
public DrawableCollectionListItem(Live<BeatmapCollection> item, bool isCreated)
: base(item)
{
this.isCreated.Value = isCreated;
ShowDragHandle.BindTo(this.isCreated);
ShowDragHandle.Value = item.IsManaged;
}
protected override Drawable CreateContent() => new ItemContent(Model)
{
IsCreated = { BindTarget = isCreated }
};
protected override Drawable CreateContent() => new ItemContent(Model);
/// <summary>
/// The main content of the <see cref="DrawableCollectionListItem"/>.
/// </summary>
private class ItemContent : CircularContainer
{
public readonly Bindable<bool> IsCreated = new Bindable<bool>();
private readonly Live<BeatmapCollection> collection;
private readonly BeatmapCollection collection;
private Container textBoxPaddingContainer = null!;
private ItemTextBox textBox = null!;
[Resolved]
private RealmAccess realm { get; set; } = null!;
public ItemContent(BeatmapCollection collection)
public ItemContent(Live<BeatmapCollection> collection)
{
this.collection = collection;
@ -80,19 +65,20 @@ namespace osu.Game.Collections
[BackgroundDependencyLoader]
private void load()
{
Children = new Drawable[]
Children = new[]
{
new DeleteButton(collection)
{
Anchor = Anchor.CentreRight,
Origin = Anchor.CentreRight,
IsCreated = { BindTarget = IsCreated },
IsTextBoxHovered = v => textBox.ReceivePositionalInputAt(v)
},
textBoxPaddingContainer = new Container
collection.IsManaged
? new DeleteButton(collection)
{
Anchor = Anchor.CentreRight,
Origin = Anchor.CentreRight,
IsTextBoxHovered = v => textBox.ReceivePositionalInputAt(v)
}
: Empty(),
new Container
{
RelativeSizeAxes = Axes.Both,
Padding = new MarginPadding { Right = button_width },
Padding = new MarginPadding { Right = collection.IsManaged ? button_width : 0 },
Children = new Drawable[]
{
textBox = new ItemTextBox
@ -100,7 +86,7 @@ namespace osu.Game.Collections
RelativeSizeAxes = Axes.Both,
Size = Vector2.One,
CornerRadius = item_height / 2,
PlaceholderText = IsCreated.Value ? string.Empty : "Create a new collection"
PlaceholderText = collection.IsManaged ? string.Empty : "Create a new collection"
},
}
},
@ -112,28 +98,18 @@ namespace osu.Game.Collections
base.LoadComplete();
// Bind late, as the collection name may change externally while still loading.
textBox.Current.Value = collection.Name;
textBox.Current.BindValueChanged(_ => createNewCollection(), true);
IsCreated.BindValueChanged(created => textBoxPaddingContainer.Padding = new MarginPadding { Right = created.NewValue ? button_width : 0 }, true);
textBox.Current.Value = collection.PerformRead(c => c.IsValid ? c.Name : string.Empty);
textBox.OnCommit += onCommit;
}
private void createNewCollection()
private void onCommit(TextBox sender, bool newText)
{
if (IsCreated.Value)
return;
if (collection.IsManaged)
collection.PerformWrite(c => c.Name = textBox.Current.Value);
else if (!string.IsNullOrEmpty(textBox.Current.Value))
realm.Write(r => r.Add(new BeatmapCollection(textBox.Current.Value)));
if (string.IsNullOrEmpty(textBox.Current.Value))
return;
// Add the new collection and disable our placeholder. If all text is removed, the placeholder should not show back again.
realm.Write(r => r.Add(collection));
textBox.PlaceholderText = string.Empty;
// When this item changes from placeholder to non-placeholder (via changing containers), its textbox will lose focus, so it needs to be re-focused.
Schedule(() => GetContainingInputManager().ChangeFocus(textBox));
IsCreated.Value = true;
textBox.Text = string.Empty;
}
}
@ -151,22 +127,17 @@ namespace osu.Game.Collections
public class DeleteButton : CompositeDrawable
{
public readonly IBindable<bool> IsCreated = new Bindable<bool>();
public Func<Vector2, bool> IsTextBoxHovered = null!;
[Resolved]
private IDialogOverlay? dialogOverlay { get; set; }
[Resolved]
private RealmAccess realmAccess { get; set; } = null!;
private readonly BeatmapCollection collection;
private readonly Live<BeatmapCollection> collection;
private Drawable fadeContainer = null!;
private Drawable background = null!;
public DeleteButton(BeatmapCollection collection)
public DeleteButton(Live<BeatmapCollection> collection)
{
this.collection = collection;
RelativeSizeAxes = Axes.Y;
@ -200,12 +171,6 @@ namespace osu.Game.Collections
};
}
protected override void LoadComplete()
{
base.LoadComplete();
IsCreated.BindValueChanged(created => Alpha = created.NewValue ? 1 : 0, true);
}
public override bool ReceivePositionalInputAt(Vector2 screenSpacePos) => base.ReceivePositionalInputAt(screenSpacePos) && !IsTextBoxHovered(screenSpacePos);
protected override bool OnHover(HoverEvent e)
@ -223,7 +188,7 @@ namespace osu.Game.Collections
{
background.FlashColour(Color4.White, 150);
if (collection.BeatmapMD5Hashes.Count == 0)
if (collection.PerformRead(c => c.BeatmapMD5Hashes.Count) == 0)
deleteCollection();
else
dialogOverlay?.Push(new DeleteCollectionDialog(collection, deleteCollection));
@ -231,7 +196,7 @@ namespace osu.Game.Collections
return true;
}
private void deleteCollection() => realmAccess.Write(r => r.Remove(collection));
private void deleteCollection() => collection.PerformWrite(c => c.Realm.Remove(c));
}
}
}