Skip to content

Commit

Permalink
Merge pull request #30382 from Maks1mio/ChatChannelListSearch
Browse files Browse the repository at this point in the history
Add search box to chat overlay
  • Loading branch information
peppy authored Oct 22, 2024
2 parents c15490e + 54aeeaa commit 213be02
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 3 deletions.
28 changes: 28 additions & 0 deletions osu.Game.Tests/Visual/Online/TestSceneChatOverlay.cs
Original file line number Diff line number Diff line change
Expand Up @@ -648,6 +648,34 @@ public void TestChatReport()
AddUntilStep("Info message displayed", () => channelManager.CurrentChannel.Value.Messages.Last(), () => Is.InstanceOf(typeof(InfoMessage)));
}

[Test]
public void TestFiltering()
{
AddStep("Show overlay", () => chatOverlay.Show());
joinTestChannel(1);
joinTestChannel(3);
joinTestChannel(5);
joinChannel(new Channel(new APIUser { Id = 2001, Username = "alice" }));
joinChannel(new Channel(new APIUser { Id = 2002, Username = "bob" }));
joinChannel(new Channel(new APIUser { Id = 2003, Username = "charley the plant" }));

AddStep("filter to \"c\"", () => chatOverlay.ChildrenOfType<SearchTextBox>().Single().Text = "c");
AddUntilStep("bob filtered out", () => chatOverlay.ChildrenOfType<ChannelListItem>().Count(i => i.Alpha > 0), () => Is.EqualTo(5));

AddStep("filter to \"channel\"", () => chatOverlay.ChildrenOfType<SearchTextBox>().Single().Text = "channel");
AddUntilStep("only public channels left", () => chatOverlay.ChildrenOfType<ChannelListItem>().Count(i => i.Alpha > 0), () => Is.EqualTo(3));

AddStep("commit textbox", () =>
{
chatOverlay.ChildrenOfType<SearchTextBox>().Single().TakeFocus();
Schedule(() => InputManager.PressKey(Key.Enter));
});
AddUntilStep("#channel-2 active", () => channelManager.CurrentChannel.Value.Name, () => Is.EqualTo("#channel-2"));

AddStep("filter to \"channel-3\"", () => chatOverlay.ChildrenOfType<SearchTextBox>().Single().Text = "channel-3");
AddUntilStep("no channels left", () => chatOverlay.ChildrenOfType<ChannelListItem>().Count(i => i.Alpha > 0), () => Is.EqualTo(0));
}

