Skip to content

Commit

Permalink
application: Add RunOnMainLoop
Browse files Browse the repository at this point in the history
  • Loading branch information
roblillack committed May 14, 2024
1 parent 7c5d188 commit c93457a
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 1 deletion.
14 changes: 14 additions & 0 deletions application.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package gocoa
// #cgo LDFLAGS: -framework Cocoa
// #include "application.h"
import "C"
import "runtime/cgo"

var appDidFinishLaunchingFunc func()

Expand Down Expand Up @@ -34,3 +35,16 @@ func callOnApplicationDidFinishLaunchingHandler() {
appDidFinishLaunchingFunc()
}
}

//export go_callback
func go_callback(h C.uintptr_t) {
hnd := cgo.Handle(h)
fn := hnd.Value().(func())
fn()
hnd.Delete()
}

func RunOnMainLoop(fn func()) {
h := cgo.NewHandle(fn)
C.RunOnMainLoop(C.uintptr_t(h))
}
4 changes: 3 additions & 1 deletion application.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#include <stdint.h>
#import <Cocoa/Cocoa.h>

void InitSharedApplication();
void RunApplication();
void TerminateApplication();
void TerminateApplication();
void RunOnMainLoop(uintptr_t h);
7 changes: 7 additions & 0 deletions application.m
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,11 @@ void RunApplication() {

void TerminateApplication() {
[NSApp terminate:nil];
}

extern void go_callback(uintptr_t h);
void RunOnMainLoop(uintptr_t h) {
dispatch_async(dispatch_get_main_queue(), ^{
go_callback(h);
});
}

0 comments on commit c93457a

Please sign in to comment.