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
|
|
|
|
2018-04-09 22:45:54 +02:00
|
|
|
package ui
|
2018-03-17 00:27:30 +01:00
|
|
|
|
|
|
|
import (
|
2018-03-18 16:34:42 +01:00
|
|
|
"fmt"
|
2018-03-23 16:26:06 +01:00
|
|
|
"math"
|
2018-04-18 12:38:33 +02:00
|
|
|
"strings"
|
2019-04-27 14:02:21 +02:00
|
|
|
"sync/atomic"
|
2018-03-17 00:27:30 +01:00
|
|
|
|
2018-04-18 12:38:33 +02:00
|
|
|
"github.com/mattn/go-runewidth"
|
2019-04-27 14:02:52 +02:00
|
|
|
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"
|
2018-04-09 22:45:54 +02:00
|
|
|
"maunium.net/go/gomuks/debug"
|
|
|
|
"maunium.net/go/gomuks/interface"
|
2018-04-14 14:33:20 +02:00
|
|
|
"maunium.net/go/gomuks/lib/open"
|
2018-04-09 22:45:54 +02:00
|
|
|
"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
|
2018-03-18 16:34:42 +01:00
|
|
|
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
|
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
|
|
|
|
2018-04-10 15:07:16 +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),
|
2018-03-18 16:34:42 +01:00
|
|
|
|
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()
|
|
|
|
}
|
|
|
|
|
2018-03-18 16:34:42 +01:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
2018-03-18 16:34:42 +01:00
|
|
|
}
|
|
|
|
|
2019-04-10 00:04:39 +02:00
|
|
|
type MessageDirection int
|
|
|
|
|
|
|
|
const (
|
|
|
|
AppendMessage MessageDirection = iota
|
|
|
|
PrependMessage
|
|
|
|
IgnoreMessage
|
|
|
|
)
|
2018-03-20 18:14:39 +01:00
|
|
|
|
2019-04-10 00:04:39 +02:00
|
|
|
func (view *MessageView) AddMessage(ifcMessage ifc.Message, direction MessageDirection) {
|
2018-04-09 22:45:54 +02:00
|
|
|
if ifcMessage == nil {
|
|
|
|
return
|
|
|
|
}
|
2019-06-15 00:11:51 +02:00
|
|
|
message, ok := ifcMessage.(*messages.UIMessage)
|
|
|
|
if !ok || message == nil {
|
2018-04-09 22:45:54 +02:00
|
|
|
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 {
|
2019-04-09 17:45:41 +02:00
|
|
|
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 {
|
2018-04-18 13:20:57 +02:00
|
|
|
view.replaceMessage(oldMsg, message)
|
2019-06-15 00:11:51 +02:00
|
|
|
view.deleteMessageID(message.TxnID)
|
|
|
|
direction = IgnoreMessage
|
2018-03-17 00:27:30 +01:00
|
|
|
}
|
2018-03-18 16:34:42 +01:00
|
|
|
|
2018-04-09 22:45:54 +02:00
|
|
|
view.updateWidestSender(message.Sender())
|
2018-03-18 16:34:42 +01:00
|
|
|
|
2019-04-27 14:02:21 +02:00
|
|
|
width := view.width()
|
2018-06-01 23:43:56 +02:00
|
|
|
bare := view.config.Preferences.BareMessageView
|
2018-05-22 21:06:48 +02:00
|
|
|
if !bare {
|
2019-04-27 14:02:21 +02:00
|
|
|
width -= view.TimestampWidth + TimestampSenderGap + view.widestSender() + SenderMessageGap
|
2018-05-22 21:06:48 +02:00
|
|
|
}
|
2018-06-01 23:43:56 +02:00
|
|
|
message.CalculateBuffer(view.config.Preferences, width)
|
2018-03-18 16:34:42 +01:00
|
|
|
|
2019-06-15 00:11:51 +02:00
|
|
|
makeDateChange := func() *messages.UIMessage {
|
2019-04-10 00:42:27 +02:00
|
|
|
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 {
|
2018-03-18 16:34:42 +01:00
|
|
|
if view.ScrollOffset > 0 {
|
2018-03-22 18:51:20 +01:00
|
|
|
view.ScrollOffset += message.Height()
|
2018-03-18 16:34:42 +01:00
|
|
|
}
|
2019-04-27 14:02:21 +02:00
|
|
|
view.messagesLock.Lock()
|
2019-04-10 00:42:27 +02:00
|
|
|
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()
|
2018-03-20 16:26:28 +01:00
|
|
|
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()
|
2019-04-10 00:42:27 +02:00
|
|
|
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...)
|
2019-04-10 00:42:27 +02:00
|
|
|
} else {
|
2019-06-15 00:11:51 +02:00
|
|
|
view.messages = append([]*messages.UIMessage{message}, view.messages...)
|
2019-04-10 00:42:27 +02:00
|
|
|
}
|
2019-04-27 14:02:21 +02:00
|
|
|
view.messagesLock.Unlock()
|
2018-04-18 13:20:57 +02:00
|
|
|
} else if oldMsg != nil {
|
|
|
|
view.replaceBuffer(oldMsg, message)
|
2018-04-10 15:07:16 +02:00
|
|
|
} 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
|
|
|
}
|
2018-03-18 16:34:42 +01:00
|
|
|
|
2019-04-10 00:42:27 +02: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) {
|
2019-04-10 00:42:27 +02:00
|
|
|
if len(new.ID()) > 0 {
|
2019-04-27 14:02:21 +02:00
|
|
|
view.setMessageID(new)
|
2019-04-10 00:42:27 +02:00
|
|
|
}
|
2019-04-27 14:02:21 +02:00
|
|
|
view.messagesLock.Lock()
|
2018-04-18 13:20:57 +02:00
|
|
|
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++
|
2018-04-18 13:20:57 +02:00
|
|
|
}
|
|
|
|
|
2019-06-15 00:11:51 +02:00
|
|
|
func (view *MessageView) replaceBuffer(original *messages.UIMessage, new *messages.UIMessage) {
|
2018-04-10 15:07:16 +02:00
|
|
|
start := -1
|
|
|
|
end := -1
|
2019-04-27 14:02:21 +02:00
|
|
|
view.msgBufferLock.RLock()
|
2019-04-09 17:45:41 +02:00
|
|
|
for index, meta := range view.msgBuffer {
|
2018-04-18 13:20:57 +02:00
|
|
|
if meta == original {
|
2018-04-10 15:07:16 +02:00
|
|
|
if start == -1 {
|
|
|
|
start = index
|
|
|
|
}
|
|
|
|
end = index
|
|
|
|
} else if start != -1 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2019-04-27 14:02:21 +02:00
|
|
|
view.msgBufferLock.RUnlock()
|
2018-04-10 15:07:16 +02:00
|
|
|
|
2018-04-23 19:13:42 +02:00
|
|
|
if start == -1 {
|
2019-04-12 23:51:58 +02:00
|
|
|
debug.Print("Called replaceBuffer() with message that was not in the buffer:", original)
|
2019-06-15 00:11:51 +02:00
|
|
|
//debug.PrintStack()
|
2018-04-23 19:13:42 +02:00
|
|
|
view.appendBuffer(new)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-04-09 17:45:41 +02:00
|
|
|
if len(view.msgBuffer) > end {
|
2018-04-10 15:07:16 +02:00
|
|
|
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)]
|
2018-04-10 15:07:16 +02:00
|
|
|
}
|
2020-02-20 20:56:03 +01:00
|
|
|
view.msgBuffer = append(view.msgBuffer[0:start], newBuffer...)
|
2018-04-18 13:20:57 +02:00
|
|
|
} else {
|
|
|
|
for i := start; i < end; i++ {
|
2019-04-09 17:45:41 +02:00
|
|
|
view.msgBuffer[i] = new
|
2018-04-18 13:20:57 +02:00
|
|
|
}
|
2018-04-10 15:07:16 +02:00
|
|
|
}
|
2019-04-27 14:02:21 +02:00
|
|
|
view.msgBufferLock.Unlock()
|
2018-04-10 15:07:16 +02:00
|
|
|
}
|
|
|
|
|
2018-03-20 16:26:28 +01:00
|
|
|
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()
|
2018-05-22 21:06:48 +02:00
|
|
|
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{}
|
2018-03-20 16:26:28 +01:00
|
|
|
view.prevMsgCount = 0
|
2018-04-10 18:31:28 +02:00
|
|
|
for i, message := range view.messages {
|
|
|
|
if message == nil {
|
|
|
|
debug.Print("O.o found nil message at", i)
|
|
|
|
break
|
|
|
|
}
|
2018-03-20 16:26:28 +01:00
|
|
|
if recalculateMessageBuffers {
|
2018-06-01 23:43:56 +02:00
|
|
|
message.CalculateBuffer(prefs, width)
|
2018-03-20 16:26:28 +01:00
|
|
|
}
|
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
|
|
|
|
}
|
2020-02-29 23:33:37 +01:00
|
|
|
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 {
|
2020-04-07 18:06:20 +02:00
|
|
|
if msg, ok := message.Renderer.(*messages.FileMessage); ok && mod > 0 {
|
2019-06-15 00:11:51 +02:00
|
|
|
open.Open(msg.Path())
|
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)
|
2020-02-29 23:33:37 +01:00
|
|
|
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 {
|
2020-02-22 02:04:44 +01:00
|
|
|
// 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
|
|
|
|
2020-02-22 02:04:44 +01:00
|
|
|
if message.SenderName == "---" || message.SenderName == "-->" || message.SenderName == "<--" || message.Type == mautrix.MsgEmote {
|
2018-04-15 13:03:05 +02:00
|
|
|
return false
|
|
|
|
}
|
2020-02-22 02:04:44 +01:00
|
|
|
|
|
|
|
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()
|
2018-04-18 12:38:33 +02:00
|
|
|
text := view.parent.input.GetText()
|
|
|
|
var buf strings.Builder
|
2018-04-15 13:03:05 +02:00
|
|
|
if cursorPos == 0 {
|
2018-04-18 12:38:33 +02:00
|
|
|
buf.WriteString(sender)
|
|
|
|
buf.WriteRune(':')
|
|
|
|
buf.WriteRune(' ')
|
|
|
|
buf.WriteString(text)
|
2018-04-15 13:03:05 +02:00
|
|
|
} else {
|
2018-04-18 12:38:33 +02:00
|
|
|
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
|
|
|
}
|
2018-04-18 12:38:33 +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 {
|
2020-02-19 14:07:10 +01:00
|
|
|
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
|
|
|
|
}
|
2018-04-11 18:20:40 +02:00
|
|
|
|
2019-04-27 14:02:21 +02:00
|
|
|
view.msgBufferLock.RLock()
|
2019-04-09 17:45:41 +02:00
|
|
|
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 {
|
2019-04-09 17:45:41 +02:00
|
|
|
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-04-11 18:20:40 +02:00
|
|
|
}
|
|
|
|
|
2018-03-20 11:16:32 +01:00
|
|
|
const PaddingAtTop = 5
|
|
|
|
|
2018-03-25 11:35:50 +02:00
|
|
|
func (view *MessageView) AddScrollOffset(diff int) {
|
2018-04-10 15:07:16 +02:00
|
|
|
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 {
|
2018-03-25 11:35:50 +02:00
|
|
|
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))
|
|
|
|
}
|
|
|
|
|
2018-03-25 11:35:50 +02:00
|
|
|
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))
|
2018-03-25 11:35:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (view *MessageView) TotalHeight() int {
|
2019-04-27 14:02:21 +02:00
|
|
|
view.msgBufferLock.RLock()
|
|
|
|
defer view.msgBufferLock.RUnlock()
|
2019-04-09 17:45:41 +02:00
|
|
|
return len(view.msgBuffer)
|
2018-03-17 00:27:30 +01:00
|
|
|
}
|
|
|
|
|
2018-03-25 11:35:50 +02: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 {
|
2018-05-27 13:49:46 +02:00
|
|
|
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)
|
2018-05-27 13:49:46 +02:00
|
|
|
}
|
|
|
|
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())
|
2018-03-20 16:26:28 +01:00
|
|
|
view.recalculateBuffers()
|
2018-03-18 16:34:42 +01:00
|
|
|
|
2019-04-27 14:02:21 +02:00
|
|
|
height := view.Height()
|
2018-04-10 15:07:16 +02:00
|
|
|
if view.TotalHeight() == 0 {
|
2019-04-27 14:02:21 +02:00
|
|
|
widget.WriteLineSimple(screen, "It's quite empty in here.", 0, height)
|
2018-03-18 16:34:42 +01:00
|
|
|
return
|
2018-03-17 00:27:30 +01:00
|
|
|
}
|
2018-03-18 16:34:42 +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
|
2018-05-22 21:06:48 +02:00
|
|
|
if bareMode {
|
2019-03-25 23:37:35 +01:00
|
|
|
messageX = 0
|
2018-05-22 21:06:48 +02:00
|
|
|
}
|
|
|
|
|
2019-04-27 14:02:21 +02:00
|
|
|
indexOffset := view.getIndexOffset(screen, height, messageX)
|
2018-03-22 22:40:26 +01:00
|
|
|
|
2019-04-08 23:59:56 +02:00
|
|
|
viewStart := 0
|
|
|
|
if indexOffset < 0 {
|
|
|
|
viewStart = -indexOffset
|
|
|
|
}
|
2018-03-26 13:31:44 +02:00
|
|
|
|
2019-04-09 17:45:41 +02:00
|
|
|
if !bareMode {
|
2019-04-27 14:02:21 +02:00
|
|
|
separatorX := usernameX + view.widestSender() + SenderSeparatorGap
|
|
|
|
scrollBarHeight, scrollBarPos := view.calculateScrollBar(height)
|
2019-04-09 17:45:41 +02:00
|
|
|
|
2019-04-27 14:02:21 +02:00
|
|
|
for line := viewStart; line < height; line++ {
|
2019-04-09 17:45:41 +02:00
|
|
|
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
|
2019-04-09 17:45:41 +02:00
|
|
|
|
|
|
|
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)
|
2018-05-22 21:06:48 +02:00
|
|
|
}
|
2019-04-08 23:59:56 +02:00
|
|
|
}
|
|
|
|
|
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); {
|
2019-04-08 23:59:56 +02:00
|
|
|
index := indexOffset + line
|
2018-03-23 16:26:06 +01:00
|
|
|
|
2019-04-09 17:45:41 +02:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2019-04-09 17:45:41 +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
|
|
|
}
|