progrium/darwinkit

Getting the sender of an event

programmingkidx opened this issue · 0 comments

In Objective-C the action method to a control like a button has a sender argument that lets the method know who sent the event. Is something like this possible in MacDriver? I made a program that display buttons in a grid and I want to print out the name of the button that generated an event. Here is the program:

package main

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

func main() {

	myclass := objc.NewClass("MyClass", "NSObject")
	myclass.AddMethod("doButton:", doButton)
	objc.RegisterClass(myclass)
	myobj := objc.Get("MyClass").Alloc()
	
	
	app := cocoa.NSApp_WithDidLaunch(func(n objc.Object) {
			win := cocoa.NSWindow_New()
			win.SetTitle("NSButton")
			winRect := core.NSMakeRect(20, 20, 700, 300)
			win.SetFrameDisplay(winRect, true)
			win.MakeKeyAndOrderFront(nil)

			/* NSButton code */
			buttonWidth := 70.0
			buttonHeight := 30.0
			for y := 0; y < 10; y++ {
				for x := 0; x < 10; x++ {
					rect := core.NSMakeRect(float64(x) * buttonWidth, float64(y) * buttonHeight, buttonWidth, buttonHeight)
					button := cocoa.NSButton_Alloc()
					button.InitWithFrame(rect)
					newTitle := fmt.Sprintf("%dx%d", x, y)
					button.SetTitle(newTitle)
					button.SetTarget(myobj)
					button.SetAction(objc.Sel("doButton:"))
					win.ContentView().AddSubview(button)
				}
			}
		})

		app.SetActivationPolicy(cocoa.NSApplicationActivationPolicyRegular)
		app.ActivateIgnoringOtherApps(true)
		app.Run()
}

func doButton(button objc.Object) {
	fmt.Println("Button pressed:", button)
}

The window this program displays looks like this:
grid

Can the doButton() function's argument somehow tell who sent the event? Currently the argument is set to a MyClass class instance.