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"
|
2018-03-17 00:27:30 +01:00
|
|
|
|
2018-04-18 12:38:33 +02:00
|
|
|
"github.com/mattn/go-runewidth"
|
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"
|
|
|
|
|
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
|
2018-03-20 11:16:32 +01:00
|
|
|
LoadingMessages bool
|
2018-03-17 00:27:30 +01:00
|
|
|
|
2018-03-20 11:16:32 +01:00
|
|
|
widestSender int
|
2019-03-25 23:37:35 +01:00
|
|
|
width int
|
|
|
|
height int
|
2018-03-20 11:16:32 +01:00
|
|
|
prevWidth int
|
|
|
|
prevHeight int
|
2018-03-20 16:26:28 +01:00
|
|
|
prevMsgCount int
|
2018-06-01 23:43:56 +02:00
|
|
|
prevPrefs config.UserPreferences
|
2018-03-17 00:27:30 +01:00
|
|
|
|
2018-04-09 22:45:54 +02:00
|
|
|
messageIDs map[string]messages.UIMessage
|
|
|
|
messages []messages.UIMessage
|
2018-03-20 11:16:32 +01:00
|
|
|
|
2019-04-09 17:45:41 +02:00
|
|
|
msgBuffer []messages.UIMessage
|
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
|
|
|
|
2018-04-09 22:45:54 +02:00
|
|
|
messages: make([]messages.UIMessage, 0),
|
|
|
|
messageIDs: make(map[string]messages.UIMessage),
|
2019-04-09 17:45:41 +02:00
|
|
|
msgBuffer: make([]messages.UIMessage, 0),
|
2018-03-18 16:34:42 +01:00
|
|
|
|
2019-04-07 17:21:38 +02:00
|
|
|
width: 80,
|
2018-03-20 11:16:32 +01:00
|
|
|
widestSender: 5,
|
|
|
|
prevWidth: -1,
|
|
|
|
prevHeight: -1,
|
2018-03-20 16:26:28 +01:00
|
|
|
prevMsgCount: -1,
|
2018-03-17 00:27:30 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-18 16:34:42 +01:00
|
|
|
func (view *MessageView) updateWidestSender(sender string) {
|
2018-03-17 00:27:30 +01:00
|
|
|
if len(sender) > view.widestSender {
|
|
|
|
view.widestSender = len(sender)
|
|
|
|
if view.widestSender > view.MaxSenderWidth {
|
|
|
|
view.widestSender = view.MaxSenderWidth
|
|
|
|
}
|
|
|
|
}
|
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
|
|
|
|
}
|
|
|
|
message, ok := ifcMessage.(messages.UIMessage)
|
|
|
|
if !ok {
|
|
|
|
debug.Print("[Warning] Passed non-UIMessage ifc.Message object to AddMessage().")
|
|
|
|
debug.PrintStack()
|
2018-03-22 18:51:20 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-04-09 17:45:41 +02:00
|
|
|
var oldMsg messages.UIMessage
|
|
|
|
var messageExists bool
|
|
|
|
if oldMsg, messageExists = view.messageIDs[message.ID()]; messageExists {
|
|
|
|
view.replaceMessage(oldMsg, message)
|
2019-04-10 00:04:39 +02:00
|
|
|
direction = IgnoreMessage
|
2019-04-09 17:45:41 +02:00
|
|
|
} else if oldMsg, messageExists = view.messageIDs[message.TxnID()]; messageExists {
|
2018-04-18 13:20:57 +02:00
|
|
|
view.replaceMessage(oldMsg, message)
|
2019-04-09 17:45:41 +02:00
|
|
|
delete(view.messageIDs, message.TxnID())
|
2019-04-10 00:04:39 +02:00
|
|
|
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-03-25 23:37:35 +01: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 {
|
|
|
|
width -= view.TimestampWidth + TimestampSenderGap + view.widestSender + SenderMessageGap
|
|
|
|
}
|
2018-06-01 23:43:56 +02:00
|
|
|
message.CalculateBuffer(view.config.Preferences, width)
|
2018-03-18 16:34:42 +01:00
|
|
|
|
2019-04-10 00:42:27 +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 {
|
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-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)
|
|
|
|
}
|
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-10 00:42:27 +02:00
|
|
|
if len(view.messages) > 0 && !view.messages[0].SameDate(message) {
|
|
|
|
view.messages = append([]messages.UIMessage{message, makeDateChange()}, view.messages...)
|
|
|
|
} else {
|
|
|
|
view.messages = append([]messages.UIMessage{message}, view.messages...)
|
|
|
|
}
|
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 {
|
|
|
|
view.messageIDs[message.ID()] = message
|
|
|
|
}
|
2018-03-20 11:16:32 +01:00
|
|
|
}
|
2018-03-17 00:27:30 +01:00
|
|
|
|
2018-04-09 22:45:54 +02:00
|
|
|
func (view *MessageView) appendBuffer(message messages.UIMessage) {
|
2019-04-07 17:21:38 +02:00
|
|
|
for i := 0; i < message.Height(); i++ {
|
2019-04-09 17:45:41 +02:00
|
|
|
view.msgBuffer = append(view.msgBuffer, message)
|
2018-03-20 11:16:32 +01:00
|
|
|
}
|
2018-03-20 16:26:28 +01:00
|
|
|
view.prevMsgCount++
|
2018-03-20 11:16:32 +01:00
|
|
|
}
|
|
|
|
|
2018-04-18 13:20:57 +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 {
|
|
|
|
view.messageIDs[new.ID()] = new
|
|
|
|
}
|
2018-04-18 13:20:57 +02:00
|
|
|
for index, msg := range view.messages {
|
|
|
|
if msg == original {
|
|
|
|
view.messages[index] = new
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (view *MessageView) replaceBuffer(original messages.UIMessage, new messages.UIMessage) {
|
2018-04-10 15:07:16 +02:00
|
|
|
start := -1
|
|
|
|
end := -1
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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)
|
2018-04-24 16:12:08 +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 {
|
|
|
|
new.CalculateBuffer(view.prevPrefs, view.prevWidth)
|
|
|
|
}
|
|
|
|
|
|
|
|
if new.Height() != end-start {
|
2019-04-09 17:45:41 +02:00
|
|
|
metaBuffer := view.msgBuffer[0:start]
|
2019-04-07 17:21:38 +02:00
|
|
|
for i := 0; i < new.Height(); i++ {
|
2018-04-18 13:20:57 +02:00
|
|
|
metaBuffer = append(metaBuffer, new)
|
2018-04-10 15:07:16 +02:00
|
|
|
}
|
2019-04-09 17:45:41 +02:00
|
|
|
view.msgBuffer = append(metaBuffer, view.msgBuffer[end:]...)
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-03-25 23:37:35 +01:00
|
|
|
recalculateMessageBuffers := view.width != view.prevWidth ||
|
2018-06-01 23:43:56 +02:00
|
|
|
view.prevPrefs.BareMessageView != prefs.BareMessageView ||
|
|
|
|
view.prevPrefs.DisableImages != prefs.DisableImages
|
2018-05-22 21:06:48 +02:00
|
|
|
if recalculateMessageBuffers || len(view.messages) != view.prevMsgCount {
|
2019-03-25 23:37:35 +01:00
|
|
|
width := view.width
|
|
|
|
if !prefs.BareMessageView {
|
|
|
|
width -= view.TimestampWidth + TimestampSenderGap + view.widestSender + SenderMessageGap
|
|
|
|
}
|
2019-04-09 17:45:41 +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
|
|
|
}
|
2018-03-20 11:16:32 +01:00
|
|
|
view.appendBuffer(message)
|
2018-03-17 00:27:30 +01:00
|
|
|
}
|
|
|
|
}
|
2019-03-25 23:37:35 +01:00
|
|
|
view.prevHeight = view.height
|
|
|
|
view.prevWidth = view.width
|
2018-06-01 23:43:56 +02:00
|
|
|
view.prevPrefs = prefs
|
2018-03-17 00:27:30 +01:00
|
|
|
}
|
|
|
|
|
2019-04-09 17:45:41 +02:00
|
|
|
func (view *MessageView) handleMessageClick(message messages.UIMessage) bool {
|
2018-04-15 13:03:05 +02:00
|
|
|
switch message := message.(type) {
|
|
|
|
case *messages.ImageMessage:
|
|
|
|
open.Open(message.Path())
|
|
|
|
case messages.UIMessage:
|
2019-04-10 20:06:19 +02:00
|
|
|
debug.Print("Message clicked:", message)
|
2018-04-15 13:03:05 +02:00
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2019-04-09 17:45:41 +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
|
|
|
|
}
|
|
|
|
|
2019-04-09 17:45:41 +02:00
|
|
|
if len(message.Sender()) == 0 {
|
2018-04-15 13:03:05 +02:00
|
|
|
return false
|
|
|
|
}
|
2019-04-09 17:45:41 +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()
|
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 {
|
|
|
|
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()
|
|
|
|
line := view.TotalHeight() - view.ScrollOffset - view.height + y
|
|
|
|
if line < 0 || line >= view.TotalHeight() {
|
|
|
|
return false
|
|
|
|
}
|
2018-04-11 18:20:40 +02:00
|
|
|
|
2019-04-09 17:45:41 +02:00
|
|
|
message := view.msgBuffer[line]
|
|
|
|
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
|
|
|
}
|
2018-04-14 14:33:20 +02:00
|
|
|
|
2019-03-25 23:37:35 +01:00
|
|
|
usernameX := view.TimestampWidth + TimestampSenderGap
|
|
|
|
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-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-03-25 23:37:35 +01:00
|
|
|
if diff >= 0 && view.ScrollOffset+diff >= totalHeight-view.height+PaddingAtTop {
|
|
|
|
view.ScrollOffset = totalHeight - view.height + PaddingAtTop
|
2018-03-26 13:31:44 +02:00
|
|
|
} else {
|
|
|
|
view.ScrollOffset += diff
|
2018-03-20 11:16:32 +01:00
|
|
|
}
|
|
|
|
|
2019-03-25 23:37:35 +01:00
|
|
|
if view.ScrollOffset > totalHeight-view.height+PaddingAtTop {
|
|
|
|
view.ScrollOffset = totalHeight - view.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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-25 11:35:50 +02:00
|
|
|
func (view *MessageView) Height() int {
|
2019-03-25 23:37:35 +01:00
|
|
|
return view.height
|
2018-03-25 11:35:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (view *MessageView) TotalHeight() int {
|
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-09 17:45:41 +02:00
|
|
|
return view.ScrollOffset >= len(view.msgBuffer)-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."
|
|
|
|
if view.LoadingMessages {
|
|
|
|
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
|
|
|
|
var prevMessage messages.UIMessage
|
|
|
|
for line := 0; line < height; line++ {
|
|
|
|
index := indexOffset + line
|
|
|
|
if index < 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2019-04-09 17:45:41 +02:00
|
|
|
meta := view.msgBuffer[index]
|
2018-05-22 23:44:29 +02:00
|
|
|
message, ok := meta.(messages.UIMessage)
|
|
|
|
if ok && message != prevMessage {
|
2018-05-27 13:49:46 +02:00
|
|
|
var sender string
|
|
|
|
if len(message.Sender()) > 0 {
|
|
|
|
sender = fmt.Sprintf(" <%s>", message.Sender())
|
|
|
|
} else if message.Type() == "m.emote" {
|
|
|
|
sender = fmt.Sprintf(" * %s", message.RealSender())
|
|
|
|
}
|
|
|
|
fmt.Fprintf(&buf, "%s%s %s\n", message.FormatTime(), sender, message.PlainText())
|
2018-05-22 23:44:29 +02:00
|
|
|
prevMessage = message
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return buf.String()
|
|
|
|
}
|
|
|
|
|
2019-03-25 23:37:35 +01:00
|
|
|
func (view *MessageView) Draw(screen mauview.Screen) {
|
|
|
|
view.width, view.height = screen.Size()
|
2018-03-20 16:26:28 +01:00
|
|
|
view.recalculateBuffers()
|
2018-03-18 16:34:42 +01:00
|
|
|
|
2018-04-10 15:07:16 +02:00
|
|
|
if view.TotalHeight() == 0 {
|
2019-03-25 23:37:35 +01:00
|
|
|
widget.WriteLineSimple(screen, "It's quite empty in here.", 0, view.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
|
2018-03-23 16:26:06 +01: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-03-25 23:37:35 +01:00
|
|
|
indexOffset := view.getIndexOffset(screen, view.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 {
|
|
|
|
separatorX := usernameX + view.widestSender + SenderSeparatorGap
|
|
|
|
scrollBarHeight, scrollBarPos := view.calculateScrollBar(view.height)
|
|
|
|
|
|
|
|
for line := viewStart; line < view.height; line++ {
|
|
|
|
showScrollbar := line-viewStart >= scrollBarPos-scrollBarHeight && line-viewStart < scrollBarPos
|
|
|
|
isTop := line == viewStart && view.ScrollOffset+view.height >= view.TotalHeight()
|
|
|
|
isBottom := line == view.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)
|
2018-05-22 21:06:48 +02:00
|
|
|
}
|
2019-04-08 23:59:56 +02:00
|
|
|
}
|
|
|
|
|
2019-04-09 17:45:41 +02:00
|
|
|
var prevMsg messages.UIMessage
|
2019-04-08 23:59:56 +02:00
|
|
|
for line := viewStart; line < view.height && indexOffset+line < view.TotalHeight(); line++ {
|
|
|
|
index := indexOffset + line
|
2018-03-23 16:26:06 +01:00
|
|
|
|
2019-04-09 17:45:41 +02:00
|
|
|
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
|
|
|
}
|
2019-04-07 21:54:55 +02:00
|
|
|
// TODO hiding senders might not be that nice after all, maybe an option? (disabled for now)
|
2019-04-09 17:45:41 +02:00
|
|
|
//if !bareMode && (prevMsg == nil || meta.Sender() != prevMsg.Sender()) {
|
2019-04-07 21:54:55 +02:00
|
|
|
widget.WriteLineColor(
|
2019-04-09 17:45:41 +02:00
|
|
|
screen, mauview.AlignRight, msg.Sender(),
|
2019-04-07 21:54:55 +02:00
|
|
|
usernameX, line, view.widestSender,
|
2019-04-09 17:45:41 +02:00
|
|
|
msg.SenderColor())
|
2019-04-07 21:54:55 +02:00
|
|
|
//}
|
2019-04-09 17:45:41 +02:00
|
|
|
prevMsg = msg
|
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-09 17:45:41 +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
|
|
|
}
|
|
|
|
}
|