Factored out (render) buffer.
This commit is contained in:
parent
c024416702
commit
2ffb579340
32
ui/buffer.go
Normal file
32
ui/buffer.go
Normal 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)
|
||||
}
|
@ -15,7 +15,7 @@ type overflow struct {
|
||||
desired geom.PointF32
|
||||
bounds geom.RectangleF32
|
||||
offset geom.PointF32
|
||||
buffer Image
|
||||
content Buffer
|
||||
|
||||
hor *Scrollbar
|
||||
ver *Scrollbar
|
||||
@ -124,24 +124,14 @@ func (o *overflow) Render(ctx Context) {
|
||||
|
||||
var content = o.Content.Bounds()
|
||||
content.Min = geom.ZeroPtF32
|
||||
if o.buffer == nil || o.buffer.Width() != content.Dx() || o.buffer.Height() != content.Dy() {
|
||||
if o.buffer != nil {
|
||||
o.buffer.Destroy()
|
||||
o.buffer = nil
|
||||
}
|
||||
buffer, err := renderer.CreateImageSize(content.Dx(), content.Dy())
|
||||
err := o.content.Update(ctx, content.Size())
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
o.buffer = buffer
|
||||
}
|
||||
|
||||
target := renderer.Target()
|
||||
renderer.RenderTo(o.buffer)
|
||||
o.content.Render(ctx, o.bounds.Min, func() {
|
||||
renderer.Clear(color.Transparent)
|
||||
o.Content.Render(ctx)
|
||||
renderer.RenderTo(target)
|
||||
renderer.DrawImage(o.buffer, o.bounds.Min)
|
||||
})
|
||||
|
||||
o.doOnVisibleBars(func(bar *Scrollbar) {
|
||||
bar.Render(ctx)
|
||||
|
Loading…
Reference in New Issue
Block a user