2020-06-06 07:44:36 +00:00
|
|
|
package message
|
2020-05-26 06:51:06 +00:00
|
|
|
|
|
|
|
import (
|
2021-03-29 21:46:52 +00:00
|
|
|
"context"
|
2020-05-26 06:51:06 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/diamondburned/cchat"
|
2020-06-13 07:29:32 +00:00
|
|
|
"github.com/diamondburned/cchat-gtk/internal/ui/primitives"
|
2020-07-17 00:21:14 +00:00
|
|
|
"github.com/diamondburned/cchat-gtk/internal/ui/primitives/menu"
|
2020-06-13 07:29:32 +00:00
|
|
|
"github.com/diamondburned/cchat-gtk/internal/ui/rich"
|
2020-07-14 07:24:55 +00:00
|
|
|
"github.com/diamondburned/cchat-gtk/internal/ui/rich/labeluri"
|
2021-03-29 21:46:52 +00:00
|
|
|
"github.com/diamondburned/cchat-gtk/internal/ui/rich/parser/markup"
|
2020-05-26 06:51:06 +00:00
|
|
|
"github.com/diamondburned/cchat/text"
|
|
|
|
"github.com/gotk3/gotk3/gtk"
|
|
|
|
"github.com/gotk3/gotk3/pango"
|
|
|
|
)
|
|
|
|
|
2021-05-05 03:30:50 +00:00
|
|
|
const AvatarSize = 40
|
|
|
|
|
2021-03-29 21:46:52 +00:00
|
|
|
// Container describes a message container that wraps a state. These methods are
|
|
|
|
// made for containers to override; methods not meant to be override are not
|
|
|
|
// exposed and will be done directly on the State.
|
2020-06-06 07:44:36 +00:00
|
|
|
type Container interface {
|
2021-05-01 07:26:17 +00:00
|
|
|
// Unwrap returns the internal message state.
|
|
|
|
Unwrap() *State
|
|
|
|
// Revert unwraps and reverts all widget changes to the internal state then
|
|
|
|
// returns that state.
|
|
|
|
Revert() *State
|
2020-06-06 07:44:36 +00:00
|
|
|
|
2021-03-29 21:46:52 +00:00
|
|
|
// UpdateContent updates the underlying content widget.
|
|
|
|
UpdateContent(content text.Rich, edited bool)
|
2020-06-07 04:27:28 +00:00
|
|
|
|
2021-03-29 21:46:52 +00:00
|
|
|
// SetReferenceHighlighter sets the reference highlighter into the message.
|
|
|
|
SetReferenceHighlighter(refer labeluri.ReferenceHighlighter)
|
2020-06-13 07:29:32 +00:00
|
|
|
}
|
|
|
|
|
2021-03-29 21:46:52 +00:00
|
|
|
// State provides a single generic message container for subpackages
|
2020-06-06 07:44:36 +00:00
|
|
|
// to use.
|
2021-03-29 21:46:52 +00:00
|
|
|
type State struct {
|
|
|
|
gtk.Box
|
|
|
|
Row *gtk.ListBoxRow // contains Box
|
2021-01-02 09:24:14 +00:00
|
|
|
class string
|
|
|
|
|
2021-03-29 21:46:52 +00:00
|
|
|
ID cchat.ID
|
|
|
|
Time time.Time
|
|
|
|
Nonce string
|
|
|
|
Author *Author
|
2020-05-26 06:51:06 +00:00
|
|
|
|
2021-01-05 02:05:33 +00:00
|
|
|
Content *gtk.Box
|
|
|
|
ContentBody *labeluri.Label
|
|
|
|
ContentBodyStyle *gtk.StyleContext
|
2020-06-13 07:29:32 +00:00
|
|
|
|
2021-03-29 21:46:52 +00:00
|
|
|
MenuItems []menu.Item
|
2020-05-26 06:51:06 +00:00
|
|
|
}
|
|
|
|
|
2021-05-01 07:26:17 +00:00
|
|
|
// NewState creates a new message state with the given MessageCreate.
|
2021-03-29 21:46:52 +00:00
|
|
|
func NewState(msg cchat.MessageCreate) *State {
|
|
|
|
author := msg.Author()
|
2020-06-06 07:44:36 +00:00
|
|
|
|
2021-03-29 21:46:52 +00:00
|
|
|
c := NewEmptyState()
|
|
|
|
c.Author.ID = author.ID()
|
|
|
|
c.Author.Name.QueueNamer(context.Background(), author)
|
|
|
|
c.ID = msg.ID()
|
|
|
|
c.Time = msg.Time()
|
|
|
|
c.Nonce = msg.Nonce()
|
2021-05-01 07:26:17 +00:00
|
|
|
c.UpdateContent(msg.Content(), false)
|
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
|
|
|
}
|
|
|
|
|
2021-03-29 21:46:52 +00:00
|
|
|
// NewEmptyState creates a new empty message state. The author should be set
|
|
|
|
// immediately afterwards; it is invalid once the state is used.
|
|
|
|
func NewEmptyState() *State {
|
2020-07-14 07:24:55 +00:00
|
|
|
ctbody := labeluri.NewLabel(text.Rich{})
|
2021-05-05 03:30:50 +00:00
|
|
|
ctbody.Tooltip = false
|
2021-05-01 07:26:17 +00:00
|
|
|
ctbody.SetHAlign(gtk.ALIGN_FILL)
|
2020-07-14 07:24:55 +00:00
|
|
|
ctbody.SetEllipsize(pango.ELLIPSIZE_NONE)
|
2020-07-10 23:26:07 +00:00
|
|
|
ctbody.SetLineWrap(true)
|
|
|
|
ctbody.SetLineWrapMode(pango.WRAP_WORD_CHAR)
|
|
|
|
ctbody.SetXAlign(0) // left align
|
|
|
|
ctbody.SetSelectable(true)
|
2020-07-17 00:21:14 +00:00
|
|
|
ctbody.SetTrackVisitedLinks(false)
|
2020-07-10 23:26:07 +00:00
|
|
|
ctbody.Show()
|
|
|
|
|
2021-01-05 02:05:33 +00:00
|
|
|
ctbodyStyle, _ := ctbody.GetStyleContext()
|
|
|
|
ctbodyStyle.AddClass("message-content")
|
|
|
|
|
2020-07-10 23:26:07 +00:00
|
|
|
// Wrap the content label inside a content box.
|
|
|
|
ctbox, _ := gtk.BoxNew(gtk.ORIENTATION_VERTICAL, 0)
|
|
|
|
ctbox.PackStart(ctbody, false, false, 0)
|
2021-05-01 07:26:17 +00:00
|
|
|
ctbox.SetHAlign(gtk.ALIGN_FILL)
|
2020-07-10 23:26:07 +00:00
|
|
|
ctbox.Show()
|
2020-05-26 06:51:06 +00:00
|
|
|
|
2021-05-01 07:26:17 +00:00
|
|
|
// Box that belongs to the implementations of messages.
|
2021-01-02 09:24:14 +00:00
|
|
|
box, _ := gtk.BoxNew(gtk.ORIENTATION_HORIZONTAL, 0)
|
|
|
|
box.Show()
|
2020-07-09 07:19:18 +00:00
|
|
|
|
2021-01-02 09:24:14 +00:00
|
|
|
row, _ := gtk.ListBoxRowNew()
|
|
|
|
row.Add(box)
|
|
|
|
row.Show()
|
|
|
|
primitives.AddClass(row, "message-row")
|
2020-07-06 00:19:48 +00:00
|
|
|
|
2021-03-29 21:46:52 +00:00
|
|
|
gc := &State{
|
|
|
|
Box: *box,
|
|
|
|
Row: row,
|
|
|
|
Author: &Author{},
|
2021-01-02 09:24:14 +00:00
|
|
|
|
2021-01-05 02:05:33 +00:00
|
|
|
Content: ctbox,
|
|
|
|
ContentBody: ctbody,
|
|
|
|
ContentBodyStyle: ctbodyStyle,
|
2020-10-24 21:58:48 +00:00
|
|
|
|
|
|
|
// Time is important, as it is used to sort messages, so we have to be
|
|
|
|
// careful with this.
|
2021-03-29 21:46:52 +00:00
|
|
|
Time: time.Now(),
|
2020-05-26 06:51:06 +00:00
|
|
|
}
|
2020-06-13 07:29:32 +00:00
|
|
|
|
2021-03-29 21:46:52 +00:00
|
|
|
// This may either work, or it may cause memory leaks.
|
|
|
|
row.Connect("destroy", func() { gc.Author.Name.Stop() })
|
|
|
|
|
2020-07-10 23:26:07 +00:00
|
|
|
// Bind the custom popup menu to the content label.
|
|
|
|
gc.ContentBody.Connect("populate-popup", func(l *gtk.Label, m *gtk.Menu) {
|
2020-06-28 01:35:26 +00:00
|
|
|
menu.MenuSeparator(m)
|
2021-03-29 21:46:52 +00:00
|
|
|
menu.MenuItems(m, gc.MenuItems)
|
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
|
|
|
|
2021-05-01 07:26:17 +00:00
|
|
|
// ClearBox clears the state's widget container.
|
|
|
|
func (m *State) ClearBox() {
|
|
|
|
primitives.RemoveChildren(m)
|
|
|
|
m.SetClass("")
|
|
|
|
}
|
|
|
|
|
|
|
|
// // For debugging use only.
|
|
|
|
// func (m *State) PackStart(child gtk.IWidget, expand bool, fill bool, padding uint) {
|
|
|
|
// paths := make([]string, 0, 5)
|
|
|
|
// for i := 1; i < 5; i++ {
|
|
|
|
// _, file, line, ok := runtime.Caller(i)
|
|
|
|
// if !ok {
|
|
|
|
// break
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// paths = append(paths, fmt.Sprintf("%s:%d", filepath.Base(file), line))
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// log.Println("child packstart", m.ID, "at", strings.Join(paths, " < "))
|
|
|
|
// m.Box.PackStart(child, expand, fill, padding)
|
|
|
|
// }
|
|
|
|
|
2021-01-02 09:24:14 +00:00
|
|
|
// SetClass sets the internal row's class.
|
2021-03-29 21:46:52 +00:00
|
|
|
func (m *State) SetClass(class string) {
|
2021-01-02 09:24:14 +00:00
|
|
|
if m.class != "" {
|
2021-03-29 21:46:52 +00:00
|
|
|
primitives.RemoveClass(m.Row, m.class)
|
2021-01-02 09:24:14 +00:00
|
|
|
}
|
|
|
|
|
2021-05-01 07:26:17 +00:00
|
|
|
if class != "" {
|
|
|
|
primitives.AddClass(m.Row, class)
|
|
|
|
}
|
|
|
|
|
2021-01-02 09:24:14 +00:00
|
|
|
m.class = class
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetReferenceHighlighter sets the reference highlighter into the message.
|
2021-03-29 21:46:52 +00:00
|
|
|
func (m *State) SetReferenceHighlighter(r labeluri.ReferenceHighlighter) {
|
2021-01-02 09:24:14 +00:00
|
|
|
m.ContentBody.SetReferenceHighlighter(r)
|
|
|
|
}
|
|
|
|
|
2021-03-29 21:46:52 +00:00
|
|
|
// UpdateContent replaces the internal content and the widget.
|
|
|
|
func (m *State) UpdateContent(content text.Rich, edited bool) {
|
|
|
|
m.ContentBody.SetLabel(content)
|
2020-07-14 07:24:55 +00:00
|
|
|
|
2020-06-13 07:29:32 +00:00
|
|
|
if edited {
|
2021-03-29 21:46:52 +00:00
|
|
|
m.ContentBody.SetRenderer(func(content text.Rich) markup.RenderOutput {
|
|
|
|
output := markup.RenderCmplx(content)
|
|
|
|
output.Markup += rich.Small(text.Plain("(edited)")).Markup
|
|
|
|
return output
|
|
|
|
})
|
2020-06-13 07:29:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-29 21:46:52 +00:00
|
|
|
func (m *State) Focusable() gtk.IWidget {
|
2020-08-20 23:53:13 +00:00
|
|
|
return m.Content
|
|
|
|
}
|
2021-05-01 07:26:17 +00:00
|
|
|
|
|
|
|
// Unwrap returns itself.
|
|
|
|
func (m *State) Unwrap() *State { return m }
|