shiny.go 1.6 KB

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