window.go 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. package common
  2. import (
  3. "sync"
  4. "fyne.io/fyne/v2/internal/async"
  5. )
  6. // Window defines common functionality for windows.
  7. type Window struct {
  8. eventQueue *async.UnboundedFuncChan
  9. }
  10. // DestroyEventQueue destroys the event queue.
  11. func (w *Window) DestroyEventQueue() {
  12. w.eventQueue.Close()
  13. }
  14. // InitEventQueue initializes the event queue.
  15. func (w *Window) InitEventQueue() {
  16. // This channel should be closed when the window is closed.
  17. w.eventQueue = async.NewUnboundedFuncChan()
  18. }
  19. // QueueEvent uses this method to queue up a callback that handles an event. This ensures
  20. // user interaction events for a given window are processed in order.
  21. func (w *Window) QueueEvent(fn func()) {
  22. w.eventQueue.In() <- fn
  23. }
  24. // RunEventQueue runs the event queue. This should called inside a go routine.
  25. // This function blocks.
  26. func (w *Window) RunEventQueue() {
  27. for fn := range w.eventQueue.Out() {
  28. fn()
  29. }
  30. }
  31. // WaitForEvents wait for all the events.
  32. func (w *Window) WaitForEvents() {
  33. done := donePool.Get().(chan struct{})
  34. defer donePool.Put(done)
  35. w.eventQueue.In() <- func() { done <- struct{}{} }
  36. <-done
  37. }
  38. var donePool = sync.Pool{
  39. New: func() interface{} {
  40. return make(chan struct{})
  41. },
  42. }