summaryrefslogtreecommitdiff
path: root/uitask_darwin.go
blob: 79cd5c3a2cf286dc73af57328769d18743a55815 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
// 28 february 2014

package ui

import (
	"fmt"
	"runtime"
	"unsafe"
)

// #cgo LDFLAGS: -lobjc -framework Foundation -framework AppKit
// #include "objc_darwin.h"
// #include "delegateuitask_darwin.h"
import "C"

var uitask chan func()

func ui(main func()) error {
	runtime.LockOSThread()

	uitask = make(chan func())

	err := initCocoa()
	if err != nil {
		return err
	}

	// Cocoa must run on the first thread created by the program, so we run our dispatcher on another thread instead
	go func() {
		for f := range uitask {
			C.douitask(appDelegate, unsafe.Pointer(&f))
		}
	}()

	go func() {
		main()
		uitask <- func() {
			C.breakMainLoop()
		}
	}()

	C.cocoaMainLoop()
	return nil
}

// TODO move to init_darwin.go?

func initCocoa() (err error) {
	C.initBleh()		// initialize bleh_darwin.m functions
	makeAppDelegate()
	if C.initCocoa(appDelegate) != C.YES {
		return fmt.Errorf("error setting NSApplication activation policy (basically identifies our program as a separate program; needed for several things, such as Dock icon, application menu, window resizing, etc.) (unknown reason)")
	}
	return nil
}

//export appDelegate_uitask
func appDelegate_uitask(p unsafe.Pointer) {
	f := (*func ())(unsafe.Pointer(p))
	(*f)()
}