Add keyboard shortcuts to ChatOverlayV2 with tests

This commit is contained in:
Jai Sharma 2022-05-24 23:05:25 +01:00
parent fce527b0df
commit f6810d3f59
2 changed files with 122 additions and 2 deletions

View File

@ -12,6 +12,7 @@ using osu.Framework.Allocation;
using osu.Framework.Bindables;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Framework.Input;
using osu.Framework.Logging;
using osu.Framework.Testing;
using osu.Framework.Utils;
@ -417,6 +418,67 @@ namespace osu.Game.Tests.Visual.Online
AddAssert("Channel 1 displayed", () => channelIsVisible && currentDrawableChannel.Channel == testChannel1);
}
[Test]
public void TestKeyboardCloseAndRestoreChannel()
{
AddStep("Show overlay with channel 1", () =>
{
channelManager.JoinChannel(testChannel1);
chatOverlay.Show();
});
AddAssert("Channel 1 displayed", () => channelIsVisible && currentDrawableChannel.Channel == testChannel1);
AddStep("Press document close keys", () => InputManager.Keys(PlatformAction.DocumentClose));
AddAssert("Listing is visible", () => listingIsVisible);
AddStep("Press tab restore keys", () => InputManager.Keys(PlatformAction.TabRestore));
AddAssert("Channel 1 displayed", () => channelIsVisible && currentDrawableChannel.Channel == testChannel1);
}
[Test]
public void TestKeyboardNewChannel()
{
AddStep("Show overlay with channel 1", () =>
{
channelManager.JoinChannel(testChannel1);
chatOverlay.Show();
});
AddAssert("Channel 1 displayed", () => channelIsVisible && currentDrawableChannel.Channel == testChannel1);
AddStep("Press tab new keys", () => InputManager.Keys(PlatformAction.TabNew));
AddAssert("Listing is visible", () => listingIsVisible);
}
[Test]
public void TestKeyboardNextChannel()
{
Channel pmChannel1 = createPrivateChannel();
Channel pmChannel2 = createPrivateChannel();
AddStep("Show overlay with channels", () =>
{
channelManager.JoinChannel(testChannel1);
channelManager.JoinChannel(testChannel2);
channelManager.JoinChannel(pmChannel1);
channelManager.JoinChannel(pmChannel2);
chatOverlay.Show();
});
AddAssert("Channel 1 displayed", () => channelIsVisible && currentDrawableChannel.Channel == testChannel1);
AddStep("Press document next keys", () => InputManager.Keys(PlatformAction.DocumentNext));
AddAssert("Channel 2 displayed", () => channelIsVisible && currentDrawableChannel.Channel == testChannel2);
AddStep("Press document next keys", () => InputManager.Keys(PlatformAction.DocumentNext));
AddAssert("PM Channel 1 displayed", () => channelIsVisible && currentDrawableChannel.Channel == pmChannel1);
AddStep("Press document next keys", () => InputManager.Keys(PlatformAction.DocumentNext));
AddAssert("PM Channel 2 displayed", () => channelIsVisible && currentDrawableChannel.Channel == pmChannel2);
AddStep("Press document next keys", () => InputManager.Keys(PlatformAction.DocumentNext));
AddAssert("Channel 1 displayed", () => channelIsVisible && currentDrawableChannel.Channel == testChannel1);
}
private bool listingIsVisible =>
chatOverlay.ChildrenOfType<ChannelListing>().Single().State.Value == Visibility.Visible;
@ -467,6 +529,16 @@ namespace osu.Game.Tests.Visual.Online
Type = ChannelType.Public,
};
private Channel createPrivateChannel()
{
int id = RNG.Next(0, 10000);
return new Channel(new APIUser
{
Id = id,
Username = $"test user {id}",
});
}
private class TestChatOverlayV2 : ChatOverlayV2
{
public bool SlowLoading { get; set; }

View File

@ -13,6 +13,8 @@ using osu.Framework.Bindables;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Framework.Graphics.Shapes;
using osu.Framework.Input;
using osu.Framework.Input.Bindings;
using osu.Framework.Input.Events;
using osu.Framework.Localisation;
using osu.Game.Configuration;
@ -26,7 +28,7 @@ using osu.Game.Overlays.Chat.Listing;
namespace osu.Game.Overlays
{
public class ChatOverlayV2 : OsuFocusedOverlayContainer, INamedOverlayComponent
public class ChatOverlayV2 : OsuFocusedOverlayContainer, INamedOverlayComponent, IKeyBindingHandler<PlatformAction>
{
public string IconTexture => "Icons/Hexacons/messaging";
public LocalisableString Title => ChatStrings.HeaderTitle;
@ -197,6 +199,35 @@ namespace osu.Game.Overlays
Show();
}
public bool OnPressed(KeyBindingPressEvent<PlatformAction> e)
{
switch (e.Action)
{
case PlatformAction.TabNew:
currentChannel.Value = channelList.ChannelListingChannel;
return true;
case PlatformAction.DocumentClose:
channelManager.LeaveChannel(currentChannel.Value);
return true;
case PlatformAction.TabRestore:
channelManager.JoinLastClosedChannel();
return true;
case PlatformAction.DocumentNext:
cycleChannel();
return true;
default:
return false;
}
}
public void OnReleased(KeyBindingReleaseEvent<PlatformAction> e)
{
}
protected override bool OnDragStart(DragStartEvent e)
{
isDraggingTopBar = topBar.IsHovered;
@ -341,7 +372,7 @@ namespace osu.Game.Overlays
private void availableChannelsChanged(object sender, NotifyCollectionChangedEventArgs args)
=> channelListing.UpdateAvailableChannels(channelManager.AvailableChannels);
private IEnumerable<Channel> filterChannels(IList channels)
private IEnumerable<Channel> filterChannels(IEnumerable channels)
=> channels.Cast<Channel>().Where(c => c.Type == ChannelType.Public || c.Type == ChannelType.PM);
private void handleChatMessage(string message)
@ -354,5 +385,22 @@ namespace osu.Game.Overlays
else
channelManager.PostMessage(message);
}
private void cycleChannel()
{
List<Channel> overlayChannels = filterChannels(channelManager.JoinedChannels).ToList();
if (overlayChannels.Count < 2)
return;
int currentIdx = overlayChannels.IndexOf(currentChannel.Value);
int nextIdx = currentIdx + 1;
// Cycle the list when reaching the end
if (nextIdx > overlayChannels.Count - 1)
nextIdx = 0;
currentChannel.Value = overlayChannels[nextIdx];
}
}
}