Skip to content

Commit

Permalink
Add search bar to collection management dialog
Browse files Browse the repository at this point in the history
  • Loading branch information
peppy committed Nov 18, 2024
1 parent a570863 commit 5ce1f76
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
using osu.Framework.Testing;
using osu.Game.Beatmaps;
using osu.Game.Collections;
using osu.Game.Graphics.UserInterface;
using osu.Game.Overlays;
using osu.Game.Overlays.Dialog;
using osu.Game.Rulesets;
Expand Down Expand Up @@ -265,7 +266,6 @@ public void TestCollectionNotRemovedWhenDialogCancelled()
}

[Test]
[Solo]
public void TestCollectionRenamedExternal()
{
BeatmapCollection first = null!;
Expand Down Expand Up @@ -338,10 +338,44 @@ public void TestCollectionRenamedOnTextChange(bool commitWithEnter)
AddUntilStep("collection has new name", () => first.Name == "First");
}

[Test]
public void TestSearch()
{
BeatmapCollection first = null!;

AddStep("add two collections", () =>
{
Realm.Write(r =>
{
r.Add(new[]
{
first = new BeatmapCollection(name: "1"),
new BeatmapCollection(name: "2"),
});
});
});

assertCollectionName(0, "1");
assertCollectionName(1, "2");

AddStep("search for 1", () => dialog.ChildrenOfType<SearchTextBox>().Single().Current.Value = "1");

assertCollectionCount(1);

AddStep("change first collection name", () => Realm.Write(_ => first.Name = "First"));

assertCollectionCount(0);

AddStep("search for first", () => dialog.ChildrenOfType<SearchTextBox>().Single().Current.Value = "firs");

assertCollectionCount(1);
}

private void assertCollectionCount(int count)
=> AddUntilStep($"{count} collections shown", () => dialog.ChildrenOfType<DrawableCollectionListItem>().Count() == count + 1); // +1 for placeholder
=> AddUntilStep($"{count} collections shown", () => dialog.ChildrenOfType<DrawableCollectionListItem>().Count(i => i.IsPresent) == count + 1); // +1 for placeholder

private void assertCollectionName(int index, string name)
=> AddUntilStep($"item {index + 1} has correct name", () => dialog.ChildrenOfType<DrawableCollectionList>().Single().OrderedItems.ElementAt(index).ChildrenOfType<TextBox>().First().Text == name);
=> AddUntilStep($"item {index + 1} has correct name",
() => dialog.ChildrenOfType<DrawableCollectionList>().Single().OrderedItems.ElementAt(index).ChildrenOfType<TextBox>().First().Text == name);
}
}
11 changes: 8 additions & 3 deletions osu.Game/Collections/DrawableCollectionList.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@ public partial class DrawableCollectionList : OsuRearrangeableListContainer<Live

public IEnumerable<Drawable> OrderedItems => flow.FlowingChildren;

public string SearchTerm
{
get => flow.SearchTerm;
set => flow.SearchTerm = value;
}

