gomuks/ui/message-view.go

671 lines
18 KiB
Go
Raw Normal View History

2018-03-17 00:27:30 +01:00
// gomuks - A terminal Matrix client written in Go.
2020-04-19 17:10:14 +02:00
// Copyright (C) 2020 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-03-25 23:37:35 +01:00
"maunium.net/go/mauview"
2019-01-17 13:13:25 +01:00
"maunium.net/go/tcell"
"maunium.net/go/mautrix/event"
"maunium.net/go/mautrix/id"
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
historyLoadPtr uint64
2019-04-27 14:02:21 +02:00
_widestSender uint32
_prevWidestSender uint32
_width uint32
_height uint32
_prevWidth uint32
_prevHeight uint32
prevMsgCount int
prevPrefs config.UserPreferences
2019-04-27 14:02:21 +02:00
messageIDLock sync.RWMutex
messageIDs map[id.EventID]*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
2020-02-20 23:29:29 +01:00
selected *messages.UIMessage
2019-06-15 00:11:51 +02:00
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[id.EventID]*messages.UIMessage),
2019-06-15 00:11:51 +02:00
msgBuffer: make([]*messages.UIMessage, 0),
_widestSender: 5,
_prevWidestSender: 0,
_width: 80,
_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[id.EventID]*messages.UIMessage)
2019-06-15 16:04:08 +02:00
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.historyLoadPtr = 0
2019-06-15 16:04:08 +02:00
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
} else if oldMsg = view.getMessageByID(id.EventID(message.TxnID)); oldMsg != nil {
view.replaceMessage(oldMsg, message)
view.deleteMessageID(id.EventID(message.TxnID))
2019-06-15 00:11:51 +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()
}
func (view *MessageView) getMessageByID(id id.EventID) *messages.UIMessage {
2019-06-15 00:11:51 +02:00
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 id.EventID) {
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 {
2020-02-20 20:56:03 +01:00
height := new.Height()
2020-02-20 23:29:29 +01:00
newBuffer := make([]*messages.UIMessage, height+len(view.msgBuffer)-end)
2020-02-20 20:56:03 +01:00
for i := 0; i < height; i++ {
newBuffer[i] = new
}
for i := height; i < len(newBuffer); i++ {
2020-02-20 23:29:29 +01:00
newBuffer[i] = view.msgBuffer[end+(i-height)]
}
2020-02-20 20:56:03 +01:00
view.msgBuffer = append(view.msgBuffer[0:start], newBuffer...)
} 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() ||
view.widestSender() != view.prevWidestSender() ||
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
}
2020-02-20 23:29:29 +01:00
func (view *MessageView) SetSelected(message *messages.UIMessage) {
if view.selected != nil {
view.selected.IsSelected = false
}
if message != nil && (view.selected == message || message.IsService) {
view.selected = nil
} else {
view.selected = message
}
2020-02-20 23:29:29 +01:00
if view.selected != nil {
view.selected.IsSelected = true
}
}
func (view *MessageView) handleMessageClick(message *messages.UIMessage, mod tcell.ModMask) bool {
if msg, ok := message.Renderer.(*messages.FileMessage); ok && mod > 0 && !msg.Thumbnail.IsEmpty() {
2020-10-02 17:22:00 +02:00
debug.Print("Opening thumbnail", msg.ThumbnailPath())
open.Open(msg.ThumbnailPath())
2020-02-20 23:29:29 +01:00
// No need to re-render
return false
2018-04-15 13:03:05 +02:00
}
2020-02-20 23:29:29 +01:00
view.SetSelected(message)
view.parent.OnSelect(view.selected)
2020-02-20 23:29:29 +01:00
return true
2018-04-15 13:03:05 +02:00
}
2019-06-15 00:11:51 +02:00
func (view *MessageView) handleUsernameClick(message *messages.UIMessage, prevMessage *messages.UIMessage) bool {
// TODO this is needed if senders are hidden for messages from the same sender (see Draw method)
//if prevMessage != nil && prevMessage.SenderName == message.SenderName {
// return false
//}
2018-04-15 13:03:05 +02:00
if message.SenderName == "---" || message.SenderName == "-->" || message.SenderName == "<--" || message.Type == event.MsgEmote {
2018-04-15 13:03:05 +02:00
return false
}
sender := fmt.Sprintf("[%s](https://matrix.to/#/%s)", message.SenderName, 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 {
if event.HasMotion() {
return false
}
2019-03-25 23:37:35 +01:00
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 {
2020-02-20 23:29:29 +01:00
return view.handleMessageClick(message, event.Modifiers())
2019-03-25 23:37:35 +01:00
} 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))
atomic.StoreUint32(&view._prevWidestSender, atomic.LoadUint32(&view._widestSender))
2019-04-27 14:02:21 +02:00
}
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) prevWidestSender() int {
return int(atomic.LoadUint32(&view._prevWidestSender))
}
2019-04-27 14:02:21 +02:00
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())
} else if message.Type == event.MsgEmote {
2019-06-15 00:11:51 +02:00
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()
2020-02-20 20:56:03 +01:00
for line := viewStart; line < height && indexOffset+line < len(view.msgBuffer); {
index := indexOffset + line
msg := view.msgBuffer[index]
2020-02-20 20:56:03 +01:00
if msg == prevMsg {
debug.Print("Unexpected re-encounter of", msg, msg.Height(), "at", line, index)
line++
continue
}
if len(msg.FormatTime()) > 0 {
widget.WriteLineSimpleColor(screen, msg.FormatTime(), 0, line, msg.TimestampColor())
}
// 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(),
usernameX, line, view.widestSender(),
msg.SenderColor())
//}
if msg.Edited {
// TODO add better indicator for edits
screen.SetCell(usernameX+view.widestSender(), line, tcell.StyleDefault.Foreground(tcell.ColorDarkRed), '*')
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()))
2020-02-20 20:56:03 +01:00
line += msg.Height()
prevMsg = msg
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
}