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,33 +257,17 @@ func (view *MessageView) recalculateBuffers() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (view *MessageView) HandleClick(x, y int, button tcell.ButtonMask) bool {
|
func (view *MessageView) handleMessageClick(message ifc.MessageMeta) bool {
|
||||||
if button != tcell.Button1 {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
_, _, _, height := view.GetRect()
|
|
||||||
line := view.TotalHeight() - view.ScrollOffset - height + y
|
|
||||||
if line < 0 || line >= view.TotalHeight() {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
message := view.metaBuffer[line]
|
|
||||||
var prevMessage ifc.MessageMeta
|
|
||||||
if line > 0 {
|
|
||||||
prevMessage = view.metaBuffer[line-1]
|
|
||||||
}
|
|
||||||
|
|
||||||
usernameX := view.TimestampWidth + TimestampSenderGap
|
|
||||||
messageX := usernameX + view.widestSender + SenderMessageGap
|
|
||||||
if x >= messageX {
|
|
||||||
switch message := message.(type) {
|
switch message := message.(type) {
|
||||||
case *messages.ImageMessage:
|
case *messages.ImageMessage:
|
||||||
open.Open(message.Path())
|
open.Open(message.Path())
|
||||||
case messages.UIMessage:
|
case messages.UIMessage:
|
||||||
debug.Print("Message clicked:", message.NotificationContent())
|
debug.Print("Message clicked:", message.NotificationContent())
|
||||||
}
|
}
|
||||||
} else if x >= usernameX {
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
func (view *MessageView) handleUsernameClick(message ifc.MessageMeta, prevMessage ifc.MessageMeta) bool {
|
||||||
uiMessage, ok := message.(messages.UIMessage)
|
uiMessage, ok := message.(messages.UIMessage)
|
||||||
if !ok {
|
if !ok {
|
||||||
return false
|
return false
|
||||||
@ -313,8 +297,36 @@ func (view *MessageView) HandleClick(x, y int, button tcell.ButtonMask) bool {
|
|||||||
view.parent.input.SetText(string(newText))
|
view.parent.input.SetText(string(newText))
|
||||||
view.parent.input.SetCursorOffset(cursorPos + len(newText) - len(text))
|
view.parent.input.SetCursorOffset(cursorPos + len(newText) - len(text))
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (view *MessageView) HandleClick(x, y int, button tcell.ButtonMask) bool {
|
||||||
|
if button != tcell.Button1 {
|
||||||
return false
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
_, _, _, height := view.GetRect()
|
||||||
|
line := view.TotalHeight() - view.ScrollOffset - height + y
|
||||||
|
if line < 0 || line >= view.TotalHeight() {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
message := view.metaBuffer[line]
|
||||||
|
var prevMessage ifc.MessageMeta
|
||||||
|
if y != 0 && line > 0 {
|
||||||
|
prevMessage = view.metaBuffer[line-1]
|
||||||
|
}
|
||||||
|
|
||||||
|
usernameX := view.TimestampWidth + TimestampSenderGap
|
||||||
|
messageX := usernameX + view.widestSender + SenderMessageGap
|
||||||
|
|
||||||
|
shouldRerender := false
|
||||||
|
if x >= messageX {
|
||||||
|
shouldRerender = view.handleMessageClick(message)
|
||||||
|
} else if x >= usernameX {
|
||||||
|
shouldRerender = view.handleUsernameClick(message, prevMessage)
|
||||||
|
}
|
||||||
|
|
||||||
|
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