aboutsummaryrefslogtreecommitdiff
path: root/ui
diff options
context:
space:
mode:
Diffstat (limited to 'ui')
-rw-r--r--ui/command-processor.go2
-rw-r--r--ui/commands.go61
-rw-r--r--ui/message-view.go104
-rw-r--r--ui/messages/base.go235
-rw-r--r--ui/messages/expandedtextmessage.go67
-rw-r--r--ui/messages/htmlmessage.go35
-rw-r--r--ui/messages/imagemessage.go44
-rw-r--r--ui/messages/message.go53
-rw-r--r--ui/messages/parser.go55
-rw-r--r--ui/messages/textbase.go11
-rw-r--r--ui/messages/textmessage.go78
-rw-r--r--ui/room-list.go25
-rw-r--r--ui/room-view.go50
-rw-r--r--ui/view-login.go2
-rw-r--r--ui/view-main.go38
15 files changed, 466 insertions, 394 deletions
diff --git a/ui/command-processor.go b/ui/command-processor.go
index 96b1ada..26644c5 100644
--- a/ui/command-processor.go
+++ b/ui/command-processor.go
@@ -108,6 +108,8 @@ func NewCommandProcessor(parent *MainView) *CommandProcessor {
"rainbow": cmdRainbow,
"invite": cmdInvite,
"hprof": cmdHeapProfile,
+ "cprof": cmdCPUProfile,
+ "trace": cmdTrace,
},
}
}
diff --git a/ui/commands.go b/ui/commands.go
index 5bcba98..0b15d8e 100644
--- a/ui/commands.go
+++ b/ui/commands.go
@@ -19,10 +19,15 @@ package ui
import (
"encoding/json"
"fmt"
+ "io"
"os"
"runtime"
+ dbg "runtime/debug"
"runtime/pprof"
+ "runtime/trace"
+ "strconv"
"strings"
+ "time"
"unicode"
"github.com/lucasb-eyer/go-colorful"
@@ -72,40 +77,80 @@ var rainbow = GradientTable{
func cmdHeapProfile(cmd *Command) {
runtime.GC()
- memProfile, err := os.Create("gomuks.prof")
+ dbg.FreeOSMemory()
+ memProfile, err := os.Create("gomuks.heap.prof")
if err != nil {
- debug.Print(err)
+ debug.Print("Failed to open gomuks.heap.prof:", err)
+ return
}
- defer memProfile.Close()
+ defer func() {
+ err := memProfile.Close()
+ if err != nil {
+ debug.Print("Failed to close gomuks.heap.prof:", err)
+ }
+ }()
if err := pprof.WriteHeapProfile(memProfile); err != nil {
- debug.Print(err)
+ debug.Print("Heap profile error:", err)
}
}
+func runTimedProfile(cmd *Command, start func(writer io.Writer) error, stop func(), task, file string) {
+ if len(cmd.Args) == 0 {
+ cmd.Reply("Usage: /%s <seconds>", cmd.Command)
+ } else if dur, err := strconv.Atoi(cmd.Args[0]); err != nil || dur < 0 {
+ cmd.Reply("Usage: /%s <seconds>", cmd.Command)
+ } else if cpuProfile, err := os.Create(file); err != nil {
+ debug.Printf("Failed to open %s: %v", file, err)
+ } else if err = start(cpuProfile); err != nil {
+ _ = cpuProfile.Close()
+ debug.Print(task, "error:", err)
+ } else {
+ cmd.Reply("Started %s for %d seconds", task, dur)
+ go func() {
+ time.Sleep(time.Duration(dur) * time.Second)
+ stop()
+ cmd.Reply("%s finished.", task)
+
+ err := cpuProfile.Close()
+ if err != nil {
+ debug.Print("Failed to close gomuks.cpu.prof:", err)
+ }
+ }()
+ }
+}
+
+func cmdCPUProfile(cmd *Command) {
+ runTimedProfile(cmd, pprof.StartCPUProfile, pprof.StopCPUProfile, "CPU profiling", "gomuks.cpu.prof")
+}
+
+func cmdTrace(cmd *Command) {
+ runTimedProfile(cmd, trace.Start, trace.Stop, "Call tracing", "gomuks.trace")
+}
+
// TODO this command definitely belongs in a plugin once we have a plugin system.
func cmdRainbow(cmd *Command) {
text := strings.Join(cmd.Args, " ")
var html strings.Builder
- fmt.Fprint(&html, "**🌈** ")
+ _, _ = fmt.Fprint(&html, "**🌈** ")
for i, char := range text {
if unicode.IsSpace(char) {
html.WriteRune(char)
continue
}
color := rainbow.GetInterpolatedColorFor(float64(i) / float64(len(text))).Hex()
- fmt.Fprintf(&html, "<font color=\"%s\">%c</font>", color, char)
+ _, _ = fmt.Fprintf(&html, "<font color=\"%s\">%c</font>", color, char)
}
go cmd.Room.SendMessage("m.text", html.String())
cmd.UI.Render()
}
func cmdQuit(cmd *Command) {
- cmd.Gomuks.Stop()
+ cmd.Gomuks.Stop(true)
}
func cmdClearCache(cmd *Command) {
cmd.Config.Clear()
- cmd.Gomuks.Stop()
+ cmd.Gomuks.Stop(false)
}
func cmdUnknownCommand(cmd *Command) {
diff --git a/ui/message-view.go b/ui/message-view.go
index 75cd022..9aa38a1 100644
--- a/ui/message-view.go
+++ b/ui/message-view.go
@@ -25,6 +25,7 @@ import (
"github.com/mattn/go-runewidth"
sync "github.com/sasha-s/go-deadlock"
+ "maunium.net/go/mautrix"
"maunium.net/go/mauview"
"maunium.net/go/tcell"
@@ -58,11 +59,13 @@ type MessageView struct {
prevPrefs config.UserPreferences
messageIDLock sync.RWMutex
- messageIDs map[string]messages.UIMessage
+ messageIDs map[string]*messages.UIMessage
messagesLock sync.RWMutex
- messages []messages.UIMessage
+ messages []*messages.UIMessage
msgBufferLock sync.RWMutex
- msgBuffer []messages.UIMessage
+ msgBuffer []*messages.UIMessage
+
+ initialHistoryLoaded bool
}
func NewMessageView(parent *RoomView) *MessageView {
@@ -74,9 +77,9 @@ func NewMessageView(parent *RoomView) *MessageView {
TimestampWidth: len(messages.TimeFormat),
ScrollOffset: 0,
- messages: make([]messages.UIMessage, 0),
- messageIDs: make(map[string]messages.UIMessage),
- msgBuffer: make([]messages.UIMessage, 0),
+ messages: make([]*messages.UIMessage, 0),
+ messageIDs: make(map[string]*messages.UIMessage),
+ msgBuffer: make([]*messages.UIMessage, 0),
_width: 80,
_widestSender: 5,
@@ -86,6 +89,23 @@ func NewMessageView(parent *RoomView) *MessageView {
}
}
+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) {
if len(sender) > int(view._widestSender) {
if len(sender) > view.MaxSenderWidth {
@@ -108,20 +128,22 @@ func (view *MessageView) AddMessage(ifcMessage ifc.Message, direction MessageDir
if ifcMessage == nil {
return
}
- message, ok := ifcMessage.(messages.UIMessage)
- if !ok {
+ message, ok := ifcMessage.(*messages.UIMessage)
+ if !ok || message == nil {
debug.Print("[Warning] Passed non-UIMessage ifc.Message object to AddMessage().")
debug.PrintStack()
return
}
- var oldMsg messages.UIMessage
- if oldMsg = view.getMessageByID(message.ID()); oldMsg != nil {
+ var oldMsg *messages.UIMessage
+ if oldMsg = view.getMessageByID(message.EventID); oldMsg != nil {
view.replaceMessage(oldMsg, message)
direction = IgnoreMessage
- } else if oldMsg = view.getMessageByID(message.TxnID()); oldMsg != nil {
+ } else if oldMsg = view.getMessageByID(message.TxnID); oldMsg != nil {
view.replaceMessage(oldMsg, message)
- view.deleteMessageID(message.TxnID())
+ view.deleteMessageID(message.TxnID)
+ direction = IgnoreMessage
+ } else if oldMsg = view.getMessageByID(message.Relation.GetReplaceID()); oldMsg != nil {
direction = IgnoreMessage
}
@@ -134,7 +156,7 @@ func (view *MessageView) AddMessage(ifcMessage ifc.Message, direction MessageDir
}
message.CalculateBuffer(view.config.Preferences, width)
- makeDateChange := func() messages.UIMessage {
+ makeDateChange := func() *messages.UIMessage {
dateChange := messages.NewDateChangeMessage(
fmt.Sprintf("Date changed to %s", message.FormatDate()))
dateChange.CalculateBuffer(view.config.Preferences, width)
@@ -157,9 +179,9 @@ func (view *MessageView) AddMessage(ifcMessage ifc.Message, direction MessageDir
} else if direction == PrependMessage {
view.messagesLock.Lock()
if len(view.messages) > 0 && !view.messages[0].SameDate(message) {
- view.messages = append([]messages.UIMessage{message, makeDateChange()}, view.messages...)
+ view.messages = append([]*messages.UIMessage{message, makeDateChange()}, view.messages...)
} else {
- view.messages = append([]messages.UIMessage{message}, view.messages...)
+ view.messages = append([]*messages.UIMessage{message}, view.messages...)
}
view.messagesLock.Unlock()
} else if oldMsg != nil {
@@ -174,7 +196,7 @@ func (view *MessageView) AddMessage(ifcMessage ifc.Message, direction MessageDir
}
}
-func (view *MessageView) replaceMessage(original messages.UIMessage, new messages.UIMessage) {
+func (view *MessageView) replaceMessage(original *messages.UIMessage, new *messages.UIMessage) {
if len(new.ID()) > 0 {
view.setMessageID(new)
}
@@ -187,7 +209,10 @@ func (view *MessageView) replaceMessage(original messages.UIMessage, new message
view.messagesLock.Unlock()
}
-func (view *MessageView) getMessageByID(id string) messages.UIMessage {
+func (view *MessageView) getMessageByID(id string) *messages.UIMessage {
+ if id == "" {
+ return nil
+ }
view.messageIDLock.RLock()
defer view.messageIDLock.RUnlock()
msg, ok := view.messageIDs[id]
@@ -198,31 +223,37 @@ func (view *MessageView) getMessageByID(id string) messages.UIMessage {
}
func (view *MessageView) deleteMessageID(id string) {
+ if id == "" {
+ return
+ }
view.messageIDLock.Lock()
delete(view.messageIDs, id)
view.messageIDLock.Unlock()
}
-func (view *MessageView) setMessageID(message messages.UIMessage) {
+func (view *MessageView) setMessageID(message *messages.UIMessage) {
+ if message.ID() == "" {
+ return
+ }
view.messageIDLock.Lock()
view.messageIDs[message.ID()] = message
view.messageIDLock.Unlock()
}
-func (view *MessageView) appendBuffer(message messages.UIMessage) {
+func (view *MessageView) appendBuffer(message *messages.UIMessage) {
view.msgBufferLock.Lock()
view.appendBufferUnlocked(message)
view.msgBufferLock.Unlock()
}
-func (view *MessageView) appendBufferUnlocked(message messages.UIMessage) {
+func (view *MessageView) appendBufferUnlocked(message *messages.UIMessage) {
for i := 0; i < message.Height(); i++ {
view.msgBuffer = append(view.msgBuffer, message)
}
view.prevMsgCount++
}
-func (view *MessageView) replaceBuffer(original messages.UIMessage, new messages.UIMessage) {
+func (view *MessageView) replaceBuffer(original *messages.UIMessage, new *messages.UIMessage) {
start := -1
end := -1
view.msgBufferLock.RLock()
@@ -240,7 +271,7 @@ func (view *MessageView) replaceBuffer(original messages.UIMessage, new messages
if start == -1 {
debug.Print("Called replaceBuffer() with message that was not in the buffer:", original)
- debug.PrintStack()
+ //debug.PrintStack()
view.appendBuffer(new)
return
}
@@ -280,7 +311,7 @@ func (view *MessageView) recalculateBuffers() {
if !prefs.BareMessageView {
width -= view.TimestampWidth + TimestampSenderGap + view.widestSender() + SenderMessageGap
}
- view.msgBuffer = []messages.UIMessage{}
+ view.msgBuffer = []*messages.UIMessage{}
view.prevMsgCount = 0
for i, message := range view.messages {
if message == nil {
@@ -299,17 +330,17 @@ func (view *MessageView) recalculateBuffers() {
view.prevPrefs = prefs
}
-func (view *MessageView) handleMessageClick(message messages.UIMessage) bool {
- switch message := message.(type) {
+func (view *MessageView) handleMessageClick(message *messages.UIMessage) bool {
+ switch msg := message.Renderer.(type) {
case *messages.ImageMessage:
- open.Open(message.Path())
- case messages.UIMessage:
+ open.Open(msg.Path())
+ default:
debug.Print("Message clicked:", message)
}
return false
}
-func (view *MessageView) handleUsernameClick(message messages.UIMessage, prevMessage messages.UIMessage) bool {
+func (view *MessageView) handleUsernameClick(message *messages.UIMessage, prevMessage *messages.UIMessage) bool {
if prevMessage != nil && prevMessage.Sender() == message.Sender() {
return false
}
@@ -317,7 +348,7 @@ func (view *MessageView) handleUsernameClick(message messages.UIMessage, prevMes
if len(message.Sender()) == 0 {
return false
}
- sender := fmt.Sprintf("[%s](https://matrix.to/#/%s)", message.Sender(), message.SenderID())
+ sender := fmt.Sprintf("[%s](https://matrix.to/#/%s)", message.Sender(), message.SenderID)
cursorPos := view.parent.input.GetCursorOffset()
text := view.parent.input.GetText()
@@ -363,7 +394,7 @@ func (view *MessageView) OnMouseEvent(event mauview.MouseEvent) bool {
view.msgBufferLock.RLock()
message := view.msgBuffer[line]
- var prevMessage messages.UIMessage
+ var prevMessage *messages.UIMessage
if y != 0 && line > 0 {
prevMessage = view.msgBuffer[line-1]
}
@@ -496,7 +527,7 @@ func (view *MessageView) getIndexOffset(screen mauview.Screen, height, messageX
func (view *MessageView) CapturePlaintext(height int) string {
var buf strings.Builder
indexOffset := view.TotalHeight() - view.ScrollOffset - height
- var prevMessage messages.UIMessage
+ var prevMessage *messages.UIMessage
view.msgBufferLock.RLock()
for line := 0; line < height; line++ {
index := indexOffset + line
@@ -504,14 +535,13 @@ func (view *MessageView) CapturePlaintext(height int) string {
continue
}
- meta := view.msgBuffer[index]
- message, ok := meta.(messages.UIMessage)
- if ok && message != prevMessage {
+ 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() == "m.emote" {
- sender = fmt.Sprintf(" * %s", message.RealSender())
+ } else if message.Type == mautrix.MsgEmote {
+ sender = fmt.Sprintf(" * %s", message.SenderName)
}
fmt.Fprintf(&buf, "%s%s %s\n", message.FormatTime(), sender, message.PlainText())
prevMessage = message
@@ -561,7 +591,7 @@ func (view *MessageView) Draw(screen mauview.Screen) {
}
}
- var prevMsg messages.UIMessage
+ var prevMsg *messages.UIMessage
view.msgBufferLock.RLock()
for line := viewStart; line < height && indexOffset+line < len(view.msgBuffer); line++ {
index := indexOffset + line
diff --git a/ui/messages/base.go b/ui/messages/base.go
index 05df72b..ef495fb 100644
--- a/ui/messages/base.go
+++ b/ui/messages/base.go
@@ -27,44 +27,60 @@ import (
"maunium.net/go/tcell"
"maunium.net/go/gomuks/interface"
- "maunium.net/go/gomuks/ui/messages/tstring"
"maunium.net/go/gomuks/ui/widget"
)
-type BaseMessage struct {
- MsgID string
- MsgTxnID string
- MsgType mautrix.MessageType
- MsgSenderID string
- MsgSender string
- MsgSenderColor tcell.Color
- MsgTimestamp time.Time
- MsgState mautrix.OutgoingEventState
- MsgIsHighlight bool
- MsgIsService bool
- MsgSource json.RawMessage
- ReplyTo UIMessage
- buffer []tstring.TString
-}
-
-func newBaseMessage(event *mautrix.Event, displayname string) BaseMessage {
+type MessageRenderer interface {
+ Draw(screen mauview.Screen)
+ NotificationContent() string
+ PlainText() string
+ CalculateBuffer(prefs config.UserPreferences, width int, msg *UIMessage)
+ RegisterMatrix(matrix ifc.MatrixContainer)
+ Height() int
+ Clone() MessageRenderer
+ String() string
+}
+
+type UIMessage struct {
+ EventID string
+ TxnID string
+ Relation mautrix.RelatesTo
+ Type mautrix.MessageType
+ SenderID string
+ SenderName string
+ DefaultSenderColor tcell.Color
+ Timestamp time.Time
+ State mautrix.OutgoingEventState
+ IsHighlight bool
+ IsService bool
+ Source json.RawMessage
+ ReplyTo *UIMessage
+ Renderer MessageRenderer
+}
+
+const DateFormat = "January _2, 2006"
+const TimeFormat = "15:04:05"
+
+func newUIMessage(event *mautrix.Event, displayname string, renderer MessageRenderer) *UIMessage {
msgtype := event.Content.MsgType
if len(msgtype) == 0 {
msgtype = mautrix.MessageType(event.Type.String())
}
- return BaseMessage{
- MsgSenderID: event.Sender,
- MsgSender: displayname,
- MsgTimestamp: unixToTime(event.Timestamp),
- MsgSenderColor: widget.GetHashColor(event.Sender),
- MsgType: msgtype,
- MsgID: event.ID,
- MsgTxnID: event.Unsigned.TransactionID,
- MsgState: event.Unsigned.OutgoingState,
- MsgIsHighlight: false,
- MsgIsService: false,
- MsgSource: event.Content.VeryRaw,
+ return &UIMessage{
+ SenderID: event.Sender,
+ SenderName: displayname,
+ Timestamp: unixToTime(event.Timestamp),
+ DefaultSenderColor: widget.GetHashColor(event.Sender),
+ Type: msgtype,
+ EventID: event.ID,
+ TxnID: event.Unsigned.TransactionID,
+ Relation: *event.Content.GetRelatesTo(),
+ State: event.Unsigned.OutgoingState,
+ IsHighlight: false,
+ IsService: false,
+ Source: event.Content.VeryRaw,
+ Renderer: renderer,
}
}
@@ -76,44 +92,38 @@ func unixToTime(unix int64) time.Time {
return timestamp
}
-func (msg *BaseMessage) RegisterMatrix(matrix ifc.MatrixContainer) {}
-
// Sender gets the string that should be displayed as the sender of this message.
//
// If the message is being sent, the sender is "Sending...".
// If sending has failed, the sender is "Error".
// If the message is an emote, the sender is blank.
// In any other case, the sender is the display name of the user who sent the message.
-func (msg *BaseMessage) Sender() string {
- switch msg.MsgState {
+func (msg *UIMessage) Sender() string {
+ switch msg.State {
case mautrix.EventStateLocalEcho:
return "Sending..."
case mautrix.EventStateSendFail:
return "Error"
}
- switch msg.MsgType {
+ switch msg.Type {
case "m.emote":
// Emotes don't show a separate sender, it's included in the buffer.
return ""
default:
- return msg.MsgSender
+ return msg.SenderName
}
}
-func (msg *BaseMessage) SenderID() string {
- return msg.MsgSenderID
+func (msg *UIMessage) NotificationSenderName() string {
+ return msg.SenderName
}
-func (msg *BaseMessage) RealSender() string {
- return msg.MsgSender
+func (msg *UIMessage) NotificationContent() string {
+ return msg.Renderer.NotificationContent()
}
-func (msg *BaseMessage) NotificationSenderName() string {
- return msg.MsgSender
-}
-
-func (msg *BaseMessage) getStateSpecificColor() tcell.Color {
- switch msg.MsgState {
+func (msg *UIMessage) getStateSpecificColor() tcell.Color {
+ switch msg.State {
case mautrix.EventStateLocalEcho:
return tcell.ColorGray
case mautrix.EventStateSendFail:
@@ -132,31 +142,31 @@ func (msg *BaseMessage) getStateSpecificColor() tcell.Color {
//
// In any other case, the color is whatever is specified in the Message struct.
// Usually that means it is the hash-based color of the sender (see ui/widget/color.go)
-func (msg *BaseMessage) SenderColor() tcell.Color {
+func (msg *UIMessage) SenderColor() tcell.Color {
stateColor := msg.getStateSpecificColor()
switch {
case stateColor != tcell.ColorDefault:
return stateColor
- case msg.MsgType == "m.room.member":
- return widget.GetHashColor(msg.MsgSender)
- case msg.MsgIsService:
+ case msg.Type == "m.room.member":
+ return widget.GetHashColor(msg.SenderName)
+ case msg.IsService:
return tcell.ColorGray
default:
- return msg.MsgSenderColor
+ return msg.DefaultSenderColor
}
}
// TextColor returns the color the actual content of the message should be shown in.
-func (msg *BaseMessage) TextColor() tcell.Color {
+func (msg *UIMessage) TextColor() tcell.Color {
stateColor := msg.getStateSpecificColor()
switch {
case stateColor != tcell.ColorDefault:
return stateColor
- case msg.MsgIsService, msg.MsgType == "m.notice":
+ case msg.IsService, msg.Type == "m.notice":
return tcell.ColorGray
- case msg.MsgIsHighlight:
+ case msg.IsHighlight:
return tcell.ColorYellow
- case msg.MsgType == "m.room.member":
+ case msg.Type == "m.room.member":
return tcell.ColorGreen
default:
return tcell.ColorDefault
@@ -169,14 +179,14 @@ func (msg *BaseMessage) TextColor() tcell.Color {
// gray and red respectively.
//
// However, other messages are the default color instead of a color stored in the struct.
-func (msg *BaseMessage) TimestampColor() tcell.Color {
- if msg.MsgIsService {
+func (msg *UIMessage) TimestampColor() tcell.Color {
+ if msg.IsService {
return tcell.ColorGray
}
return msg.getStateSpecificColor()
}
-func (msg *BaseMessage) ReplyHeight() int {
+func (msg *UIMessage) ReplyHeight() int {
if msg.ReplyTo != nil {
return 1 + msg.ReplyTo.Height()
}
@@ -184,102 +194,78 @@ func (msg *BaseMessage) ReplyHeight() int {
}
// Height returns the number of rows in the computed buffer (see Buffer()).
-func (msg *BaseMessage) Height() int {
- return msg.ReplyHeight() + len(msg.buffer)
+func (msg *UIMessage) Height() int {
+ return msg.ReplyHeight() + msg.Renderer.Height()
}
-// Timestamp returns the full timestamp when the message was sent.
-func (msg *BaseMessage) Timestamp() time.Time {
- return msg.MsgTimestamp
+func (msg *UIMessage) Time() time.Time {
+ return msg.Timestamp
}
// FormatTime returns the formatted time when the message was sent.
-func (msg *BaseMessage) FormatTime() string {
- return msg.MsgTimestamp.Format(TimeFormat)
+func (msg *UIMessage) FormatTime() string {
+ return msg.Timestamp.Format(TimeFormat)
}
// FormatDate returns the formatted date when the message was sent.
-func (msg *BaseMessage) FormatDate() string {
- return msg.MsgTimestamp.Format(DateFormat)
+func (msg *UIMessage) FormatDate() string {
+ return msg.Timestamp.Format(DateFormat)
}
-func (msg *BaseMessage) SameDate(message UIMessage) bool {
- year1, month1, day1 := msg.Timestamp().Date()
- year2, month2, day2 := message.Timestamp().Date()
+func (msg *UIMessage) SameDate(message *UIMessage) bool {
+ year1, month1, day1 := msg.Timestamp.Date()
+ year2, month2, day2 := message.Timestamp.Date()
return day1 == day2 && month1 == month2 && year1 == year2
}
-func (msg *BaseMessage) ID() string {
- if len(msg.MsgID) == 0 {
- return msg.MsgTxnID
+func (msg *UIMessage) ID() string {
+ if len(msg.EventID) == 0 {
+ return msg.TxnID
}
- return msg.MsgID
-}
-
-func (msg *BaseMessage) SetID(id string) {
- msg.MsgID = id
-}
-
-func (msg *BaseMessage) TxnID() string {
- return msg.MsgTxnID
-}
-
-func (msg *BaseMessage) Type() mautrix.MessageType {
- return msg.MsgType
-}
-
-func (msg *BaseMessage) State() mautrix.OutgoingEventState {
- return msg.MsgState
-}
-
-func (msg *BaseMessage) SetState(state mautrix.OutgoingEventState) {
- msg.MsgState = state
-}
-
-func (msg *BaseMessage) IsHighlight() bool {
- return msg.MsgIsHighlight
-}
-
-func (msg *BaseMessage) SetIsHighlight(isHighlight bool) {
- msg.MsgIsHighlight = isHighlight
+ return msg.EventID
}
-func (msg *BaseMessage) Source() json.RawMessage {
- return msg.MsgSource
+func (msg *UIMessage) SetID(id string) {
+ msg.EventID = id
}
-func (msg *BaseMessage) SetReplyTo(event UIMessage) {
- msg.ReplyTo = event
+func (msg *UIMessage) SetIsHighlight(isHighlight bool) {
+ // TODO Textmessage cache needs to be cleared
+ msg.IsHighlight = isHighlight
}
-func (msg *BaseMessage) Draw(screen mauview.Screen) {
+func (msg *UIMessage) Draw(screen mauview.Screen) {
screen = msg.DrawReply(screen)
- for y, line := range msg.buffer {
- line.Draw(screen, 0, y)
- }
+ msg.Renderer.Draw(screen)
}
-func (msg *BaseMessage) clone() BaseMessage {
+func (msg *UIMessage) Clone() *UIMessage {
clone := *msg
- clone.buffer = nil
- return clone
+ clone.ReplyTo = nil
+ clone.Renderer = clone.Renderer.Clone()
+ return &clone
}
-func (msg *BaseMessage) CalculateReplyBuffer(preferences config.UserPreferences, width int) {
+func (msg *UIMessage) 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 {
+func (msg *UIMessage) CalculateBuffer(preferences config.UserPreferences, width int) {
+ msg.Renderer.CalculateBuffer(preferences, width, msg)
+ msg.CalculateReplyBuffer(preferences, width)
+}
+
+func (msg *UIMessage) 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", 1, 0, tcell.ColorGreen)
- widget.WriteLineSimpleColor(screen, msg.ReplyTo.RealSender(), 13, 0, msg.ReplyTo.SenderColor())
+ widget.WriteLineSimpleColor(screen, msg.ReplyTo.SenderName, 13, 0, msg.ReplyTo.SenderColor())
for y := 0; y < 1+replyHeight; y++ {
screen.SetCell(0, y, tcell.StyleDefault, 'â–Š')
}
@@ -288,16 +274,21 @@ func (msg *BaseMessage) DrawReply(screen mauview.Screen) mauview.Screen {
return mauview.NewProxyScreen(screen, 0, replyHeight+1, width, height-replyHeight-1)
}
-func (msg *BaseMessage) String() string {
- return fmt.Sprintf(`&messages.BaseMessage{
+func (msg *UIMessage) String() string {
+ return fmt.Sprintf(`&messages.UIMessage{
ID="%s", TxnID="%s",
Type="%s", Timestamp=%s,
Sender={ID="%s", Name="%s", Color=#%X},
IsService=%t, IsHighlight=%t,
+ Renderer=%s,
}`,
- msg.MsgID, msg.MsgTxnID,
- msg.MsgType, msg.MsgTimestamp.String(),
- msg.MsgSenderID, msg.MsgSender, msg.MsgSenderColor.Hex(),
- msg.MsgIsService, msg.MsgIsHighlight,
+ msg.EventID, msg.TxnID,
+ msg.Type, msg.Timestamp.String(),
+ msg.SenderID, msg.SenderName, msg.DefaultSenderColor.Hex(),
+ msg.IsService, msg.IsHighlight, msg.Renderer.String(),
)
}
+
+func (msg *UIMessage) PlainText() string {
+ return msg.Renderer.PlainText()
+}
diff --git a/ui/messages/expandedtextmessage.go b/ui/messages/expandedtextmessage.go
index cf71ba1..c9cbf0c 100644
--- a/ui/messages/expandedtextmessage.go
+++ b/ui/messages/expandedtextmessage.go
@@ -17,9 +17,12 @@
package messages
import (
+ "fmt"
"time"
+ ifc "maunium.net/go/gomuks/interface"
"maunium.net/go/mautrix"
+ "maunium.net/go/mauview"
"maunium.net/go/tcell"
"maunium.net/go/gomuks/config"
@@ -27,55 +30,63 @@ import (
)
type ExpandedTextMessage struct {
- BaseMessage
- MsgText tstring.TString
+ Text tstring.TString
+ buffer []tstring.TString
}
// NewExpandedTextMessage creates a new ExpandedTextMessage object with the provided values and the default state.
-func NewExpandedTextMessage(event *mautrix.Event, displayname string, text tstring.TString) UIMessage {
- return &ExpandedTextMessage{
- BaseMessage: newBaseMessage(event, displayname),
- MsgText: text,
- }
+func NewExpandedTextMessage(event *mautrix.Event, displayname string, text tstring.TString) *UIMessage {
+ return newUIMessage(event, displayname, &ExpandedTextMessage{
+ Text: text,
+ })
}
-func NewDateChangeMessage(text string) UIMessage {
+func NewDateChangeMessage(text string) *UIMessage {
midnight := time.Now()
midnight = time.Date(midnight.Year(), midnight.Month(), midnight.Day(),
0, 0, 0, 0,
midnight.Location())
- return &ExpandedTextMessage{
- BaseMessage: BaseMessage{
- MsgSenderID: "*",
- MsgSender: "*",
- MsgTimestamp: midnight,
- MsgIsService: true,
+ return &UIMessage{
+ SenderID: "*",
+ SenderName: "*",
+ Timestamp: midnight,
+ IsService: true,
+ Renderer: &ExpandedTextMessage{
+ Text: tstring.NewColorTString(text, tcell.ColorGreen),
},
- MsgText: tstring.NewColorTString(text, tcell.ColorGreen),
}
}
-
-func (msg *ExpandedTextMessage) Clone() UIMessage {
+func (msg *ExpandedTextMessage) Clone() MessageRenderer {
return &ExpandedTextMessage{
- BaseMessage: msg.BaseMessage.clone(),
- MsgText: msg.MsgText.Clone(),
+ Text: msg.Text.Clone(),
}
}
-func (msg *ExpandedTextMessage) GenerateText() tstring.TString {
- return msg.MsgText
-}
-
func (msg *ExpandedTextMessage) NotificationContent() string {
- return msg.MsgText.String()
+ return msg.Text.String()
}
func (msg *ExpandedTextMessage) PlainText() string {
- return msg.MsgText.String()
+ return msg.Text.String()
+}
+
+func (msg *ExpandedTextMessage) String() string {
+ return fmt.Sprintf(`&messages.ExpandedTextMessage{Text="%s"}`, msg.Text.String())
+}
+
+func (msg *ExpandedTextMessage) CalculateBuffer(prefs config.UserPreferences, width int, uiMsg *UIMessage) {
+ msg.buffer = calculateBufferWithText(prefs, msg.Text, width, uiMsg)
}
-func (msg *ExpandedTextMessage) CalculateBuffer(prefs config.UserPreferences, width int) {
- msg.CalculateReplyBuffer(prefs, width)
- msg.calculateBufferWithText(prefs, msg.MsgText, width)
+func (msg *ExpandedTextMessage) Height() int {
+ return len(msg.buffer)
}
+
+func (msg *ExpandedTextMessage) Draw(screen mauview.Screen) {
+ for y, line := range msg.buffer {
+ line.Draw(screen, 0, y)
+ }
+}
+
+func (msg *ExpandedTextMessage) RegisterMatrix(matrix ifc.MatrixContainer) {}
diff --git a/ui/messages/htmlmessage.go b/ui/messages/htmlmessage.go
index 30b1588..5b95a82 100644
--- a/ui/messages/htmlmessage.go
+++ b/ui/messages/htmlmessage.go
@@ -17,9 +17,7 @@
package messages
import (
- "fmt"
- "strings"
-
+ ifc "maunium.net/go/gomuks/interface"
"maunium.net/go/mautrix"
"maunium.net/go/mauview"
"maunium.net/go/tcell"
@@ -29,30 +27,27 @@ import (
)
type HTMLMessage struct {
- BaseMessage
-
Root html.Entity
FocusedBg tcell.Color
focused bool
}
-func NewHTMLMessage(event *mautrix.Event, displayname string, root html.Entity) UIMessage {
- return &HTMLMessage{
- BaseMessage: newBaseMessage(event, displayname),
- Root: root,
- }
+func NewHTMLMessage(event *mautrix.Event, displayname string, root html.Entity) *UIMessage {
+ return newUIMessage(event, displayname, &HTMLMessage{
+ Root: root,
+ })
}
-func (hw *HTMLMessage) Clone() UIMessage {
+func (hw *HTMLMessage) RegisterMatrix(matrix ifc.MatrixContainer) {}
+
+func (hw *HTMLMessage) Clone() MessageRenderer {
return &HTMLMessage{
- BaseMessage: hw.BaseMessage.clone(),
- Root: hw.Root.Clone(),
- FocusedBg: hw.FocusedBg,
+ Root: hw.Root.Clone(),
+ FocusedBg: hw.FocusedBg,
}
}
func (hw *HTMLMessage) Draw(screen mauview.Screen) {
- screen = hw.DrawReply(screen)
if hw.focused {
screen.SetStyle(tcell.StyleDefault.Background(hw.FocusedBg))
}
@@ -80,18 +75,17 @@ func (hw *HTMLMessage) OnPasteEvent(event mauview.PasteEvent) bool {
return false
}
-func (hw *HTMLMessage) CalculateBuffer(preferences config.UserPreferences, width int) {
+func (hw *HTMLMessage) CalculateBuffer(preferences config.UserPreferences, width int, msg *UIMessage) {
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.ReplyHeight() + hw.Root.Height()
+ return hw.Root.Height()
}
func (hw *HTMLMessage) PlainText() string {
@@ -103,8 +97,5 @@ func (hw *HTMLMessage) NotificationContent() string {
}
func (hw *HTMLMessage) String() string {
- return fmt.Sprintf("&messages.HTMLMessage{\n" +
- " Base=%s,\n" +
- " Root=||\n%s\n" +
- "}", strings.Replace(hw.BaseMessage.String(), "\n", "\n ", -1), hw.Root.String())
+ return hw.Root.String()
}
diff --git a/ui/messages/imagemessage.go b/ui/messages/imagemessage.go
index 01a6500..6c31327 100644
--- a/ui/messages/imagemessage.go
+++ b/ui/messages/imagemessage.go
@@ -22,6 +22,7 @@ import (
"image/color"
"maunium.net/go/mautrix"
+ "maunium.net/go/mauview"
"maunium.net/go/tcell"
"maunium.net/go/gomuks/config"
@@ -32,32 +33,30 @@ import (
)
type ImageMessage struct {
- BaseMessage
Body string
Homeserver string
FileID string
data []byte
+ buffer []tstring.TString
matrix ifc.MatrixContainer
}
// NewImageMessage creates a new ImageMessage object with the provided values and the default state.
-func NewImageMessage(matrix ifc.MatrixContainer, event *mautrix.Event, displayname string, body, homeserver, fileID string, data []byte) UIMessage {
- return &ImageMessage{
- newBaseMessage(event, displayname),
- body,
- homeserver,
- fileID,
- data,
- matrix,
- }
+func NewImageMessage(matrix ifc.MatrixContainer, event *mautrix.Event, displayname string, body, homeserver, fileID string, data []byte) *UIMessage {
+ return newUIMessage(event, displayname, &ImageMessage{
+ Body: body,
+ Homeserver: homeserver,
+ FileID: fileID,
+ data: data,
+ matrix: matrix,
+ })
}
-func (msg *ImageMessage) Clone() UIMessage {
+func (msg *ImageMessage) Clone() MessageRenderer {
data := make([]byte, len(msg.data))
copy(data, msg.data)
return &ImageMessage{
- BaseMessage: msg.BaseMessage.clone(),
Body: msg.Body,
Homeserver: msg.Homeserver,
FileID: msg.FileID,
@@ -70,7 +69,7 @@ func (msg *ImageMessage) RegisterMatrix(matrix ifc.MatrixContainer) {
msg.matrix = matrix
if len(msg.data) == 0 {
- go msg.updateData()
+ //FIXME go msg.updateData()
}
}
@@ -82,6 +81,10 @@ func (msg *ImageMessage) PlainText() string {
return fmt.Sprintf("%s: %s", msg.Body, msg.matrix.GetDownloadURL(msg.Homeserver, msg.FileID))
}
+func (msg *ImageMessage) String() string {
+ return fmt.Sprintf(`&messages.ImageMessage{Body="%s", Homeserver="%s", FileID="%s"}`, msg.Body, msg.Homeserver, msg.FileID)
+}
+
func (msg *ImageMessage) updateData() {
defer debug.Recover()
debug.Print("Loading image:", msg.Homeserver, msg.FileID)
@@ -101,14 +104,13 @@ func (msg *ImageMessage) Path() string {
// CalculateBuffer generates the internal buffer for this message that consists
// of the text of this message split into lines at most as wide as the width
// parameter.
-func (msg *ImageMessage) CalculateBuffer(prefs config.UserPreferences, width int) {
+func (msg *ImageMessage) CalculateBuffer(prefs config.UserPreferences, width int, uiMsg *UIMessage) {
if width < 2 {
return
}
- msg.CalculateReplyBuffer(prefs, width)
if prefs.BareMessageView || prefs.DisableImages {
- msg.calculateBufferWithText(prefs, tstring.NewTString(msg.PlainText()), width)
+ msg.buffer = calculateBufferWithText(prefs, tstring.NewTString(msg.PlainText()), width, uiMsg)
return
}
@@ -121,3 +123,13 @@ func (msg *ImageMessage) CalculateBuffer(prefs config.UserPreferences, width int
msg.buffer = image.Render()
}
+
+func (msg *ImageMessage) Height() int {
+ return len(msg.buffer)
+}
+
+func (msg *ImageMessage) Draw(screen mauview.Screen) {
+ for y, line := range msg.buffer {
+ line.Draw(screen, 0, y)
+ }
+}
diff --git a/ui/messages/message.go b/ui/messages/message.go
deleted file mode 100644
index c990368..0000000
--- a/ui/messages/message.go
+++ /dev/null
@@ -1,53 +0,0 @@
-// gomuks - A terminal Matrix client written in Go.
-// Copyright (C) 2019 Tulir Asokan
-//
-// This program is free software: you can redistribute it and/or modify
-// it under the terms of the GNU Affero General Public License as published by
-// 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
-// GNU Affero General Public License for more details.
-//
-// 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/>.
-
-package messages
-
-import (
- "maunium.net/go/gomuks/config"
- "maunium.net/go/gomuks/interface"
- "maunium.net/go/mautrix"
- "maunium.net/go/mauview"
- "maunium.net/go/tcell"
-)
-
-// UIMessage is a wrapper for the content and metadata of a Matrix message intended to be displayed.
-type UIMessage interface {
- ifc.Message
-
- Type() mautrix.MessageType
- Sender() string
- SenderColor() tcell.Color
- TextColor() tcell.Color
- TimestampColor() tcell.Color
- FormatTime() string
- FormatDate() string
- SameDate(message UIMessage) bool
-
- SetReplyTo(message UIMessage)
- CalculateBuffer(preferences config.UserPreferences, width int)
- Draw(screen mauview.Screen)
- Height() int
- PlainText() string
-
- Clone() UIMessage
-
- RealSender() string
- RegisterMatrix(matrix ifc.MatrixContainer)
-}
-
-const DateFormat = "January _2, 2006"
-const TimeFormat = "15:04:05"
diff --git a/ui/messages/parser.go b/ui/messages/parser.go
index 0723257..29f078c 100644
--- a/ui/messages/parser.go
+++ b/ui/messages/parser.go
@@ -31,10 +31,10 @@ import (
"maunium.net/go/gomuks/ui/widget"
)
-func getCachedEvent(mainView ifc.MainView, roomID, eventID string) UIMessage {
+func getCachedEvent(mainView ifc.MainView, roomID, eventID string) *UIMessage {
if roomView := mainView.GetRoom(roomID); roomView != nil {
if replyToIfcMsg := roomView.GetEvent(eventID); replyToIfcMsg != nil {
- if replyToMsg, ok := replyToIfcMsg.(UIMessage); ok && replyToMsg != nil {
+ if replyToMsg, ok := replyToIfcMsg.(*UIMessage); ok && replyToMsg != nil {
return replyToMsg
}
}
@@ -42,24 +42,17 @@ func getCachedEvent(mainView ifc.MainView, roomID, eventID string) UIMessage {
return nil
}
-func ParseEvent(matrix ifc.MatrixContainer, mainView ifc.MainView, room *rooms.Room, evt *mautrix.Event) UIMessage {
+func ParseEvent(matrix ifc.MatrixContainer, mainView ifc.MainView, room *rooms.Room, evt *mautrix.Event) *UIMessage {
msg := directParseEvent(matrix, room, evt)
if msg == nil {
return nil
}
if len(evt.Content.GetReplyTo()) > 0 {
- replyToRoom := room
- if len(evt.Content.RelatesTo.InReplyTo.RoomID) > 0 {
- replyToRoom = matrix.GetRoom(evt.Content.RelatesTo.InReplyTo.RoomID)
- }
-
- if replyToMsg := getCachedEvent(mainView, replyToRoom.ID, evt.Content.GetReplyTo()); replyToMsg != nil {
- replyToMsg = replyToMsg.Clone()
- replyToMsg.SetReplyTo(nil)
- msg.SetReplyTo(replyToMsg)
- } else if replyToEvt, _ := matrix.GetEvent(replyToRoom, evt.Content.GetReplyTo()); replyToEvt != nil {
- if replyToMsg := directParseEvent(matrix, replyToRoom, replyToEvt); replyToMsg != nil {
- msg.SetReplyTo(replyToMsg)
+ if replyToMsg := getCachedEvent(mainView, room.ID, evt.Content.GetReplyTo()); replyToMsg != nil {
+ msg.ReplyTo = replyToMsg.Clone()
+ } else if replyToEvt, _ := matrix.GetEvent(room, evt.Content.GetReplyTo()); replyToEvt != nil {
+ if replyToMsg := directParseEvent(matrix, room, replyToEvt); replyToMsg != nil {
+ msg.ReplyTo = replyToMsg
} else {
// TODO add unrenderable reply header
}
@@ -70,15 +63,22 @@ func ParseEvent(matrix ifc.MatrixContainer, mainView ifc.MainView, room *rooms.R
return msg
}
-func directParseEvent(matrix ifc.MatrixContainer, room *rooms.Room, evt *mautrix.Event) UIMessage {
+func directParseEvent(matrix ifc.MatrixContainer, room *rooms.Room, evt *mautrix.Event) *UIMessage {
+ displayname := evt.Sender
+ member := room.GetMember(evt.Sender)
+ if member != nil {
+ displayname = member.Displayname
+ }
switch evt.Type {
case mautrix.EventSticker:
evt.Content.MsgType = mautrix.MsgImage
fallthrough
case mautrix.EventMessage:
- return ParseMessage(matrix, room, evt)
+ return ParseMessage(matrix, room, evt, displayname)
+ case mautrix.EventEncrypted:
+ return NewExpandedTextMessage(evt, displayname, tstring.NewStyleTString("Encrypted messages are not yet supported", tcell.StyleDefault.Italic(true)))
case mautrix.StateTopic, mautrix.StateRoomName, mautrix.StateAliases, mautrix.StateCanonicalAlias:
- return ParseStateEvent(matrix, room, evt)
+ return ParseStateEvent(evt, displayname)
case mautrix.StateMember:
return ParseMembershipEvent(room, evt)
}
@@ -86,12 +86,7 @@ func directParseEvent(matrix ifc.MatrixContainer, room *rooms.Room, evt *mautrix
return nil
}
-func ParseStateEvent(matrix ifc.MatrixContainer, room *rooms.Room, evt *mautrix.Event) UIMessage {
- displayname := evt.Sender
- member := room.GetMember(evt.Sender)
- if member != nil {
- displayname = member.Displayname
- }
+func ParseStateEvent(evt *mautrix.Event, displayname string) *UIMessage {
text := tstring.NewColorTString(displayname, widget.GetHashColor(evt.Sender))
switch evt.Type {
case mautrix.StateTopic:
@@ -124,15 +119,13 @@ func ParseStateEvent(matrix ifc.MatrixContainer, room *rooms.Room, evt *mautrix.
return NewExpandedTextMessage(evt, displayname, text)
}
-func ParseMessage(matrix ifc.MatrixContainer, room *rooms.Room, evt *mautrix.Event) UIMessage {
- displayname := evt.Sender
- member := room.GetMember(evt.Sender)
- if member != nil {
- displayname = member.Displayname
- }
+func ParseMessage(matrix ifc.MatrixContainer, room *rooms.Room, evt *mautrix.Event, displayname string) *UIMessage {
if len(evt.Content.GetReplyTo()) > 0 {
evt.Content.RemoveReplyFallback()
}
+ if evt.Content.GetRelatesTo().Type == mautrix.RelReplace && evt.Content.NewContent != nil {
+ evt.Content = *evt.Content.NewContent
+ }
switch evt.Content.MsgType {
case "m.text", "m.notice", "m.emote":
if evt.Content.Format == mautrix.FormatHTML {
@@ -224,7 +217,7 @@ func getMembershipEventContent(room *rooms.Room, evt *mautrix.Event) (sender str
return
}
-func ParseMembershipEvent(room *rooms.Room, evt *mautrix.Event) UIMessage {
+func ParseMembershipEvent(room *rooms.Room, evt *mautrix.Event) *UIMessage {
displayname, text := getMembershipEventContent(room, evt)
if len(text) == 0 {
return nil
diff --git a/ui/messages/textbase.go b/ui/messages/textbase.go
index 321d998..0d1cc3b 100644
--- a/ui/messages/textbase.go
+++ b/ui/messages/textbase.go
@@ -52,12 +52,12 @@ func matchBoundaryPattern(bare bool, extract tstring.TString) tstring.TString {
// CalculateBuffer generates the internal buffer for this message that consists
// of the text of this message split into lines at most as wide as the width
// parameter.
-func (msg *BaseMessage) calculateBufferWithText(prefs config.UserPreferences, text tstring.TString, width int) {
+func calculateBufferWithText(prefs config.UserPreferences, text tstring.TString, width int, msg *UIMessage) []tstring.TString {
if width < 2 {
- return
+ return nil
}
- msg.buffer = []tstring.TString{}
+ var buffer []tstring.TString
if prefs.BareMessageView {
newText := tstring.NewTString(msg.FormatTime())
@@ -74,7 +74,7 @@ func (msg *BaseMessage) calculateBufferWithText(prefs config.UserPreferences, te
newlines := 0
for _, str := range forcedLinebreaks {
if len(str) == 0 && newlines < 1 {
- msg.buffer = append(msg.buffer, tstring.TString{})
+ buffer = append(buffer, tstring.TString{})
newlines++
} else {
newlines = 0
@@ -88,8 +88,9 @@ func (msg *BaseMessage) calculateBufferWithText(prefs config.UserPreferences, te
}
extract = matchBoundaryPattern(prefs.BareMessageView, extract)
}
- msg.buffer = append(msg.buffer, extract)
+ buffer = append(buffer, extract)
str = str[len(extract):]
}
}
+ return buffer
}
diff --git a/ui/messages/textmessage.go b/ui/messages/textmessage.go
index f8c4573..9ace201 100644
--- a/ui/messages/textmessage.go
+++ b/ui/messages/textmessage.go
@@ -20,72 +20,82 @@ import (
"fmt"
"time"
+ ifc "maunium.net/go/gomuks/interface"
"maunium.net/go/mautrix"
+ "maunium.net/go/mauview"
"maunium.net/go/gomuks/config"
"maunium.net/go/gomuks/ui/messages/tstring"
)
type TextMessage struct {
- BaseMessage
- cache tstring.TString
- MsgText string
+ cache tstring.TString
+ buffer []tstring.TString
+ Text string
}
// NewTextMessage creates a new UITextMessage object with the provided values and the default state.
-func NewTextMessage(event *mautrix.Event, displayname string, text string) UIMessage {
- return &TextMessage{
- BaseMessage: newBaseMessage(event, displayname),
- MsgText: text,
- }
+func NewTextMessage(event *mautrix.Event, displayname string, text string) *UIMessage {
+ return newUIMessage(event, displayname, &TextMessage{
+ Text: text,
+ })
}
-func NewServiceMessage(text string) UIMessage {
- return &TextMessage{
- BaseMessage: BaseMessage{
- MsgSenderID: "*",
- MsgSender: "*",
- MsgTimestamp: time.Now(),
- MsgIsService: true,
+func NewServiceMessage(text string) *UIMessage {
+ return &UIMessage{
+ SenderID: "*",
+ SenderName: "*",
+ Timestamp: time.Now(),
+ IsService: true,
+ Renderer: &TextMessage{
+ Text: text,
},
- MsgText: text,
}
}
-func (msg *TextMessage) Clone() UIMessage {
+func (msg *TextMessage) Clone() MessageRenderer {
return &TextMessage{
- BaseMessage: msg.BaseMessage.clone(),
- MsgText: msg.MsgText,
+ Text: msg.Text,
}
}
-func (msg *TextMessage) getCache() tstring.TString {
+func (msg *TextMessage) getCache(uiMsg *UIMessage) tstring.TString {
if msg.cache == nil {
- switch msg.MsgType {
+ switch uiMsg.Type {
case "m.emote":
- msg.cache = tstring.NewColorTString(fmt.Sprintf("* %s %s", msg.MsgSender, msg.MsgText), msg.TextColor())
- msg.cache.Colorize(0, len(msg.MsgSender)+2, msg.SenderColor())
+ msg.cache = tstring.NewColorTString(fmt.Sprintf("* %s %s", uiMsg.SenderName, msg.Text), uiMsg.TextColor())
+ msg.cache.Colorize(0, len(uiMsg.SenderName)+2, uiMsg.SenderColor())
default:
- msg.cache = tstring.NewColorTString(msg.MsgText, msg.TextColor())
+ msg.cache = tstring.NewColorTString(msg.Text, uiMsg.TextColor())
}
}
return msg.cache
}
-func (msg *TextMessage) SetIsHighlight(isHighlight bool) {
- msg.BaseMessage.SetIsHighlight(isHighlight)
- msg.cache = nil
-}
-
func (msg *TextMessage) NotificationContent() string {
- return msg.MsgText
+ return msg.Text
}
func (msg *TextMessage) PlainText() string {
- return msg.MsgText
+ return msg.Text
+}
+
+func (msg *TextMessage) String() string {
+ return fmt.Sprintf(`&messages.TextMessage{Text="%s"}`, msg.Text)
}
-func (msg *TextMessage) CalculateBuffer(prefs config.UserPreferences, width int) {
- msg.CalculateReplyBuffer(prefs, width)
- msg.calculateBufferWithText(prefs, msg.getCache(), width)
+func (msg *TextMessage) CalculateBuffer(prefs config.UserPreferences, width int, uiMsg *UIMessage) {
+ msg.buffer = calculateBufferWithText(prefs, msg.getCache(uiMsg), width, uiMsg)
}
+
+func (msg *TextMessage) Height() int {
+ return len(msg.buffer)
+}
+
+func (msg *TextMessage) Draw(screen mauview.Screen) {
+ for y, line := range msg.buffer {
+ line.Draw(screen, 0, y)
+ }
+}
+
+func (msg *TextMessage) RegisterMatrix(matrix ifc.MatrixContainer) {}
diff --git a/ui/room-list.go b/ui/room-list.go
index 6b22c8e..53337b6 100644
--- a/ui/room-list.go
+++ b/ui/room-list.go
@@ -236,6 +236,10 @@ func (list *RoomList) AddScrollOffset(offset int) {
func (list *RoomList) First() (string, *rooms.Room) {
list.RLock()
defer list.RUnlock()
+ return list.first()
+}
+
+func (list *RoomList) first() (string, *rooms.Room) {
for _, tag := range list.tags {
trl := list.items[tag]
if trl.HasVisibleRooms() {
@@ -248,6 +252,10 @@ func (list *RoomList) First() (string, *rooms.Room) {
func (list *RoomList) Last() (string, *rooms.Room) {
list.RLock()
defer list.RUnlock()
+ return list.last()
+}
+
+func (list *RoomList) last() (string, *rooms.Room) {
for tagIndex := len(list.tags) - 1; tagIndex >= 0; tagIndex-- {
tag := list.tags[tagIndex]
trl := list.items[tag]
@@ -273,7 +281,7 @@ func (list *RoomList) Previous() (string, *rooms.Room) {
if len(list.items) == 0 {
return "", nil
} else if list.selected == nil {
- return list.First()
+ return list.first()
}
trl := list.items[list.selectedTag]
@@ -295,11 +303,11 @@ func (list *RoomList) Previous() (string, *rooms.Room) {
return prevTag, prevTRL.LastVisible()
}
}
- return list.Last()
+ return list.last()
} else if index >= 0 {
return list.selectedTag, trl.Visible()[index+1].Room
}
- return list.First()
+ return list.first()
}
func (list *RoomList) Next() (string, *rooms.Room) {
@@ -308,7 +316,7 @@ func (list *RoomList) Next() (string, *rooms.Room) {
if len(list.items) == 0 {
return "", nil
} else if list.selected == nil {
- return list.First()
+ return list.first()
}
trl := list.items[list.selectedTag]
@@ -330,11 +338,11 @@ func (list *RoomList) Next() (string, *rooms.Room) {
return nextTag, nextTRL.FirstVisible()
}
}
- return list.First()
+ return list.first()
} else if index > 0 {
return list.selectedTag, trl.Visible()[index-1].Room
}
- return list.Last()
+ return list.last()
}
// NextWithActivity Returns next room with activity.
@@ -415,8 +423,10 @@ func (list *RoomList) OnMouseEvent(event mauview.MouseEvent) bool {
switch event.Buttons() {
case tcell.WheelUp:
list.AddScrollOffset(-WheelScrollOffsetDiff)
+ return true
case tcell.WheelDown:
list.AddScrollOffset(WheelScrollOffsetDiff)
+ return true
case tcell.Button1:
x, y := event.Position()
return list.clickRoom(y, x, event.Modifiers() == tcell.ModCtrl)
@@ -478,7 +488,8 @@ func (list *RoomList) clickRoom(line, column int, mod bool) bool {
if trl.maxShown < 10 {
trl.maxShown = 10
}
- break
+ list.RUnlock()
+ return true
}
}
// Tag footer
diff --git a/ui/room-view.go b/ui/room-view.go
index a2e1bc1..00aa09a 100644
--- a/ui/room-view.go
+++ b/ui/room-view.go
@@ -57,6 +57,8 @@ type RoomView struct {
ulBorderScreen *mauview.ProxyScreen
ulScreen *mauview.ProxyScreen
+ userListLoaded bool
+
prevScreen mauview.Screen
parent *MainView
@@ -91,6 +93,14 @@ func NewRoomView(parent *MainView, room *rooms.Room) *RoomView {
config: parent.config,
}
view.content = NewMessageView(view)
+ view.Room.SetPreUnload(func() bool {
+ if view.parent.currentRoom == view {
+ return false
+ }
+ view.content.Unload()
+ return true
+ })
+ view.Room.SetPostLoad(view.loadTyping)
view.input.
SetBackgroundColor(tcell.ColorDefault).
@@ -99,7 +109,6 @@ func NewRoomView(parent *MainView, room *rooms.Room) *RoomView {
SetTabCompleteFunc(view.InputTabComplete)
view.topic.
- SetText(strings.Replace(room.GetTopic(), "\n", " ", -1)).
SetTextColor(tcell.ColorWhite).
SetBackgroundColor(tcell.ColorDarkGreen)
@@ -269,14 +278,20 @@ func (view *RoomView) SetCompletions(completions []string) {
view.completions.time = time.Now()
}
-func (view *RoomView) SetTyping(users []string) {
- for index, user := range users {
+func (view *RoomView) loadTyping() {
+ for index, user := range view.typing {
member := view.Room.GetMember(user)
if member != nil {
- users[index] = member.Displayname
+ view.typing[index] = member.Displayname
}
}
+}
+
+func (view *RoomView) SetTyping(users []string) {
view.typing = users
+ if view.Room.Loaded() {
+ view.loadTyping()
+ }
}
type completion struct {
@@ -385,11 +400,11 @@ func (view *RoomView) SendMessage(msgtype mautrix.MessageType, text string) {
text = emoji.Sprint(text)
}
evt := view.parent.matrix.PrepareMarkdownMessage(view.Room.ID, msgtype, text)
- msg := view.ParseEvent(evt)
+ msg := view.parseEvent(evt)
view.AddMessage(msg)
eventID, err := view.parent.matrix.SendEvent(evt)
if err != nil {
- msg.SetState(mautrix.EventStateSendFail)
+ msg.State = mautrix.EventStateSendFail
// Show shorter version if available
if httpErr, ok := err.(mautrix.HTTPError); ok {
err = httpErr
@@ -401,7 +416,10 @@ func (view *RoomView) SendMessage(msgtype mautrix.MessageType, text string) {
view.parent.parent.Render()
} else {
debug.Print("Event ID received:", eventID)
- //view.MessageView().UpdateMessageID(msg, eventID)
+ msg.EventID = eventID
+ msg.State = mautrix.EventStateDefault
+ view.MessageView().setMessageID(msg)
+ view.parent.parent.Render()
}
}
@@ -413,12 +431,20 @@ func (view *RoomView) MxRoom() *rooms.Room {
return view.Room
}
+func (view *RoomView) Update() {
+ view.topic.SetText(strings.Replace(view.Room.GetTopic(), "\n", " ", -1))
+ if !view.userListLoaded {
+ view.UpdateUserList()
+ }
+}
+
func (view *RoomView) UpdateUserList() {
pls := &mautrix.PowerLevels{}
if plEvent := view.Room.GetStateEvent(mautrix.StatePowerLevels, ""); plEvent != nil {
pls = plEvent.Content.GetPowerLevels()
}
view.userList.Update(view.Room.GetMembers(), pls)
+ view.userListLoaded = true
}
func (view *RoomView) AddServiceMessage(text string) {
@@ -429,10 +455,18 @@ func (view *RoomView) AddMessage(message ifc.Message) {
view.content.AddMessage(message, AppendMessage)
}
-func (view *RoomView) ParseEvent(evt *mautrix.Event) ifc.Message {
+func (view *RoomView) parseEvent(evt *mautrix.Event) *messages.UIMessage {
return messages.ParseEvent(view.parent.matrix, view.parent, view.Room, evt)
}
+func (view *RoomView) ParseEvent(evt *mautrix.Event) ifc.Message {
+ msg := view.parseEvent(evt)
+ if msg == nil {
+ return nil
+ }
+ return msg
+}
+
func (view *RoomView) GetEvent(eventID string) ifc.Message {
message, ok := view.content.messageIDs[eventID]
if !ok {
diff --git a/ui/view-login.go b/ui/view-login.go
index b929d62..a65a77c 100644
--- a/ui/view-login.go
+++ b/ui/view-login.go
@@ -74,7 +74,7 @@ func (ui *GomuksUI) NewLoginView() mauview.Component {
view.username.SetText(ui.gmx.Config().UserID)
view.password.SetMaskCharacter('*')
- view.quitButton.SetOnClick(ui.gmx.Stop).SetBackgroundColor(tcell.ColorDarkCyan)
+ view.quitButton.SetOnClick(func() { ui.gmx.Stop(true) }).SetBackgroundColor(tcell.ColorDarkCyan)
view.loginButton.SetOnClick(view.Login).SetBackgroundColor(tcell.ColorDarkCyan)
view.SetColumns([]int{1, 10, 1, 9, 1, 9, 1, 10, 1})
diff --git a/ui/view-main.go b/ui/view-main.go
index 48004c1..ea24644 100644
--- a/ui/view-main.go
+++ b/ui/view-main.go
@@ -26,6 +26,7 @@ import (
sync "github.com/sasha-s/go-deadlock"
+ "maunium.net/go/gomuks/ui/messages"
"maunium.net/go/mauview"
"maunium.net/go/tcell"
@@ -256,6 +257,7 @@ func (view *MainView) switchRoom(tag string, room *rooms.Room, lock bool) {
if room == nil {
return
}
+ room.Load()
roomView, ok := view.getRoomView(room.ID, lock)
if !ok {
@@ -263,12 +265,15 @@ func (view *MainView) switchRoom(tag string, room *rooms.Room, lock bool) {
debug.Print(tag, room)
return
}
+ roomView.Update()
view.roomView.SetInnerComponent(roomView)
view.currentRoom = roomView
view.MarkRead(roomView)
view.roomList.SetSelected(tag, room)
view.parent.Render()
- if len(roomView.MessageView().messages) == 0 {
+
+ if msgView := roomView.MessageView(); len(msgView.messages) < 20 && !msgView.initialHistoryLoaded {
+ msgView.initialHistoryLoaded = true
go view.LoadHistory(room.ID)
}
}
@@ -278,12 +283,6 @@ func (view *MainView) addRoomPage(room *rooms.Room) *RoomView {
roomView := NewRoomView(view, room).
SetInputChangedFunc(view.InputChanged)
view.rooms[room.ID] = roomView
- roomView.UpdateUserList()
-
- // TODO make sure this works
- if len(roomView.MessageView().messages) == 0 {
- go view.LoadHistory(room.ID)
- }
return roomView
}
return nil
@@ -292,7 +291,7 @@ func (view *MainView) addRoomPage(room *rooms.Room) *RoomView {
func (view *MainView) GetRoom(roomID string) ifc.RoomView {
room, ok := view.getRoomView(roomID, true)
if !ok {
- return view.addRoom(view.matrix.GetRoom(roomID))
+ return view.addRoom(view.matrix.GetOrCreateRoom(roomID))
}
return room
}
@@ -348,11 +347,11 @@ func (view *MainView) addRoom(room *rooms.Room) *RoomView {
return roomView
}
-func (view *MainView) SetRooms(rooms map[string]*rooms.Room) {
+func (view *MainView) SetRooms(rooms *rooms.RoomCache) {
view.roomList.Clear()
view.roomsLock.Lock()
view.rooms = make(map[string]*RoomView)
- for _, room := range rooms {
+ for _, room := range rooms.Map {
if room.HasLeft {
continue
}
@@ -388,9 +387,14 @@ func sendNotification(room *rooms.Room, sender, text string, critical, sound boo
notification.Send(sender, text, critical, sound)
}
-func (view *MainView) NotifyMessage(room *rooms.Room, message ifc.Message, should pushrules.PushActionArrayShould) {
+func (view *MainView) Bump(room *rooms.Room) {
view.roomList.Bump(room)
- if message.SenderID() == view.config.UserID {
+}
+
+func (view *MainView) NotifyMessage(room *rooms.Room, message ifc.Message, should pushrules.PushActionArrayShould) {
+ view.Bump(room)
+ uiMsg, ok := message.(*messages.UIMessage)
+ if ok && uiMsg.SenderID == view.config.UserID {
return
}
// Whether or not the room where the message came is the currently shown room.
@@ -420,16 +424,6 @@ func (view *MainView) NotifyMessage(room *rooms.Room, message ifc.Message, shoul
message.SetIsHighlight(should.Highlight)
}
-func (view *MainView) InitialSyncDone() {
- view.roomList.Clear()
- view.roomsLock.RLock()
- for _, room := range view.rooms {
- view.roomList.Add(room.Room)
- room.UpdateUserList()
- }
- view.roomsLock.RUnlock()
-}
-
func (view *MainView) LoadHistory(roomID string) {
defer debug.Recover()
roomView, ok := view.getRoomView(roomID, true)