aboutsummaryrefslogtreecommitdiffstats
path: root/intercepter.go
blob: 95df3c64525250ea185a374bdbc91e52dfb244da (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
package gui

import (
	"image"
	"image/draw"
)

// Intercepter represents an element that can interact with Envs.
// An Intercepter can modify Events, stop them or emit arbitrary ones.
// It can also put itself in the draw pipeline, for throttling very
// expensive draw calls for example.
type Intercepter interface {
	Intercept(Env) Env
}

// RedrawIntercepter is a basic Intercepter, it is meant for use in simple Layouts
// that only need to redraw themselves.
type RedrawIntercepter struct {
	Redraw func(draw.Image, image.Rectangle)
}

func (ri RedrawIntercepter) Intercept(parent Env) Env {
	return newEnv(parent,
		func(e Event, c chan<- Event) {
			c <- e
			if resize, ok := e.(Resize); ok {
				parent.Draw() <- func(drw draw.Image) image.Rectangle {
					ri.Redraw(drw, resize.Rectangle)
					return resize.Rectangle
				}
			}
		},
		send, // forward draw functions un-modified
		func() {})
}