gomuks/ui/message-view.go

622 lines
16 KiB
Go
Raw Normal View History

2018-03-17 00:27:30 +01:00
// gomuks - A terminal Matrix client written in Go.
2019-01-17 13:13:25 +01:00
// Copyright (C) 2019 Tulir Asokan
2018-03-17 00:27:30 +01:00
//
// This program is free software: you can redistribute it and/or modify
2019-01-17 13:13:25 +01:00
// it under the terms of the GNU Affero General Public License as published by
2018-03-17 00:27:30 +01:00
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2019-01-17 13:13:25 +01:00
// GNU Affero General Public License for more details.
2018-03-17 00:27:30 +01:00
//
2019-01-17 13:13:25 +01:00
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
2018-03-17 00:27:30 +01:00
package ui
2018-03-17 00:27:30 +01:00
import (
"fmt"
"math"
"strings"
2019-04-27 14:02:21 +02:00
"sync/atomic"
2018-03-17 00:27:30 +01:00
"github.com/mattn/go-runewidth"
sync "github.com/sasha-s/go-deadlock"
2019-01-17 13:13:25 +01:00
2019-06-15 00:11:51 +02:00
"maunium.net/go/mautrix"
2019-03-25 23:37:35 +01:00
"maunium.net/go/mauview"
2019-01-17 13:13:25 +01:00
"maunium.net/go/tcell"
2018-06-01 23:44:21 +02:00
"maunium.net/go/gomuks/config"
"maunium.net/go/gomuks/debug"
"maunium.net/go/gomuks/interface"
2018-04-14 14:33:20 +02:00
"maunium.net/go/gomuks/lib/open"
"maunium.net/go/gomuks/ui/messages"
"maunium.net/go/gomuks/ui/widget"
2018-03-17 00:27:30 +01:00
)
type MessageView struct {
2018-04-14 14:33:20 +02:00
parent *RoomView
2018-06-01 23:43:56 +02:00
config *config.Config
2018-04-14 14:33:20 +02:00
2018-03-17 00:27:30 +01:00
ScrollOffset int
MaxSenderWidth int
DateFormat string
2018-03-17 00:27:30 +01:00
TimestampFormat string
TimestampWidth int
2019-04-27 14:02:21 +02:00
// Used for locking
loadingMessages int32
_widestSender uint32
_width uint32
_height uint32
_prevWidth uint32
_prevHeight uint32
prevMsgCount int
prevPrefs config.UserPreferences
messageIDLock sync.RWMutex
2019-06-15 00:11:51 +02:00
messageIDs map[string]*messages.UIMessage
2019-04-27 14:02:21 +02:00
messagesLock sync.RWMutex
2019-06-15 00:11:51 +02:00
messages []*messages.UIMessage
2019-04-27 14:02:21 +02:00
msgBufferLock sync.RWMutex
2019-06-15 00:11:51 +02:00
msgBuffer []*messages.UIMessage
initialHistoryLoaded bool
2018-03-17 00:27:30 +01:00
}
2018-04-14 14:33:20 +02:00
func NewMessageView(parent *RoomView) *MessageView {
2018-03-17 00:27:30 +01:00
return &MessageView{
2018-04-14 14:33:20 +02:00
parent: parent,
2018-06-01 23:43:56 +02:00
config: parent.config,
2018-04-14 14:33:20 +02:00
MaxSenderWidth: 15,
TimestampWidth: len(messages.TimeFormat),
ScrollOffset: 0,
2018-03-17 00:27:30 +01:00
2019-06-15 00:11:51 +02:00
messages: make([]*messages.UIMessage, 0),
messageIDs: make(map[string]*messages.UIMessage),
msgBuffer: make([]*messages.UIMessage, 0),
2019-04-27 14:02:21 +02:00
_width: 80,
_widestSender: 5,
_prevWidth: 0,
_prevHeight: 0,
prevMsgCount: -1,
2018-03-17 00:27:30 +01:00
}
}
2019-06-15 16:04:08 +02:00
func (view *MessageView) Unload() {
debug.Print("Unloading message view", view.parent.Room.ID)
view.messagesLock.Lock()
view.msgBufferLock.Lock()
view.messageIDLock.Lock()
view.messageIDs = make(map[string]*messages.UIMessage)
view.msgBuffer = make([]*messages.UIMessage, 0)
view.messages = make([]*messages.UIMessage, 0)
view.initialHistoryLoaded = false
view.ScrollOffset = 0
view._widestSender = 5
view.prevMsgCount = -1
view.messagesLock.Unlock()
view.msgBufferLock.Unlock()
view.messageIDLock.Unlock()
}
func (view *MessageView) updateWidestSender(sender string) {
2019-04-27 14:02:21 +02:00
if len(sender) > int(view._widestSender) {
if len(sender) > view.MaxSenderWidth {
atomic.StoreUint32(&view._widestSender, uint32(view.MaxSenderWidth))
} else {
atomic.StoreUint32(&view._widestSender, uint32(len(sender)))
2018-03-17 00:27:30 +01:00
}
}
}
2019-04-10 00:04:39 +02:00
type MessageDirection int
const (
AppendMessage MessageDirection = iota
PrependMessage
IgnoreMessage
)
2019-04-10 00:04:39 +02:00
func (view *MessageView) AddMessage(ifcMessage ifc.Message, direction MessageDirection) {
if ifcMessage == nil {
return
}
2019-06-15 00:11:51 +02:00
message, ok := ifcMessage.(*messages.UIMessage)
if !ok || message == nil {
debug.Print("[Warning] Passed non-UIMessage ifc.Message object to AddMessage().")
debug.PrintStack()
2018-03-22 18:51:20 +01:00
return
}
2019-06-15 00:11:51 +02:00
var oldMsg *messages.UIMessage
if oldMsg = view.getMessageByID(message.EventID); oldMsg != nil {
view.replaceMessage(oldMsg, message)
2019-04-10 00:04:39 +02:00
direction = IgnoreMessage
2019-06-15 00:11:51 +02:00
} else if oldMsg = view.getMessageByID(message.TxnID); oldMsg != nil {
view.replaceMessage(oldMsg, message)
2019-06-15 00:11:51 +02:00
view.deleteMessageID(message.TxnID)
direction = IgnoreMessage
} else if oldMsg = view.getMessageByID(message.Relation.EventID); oldMsg != nil {
2019-04-10 00:04:39 +02:00
direction = IgnoreMessage
2018-03-17 00:27:30 +01:00
}
view.updateWidestSender(message.Sender())
2019-04-27 14:02:21 +02:00
width := view.width()
2018-06-01 23:43:56 +02:00
bare := view.config.Preferences.BareMessageView
if !bare {
2019-04-27 14:02:21 +02:00
width -= view.TimestampWidth + TimestampSenderGap + view.widestSender() + SenderMessageGap
}
2018-06-01 23:43:56 +02:00
message.CalculateBuffer(view.config.Preferences, width)
2019-06-15 00:11:51 +02:00
makeDateChange := func() *messages.UIMessage {
dateChange := messages.NewDateChangeMessage(
fmt.Sprintf("Date changed to %s", message.FormatDate()))
dateChange.CalculateBuffer(view.config.Preferences, width)
view.appendBuffer(dateChange)
return dateChange
}
2019-04-10 00:04:39 +02:00
if direction == AppendMessage {
if view.ScrollOffset > 0 {
2018-03-22 18:51:20 +01:00
view.ScrollOffset += message.Height()
}
2019-04-27 14:02:21 +02:00
view.messagesLock.Lock()
if len(view.messages) > 0 && !view.messages[len(view.messages)-1].SameDate(message) {
view.messages = append(view.messages, makeDateChange(), message)
} else {
view.messages = append(view.messages, message)
}
2019-04-27 14:02:21 +02:00
view.messagesLock.Unlock()
view.appendBuffer(message)
2019-04-10 00:04:39 +02:00
} else if direction == PrependMessage {
2019-04-27 14:02:21 +02:00
view.messagesLock.Lock()
if len(view.messages) > 0 && !view.messages[0].SameDate(message) {
2019-06-15 00:11:51 +02:00
view.messages = append([]*messages.UIMessage{message, makeDateChange()}, view.messages...)
} else {
2019-06-15 00:11:51 +02:00
view.messages = append([]*messages.UIMessage{message}, view.messages...)
}
2019-04-27 14:02:21 +02:00
view.messagesLock.Unlock()
} else if oldMsg != nil {
view.replaceBuffer(oldMsg, message)
} else {
2018-04-24 15:51:40 +02:00
debug.Print("Unexpected AddMessage() call: Direction is not append or prepend, but message is new.")
debug.PrintStack()
2018-03-17 00:27:30 +01:00
}
if len(message.ID()) > 0 {
2019-04-27 14:02:21 +02:00
view.setMessageID(message)
2018-03-20 11:16:32 +01:00
}
}
2019-06-15 00:11:51 +02:00
func (view *MessageView) replaceMessage(original *messages.UIMessage, new *messages.UIMessage) {
if len(new.ID()) > 0 {
2019-04-27 14:02:21 +02:00
view.setMessageID(new)
}
2019-04-27 14:02:21 +02:00
view.messagesLock.Lock()
for index, msg := range view.messages {
if msg == original {
view.messages[index] = new
}
}
2019-04-27 14:02:21 +02:00
view.messagesLock.Unlock()
}
2019-06-15 00:11:51 +02:00
func (view *MessageView) getMessageByID(id string) *messages.UIMessage {
if id == "" {
return nil
}
2019-04-27 14:02:21 +02:00
view.messageIDLock.RLock()
defer view.messageIDLock.RUnlock()
msg, ok := view.messageIDs[id]
if !ok {
return nil
}
return msg
}
func (view *MessageView) deleteMessageID(id string) {
2019-06-15 00:11:51 +02:00
if id == "" {
return
}
2019-04-27 14:02:21 +02:00
view.messageIDLock.Lock()
delete(view.messageIDs, id)
view.messageIDLock.Unlock()
}
2019-06-15 00:11:51 +02:00
func (view *MessageView) setMessageID(message *messages.UIMessage) {
if message.ID() == "" {
return
}
2019-04-27 14:02:21 +02:00
view.messageIDLock.Lock()
view.messageIDs[message.ID()] = message
view.messageIDLock.Unlock()
}
2019-06-15 00:11:51 +02:00
func (view *MessageView) appendBuffer(message *messages.UIMessage) {
2019-04-27 14:02:21 +02:00
view.msgBufferLock.Lock()
view.appendBufferUnlocked(message)
view.msgBufferLock.Unlock()
}
2019-06-15 00:11:51 +02:00
func (view *MessageView) appendBufferUnlocked(message *messages.UIMessage) {
2019-04-27 14:02:21 +02:00
for i := 0; i < message.Height(); i++ {
view.msgBuffer = append(view.msgBuffer, message)
}
view.prevMsgCount++
}
2019-06-15 00:11:51 +02:00
func (view *MessageView) replaceBuffer(original *messages.UIMessage, new *messages.UIMessage) {
start := -1
end := -1
2019-04-27 14:02:21 +02:00
view.msgBufferLock.RLock()
for index, meta := range view.msgBuffer {
if meta == original {
if start == -1 {
start = index
}
end = index
} else if start != -1 {
break
}
}
2019-04-27 14:02:21 +02:00
view.msgBufferLock.RUnlock()
if start == -1 {
debug.Print("Called replaceBuffer() with message that was not in the buffer:", original)
2019-06-15 00:11:51 +02:00
//debug.PrintStack()
view.appendBuffer(new)
return
}
if len(view.msgBuffer) > end {
end++
}
2019-04-07 17:21:38 +02:00
if new.Height() == 0 {
2019-04-27 14:02:21 +02:00
new.CalculateBuffer(view.prevPrefs, view.prevWidth())
2019-04-07 17:21:38 +02:00
}
2019-04-27 14:02:21 +02:00
view.msgBufferLock.Lock()
2019-04-07 17:21:38 +02:00
if new.Height() != end-start {
metaBuffer := view.msgBuffer[0:start]
2019-04-07 17:21:38 +02:00
for i := 0; i < new.Height(); i++ {
metaBuffer = append(metaBuffer, new)
}
view.msgBuffer = append(metaBuffer, view.msgBuffer[end:]...)
} else {
for i := start; i < end; i++ {
view.msgBuffer[i] = new
}
}
2019-04-27 14:02:21 +02:00
view.msgBufferLock.Unlock()
}
func (view *MessageView) recalculateBuffers() {
2018-06-01 23:43:56 +02:00
prefs := view.config.Preferences
2019-04-27 14:02:21 +02:00
recalculateMessageBuffers := view.width() != view.prevWidth() ||
2018-06-01 23:43:56 +02:00
view.prevPrefs.BareMessageView != prefs.BareMessageView ||
view.prevPrefs.DisableImages != prefs.DisableImages
2019-04-27 14:02:21 +02:00
view.messagesLock.RLock()
view.msgBufferLock.Lock()
if recalculateMessageBuffers || len(view.messages) != view.prevMsgCount {
2019-04-27 14:02:21 +02:00
width := view.width()
2019-03-25 23:37:35 +01:00
if !prefs.BareMessageView {
2019-04-27 14:02:21 +02:00
width -= view.TimestampWidth + TimestampSenderGap + view.widestSender() + SenderMessageGap
2019-03-25 23:37:35 +01:00
}
2019-06-15 00:11:51 +02:00
view.msgBuffer = []*messages.UIMessage{}
view.prevMsgCount = 0
for i, message := range view.messages {
if message == nil {
debug.Print("O.o found nil message at", i)
break
}
if recalculateMessageBuffers {
2018-06-01 23:43:56 +02:00
message.CalculateBuffer(prefs, width)
}
2019-04-27 14:02:21 +02:00
view.appendBufferUnlocked(message)
2018-03-17 00:27:30 +01:00
}
}
2019-04-27 14:02:21 +02:00
view.msgBufferLock.Unlock()
view.messagesLock.RUnlock()
view.updatePrevSize()
2018-06-01 23:43:56 +02:00
view.prevPrefs = prefs
2018-03-17 00:27:30 +01:00
}
2019-06-15 00:11:51 +02:00
func (view *MessageView) handleMessageClick(message *messages.UIMessage) bool {
switch msg := message.Renderer.(type) {
2018-04-15 13:03:05 +02:00
case *messages.ImageMessage:
2019-06-15 00:11:51 +02:00
open.Open(msg.Path())
default:
debug.Print("Message clicked:", message)
2018-04-15 13:03:05 +02:00
}
return false
}
2019-06-15 00:11:51 +02:00
func (view *MessageView) handleUsernameClick(message *messages.UIMessage, prevMessage *messages.UIMessage) bool {
if prevMessage != nil && prevMessage.Sender() == message.Sender() {
2018-04-15 13:03:05 +02:00
return false
}
if len(message.Sender()) == 0 {
2018-04-15 13:03:05 +02:00
return false
}
2019-06-15 00:11:51 +02:00
sender := fmt.Sprintf("[%s](https://matrix.to/#/%s)", message.Sender(), message.SenderID)
2018-04-15 13:03:05 +02:00
cursorPos := view.parent.input.GetCursorOffset()
text := view.parent.input.GetText()
var buf strings.Builder
2018-04-15 13:03:05 +02:00
if cursorPos == 0 {
buf.WriteString(sender)
buf.WriteRune(':')
buf.WriteRune(' ')
buf.WriteString(text)
2018-04-15 13:03:05 +02:00
} else {
textBefore := runewidth.Truncate(text, cursorPos, "")
textAfter := text[len(textBefore):]
buf.WriteString(textBefore)
buf.WriteString(sender)
buf.WriteRune(' ')
buf.WriteString(textAfter)
2018-04-15 13:03:05 +02:00
}
newText := buf.String()
2018-04-15 13:03:05 +02:00
view.parent.input.SetText(string(newText))
view.parent.input.SetCursorOffset(cursorPos + len(newText) - len(text))
return true
}
2019-03-25 23:37:35 +01:00
func (view *MessageView) OnMouseEvent(event mauview.MouseEvent) bool {
switch event.Buttons() {
case tcell.WheelUp:
if view.IsAtTop() {
go view.parent.parent.LoadHistory(view.parent.Room.ID)
} else {
view.AddScrollOffset(WheelScrollOffsetDiff)
return true
}
case tcell.WheelDown:
view.AddScrollOffset(-WheelScrollOffsetDiff)
view.parent.parent.MarkRead(view.parent)
return true
case tcell.Button1:
x, y := event.Position()
2019-04-27 14:02:21 +02:00
line := view.TotalHeight() - view.ScrollOffset - view.Height() + y
2019-03-25 23:37:35 +01:00
if line < 0 || line >= view.TotalHeight() {
return false
}
2019-04-27 14:02:21 +02:00
view.msgBufferLock.RLock()
message := view.msgBuffer[line]
2019-06-15 00:11:51 +02:00
var prevMessage *messages.UIMessage
2019-03-25 23:37:35 +01:00
if y != 0 && line > 0 {
prevMessage = view.msgBuffer[line-1]
2019-03-25 23:37:35 +01:00
}
2019-04-27 14:02:21 +02:00
view.msgBufferLock.RUnlock()
2018-04-14 14:33:20 +02:00
2019-03-25 23:37:35 +01:00
usernameX := view.TimestampWidth + TimestampSenderGap
2019-04-27 14:02:21 +02:00
messageX := usernameX + view.widestSender() + SenderMessageGap
2018-04-15 13:03:05 +02:00
2019-03-25 23:37:35 +01:00
if x >= messageX {
return view.handleMessageClick(message)
} else if x >= usernameX {
return view.handleUsernameClick(message, prevMessage)
}
2018-04-14 14:33:20 +02:00
}
2019-03-25 23:37:35 +01:00
return false
}
2018-03-20 11:16:32 +01:00
const PaddingAtTop = 5
func (view *MessageView) AddScrollOffset(diff int) {
totalHeight := view.TotalHeight()
2019-04-27 14:02:21 +02:00
height := view.Height()
if diff >= 0 && view.ScrollOffset+diff >= totalHeight-height+PaddingAtTop {
view.ScrollOffset = totalHeight - height + PaddingAtTop
2018-03-26 13:31:44 +02:00
} else {
view.ScrollOffset += diff
2018-03-20 11:16:32 +01:00
}
2019-04-27 14:02:21 +02:00
if view.ScrollOffset > totalHeight-height+PaddingAtTop {
view.ScrollOffset = totalHeight - height + PaddingAtTop
2018-03-26 13:31:44 +02:00
}
if view.ScrollOffset < 0 {
view.ScrollOffset = 0
2018-03-20 11:16:32 +01:00
}
}
2019-04-27 14:02:21 +02:00
func (view *MessageView) setSize(width, height int) {
atomic.StoreUint32(&view._width, uint32(width))
atomic.StoreUint32(&view._height, uint32(height))
}
func (view *MessageView) updatePrevSize() {
atomic.StoreUint32(&view._prevWidth, atomic.LoadUint32(&view._width))
atomic.StoreUint32(&view._prevHeight, atomic.LoadUint32(&view._height))
}
func (view *MessageView) prevHeight() int {
return int(atomic.LoadUint32(&view._prevHeight))
}
func (view *MessageView) prevWidth() int {
return int(atomic.LoadUint32(&view._prevWidth))
}
func (view *MessageView) widestSender() int {
return int(atomic.LoadUint32(&view._widestSender))
}
func (view *MessageView) Height() int {
2019-04-27 14:02:21 +02:00
return int(atomic.LoadUint32(&view._height))
}
func (view *MessageView) width() int {
return int(atomic.LoadUint32(&view._width))
}
func (view *MessageView) TotalHeight() int {
2019-04-27 14:02:21 +02:00
view.msgBufferLock.RLock()
defer view.msgBufferLock.RUnlock()
return len(view.msgBuffer)
2018-03-17 00:27:30 +01:00
}
func (view *MessageView) IsAtTop() bool {
2019-04-27 14:02:21 +02:00
return view.ScrollOffset >= view.TotalHeight()-view.Height()+PaddingAtTop
2018-03-17 00:27:30 +01:00
}
const (
TimestampSenderGap = 1
SenderSeparatorGap = 1
SenderMessageGap = 3
)
2018-03-26 13:31:44 +02:00
func getScrollbarStyle(scrollbarHere, isTop, isBottom bool) (char rune, style tcell.Style) {
char = '│'
style = tcell.StyleDefault
if scrollbarHere {
style = style.Foreground(tcell.ColorGreen)
}
if isTop {
if scrollbarHere {
char = '╥'
} else {
char = '┬'
}
} else if isBottom {
if scrollbarHere {
char = '╨'
} else {
char = '┴'
}
} else if scrollbarHere {
char = '║'
}
return
}
2018-04-15 13:03:05 +02:00
func (view *MessageView) calculateScrollBar(height int) (scrollBarHeight, scrollBarPos int) {
viewportHeight := float64(height)
contentHeight := float64(view.TotalHeight())
scrollBarHeight = int(math.Ceil(viewportHeight / (contentHeight / viewportHeight)))
scrollBarPos = height - int(math.Round(float64(view.ScrollOffset)/contentHeight*viewportHeight))
return
}
2019-03-25 23:37:35 +01:00
func (view *MessageView) getIndexOffset(screen mauview.Screen, height, messageX int) (indexOffset int) {
2018-04-15 13:03:05 +02:00
indexOffset = view.TotalHeight() - view.ScrollOffset - height
if indexOffset <= -PaddingAtTop {
message := "Scroll up to load more messages."
2019-04-27 14:02:21 +02:00
if atomic.LoadInt32(&view.loadingMessages) == 1 {
2018-04-15 13:03:05 +02:00
message = "Loading more messages..."
}
2019-03-25 23:37:35 +01:00
widget.WriteLineSimpleColor(screen, message, messageX, 0, tcell.ColorGreen)
2018-04-15 13:03:05 +02:00
}
return
}
2018-03-17 00:27:30 +01:00
2018-05-22 23:44:29 +02:00
func (view *MessageView) CapturePlaintext(height int) string {
var buf strings.Builder
indexOffset := view.TotalHeight() - view.ScrollOffset - height
2019-06-15 00:11:51 +02:00
var prevMessage *messages.UIMessage
2019-04-27 14:02:21 +02:00
view.msgBufferLock.RLock()
2018-05-22 23:44:29 +02:00
for line := 0; line < height; line++ {
index := indexOffset + line
if index < 0 {
continue
}
2019-06-15 00:11:51 +02:00
message := view.msgBuffer[index]
if message != prevMessage {
var sender string
if len(message.Sender()) > 0 {
sender = fmt.Sprintf(" <%s>", message.Sender())
2019-06-15 00:11:51 +02:00
} else if message.Type == mautrix.MsgEmote {
sender = fmt.Sprintf(" * %s", message.SenderName)
}
fmt.Fprintf(&buf, "%s%s %s\n", message.FormatTime(), sender, message.PlainText())
2018-05-22 23:44:29 +02:00
prevMessage = message
}
}
2019-04-27 14:02:21 +02:00
view.msgBufferLock.RUnlock()
2018-05-22 23:44:29 +02:00
return buf.String()
}
2019-03-25 23:37:35 +01:00
func (view *MessageView) Draw(screen mauview.Screen) {
2019-04-27 14:02:21 +02:00
view.setSize(screen.Size())
view.recalculateBuffers()
2019-04-27 14:02:21 +02:00
height := view.Height()
if view.TotalHeight() == 0 {
2019-04-27 14:02:21 +02:00
widget.WriteLineSimple(screen, "It's quite empty in here.", 0, height)
return
2018-03-17 00:27:30 +01:00
}
2019-03-25 23:37:35 +01:00
usernameX := view.TimestampWidth + TimestampSenderGap
2019-04-27 14:02:21 +02:00
messageX := usernameX + view.widestSender() + SenderMessageGap
2018-03-17 00:27:30 +01:00
2018-06-01 23:43:56 +02:00
bareMode := view.config.Preferences.BareMessageView
if bareMode {
2019-03-25 23:37:35 +01:00
messageX = 0
}
2019-04-27 14:02:21 +02:00
indexOffset := view.getIndexOffset(screen, height, messageX)
2018-03-22 22:40:26 +01:00
viewStart := 0
if indexOffset < 0 {
viewStart = -indexOffset
}
2018-03-26 13:31:44 +02:00
if !bareMode {
2019-04-27 14:02:21 +02:00
separatorX := usernameX + view.widestSender() + SenderSeparatorGap
scrollBarHeight, scrollBarPos := view.calculateScrollBar(height)
2019-04-27 14:02:21 +02:00
for line := viewStart; line < height; line++ {
showScrollbar := line-viewStart >= scrollBarPos-scrollBarHeight && line-viewStart < scrollBarPos
2019-04-27 14:02:21 +02:00
isTop := line == viewStart && view.ScrollOffset+height >= view.TotalHeight()
isBottom := line == height-1 && view.ScrollOffset == 0
borderChar, borderStyle := getScrollbarStyle(showScrollbar, isTop, isBottom)
2018-03-26 13:31:44 +02:00
2019-03-25 23:37:35 +01:00
screen.SetContent(separatorX, line, borderChar, nil, borderStyle)
}
}
2019-06-15 00:11:51 +02:00
var prevMsg *messages.UIMessage
2019-04-27 14:02:21 +02:00
view.msgBufferLock.RLock()
for line := viewStart; line < height && indexOffset+line < len(view.msgBuffer); line++ {
index := indexOffset + line
msg := view.msgBuffer[index]
if msg != prevMsg {
if len(msg.FormatTime()) > 0 {
widget.WriteLineSimpleColor(screen, msg.FormatTime(), 0, line, msg.TimestampColor())
2018-03-20 12:01:59 +01:00
}
// TODO hiding senders might not be that nice after all, maybe an option? (disabled for now)
//if !bareMode && (prevMsg == nil || meta.Sender() != prevMsg.Sender()) {
widget.WriteLineColor(
screen, mauview.AlignRight, msg.Sender(),
2019-04-27 14:02:21 +02:00
usernameX, line, view.widestSender(),
msg.SenderColor())
//}
prevMsg = msg
2019-04-07 17:21:38 +02:00
}
for i := index - 1; i >= 0 && view.msgBuffer[i] == msg; i-- {
line--
2018-03-17 00:27:30 +01:00
}
2019-04-27 14:02:21 +02:00
msg.Draw(mauview.NewProxyScreen(screen, messageX, line, view.width()-messageX, msg.Height()))
line += msg.Height() - 1
2018-03-17 00:27:30 +01:00
}
2019-04-27 14:02:21 +02:00
view.msgBufferLock.RUnlock()
2018-03-17 00:27:30 +01:00
}