Factored out (render) buffer.

This commit is contained in:
Sander Schobers 2019-04-09 20:32:42 +02:00
parent c024416702
commit 2ffb579340
2 changed files with 40 additions and 18 deletions

32
ui/buffer.go Normal file
View File

@ -0,0 +1,32 @@
package ui
import "opslag.de/schobers/geom"
type Buffer struct {
im Image
}
func (b *Buffer) Update(ctx Context, size geom.PointF32) error {
if b.im != nil {
if b.im.Width() == size.X && b.im.Height() == size.Y {
return nil
}
b.im.Destroy()
b.im = nil
}
im, err := ctx.Renderer().CreateImageSize(size.X, size.Y)
if err != nil {
return err
}
b.im = im
return nil
}
func (b *Buffer) Render(ctx Context, pos geom.PointF32, render func()) {
renderer := ctx.Renderer()
currTarget := renderer.Target()
renderer.RenderTo(b.im)
render()
renderer.RenderTo(currTarget)
renderer.DrawImage(b.im, pos)
}

View File

@ -15,7 +15,7 @@ type overflow struct {
desired geom.PointF32 desired geom.PointF32
bounds geom.RectangleF32 bounds geom.RectangleF32
offset geom.PointF32 offset geom.PointF32
buffer Image content Buffer
hor *Scrollbar hor *Scrollbar
ver *Scrollbar ver *Scrollbar
@ -124,24 +124,14 @@ func (o *overflow) Render(ctx Context) {
var content = o.Content.Bounds() var content = o.Content.Bounds()
content.Min = geom.ZeroPtF32 content.Min = geom.ZeroPtF32
if o.buffer == nil || o.buffer.Width() != content.Dx() || o.buffer.Height() != content.Dy() { err := o.content.Update(ctx, content.Size())
if o.buffer != nil { if err != nil {
o.buffer.Destroy() panic(err)
o.buffer = nil
}
buffer, err := renderer.CreateImageSize(content.Dx(), content.Dy())
if err != nil {
panic(err)
}
o.buffer = buffer
} }
o.content.Render(ctx, o.bounds.Min, func() {
target := renderer.Target() renderer.Clear(color.Transparent)
renderer.RenderTo(o.buffer) o.Content.Render(ctx)
renderer.Clear(color.Transparent) })
o.Content.Render(ctx)
renderer.RenderTo(target)
renderer.DrawImage(o.buffer, o.bounds.Min)
o.doOnVisibleBars(func(bar *Scrollbar) { o.doOnVisibleBars(func(bar *Scrollbar) {
bar.Render(ctx) bar.Render(ctx)