From bb36ce61bc7cb0be2f2c8b9bfade801761440330 Mon Sep 17 00:00:00 2001 From: Lea Anthony Date: Wed, 14 Dec 2022 07:52:10 +1100 Subject: [PATCH] Refactor dispatching --- exp/pkg/application/mainthread.go | 36 ++++++------------------ exp/pkg/application/mainthread_darwin.go | 19 ++++++++++++- exp/pkg/application/systemtray_darwin.go | 4 +-- exp/pkg/application/window_darwin.go | 12 ++++---- 4 files changed, 35 insertions(+), 36 deletions(-) diff --git a/exp/pkg/application/mainthread.go b/exp/pkg/application/mainthread.go index 16068c39b..8c62a8672 100644 --- a/exp/pkg/application/mainthread.go +++ b/exp/pkg/application/mainthread.go @@ -1,22 +1,16 @@ -//go:build darwin - package application -/* -extern void dispatch(unsigned int id); -*/ -import "C" import ( "sync" ) -var mainThreadFuntionStore = make(map[uint]func()) -var mainThreadFuntionStoreLock sync.RWMutex +var mainThreadFunctionStore = make(map[uint]func()) +var mainThreadFunctionStoreLock sync.RWMutex func generateFunctionStoreID() uint { startID := 0 for { - if _, ok := mainThreadFuntionStore[uint(startID)]; !ok { + if _, ok := mainThreadFunctionStore[uint(startID)]; !ok { return uint(startID) } startID++ @@ -26,23 +20,11 @@ func generateFunctionStoreID() uint { } } -func Dispatch(fn func()) { - mainThreadFuntionStoreLock.Lock() +func DispatchOnMainThread(fn func()) { + mainThreadFunctionStoreLock.Lock() id := generateFunctionStoreID() - mainThreadFuntionStore[id] = fn - mainThreadFuntionStoreLock.Unlock() - C.dispatch(C.uint(id)) -} - -//export dispatchCallback -func dispatchCallback(callbackID C.uint) { - mainThreadFuntionStoreLock.RLock() - id := uint(callbackID) - fn := mainThreadFuntionStore[id] - if fn == nil { - Fatal("dispatchCallback called with invalid id: ", id) - } - delete(mainThreadFuntionStore, id) - mainThreadFuntionStoreLock.RUnlock() - fn() + mainThreadFunctionStore[id] = fn + mainThreadFunctionStoreLock.Unlock() + // Call platform specific dispatch function + platformDispatch(id) } diff --git a/exp/pkg/application/mainthread_darwin.go b/exp/pkg/application/mainthread_darwin.go index 4654bfa6c..5a026d1ca 100644 --- a/exp/pkg/application/mainthread_darwin.go +++ b/exp/pkg/application/mainthread_darwin.go @@ -10,7 +10,7 @@ package application extern void dispatchCallback(unsigned int); -void dispatch(unsigned int id) { +static void dispatch(unsigned int id) { dispatch_async(dispatch_get_main_queue(), ^{ dispatchCallback(id); }); @@ -18,3 +18,20 @@ void dispatch(unsigned int id) { */ import "C" + +func platformDispatch(id uint) { + C.dispatch(C.uint(id)) +} + +//export dispatchCallback +func dispatchCallback(callbackID C.uint) { + mainThreadFunctionStoreLock.RLock() + id := uint(callbackID) + fn := mainThreadFunctionStore[id] + if fn == nil { + Fatal("dispatchCallback called with invalid id: ", id) + } + delete(mainThreadFunctionStore, id) + mainThreadFunctionStoreLock.RUnlock() + fn() +} diff --git a/exp/pkg/application/systemtray_darwin.go b/exp/pkg/application/systemtray_darwin.go index 6f99e0044..c04ae2b2b 100644 --- a/exp/pkg/application/systemtray_darwin.go +++ b/exp/pkg/application/systemtray_darwin.go @@ -86,7 +86,7 @@ func (s *macosSystemTray) setMenu(menu *Menu) { } func (s *macosSystemTray) run() { - Dispatch(func() { + DispatchOnMainThread(func() { if s.nsStatusItem != nil { Fatal("System tray '%d' already running", s.id) } @@ -110,7 +110,7 @@ func (s *macosSystemTray) run() { func (s *macosSystemTray) setIcon(icon []byte) { s.icon = icon - Dispatch(func() { + DispatchOnMainThread(func() { s.nsImage = unsafe.Pointer(C.imageFromBytes((*C.uchar)(&icon[0]), C.int(len(icon)))) C.systemTraySetIcon(s.nsStatusItem, s.nsImage, C.int(s.iconPosition)) }) diff --git a/exp/pkg/application/window_darwin.go b/exp/pkg/application/window_darwin.go index 616622fda..4f7b21cf7 100644 --- a/exp/pkg/application/window_darwin.go +++ b/exp/pkg/application/window_darwin.go @@ -493,7 +493,7 @@ func (w *macosWindow) syncMainThreadReturningBool(fn func() bool) bool { var wg sync.WaitGroup wg.Add(1) var result bool - Dispatch(func() { + DispatchOnMainThread(func() { result = fn() wg.Done() }) @@ -569,7 +569,7 @@ func (w *macosWindow) size() (int, int) { var width, height C.int var wg sync.WaitGroup wg.Add(1) - Dispatch(func() { + DispatchOnMainThread(func() { C.windowGetSize(w.nsWindow, &width, &height) wg.Done() }) @@ -581,7 +581,7 @@ func (w *macosWindow) width() int { var width C.int var wg sync.WaitGroup wg.Add(1) - Dispatch(func() { + DispatchOnMainThread(func() { width = C.windowGetWidth(w.nsWindow) wg.Done() }) @@ -592,7 +592,7 @@ func (w *macosWindow) height() int { var height C.int var wg sync.WaitGroup wg.Add(1) - Dispatch(func() { + DispatchOnMainThread(func() { height = C.windowGetHeight(w.nsWindow) wg.Done() }) @@ -601,7 +601,7 @@ func (w *macosWindow) height() int { } func (w *macosWindow) run() { - Dispatch(func() { + DispatchOnMainThread(func() { w.nsWindow = C.windowNew(C.uint(w.id), C.int(w.options.Width), C.int(w.options.Height)) w.setTitle(w.options.Title) w.setAlwaysOnTop(w.options.AlwaysOnTop) @@ -668,7 +668,7 @@ func (w *macosWindow) position() (int, int) { var x, y C.int var wg sync.WaitGroup wg.Add(1) - go Dispatch(func() { + go DispatchOnMainThread(func() { C.windowGetPosition(w.nsWindow, &x, &y) wg.Done() })