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/message/message.go

80 lines
1.7 KiB
Go
Raw Normal View History

2020-05-26 06:51:06 +00:00
package message
import (
"time"
"github.com/diamondburned/cchat"
"github.com/diamondburned/cchat-gtk/internal/humanize"
"github.com/diamondburned/cchat/text"
"github.com/gotk3/gotk3/gtk"
"github.com/gotk3/gotk3/pango"
)
type Message struct {
2020-05-28 19:26:55 +00:00
index int
2020-05-26 06:51:06 +00:00
ID string
Nonce string
Timestamp *gtk.Label
Username *gtk.Label
Content *gtk.Label
}
func NewMessage(msg cchat.MessageCreate) Message {
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)
ts.Show()
2020-05-26 06:51:06 +00:00
user, _ := gtk.LabelNew("")
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)
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-05-26 06:51:06 +00:00
content.Show()
m := Message{
ID: msg.ID(),
Timestamp: ts,
Username: user,
Content: content,
}
m.UpdateTimestamp(msg.Time())
m.UpdateAuthor(msg.Author())
m.UpdateContent(msg.Content())
if noncer, ok := msg.(cchat.MessageNonce); ok {
m.Nonce = noncer.Nonce()
}
return m
}
2020-05-28 19:26:55 +00:00
func (m *Message) Attach(grid *gtk.Grid, row int) {
grid.Attach(m.Timestamp, 0, row, 1, 1)
grid.Attach(m.Username, 1, row, 1, 1)
grid.Attach(m.Content, 2, row, 1, 1)
}
2020-05-26 06:51:06 +00:00
func (m *Message) UpdateTimestamp(t time.Time) {
m.Timestamp.SetLabel(humanize.TimeAgo(t))
}
func (m *Message) UpdateAuthor(author text.Rich) {
m.Username.SetLabel(author.Content)
}
func (m *Message) UpdateContent(content text.Rich) {
m.Content.SetLabel(content.Content)
}