mirror of
https://git.sr.ht/~eliasnaur/gio
synced 2026-07-03 16:35:36 +00:00
cmd/gogio: add clicks to the e2e tests
The test app now responds to mouse clicks; clicking on one of the four sections of the app flips it to red color until clicked again. Add a Click method to the TestDriver interface, and implement it in both of the current drivers. Unfortunately, I failed at implementing it in X11 with the xdg library, after a few wasted hours. Instead, start relying on more external tools which are simple to use and not heavy to install. Signed-off-by: Daniel Martí <mvdan@mvdan.cc>
This commit is contained in:
Vendored
+51
-16
@@ -1,14 +1,16 @@
|
||||
// SPDX-License-Identifier: Unlicense OR MIT
|
||||
|
||||
// A dead simple app that just paints the background red.
|
||||
// A simple app used for gogio's end-to-end tests.
|
||||
package main
|
||||
|
||||
import (
|
||||
"image"
|
||||
"image/color"
|
||||
"log"
|
||||
|
||||
"gioui.org/app"
|
||||
"gioui.org/f32"
|
||||
"gioui.org/io/pointer"
|
||||
"gioui.org/io/system"
|
||||
"gioui.org/layout"
|
||||
"gioui.org/op/paint"
|
||||
@@ -25,10 +27,18 @@ func main() {
|
||||
}
|
||||
|
||||
func loop(w *app.Window) error {
|
||||
topLeft := color.RGBA{R: 0xde, G: 0xad, B: 0xbe, A: 0xff}
|
||||
topRight := color.RGBA{R: 0xff, G: 0xff, B: 0xff, A: 0xff}
|
||||
botLeft := color.RGBA{R: 0x00, G: 0x00, B: 0x00, A: 0xff}
|
||||
botRight := color.RGBA{R: 0x00, G: 0x00, B: 0x00, A: 0x80}
|
||||
topLeft := quarterWidget{
|
||||
color: color.RGBA{R: 0xde, G: 0xad, B: 0xbe, A: 0xff},
|
||||
}
|
||||
topRight := quarterWidget{
|
||||
color: color.RGBA{R: 0xff, G: 0xff, B: 0xff, A: 0xff},
|
||||
}
|
||||
botLeft := quarterWidget{
|
||||
color: color.RGBA{R: 0x00, G: 0x00, B: 0x00, A: 0xff},
|
||||
}
|
||||
botRight := quarterWidget{
|
||||
color: color.RGBA{R: 0x00, G: 0x00, B: 0x00, A: 0x80},
|
||||
}
|
||||
|
||||
gtx := &layout.Context{
|
||||
Queue: w.Queue(),
|
||||
@@ -39,32 +49,57 @@ func loop(w *app.Window) error {
|
||||
case system.DestroyEvent:
|
||||
return e.Err
|
||||
case system.FrameEvent:
|
||||
|
||||
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))
|
||||
r1c1 := columns.Flex(gtx, 0.5, func() { topLeft.Layout(gtx) })
|
||||
r1c2 := columns.Flex(gtx, 0.5, func() { topRight.Layout(gtx) })
|
||||
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))
|
||||
r2c1 := columns.Flex(gtx, 0.5, func() { botLeft.Layout(gtx) })
|
||||
r2c2 := columns.Flex(gtx, 0.5, func() { botRight.Layout(gtx) })
|
||||
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)
|
||||
// quarterWidget paints a quarter of the screen with one color. When clicked, it
|
||||
// turns red, going back to its normal color when clicked again.
|
||||
type quarterWidget struct {
|
||||
color color.RGBA
|
||||
|
||||
clicked bool
|
||||
}
|
||||
|
||||
var red = color.RGBA{R: 0xff, G: 0x00, B: 0x00, A: 0xff}
|
||||
|
||||
func (w *quarterWidget) Layout(gtx *layout.Context) {
|
||||
if w.clicked {
|
||||
paint.ColorOp{Color: red}.Add(gtx.Ops)
|
||||
} else {
|
||||
paint.ColorOp{Color: w.color}.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)
|
||||
|
||||
pointer.RectAreaOp{Rect: image.Rectangle{
|
||||
Max: image.Pt(gtx.Constraints.Width.Max, gtx.Constraints.Height.Max),
|
||||
}}.Add(gtx.Ops)
|
||||
pointer.InputOp{Key: w}.Add(gtx.Ops)
|
||||
|
||||
for _, e := range gtx.Events(w) {
|
||||
if e, ok := e.(pointer.Event); ok && e.Type == pointer.Press {
|
||||
w.clicked = !w.clicked
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user