2020-06-06 07:44:36 +00:00
|
|
|
package container
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/diamondburned/cchat"
|
|
|
|
"github.com/diamondburned/cchat-gtk/internal/ui/messages/message"
|
2021-01-03 04:52:30 +00:00
|
|
|
"github.com/diamondburned/cchat-gtk/internal/ui/primitives"
|
|
|
|
"github.com/diamondburned/handy"
|
2020-06-06 07:44:36 +00:00
|
|
|
"github.com/gotk3/gotk3/gtk"
|
|
|
|
)
|
|
|
|
|
2020-06-13 21:51:03 +00:00
|
|
|
// BacklogLimit is the maximum number of messages to store in the container at
|
|
|
|
// once.
|
2021-01-03 09:36:43 +00:00
|
|
|
const BacklogLimit = 50
|
2020-06-13 21:51:03 +00:00
|
|
|
|
2021-01-02 09:24:14 +00:00
|
|
|
type MessageRow interface {
|
2020-06-06 07:44:36 +00:00
|
|
|
message.Container
|
2021-03-29 21:46:52 +00:00
|
|
|
GrabFocus()
|
2020-06-13 07:29:32 +00:00
|
|
|
}
|
|
|
|
|
2021-01-02 09:24:14 +00:00
|
|
|
type PresendMessageRow interface {
|
|
|
|
MessageRow
|
2021-03-29 21:46:52 +00:00
|
|
|
message.Presender
|
2020-06-06 07:44:36 +00:00
|
|
|
}
|
|
|
|
|
2020-06-13 07:29:32 +00:00
|
|
|
// Container is a generic messages container for children messages for children
|
|
|
|
// packages.
|
2020-06-06 07:44:36 +00:00
|
|
|
type Container interface {
|
|
|
|
gtk.IWidget
|
2021-03-29 21:46:52 +00:00
|
|
|
cchat.MessagesContainer
|
2020-06-13 07:29:32 +00:00
|
|
|
|
2020-11-06 04:01:00 +00:00
|
|
|
// Reset resets the message container to its original state.
|
|
|
|
Reset()
|
|
|
|
|
2021-03-29 21:46:52 +00:00
|
|
|
// SetSelf sets the author for the current user.
|
|
|
|
SetSelf(self *message.Author)
|
|
|
|
|
|
|
|
// NewPresendMessage creates and adds a presend message state into the list.
|
|
|
|
NewPresendMessage(state *message.PresendState) PresendMessageRow
|
|
|
|
|
|
|
|
// AddMessage adds a new message into the list.
|
|
|
|
AddMessage(row MessageRow)
|
2020-06-13 07:29:32 +00:00
|
|
|
|
2020-08-20 23:53:13 +00:00
|
|
|
// FirstMessage returns the first message in the buffer. Nil is returned if
|
|
|
|
// there's nothing.
|
2021-01-02 09:24:14 +00:00
|
|
|
FirstMessage() MessageRow
|
2021-03-29 21:46:52 +00:00
|
|
|
// LastMessage returns the last message in the buffer or nil if there's
|
|
|
|
// nothing.
|
|
|
|
LastMessage() MessageRow
|
|
|
|
// Message finds and returns the message, if any. It performs maximum 2
|
|
|
|
// constant-time lookups.
|
2021-01-02 09:24:14 +00:00
|
|
|
Message(id cchat.ID, nonce string) MessageRow
|
2021-03-29 21:46:52 +00:00
|
|
|
// FindMessage finds a message that satisfies the given callback. It
|
|
|
|
// iterates the message buffer from latest to earliest.
|
2021-01-07 01:12:10 +00:00
|
|
|
FindMessage(isMessage func(MessageRow) bool) MessageRow
|
2021-01-02 09:24:14 +00:00
|
|
|
|
2021-01-02 09:55:19 +00:00
|
|
|
// Highlight temporarily highlights the given message for a short while.
|
2021-01-02 09:24:14 +00:00
|
|
|
Highlight(msg MessageRow)
|
2020-08-20 23:53:13 +00:00
|
|
|
|
|
|
|
// UI methods.
|
|
|
|
|
|
|
|
SetFocusHAdjustment(*gtk.Adjustment)
|
|
|
|
SetFocusVAdjustment(*gtk.Adjustment)
|
2020-06-06 07:44:36 +00:00
|
|
|
}
|
|
|
|
|
2021-03-29 21:46:52 +00:00
|
|
|
// UpdateMessage is a convenient function to update a message in the container.
|
|
|
|
// It does nothing if the message is not found.
|
|
|
|
func UpdateMessage(ct Container, update cchat.MessageUpdate) {
|
|
|
|
if msg := ct.Message(update.ID(), ""); msg != nil {
|
|
|
|
msg.UpdateContent(update.Content(), true)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// LatestMessageFrom returns the latest message from the given author ID.
|
|
|
|
func LatestMessageFrom(ct Container, authorID cchat.ID) MessageRow {
|
|
|
|
return ct.FindMessage(func(msg MessageRow) bool {
|
|
|
|
return msg.Unwrap(false).Author.ID == authorID
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-06-13 07:29:32 +00:00
|
|
|
// Controller is for menu actions.
|
|
|
|
type Controller interface {
|
2021-01-03 04:52:30 +00:00
|
|
|
// Connector is used for button press events to unselect messages.
|
|
|
|
primitives.Connector
|
2020-06-13 07:29:32 +00:00
|
|
|
// BindMenu expects the controller to add actioner into the message.
|
2021-01-02 09:24:14 +00:00
|
|
|
BindMenu(MessageRow)
|
2020-07-03 03:22:48 +00:00
|
|
|
// Bottomed returns whether or not the message scroller is at the bottom.
|
|
|
|
Bottomed() bool
|
2020-07-04 04:41:12 +00:00
|
|
|
// AuthorEvent is called on message create/update. This is used to update
|
|
|
|
// the typer state.
|
2021-03-29 21:46:52 +00:00
|
|
|
AuthorEvent(authorID cchat.ID)
|
2021-01-03 04:52:30 +00:00
|
|
|
// SelectMessage is called when a message is selected.
|
|
|
|
SelectMessage(list *ListStore, msg MessageRow)
|
|
|
|
// UnselectMessage is called when the message selection is cleared.
|
|
|
|
UnselectMessage()
|
2020-06-13 07:29:32 +00:00
|
|
|
}
|
|
|
|
|
2021-01-02 09:24:14 +00:00
|
|
|
const ColumnSpacing = 8
|
2020-06-07 04:27:28 +00:00
|
|
|
|
2021-01-02 09:24:14 +00:00
|
|
|
// ListContainer is an implementation of Container, which allows flexible
|
2020-06-06 07:44:36 +00:00
|
|
|
// message grids.
|
2021-01-02 09:24:14 +00:00
|
|
|
type ListContainer struct {
|
2021-01-03 04:52:30 +00:00
|
|
|
*handy.Clamp
|
|
|
|
|
2021-01-02 09:24:14 +00:00
|
|
|
*ListStore
|
2021-01-03 04:52:30 +00:00
|
|
|
|
2020-07-03 03:22:48 +00:00
|
|
|
Controller
|
2020-06-07 04:27:28 +00:00
|
|
|
}
|
|
|
|
|
2021-01-02 09:24:14 +00:00
|
|
|
// messageRow w/ required internals
|
|
|
|
type messageRow struct {
|
|
|
|
MessageRow
|
2021-03-29 21:46:52 +00:00
|
|
|
presend message.Presender // this shouldn't be here but i'm lazy
|
2020-10-23 06:21:34 +00:00
|
|
|
}
|
|
|
|
|
2021-01-05 02:05:33 +00:00
|
|
|
// unwrapRow is a helper that unwraps a messageRow if it's not nil. If it's nil,
|
|
|
|
// then a nil interface is returned.
|
|
|
|
func unwrapRow(msg *messageRow) MessageRow {
|
|
|
|
if msg == nil || msg.MessageRow == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return msg.MessageRow
|
|
|
|
}
|
|
|
|
|
2021-03-29 21:46:52 +00:00
|
|
|
func NewListContainer(ctrl Controller) *ListContainer {
|
|
|
|
listStore := NewListStore(ctrl)
|
2021-01-03 04:52:30 +00:00
|
|
|
listStore.ListBox.Show()
|
|
|
|
|
|
|
|
clamp := handy.ClampNew()
|
|
|
|
clamp.SetMaximumSize(800)
|
|
|
|
clamp.SetTighteningThreshold(600)
|
|
|
|
clamp.SetHExpand(true)
|
|
|
|
clamp.SetVExpand(true)
|
|
|
|
clamp.Add(listStore.ListBox)
|
|
|
|
clamp.Show()
|
|
|
|
|
2021-01-02 09:24:14 +00:00
|
|
|
return &ListContainer{
|
2021-01-03 04:52:30 +00:00
|
|
|
Clamp: clamp,
|
|
|
|
ListStore: listStore,
|
2020-07-03 03:22:48 +00:00
|
|
|
Controller: ctrl,
|
2020-06-06 07:44:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-29 21:46:52 +00:00
|
|
|
func (c *ListContainer) AddMessage(row MessageRow) {
|
|
|
|
c.ListStore.AddMessage(row)
|
|
|
|
c.CleanMessages()
|
|
|
|
}
|
2020-06-13 21:51:03 +00:00
|
|
|
|
2020-11-06 03:23:06 +00:00
|
|
|
// CleanMessages cleans up the oldest messages if the user is scrolled to the
|
|
|
|
// bottom. True is returned if there were changes.
|
2021-01-02 09:24:14 +00:00
|
|
|
func (c *ListContainer) CleanMessages() bool {
|
2020-06-13 21:51:03 +00:00
|
|
|
// Determine if the user is scrolled to the bottom for cleaning up.
|
2020-10-24 21:58:48 +00:00
|
|
|
if c.Bottomed() {
|
|
|
|
// Clean up the backlog.
|
2020-11-06 03:23:06 +00:00
|
|
|
if delta := c.MessagesLen() - BacklogLimit; delta > 0 {
|
|
|
|
c.DeleteEarliest(delta)
|
|
|
|
return true
|
|
|
|
}
|
2020-10-23 06:21:34 +00:00
|
|
|
}
|
2020-11-06 03:23:06 +00:00
|
|
|
|
|
|
|
return false
|
2020-06-13 21:51:03 +00:00
|
|
|
}
|
2021-01-03 04:52:30 +00:00
|
|
|
|
|
|
|
func (c *ListContainer) SetFocusHAdjustment(adj *gtk.Adjustment) {
|
|
|
|
c.ListBox.SetFocusHAdjustment(adj)
|
|
|
|
}
|
|
|
|
func (c *ListContainer) SetFocusVAdjustment(adj *gtk.Adjustment) {
|
|
|
|
c.ListBox.SetFocusVAdjustment(adj)
|
|
|
|
}
|