aboutsummaryrefslogtreecommitdiff
path: root/ui
diff options
context:
space:
mode:
Diffstat (limited to 'ui')
-rw-r--r--ui/message-view.go40
-rw-r--r--ui/messages/base.go6
-rw-r--r--ui/messages/expandedtextmessage.go21
-rw-r--r--ui/messages/message.go1
-rw-r--r--ui/room-view.go11
5 files changed, 54 insertions, 25 deletions
diff --git a/ui/message-view.go b/ui/message-view.go
index 87889fb..a2ed029 100644
--- a/ui/message-view.go
+++ b/ui/message-view.go
@@ -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
diff --git a/ui/messages/base.go b/ui/messages/base.go
index d9db866..f54c2cd 100644
--- a/ui/messages/base.go
+++ b/ui/messages/base.go
@@ -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
diff --git a/ui/messages/expandedtextmessage.go b/ui/messages/expandedtextmessage.go
index 4a4ed24..b2005b9 100644
--- a/ui/messages/expandedtextmessage.go
+++ b/ui/messages/expandedtextmessage.go
@@ -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
}
diff --git a/ui/messages/message.go b/ui/messages/message.go
index bd78e5d..c1f2014 100644
--- a/ui/messages/message.go
+++ b/ui/messages/message.go
@@ -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)
diff --git a/ui/room-view.go b/ui/room-view.go
index e37760e..4402eaa 100644
--- a/ui/room-view.go
+++ b/ui/room-view.go
@@ -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
}
}