Skip to content

Commit c93457a

Browse files
committed
application: Add RunOnMainLoop
1 parent 7c5d188 commit c93457a

File tree

3 files changed

+24
-1
lines changed

3 files changed

+24
-1
lines changed

application.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ package gocoa
44
// #cgo LDFLAGS: -framework Cocoa
55
// #include "application.h"
66
import "C"
7+
import "runtime/cgo"
78

89
var appDidFinishLaunchingFunc func()
910

@@ -34,3 +35,16 @@ func callOnApplicationDidFinishLaunchingHandler() {
3435
appDidFinishLaunchingFunc()
3536
}
3637
}
38+
39+
//export go_callback
40+
func go_callback(h C.uintptr_t) {
41+
hnd := cgo.Handle(h)
42+
fn := hnd.Value().(func())
43+
fn()
44+
hnd.Delete()
45+
}
46+
47+
func RunOnMainLoop(fn func()) {
48+
h := cgo.NewHandle(fn)
49+
C.RunOnMainLoop(C.uintptr_t(h))
50+
}

application.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
#include <stdint.h>
12
#import <Cocoa/Cocoa.h>
23

34
void InitSharedApplication();
45
void RunApplication();
5-
void TerminateApplication();
6+
void TerminateApplication();
7+
void RunOnMainLoop(uintptr_t h);

application.m

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,11 @@ void RunApplication() {
3131

3232
void TerminateApplication() {
3333
[NSApp terminate:nil];
34+
}
35+
36+
extern void go_callback(uintptr_t h);
37+
void RunOnMainLoop(uintptr_t h) {
38+
dispatch_async(dispatch_get_main_queue(), ^{
39+
go_callback(h);
40+
});
3441
}

0 commit comments

Comments
 (0)