2020-06-04 23:00:41 +00:00
|
|
|
package compact
|
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"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Message struct {
|
2020-06-04 23:00:41 +00:00
|
|
|
index int
|
|
|
|
ID string
|
|
|
|
AuthorID string
|
|
|
|
Nonce string
|
2020-05-26 06:51:06 +00:00
|
|
|
|
|
|
|
Timestamp *gtk.Label
|
|
|
|
Username *gtk.Label
|
|
|
|
Content *gtk.Label
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewMessage(msg cchat.MessageCreate) Message {
|
2020-06-04 23:00:41 +00:00
|
|
|
m := NewEmptyMessage()
|
|
|
|
m.ID = msg.ID()
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewPresendMessage(content string, author text.Rich, authorID, nonce string) Message {
|
|
|
|
msgc := NewEmptyMessage()
|
|
|
|
msgc.Nonce = nonce
|
|
|
|
msgc.AuthorID = authorID
|
|
|
|
msgc.SetSensitive(false)
|
|
|
|
msgc.UpdateContent(text.Rich{Content: content})
|
|
|
|
msgc.UpdateTimestamp(time.Now())
|
|
|
|
msgc.updateAuthorName(author)
|
|
|
|
|
|
|
|
return msgc
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewEmptyMessage() Message {
|
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-04 23:00:41 +00:00
|
|
|
return Message{
|
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-04 23:00:41 +00:00
|
|
|
func (m *Message) SetSensitive(sensitive bool) {
|
|
|
|
m.Timestamp.SetSensitive(sensitive)
|
|
|
|
m.Username.SetSensitive(sensitive)
|
|
|
|
m.Content.SetSensitive(sensitive)
|
2020-05-26 06:51:06 +00:00
|
|
|
}
|
|
|
|
|
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))
|
2020-06-04 23:00:41 +00:00
|
|
|
m.Timestamp.SetTooltipText(t.Format(time.Stamp))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Message) UpdateAuthor(author cchat.MessageAuthor) {
|
|
|
|
m.AuthorID = author.ID()
|
|
|
|
m.updateAuthorName(author.Name())
|
2020-05-26 06:51:06 +00:00
|
|
|
}
|
|
|
|
|
2020-06-04 23:00:41 +00:00
|
|
|
func (m *Message) 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 *Message) 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
|
|
|
}
|