aboutsummaryrefslogtreecommitdiffstats
path: root/test/rows/main.go
blob: 29b73449a780b391e4950c23cf661842c48a26af (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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package main

import (
	"image"
	"image/color"
	"image/draw"

	"github.com/faiface/gui"
	"github.com/faiface/gui/layout"
	"github.com/faiface/gui/win"
	"github.com/faiface/mainthread"
)

const (
	nrows     = 16
	rowHeight = 12
	rowWidth  = 128
)

var bgclr = gui.HexToColor("#ffffea") // background color

func main() {
	mainthread.Run(run)
}

func run() {
	w, err := win.New(win.Title("Grid Layout Test"), win.Resizable())
	if err != nil {
		panic(err)
	}

	mux, env := gui.NewMux(w)
	bg := layout.NewRegion(mux.MakeEnv(), bgclr, layout.ResizeAll)
	rows := layout.NewRows(bg, nrows)
	for i, row := range rows {
		go colorBlock(row, image.Pt(rowWidth, rowHeight), color.RGBA{uint8(i * 256 / 4), 0x20, 0x20, 0xFF})
	}

	for event := range env.Events() {
		switch event.(type) {
		case win.WiClose:
			close(env.Draw())
			return
		}
	}
}

func colorBlock(env gui.Env, size image.Point, c color.Color) {
	redraw := func(img draw.Image) image.Rectangle {
		r := image.Rectangle{img.Bounds().Min, img.Bounds().Min.Add(size)}
		draw.Draw(img, r, &image.Uniform{c}, image.ZP, draw.Src)
		return r
	}
	for event := range env.Events() {
		switch event.(type) {
		case gui.Resize:
			env.Draw() <- redraw
		}
	}
	close(env.Draw())
}