mirror of
https://git.sr.ht/~eliasnaur/gio
synced 2026-07-03 08:25:34 +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:
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)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user