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

103 lines
2.3 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"
"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/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"
"github.com/diamondburned/cchat/text"
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 {
message.Presender
2020-06-07 04:27:28 +00:00
Message
2020-06-06 07:44:36 +00:00
}
func WrapPresendMessage(pstate *message.PresendState) PresendMessage {
2020-06-06 07:44:36 +00:00
return PresendMessage{
Presender: pstate,
Message: WrapMessage(pstate.State),
2020-06-06 07:44:36 +00:00
}
}
type Message struct {
*message.State
2021-01-05 02:05:33 +00:00
Timestamp *gtk.Label
Username *labeluri.Label
2020-06-06 07:44:36 +00:00
unwrap func()
2020-06-06 07:44:36 +00:00
}
var _ container.MessageRow = (*Message)(nil)
2020-06-06 07:44:36 +00:00
func WrapMessage(ct *message.State) Message {
2021-01-05 02:05:33 +00:00
ts := message.NewTimestamp()
ts.SetVAlign(gtk.ALIGN_START)
ts.SetText(humanize.TimeAgo(ct.Time))
ts.SetTooltipText(ct.Time.Format(time.Stamp))
2021-01-05 02:05:33 +00:00
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")
rcfg := markup.RenderConfig{}
rcfg.NoReferencing = true
rcfg.SetForegroundAnchor(ct.ContentBodyStyle)
user.SetRenderer(func(rich text.Rich) markup.RenderOutput {
return markup.RenderCmplxWithConfig(rich, rcfg)
})
2021-01-05 02:05:33 +00:00
return Message{
State: ct,
Timestamp: ts,
Username: user,
unwrap: ct.Author.Name.OnUpdate(func() {
user.SetLabel(ct.Author.Name.Label())
}),
}
2021-01-05 02:05:33 +00:00
}
// SetReferenceHighlighter sets the reference highlighter into the message.
func (m Message) SetReferenceHighlighter(r labeluri.ReferenceHighlighter) {
m.State.SetReferenceHighlighter(r)
2021-01-05 02:05:33 +00:00
m.Username.SetReferenceHighlighter(r)
}
func (m Message) Revert() *message.State {
m.unwrap()
m.ClearBox()
return m.Unwrap()
2020-06-06 07:44:36 +00:00
}