From 8f28d1044fb83153fde4505421b75072930d6fb9 Mon Sep 17 00:00:00 2001 From: Sam Anthony Date: Mon, 9 Feb 2026 22:00:33 -0500 Subject: layout: add background color option --- layout/doc.go | 15 --------------- layout/layout.go | 28 ++++++++++++++++++++++++++++ layout/options.go | 34 ++++++++++++++++++++++++++++++++++ layout/region.go | 13 +++++++++---- 4 files changed, 71 insertions(+), 19 deletions(-) delete mode 100644 layout/doc.go create mode 100644 layout/layout.go create mode 100644 layout/options.go (limited to 'layout') diff --git a/layout/doc.go b/layout/doc.go deleted file mode 100644 index bae4673..0000000 --- a/layout/doc.go +++ /dev/null @@ -1,15 +0,0 @@ -/* -Package layout provides means of partitioning screen space. - -A layout exists in a parent Env, and has one or more child Envs. It -acts as a multiplexer for the children. The parent Env may be a window, -another layout, or whatever... Several layers of layouts can be composed. - -A layout allocates screen area to its children by intercepting Resize -events from the parent Env. Upon reception by the layout, a Resize event -is transformed for each child, and forwarded to them. - -Draw calls from the children are intercepted and translated onto their -respective areas before being forwarded to the parent Env. -*/ -package layout diff --git a/layout/layout.go b/layout/layout.go new file mode 100644 index 0000000..b04aa35 --- /dev/null +++ b/layout/layout.go @@ -0,0 +1,28 @@ +/* +Package layout provides means of partitioning screen space. + +A layout exists in a parent Env, and has one or more child Envs. It +acts as a multiplexer for the children. The parent Env may be a window, +another layout, or whatever... Several layers of layouts can be composed. + +A layout allocates screen area to its children by intercepting Resize +events from the parent Env. Upon reception by the layout, a Resize event +is transformed for each child, and forwarded to them. + +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()) -- cgit v1.2.3