From 2ffb579340590287683e80b268ad8a9483b7c5c5 Mon Sep 17 00:00:00 2001 From: Sander Schobers Date: Tue, 9 Apr 2019 20:32:42 +0200 Subject: [PATCH] Factored out (render) buffer. --- ui/buffer.go | 32 ++++++++++++++++++++++++++++++++ ui/overflow.go | 26 ++++++++------------------ 2 files changed, 40 insertions(+), 18 deletions(-) create mode 100644 ui/buffer.go diff --git a/ui/buffer.go b/ui/buffer.go new file mode 100644 index 0000000..634217a --- /dev/null +++ b/ui/buffer.go @@ -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) +} diff --git a/ui/overflow.go b/ui/overflow.go index ca76c79..046ad62 100644 --- a/ui/overflow.go +++ b/ui/overflow.go @@ -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()) - if err != nil { - panic(err) - } - o.buffer = buffer + err := o.content.Update(ctx, content.Size()) + if err != nil { + panic(err) } - - target := renderer.Target() - renderer.RenderTo(o.buffer) - renderer.Clear(color.Transparent) - o.Content.Render(ctx) - renderer.RenderTo(target) - renderer.DrawImage(o.buffer, o.bounds.Min) + o.content.Render(ctx, o.bounds.Min, func() { + renderer.Clear(color.Transparent) + o.Content.Render(ctx) + }) o.doOnVisibleBars(func(bar *Scrollbar) { bar.Render(ctx)