aboutsummaryrefslogtreecommitdiff
path: root/ui
diff options
context:
space:
mode:
authorTulir Asokan <tulir@maunium.net>2018-03-20 17:26:28 +0200
committerTulir Asokan <tulir@maunium.net>2018-03-20 17:26:37 +0200
commit59cd9003013aa62495d2a73c28f22fd7e64f861a (patch)
tree6af1094cc0d37661f66b1f49d1d18f77657516e3 /ui
parent508b4ea280c60a855ccbc3dff8c27c775aec067a (diff)
Simplify MessageView buffer recalculation to one loop
Diffstat (limited to 'ui')
-rw-r--r--ui/widget/message-view.go34
1 files changed, 16 insertions, 18 deletions
diff --git a/ui/widget/message-view.go b/ui/widget/message-view.go
index 070d982..14d026a 100644
--- a/ui/widget/message-view.go
+++ b/ui/widget/message-view.go
@@ -40,6 +40,7 @@ type MessageView struct {
widestSender int
prevWidth int
prevHeight int
+ prevMsgCount int
messageIDs map[string]bool
messages []*types.Message
@@ -66,6 +67,7 @@ func NewMessageView() *MessageView {
widestSender: 5,
prevWidth: -1,
prevHeight: -1,
+ prevMsgCount: -1,
}
}
@@ -107,23 +109,12 @@ func (view *MessageView) AddMessage(message *types.Message, direction int) {
view.ScrollOffset += len(message.Buffer)
}
view.messages = append(view.messages, message)
+ view.appendBuffer(message)
} else if direction == PrependMessage {
view.messages = append([]*types.Message{message}, view.messages...)
}
view.messageIDs[message.ID] = true
- view.appendBuffer(message)
-}
-
-func (view *MessageView) recalculateMessageBuffers() {
- _, _, width, _ := view.GetInnerRect()
- width -= view.TimestampWidth + TimestampSenderGap + view.widestSender + SenderMessageGap
- if width != view.prevWidth {
- for _, message := range view.messages {
- message.CalculateBuffer(width)
- }
- view.prevWidth = width
- }
}
func (view *MessageView) appendBuffer(message *types.Message) {
@@ -139,18 +130,26 @@ func (view *MessageView) appendBuffer(message *types.Message) {
for range message.Buffer {
view.metaBuffer = append(view.metaBuffer, message)
}
+ view.prevMsgCount++
}
-func (view *MessageView) recalculateBuffer() {
+func (view *MessageView) recalculateBuffers() {
_, _, width, height := view.GetInnerRect()
- view.textBuffer = make([]string, 0)
- view.metaBuffer = make([]types.MessageMeta, 0)
- if height != view.prevHeight || width != view.prevWidth {
+ width -= view.TimestampWidth + TimestampSenderGap + view.widestSender + SenderMessageGap
+ recalculateMessageBuffers := width != view.prevWidth
+ if height != view.prevHeight || recalculateMessageBuffers || len(view.messages) != view.prevMsgCount {
+ view.textBuffer = []string{}
+ view.metaBuffer = []types.MessageMeta{}
+ view.prevMsgCount = 0
for _, message := range view.messages {
+ if recalculateMessageBuffers {
+ message.CalculateBuffer(width)
+ }
view.appendBuffer(message)
}
view.prevHeight = height
+ view.prevWidth = width
}
}
@@ -244,8 +243,7 @@ func (view *MessageView) Draw(screen tcell.Screen) {
view.Box.Draw(screen)
x, y, _, height := view.GetInnerRect()
- view.recalculateMessageBuffers()
- view.recalculateBuffer()
+ view.recalculateBuffers()
if len(view.textBuffer) == 0 {
view.writeLine(screen, "It's quite empty in here.", x, y+height, tcell.ColorDefault)