2020-06-13 07:29:32 +00:00
|
|
|
package container
|
|
|
|
|
|
|
|
import (
|
2021-05-01 07:26:17 +00:00
|
|
|
"log"
|
2021-01-07 01:12:10 +00:00
|
|
|
"strings"
|
2021-01-02 09:55:19 +00:00
|
|
|
"time"
|
2020-10-23 06:21:34 +00:00
|
|
|
|
2020-06-13 07:29:32 +00:00
|
|
|
"github.com/diamondburned/cchat"
|
2021-01-02 09:24:14 +00:00
|
|
|
"github.com/diamondburned/cchat-gtk/internal/gts"
|
2021-03-29 21:46:52 +00:00
|
|
|
"github.com/diamondburned/cchat-gtk/internal/ui/messages/message"
|
2020-06-13 07:29:32 +00:00
|
|
|
"github.com/diamondburned/cchat-gtk/internal/ui/primitives"
|
2021-01-02 09:24:14 +00:00
|
|
|
"github.com/diamondburned/cchat-gtk/internal/ui/rich/parser/markup"
|
2021-03-29 21:46:52 +00:00
|
|
|
"github.com/diamondburned/cchat/text"
|
2020-06-13 07:29:32 +00:00
|
|
|
"github.com/gotk3/gotk3/gtk"
|
|
|
|
)
|
|
|
|
|
2020-12-20 05:46:50 +00:00
|
|
|
type messageKey struct {
|
|
|
|
id string
|
|
|
|
nonce bool
|
|
|
|
}
|
|
|
|
|
|
|
|
func nonceKey(nonce string) messageKey { return messageKey{nonce, true} }
|
|
|
|
func idKey(id cchat.ID) messageKey { return messageKey{id, false} }
|
|
|
|
|
2021-05-01 07:26:17 +00:00
|
|
|
// newKey creates a new message key.
|
|
|
|
func newKey(state *message.State) messageKey {
|
|
|
|
if state.ID != "" {
|
|
|
|
return messageKey{state.ID, false}
|
|
|
|
}
|
|
|
|
if state.Nonce != "" {
|
|
|
|
return messageKey{state.Nonce, true}
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Printf("state is missing both ID and Nonce: \n%#v\n", state)
|
|
|
|
return messageKey{}
|
|
|
|
}
|
|
|
|
|
2021-01-07 01:12:10 +00:00
|
|
|
func parseKeyFromNamer(n primitives.Namer) messageKey {
|
|
|
|
name, err := n.GetName()
|
|
|
|
if err != nil {
|
|
|
|
panic("BUG: failed to get primitive name: " + err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
parts := strings.SplitN(name, ":", 2)
|
|
|
|
if len(parts) != 2 {
|
|
|
|
return messageKey{id: name}
|
|
|
|
}
|
|
|
|
|
|
|
|
switch parts[0] {
|
|
|
|
case "id":
|
|
|
|
return messageKey{id: parts[1]}
|
|
|
|
case "nonce":
|
|
|
|
return messageKey{id: parts[1], nonce: true}
|
|
|
|
default:
|
2021-05-01 07:26:17 +00:00
|
|
|
panic("unknown prefix in message row name " + parts[0])
|
2021-01-07 01:12:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (key messageKey) expand() (id, nonce string) {
|
|
|
|
if key.nonce {
|
|
|
|
return "", key.id
|
|
|
|
}
|
|
|
|
return key.id, ""
|
|
|
|
}
|
|
|
|
|
|
|
|
func (key messageKey) name() string {
|
|
|
|
if key.nonce {
|
|
|
|
return "nonce:" + key.id
|
|
|
|
}
|
|
|
|
return "id:" + key.id
|
|
|
|
}
|
|
|
|
|
|
|
|
// String satisfies the fmt.Stringer interface.
|
|
|
|
func (key messageKey) String() string { return key.name() }
|
|
|
|
|
2021-01-02 09:24:14 +00:00
|
|
|
var messageListCSS = primitives.PrepareClassCSS("message-list", `
|
|
|
|
.message-list { background: transparent; }
|
|
|
|
`)
|
|
|
|
|
2021-03-29 21:46:52 +00:00
|
|
|
var fallbackAuthor = message.NewCustomAuthor("", text.Plain("self"))
|
2020-06-13 07:29:32 +00:00
|
|
|
|
2021-03-29 21:46:52 +00:00
|
|
|
type ListStore struct {
|
|
|
|
ListBox *gtk.ListBox
|
2020-06-13 07:29:32 +00:00
|
|
|
Controller Controller
|
|
|
|
|
2021-03-29 21:46:52 +00:00
|
|
|
self *message.Author
|
2020-11-06 04:01:00 +00:00
|
|
|
|
2021-03-29 21:46:52 +00:00
|
|
|
resetMe bool
|
2021-01-03 05:52:27 +00:00
|
|
|
messages map[messageKey]*messageRow
|
2020-06-13 07:29:32 +00:00
|
|
|
}
|
|
|
|
|
2021-03-29 21:46:52 +00:00
|
|
|
func NewListStore(ctrl Controller) *ListStore {
|
2021-01-02 09:24:14 +00:00
|
|
|
listBox, _ := gtk.ListBoxNew()
|
2021-01-03 04:52:30 +00:00
|
|
|
listBox.SetSelectionMode(gtk.SELECTION_SINGLE)
|
2021-01-02 09:24:14 +00:00
|
|
|
listBox.Show()
|
|
|
|
messageListCSS(listBox)
|
2020-06-13 07:29:32 +00:00
|
|
|
|
2021-01-03 04:52:30 +00:00
|
|
|
listStore := ListStore{
|
2021-01-03 05:52:27 +00:00
|
|
|
ListBox: listBox,
|
|
|
|
Controller: ctrl,
|
|
|
|
messages: make(map[messageKey]*messageRow, BacklogLimit+1),
|
2021-03-29 21:46:52 +00:00
|
|
|
self: &fallbackAuthor,
|
2020-06-13 07:29:32 +00:00
|
|
|
}
|
2021-01-03 04:52:30 +00:00
|
|
|
|
|
|
|
var selected bool
|
|
|
|
|
|
|
|
listBox.Connect("row-selected", func(listBox *gtk.ListBox, r *gtk.ListBoxRow) {
|
|
|
|
if r == nil || selected {
|
|
|
|
if selected {
|
|
|
|
listBox.UnselectAll()
|
|
|
|
selected = false
|
|
|
|
}
|
|
|
|
ctrl.UnselectMessage()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-01-07 01:12:10 +00:00
|
|
|
id := parseKeyFromNamer(r)
|
2021-01-03 04:52:30 +00:00
|
|
|
|
2021-01-07 01:12:10 +00:00
|
|
|
msg := listStore.Message(id.expand())
|
2021-01-03 04:52:30 +00:00
|
|
|
if msg == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
selected = true
|
|
|
|
ctrl.SelectMessage(&listStore, msg)
|
|
|
|
})
|
|
|
|
|
|
|
|
return &listStore
|
2020-06-13 07:29:32 +00:00
|
|
|
}
|
|
|
|
|
2021-03-29 21:46:52 +00:00
|
|
|
// Reset resets the list store.
|
2021-01-02 09:24:14 +00:00
|
|
|
func (c *ListStore) Reset() {
|
2021-03-29 21:46:52 +00:00
|
|
|
for _, msg := range c.messages {
|
|
|
|
destroyMsg(msg)
|
|
|
|
}
|
|
|
|
|
2021-01-05 08:02:26 +00:00
|
|
|
// Delegate removing children to the constructor.
|
2021-01-02 09:24:14 +00:00
|
|
|
c.messages = make(map[messageKey]*messageRow, BacklogLimit+1)
|
2021-03-29 21:46:52 +00:00
|
|
|
|
2021-05-01 07:26:17 +00:00
|
|
|
c.self.Name.Stop()
|
2021-03-29 21:46:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// SetSelf sets the current author to presend. If ID is empty or Namer is nil,
|
|
|
|
// then the fallback author is used instead. The given author will be stopped
|
|
|
|
// on reset.
|
|
|
|
func (c *ListStore) SetSelf(self *message.Author) {
|
|
|
|
if self != nil {
|
|
|
|
c.self = self
|
|
|
|
} else {
|
|
|
|
c.self = &fallbackAuthor
|
|
|
|
}
|
2020-11-06 04:01:00 +00:00
|
|
|
}
|
|
|
|
|
2021-01-02 09:24:14 +00:00
|
|
|
func (c *ListStore) MessagesLen() int {
|
2021-01-03 05:52:27 +00:00
|
|
|
return len(c.messages)
|
2020-10-23 06:21:34 +00:00
|
|
|
}
|
|
|
|
|
2020-06-13 07:29:32 +00:00
|
|
|
// Swap changes the message with the ID to the given message. This provides a
|
|
|
|
// low level API for edits that need a new Attach method.
|
|
|
|
//
|
|
|
|
// TODO: combine compact and full so they share the same attach method.
|
2021-01-02 09:24:14 +00:00
|
|
|
func (c *ListStore) SwapMessage(msg MessageRow) bool {
|
2021-01-02 09:55:19 +00:00
|
|
|
// Unwrap msg from a *messageRow if it's not already.
|
2021-05-01 07:26:17 +00:00
|
|
|
if mrow, ok := msg.(*messageRow); ok {
|
|
|
|
msg = mrow.MessageRow
|
2020-10-24 21:58:48 +00:00
|
|
|
}
|
|
|
|
|
2021-05-01 07:26:17 +00:00
|
|
|
state := msg.Unwrap()
|
2021-03-29 21:46:52 +00:00
|
|
|
|
2020-10-23 06:21:34 +00:00
|
|
|
// Get the current message's index.
|
2021-05-01 07:26:17 +00:00
|
|
|
oldMsg, ix := c.findIndex(state.ID)
|
2020-10-23 06:21:34 +00:00
|
|
|
if ix == -1 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2021-05-01 07:26:17 +00:00
|
|
|
// Remove the previous message off the message map using the key from its
|
|
|
|
// state.
|
|
|
|
delete(c.messages, newKey(oldMsg.state))
|
2021-03-29 21:46:52 +00:00
|
|
|
|
2021-05-01 07:26:17 +00:00
|
|
|
// Remove the to-be-replaced message box.
|
|
|
|
// TODO: We should probably reuse the row.
|
|
|
|
c.ListBox.Remove(oldMsg.state.Row)
|
2021-01-02 09:55:19 +00:00
|
|
|
|
2020-06-13 07:29:32 +00:00
|
|
|
// Add a row at index. The actual row we want to delete will be shifted
|
|
|
|
// downwards.
|
2021-05-01 07:26:17 +00:00
|
|
|
c.ListBox.Insert(state.Row, ix)
|
|
|
|
|
|
|
|
row := messageRow{
|
|
|
|
MessageRow: msg,
|
|
|
|
state: state,
|
|
|
|
}
|
2020-06-13 07:29:32 +00:00
|
|
|
|
2020-10-23 06:21:34 +00:00
|
|
|
// Set the message into the map.
|
2021-05-01 07:26:17 +00:00
|
|
|
c.messages[newKey(state)] = &row
|
|
|
|
c.bindMessage(&row)
|
2020-10-23 06:21:34 +00:00
|
|
|
|
2020-06-13 07:29:32 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2020-10-24 21:58:48 +00:00
|
|
|
// Around returns the message before and after the given ID, or nil if none.
|
2021-01-02 09:24:14 +00:00
|
|
|
func (c *ListStore) Around(id cchat.ID) (before, after MessageRow) {
|
2020-10-24 21:58:48 +00:00
|
|
|
gridBefore, gridAfter := c.around(id)
|
|
|
|
|
|
|
|
if gridBefore != nil {
|
2021-01-02 09:24:14 +00:00
|
|
|
before = gridBefore.MessageRow
|
2020-10-24 21:58:48 +00:00
|
|
|
}
|
|
|
|
if gridAfter != nil {
|
2021-01-02 09:24:14 +00:00
|
|
|
after = gridAfter.MessageRow
|
2020-10-24 21:58:48 +00:00
|
|
|
}
|
2020-06-13 07:29:32 +00:00
|
|
|
|
2020-10-24 21:58:48 +00:00
|
|
|
return
|
2020-10-23 06:21:34 +00:00
|
|
|
}
|
|
|
|
|
2021-01-03 05:52:27 +00:00
|
|
|
func (c *ListStore) around(aroundID cchat.ID) (before, after *messageRow) {
|
2021-01-02 09:24:14 +00:00
|
|
|
var last *messageRow
|
2020-10-24 21:58:48 +00:00
|
|
|
var next bool
|
2020-10-23 06:21:34 +00:00
|
|
|
|
2021-01-03 05:52:27 +00:00
|
|
|
primitives.ForeachChildBackwards(c.ListBox, func(v interface{}) (stop bool) {
|
2021-01-07 01:12:10 +00:00
|
|
|
id := parseKeyFromNamer(v.(primitives.Namer))
|
2020-10-24 21:58:48 +00:00
|
|
|
if next {
|
2021-01-07 01:12:10 +00:00
|
|
|
after = c.message(id.expand())
|
2021-01-03 05:52:27 +00:00
|
|
|
return true
|
2020-10-24 21:58:48 +00:00
|
|
|
}
|
2021-01-07 01:12:10 +00:00
|
|
|
if !id.nonce && id.id == aroundID {
|
2020-10-24 21:58:48 +00:00
|
|
|
before = last
|
|
|
|
next = true
|
2021-01-03 05:52:27 +00:00
|
|
|
return false
|
2020-10-24 21:58:48 +00:00
|
|
|
}
|
2020-10-23 06:21:34 +00:00
|
|
|
|
2021-01-07 01:12:10 +00:00
|
|
|
last = c.message(id.expand())
|
2021-01-03 05:52:27 +00:00
|
|
|
return false
|
|
|
|
})
|
|
|
|
|
2020-10-24 21:58:48 +00:00
|
|
|
return
|
2020-06-13 07:29:32 +00:00
|
|
|
}
|
|
|
|
|
2021-01-03 05:52:27 +00:00
|
|
|
// findIndex searches backwards for id.
|
|
|
|
func (c *ListStore) findIndex(findID cchat.ID) (found *messageRow, index int) {
|
|
|
|
// Faster implementation of findMessage: no map lookup is done until an ID
|
|
|
|
// match, so the worst case is a single string hash.
|
|
|
|
index = c.MessagesLen() - 1
|
|
|
|
|
|
|
|
primitives.ForeachChildBackwards(c.ListBox, func(v interface{}) (stop bool) {
|
2021-01-07 01:12:10 +00:00
|
|
|
id := parseKeyFromNamer(v.(primitives.Namer))
|
|
|
|
if !id.nonce && id.id == findID {
|
|
|
|
found = c.message(id.expand())
|
2021-01-03 05:52:27 +00:00
|
|
|
return true
|
2020-10-23 06:21:34 +00:00
|
|
|
}
|
2021-01-03 05:52:27 +00:00
|
|
|
|
|
|
|
index--
|
2021-01-05 08:02:26 +00:00
|
|
|
return index <= 0
|
2021-01-03 05:52:27 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
// Preserve old behavior.
|
|
|
|
if found == nil {
|
|
|
|
index = -1
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-05-01 07:26:17 +00:00
|
|
|
// Fast path interface.
|
|
|
|
type messageFinder interface {
|
|
|
|
findMessage(presend bool, fn func(*messageRow) bool) (*messageRow, int)
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ messageFinder = (*ListStore)(nil)
|
|
|
|
|
|
|
|
// findMessage finds a message with the given callback as the filter. If presend
|
|
|
|
// is false, then presend messages are ignored.
|
2021-01-03 05:52:27 +00:00
|
|
|
func (c *ListStore) findMessage(presend bool, fn func(*messageRow) bool) (*messageRow, int) {
|
|
|
|
var r *messageRow
|
|
|
|
var i = c.MessagesLen() - 1
|
|
|
|
|
|
|
|
primitives.ForeachChildBackwards(c.ListBox, func(v interface{}) (stop bool) {
|
2021-01-07 01:12:10 +00:00
|
|
|
id := parseKeyFromNamer(v.(primitives.Namer))
|
|
|
|
gridMsg := c.message(id.expand())
|
2021-01-03 05:52:27 +00:00
|
|
|
|
2021-01-03 09:36:43 +00:00
|
|
|
// If gridMsg is actually nil, then we have bigger issues.
|
|
|
|
if gridMsg != nil {
|
2021-05-01 07:26:17 +00:00
|
|
|
// Ignore sending messages if presend is false.
|
2021-01-03 09:36:43 +00:00
|
|
|
if (presend || gridMsg.presend == nil) && fn(gridMsg) {
|
|
|
|
r = gridMsg
|
|
|
|
return true
|
|
|
|
}
|
2020-10-23 06:21:34 +00:00
|
|
|
}
|
2021-01-03 05:52:27 +00:00
|
|
|
|
|
|
|
i--
|
2021-01-05 08:02:26 +00:00
|
|
|
return i <= 0
|
2021-01-03 05:52:27 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
// Preserve old behavior.
|
|
|
|
if r == nil {
|
|
|
|
i = -1
|
2020-10-23 06:21:34 +00:00
|
|
|
}
|
2020-10-24 21:58:48 +00:00
|
|
|
|
2021-01-03 05:52:27 +00:00
|
|
|
return r, i
|
|
|
|
}
|
|
|
|
|
|
|
|
// FindMessage iterates backwards and returns the message if isMessage() returns
|
2021-05-01 07:26:17 +00:00
|
|
|
// true on that message.
|
|
|
|
func (c *ListStore) FindMessage(isMessage func(MessageRow) bool) (MessageRow, int) {
|
|
|
|
msg, ix := c.findMessage(true, func(row *messageRow) bool {
|
2021-01-03 05:52:27 +00:00
|
|
|
return isMessage(row.MessageRow)
|
|
|
|
})
|
2021-05-01 07:26:17 +00:00
|
|
|
return unwrapRow(msg), ix
|
2020-06-13 07:29:32 +00:00
|
|
|
}
|
|
|
|
|
2021-01-03 05:52:27 +00:00
|
|
|
func (c *ListStore) nthMessage(n int) *messageRow {
|
|
|
|
v := primitives.NthChild(c.ListBox, n)
|
2021-01-05 02:05:33 +00:00
|
|
|
if v == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-01-07 01:12:10 +00:00
|
|
|
id := parseKeyFromNamer(v.(primitives.Namer))
|
|
|
|
return c.message(id.expand())
|
2021-01-03 05:52:27 +00:00
|
|
|
}
|
|
|
|
|
2020-08-20 23:53:13 +00:00
|
|
|
// NthMessage returns the nth message.
|
2021-01-02 09:24:14 +00:00
|
|
|
func (c *ListStore) NthMessage(n int) MessageRow {
|
2021-01-05 02:05:33 +00:00
|
|
|
return unwrapRow(c.nthMessage(n))
|
2020-06-13 21:51:03 +00:00
|
|
|
}
|
|
|
|
|
2020-06-13 07:29:32 +00:00
|
|
|
// Message finds the message state in the container. It is not thread-safe. This
|
|
|
|
// exists for backwards compatibility.
|
2021-01-02 09:24:14 +00:00
|
|
|
func (c *ListStore) Message(msgID cchat.ID, nonce string) MessageRow {
|
2021-01-05 02:05:33 +00:00
|
|
|
return unwrapRow(c.message(msgID, nonce))
|
2020-10-23 06:21:34 +00:00
|
|
|
}
|
|
|
|
|
2021-01-02 09:24:14 +00:00
|
|
|
func (c *ListStore) message(msgID cchat.ID, nonce string) *messageRow {
|
2020-10-23 06:21:34 +00:00
|
|
|
// Search using the ID first.
|
2020-12-20 05:46:50 +00:00
|
|
|
m, ok := c.messages[idKey(msgID)]
|
2020-10-23 06:21:34 +00:00
|
|
|
if ok {
|
|
|
|
return m
|
|
|
|
}
|
|
|
|
|
|
|
|
// Is this an existing message?
|
|
|
|
if nonce != "" {
|
|
|
|
// Things in this map are guaranteed to have presend != nil.
|
2020-12-20 05:46:50 +00:00
|
|
|
m, ok := c.messages[nonceKey(nonce)]
|
2021-03-29 21:46:52 +00:00
|
|
|
|
|
|
|
// This is honestly pretty dumb, but whatever.
|
|
|
|
// TODO: make message() getter not set.
|
2020-10-23 06:21:34 +00:00
|
|
|
if ok {
|
|
|
|
// Replace the nonce key with ID.
|
2020-12-20 05:46:50 +00:00
|
|
|
delete(c.messages, nonceKey(nonce))
|
|
|
|
c.messages[idKey(msgID)] = m
|
2021-01-07 01:12:10 +00:00
|
|
|
c.bindMessage(m)
|
2020-10-23 06:21:34 +00:00
|
|
|
|
|
|
|
// Set the right ID.
|
|
|
|
m.presend.SetDone(msgID)
|
|
|
|
// Destroy the presend struct.
|
|
|
|
m.presend = nil
|
|
|
|
|
|
|
|
return m
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2020-06-13 07:29:32 +00:00
|
|
|
}
|
|
|
|
|
2021-01-07 01:12:10 +00:00
|
|
|
func (c *ListStore) bindMessage(msgc *messageRow) {
|
2021-03-29 21:46:52 +00:00
|
|
|
// Bind the message ID to the row so we can easily do a lookup.
|
|
|
|
key := messageKey{
|
2021-05-01 07:26:17 +00:00
|
|
|
id: msgc.state.ID,
|
2021-01-07 01:12:10 +00:00
|
|
|
}
|
|
|
|
|
2021-05-01 07:26:17 +00:00
|
|
|
if msgc.state.Nonce != "" {
|
|
|
|
key.id = msgc.state.Nonce
|
2021-03-29 21:46:52 +00:00
|
|
|
key.nonce = true
|
2020-06-13 07:29:32 +00:00
|
|
|
}
|
|
|
|
|
2021-05-01 07:26:17 +00:00
|
|
|
msgc.state.Row.SetName(key.name())
|
2021-03-29 21:46:52 +00:00
|
|
|
msgc.MessageRow.SetReferenceHighlighter(c)
|
2020-08-20 23:53:13 +00:00
|
|
|
|
2021-03-29 21:46:52 +00:00
|
|
|
c.Controller.BindMenu(msgc.MessageRow)
|
2021-01-02 09:24:14 +00:00
|
|
|
}
|
|
|
|
|
2021-05-01 07:26:17 +00:00
|
|
|
func (c *ListStore) AddMessageAt(msg MessageRow, ix int) {
|
|
|
|
state := msg.Unwrap()
|
2020-11-05 19:08:30 +00:00
|
|
|
|
2021-03-29 21:46:52 +00:00
|
|
|
defer c.Controller.AuthorEvent(state.Author.ID)
|
2020-07-04 04:41:12 +00:00
|
|
|
|
2021-01-02 09:24:14 +00:00
|
|
|
// Do not attempt to update before insertion (aka upsert).
|
2021-03-29 21:46:52 +00:00
|
|
|
if msgc := c.message(state.ID, state.Nonce); msgc != nil {
|
|
|
|
// This is kind of expensive, but it shouldn't really matter.
|
|
|
|
c.SwapMessage(msg)
|
|
|
|
return
|
2020-06-13 07:29:32 +00:00
|
|
|
}
|
|
|
|
|
2021-05-01 07:26:17 +00:00
|
|
|
// Attempt to guess if this is a presend message or not. This should be
|
|
|
|
// unwrapped once it's finalized.
|
|
|
|
presend, _ := msg.(message.Presender)
|
2020-10-24 21:58:48 +00:00
|
|
|
|
2021-01-05 02:05:33 +00:00
|
|
|
msgc := &messageRow{
|
2021-03-29 21:46:52 +00:00
|
|
|
MessageRow: msg,
|
2021-05-01 07:26:17 +00:00
|
|
|
presend: presend,
|
|
|
|
state: state,
|
2021-01-05 02:05:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Add the message. If before is nil, then the to-be-inserted message is the
|
|
|
|
// earliest message, therefore we prepend it.
|
2021-05-01 07:26:17 +00:00
|
|
|
if ix < 0 {
|
|
|
|
ix = 0
|
2021-03-29 21:46:52 +00:00
|
|
|
c.ListBox.Prepend(state.Row)
|
2021-01-05 02:05:33 +00:00
|
|
|
} else {
|
2021-05-01 07:26:17 +00:00
|
|
|
ix++ // insert right after
|
2021-01-05 02:05:33 +00:00
|
|
|
|
|
|
|
// Fast path: Insert did appear a lot on profiles, so we can try and use
|
|
|
|
// Add over Insert when we know.
|
2021-05-01 07:26:17 +00:00
|
|
|
if c.MessagesLen() == ix {
|
2021-03-29 21:46:52 +00:00
|
|
|
c.ListBox.Add(state.Row)
|
2021-01-05 02:05:33 +00:00
|
|
|
} else {
|
2021-05-01 07:26:17 +00:00
|
|
|
c.ListBox.Insert(state.Row, ix)
|
2021-01-05 02:05:33 +00:00
|
|
|
}
|
2020-10-24 21:58:48 +00:00
|
|
|
}
|
|
|
|
|
2021-05-01 07:26:17 +00:00
|
|
|
c.messages[newKey(state)] = msgc
|
2021-01-02 09:24:14 +00:00
|
|
|
c.bindMessage(msgc)
|
2020-06-13 07:29:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// PopMessage deletes a message off of the list and return the deleted message.
|
2021-01-02 09:24:14 +00:00
|
|
|
func (c *ListStore) PopMessage(id cchat.ID) (msg MessageRow) {
|
2020-10-24 21:58:48 +00:00
|
|
|
// Get the raw element to delete it off the list.
|
2021-01-03 05:52:27 +00:00
|
|
|
gridMsg, _ := c.findIndex(id)
|
|
|
|
if gridMsg == nil {
|
2020-06-13 07:29:32 +00:00
|
|
|
return nil
|
|
|
|
}
|
2021-01-02 09:24:14 +00:00
|
|
|
msg = gridMsg.MessageRow
|
2020-10-23 06:21:34 +00:00
|
|
|
|
2020-06-13 07:29:32 +00:00
|
|
|
// Remove off of the Gtk grid.
|
2021-03-29 21:46:52 +00:00
|
|
|
destroyMsg(gridMsg)
|
|
|
|
|
2020-10-23 06:21:34 +00:00
|
|
|
// Delete off the map.
|
2020-12-20 05:46:50 +00:00
|
|
|
delete(c.messages, idKey(id))
|
2020-06-13 07:29:32 +00:00
|
|
|
|
2020-10-23 06:21:34 +00:00
|
|
|
return
|
2020-06-13 07:29:32 +00:00
|
|
|
}
|
2020-10-24 21:58:48 +00:00
|
|
|
|
|
|
|
// DeleteEarliest deletes the n earliest messages. It does nothing if n is or
|
|
|
|
// less than 0.
|
2021-01-02 09:24:14 +00:00
|
|
|
func (c *ListStore) DeleteEarliest(n int) {
|
2020-10-24 21:58:48 +00:00
|
|
|
if n <= 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Since container/list nils out the next element, we can't just call Next
|
|
|
|
// after deleting, so we have to call Next manually before Removing.
|
2021-01-03 09:36:43 +00:00
|
|
|
primitives.ForeachChild(c.ListBox, func(v interface{}) (stop bool) {
|
2021-01-07 01:12:10 +00:00
|
|
|
id := parseKeyFromNamer(v.(primitives.Namer))
|
2021-03-29 21:46:52 +00:00
|
|
|
|
2021-05-01 07:26:17 +00:00
|
|
|
mr, ok := c.messages[id]
|
|
|
|
if !ok {
|
|
|
|
log.Panicln("message with ID", id, "not found in map")
|
2020-12-20 05:46:50 +00:00
|
|
|
}
|
|
|
|
|
2021-05-01 07:26:17 +00:00
|
|
|
delete(c.messages, id)
|
|
|
|
destroyMsg(mr)
|
2020-10-24 21:58:48 +00:00
|
|
|
|
2021-01-03 05:52:27 +00:00
|
|
|
n--
|
|
|
|
return n == 0
|
|
|
|
})
|
2020-10-24 21:58:48 +00:00
|
|
|
}
|
2021-01-02 09:24:14 +00:00
|
|
|
|
|
|
|
func (c *ListStore) HighlightReference(ref markup.ReferenceSegment) {
|
|
|
|
msg := c.message(ref.MessageID(), "")
|
|
|
|
if msg != nil {
|
|
|
|
c.Highlight(msg)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *ListStore) Highlight(msg MessageRow) {
|
2021-03-29 21:46:52 +00:00
|
|
|
gts.ExecAsync(func() {
|
2021-05-01 07:26:17 +00:00
|
|
|
state := msg.Unwrap()
|
2021-03-29 21:46:52 +00:00
|
|
|
state.Row.GrabFocus()
|
|
|
|
c.ListBox.DragHighlightRow(state.Row)
|
2021-01-02 09:55:19 +00:00
|
|
|
gts.DoAfter(2*time.Second, c.ListBox.DragUnhighlightRow)
|
2021-01-02 09:24:14 +00:00
|
|
|
})
|
|
|
|
}
|
2021-03-29 21:46:52 +00:00
|
|
|
|
|
|
|
func destroyMsg(row *messageRow) {
|
2021-05-05 03:30:50 +00:00
|
|
|
row.Revert()
|
2021-05-01 07:26:17 +00:00
|
|
|
row.state.Author.Name.Stop()
|
|
|
|
row.state.Row.Destroy()
|
2021-03-29 21:46:52 +00:00
|
|
|
}
|