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"
|
2021-01-02 09:24:14 +00:00
|
|
|
"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"
|
2021-03-29 21:46:52 +00:00
|
|
|
"github.com/diamondburned/cchat/text"
|
2020-06-06 07:44:36 +00:00
|
|
|
"github.com/gotk3/gotk3/gtk"
|
2021-01-02 09:24:14 +00:00
|
|
|
"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 {
|
2021-03-29 21:46:52 +00:00
|
|
|
message.Presender
|
2020-06-07 04:27:28 +00:00
|
|
|
Message
|
2020-06-06 07:44:36 +00:00
|
|
|
}
|
|
|
|
|
2021-03-29 21:46:52 +00:00
|
|
|
func WrapPresendMessage(pstate *message.PresendState) PresendMessage {
|
2020-06-06 07:44:36 +00:00
|
|
|
return PresendMessage{
|
2021-03-29 21:46:52 +00:00
|
|
|
Presender: pstate,
|
|
|
|
Message: WrapMessage(pstate.State),
|
2020-06-06 07:44:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type Message struct {
|
2021-03-29 21:46:52 +00:00
|
|
|
*message.State
|
2021-01-05 02:05:33 +00:00
|
|
|
Timestamp *gtk.Label
|
|
|
|
Username *labeluri.Label
|
2020-06-06 07:44:36 +00:00
|
|
|
|
2021-03-29 21:46:52 +00:00
|
|
|
unwrap func()
|
2020-06-06 07:44:36 +00:00
|
|
|
}
|
|
|
|
|
2021-03-29 21:46:52 +00:00
|
|
|
var _ container.MessageRow = (*Message)(nil)
|
2020-06-06 07:44:36 +00:00
|
|
|
|
2021-03-29 21:46:52 +00:00
|
|
|
func WrapMessage(ct *message.State) Message {
|
2021-01-05 02:05:33 +00:00
|
|
|
ts := message.NewTimestamp()
|
|
|
|
ts.SetVAlign(gtk.ALIGN_START)
|
2021-03-29 21:46:52 +00:00
|
|
|
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-02 09:24:14 +00:00
|
|
|
|
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")
|
|
|
|
|
2021-03-29 21:46:52 +00:00
|
|
|
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{
|
2021-03-29 21:46:52 +00:00
|
|
|
State: ct,
|
|
|
|
Timestamp: ts,
|
|
|
|
Username: user,
|
|
|
|
unwrap: ct.Author.Name.OnUpdate(func() {
|
|
|
|
user.SetLabel(ct.Author.Name.Label())
|
|
|
|
}),
|
2021-01-02 09:24:14 +00:00
|
|
|
}
|
2021-01-05 02:05:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// SetReferenceHighlighter sets the reference highlighter into the message.
|
|
|
|
func (m Message) SetReferenceHighlighter(r labeluri.ReferenceHighlighter) {
|
2021-03-29 21:46:52 +00:00
|
|
|
m.State.SetReferenceHighlighter(r)
|
2021-01-05 02:05:33 +00:00
|
|
|
m.Username.SetReferenceHighlighter(r)
|
|
|
|
}
|
|
|
|
|
2021-05-01 07:26:17 +00:00
|
|
|
func (m Message) Revert() *message.State {
|
|
|
|
m.unwrap()
|
|
|
|
m.ClearBox()
|
2021-01-02 09:24:14 +00:00
|
|
|
|
2021-05-01 07:26:17 +00:00
|
|
|
return m.Unwrap()
|
2020-06-06 07:44:36 +00:00
|
|
|
}
|