2019-05-06 13:21:36 -04:00
|
|
|
package main
|
2019-05-07 14:48:06 -04:00
|
|
|
//go:generate nswrap
|
2019-05-06 13:21:36 -04:00
|
|
|
|
|
|
|
import (
|
2019-05-09 22:05:04 -04:00
|
|
|
"fmt"
|
2019-05-07 14:48:06 -04:00
|
|
|
"runtime"
|
2019-05-06 13:21:36 -04:00
|
|
|
"gitlab.wow.st/gmp/nswrap/examples/app/ns"
|
|
|
|
)
|
|
|
|
|
2019-05-21 16:26:52 -04:00
|
|
|
var nst = ns.NSStringWithGoString
|
|
|
|
|
2019-05-09 22:05:04 -04:00
|
|
|
func didFinishLaunching(n *ns.NSNotification) {
|
|
|
|
fmt.Println("Go: did finish launching")
|
|
|
|
}
|
|
|
|
|
2019-05-10 02:00:56 -04:00
|
|
|
func shouldTerminateAfterLastWindowClosed(s *ns.NSApplication) ns.BOOL {
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2019-05-09 22:05:04 -04:00
|
|
|
func willTerminate(n *ns.NSNotification) {
|
|
|
|
fmt.Println("Go: will terminate")
|
|
|
|
}
|
|
|
|
|
|
|
|
func didBecomeActive(n *ns.NSNotification) {
|
|
|
|
fmt.Println("Go: did become active")
|
2019-05-21 16:26:52 -04:00
|
|
|
vc := win.ContentViewController()
|
|
|
|
if vc == nil {
|
|
|
|
fmt.Println("vc == nil")
|
|
|
|
} else {
|
|
|
|
fmt.Println("vc is not nil")
|
|
|
|
}
|
2019-05-09 22:05:04 -04:00
|
|
|
}
|
|
|
|
|
2019-05-21 16:26:52 -04:00
|
|
|
var (
|
|
|
|
win *ns.NSWindow
|
|
|
|
)
|
|
|
|
|
2019-05-09 13:33:28 -04:00
|
|
|
func app() {
|
2019-05-07 14:48:06 -04:00
|
|
|
//Lock OS thread because Cocoa uses thread-local storage
|
|
|
|
runtime.LockOSThread()
|
2019-05-06 13:21:36 -04:00
|
|
|
a := ns.NSApplicationSharedApplication()
|
|
|
|
a.SetActivationPolicy(ns.NSApplicationActivationPolicyRegular)
|
2019-05-09 22:05:04 -04:00
|
|
|
|
|
|
|
//Set up an AppDelegate
|
|
|
|
del := ns.AppDelegateAlloc()
|
|
|
|
del.ApplicationDidFinishLaunchingCallback(didFinishLaunching)
|
2019-05-10 02:00:56 -04:00
|
|
|
del.ApplicationShouldTerminateAfterLastWindowClosedCallback(shouldTerminateAfterLastWindowClosed)
|
2019-05-09 22:05:04 -04:00
|
|
|
del.ApplicationWillTerminateCallback(willTerminate)
|
|
|
|
del.ApplicationDidBecomeActiveCallback(didBecomeActive)
|
|
|
|
a.SetDelegate(del)
|
|
|
|
|
|
|
|
//Set up an NSWindow
|
2019-05-21 16:26:52 -04:00
|
|
|
win = ns.NSWindowAlloc().InitWithContentRect(
|
2019-05-06 13:21:36 -04:00
|
|
|
ns.NSMakeRect(200,200,600,600),
|
2019-05-21 16:26:52 -04:00
|
|
|
ns.NSWindowStyleMaskTitled | ns.NSWindowStyleMaskClosable |
|
|
|
|
ns.NSWindowStyleMaskResizable,
|
2019-05-06 13:21:36 -04:00
|
|
|
ns.NSBackingStoreBuffered,
|
|
|
|
0,
|
|
|
|
nil,
|
|
|
|
)
|
2019-05-21 16:26:52 -04:00
|
|
|
win.SetTitle(nst("Hi World"))
|
|
|
|
win.MakeKeyAndOrderFront(win)
|
|
|
|
win.SetAlphaValue(0.85)
|
2019-05-09 22:05:04 -04:00
|
|
|
|
|
|
|
//Build a basic menu
|
2019-05-09 08:52:35 -04:00
|
|
|
m1 := ns.NSMenuAlloc().InitWithTitle(nst("Main"))
|
2019-05-07 14:48:06 -04:00
|
|
|
appItem := ns.NSMenuItemAlloc()
|
|
|
|
fileItem := ns.NSMenuItemAlloc()
|
|
|
|
m1.AddItem(appItem)
|
|
|
|
m1.AddItem(fileItem)
|
|
|
|
|
2019-05-09 08:52:35 -04:00
|
|
|
appMenu := ns.NSMenuAlloc().InitWithTitle(nst("App"))
|
|
|
|
fileMenu := ns.NSMenuAlloc().InitWithTitle(nst("File"))
|
2019-05-07 14:48:06 -04:00
|
|
|
m1.SetSubmenu(appMenu, appItem)
|
|
|
|
m1.SetSubmenu(fileMenu, fileItem)
|
|
|
|
|
2019-05-21 16:26:52 -04:00
|
|
|
appMenu.AddItemWithTitle(nst("About"), nil, nst(""))
|
|
|
|
appMenu.AddItemWithTitle(nst("Preferences"), nil, nst(""))
|
2019-05-09 08:52:35 -04:00
|
|
|
appMenu.AddItemWithTitle(nst("Quit"),ns.Selector("terminate:"), nst("q"))
|
2019-05-07 14:48:06 -04:00
|
|
|
a.SetMainMenu(m1)
|
2019-05-21 16:26:52 -04:00
|
|
|
fileMenu.AddItemWithTitle(nst("Open"), nil, nst(""))
|
|
|
|
fileMenu.AddItemWithTitle(nst("New"), nil, nst(""))
|
2019-05-07 14:48:06 -04:00
|
|
|
|
|
|
|
a.SetMainMenu(m1)
|
2019-05-07 15:49:43 -04:00
|
|
|
|
2019-05-09 22:05:04 -04:00
|
|
|
//Run the app
|
2019-05-06 13:21:36 -04:00
|
|
|
a.Run()
|
|
|
|
}
|
|
|
|
|
2019-05-07 14:48:06 -04:00
|
|
|
func main() {
|
2019-05-09 22:05:04 -04:00
|
|
|
//Run our app in an autorelease pool just for fun
|
2019-05-11 23:03:56 -04:00
|
|
|
go ns.Autoreleasepool(app)
|
2019-05-07 14:48:06 -04:00
|
|
|
select { }
|
|
|
|
}
|
|
|
|
|