Optimized rendering a lot of labels.

- Added caching of the desired size of a label.
This commit is contained in:
Sander Schobers 2019-07-04 22:15:32 +02:00
parent 9e577ab1aa
commit 36d620108c
2 changed files with 30 additions and 5 deletions

21
ui/desiredsizecache.go Normal file
View File

@ -0,0 +1,21 @@
package ui
import (
"github.com/minio/highwayhash"
"opslag.de/schobers/geom"
)
type desiredSizeCache struct {
sum [32]byte
size geom.PointF32
}
func (c *desiredSizeCache) Update(ctx Context, data string, calcFn func(Context) geom.PointF32) geom.PointF32 {
var key = [32]byte{}
sum := highwayhash.Sum([]byte(data), key[:])
if c.sum != sum {
c.size = calcFn(ctx)
c.sum = sum
}
return c.size
}

View File

@ -8,6 +8,8 @@ type Label struct {
ControlBase ControlBase
Text string Text string
size desiredSizeCache
} }
func BuildLabel(text string, fn func(*Label)) *Label { func BuildLabel(text string, fn func(*Label)) *Label {
@ -20,11 +22,13 @@ func BuildLabel(text string, fn func(*Label)) *Label {
func (l *Label) DesiredSize(ctx Context) geom.PointF32 { func (l *Label) DesiredSize(ctx Context) geom.PointF32 {
var fontName = l.FontName(ctx) var fontName = l.FontName(ctx)
return l.size.Update(ctx, fontName+l.Text, func(ctx Context) geom.PointF32 {
var font = ctx.Renderer().Font(fontName) var font = ctx.Renderer().Font(fontName)
var width = font.WidthOf(l.Text) var width = font.WidthOf(l.Text)
var height = font.Height() var height = font.Height()
var pad = ctx.Style().Dimensions.TextPadding var pad = ctx.Style().Dimensions.TextPadding
return geom.PtF32(width+pad*2, height+pad*2) return geom.PtF32(width+pad*2, height+pad*2)
})
} }
func (l *Label) Render(ctx Context) { func (l *Label) Render(ctx Context) {