Fix date change messages and input submit handling

This commit is contained in:
Tulir Asokan 2019-04-10 01:42:27 +03:00
parent bbde121947
commit 06c306bee9
5 changed files with 54 additions and 25 deletions

View File

@ -120,7 +120,6 @@ func (view *MessageView) AddMessage(ifcMessage ifc.Message, direction MessageDir
return
}
var oldMsg messages.UIMessage
var messageExists bool
if oldMsg, messageExists = view.messageIDs[message.ID()]; messageExists {
@ -141,14 +140,30 @@ func (view *MessageView) AddMessage(ifcMessage ifc.Message, direction MessageDir
}
message.CalculateBuffer(view.config.Preferences, width)
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
}
if direction == AppendMessage {
if view.ScrollOffset > 0 {
view.ScrollOffset += message.Height()
}
view.messages = append(view.messages, message)
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)
}
view.appendBuffer(message)
} else if direction == PrependMessage {
view.messages = append([]messages.UIMessage{message}, view.messages...)
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...)
}
} else if oldMsg != nil {
view.replaceBuffer(oldMsg, message)
} else {
@ -156,21 +171,12 @@ func (view *MessageView) AddMessage(ifcMessage ifc.Message, direction MessageDir
debug.PrintStack()
}
view.messageIDs[message.ID()] = message
if len(message.ID()) > 0 {
view.messageIDs[message.ID()] = message
}
}
func (view *MessageView) appendBuffer(message messages.UIMessage) {
if len(view.msgBuffer) > 0 {
prevMeta := view.msgBuffer[len(view.msgBuffer)-1]
if prevMeta != nil && prevMeta.FormatDate() != message.FormatDate() {
/* FIXME view.textBuffer = append(view.textBuffer, tstring.NewColorTString(
fmt.Sprintf("Date changed to %s", message.FormatDate()),
tcell.ColorGreen))
view.msgBuffer = append(view.msgBuffer, &messages.BasicMeta{
BTimestampColor: tcell.ColorDefault, BTextColor: tcell.ColorGreen})*/
}
}
for i := 0; i < message.Height(); i++ {
view.msgBuffer = append(view.msgBuffer, message)
}
@ -178,7 +184,9 @@ func (view *MessageView) appendBuffer(message messages.UIMessage) {
}
func (view *MessageView) replaceMessage(original messages.UIMessage, new messages.UIMessage) {
view.messageIDs[new.ID()] = new
if len(new.ID()) > 0 {
view.messageIDs[new.ID()] = new
}
for index, msg := range view.messages {
if msg == original {
view.messages[index] = new

View File

@ -191,6 +191,12 @@ func (msg *BaseMessage) FormatDate() string {
return msg.MsgTimestamp.Format(DateFormat)
}
func (msg *BaseMessage) 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

View File

@ -17,7 +17,10 @@
package messages
import (
"time"
"maunium.net/go/mautrix"
"maunium.net/go/tcell"
"maunium.net/go/gomuks/config"
"maunium.net/go/gomuks/ui/messages/tstring"
@ -36,6 +39,24 @@ func NewExpandedTextMessage(event *mautrix.Event, displayname string, text tstri
}
}
var ZeroTime = time.Time{}
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,
},
MsgText: tstring.NewColorTString(text, tcell.ColorGreen),
}
}
func (msg *ExpandedTextMessage) GenerateText() tstring.TString {
return msg.MsgText
}

View File

@ -35,6 +35,7 @@ type UIMessage interface {
TimestampColor() tcell.Color
FormatTime() string
FormatDate() string
SameDate(message UIMessage) bool
CalculateBuffer(preferences config.UserPreferences, width int)
Draw(screen mauview.Screen)

View File

@ -57,8 +57,6 @@ type RoomView struct {
ulBorderScreen *mauview.ProxyScreen
ulScreen *mauview.ProxyScreen
inputSubmitFunc func(room *RoomView, text string)
prevScreen mauview.Screen
parent *MainView
@ -118,11 +116,6 @@ func (view *RoomView) logPath(dir string) string {
return filepath.Join(dir, fmt.Sprintf("%s.gmxlog", view.Room.ID))
}
func (view *RoomView) SetInputSubmitFunc(fn func(room *RoomView, text string)) *RoomView {
view.inputSubmitFunc = fn
return view
}
func (view *RoomView) SetInputChangedFunc(fn func(room *RoomView, text string)) *RoomView {
view.input.SetChangedFunc(func(text string) {
fn(view, text)
@ -250,8 +243,8 @@ func (view *RoomView) OnKeyEvent(event mauview.KeyEvent) bool {
msgView.AddScrollOffset(-msgView.Height() / 2)
return true
case tcell.KeyEnter:
if event.Modifiers()&tcell.ModShift == 0 && event.Modifiers()&tcell.ModCtrl == 0 && view.inputSubmitFunc != nil {
view.inputSubmitFunc(view, view.input.GetText())
if event.Modifiers()&tcell.ModShift == 0 && event.Modifiers()&tcell.ModCtrl == 0 {
view.InputSubmit(view.input.GetText())
return true
}
}