Re-add reply rendering

This commit is contained in:
Tulir Asokan 2019-04-10 02:19:38 +03:00
parent 007b05d54e
commit 005c51c3b5
7 changed files with 57 additions and 9 deletions

View File

@ -20,6 +20,7 @@ import (
"encoding/json"
"time"
"maunium.net/go/gomuks/config"
"maunium.net/go/mautrix"
"maunium.net/go/mauview"
"maunium.net/go/tcell"
@ -41,6 +42,7 @@ type BaseMessage struct {
MsgIsHighlight bool
MsgIsService bool
MsgSource json.RawMessage
ReplyTo UIMessage
buffer []tstring.TString
plainBuffer []tstring.TString
}
@ -171,9 +173,16 @@ func (msg *BaseMessage) TimestampColor() tcell.Color {
return msg.getStateSpecificColor()
}
func (msg *BaseMessage) ReplyHeight() int {
if msg.ReplyTo != nil {
return 2 + msg.ReplyTo.Height()
}
return 0
}
// Height returns the number of rows in the computed buffer (see Buffer()).
func (msg *BaseMessage) Height() int {
return len(msg.buffer)
return msg.ReplyHeight() + len(msg.buffer)
}
// Timestamp returns the full timestamp when the message was sent.
@ -236,8 +245,36 @@ func (msg *BaseMessage) Source() json.RawMessage {
return msg.MsgSource
}
func (msg *BaseMessage) SetReplyTo(event UIMessage) {
msg.ReplyTo = event
}
func (msg *BaseMessage) Draw(screen mauview.Screen) {
screen = msg.DrawReply(screen)
for y, line := range msg.buffer {
line.Draw(screen, 0, y)
}
}
func (msg *BaseMessage) CalculateReplyBuffer(preferences config.UserPreferences, width int) {
if msg.ReplyTo == nil {
return
}
msg.ReplyTo.CalculateBuffer(preferences, width-1)
}
func (msg *BaseMessage) DrawReply(screen mauview.Screen) mauview.Screen {
if msg.ReplyTo == nil {
return screen
}
width, height := screen.Size()
replyHeight := msg.ReplyTo.Height()
widget.WriteLineSimpleColor(screen, "In reply to", 0, 0, tcell.ColorGreen)
widget.WriteLineSimpleColor(screen, msg.ReplyTo.RealSender(), len("In reply to "), 0, msg.ReplyTo.SenderColor())
for y := 1; y < 1+replyHeight; y++ {
screen.SetCell(0, y, tcell.StyleDefault, '▋')
}
replyScreen := mauview.NewProxyScreen(screen, 1, 1, width-1, replyHeight)
msg.ReplyTo.Draw(replyScreen)
return mauview.NewProxyScreen(screen, 0, replyHeight+2, width, height-replyHeight-2)
}

View File

@ -70,5 +70,6 @@ func (msg *ExpandedTextMessage) PlainText() string {
}
func (msg *ExpandedTextMessage) CalculateBuffer(prefs config.UserPreferences, width int) {
msg.CalculateReplyBuffer(prefs, width)
msg.calculateBufferWithText(prefs, msg.MsgText, width)
}

View File

@ -41,6 +41,7 @@ func NewHTMLMessage(event *mautrix.Event, displayname string, root html.Entity)
}
func (hw *HTMLMessage) Draw(screen mauview.Screen) {
screen = hw.DrawReply(screen)
if hw.focused {
screen.SetStyle(tcell.StyleDefault.Background(hw.FocusedBg))
}
@ -69,16 +70,17 @@ func (hw *HTMLMessage) OnPasteEvent(event mauview.PasteEvent) bool {
}
func (hw *HTMLMessage) CalculateBuffer(preferences config.UserPreferences, width int) {
if width <= 0 {
panic("Negative width in CalculateBuffer")
if width < 2 {
return
}
hw.CalculateReplyBuffer(preferences, width)
// TODO account for bare messages in initial startX
startX := 0
hw.Root.CalculateBuffer(width, startX, preferences.BareMessageView)
}
func (hw *HTMLMessage) Height() int {
return hw.Root.Height()
return hw.ReplyHeight() + hw.Root.Height()
}
func (hw *HTMLMessage) PlainText() string {

View File

@ -92,6 +92,7 @@ func (msg *ImageMessage) CalculateBuffer(prefs config.UserPreferences, width int
if width < 2 {
return
}
msg.CalculateReplyBuffer(prefs, width)
if prefs.BareMessageView || prefs.DisableImages {
msg.calculateBufferWithText(prefs, tstring.NewTString(msg.PlainText()), width)

View File

@ -37,6 +37,7 @@ type UIMessage interface {
FormatDate() string
SameDate(message UIMessage) bool
SetReplyTo(message UIMessage)
CalculateBuffer(preferences config.UserPreferences, width int)
Draw(screen mauview.Screen)
Height() int

View File

@ -37,13 +37,18 @@ func ParseEvent(matrix ifc.MatrixContainer, room *rooms.Room, evt *mautrix.Event
return nil
}
if len(evt.Content.GetReplyTo()) > 0 {
roomID := evt.Content.RelatesTo.InReplyTo.RoomID
if len(roomID) == 0 {
roomID = room.ID
replyToRoom := room
if len(evt.Content.RelatesTo.InReplyTo.RoomID) > 0 {
replyToRoom = matrix.GetRoom(evt.Content.RelatesTo.InReplyTo.RoomID)
}
replyToEvt, _ := matrix.GetEvent(room, evt.Content.GetReplyTo())
replyToEvt, _ := matrix.GetEvent(replyToRoom, evt.Content.GetReplyTo())
if replyToEvt != nil {
// TODO add reply header
replyToMsg := directParseEvent(matrix, replyToRoom, replyToEvt)
if replyToMsg != nil {
msg.SetReplyTo(replyToMsg)
} else {
// TODO add unrenderable reply header
}
} else {
// TODO add unknown reply header
}

View File

@ -79,5 +79,6 @@ func (msg *TextMessage) PlainText() string {
}
func (msg *TextMessage) CalculateBuffer(prefs config.UserPreferences, width int) {
msg.CalculateReplyBuffer(prefs, width)
msg.calculateBufferWithText(prefs, msg.getCache(), width)
}