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-09 22:05:04 -04:00
|
|
|
func didFinishLaunching(n *ns.NSNotification) {
|
|
|
|
fmt.Println("Go: did finish launching")
|
|
|
|
}
|
|
|
|
|
|
|
|
func shouldTerminate(s *ns.NSApplication) ns.NSApplicationTerminateReply {
|
|
|
|
fmt.Println("Go: should terminate")
|
|
|
|
return ns.NSTerminateNow
|
|
|
|
}
|
|
|
|
|
|
|
|
func willTerminate(n *ns.NSNotification) {
|
|
|
|
fmt.Println("Go: will terminate")
|
|
|
|
}
|
|
|
|
|
|
|
|
func didBecomeActive(n *ns.NSNotification) {
|
|
|
|
fmt.Println("Go: did become active")
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
del.ApplicationShouldTerminateCallback(shouldTerminate)
|
|
|
|
del.ApplicationWillTerminateCallback(willTerminate)
|
|
|
|
del.ApplicationDidBecomeActiveCallback(didBecomeActive)
|
|
|
|
a.SetDelegate(del)
|
|
|
|
|
|
|
|
//Set up an NSWindow
|
2019-05-06 13:21:36 -04:00
|
|
|
w := ns.NSWindowAlloc().InitWithContentRect(
|
|
|
|
ns.NSMakeRect(200,200,600,600),
|
|
|
|
ns.NSWindowStyleMaskTitled,
|
|
|
|
ns.NSBackingStoreBuffered,
|
|
|
|
0,
|
|
|
|
nil,
|
|
|
|
)
|
2019-05-09 08:52:35 -04:00
|
|
|
nst := ns.NSStringWithGoString
|
|
|
|
w.SetTitle(nst("Hi World"))
|
2019-05-06 13:21:36 -04:00
|
|
|
w.MakeKeyAndOrderFront(w)
|
2019-05-07 14:48:06 -04:00
|
|
|
w.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)
|
|
|
|
|
|
|
|
s := ns.NSStringWithGoString("")
|
2019-05-09 08:52:35 -04:00
|
|
|
appMenu.AddItemWithTitle(nst("About"), nil, s)
|
|
|
|
appMenu.AddItemWithTitle(nst("Preferences"), nil, s)
|
|
|
|
appMenu.AddItemWithTitle(nst("Quit"),ns.Selector("terminate:"), nst("q"))
|
2019-05-07 14:48:06 -04:00
|
|
|
a.SetMainMenu(m1)
|
2019-05-09 08:52:35 -04:00
|
|
|
fileMenu.AddItemWithTitle(nst("Open"), nil, s)
|
|
|
|
fileMenu.AddItemWithTitle(nst("New"), nil, s)
|
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
|
|
|
//Add a random button that does nothing
|
2019-05-09 08:52:35 -04:00
|
|
|
b1 := ns.NSButtonWithTitle(nst("push"),s,nil)
|
2019-05-09 22:05:04 -04:00
|
|
|
b1.Id.NSView().SetFrame(ns.NSMakeRect(0,550,100,50))
|
2019-05-07 15:49:43 -04:00
|
|
|
w.ContentView().AddSubview(&b1.NSView,ns.NSWindowAbove,nil)
|
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-09 13:33:28 -04:00
|
|
|
go ns.Autorelease(app)
|
2019-05-07 14:48:06 -04:00
|
|
|
select { }
|
|
|
|
}
|
|
|
|
|