shiny.go 1.6 KB

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