diff options
Diffstat (limited to 'layout')
| -rw-r--r-- | layout/doc.go (renamed from layout/layout.go) | 13 | ||||
| -rw-r--r-- | layout/draw.go (renamed from layout/subimage.go) | 10 | ||||
| -rw-r--r-- | layout/resize.go | 35 | ||||
| -rw-r--r-- | layout/resize_test.go | 58 |
4 files changed, 103 insertions, 13 deletions
diff --git a/layout/layout.go b/layout/doc.go index b04aa35..bae4673 100644 --- a/layout/layout.go +++ b/layout/doc.go @@ -13,16 +13,3 @@ 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/subimage.go b/layout/draw.go index ecbe931..760a149 100644 --- a/layout/subimage.go +++ b/layout/draw.go @@ -2,9 +2,11 @@ package layout import ( "image" + "image/color" "image/draw" ) +// subimager is implemented by most Image types. type subimager interface { SubImage(image.Rectangle) image.Image } @@ -15,3 +17,11 @@ type subimager interface { func subimage(m draw.Image, r image.Rectangle) draw.Image { return m.(subimager).SubImage(r).(draw.Image) } + +// drawBackground returns a draw call that fills the entire image with a color. +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/resize.go b/layout/resize.go new file mode 100644 index 0000000..1d3d6f1 --- /dev/null +++ b/layout/resize.go @@ -0,0 +1,35 @@ +package layout + +import "image" + +// ResizeAll resizes a layout to consume all of its parent's area. +func ResizeAll(r image.Rectangle) image.Rectangle { return r } + +// ResizeQuad1 resizes a layout to consume the top-right quadrant of its parent. +func ResizeQuad1(r image.Rectangle) image.Rectangle { + mid := midpoint(r) + return image.Rect(mid.X, r.Min.Y, r.Max.X, mid.Y) +} + +// ResizeQuad2 resizes a layout to consume the top-left quadrant of its parent. +func ResizeQuad2(r image.Rectangle) image.Rectangle { + mid := midpoint(r) + return image.Rectangle{r.Min, mid} +} + +// ResizeQuad3 resizes a layout to consume the bottom-left quadrant of its parent. +func ResizeQuad3(r image.Rectangle) image.Rectangle { + mid := midpoint(r) + return image.Rect(r.Min.X, mid.Y, mid.X, r.Max.Y) +} + +// ResizeQuad4 resizes a layout to consume the bottom-right quadrant of its parent. +func ResizeQuad4(r image.Rectangle) image.Rectangle { + mid := midpoint(r) + return image.Rectangle{mid, r.Max} +} + +// midpoint returns the point in the middle of a rectangle. +func midpoint(r image.Rectangle) image.Point { + return r.Min.Add(r.Max).Div(2) +} diff --git a/layout/resize_test.go b/layout/resize_test.go new file mode 100644 index 0000000..5be7a41 --- /dev/null +++ b/layout/resize_test.go @@ -0,0 +1,58 @@ +package layout_test + +import ( + "image" + "testing" + + "github.com/faiface/gui/layout" +) + +func TestResizeAll(t *testing.T) { + t.Parallel() + parent := image.Rect(111, 222, 333, 444) + child := layout.ResizeAll(parent) + want := parent + if child != want { + t.Errorf("got %v; want %v", child, want) + } +} + +func TestResizeQuad1(t *testing.T) { + t.Parallel() + parent := image.Rect(111, 222, 333, 444) + child := layout.ResizeQuad1(parent) + want := image.Rect(222, 222, 333, 333) + if child != want { + t.Errorf("got %v; want %v", child, want) + } +} + +func TestResizeQuad2(t *testing.T) { + t.Parallel() + parent := image.Rect(111, 222, 333, 444) + child := layout.ResizeQuad2(parent) + want := image.Rect(111, 222, 222, 333) + if child != want { + t.Errorf("got %v; want %v", child, want) + } +} + +func TestResizeQuad3(t *testing.T) { + t.Parallel() + parent := image.Rect(111, 222, 333, 444) + child := layout.ResizeQuad3(parent) + want := image.Rect(111, 333, 222, 444) + if child != want { + t.Errorf("got %v; want %v", child, want) + } +} + +func TestResizeQuad4(t *testing.T) { + t.Parallel() + parent := image.Rect(111, 222, 333, 444) + child := layout.ResizeQuad4(parent) + want := image.Rect(222, 333, 333, 444) + if child != want { + t.Errorf("got %v; want %v", child, want) + } +} |