Optimized rendering a lot of labels.
- Added caching of the desired size of a label.
This commit is contained in:
parent
9e577ab1aa
commit
36d620108c
21
ui/desiredsizecache.go
Normal file
21
ui/desiredsizecache.go
Normal 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
|
||||
}
|
14
ui/label.go
14
ui/label.go
@ -8,6 +8,8 @@ type Label struct {
|
||||
ControlBase
|
||||
|
||||
Text string
|
||||
|
||||
size desiredSizeCache
|
||||
}
|
||||
|
||||
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 {
|
||||
var fontName = l.FontName(ctx)
|
||||
var font = ctx.Renderer().Font(fontName)
|
||||
var width = font.WidthOf(l.Text)
|
||||
var height = font.Height()
|
||||
var pad = ctx.Style().Dimensions.TextPadding
|
||||
return geom.PtF32(width+pad*2, height+pad*2)
|
||||
return l.size.Update(ctx, fontName+l.Text, func(ctx Context) geom.PointF32 {
|
||||
var font = ctx.Renderer().Font(fontName)
|
||||
var width = font.WidthOf(l.Text)
|
||||
var height = font.Height()
|
||||
var pad = ctx.Style().Dimensions.TextPadding
|
||||
return geom.PtF32(width+pad*2, height+pad*2)
|
||||
})
|
||||
}
|
||||
|
||||
func (l *Label) Render(ctx Context) {
|
||||
|
Loading…
Reference in New Issue
Block a user