Refactor things
This commit is contained in:
parent
28e5f9d4b0
commit
bb9ed4558b
@ -419,6 +419,7 @@ func (c *Container) Download(mxcURL string) (data []byte, hs, id string, err err
|
|||||||
err = fmt.Errorf("invalid matrix content URL")
|
err = fmt.Errorf("invalid matrix content URL")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
hs = parts[1]
|
hs = parts[1]
|
||||||
id = parts[2]
|
id = parts[2]
|
||||||
|
|
||||||
@ -430,6 +431,11 @@ func (c *Container) Download(mxcURL string) (data []byte, hs, id string, err err
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
data, err = c.download(hs, id, cacheFile)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Container) download(hs, id, cacheFile string) (data []byte, err error) {
|
||||||
dlURL, _ := url.Parse(c.client.HomeserverURL.String())
|
dlURL, _ := url.Parse(c.client.HomeserverURL.String())
|
||||||
dlURL.Path = path.Join(dlURL.Path, "/_matrix/media/v1/download", hs, id)
|
dlURL.Path = path.Join(dlURL.Path, "/_matrix/media/v1/download", hs, id)
|
||||||
|
|
||||||
|
@ -257,6 +257,48 @@ func (view *MessageView) recalculateBuffers() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (view *MessageView) handleMessageClick(message ifc.MessageMeta) bool {
|
||||||
|
switch message := message.(type) {
|
||||||
|
case *messages.ImageMessage:
|
||||||
|
open.Open(message.Path())
|
||||||
|
case messages.UIMessage:
|
||||||
|
debug.Print("Message clicked:", message.NotificationContent())
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
func (view *MessageView) handleUsernameClick(message ifc.MessageMeta, prevMessage ifc.MessageMeta) bool {
|
||||||
|
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
|
||||||
|
}
|
||||||
|
|
||||||
func (view *MessageView) HandleClick(x, y int, button tcell.ButtonMask) bool {
|
func (view *MessageView) HandleClick(x, y int, button tcell.ButtonMask) bool {
|
||||||
if button != tcell.Button1 {
|
if button != tcell.Button1 {
|
||||||
return false
|
return false
|
||||||
@ -270,51 +312,21 @@ func (view *MessageView) HandleClick(x, y int, button tcell.ButtonMask) bool {
|
|||||||
|
|
||||||
message := view.metaBuffer[line]
|
message := view.metaBuffer[line]
|
||||||
var prevMessage ifc.MessageMeta
|
var prevMessage ifc.MessageMeta
|
||||||
if line > 0 {
|
if y != 0 && line > 0 {
|
||||||
prevMessage = view.metaBuffer[line-1]
|
prevMessage = view.metaBuffer[line-1]
|
||||||
}
|
}
|
||||||
|
|
||||||
usernameX := view.TimestampWidth + TimestampSenderGap
|
usernameX := view.TimestampWidth + TimestampSenderGap
|
||||||
messageX := usernameX + view.widestSender + SenderMessageGap
|
messageX := usernameX + view.widestSender + SenderMessageGap
|
||||||
|
|
||||||
|
shouldRerender := false
|
||||||
if x >= messageX {
|
if x >= messageX {
|
||||||
switch message := message.(type) {
|
shouldRerender = view.handleMessageClick(message)
|
||||||
case *messages.ImageMessage:
|
|
||||||
open.Open(message.Path())
|
|
||||||
case messages.UIMessage:
|
|
||||||
debug.Print("Message clicked:", message.NotificationContent())
|
|
||||||
}
|
|
||||||
} else if x >= usernameX {
|
} else if x >= usernameX {
|
||||||
uiMessage, ok := message.(messages.UIMessage)
|
shouldRerender = view.handleUsernameClick(message, prevMessage)
|
||||||
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
|
|
||||||
|
return shouldRerender
|
||||||
}
|
}
|
||||||
|
|
||||||
const PaddingAtTop = 5
|
const PaddingAtTop = 5
|
||||||
@ -382,9 +394,31 @@ func getScrollbarStyle(scrollbarHere, isTop, isBottom bool) (char rune, style tc
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func (view *MessageView) Draw(screen tcell.Screen) {
|
func (view *MessageView) calculateScrollBar(height int) (scrollBarHeight, scrollBarPos int) {
|
||||||
view.Box.Draw(screen)
|
viewportHeight := float64(height)
|
||||||
|
contentHeight := float64(view.TotalHeight())
|
||||||
|
|
||||||
|
scrollBarHeight = int(math.Ceil(viewportHeight / (contentHeight / viewportHeight)))
|
||||||
|
|
||||||
|
scrollBarPos = height - int(math.Round(float64(view.ScrollOffset)/contentHeight*viewportHeight))
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func (view *MessageView) getIndexOffset(screen tcell.Screen, height, messageX int) (indexOffset int) {
|
||||||
|
indexOffset = view.TotalHeight() - view.ScrollOffset - height
|
||||||
|
if indexOffset <= -PaddingAtTop {
|
||||||
|
message := "Scroll up to load more messages."
|
||||||
|
if view.LoadingMessages {
|
||||||
|
message = "Loading more messages..."
|
||||||
|
}
|
||||||
|
_, y, _, _ := view.GetInnerRect()
|
||||||
|
widget.WriteLineSimpleColor(screen, message, messageX, y, tcell.ColorGreen)
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func (view *MessageView) Draw(screen tcell.Screen) {
|
||||||
x, y, _, height := view.GetInnerRect()
|
x, y, _, height := view.GetInnerRect()
|
||||||
view.recalculateBuffers()
|
view.recalculateBuffers()
|
||||||
|
|
||||||
@ -397,30 +431,14 @@ func (view *MessageView) Draw(screen tcell.Screen) {
|
|||||||
messageX := usernameX + view.widestSender + SenderMessageGap
|
messageX := usernameX + view.widestSender + SenderMessageGap
|
||||||
separatorX := usernameX + view.widestSender + SenderSeparatorGap
|
separatorX := usernameX + view.widestSender + SenderSeparatorGap
|
||||||
|
|
||||||
indexOffset := view.TotalHeight() - view.ScrollOffset - height
|
indexOffset := view.getIndexOffset(screen, height, messageX)
|
||||||
if indexOffset <= -PaddingAtTop {
|
|
||||||
message := "Scroll up to load more messages."
|
|
||||||
if view.LoadingMessages {
|
|
||||||
message = "Loading more messages..."
|
|
||||||
}
|
|
||||||
widget.WriteLineSimpleColor(screen, message, messageX, y, tcell.ColorGreen)
|
|
||||||
}
|
|
||||||
|
|
||||||
if len(view.textBuffer) != len(view.metaBuffer) {
|
if len(view.textBuffer) != len(view.metaBuffer) {
|
||||||
debug.Printf("Unexpected text/meta buffer length mismatch: %d != %d.", len(view.textBuffer), len(view.metaBuffer))
|
debug.Printf("Unexpected text/meta buffer length mismatch: %d != %d.", len(view.textBuffer), len(view.metaBuffer))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
var scrollBarHeight, scrollBarPos int
|
scrollBarHeight, scrollBarPos := view.calculateScrollBar(height)
|
||||||
// Black magic (aka math) used to figure out where the scroll bar should be put.
|
|
||||||
{
|
|
||||||
viewportHeight := float64(height)
|
|
||||||
contentHeight := float64(view.TotalHeight())
|
|
||||||
|
|
||||||
scrollBarHeight = int(math.Ceil(viewportHeight / (contentHeight / viewportHeight)))
|
|
||||||
|
|
||||||
scrollBarPos = height - int(math.Round(float64(view.ScrollOffset)/contentHeight*viewportHeight))
|
|
||||||
}
|
|
||||||
|
|
||||||
var prevMeta ifc.MessageMeta
|
var prevMeta ifc.MessageMeta
|
||||||
firstLine := true
|
firstLine := true
|
||||||
|
Loading…
Reference in New Issue
Block a user