2014-02-27 19:48:32 -06:00
// 27 february 2014
package main
import (
"fmt"
2014-02-28 14:08:07 -06:00
"time"
2014-02-27 19:48:32 -06:00
)
2014-02-27 20:53:44 -06:00
// #cgo LDFLAGS: -lobjc -framework Foundation -framework AppKit
2014-02-27 19:48:32 -06:00
// #include <stdlib.h>
2014-02-28 17:30:06 -06:00
// #include "objc_darwin.h"
2014-02-27 19:48:32 -06:00
import "C"
2014-02-28 19:37:11 -06:00
var (
_NSApplication = objc_getClass ( "NSApplication" )
2014-02-27 19:56:08 -06:00
2014-02-28 19:37:11 -06:00
_sharedApplication = sel_getUid ( "sharedApplication" )
_run = sel_getUid ( "run" )
)
2014-02-27 19:56:08 -06:00
2014-02-28 10:06:20 -06:00
var NSApp C . id
2014-02-28 14:08:07 -06:00
var delegate C . id
2014-02-28 16:20:22 -06:00
var notesel C . SEL
2014-02-28 10:06:20 -06:00
func init ( ) {
// need an NSApplication first - see https://github.com/TooTallNate/NodObjC/issues/21
2014-02-28 19:37:11 -06:00
NSApp = C . objc_msgSend_noargs ( _NSApplication , _sharedApplication )
2014-02-28 10:06:20 -06:00
2014-02-28 10:55:35 -06:00
selW := sel_getUid ( "windowShouldClose:" )
selB := sel_getUid ( "buttonClicked:" )
2014-02-28 14:08:07 -06:00
selN := sel_getUid ( "gotNotification:" )
mk ( "hello" , selW , selB , selN )
2014-02-28 19:37:11 -06:00
delegate = objc_alloc ( objc_getClass ( "hello" ) )
2014-02-28 14:08:07 -06:00
2014-02-28 16:20:22 -06:00
notesel = selN
2014-02-27 22:07:45 -06:00
}
2014-02-27 20:53:44 -06:00
const (
NSBorderlessWindowMask = 0
NSTitledWindowMask = 1 << 0
NSClosableWindowMask = 1 << 1
NSMiniaturizableWindowMask = 1 << 2
NSResizableWindowMask = 1 << 3
NSTexturedBackgroundWindowMask = 1 << 8
)
const (
// NSBackingStoreRetained = 0 // "You should not use this mode."
// NSBackingStoreNonretained = 1 // "You should not use this mode."
NSBackingStoreBuffered = 2
)
2014-02-28 11:09:15 -06:00
const (
NSRoundedBezelStyle = 1
2014-02-28 19:37:11 -06:00
// TODO copy in the rest?
2014-02-28 11:09:15 -06:00
)
2014-02-28 19:37:11 -06:00
var (
_NSAutoreleasePool = objc_getClass ( "NSAutoreleasePool" )
2014-02-27 20:53:44 -06:00
2014-02-28 19:37:11 -06:00
_performSelectorOnMainThread =
sel_getUid ( "performSelectorOnMainThread:withObject:waitUntilDone:" )
)
2014-02-28 14:08:07 -06:00
2014-02-28 19:37:11 -06:00
func notify ( source string ) {
2014-02-28 16:32:55 -06:00
// we need to make an NSAutoreleasePool, otherwise we get leak warnings on stderr
2014-02-28 19:37:11 -06:00
pool := objc_new ( _NSAutoreleasePool )
src := toNSString ( source )
2014-02-28 16:20:22 -06:00
C . objc_msgSend_sel_id_bool (
delegate ,
2014-02-28 19:37:11 -06:00
_performSelectorOnMainThread ,
2014-02-28 16:20:22 -06:00
notesel ,
2014-02-28 14:08:07 -06:00
src ,
2014-02-28 18:34:18 -06:00
C . BOOL ( C . YES ) ) // wait so we can properly drain the autorelease pool; on other platforms we wind up waiting anyway (since the main thread can only handle one thing at a time) so
2014-02-28 19:37:11 -06:00
objc_release ( pool )
2014-02-28 14:08:07 -06:00
}
2014-02-28 19:37:11 -06:00
var (
_NSWindow = objc_getClass ( "NSWindow" )
_NSButton = objc_getClass ( "NSButton" )
_initWithContentRect = sel_getUid ( "initWithContentRect:styleMask:backing:defer:" )
_setDelegate = sel_getUid ( "setDelegate:" )
_makeKeyAndOrderFront = sel_getUid ( "makeKeyAndOrderFront:" )
_contentView = sel_getUid ( "contentView" )
_initWithFrame = sel_getUid ( "initWithFrame:" )
_setTarget = sel_getUid ( "setTarget:" )
_setAction = sel_getUid ( "setAction:" )
_setBezelStyle = sel_getUid ( "setBezelStyle:" )
_addSubview = sel_getUid ( "addSubview:" )
)
2014-02-27 20:53:44 -06:00
2014-02-28 19:37:11 -06:00
func main ( ) {
2014-02-28 18:34:18 -06:00
style := uintptr ( NSTitledWindowMask | NSClosableWindowMask )
backing := uintptr ( NSBackingStoreBuffered )
2014-02-27 20:53:44 -06:00
deferx := C . BOOL ( C . YES )
2014-02-28 19:37:11 -06:00
window := objc_alloc ( _NSWindow )
window = objc_msgSend_rect_uint_uint_bool ( window ,
_initWithContentRect ,
2014-02-28 18:34:18 -06:00
100 , 100 , 320 , 240 ,
style , backing , deferx )
2014-02-28 19:37:11 -06:00
C . objc_msgSend_id ( window , _makeKeyAndOrderFront , window )
C . objc_msgSend_id ( window , _setDelegate , delegate )
windowView := C . objc_msgSend_noargs ( window , _contentView )
button := objc_alloc ( _NSButton )
2014-02-28 18:34:18 -06:00
button = objc_msgSend_rect ( button ,
2014-02-28 19:37:11 -06:00
_initWithFrame ,
2014-02-28 18:34:18 -06:00
20 , 20 , 200 , 200 )
2014-02-28 19:37:11 -06:00
C . objc_msgSend_id ( button , _setTarget , delegate )
2014-02-28 10:55:35 -06:00
C . objc_msgSend_sel ( button ,
2014-02-28 19:37:11 -06:00
_setAction ,
2014-02-28 10:55:35 -06:00
sel_getUid ( "buttonClicked:" ) )
2014-02-28 19:37:11 -06:00
objc_msgSend_uint ( button , _setBezelStyle , NSRoundedBezelStyle )
C . objc_msgSend_id ( windowView , _addSubview , button )
2014-02-28 10:55:35 -06:00
2014-02-28 14:08:07 -06:00
go func ( ) {
for {
<- time . After ( 5 * time . Second )
fmt . Println ( "five seconds passed; sending notification..." )
notify ( "timer" )
}
} ( )
2014-02-28 19:37:11 -06:00
C . objc_msgSend_noargs ( NSApp , _run )
2014-02-27 19:48:32 -06:00
}