2023-01-02 11:55:59 -05:00
|
|
|
using GraphicsManager.Enums;
|
|
|
|
using GraphicsManager.Interfaces;
|
|
|
|
using GraphicsManager.Objects;
|
|
|
|
using GraphicsManager.Objects.Core;
|
|
|
|
using Luski.net.Interfaces;
|
|
|
|
using Luski.net.JsonTypes;
|
2023-01-02 12:48:35 -05:00
|
|
|
using File = Luski.net.JsonTypes.File;
|
2023-01-02 11:55:59 -05:00
|
|
|
|
|
|
|
namespace Luski.GUI.MainScreen.UI;
|
|
|
|
|
|
|
|
public class ChatMessage : UserControl
|
|
|
|
{
|
|
|
|
readonly int padding = 2;
|
|
|
|
private SocketMessage Msg { get; }
|
|
|
|
private Label label1, label2;
|
|
|
|
|
|
|
|
public ChatMessage(SocketMessage message)
|
|
|
|
{
|
|
|
|
Msg = message;
|
|
|
|
IUser user = message.GetAuthor().Result;
|
|
|
|
Anchor = ObjectAnchor.Left | ObjectAnchor.Right;
|
|
|
|
|
|
|
|
Controls.Add(label1 = new Label() {Location = new(83, 9), Text = user.Username});
|
|
|
|
Controls.Add(label2 = new Label() {Location = new(83, 40), Text = message.Context});
|
|
|
|
|
|
|
|
if (Msg.Files != null && Msg.Files.Length > 0)
|
|
|
|
{
|
|
|
|
for (int i = 0; i < Msg.Files.Length; i++)
|
|
|
|
{
|
2023-01-02 12:48:35 -05:00
|
|
|
Controls.Add(new ContentEmbed(Msg.Files[i], Msg.ChannelID));
|
|
|
|
}
|
2023-01-02 11:55:59 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
DateTime time = new DateTime(2022, 1, 1, 0, 0, 0, 0).AddMilliseconds(Msg.Id >> 22).ToLocalTime();
|
|
|
|
string timestr;
|
|
|
|
if (time.Date == DateTime.Now.ToLocalTime().Date)
|
|
|
|
{
|
|
|
|
timestr = $"Today at {time.ToShortTimeString()}";
|
|
|
|
}
|
|
|
|
else if (time.Date == DateTime.Now.ToLocalTime().AddDays(-1).Date)
|
|
|
|
{
|
|
|
|
timestr = $"Yesterday at {time.ToShortTimeString()}";
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
timestr = $"{time:M/dd/yyyy h:mm tt}";
|
|
|
|
}
|
|
|
|
Controls.Add(new Label() {Location = new(label1.Location.X + label1.Size.X + 4, 17), Text = timestr});
|
|
|
|
Controls.Add(new Label() {Location = new(129, 17), Text = message.Context});
|
|
|
|
Controls.Add(new Rectangle(new Texture(user.GetAvatar().Result)) { Location = new(15, 3), Size = new(58, 58) });
|
|
|
|
}
|
|
|
|
|
|
|
|
public void AddMessage(SocketMessage msg)
|
|
|
|
{
|
|
|
|
Label newLabel = new()
|
|
|
|
{
|
|
|
|
Text = msg.Context,
|
|
|
|
Tag = msg,
|
|
|
|
Location = new(label2.Location.X, Size.Y)
|
|
|
|
};
|
|
|
|
|
|
|
|
newLabel.MouseEnter += NewLabel_MouseEnter;
|
|
|
|
newLabel.MouseLeave += NewLabel_MouseLeave;
|
|
|
|
Controls.Add(newLabel);
|
|
|
|
if (msg.Files != null && msg.Files.Length > 0)
|
|
|
|
{
|
|
|
|
for (int i = 0; i < msg.Files.Length; i++)
|
|
|
|
{
|
2023-01-02 12:48:35 -05:00
|
|
|
Controls.Add(new ContentEmbed(msg.Files[i], Msg.ChannelID));
|
2023-01-02 11:55:59 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
readonly List<Label> Labels = new();
|
|
|
|
private Task NewLabel_MouseLeave(IRenderObject sender)
|
|
|
|
{
|
|
|
|
if (sender is not Label label) return Task.CompletedTask;
|
|
|
|
if (label.Tag is not SocketMessage Message) return Task.CompletedTask;
|
|
|
|
DateTime time = new DateTime(2022, 1, 1, 0, 0, 0, 0).AddMilliseconds(Message.Id >> 22).ToLocalTime();
|
|
|
|
string b = time.ToString("h:mm tt");
|
|
|
|
IEnumerable<Label>? l = Labels.Where(s => s.Text == b);
|
|
|
|
if (l is not null && l.Any())
|
|
|
|
{
|
|
|
|
Controls.Remove(l.First());
|
|
|
|
Labels.Remove(l.First());
|
|
|
|
}
|
|
|
|
|
|
|
|
return Task.CompletedTask;
|
|
|
|
}
|
|
|
|
|
|
|
|
private Task NewLabel_MouseEnter(IRenderObject sender)
|
|
|
|
{
|
|
|
|
if (sender is not Label label) return Task.CompletedTask;
|
|
|
|
if (label.Tag is not SocketMessage Message) return Task.CompletedTask;
|
|
|
|
DateTime time = new DateTime(2022, 1, 1, 0, 0, 0, 0).AddMilliseconds(Message.Id >> 22).ToLocalTime();
|
|
|
|
Label m = new()
|
|
|
|
{
|
|
|
|
Text = time.ToString("h:mm tt"),
|
|
|
|
Location = new(15, label.Location.Y + 1),
|
|
|
|
};
|
|
|
|
|
|
|
|
Controls.Add(m);
|
|
|
|
Labels.Add(m);
|
|
|
|
return Task.CompletedTask;
|
|
|
|
}
|
|
|
|
}
|