Refactor things

This commit is contained in:
Tulir Asokan 2018-04-15 14:03:05 +03:00
parent 28e5f9d4b0
commit bb9ed4558b
2 changed files with 81 additions and 57 deletions

View File

@ -419,6 +419,7 @@ func (c *Container) Download(mxcURL string) (data []byte, hs, id string, err err
err = fmt.Errorf("invalid matrix content URL")
return
}
hs = parts[1]
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.Path = path.Join(dlURL.Path, "/_matrix/media/v1/download", hs, id)

View File

@ -257,33 +257,17 @@ func (view *MessageView) recalculateBuffers() {
}
}
func (view *MessageView) HandleClick(x, y int, button tcell.ButtonMask) 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 {
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())
}
} else if x >= usernameX {
return false
}
func (view *MessageView) handleUsernameClick(message ifc.MessageMeta, prevMessage ifc.MessageMeta) bool {
uiMessage, ok := message.(messages.UIMessage)
if !ok {
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.SetCursorOffset(cursorPos + len(newText) - len(text))
return true
}
}
func (view *MessageView) HandleClick(x, y int, button tcell.ButtonMask) 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 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
@ -382,9 +394,31 @@ func getScrollbarStyle(scrollbarHere, isTop, isBottom bool) (char rune, style tc
return
}
func (view *MessageView) Draw(screen tcell.Screen) {
view.Box.Draw(screen)
func (view *MessageView) calculateScrollBar(height int) (scrollBarHeight, scrollBarPos int) {
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()
view.recalculateBuffers()
@ -397,30 +431,14 @@ func (view *MessageView) Draw(screen tcell.Screen) {
messageX := usernameX + view.widestSender + SenderMessageGap
separatorX := usernameX + view.widestSender + SenderSeparatorGap
indexOffset := view.TotalHeight() - view.ScrollOffset - height
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)
}
indexOffset := view.getIndexOffset(screen, height, messageX)
if len(view.textBuffer) != len(view.metaBuffer) {
debug.Printf("Unexpected text/meta buffer length mismatch: %d != %d.", len(view.textBuffer), len(view.metaBuffer))
return
}
var scrollBarHeight, scrollBarPos int
// 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))
}
scrollBarHeight, scrollBarPos := view.calculateScrollBar(height)
var prevMeta ifc.MessageMeta
firstLine := true