Change Go types to plain structs instead of pointers to structs.

Add methods for protocols that Objective-C interfaces claim to
implement. Separate class methods from instance methods in
internal data structures. Add layout constraints with visual
formats to examples/app. Fix typedefs for wrapped objective-c
object types.
This commit is contained in:
gmp
2019-05-23 16:31:47 -04:00
parent 4f6930002d
commit a616761475
10 changed files with 355 additions and 220 deletions
+69 -41
View File
@@ -7,57 +7,24 @@ import (
"gitlab.wow.st/gmp/nswrap/examples/app/ns"
)
//Shortcut for literal NSStrings
var nst = ns.NSStringWithGoString
func didFinishLaunching(n *ns.NSNotification) {
func didFinishLaunching(n ns.NSNotification) {
fmt.Println("Go: did finish launching")
}
func shouldTerminateAfterLastWindowClosed(s *ns.NSApplication) ns.BOOL {
return 1
}
func willTerminate(n *ns.NSNotification) {
fmt.Println("Go: will terminate")
}
func didBecomeActive(n *ns.NSNotification) {
fmt.Println("Go: did become active")
vc := win.ContentViewController()
if vc == nil {
fmt.Println("vc == nil")
} else {
fmt.Println("vc is not nil")
}
}
var (
win *ns.NSWindow
)
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.ApplicationShouldTerminateAfterLastWindowClosedCallback(shouldTerminateAfterLastWindowClosed)
del.ApplicationWillTerminateCallback(willTerminate)
del.ApplicationDidBecomeActiveCallback(didBecomeActive)
a.SetDelegate(del)
fmt.Printf("Notification: %s\n",n.Name().UTF8String())
//Set up an NSWindow
win = ns.NSWindowAlloc().InitWithContentRect(
ns.NSMakeRect(200,200,600,600),
ns.NSWindowStyleMaskTitled | ns.NSWindowStyleMaskClosable |
ns.NSWindowStyleMaskResizable,
ns.NSWindowStyleMaskResizable,
ns.NSBackingStoreBuffered,
0,
nil,
ns.NSScreen{},
)
// retain win since we called Alloc and did not add it to a collection
win.Retain()
win.SetTitle(nst("Hi World"))
win.MakeKeyAndOrderFront(win)
win.SetAlphaValue(0.85)
@@ -83,6 +50,67 @@ func app() {
a.SetMainMenu(m1)
//add some buttons and do some layout
//don't do this:
//b := ns.NSButtonAlloc().InitWithFrame(ns.NSMakeRect(100,100,100,50))
b1 := ns.NSButtonWithTitle(nst("PUSH"),ns.Id{},ns.Selector(""))
b2 := ns.NSButtonWithTitle(nst("QUIT"),ns.Id{},ns.Selector("terminate:"))
b1.SetTranslatesAutoresizingMaskIntoConstraints(0)
b2.SetTranslatesAutoresizingMaskIntoConstraints(0)
cv := win.ContentView()
cv.AddSubview(b1.NSView,ns.NSWindowAbove,ns.NSView{})
cv.AddSubview(b2.NSView,ns.NSWindowAbove,ns.NSView{})
viewmap := ns.NSDictionaryWithObjects(
ns.NSArrayWithObjects(b1,b2),
ns.NSArrayWithObjects(nst("b1"),nst("b2")))
cv.AddConstraints(ns.NSLayoutConstraintsWithVisualFormat(
nst("V:|-[b1]"),0, ns.NSDictionary{}, viewmap))
cv.AddConstraints(ns.NSLayoutConstraintsWithVisualFormat(
nst("H:|-[b1]"),0, ns.NSDictionary{}, viewmap))
cv.AddConstraints(ns.NSLayoutConstraintsWithVisualFormat(
nst("H:[b1]-[b2]"),ns.NSLayoutFormatAlignAllBaseline,
ns.NSDictionary{}, viewmap))
}
func shouldTerminateAfterLastWindowClosed(s ns.NSApplication) ns.BOOL {
return 1
}
func willTerminate(n ns.NSNotification) {
fmt.Println("Go: will terminate")
}
func didBecomeActive(n ns.NSNotification) {
fmt.Println("Go: did become active")
fmt.Printf("Notification: %s\n",n.Name().UTF8String())
}
var (
a ns.NSApplication
win ns.NSWindow
)
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.ApplicationShouldTerminateAfterLastWindowClosedCallback(shouldTerminateAfterLastWindowClosed)
del.ApplicationWillTerminateCallback(willTerminate)
del.ApplicationDidBecomeActiveCallback(didBecomeActive)
a.SetDelegate(del)
//Run the app
a.Run()
}
+4 -11
View File
@@ -1,3 +1,4 @@
# nswrap.yaml
inputfiles:
- /System/Library/Frameworks/Foundation.framework/Headers/Foundation.h
- /System/Library/Frameworks/AppKit.framework/Headers/AppKit.h
@@ -17,17 +18,17 @@ classes:
- NSLayoutConstraint
- NSDictionary
- NSArray
- NSColor
- NSObject
functions: [NSMake.*]
enums:
- CF.*
- NSApplication.*
- NSBackingStore.*
- NSWindowStyleMask.*
- NSWindowButton
- NSWindowOrderingMode
- NSLayout.*
delegates:
AppDelegate:
@@ -36,15 +37,7 @@ delegates:
- applicationDidFinishLaunching
- applicationShouldTerminateAfterLastWindowClosed
- applicationDidBecomeActive
subclasses:
ViewController:
NSViewController:
- viewDidLoad
- viewDidDisappear
- loadView
frameworks: [ Foundation, AppKit, CoreGraphics ]
frameworks: [ Foundation, AppKit ]
pragma: [ clang diagnostic ignored "-Wformat-security" ]
vaargs: 32
+22 -23
View File
@@ -8,7 +8,7 @@ import (
"gitlab.wow.st/gmp/nswrap/examples/bluetooth/ns"
)
func updateState(c *ns.CBCentralManager) {
func updateState(c ns.CBCentralManager) {
fmt.Printf("Go: did update state\n")
switch cm.CBManager.State() {
case ns.CBManagerStateUnknown:
@@ -23,44 +23,44 @@ func updateState(c *ns.CBCentralManager) {
fmt.Printf(" powered off\n")
case ns.CBManagerStatePoweredOn:
fmt.Printf(" powered on\n")
cm.ScanForPeripheralsWithServices(ns.NSArrayWithObjects(hrm_uuid),nil)
cm.ScanForPeripheralsWithServices(ns.NSArrayWithObjects(hrm_uuid),ns.NSDictionary{})
}
}
func discoverPeripheral(c *ns.CBCentralManager, p *ns.CBPeripheral, d *ns.NSDictionary, rssi *ns.NSNumber) {
func discoverPeripheral(c ns.CBCentralManager, p ns.CBPeripheral, d ns.NSDictionary, rssi ns.NSNumber) {
fmt.Printf("Did discover peripheral\n")
c.StopScan()
if peripheral != nil {
if peripheral.Ptr() != nil {
peripheral.Release()
}
peripheral = p
peripheral.Retain()
c.ConnectPeripheral(peripheral,nil)
c.ConnectPeripheral(peripheral,ns.NSDictionary{})
}
func connectPeripheral(c *ns.CBCentralManager, p *ns.CBPeripheral) {
func connectPeripheral(c ns.CBCentralManager, p ns.CBPeripheral) {
fmt.Printf("Did connect peripheral\n")
p.SetDelegate(cd)
p.DiscoverServices(nil)
p.DiscoverServices(ns.NSArray{})
}
func discoverServices(p *ns.CBPeripheral, e *ns.NSError) {
func discoverServices(p ns.CBPeripheral, e ns.NSError) {
fmt.Printf("Did discover services\n")
p.Services().ObjectEnumerator().ForIn(func(o *ns.Id) bool {
p.Services().ObjectEnumerator().ForIn(func(o ns.Id) bool {
serv := o.CBService()
switch {
case serv.UUID().IsEqualTo(hrm_uuid):
fmt.Printf("--heart rate monitor service\n")
p.DiscoverCharacteristics(nil,serv)
p.DiscoverCharacteristics(ns.NSArray{},serv)
case serv.UUID().IsEqualTo(ns.CBUUIDWithGoString("180A")):
fmt.Printf("--device information service\n")
p.DiscoverCharacteristics(nil,serv)
p.DiscoverCharacteristics(ns.NSArray{},serv)
}
return true
})
}
func hr(d *ns.NSData) int {
func hr(d ns.NSData) int {
if l := int(d.Length()); l < 4 {
return 0
}
@@ -73,11 +73,11 @@ func hr(d *ns.NSData) int {
}
}
func discoverCharacteristics(p *ns.CBPeripheral, s *ns.CBService, e *ns.NSError) {
func discoverCharacteristics(p ns.CBPeripheral, s ns.CBService, e ns.NSError) {
fmt.Printf("Did discover characteristics\n")
fmt.Printf("----%s\n",s.UUID().UUIDString().UTF8String())
if s.UUID().IsEqualTo(hrm_uuid) {
s.Characteristics().ObjectEnumerator().ForIn(func(o *ns.Id) bool {
s.Characteristics().ObjectEnumerator().ForIn(func(o ns.Id) bool {
chr := o.CBCharacteristic()
fmt.Printf("------%s\n",chr.UUID().UUIDString().UTF8String())
if chr.UUID().IsEqualTo(hrv_uuid) {
@@ -90,7 +90,7 @@ func discoverCharacteristics(p *ns.CBPeripheral, s *ns.CBService, e *ns.NSError)
}
}
func updateValue(p *ns.CBPeripheral, chr *ns.CBCharacteristic, e *ns.NSError) {
func updateValue(p ns.CBPeripheral, chr ns.CBCharacteristic, e ns.NSError) {
if chr.UUID().IsEqualTo(hrv_uuid) {
v := chr.Value()
fmt.Printf("Heart rate: %d\n",hr(v))
@@ -98,17 +98,17 @@ func updateValue(p *ns.CBPeripheral, chr *ns.CBCharacteristic, e *ns.NSError) {
}
var (
hrm_uuid *ns.CBUUID
hrv_uuid *ns.CBUUID
cd *ns.BleDelegate
cm *ns.CBCentralManager
peripheral *ns.CBPeripheral
hrm_uuid ns.CBUUID
hrv_uuid ns.CBUUID
cd ns.CBDelegate
cm ns.CBCentralManager
peripheral ns.CBPeripheral
)
func main() {
queue := ns.DispatchQueueCreate(ns.CharWithGoString("st.wow.gitlab.ble"),nil)
cd = ns.BleDelegateAlloc()
cd = ns.CBDelegateAlloc()
cd.CentralManagerDidUpdateStateCallback(updateState)
cd.CentralManagerDidDiscoverPeripheralCallback(discoverPeripheral)
@@ -120,8 +120,7 @@ func main() {
hrm_uuid = ns.CBUUIDWithGoString("180D")
hrv_uuid = ns.CBUUIDWithGoString("2A37")
cm = ns.CBCentralManagerAlloc()
cm.InitWithDelegate(cd,queue,nil)
cm = ns.CBCentralManagerAlloc().InitWithDelegate(cd,queue,ns.NSDictionary{})
select { }
}
+1 -1
View File
@@ -27,7 +27,7 @@ functions: [ NSMakeRange, dispatch_queue_create ]
enums: [ CB.* ]
frameworks: [ Foundation, CoreBluetooth ]
delegates:
BleDelegate:
CBDelegate:
CBCentralManagerDelegate:
- centralManagerDidUpdateState
- centralManagerDidDiscoverPeripheral
+28 -2
View File
@@ -27,9 +27,35 @@ func main() {
a2 := a.SubarrayWithRange(ns.NSMakeRange(1,3))
fmt.Println("Length(a2) = ",a2.Count())
i1 := a.ObjectAtIndex(1).NSString()
fmt.Println(i1.UTF8String())
a.ObjectEnumerator().ForIn(func(o *ns.Id) bool {
fmt.Printf("i1 = %@\n",i1)
fmt.Printf("i1.Ptr() = %p\n",i1.Ptr())
a.ObjectEnumerator().ForIn(func(o ns.Id) bool {
fmt.Println(o.NSString().UTF8String())
return true
})
s1 := ns.NSSetWithObjects(n1,n2)
a = ns.NSMutableArrayWithObjects(n1,s1)
a.ObjectEnumerator().ForIn(func(o ns.Id) bool {
fmt.Printf("%s -- ",o.ClassName().UTF8String())
switch {
case o.IsKindOfClass(ns.NSStringClass()):
fmt.Printf("It's a string\n")
case o.IsKindOfClass(ns.NSSetClass()):
fmt.Printf("It's a set\n")
default:
fmt.Printf("I don't know what it is!\n")
}
return true
})
a2 = ns.NSArrayWithObjects(n1,n2,n3,s1)
a2.ObjectEnumerator().ForIn(func (o ns.Id) bool {
switch {
case o.IsKindOfClass(ns.NSStringClass()):
fmt.Println(o.NSString().UTF8String())
return true // continue enumeration
default:
fmt.Println("Unknown class")
return false // terminate enumeration
}
})
}
+1
View File
@@ -15,6 +15,7 @@ classes:
- NSString
- NSScanner
- NSFileManager
- NSObject
functions: [ NSMakeRange ]
enums: [ P_ALL, CF.* ]
frameworks: [ Foundation ]
+1 -1
View File
@@ -6,6 +6,6 @@ classes:
subclasses:
ClassThree:
ClassTwo:
- hi.*
- .*
imports: [ simple.h ]
frameworks: [ Foundation ]