1
0
Fork 0
mirror of https://github.com/diamondburned/cchat-gtk.git synced 2024-11-16 11:12:44 +00:00
cchat-gtk/internal/ui/messages/message/message.go

187 lines
4.6 KiB
Go
Raw Normal View History

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"
"github.com/diamondburned/cchat-gtk/internal/ui/imgview"
"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"
"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
AvatarURL() string // avatar
2020-06-06 07:44:36 +00:00
Nonce() string
UpdateAuthor(cchat.MessageAuthor)
UpdateAuthorName(text.Rich)
UpdateContent(c text.Rich, edited bool)
2020-06-06 07:44:36 +00:00
UpdateTimestamp(time.Time)
}
// 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())
c.UpdateContent(msg.Content(), false)
2020-06-07 04:27:28 +00:00
c.UpdateTimestamp(msg.Time())
}
// 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 {
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
Content *gtk.Label
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()
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("")
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)
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
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()
// 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,
}
gc.Content.Connect("populate-popup", func(l *gtk.Label, m *gtk.Menu) {
menu.MenuSeparator(m)
menu.MenuItems(m, gc.MenuItems)
})
// Make up for the lack of inline images with an image popover that's shown
// when links are clicked.
imgview.BindTooltip(gc.Content)
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
}
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
}
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) {
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) {
m.authorID = author.ID()
2020-06-06 07:44:36 +00:00
m.UpdateAuthorName(author.Name())
// 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
}
func (m *GenericContainer) UpdateContent(content text.Rich, edited bool) {
var markup = parser.RenderMarkup(content)
if edited {
markup += " " + rich.Small("(edited)")
}
m.Content.SetMarkup(markup)
// // Render the content.
// parser.RenderTextBuffer(m.CBuffer, content)
// if edited {
// parser.AppendEditBadge(m.CBuffer, m.Time())
// }
}
// AttachMenu connects signal handlers to handle a list of menu items from
// the container.
func (m *GenericContainer) AttachMenu(newItems []menu.Item) {
m.MenuItems = newItems
2020-05-26 06:51:06 +00:00
}