diff options
Diffstat (limited to 'layout')
| -rw-r--r-- | layout/layout.go (renamed from layout/doc.go) | 13 | ||||
| -rw-r--r-- | layout/options.go | 34 | ||||
| -rw-r--r-- | layout/region.go | 13 |
3 files changed, 56 insertions, 4 deletions
diff --git a/layout/doc.go b/layout/layout.go index bae4673..b04aa35 100644 --- a/layout/doc.go +++ b/layout/layout.go @@ -13,3 +13,16 @@ Draw calls from the children are intercepted and translated onto their respective areas before being forwarded to the parent Env. */ package layout + +import ( + "image" + "image/color" + "image/draw" +) + +func drawBackground(c color.Color) func(draw.Image) image.Rectangle { + return func(img draw.Image) image.Rectangle { + draw.Draw(img, img.Bounds(), &image.Uniform{c}, image.ZP, draw.Src) + return img.Bounds() + } +} diff --git a/layout/options.go b/layout/options.go new file mode 100644 index 0000000..2ea0c81 --- /dev/null +++ b/layout/options.go @@ -0,0 +1,34 @@ +package layout + +import ( + "image/color" +) + +var ( + defaultBg = color.Transparent +) + +// Option is a function option for layout constructors. +type Option func(*options) + +type options struct { + bg color.Color // background color +} + +// Background sets the background color of a layout. +func Background(bg color.Color) Option { + return func(o *options) { + o.bg = bg + } +} + +// EvalOptions evaluates a list of option functions, returning the final set. +func evalOptions(o ...Option) options { + opts := options{ + bg: defaultBg, + } + for i := range o { + o[i](&opts) + } + return opts +} diff --git a/layout/region.go b/layout/region.go index 1e92455..4e3a791 100644 --- a/layout/region.go +++ b/layout/region.go @@ -15,7 +15,9 @@ type Region struct { // NewRegion creates a region layout that occupies part of the parent env's area, as determined by the resize function. // Resize takes the area of the parent and returns the area of the region. -func NewRegion(env gui.Env, resize func(image.Rectangle) image.Rectangle) gui.Env { +func NewRegion(env gui.Env, resize func(image.Rectangle) image.Rectangle, o ...Option) gui.Env { + opts := evalOptions(o...) + events := make(chan gui.Event) // to child drw := make(chan func(draw.Image) image.Rectangle) // from child @@ -25,17 +27,20 @@ func NewRegion(env gui.Env, resize func(image.Rectangle) image.Rectangle) gui.En area := resize(event.(gui.Resize).Rectangle) // first event guaranteed to be Resize events <- gui.Resize{area} + env.Draw() <- drawBackground(opts.bg) + for { select { - case event := <-env.Events(): + case event := <-env.Events(): // event from parent switch event := event.(type) { case gui.Resize: + env.Draw() <- drawBackground(opts.bg) area = resize(event.Rectangle) - events <- gui.Resize{area} + events <- gui.Resize{area} // forward to child default: events <- event } - case f, ok := <-drw: + case f, ok := <-drw: // draw call from child if !ok { close(events) close(env.Draw()) |