github.com/coming-chat/gomobile@v0.0.0-20220601074111-56995f7d7aba/app/internal/testapp/testapp.go (about) 1 // Copyright 2015 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 //go:build darwin || linux 6 // +build darwin linux 7 8 // Small test app used by app/app_test.go. 9 package main 10 11 import ( 12 "log" 13 "net" 14 15 "golang.org/x/mobile/app" 16 "golang.org/x/mobile/app/internal/apptest" 17 "golang.org/x/mobile/event/lifecycle" 18 "golang.org/x/mobile/event/paint" 19 "golang.org/x/mobile/event/size" 20 "golang.org/x/mobile/event/touch" 21 "golang.org/x/mobile/gl" 22 ) 23 24 func main() { 25 app.Main(func(a app.App) { 26 var ( 27 glctx gl.Context 28 visible bool 29 ) 30 31 addr := "127.0.0.1:" + apptest.Port 32 log.Printf("addr: %s", addr) 33 34 conn, err := net.Dial("tcp", addr) 35 if err != nil { 36 log.Fatal(err) 37 } 38 defer conn.Close() 39 log.Printf("dialled") 40 comm := &apptest.Comm{ 41 Conn: conn, 42 Fatalf: log.Panicf, 43 Printf: log.Printf, 44 } 45 46 comm.Send("hello_from_testapp") 47 comm.Recv("hello_from_host") 48 49 color := "red" 50 sendPainting := false 51 for e := range a.Events() { 52 switch e := a.Filter(e).(type) { 53 case lifecycle.Event: 54 switch e.Crosses(lifecycle.StageVisible) { 55 case lifecycle.CrossOn: 56 comm.Send("lifecycle_visible") 57 sendPainting = true 58 visible = true 59 glctx, _ = e.DrawContext.(gl.Context) 60 case lifecycle.CrossOff: 61 comm.Send("lifecycle_not_visible") 62 visible = false 63 } 64 case size.Event: 65 comm.Send("size", e.PixelsPerPt, e.Orientation) 66 case paint.Event: 67 if visible { 68 if color == "red" { 69 glctx.ClearColor(1, 0, 0, 1) 70 } else { 71 glctx.ClearColor(0, 1, 0, 1) 72 } 73 glctx.Clear(gl.COLOR_BUFFER_BIT) 74 a.Publish() 75 } 76 if sendPainting { 77 comm.Send("paint", color) 78 sendPainting = false 79 } 80 case touch.Event: 81 comm.Send("touch", e.Type, e.X, e.Y) 82 if e.Type == touch.TypeEnd { 83 if color == "red" { 84 color = "green" 85 } else { 86 color = "red" 87 } 88 sendPainting = true 89 // Send a paint event so the screen gets redrawn. 90 a.Send(paint.Event{}) 91 } 92 } 93 } 94 }) 95 }