Add scrollbar
This transplants the logic for drawing the scrollbar from dirlist and the completion popover and adds it to the msglist.
This commit is contained in:
parent
15b72df1da
commit
d841c8c251
1 changed files with 37 additions and 3 deletions
|
@ -3,6 +3,7 @@ package widgets
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
|
"math"
|
||||||
|
|
||||||
"github.com/gdamore/tcell"
|
"github.com/gdamore/tcell"
|
||||||
"github.com/mattn/go-runewidth"
|
"github.com/mattn/go-runewidth"
|
||||||
|
@ -65,6 +66,20 @@ func (ml *MessageList) Draw(ctx *ui.Context) {
|
||||||
|
|
||||||
ml.ensureScroll()
|
ml.ensureScroll()
|
||||||
|
|
||||||
|
needScrollbar := true
|
||||||
|
percentVisible := float64(ctx.Height()) / float64(len(store.Uids()))
|
||||||
|
if percentVisible >= 1.0 {
|
||||||
|
needScrollbar = false
|
||||||
|
}
|
||||||
|
|
||||||
|
textWidth := ctx.Width()
|
||||||
|
if needScrollbar {
|
||||||
|
textWidth -= 1
|
||||||
|
}
|
||||||
|
if textWidth < 0 {
|
||||||
|
textWidth = 0
|
||||||
|
}
|
||||||
|
|
||||||
var (
|
var (
|
||||||
needsHeaders []uint32
|
needsHeaders []uint32
|
||||||
row int = 0
|
row int = 0
|
||||||
|
@ -81,7 +96,7 @@ func (ml *MessageList) Draw(ctx *ui.Context) {
|
||||||
|
|
||||||
if msg == nil {
|
if msg == nil {
|
||||||
needsHeaders = append(needsHeaders, uid)
|
needsHeaders = append(needsHeaders, uid)
|
||||||
ml.spinner.Draw(ctx.Subcontext(0, row, ctx.Width(), 1))
|
ml.spinner.Draw(ctx.Subcontext(0, row, textWidth, 1))
|
||||||
row += 1
|
row += 1
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
@ -107,7 +122,7 @@ func (ml *MessageList) Draw(ctx *ui.Context) {
|
||||||
style = style.Bold(true)
|
style = style.Bold(true)
|
||||||
}
|
}
|
||||||
|
|
||||||
ctx.Fill(0, row, ctx.Width(), 1, ' ', style)
|
ctx.Fill(0, row, textWidth, 1, ' ', style)
|
||||||
uiConfig := ml.conf.GetUiConfig(map[config.ContextType]string{
|
uiConfig := ml.conf.GetUiConfig(map[config.ContextType]string{
|
||||||
config.UI_CONTEXT_ACCOUNT: ml.aerc.SelectedAccount().AccountConfig().Name,
|
config.UI_CONTEXT_ACCOUNT: ml.aerc.SelectedAccount().AccountConfig().Name,
|
||||||
config.UI_CONTEXT_FOLDER: ml.aerc.SelectedAccount().Directories().Selected(),
|
config.UI_CONTEXT_FOLDER: ml.aerc.SelectedAccount().Directories().Selected(),
|
||||||
|
@ -122,13 +137,18 @@ func (ml *MessageList) Draw(ctx *ui.Context) {
|
||||||
ctx.Printf(0, row, style, "%v", err)
|
ctx.Printf(0, row, style, "%v", err)
|
||||||
} else {
|
} else {
|
||||||
line := fmt.Sprintf(fmtStr, args...)
|
line := fmt.Sprintf(fmtStr, args...)
|
||||||
line = runewidth.Truncate(line, ctx.Width(), "…")
|
line = runewidth.Truncate(line, textWidth, "…")
|
||||||
ctx.Printf(0, row, style, "%s", line)
|
ctx.Printf(0, row, style, "%s", line)
|
||||||
}
|
}
|
||||||
|
|
||||||
row += 1
|
row += 1
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if needScrollbar {
|
||||||
|
scrollbarCtx := ctx.Subcontext(ctx.Width()-1, 0, 1, ctx.Height())
|
||||||
|
ml.drawScrollbar(scrollbarCtx, percentVisible)
|
||||||
|
}
|
||||||
|
|
||||||
if len(uids) == 0 {
|
if len(uids) == 0 {
|
||||||
if store.Sorting {
|
if store.Sorting {
|
||||||
ml.spinner.Start()
|
ml.spinner.Start()
|
||||||
|
@ -147,6 +167,20 @@ func (ml *MessageList) Draw(ctx *ui.Context) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (ml *MessageList) drawScrollbar(ctx *ui.Context, percentVisible float64) {
|
||||||
|
gutterStyle := tcell.StyleDefault
|
||||||
|
pillStyle := tcell.StyleDefault.Reverse(true)
|
||||||
|
|
||||||
|
// gutter
|
||||||
|
ctx.Fill(0, 0, 1, ctx.Height(), ' ', gutterStyle)
|
||||||
|
|
||||||
|
// pill
|
||||||
|
pillSize := int(math.Ceil(float64(ctx.Height()) * percentVisible))
|
||||||
|
percentScrolled := float64(ml.scroll) / float64(len(ml.Store().Uids()))
|
||||||
|
pillOffset := int(math.Floor(float64(ctx.Height()) * percentScrolled))
|
||||||
|
ctx.Fill(0, pillOffset, 1, pillSize, ' ', pillStyle)
|
||||||
|
}
|
||||||
|
|
||||||
func (ml *MessageList) MouseEvent(localX int, localY int, event tcell.Event) {
|
func (ml *MessageList) MouseEvent(localX int, localY int, event tcell.Event) {
|
||||||
switch event := event.(type) {
|
switch event := event.(type) {
|
||||||
case *tcell.EventMouse:
|
case *tcell.EventMouse:
|
||||||
|
|
Loading…
Reference in a new issue