1
0
Fork 0
mirror of https://github.com/diamondburned/cchat-gtk.git synced 2024-11-17 03:32:56 +00:00
cchat-gtk/internal/ui/messages/container/compact/message.go

111 lines
2.7 KiB
Go
Raw Normal View History

2020-06-06 07:44:36 +00:00
package compact
import (
2021-01-05 02:05:33 +00:00
"time"
2020-06-06 07:44:36 +00:00
"github.com/diamondburned/cchat"
2021-01-05 02:05:33 +00:00
"github.com/diamondburned/cchat-gtk/internal/humanize"
2020-06-06 07:44:36 +00:00
"github.com/diamondburned/cchat-gtk/internal/ui/messages/container"
"github.com/diamondburned/cchat-gtk/internal/ui/messages/input"
"github.com/diamondburned/cchat-gtk/internal/ui/messages/message"
"github.com/diamondburned/cchat-gtk/internal/ui/primitives"
2021-01-05 02:05:33 +00:00
"github.com/diamondburned/cchat-gtk/internal/ui/rich/labeluri"
"github.com/diamondburned/cchat-gtk/internal/ui/rich/parser/markup"
2020-06-06 07:44:36 +00:00
"github.com/gotk3/gotk3/gtk"
"github.com/gotk3/gotk3/pango"
2020-06-06 07:44:36 +00:00
)
2021-01-05 02:05:33 +00:00
var messageTimeCSS = primitives.PrepareClassCSS("", `
.message-time {
margin-left: 1em;
margin-right: 1em;
}
`)
var messageAuthorCSS = primitives.PrepareClassCSS("", `
.message-author {
margin-right: 0.5em;
}
`)
2020-06-06 07:44:36 +00:00
type PresendMessage struct {
2020-06-07 04:27:28 +00:00
message.PresendContainer
Message
2020-06-06 07:44:36 +00:00
}
func NewPresendMessage(msg input.PresendMessage) PresendMessage {
msgc := message.NewPresendContainer(msg)
2020-06-07 04:27:28 +00:00
2020-06-06 07:44:36 +00:00
return PresendMessage{
2020-06-07 04:27:28 +00:00
PresendContainer: msgc,
2021-01-05 02:05:33 +00:00
Message: wrapMessage(msgc.GenericContainer),
2020-06-06 07:44:36 +00:00
}
}
type Message struct {
*message.GenericContainer
2021-01-05 02:05:33 +00:00
Timestamp *gtk.Label
Username *labeluri.Label
2020-06-06 07:44:36 +00:00
}
var _ container.MessageRow = (*Message)(nil)
2020-06-06 07:44:36 +00:00
func NewMessage(msg cchat.MessageCreate) Message {
2021-01-05 02:05:33 +00:00
msgc := wrapMessage(message.NewContainer(msg))
2020-06-07 04:27:28 +00:00
message.FillContainer(msgc, msg)
2021-01-05 02:05:33 +00:00
return msgc
2020-06-06 07:44:36 +00:00
}
func NewEmptyMessage() Message {
ct := message.NewEmptyContainer()
2021-01-05 02:05:33 +00:00
return wrapMessage(ct)
2020-06-06 07:44:36 +00:00
}
2021-01-05 02:05:33 +00:00
func wrapMessage(ct *message.GenericContainer) Message {
ts := message.NewTimestamp()
ts.SetVAlign(gtk.ALIGN_START)
ts.Show()
messageTimeCSS(ts)
2021-01-05 02:05:33 +00:00
user := message.NewUsername()
user.SetMaxWidthChars(25)
user.SetEllipsize(pango.ELLIPSIZE_NONE)
user.SetLineWrap(true)
user.SetLineWrapMode(pango.WRAP_WORD_CHAR)
user.Show()
messageAuthorCSS(user)
ct.PackStart(ts, false, false, 0)
ct.PackStart(user, false, false, 0)
ct.PackStart(ct.Content, true, true, 0)
ct.SetClass("compact")
return Message{
GenericContainer: ct,
Timestamp: ts,
Username: user,
}
2021-01-05 02:05:33 +00:00
}
// SetReferenceHighlighter sets the reference highlighter into the message.
func (m Message) SetReferenceHighlighter(r labeluri.ReferenceHighlighter) {
m.GenericContainer.SetReferenceHighlighter(r)
m.Username.SetReferenceHighlighter(r)
}
func (m Message) UpdateTimestamp(t time.Time) {
m.GenericContainer.UpdateTimestamp(t)
m.Timestamp.SetText(humanize.TimeAgo(t))
m.Timestamp.SetTooltipText(t.Format(time.Stamp))
}
2021-01-05 02:05:33 +00:00
func (m Message) UpdateAuthor(author cchat.Author) {
m.GenericContainer.UpdateAuthor(author)
2021-01-05 02:05:33 +00:00
cfg := markup.RenderConfig{}
cfg.NoReferencing = true
cfg.SetForegroundAnchor(m.ContentBodyStyle)
2021-01-05 02:05:33 +00:00
m.Username.SetOutput(markup.RenderCmplxWithConfig(author.Name(), cfg))
2020-06-06 07:44:36 +00:00
}