Skip to content

Commit

Permalink
Merge pull request #29314 from peppy/highlight-username-lines
Browse files Browse the repository at this point in the history
Highlight mentions in chat
  • Loading branch information
smoogipoo authored Aug 7, 2024
2 parents d637bbf + a61bf67 commit 91e8772
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 3 deletions.
28 changes: 28 additions & 0 deletions osu.Game.Tests/Visual/Online/TestSceneDrawableChannel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,34 @@ public void SetUpSteps()
});
}

[Test]
public void TestMention()
{
AddStep("add normal message", () => channel.AddNewMessages(
new Message(1)
{
Sender = new APIUser
{
Id = 2,
Username = "TestUser2"
},
Content = "Hello how are you today?",
Timestamp = new DateTimeOffset(2021, 12, 11, 13, 33, 24, TimeSpan.Zero)
}));

AddStep("add mention", () => channel.AddNewMessages(
new Message(2)
{
Sender = new APIUser
{
Id = 2,
Username = "TestUser2"
},
Content = $"Hello {API.LocalUser.Value.Username} how are you today?",
Timestamp = new DateTimeOffset(2021, 12, 11, 13, 33, 25, TimeSpan.Zero)
}));
}

[Test]
public void TestDaySeparators()
{
Expand Down
20 changes: 17 additions & 3 deletions osu.Game/Overlays/Chat/ChatLine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
using osu.Game.Graphics;
using osu.Game.Graphics.Containers;
using osu.Game.Graphics.Sprites;
using osu.Game.Online.API;
using osu.Game.Online.API.Requests.Responses;
using osu.Game.Online.Chat;
using osuTK;
Expand Down Expand Up @@ -104,6 +105,8 @@ public bool AlternatingBackground
}
}

private bool isMention;

/// <summary>
/// The colour used to paint the author's username.
/// </summary>
Expand Down Expand Up @@ -255,12 +258,21 @@ public void Highlight()
private void styleMessageContent(SpriteText text)
{
text.Shadow = false;
text.Font = text.Font.With(size: FontSize, italics: Message.IsAction);
text.Font = text.Font.With(size: FontSize, italics: Message.IsAction, weight: isMention ? FontWeight.SemiBold : FontWeight.Medium);

Color4 messageColour = colourProvider?.Content1 ?? Colour4.White;

if (isMention)
messageColour = colourProvider?.Highlight1 ?? Color4.Orange;
else if (Message.IsAction && !string.IsNullOrEmpty(message.Sender.Colour))
messageColour = Color4Extensions.FromHex(message.Sender.Colour);

bool messageHasColour = Message.IsAction && !string.IsNullOrEmpty(message.Sender.Colour);
text.Colour = messageHasColour ? Color4Extensions.FromHex(message.Sender.Colour) : colourProvider?.Content1 ?? Colour4.White;
text.Colour = messageColour;
}

[Resolved]
private IAPIProvider api { get; set; } = null!;

private void updateMessageContent()
{
this.FadeTo(message is LocalEchoMessage ? 0.4f : 1.0f, 500, Easing.OutQuint);
Expand All @@ -280,6 +292,8 @@ private void updateMessageContent()
// remove non-existent channels from the link list
message.Links.RemoveAll(link => link.Action == LinkAction.OpenChannel && chatManager?.AvailableChannels.Any(c => c.Name == link.Argument.ToString()) != true);

isMention = MessageNotifier.CheckContainsUsername(message.DisplayContent, api.LocalUser.Value.Username);

drawableContentFlow.Clear();
drawableContentFlow.AddLinks(message.DisplayContent, message.Links);
}
Expand Down

0 comments on commit 91e8772

Please sign in to comment.