Refactor mainthread dispatch

This commit is contained in:
Lea Anthony 2022-12-24 15:35:04 +11:00
commit f240f2100c
No known key found for this signature in database
GPG key ID: 33DAF7BB90A58405
6 changed files with 27 additions and 26 deletions

View file

@ -49,6 +49,7 @@ type platformApp interface {
showAboutDialog(name string, description string, icon []byte)
setIcon(icon []byte)
on(id uint)
dispatchOnMainThread(id uint)
}
// Messages sent from javascript get routed here
@ -367,3 +368,12 @@ func (a *App) Clipboard() *Clipboard {
}
return a.clipboard
}
func (a *App) dispatchOnMainThread(fn func()) {
mainThreadFunctionStoreLock.Lock()
id := generateFunctionStoreID()
mainThreadFunctionStore[id] = fn
mainThreadFunctionStoreLock.Unlock()
// Call platform specific dispatch function
a.impl.dispatchOnMainThread(id)
}

View file

@ -314,7 +314,7 @@ type macosDialog struct {
}
func (m *macosDialog) show() {
DispatchOnMainThread(func() {
globalApplication.dispatchOnMainThread(func() {
// Mac can only have 4 buttons on a dialog
if len(m.dialog.buttons) > 4 {

View file

@ -19,12 +19,3 @@ func generateFunctionStoreID() uint {
}
}
}
func DispatchOnMainThread(fn func()) {
mainThreadFunctionStoreLock.Lock()
id := generateFunctionStoreID()
mainThreadFunctionStore[id] = fn
mainThreadFunctionStoreLock.Unlock()
// Call platform specific dispatch function
platformDispatch(id)
}

View file

@ -8,23 +8,23 @@ package application
#include "Cocoa/Cocoa.h"
extern void dispatchCallback(unsigned int);
extern void dispatchOnMainThreadCallback(unsigned int);
static void dispatch(unsigned int id) {
static void dispatchOnMainThread(unsigned int id) {
dispatch_async(dispatch_get_main_queue(), ^{
dispatchCallback(id);
dispatchOnMainThreadCallback(id);
});
}
*/
import "C"
func platformDispatch(id uint) {
C.dispatch(C.uint(id))
func (m *macosApp) dispatchOnMainThread(id uint) {
C.dispatchOnMainThread(C.uint(id))
}
//export dispatchCallback
func dispatchCallback(callbackID C.uint) {
//export dispatchOnMainThreadCallback
func dispatchOnMainThreadCallback(callbackID C.uint) {
mainThreadFunctionStoreLock.RLock()
id := uint(callbackID)
fn := mainThreadFunctionStore[id]

View file

@ -100,7 +100,7 @@ func (s *macosSystemTray) setMenu(menu *Menu) {
}
func (s *macosSystemTray) run() {
DispatchOnMainThread(func() {
globalApplication.dispatchOnMainThread(func() {
if s.nsStatusItem != nil {
Fatal("System tray '%d' already running", s.id)
}
@ -124,7 +124,7 @@ func (s *macosSystemTray) run() {
func (s *macosSystemTray) setIcon(icon []byte) {
s.icon = icon
DispatchOnMainThread(func() {
globalApplication.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), C.bool(s.isTemplateIcon))
})
@ -133,7 +133,7 @@ func (s *macosSystemTray) setIcon(icon []byte) {
func (s *macosSystemTray) setTemplateIcon(icon []byte) {
s.icon = icon
s.isTemplateIcon = true
DispatchOnMainThread(func() {
globalApplication.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), C.bool(s.isTemplateIcon))
})

View file

@ -736,7 +736,7 @@ func (w *macosWindow) syncMainThreadReturningBool(fn func() bool) bool {
var wg sync.WaitGroup
wg.Add(1)
var result bool
DispatchOnMainThread(func() {
globalApplication.dispatchOnMainThread(func() {
result = fn()
wg.Done()
})
@ -812,7 +812,7 @@ func (w *macosWindow) size() (int, int) {
var width, height C.int
var wg sync.WaitGroup
wg.Add(1)
DispatchOnMainThread(func() {
globalApplication.dispatchOnMainThread(func() {
C.windowGetSize(w.nsWindow, &width, &height)
wg.Done()
})
@ -828,7 +828,7 @@ func (w *macosWindow) width() int {
var width C.int
var wg sync.WaitGroup
wg.Add(1)
DispatchOnMainThread(func() {
globalApplication.dispatchOnMainThread(func() {
width = C.windowGetWidth(w.nsWindow)
wg.Done()
})
@ -839,7 +839,7 @@ func (w *macosWindow) height() int {
var height C.int
var wg sync.WaitGroup
wg.Add(1)
DispatchOnMainThread(func() {
globalApplication.dispatchOnMainThread(func() {
height = C.windowGetHeight(w.nsWindow)
wg.Done()
})
@ -851,7 +851,7 @@ func (w *macosWindow) run() {
for eventId := range w.parent.eventListeners {
w.on(eventId)
}
DispatchOnMainThread(func() {
globalApplication.dispatchOnMainThread(func() {
w.nsWindow = C.windowNew(C.uint(w.parent.id), C.int(w.parent.options.Width), C.int(w.parent.options.Height))
w.setTitle(w.parent.options.Title)
w.setAlwaysOnTop(w.parent.options.AlwaysOnTop)
@ -938,7 +938,7 @@ func (w *macosWindow) position() (int, int) {
var x, y C.int
var wg sync.WaitGroup
wg.Add(1)
go DispatchOnMainThread(func() {
go globalApplication.dispatchOnMainThread(func() {
C.windowGetPosition(w.nsWindow, &x, &y)
wg.Done()
})