private void joinTestChannel(int i)
{
AddStep($"Join test channel {i}", () => channelManager.JoinChannel(testChannels[i]));
Expand Down
44 changes: 42 additions & 2 deletions osu.Game/Overlays/Chat/ChannelList/ChannelList.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,17 @@
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Framework.Graphics.Shapes;
using osu.Framework.Graphics.UserInterface;
using osu.Framework.Localisation;
using osu.Framework.Testing;
using osu.Game.Graphics;
using osu.Game.Graphics.Containers;
using osu.Game.Graphics.Sprites;
using osu.Game.Graphics.UserInterface;
using osu.Game.Online.Chat;
using osu.Game.Overlays.Chat.Listing;
using osu.Game.Resources.Localisation.Web;
using osuTK;

namespace osu.Game.Overlays.Chat.ChannelList
{
Expand All @@ -34,11 +38,12 @@ public partial class ChannelList : Container
private readonly Dictionary<Channel, ChannelListItem> channelMap = new Dictionary<Channel, ChannelListItem>();

private OsuScrollContainer scroll = null!;
private FillFlowContainer groupFlow = null!;
private SearchContainer groupFlow = null!;
private ChannelGroup announceChannelGroup = null!;
private ChannelGroup publicChannelGroup = null!;
private ChannelGroup privateChannelGroup = null!;
private ChannelListItem selector = null!;
private TextBox searchTextBox = null!;

[BackgroundDependencyLoader]
private void load(OverlayColourProvider colourProvider)
Expand All @@ -55,13 +60,23 @@ private void load(OverlayColourProvider colourProvider)
RelativeSizeAxes = Axes.Both,
ScrollbarAnchor = Anchor.TopRight,
ScrollDistance = 35f,
Child = groupFlow = new FillFlowContainer
Child = groupFlow = new SearchContainer
{
Direction = FillDirection.Vertical,
RelativeSizeAxes = Axes.X,
AutoSizeAxes = Axes.Y,
Children = new Drawable[]
{
new Container
{
RelativeSizeAxes = Axes.X,
AutoSizeAxes = Axes.Y,
Padding = new MarginPadding { Horizontal = 10, Top = 8 },
Child = searchTextBox = new ChannelSearchTextBox
{
RelativeSizeAxes = Axes.X,
}
},
announceChannelGroup = new ChannelGroup(ChatStrings.ChannelsListTitleANNOUNCE.ToUpper()),
publicChannelGroup = new ChannelGroup(ChatStrings.ChannelsListTitlePUBLIC.ToUpper()),
selector = new ChannelListItem(ChannelListingChannel),
Expand All @@ -71,6 +86,19 @@ private void load(OverlayColourProvider colourProvider)
},
};

searchTextBox.Current.BindValueChanged(_ => groupFlow.SearchTerm = searchTextBox.Current.Value, true);
searchTextBox.OnCommit += (_, _) =>
{
if (string.IsNullOrEmpty(searchTextBox.Current.Value))
return;

var firstMatchingItem = this.ChildrenOfType<ChannelListItem>().FirstOrDefault(item => item.MatchingFilter);
if (firstMatchingItem == null)
return;

OnRequestSelect?.Invoke(firstMatchingItem.Channel);
};

selector.OnRequestSelect += chan => OnRequestSelect?.Invoke(chan);
}

Expand Down Expand Up @@ -168,5 +196,17 @@ public ChannelGroup(LocalisableString label)
};
}
}

private partial class ChannelSearchTextBox : BasicSearchTextBox
{
protected override bool AllowCommit => true;

public ChannelSearchTextBox()
{
const float scale_factor = 0.8f;
Scale = new Vector2(scale_factor);
Width = 1 / scale_factor;
}
}
}
}
27 changes: 26 additions & 1 deletion osu.Game/Overlays/Chat/ChannelList/ChannelListItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@
// See the LICENCE file in the repository root for full licence text.

using System;
using System.Collections.Generic;
using System.Linq;
using osu.Framework.Allocation;
using osu.Framework.Bindables;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Framework.Graphics.Shapes;
using osu.Framework.Input.Events;
using osu.Framework.Localisation;
using osu.Game.Graphics;
using osu.Game.Graphics.Containers;
using osu.Game.Graphics.Sprites;
Expand All @@ -19,7 +21,7 @@

namespace osu.Game.Overlays.Chat.ChannelList
{
public partial class ChannelListItem : OsuClickableContainer
public partial class ChannelListItem : OsuClickableContainer, IFilterable
{
public event Action<Channel>? OnRequestSelect;
public event Action<Channel>? OnRequestLeave;
Expand Down Expand Up @@ -186,5 +188,28 @@ private void updateState()
}

private bool isSelector => Channel is ChannelListing.ChannelListingChannel;

#region Filtering support

public IEnumerable<LocalisableString> FilterTerms => isSelector ? Enumerable.Empty<LocalisableString>() : [Channel.Name];

private bool matchingFilter = true;

public bool MatchingFilter
{
get => matchingFilter;
set
{
if (matchingFilter == value)
return;

matchingFilter = value;
Alpha = matchingFilter ? 1 : 0;
}
}

public bool FilteringActive { get; set; }

#endregion
}
}

0 comments on commit 213be02

Please sign in to comment.