Allow clicking names to mention them

This commit is contained in:
Tulir Asokan 2018-04-14 15:33:20 +03:00
parent ba3a6f5614
commit d060d10615
3 changed files with 60 additions and 18 deletions

View File

@ -22,19 +22,21 @@ import (
"math" "math"
"os" "os"
"maunium.net/go/gomuks/lib/open"
"maunium.net/go/gomuks/ui/messages/tstring"
"maunium.net/go/tcell"
"maunium.net/go/gomuks/debug" "maunium.net/go/gomuks/debug"
"maunium.net/go/gomuks/interface" "maunium.net/go/gomuks/interface"
"maunium.net/go/gomuks/lib/open"
"maunium.net/go/gomuks/ui/messages" "maunium.net/go/gomuks/ui/messages"
"maunium.net/go/gomuks/ui/messages/tstring"
"maunium.net/go/gomuks/ui/widget" "maunium.net/go/gomuks/ui/widget"
"maunium.net/go/tcell"
"maunium.net/go/tview" "maunium.net/go/tview"
) )
type MessageView struct { type MessageView struct {
*tview.Box *tview.Box
parent *RoomView
ScrollOffset int ScrollOffset int
MaxSenderWidth int MaxSenderWidth int
DateFormat string DateFormat string
@ -54,9 +56,11 @@ type MessageView struct {
metaBuffer []ifc.MessageMeta metaBuffer []ifc.MessageMeta
} }
func NewMessageView() *MessageView { func NewMessageView(parent *RoomView) *MessageView {
return &MessageView{ return &MessageView{
Box: tview.NewBox(), Box: tview.NewBox(),
parent: parent,
MaxSenderWidth: 15, MaxSenderWidth: 15,
TimestampWidth: len(messages.TimeFormat), TimestampWidth: len(messages.TimeFormat),
ScrollOffset: 0, ScrollOffset: 0,
@ -253,28 +257,64 @@ func (view *MessageView) recalculateBuffers() {
} }
} }
func (view *MessageView) HandleClick(x, y int, button tcell.ButtonMask) { func (view *MessageView) HandleClick(x, y int, button tcell.ButtonMask) bool {
if button != tcell.Button1 { if button != tcell.Button1 {
return return false
} }
_, _, _, height := view.GetRect() _, _, _, height := view.GetRect()
line := view.TotalHeight() - view.ScrollOffset - height + y line := view.TotalHeight() - view.ScrollOffset - height + y
if line < 0 || line >= view.TotalHeight() { if line < 0 || line >= view.TotalHeight() {
return return false
} }
message := view.metaBuffer[line] message := view.metaBuffer[line]
imageMessage, ok := message.(*messages.ImageMessage) var prevMessage ifc.MessageMeta
if !ok { if line > 0 {
uiMessage, ok := message.(messages.UIMessage) prevMessage = view.metaBuffer[line-1]
if ok {
debug.Print("Message clicked:", uiMessage.NotificationContent())
}
return
} }
open.Open(imageMessage.Path()) usernameX := view.TimestampWidth + TimestampSenderGap
messageX := usernameX + view.widestSender + SenderMessageGap
if x >= messageX {
switch message := message.(type) {
case *messages.ImageMessage:
open.Open(message.Path())
case messages.UIMessage:
debug.Print("Message clicked:", message.NotificationContent())
}
} else if x >= usernameX {
uiMessage, ok := message.(messages.UIMessage)
if !ok {
return false
}
prevUIMessage, _ := prevMessage.(messages.UIMessage)
if prevUIMessage != nil && prevUIMessage.Sender() == uiMessage.Sender() {
return false
}
sender := []rune(uiMessage.Sender())
if len(sender) == 0 {
return false
}
cursorPos := view.parent.input.GetCursorOffset()
text := []rune(view.parent.input.GetText())
var newText []rune
if cursorPos == 0 {
newText = append(sender, ':', ' ')
newText = append(newText, text...)
} else {
newText = append(text[0:cursorPos], sender...)
newText = append(newText, ' ')
newText = append(newText, text[cursorPos:]...)
}
view.parent.input.SetText(string(newText))
view.parent.input.SetCursorOffset(cursorPos + len(newText) - len(text))
return true
}
return false
} }
const PaddingAtTop = 5 const PaddingAtTop = 5

View File

@ -47,13 +47,13 @@ func NewRoomView(room *rooms.Room) *RoomView {
view := &RoomView{ view := &RoomView{
Box: tview.NewBox(), Box: tview.NewBox(),
topic: tview.NewTextView(), topic: tview.NewTextView(),
content: NewMessageView(),
status: tview.NewTextView(), status: tview.NewTextView(),
userList: tview.NewTextView(), userList: tview.NewTextView(),
ulBorder: widget.NewBorder(), ulBorder: widget.NewBorder(),
input: widget.NewAdvancedInputField(), input: widget.NewAdvancedInputField(),
Room: room, Room: room,
} }
view.content = NewMessageView(view)
view.input. view.input.
SetFieldBackgroundColor(tcell.ColorDefault). SetFieldBackgroundColor(tcell.ColorDefault).

View File

@ -248,7 +248,9 @@ func (view *MainView) MouseEventHandler(roomView *RoomView, event *tcell.EventMo
default: default:
mx, my, mw, mh := msgView.GetRect() mx, my, mw, mh := msgView.GetRect()
if x >= mx && y >= my && x < mx+mw && y < my+mh { if x >= mx && y >= my && x < mx+mw && y < my+mh {
msgView.HandleClick(x-mx, y-my, event.Buttons()) if msgView.HandleClick(x-mx, y-my, event.Buttons()) {
view.parent.Render()
}
} else { } else {
debug.Print("Mouse event received:", event.Buttons(), event.Modifiers(), x, y) debug.Print("Mouse event received:", event.Buttons(), event.Modifiers(), x, y)
} }