2014-02-28 22:01:48 -06:00
// 28 february 2014
2014-03-12 20:55:45 -05:00
2014-02-28 22:01:48 -06:00
package ui
import (
2014-03-01 19:31:17 -06:00
"fmt"
2014-02-28 22:01:48 -06:00
"runtime"
"unsafe"
)
// #cgo LDFLAGS: -lobjc -framework Foundation -framework AppKit
// #include "objc_darwin.h"
import "C"
var uitask chan func ( )
2014-03-01 14:18:29 -06:00
func ui ( main func ( ) ) error {
2014-02-28 22:01:48 -06:00
runtime . LockOSThread ( )
uitask = make ( chan func ( ) )
2014-03-01 14:18:29 -06:00
2014-05-13 07:14:28 -05:00
err := initCocoa ( )
2014-03-01 14:18:29 -06:00
if err != nil {
return err
2014-02-28 22:01:48 -06:00
}
2014-03-01 14:18:29 -06:00
// 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 {
2014-05-13 07:14:28 -05:00
C . douitask ( appDelegate , unsafe . Pointer ( & f ) )
2014-03-01 14:18:29 -06:00
}
} ( )
2014-03-05 19:09:15 -06:00
go func ( ) {
main ( )
uitask <- func ( ) {
2014-05-13 07:14:28 -05:00
C . breakMainLoop ( )
2014-03-05 19:09:15 -06:00
}
} ( )
2014-03-01 14:18:29 -06:00
2014-05-13 07:14:28 -05:00
C . cocoaMainLoop ( )
2014-03-01 14:18:29 -06:00
return nil
2014-02-28 22:01:48 -06:00
}
2014-05-13 07:14:28 -05:00
func initCocoa ( ) ( err error ) {
2014-05-13 08:40:19 -05:00
makeAppDelegate ( )
2014-05-13 07:14:28 -05:00
if C . initCocoa ( appDelegate ) != C . YES {
2014-05-13 08:40:19 -05:00
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)" )
2014-05-13 07:14:28 -05:00
}
2014-05-13 08:40:19 -05:00
return nil
2014-02-28 22:01:48 -06:00
}
//export appDelegate_uitask
2014-05-13 06:56:37 -05:00
func appDelegate_uitask ( p unsafe . Pointer ) {
2014-02-28 22:01:48 -06:00
f := ( * func ( ) ) ( unsafe . Pointer ( p ) )
( * f ) ( )
}