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

198 lines
4.8 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"
2020-06-26 04:11:31 +00:00
"github.com/diamondburned/cchat-gtk/internal/log"
"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-26 04:11:31 +00:00
"github.com/pkg/errors"
2020-05-26 06:51:06 +00:00
)
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.TextView
CBuffer *gtk.TextBuffer
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)
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.TextViewNew()
2020-05-28 19:26:55 +00:00
content.SetHExpand(true)
content.SetWrapMode(gtk.WRAP_WORD_CHAR)
content.SetCursorVisible(false)
content.SetEditable(false)
2020-05-26 06:51:06 +00:00
content.Show()
cbuffer, _ := content.GetBuffer()
// 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,
CBuffer: cbuffer,
2020-05-26 06:51:06 +00:00
}
2020-06-26 04:11:31 +00:00
gc.Content.SetProperty("populate-all", true)
gc.Content.Connect("populate-popup", func(tv *gtk.TextView, popup *gtk.Widget) {
v, err := popup.Cast()
if err != nil {
log.Error(errors.Wrap(err, "Failed to cast popup to IWidget"))
return
}
switch popup := v.(type) {
case menu.MenuAppender:
menu.MenuSeparator(popup)
menu.MenuItems(popup, gc.MenuItems)
case menu.ToolbarInserter:
menu.ToolbarSeparator(popup)
menu.ToolbarItems(popup, gc.MenuItems)
default:
log.Printlnf("Debug: typeOf(popup) = %T", popup)
}
})
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) {
// 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
}