event.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. package app
  2. import "reflect"
  3. // EventHandler represents a function that can handle HTML events. They are
  4. // always called on the UI goroutine.
  5. type EventHandler func(ctx Context, e Event)
  6. // Event is the interface that describes a javascript event.
  7. type Event struct {
  8. Value
  9. }
  10. // PreventDefault cancels the event if it is cancelable. The default action that
  11. // belongs to the event will not occur.
  12. func (e Event) PreventDefault() {
  13. e.Call("preventDefault")
  14. }
  15. type eventHandlers map[string]eventHandler
  16. func (h eventHandlers) Set(event string, eh EventHandler, scope ...any) {
  17. if eh != nil {
  18. h[event] = makeEventHandler(event, eh, scope...)
  19. }
  20. }
  21. func (h eventHandlers) Mount(src UI) {
  22. for event, eh := range h {
  23. h[event] = eh.Mount(src)
  24. }
  25. }
  26. func (h eventHandlers) Update(src UI, v eventHandlers) {
  27. for event, eh := range h {
  28. if _, ok := v[event]; !ok {
  29. eh.Dismount()
  30. delete(h, event)
  31. }
  32. }
  33. for event, eh := range v {
  34. if h[event].Equal(eh) {
  35. continue
  36. }
  37. h[event].Dismount()
  38. h[event] = eh.Mount(src)
  39. }
  40. }
  41. type eventHandler struct {
  42. event string
  43. scope string
  44. goHandler EventHandler
  45. jsHandler Func
  46. close func()
  47. }
  48. func makeEventHandler(event string, h EventHandler, scope ...any) eventHandler {
  49. return eventHandler{
  50. event: event,
  51. scope: toPath(scope...),
  52. goHandler: h,
  53. }
  54. }
  55. func (h eventHandler) Equal(v eventHandler) bool {
  56. return h.event == v.event &&
  57. h.scope == v.scope &&
  58. reflect.ValueOf(h.goHandler).Pointer() == reflect.ValueOf(v.goHandler).Pointer()
  59. }
  60. func (h eventHandler) Mount(src UI) eventHandler {
  61. jsHandler := makeJSEventHandler(src, h.goHandler)
  62. src.JSValue().addEventListener(h.event, jsHandler)
  63. close := func() {
  64. src.JSValue().removeEventListener(h.event, jsHandler)
  65. jsHandler.Release()
  66. }
  67. h.jsHandler = jsHandler
  68. h.close = close
  69. return h
  70. }
  71. func (h eventHandler) Dismount() {
  72. if h.close != nil {
  73. h.close()
  74. }
  75. }
  76. func makeJSEventHandler(src UI, h EventHandler) Func {
  77. return FuncOf(func(this Value, args []Value) any {
  78. src.getDispatcher().Emit(src, func() {
  79. event := Event{
  80. Value: args[0],
  81. }
  82. trackMousePosition(event)
  83. h(makeContext(src), event)
  84. })
  85. return nil
  86. })
  87. }
  88. func trackMousePosition(e Event) {
  89. x := e.Get("clientX")
  90. if !x.Truthy() {
  91. return
  92. }
  93. y := e.Get("clientY")
  94. if !y.Truthy() {
  95. return
  96. }
  97. Window().setCursorPosition(x.Int(), y.Int())
  98. }