Complete implementation of delegates. Update app example with an

NSApplicationDelegate.
This commit is contained in:
gmp
2019-05-09 22:05:04 -04:00
parent 0d2321d516
commit 44b3b75e23
3 changed files with 321 additions and 80 deletions
+35 -1
View File
@@ -2,15 +2,43 @@ package main
//go:generate nswrap
import (
"fmt"
"runtime"
"gitlab.wow.st/gmp/nswrap/examples/app/ns"
)
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")
}
func app() {
//Lock OS thread because Cocoa uses thread-local storage
runtime.LockOSThread()
a := ns.NSApplicationSharedApplication()
a.SetActivationPolicy(ns.NSApplicationActivationPolicyRegular)
//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
w := ns.NSWindowAlloc().InitWithContentRect(
ns.NSMakeRect(200,200,600,600),
ns.NSWindowStyleMaskTitled,
@@ -22,6 +50,8 @@ func app() {
w.SetTitle(nst("Hi World"))
w.MakeKeyAndOrderFront(w)
w.SetAlphaValue(0.85)
//Build a basic menu
m1 := ns.NSMenuAlloc().InitWithTitle(nst("Main"))
appItem := ns.NSMenuItemAlloc()
fileItem := ns.NSMenuItemAlloc()
@@ -43,13 +73,17 @@ func app() {
a.SetMainMenu(m1)
//Add a random button that does nothing
b1 := ns.NSButtonWithTitle(nst("push"),s,nil)
b1.SetFrame(ns.NSMakeRect(0,550,100,50))
b1.Id.NSView().SetFrame(ns.NSMakeRect(0,550,100,50))
w.ContentView().AddSubview(&b1.NSView,ns.NSWindowAbove,nil)
//Run the app
a.Run()
}
func main() {
//Run our app in an autorelease pool just for fun
go ns.Autorelease(app)
select { }
}