2020-06-06 07:44:36 +00:00
|
|
|
package container
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/diamondburned/cchat"
|
|
|
|
"github.com/diamondburned/cchat-gtk/internal/ui/messages/input"
|
|
|
|
"github.com/diamondburned/cchat-gtk/internal/ui/messages/message"
|
2020-07-16 05:41:21 +00:00
|
|
|
"github.com/diamondburned/cchat-gtk/internal/ui/primitives/menu"
|
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.
|
|
|
|
const BacklogLimit = 35
|
|
|
|
|
2020-06-06 07:44:36 +00:00
|
|
|
type GridMessage interface {
|
|
|
|
message.Container
|
2020-08-20 23:53:13 +00:00
|
|
|
// Focusable should return a widget that can be focused.
|
|
|
|
Focusable() gtk.IWidget
|
2020-06-13 07:29:32 +00:00
|
|
|
// Attach should only be called once.
|
2020-10-21 23:47:17 +00:00
|
|
|
Attach() []gtk.IWidget
|
2020-06-13 07:29:32 +00:00
|
|
|
// AttachMenu should override the stored constructor.
|
2020-06-17 22:58:38 +00:00
|
|
|
AttachMenu(items []menu.Item) // save memory
|
2020-06-13 07:29:32 +00:00
|
|
|
}
|
|
|
|
|
2020-06-06 07:44:36 +00:00
|
|
|
type PresendGridMessage interface {
|
|
|
|
GridMessage
|
|
|
|
message.PresendContainer
|
|
|
|
}
|
|
|
|
|
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
|
2020-06-13 07:29:32 +00:00
|
|
|
|
|
|
|
// Thread-safe methods.
|
2020-11-05 19:08:30 +00:00
|
|
|
// cchat.MessagesContainer
|
2020-06-06 07:44:36 +00:00
|
|
|
|
2020-06-13 07:29:32 +00:00
|
|
|
// Thread-unsafe methods.
|
2020-11-05 19:08:30 +00:00
|
|
|
|
2020-11-06 04:01:00 +00:00
|
|
|
// Reset resets the message container to its original state.
|
|
|
|
Reset()
|
|
|
|
|
2020-11-05 19:08:30 +00:00
|
|
|
// CreateMessageUnsafe creates a new message and returns the index that is
|
|
|
|
// the location the message is added to.
|
2020-10-23 06:21:34 +00:00
|
|
|
CreateMessageUnsafe(cchat.MessageCreate)
|
2020-06-13 07:29:32 +00:00
|
|
|
UpdateMessageUnsafe(cchat.MessageUpdate)
|
|
|
|
DeleteMessageUnsafe(cchat.MessageDelete)
|
|
|
|
|
2020-08-20 23:53:13 +00:00
|
|
|
// FirstMessage returns the first message in the buffer. Nil is returned if
|
|
|
|
// there's nothing.
|
|
|
|
FirstMessage() GridMessage
|
|
|
|
// TranslateCoordinates is used for scrolling to the message.
|
|
|
|
TranslateCoordinates(parent gtk.IWidget, msg GridMessage) (y int)
|
2020-06-13 07:29:32 +00:00
|
|
|
// AddPresendMessage adds and displays an unsent message.
|
|
|
|
AddPresendMessage(msg input.PresendMessage) PresendGridMessage
|
2020-06-17 22:58:38 +00:00
|
|
|
// LatestMessageFrom returns the last message ID with that author.
|
|
|
|
LatestMessageFrom(authorID string) (msgID string, ok bool)
|
2020-08-20 23:53:13 +00:00
|
|
|
|
|
|
|
// UI methods.
|
|
|
|
|
|
|
|
SetFocusHAdjustment(*gtk.Adjustment)
|
|
|
|
SetFocusVAdjustment(*gtk.Adjustment)
|
2020-06-06 07:44:36 +00:00
|
|
|
}
|
|
|
|
|
2020-06-13 07:29:32 +00:00
|
|
|
// Controller is for menu actions.
|
|
|
|
type Controller interface {
|
|
|
|
// BindMenu expects the controller to add actioner into the message.
|
|
|
|
BindMenu(GridMessage)
|
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.
|
2020-10-23 06:21:34 +00:00
|
|
|
AuthorEvent(a cchat.Author)
|
2020-06-13 07:29:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Constructor is an interface for making custom message implementations which
|
|
|
|
// allows GridContainer to generically work with.
|
|
|
|
type Constructor interface {
|
|
|
|
NewMessage(cchat.MessageCreate) GridMessage
|
|
|
|
NewPresendMessage(input.PresendMessage) PresendGridMessage
|
2020-06-07 04:27:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const ColumnSpacing = 10
|
|
|
|
|
2020-06-06 07:44:36 +00:00
|
|
|
// GridContainer is an implementation of Container, which allows flexible
|
|
|
|
// message grids.
|
|
|
|
type GridContainer struct {
|
2020-06-13 07:29:32 +00:00
|
|
|
*GridStore
|
2020-07-03 03:22:48 +00:00
|
|
|
Controller
|
2020-06-07 04:27:28 +00:00
|
|
|
}
|
|
|
|
|
2020-10-23 06:21:34 +00:00
|
|
|
// gridMessage w/ required internals
|
|
|
|
type gridMessage struct {
|
|
|
|
GridMessage
|
|
|
|
presend message.PresendContainer // this shouldn't be here but i'm lazy
|
|
|
|
}
|
|
|
|
|
2020-06-13 07:29:32 +00:00
|
|
|
var _ Container = (*GridContainer)(nil)
|
2020-06-06 07:44:36 +00:00
|
|
|
|
2020-06-13 07:29:32 +00:00
|
|
|
func NewGridContainer(constr Constructor, ctrl Controller) *GridContainer {
|
|
|
|
return &GridContainer{
|
2020-07-03 03:22:48 +00:00
|
|
|
GridStore: NewGridStore(constr, ctrl),
|
|
|
|
Controller: ctrl,
|
2020-06-06 07:44:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-06 03:23:06 +00:00
|
|
|
// CreateMessageUnsafe inserts a message. It does not clean up old messages.
|
2020-10-23 06:21:34 +00:00
|
|
|
func (c *GridContainer) CreateMessageUnsafe(msg cchat.MessageCreate) {
|
2020-06-13 21:51:03 +00:00
|
|
|
// Insert the message first.
|
2020-10-23 06:21:34 +00:00
|
|
|
c.GridStore.CreateMessageUnsafe(msg)
|
2020-11-06 03:23:06 +00:00
|
|
|
}
|
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.
|
|
|
|
func (c *GridContainer) 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
|
|
|
}
|