progrium/darwinkit

Request: Add NSTextField class

Closed this issue · 3 comments

Please add support for NSTextField. Thank you.

Why not just everything? This might be a good time to remind you that you can use any class or API without bindings using the objc package.

You are probably talking about using objc.NewClass(). I did make this example program as a start but for some reason it keeps panicking. Do you see anything wrong with it?

package main

import "github.com/progrium/macdriver/cocoa"
import "github.com/progrium/macdriver/objc"
import "fmt"

func main() {
	app := cocoa.NSApp()
	cls := objc.NewClass("NSTextField", "NSControl")
	cls.AddMethod("labelWithString:", func(message string) {
		fmt.Println("Message:", message)
	})
	objc.RegisterClass(cls)
	app.Run()
}

Here is the panic:
panic: unable to AllocateClassPair

I traced the error to objc/class.go to this code:

func NewClass(classname string, superclass string) Class {
	superClass := GetClass(superclass)

	ptr := allocateClassPair(unsafe.Pointer(superClass.Pointer()), classname)
	if ptr == nil {
		panic("unable to AllocateClassPair")
	}

	// Register the dealloc method to be able to properly remove the classInfo
	// reference to our internal pointer.
	classAddMethod(ptr, "dealloc", encVoid+encId+encSelector)

	lazilyRegisterClassInMap(classname)
	return object{ptr: uintptr(ptr)}
}

It looks like allocateClassPair() is the problem. For some reason it doesn't like NSTextField.

Subclassing is a whole aspect of this that's been developed even less, but is a big part of the Apple development model. You can kind of get away with using delegates instead in many cases. But actually I was just saying, outside of subclassing (which is started but doesn't always work as you can see), you can work with any API with objc.Get("NSTextField") and calling methods from there with Send. objc.Get("NSTextField").Send("Alloc").Send(...etc...) In other words, bindings here are mostly convenience.