| 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 | |
| 7 | // Small test app used by app/app_test.go. |
| 8 | package main |
| 9 | |
| 10 | import ( |
| 11 | "log" |
| 12 | "net" |
| 13 | |
| 14 | "golang.org/x/mobile/app" |
| 15 | "golang.org/x/mobile/app/internal/apptest" |
| 16 | "golang.org/x/mobile/event/lifecycle" |
| 17 | "golang.org/x/mobile/event/paint" |
| 18 | "golang.org/x/mobile/event/size" |
| 19 | "golang.org/x/mobile/event/touch" |
| 20 | "golang.org/x/mobile/gl" |
| 21 | ) |
| 22 | |
| 23 | func main() { |
| 24 | app.Main(func(a app.App) { |
| 25 | var ( |
| 26 | glctx gl.Context |
| 27 | visible bool |
| 28 | ) |
| 29 | |
| 30 | addr := "127.0.0.1:" + apptest.Port |
| 31 | log.Printf("addr: %s", addr) |
| 32 | |
| 33 | conn, err := net.Dial("tcp", addr) |
| 34 | if err != nil { |
| 35 | log.Fatal(err) |
| 36 | } |
| 37 | defer conn.Close() |
| 38 | log.Printf("dialled") |
| 39 | comm := &apptest.Comm{ |
| 40 | Conn: conn, |
| 41 | Fatalf: log.Panicf, |
| 42 | Printf: log.Printf, |
| 43 | } |
| 44 | |
| 45 | comm.Send("hello_from_testapp") |
| 46 | comm.Recv("hello_from_host") |
| 47 | |
| 48 | color := "red" |
| 49 | sendPainting := false |
| 50 | for e := range a.Events() { |
| 51 | switch e := a.Filter(e).(type) { |
| 52 | case lifecycle.Event: |
| 53 | switch e.Crosses(lifecycle.StageVisible) { |
| 54 | case lifecycle.CrossOn: |
| 55 | comm.Send("lifecycle_visible") |
| 56 | sendPainting = true |
| 57 | visible = true |
| 58 | glctx, _ = e.DrawContext.(gl.Context) |
| 59 | case lifecycle.CrossOff: |
| 60 | comm.Send("lifecycle_not_visible") |
| 61 | visible = false |
| 62 | } |
| 63 | case size.Event: |
| 64 | comm.Send("size", e.PixelsPerPt, e.Orientation) |
| 65 | case paint.Event: |
| 66 | if visible { |
| 67 | if color == "red" { |
| 68 | glctx.ClearColor(1, 0, 0, 1) |
| 69 | } else { |
| 70 | glctx.ClearColor(0, 1, 0, 1) |
| 71 | } |
| 72 | glctx.Clear(gl.COLOR_BUFFER_BIT) |
| 73 | a.Publish() |
| 74 | } |
| 75 | if sendPainting { |
| 76 | comm.Send("paint", color) |
| 77 | sendPainting = false |
| 78 | } |
| 79 | case touch.Event: |
| 80 | comm.Send("touch", e.Type, e.X, e.Y) |
| 81 | if e.Type == touch.TypeEnd { |
| 82 | if color == "red" { |
| 83 | color = "green" |
| 84 | } else { |
| 85 | color = "red" |
| 86 | } |
| 87 | sendPainting = true |
| 88 | // Send a paint event so the screen gets redrawn. |
| 89 | a.Send(paint.Event{}) |
| 90 | } |
| 91 | } |
| 92 | } |
| 93 | }) |
| 94 | } |