Files
HellionChat/ChatTwo/Chunk.cs
T
Dean Sheather 4701bb3f6d feat: clickable URLs in chat log
Adds a parsing step when constructing `Message` objects that scans the
message content for anything that looks URL-like, and inserts new
`TextChunk`s into the message content with a URIPayload set.

Hovering over a URL shows an on-hover effect. Clicking a URL opens it in
the default browser. Right clicking shows the hostname, with an option
to open and an option to copy the URL to the clipboard.
2024-04-09 15:40:27 +10:00

87 lines
2.2 KiB
C#
Executable File

using ChatTwo.Code;
using Dalamud.Game.Text.SeStringHandling;
using LiteDB;
namespace ChatTwo;
internal abstract class Chunk {
[BsonIgnore]
internal Message? Message { get; set; }
internal ChunkSource Source { get; set; }
internal Payload? Link { get; set; }
protected Chunk(ChunkSource source, Payload? link) {
this.Source = source;
this.Link = link;
}
internal SeString? GetSeString() => this.Source switch {
ChunkSource.None => null,
ChunkSource.Sender => this.Message?.SenderSource,
ChunkSource.Content => this.Message?.ContentSource,
_ => null,
};
/// <summary>
/// Get some basic text for use in generating hashes.
/// </summary>
internal string StringValue() {
switch (this) {
case TextChunk text:
return text.Content;
case IconChunk icon:
return icon.Icon.ToString();
default:
return "";
}
}
}
internal enum ChunkSource {
None,
Sender,
Content,
}
internal class TextChunk : Chunk {
internal ChatType? FallbackColour { get; set; }
internal uint? Foreground { get; set; }
internal uint? Glow { get; set; }
internal bool Italic { get; set; }
internal string Content { get; set; }
internal TextChunk(ChunkSource source, Payload? link, string content) : base(source, link) {
this.Content = content;
}
#pragma warning disable CS8618
public TextChunk() : base(ChunkSource.None, null) {
}
#pragma warning restore CS8618
/// <summary>
/// Creates a new TextChunk with identical styling to this one.
/// </summary>
public TextChunk NewWithStyle(ChunkSource source, Payload? link, string content)
{
return new TextChunk(source, link, content) {
FallbackColour = this.FallbackColour,
Foreground = this.Foreground,
Glow = this.Glow,
Italic = this.Italic,
};
}
}
internal class IconChunk : Chunk {
internal BitmapFontIcon Icon { get; set; }
public IconChunk(ChunkSource source, Payload? link, BitmapFontIcon icon) : base(source, link) {
this.Icon = icon;
}
public IconChunk() : base(ChunkSource.None, null) {
}
}