mirror of
https://git.sr.ht/~eliasnaur/gio
synced 2026-07-01 07:35:40 +00:00
cmd/gogio: start using layout.Flex in the e2e app
This vastly simplifies our code, and saves us the ugly math. While at it, establish that a TestDriver must have a white background, which is already satisfied by both existing implementations. Signed-off-by: Daniel Martí <mvdan@mvdan.cc>
This commit is contained in:
@@ -16,8 +16,11 @@ var headless = flag.Bool("headless", true, "run end-to-end tests in headless mod
|
||||
type TestDriver interface {
|
||||
// Start provides the test driver with a testing.T, as well as the path
|
||||
// to the Gio app to use for the test. The app will be run with the
|
||||
// given width and height. When the function returns, the gio app must
|
||||
// be ready to use on the platform.
|
||||
// given width and height, and the platform's background should be
|
||||
// white.
|
||||
//
|
||||
// When the function returns, the gio app must be ready to use on the
|
||||
// platform.
|
||||
//
|
||||
// The returned cleanup funcs must be run in reverse order, to mimic
|
||||
// deferred funcs.
|
||||
|
||||
Vendored
+30
-53
@@ -10,7 +10,7 @@ import (
|
||||
"gioui.org/app"
|
||||
"gioui.org/f32"
|
||||
"gioui.org/io/system"
|
||||
"gioui.org/op"
|
||||
"gioui.org/layout"
|
||||
"gioui.org/op/paint"
|
||||
)
|
||||
|
||||
@@ -30,64 +30,41 @@ func loop(w *app.Window) error {
|
||||
botLeft := color.RGBA{R: 0x00, G: 0x00, B: 0x00, A: 0xff}
|
||||
botRight := color.RGBA{R: 0x00, G: 0x00, B: 0x00, A: 0x80}
|
||||
|
||||
ops := new(op.Ops)
|
||||
gtx := &layout.Context{
|
||||
Queue: w.Queue(),
|
||||
}
|
||||
for {
|
||||
e := <-w.Events()
|
||||
switch e := e.(type) {
|
||||
case system.DestroyEvent:
|
||||
return e.Err
|
||||
case system.FrameEvent:
|
||||
ops.Reset()
|
||||
|
||||
paint.ColorOp{Color: topLeft}.Add(ops)
|
||||
paint.PaintOp{Rect: f32.Rectangle{
|
||||
Min: f32.Point{
|
||||
X: 0,
|
||||
Y: 0,
|
||||
},
|
||||
Max: f32.Point{
|
||||
X: float32(e.Size.X) / 2,
|
||||
Y: float32(e.Size.Y) / 2,
|
||||
},
|
||||
}}.Add(ops)
|
||||
|
||||
paint.ColorOp{Color: topRight}.Add(ops)
|
||||
paint.PaintOp{Rect: f32.Rectangle{
|
||||
Min: f32.Point{
|
||||
X: float32(e.Size.X) / 2,
|
||||
Y: 0,
|
||||
},
|
||||
Max: f32.Point{
|
||||
X: float32(e.Size.X),
|
||||
Y: float32(e.Size.Y) / 2,
|
||||
},
|
||||
}}.Add(ops)
|
||||
|
||||
paint.ColorOp{Color: botLeft}.Add(ops)
|
||||
paint.PaintOp{Rect: f32.Rectangle{
|
||||
Min: f32.Point{
|
||||
X: 0,
|
||||
Y: float32(e.Size.Y) / 2,
|
||||
},
|
||||
Max: f32.Point{
|
||||
X: float32(e.Size.X) / 2,
|
||||
Y: float32(e.Size.Y),
|
||||
},
|
||||
}}.Add(ops)
|
||||
|
||||
paint.ColorOp{Color: botRight}.Add(ops)
|
||||
paint.PaintOp{Rect: f32.Rectangle{
|
||||
Min: f32.Point{
|
||||
X: float32(e.Size.X) / 2,
|
||||
Y: float32(e.Size.Y) / 2,
|
||||
},
|
||||
Max: f32.Point{
|
||||
X: float32(e.Size.X),
|
||||
Y: float32(e.Size.Y),
|
||||
},
|
||||
}}.Add(ops)
|
||||
|
||||
e.Frame(ops)
|
||||
gtx.Reset(e.Config, e.Size)
|
||||
rows := layout.Flex{Axis: layout.Vertical}
|
||||
r1 := rows.Flex(gtx, 0.5, func() {
|
||||
columns := layout.Flex{Axis: layout.Horizontal}
|
||||
r1c1 := columns.Flex(gtx, 0.5, quarterWidget(gtx, topLeft))
|
||||
r1c2 := columns.Flex(gtx, 0.5, quarterWidget(gtx, topRight))
|
||||
columns.Layout(gtx, r1c1, r1c2)
|
||||
})
|
||||
r2 := rows.Flex(gtx, 0.5, func() {
|
||||
columns := layout.Flex{Axis: layout.Horizontal}
|
||||
r2c1 := columns.Flex(gtx, 0.5, quarterWidget(gtx, botLeft))
|
||||
r2c2 := columns.Flex(gtx, 0.5, quarterWidget(gtx, botRight))
|
||||
columns.Layout(gtx, r2c1, r2c2)
|
||||
})
|
||||
rows.Layout(gtx, r1, r2)
|
||||
e.Frame(gtx.Ops)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func quarterWidget(gtx *layout.Context, clr color.RGBA) func() {
|
||||
return func() {
|
||||
paint.ColorOp{Color: clr}.Add(gtx.Ops)
|
||||
paint.PaintOp{Rect: f32.Rectangle{Max: f32.Point{
|
||||
X: float32(gtx.Constraints.Width.Max),
|
||||
Y: float32(gtx.Constraints.Height.Max),
|
||||
}}}.Add(gtx.Ops)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -44,7 +44,9 @@ func (d *X11TestDriver) Start(t_ *testing.T, path string, width, height int) (cl
|
||||
display := fmt.Sprintf(":%d", rnd.Intn(100000)+1)
|
||||
|
||||
var xprog string
|
||||
xflags := []string{"-wr"}
|
||||
xflags := []string{
|
||||
"-wr", // we want a white background; the default is black
|
||||
}
|
||||
if *headless {
|
||||
xprog = "Xvfb" // virtual X server
|
||||
xflags = append(xflags, "-screen", "0", fmt.Sprintf("%dx%dx24", width, height))
|
||||
|
||||
Reference in New Issue
Block a user