1
0
Fork 0
mirror of https://github.com/diamondburned/cchat-gtk.git synced 2024-10-31 19:44:23 +00:00

minor message buffer improvements

This commit is contained in:
diamondburned 2020-11-05 19:23:06 -08:00
parent f5ba082b86
commit 2f64bb509a
3 changed files with 16 additions and 5 deletions

View file

@ -19,7 +19,10 @@ func NewContainer(ctrl container.Controller) *Container {
}
func (c *Container) CreateMessage(msg cchat.MessageCreate) {
gts.ExecAsync(func() { c.GridContainer.CreateMessageUnsafe(msg) })
gts.ExecAsync(func() {
c.GridContainer.CreateMessageUnsafe(msg)
c.GridContainer.CleanMessages()
})
}
func (c *Container) UpdateMessage(msg cchat.MessageUpdate) {

View file

@ -101,15 +101,23 @@ func NewGridContainer(constr Constructor, ctrl Controller) *GridContainer {
}
}
// CreateMessageUnsafe inserts a message as well as cleaning up the backlog if
// the user is scrolled to the bottom.
// CreateMessageUnsafe inserts a message. It does not clean up old messages.
func (c *GridContainer) CreateMessageUnsafe(msg cchat.MessageCreate) {
// Insert the message first.
c.GridStore.CreateMessageUnsafe(msg)
}
// 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 {
// Determine if the user is scrolled to the bottom for cleaning up.
if c.Bottomed() {
// Clean up the backlog.
c.DeleteEarliest(c.MessagesLen() - BacklogLimit)
if delta := c.MessagesLen() - BacklogLimit; delta > 0 {
c.DeleteEarliest(delta)
return true
}
}
return false
}

View file

@ -134,7 +134,7 @@ func (c *Container) CreateMessage(msg cchat.MessageCreate) {
// Did the handler wipe old messages? It will only do so if the user is
// scrolled to the bottom.
if c.Bottomed() {
if c.GridContainer.CleanMessages() {
// We need to uncollapse the first (top) message. No length check is
// needed here, as we just inserted a message.
c.uncompact(c.FirstMessage())