Debian Codegraph @

app/shiny.go

raw
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 windows
6
7 package app
8
9 import (
10 "log"
11
12 "golang.org/x/exp/shiny/driver/gldriver"
13 "golang.org/x/exp/shiny/screen"
14 "golang.org/x/mobile/event/lifecycle"
15 "golang.org/x/mobile/event/mouse"
16 "golang.org/x/mobile/event/touch"
17 "golang.org/x/mobile/gl"
18 )
19
20 func main(f func(a App)) {
21 gldriver.Main(func(s screen.Screen) {
22 w, err := s.NewWindow(nil)
23 if err != nil {
24 log.Fatal(err)
25 }
26 defer w.Release()
27
28 theApp.glctx = nil
29 theApp.worker = nil // handled by shiny
30
31 go func() {
32 for range theApp.publish {
33 res := w.Publish()
34 theApp.publishResult <- PublishResult{
35 BackBufferPreserved: res.BackBufferPreserved,
36 }
37 }
38 }()
39
40 donec := make(chan struct{})
41 go func() {
42 // close the donec channel in a defer statement
43 // so that we could still be able to return even
44 // if f panics.
45 defer close(donec)
46
47 f(theApp)
48 }()
49
50 for {
51 select {
52 case <-donec:
53 return
54 default:
55 theApp.Send(convertEvent(w.NextEvent()))
56 }
57 }
58 })
59 }
60
61 func convertEvent(e interface{}) interface{} {
62 switch e := e.(type) {
63 case lifecycle.Event:
64 if theApp.glctx == nil {
65 theApp.glctx = e.DrawContext.(gl.Context)
66 }
67 case mouse.Event:
68 te := touch.Event{
69 X: e.X,
70 Y: e.Y,
71 }
72 switch e.Direction {
73 case mouse.DirNone:
74 te.Type = touch.TypeMove
75 case mouse.DirPress:
76 te.Type = touch.TypeBegin
77 case mouse.DirRelease:
78 te.Type = touch.TypeEnd
79 }
80 return te
81 }
82 return e
83 }