protected override FillFlowContainer<RearrangeableListItem<Live<BeatmapCollection>>> CreateListFillFlowContainer() => flow = new Flow
{
DragActive = { BindTarget = DragActive }
Expand Down Expand Up @@ -128,7 +134,6 @@ private partial class Scroll : OsuScrollContainer

public Scroll()
{
Padding = new MarginPadding(10);
ScrollbarOverlapsContent = false;

base.Content.Add(new FillFlowContainer
Expand Down Expand Up @@ -157,7 +162,7 @@ protected override void Update()
base.Update();

// AutoSizeAxes cannot be used as the height should represent the post-layout-transform height at all times, so that the placeholder doesn't bounce around.
content.Height = ((Flow)Child).Children.Sum(c => c.DrawHeight + 5);
content.Height = ((Flow)Child).Children.Sum(c => c.IsPresent ? c.DrawHeight + 5 : 0);
}

/// <summary>
Expand Down Expand Up @@ -203,7 +208,7 @@ protected override void LoadComplete()
/// <summary>
/// The flow of <see cref="DrawableCollectionListItem"/>. Disables layout easing unless a drag is in progress.
/// </summary>
private partial class Flow : FillFlowContainer<RearrangeableListItem<Live<BeatmapCollection>>>
private partial class Flow : SearchContainer<RearrangeableListItem<Live<BeatmapCollection>>>
{
public readonly IBindable<bool> DragActive = new Bindable<bool>();

Expand Down
24 changes: 23 additions & 1 deletion osu.Game/Collections/DrawableCollectionListItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// See the LICENCE file in the repository root for full licence text.

using System;
using System.Collections.Generic;
using osu.Framework.Allocation;
using osu.Framework.Extensions.Color4Extensions;
using osu.Framework.Graphics;
Expand All @@ -10,6 +11,7 @@
using osu.Framework.Graphics.Sprites;
using osu.Framework.Graphics.UserInterface;
using osu.Framework.Input.Events;
using osu.Framework.Localisation;
using osu.Game.Database;
using osu.Game.Graphics;
using osu.Game.Graphics.Containers;
Expand All @@ -23,7 +25,7 @@ namespace osu.Game.Collections
/// <summary>
/// Visualises a <see cref="BeatmapCollection"/> inside a <see cref="DrawableCollectionList"/>.
/// </summary>
public partial class DrawableCollectionListItem : OsuRearrangeableListItem<Live<BeatmapCollection>>
public partial class DrawableCollectionListItem : OsuRearrangeableListItem<Live<BeatmapCollection>>, IFilterable
{
private const float item_height = 35;
private const float button_width = item_height * 0.75f;
Expand Down Expand Up @@ -207,5 +209,25 @@ protected override bool OnClick(ClickEvent e)

private void deleteCollection() => collection.PerformWrite(c => c.Realm!.Remove(c));
}

public IEnumerable<LocalisableString> FilterTerms => [(LocalisableString)Model.Value.Name];

private bool matchingFilter = true;

public bool MatchingFilter
{
get => matchingFilter;
set
{
matchingFilter = value;

if (matchingFilter)
this.FadeIn(200);
else
Hide();
}
}

public bool FilteringActive { get; set; }
}
}
37 changes: 35 additions & 2 deletions osu.Game/Collections/ManageCollectionsDialog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
using osu.Game.Graphics.Sprites;
using osu.Game.Graphics.UserInterface;
using osu.Game.Overlays;
using osu.Game.Resources.Localisation.Web;
using osuTK;

namespace osu.Game.Collections
Expand All @@ -26,6 +27,9 @@ public partial class ManageCollectionsDialog : OsuFocusedOverlayContainer

private IDisposable? duckOperation;

private BasicSearchTextBox searchTextBox = null!;
private DrawableCollectionList list = null!;

[Resolved]
private MusicController? musicController { get; set; }

Expand Down Expand Up @@ -104,10 +108,29 @@ private void load(OsuColour colours)
RelativeSizeAxes = Axes.Both,
Colour = colours.GreySeaFoamDarker
},
new DrawableCollectionList
new FillFlowContainer
{
RelativeSizeAxes = Axes.Both,
}
Direction = FillDirection.Vertical,
Padding = new MarginPadding(10),
Spacing = new Vector2(0, 10),
Children = new Drawable[]
{
searchTextBox = new BasicSearchTextBox
{
RelativeSizeAxes = Axes.X,
Y = 10,
Height = 40,
ReleaseFocusOnCommit = false,
HoldFocus = true,
PlaceholderText = HomeStrings.SearchPlaceholder,
},
list = new DrawableCollectionList
{
RelativeSizeAxes = Axes.Both,
}
}
},
}
}
},
Expand All @@ -117,6 +140,16 @@ private void load(OsuColour colours)
};
}

protected override void LoadComplete()
{
base.LoadComplete();

searchTextBox.Current.BindValueChanged(_ =>
{
list.SearchTerm = searchTextBox.Current.Value;
});
}

protected override void Dispose(bool isDisposing)
{
base.Dispose(isDisposing);
Expand Down

0 comments on commit 5ce1f76

Please sign in to comment.