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-28 01:35:26 +00:00
|
|
|
"github.com/diamondburned/cchat-gtk/internal/ui/imgview"
|
2020-06-13 07:29:32 +00:00
|
|
|
"github.com/diamondburned/cchat-gtk/internal/ui/primitives"
|
|
|
|
"github.com/diamondburned/cchat-gtk/internal/ui/rich"
|
2020-06-06 00:47:28 +00:00
|
|
|
"github.com/diamondburned/cchat-gtk/internal/ui/rich/parser"
|
2020-06-17 22:58:38 +00:00
|
|
|
"github.com/diamondburned/cchat-gtk/internal/ui/service/menu"
|
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
|
2020-06-13 07:29:32 +00:00
|
|
|
AvatarURL() string // avatar
|
2020-06-06 07:44:36 +00:00
|
|
|
Nonce() string
|
|
|
|
|
|
|
|
UpdateAuthor(cchat.MessageAuthor)
|
|
|
|
UpdateAuthorName(text.Rich)
|
2020-06-13 07:29:32 +00:00
|
|
|
UpdateContent(c text.Rich, edited bool)
|
2020-06-06 07:44:36 +00:00
|
|
|
UpdateTimestamp(time.Time)
|
|
|
|
}
|
|
|
|
|
2020-06-13 07:29:32 +00:00
|
|
|
// FillContainer sets the container's contents to the one from MessageCreate.
|
2020-06-07 04:27:28 +00:00
|
|
|
func FillContainer(c Container, msg cchat.MessageCreate) {
|
|
|
|
c.UpdateAuthor(msg.Author())
|
2020-06-13 07:29:32 +00:00
|
|
|
c.UpdateContent(msg.Content(), false)
|
2020-06-07 04:27:28 +00:00
|
|
|
c.UpdateTimestamp(msg.Time())
|
|
|
|
}
|
|
|
|
|
2020-06-13 07:29:32 +00:00
|
|
|
// RefreshContainer sets the container's contents to the one from
|
|
|
|
// GenericContainer. This is mainly used for transferring between different
|
|
|
|
// containers.
|
|
|
|
//
|
|
|
|
// Right now, this only works with Timestamp, as that's the only state tracked.
|
|
|
|
func RefreshContainer(c Container, gc *GenericContainer) {
|
|
|
|
c.UpdateTimestamp(gc.time)
|
|
|
|
}
|
|
|
|
|
2020-06-06 07:44:36 +00:00
|
|
|
// GenericContainer provides a single generic message container for subpackages
|
|
|
|
// to use.
|
|
|
|
type GenericContainer struct {
|
2020-06-13 07:29:32 +00:00
|
|
|
id string
|
|
|
|
time time.Time
|
|
|
|
authorID string
|
|
|
|
avatarURL string // avatar
|
|
|
|
nonce string
|
2020-05-26 06:51:06 +00:00
|
|
|
|
|
|
|
Timestamp *gtk.Label
|
|
|
|
Username *gtk.Label
|
2020-06-28 01:35:26 +00:00
|
|
|
Content *gtk.Label
|
2020-06-13 07:29:32 +00:00
|
|
|
|
2020-06-17 22:58:38 +00:00
|
|
|
MenuItems []menu.Item
|
2020-05-26 06:51:06 +00:00
|
|
|
}
|
|
|
|
|
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-13 07:29:32 +00:00
|
|
|
c.time = msg.Time()
|
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-06-13 07:29:32 +00:00
|
|
|
ts.SetEllipsize(pango.ELLIPSIZE_MIDDLE)
|
|
|
|
ts.SetXAlign(1) // right align
|
|
|
|
ts.SetVAlign(gtk.ALIGN_END)
|
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-06-28 01:35:26 +00:00
|
|
|
user.SetXAlign(1) // right align
|
2020-05-28 19:26:55 +00:00
|
|
|
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
|
|
|
|
2020-06-28 01:35:26 +00:00
|
|
|
content, _ := gtk.LabelNew("")
|
|
|
|
content.SetLineWrap(true)
|
|
|
|
content.SetLineWrapMode(pango.WRAP_WORD_CHAR)
|
|
|
|
content.SetXAlign(0) // left align
|
|
|
|
// content.SetSelectable(true)
|
2020-05-26 06:51:06 +00:00
|
|
|
content.Show()
|
|
|
|
|
2020-06-13 07:29:32 +00:00
|
|
|
// Add CSS classes.
|
|
|
|
primitives.AddClass(ts, "message-time")
|
|
|
|
primitives.AddClass(user, "message-author")
|
|
|
|
primitives.AddClass(content, "message-content")
|
|
|
|
|
|
|
|
gc := &GenericContainer{
|
2020-05-26 06:51:06 +00:00
|
|
|
Timestamp: ts,
|
|
|
|
Username: user,
|
|
|
|
Content: content,
|
|
|
|
}
|
2020-06-13 07:29:32 +00:00
|
|
|
|
2020-06-28 01:35:26 +00:00
|
|
|
gc.Content.Connect("populate-popup", func(l *gtk.Label, m *gtk.Menu) {
|
|
|
|
menu.MenuSeparator(m)
|
|
|
|
menu.MenuItems(m, gc.MenuItems)
|
2020-06-13 07:29:32 +00:00
|
|
|
})
|
|
|
|
|
2020-06-28 01:35:26 +00:00
|
|
|
// Make up for the lack of inline images with an image popover that's shown
|
|
|
|
// when links are clicked.
|
|
|
|
imgview.BindTooltip(gc.Content)
|
|
|
|
|
2020-06-13 07:29:32 +00:00
|
|
|
return gc
|
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
|
|
|
|
}
|
|
|
|
|
2020-06-13 07:29:32 +00:00
|
|
|
func (m *GenericContainer) Time() time.Time {
|
|
|
|
return m.time
|
|
|
|
}
|
|
|
|
|
2020-06-06 07:44:36 +00:00
|
|
|
func (m *GenericContainer) AuthorID() string {
|
|
|
|
return m.authorID
|
2020-05-26 06:51:06 +00:00
|
|
|
}
|
|
|
|
|
2020-06-13 07:29:32 +00:00
|
|
|
func (m *GenericContainer) AvatarURL() string {
|
|
|
|
return m.avatarURL
|
|
|
|
}
|
|
|
|
|
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-06-13 07:29:32 +00:00
|
|
|
m.time = t
|
|
|
|
m.Timestamp.SetMarkup(rich.Small(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) {
|
2020-06-13 07:29:32 +00:00
|
|
|
m.authorID = author.ID()
|
2020-06-06 07:44:36 +00:00
|
|
|
m.UpdateAuthorName(author.Name())
|
2020-06-13 07:29:32 +00:00
|
|
|
|
|
|
|
// Set the avatar URL for future access on-demand.
|
|
|
|
if avatarer, ok := author.(cchat.MessageAuthorAvatar); ok {
|
|
|
|
m.avatarURL = avatarer.Avatar()
|
|
|
|
}
|
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-13 07:29:32 +00:00
|
|
|
func (m *GenericContainer) UpdateContent(content text.Rich, edited bool) {
|
2020-06-28 01:35:26 +00:00
|
|
|
var markup = parser.RenderMarkup(content)
|
2020-06-13 07:29:32 +00:00
|
|
|
if edited {
|
2020-06-28 01:35:26 +00:00
|
|
|
markup += " " + rich.Small("(edited)")
|
2020-06-13 07:29:32 +00:00
|
|
|
}
|
2020-06-28 01:35:26 +00:00
|
|
|
|
|
|
|
m.Content.SetMarkup(markup)
|
|
|
|
|
|
|
|
// // Render the content.
|
|
|
|
// parser.RenderTextBuffer(m.CBuffer, content)
|
|
|
|
|
|
|
|
// if edited {
|
|
|
|
// parser.AppendEditBadge(m.CBuffer, m.Time())
|
|
|
|
// }
|
2020-06-13 07:29:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// AttachMenu connects signal handlers to handle a list of menu items from
|
|
|
|
// the container.
|
2020-06-17 22:58:38 +00:00
|
|
|
func (m *GenericContainer) AttachMenu(newItems []menu.Item) {
|
2020-06-13 07:29:32 +00:00
|
|
|
m.MenuItems = newItems
|
2020-05-26 06:51:06 +00:00
|
|
|
}
|