2020-06-06 07:44:36 +00:00
|
|
|
package message
|
2020-05-26 06:51:06 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/diamondburned/cchat"
|
|
|
|
"github.com/diamondburned/cchat-gtk/internal/humanize"
|
2020-06-06 00:47:28 +00:00
|
|
|
"github.com/diamondburned/cchat-gtk/internal/ui/rich/parser"
|
2020-05-26 06:51:06 +00:00
|
|
|
"github.com/diamondburned/cchat/text"
|
|
|
|
"github.com/gotk3/gotk3/gtk"
|
|
|
|
"github.com/gotk3/gotk3/pango"
|
|
|
|
)
|
|
|
|
|
2020-06-06 07:44:36 +00:00
|
|
|
type Container interface {
|
|
|
|
ID() string
|
|
|
|
AuthorID() string
|
|
|
|
Nonce() string
|
|
|
|
|
|
|
|
UpdateAuthor(cchat.MessageAuthor)
|
|
|
|
UpdateAuthorName(text.Rich)
|
|
|
|
UpdateContent(text.Rich)
|
|
|
|
UpdateTimestamp(time.Time)
|
|
|
|
}
|
|
|
|
|
2020-06-07 04:27:28 +00:00
|
|
|
func FillContainer(c Container, msg cchat.MessageCreate) {
|
|
|
|
c.UpdateAuthor(msg.Author())
|
|
|
|
c.UpdateContent(msg.Content())
|
|
|
|
c.UpdateTimestamp(msg.Time())
|
|
|
|
}
|
|
|
|
|
2020-06-06 07:44:36 +00:00
|
|
|
// GenericContainer provides a single generic message container for subpackages
|
|
|
|
// to use.
|
|
|
|
type GenericContainer struct {
|
|
|
|
id string
|
|
|
|
authorID string
|
|
|
|
nonce string
|
2020-05-26 06:51:06 +00:00
|
|
|
|
|
|
|
Timestamp *gtk.Label
|
|
|
|
Username *gtk.Label
|
|
|
|
Content *gtk.Label
|
|
|
|
}
|
|
|
|
|
2020-06-06 07:44:36 +00:00
|
|
|
var _ Container = (*GenericContainer)(nil)
|
|
|
|
|
2020-06-07 04:27:28 +00:00
|
|
|
// NewContainer creates a new message container with the given ID and nonce. It
|
|
|
|
// does not update the widgets, so FillContainer should be called afterwards.
|
2020-06-06 07:44:36 +00:00
|
|
|
func NewContainer(msg cchat.MessageCreate) *GenericContainer {
|
|
|
|
c := NewEmptyContainer()
|
|
|
|
c.id = msg.ID()
|
2020-06-07 04:27:28 +00:00
|
|
|
c.authorID = msg.Author().ID()
|
2020-06-04 23:00:41 +00:00
|
|
|
|
|
|
|
if noncer, ok := msg.(cchat.MessageNonce); ok {
|
2020-06-06 07:44:36 +00:00
|
|
|
c.nonce = noncer.Nonce()
|
2020-06-04 23:00:41 +00:00
|
|
|
}
|
|
|
|
|
2020-06-06 07:44:36 +00:00
|
|
|
return c
|
2020-06-04 23:00:41 +00:00
|
|
|
}
|
|
|
|
|
2020-06-06 07:44:36 +00:00
|
|
|
func NewEmptyContainer() *GenericContainer {
|
2020-05-26 06:51:06 +00:00
|
|
|
ts, _ := gtk.LabelNew("")
|
2020-05-28 19:26:55 +00:00
|
|
|
ts.SetLineWrap(true)
|
2020-05-26 06:51:06 +00:00
|
|
|
ts.SetLineWrapMode(pango.WRAP_WORD)
|
2020-05-28 19:26:55 +00:00
|
|
|
ts.SetHAlign(gtk.ALIGN_END)
|
|
|
|
ts.SetVAlign(gtk.ALIGN_START)
|
2020-06-04 23:00:41 +00:00
|
|
|
ts.SetSelectable(true)
|
2020-05-28 19:26:55 +00:00
|
|
|
ts.Show()
|
2020-05-26 06:51:06 +00:00
|
|
|
|
|
|
|
user, _ := gtk.LabelNew("")
|
2020-06-04 23:00:41 +00:00
|
|
|
user.SetMaxWidthChars(35)
|
2020-05-28 19:26:55 +00:00
|
|
|
user.SetLineWrap(true)
|
2020-05-26 06:51:06 +00:00
|
|
|
user.SetLineWrapMode(pango.WRAP_WORD_CHAR)
|
2020-05-28 19:26:55 +00:00
|
|
|
user.SetHAlign(gtk.ALIGN_END)
|
|
|
|
user.SetVAlign(gtk.ALIGN_START)
|
2020-06-04 23:00:41 +00:00
|
|
|
user.SetSelectable(true)
|
2020-05-28 19:26:55 +00:00
|
|
|
user.Show()
|
2020-05-26 06:51:06 +00:00
|
|
|
|
|
|
|
content, _ := gtk.LabelNew("")
|
2020-05-28 19:26:55 +00:00
|
|
|
content.SetHExpand(true)
|
|
|
|
content.SetXAlign(0) // left-align with size filled
|
|
|
|
content.SetVAlign(gtk.ALIGN_START)
|
|
|
|
content.SetLineWrap(true)
|
|
|
|
content.SetLineWrapMode(pango.WRAP_WORD_CHAR)
|
2020-06-04 23:00:41 +00:00
|
|
|
content.SetSelectable(true)
|
2020-05-26 06:51:06 +00:00
|
|
|
content.Show()
|
|
|
|
|
2020-06-06 07:44:36 +00:00
|
|
|
return &GenericContainer{
|
2020-05-26 06:51:06 +00:00
|
|
|
Timestamp: ts,
|
|
|
|
Username: user,
|
|
|
|
Content: content,
|
|
|
|
}
|
2020-06-04 23:00:41 +00:00
|
|
|
}
|
2020-05-26 06:51:06 +00:00
|
|
|
|
2020-06-06 07:44:36 +00:00
|
|
|
func (m *GenericContainer) ID() string {
|
|
|
|
return m.id
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *GenericContainer) AuthorID() string {
|
|
|
|
return m.authorID
|
2020-05-26 06:51:06 +00:00
|
|
|
}
|
|
|
|
|
2020-06-06 07:44:36 +00:00
|
|
|
func (m *GenericContainer) Nonce() string {
|
|
|
|
return m.nonce
|
2020-05-28 19:26:55 +00:00
|
|
|
}
|
|
|
|
|
2020-06-06 07:44:36 +00:00
|
|
|
func (m *GenericContainer) UpdateTimestamp(t time.Time) {
|
2020-05-26 06:51:06 +00:00
|
|
|
m.Timestamp.SetLabel(humanize.TimeAgo(t))
|
2020-06-04 23:00:41 +00:00
|
|
|
m.Timestamp.SetTooltipText(t.Format(time.Stamp))
|
|
|
|
}
|
|
|
|
|
2020-06-06 07:44:36 +00:00
|
|
|
func (m *GenericContainer) UpdateAuthor(author cchat.MessageAuthor) {
|
|
|
|
m.UpdateAuthorName(author.Name())
|
2020-05-26 06:51:06 +00:00
|
|
|
}
|
|
|
|
|
2020-06-06 07:44:36 +00:00
|
|
|
func (m *GenericContainer) UpdateAuthorName(name text.Rich) {
|
2020-06-06 00:47:28 +00:00
|
|
|
m.Username.SetMarkup(parser.RenderMarkup(name))
|
2020-05-26 06:51:06 +00:00
|
|
|
}
|
|
|
|
|
2020-06-06 07:44:36 +00:00
|
|
|
func (m *GenericContainer) UpdateContent(content text.Rich) {
|
2020-06-06 00:47:28 +00:00
|
|
|
m.Content.SetMarkup(parser.RenderMarkup(content))
|
2020-05-26 06:51:06 +00:00
|
|
|
}
|