diff --git a/internal/ui/messages/container/compact/compact.go b/internal/ui/messages/container/compact/compact.go index 96d5675..f31eb98 100644 --- a/internal/ui/messages/container/compact/compact.go +++ b/internal/ui/messages/container/compact/compact.go @@ -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) { diff --git a/internal/ui/messages/container/container.go b/internal/ui/messages/container/container.go index 36662f5..67b4a56 100644 --- a/internal/ui/messages/container/container.go +++ b/internal/ui/messages/container/container.go @@ -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 } diff --git a/internal/ui/messages/container/cozy/cozy.go b/internal/ui/messages/container/cozy/cozy.go index 1efb4eb..b846fa7 100644 --- a/internal/ui/messages/container/cozy/cozy.go +++ b/internal/ui/messages/container/cozy/cozy.go @@ -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())