Skip to content

Add helper package to easier use of Cocoa API with Go#163

Merged
progrium merged 1 commit into
progrium:mainfrom
programmingkidx:helper
Aug 3, 2023
Merged

Add helper package to easier use of Cocoa API with Go#163
progrium merged 1 commit into
progrium:mainfrom
programmingkidx:helper

Conversation

@programmingkidx

Copy link
Copy Markdown
Contributor

This package is made to help the user more easily use the Cocoa API with Go. Currently it adds the SetAction() method. This method makes it very easy to set a control's action to a Go function. One feature that the current implementation of NSControl's SetAction() method doesn't implement that this method does is the ability to send an Action method the sender of the action. This is standard behavior in Objective-C.

This program can be used to test out this pull request. It displays a window with several controls. The user can click on a control and see a message printed to the terminal.

program
package main

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

func main() {
	
	app := cocoa.NSApp_WithDidLaunch(func(n objc.Object) {
			win := cocoa.NSWindow_New()
			win.SetTitle("NSButton")
			winRect := core.NSMakeRect(20, 20, 700, 400)
			win.SetFrameDisplay(winRect, true)
			win.MakeKeyAndOrderFront(nil)

			/* NSSlider code */
			slider := cocoa.NSSlider_Alloc()
			sliderRect := core.NSMakeRect(30, 320, 60, 20)
			slider.InitWithFrame(sliderRect)
			//slider.SetMaxValue(100) // Needs the mapType() patch
			win.ContentView().AddSubview(slider)
			helper.SetAction(slider, doSliderAction)
			
			/* Textfield code */
			field := cocoa.NSTextField_Alloc()
			fieldRect := core.NSMakeRect(120, 320, 200, 20)
			field.InitWithFrame(fieldRect)
			field.SetStringValue("Click then push enter for action")
			win.ContentView().AddSubview(field)
			helper.SetAction(field, doTextFieldAction)
			
			/* 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)
					helper.SetAction(button, doButtonAction)
					win.ContentView().AddSubview(button)
				}
			}
		})

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

// all the buttons' action method
func doButtonAction(sender any) {
	button := sender.(cocoa.NSButton)
	fmt.Println("button pressed:", button.Title())
}

// the slider's action method
func doSliderAction(sender any) {
	slider := sender.(cocoa.NSSlider)
	fmt.Println("Slider:", slider.IntValue())
}

// the textfield's action method
func doTextFieldAction(sender any) {
	field := sender.(cocoa.NSTextField)
	fmt.Println("TextField:", field.StringValue())
}

@progrium

Copy link
Copy Markdown
Owner

I like it.

@progrium progrium merged commit 4ae7826 into progrium:main Aug 3, 2023
@progrium

progrium commented Aug 3, 2023

Copy link
Copy Markdown
Owner

While this is rad it turns out we'll be picking up a very similar helper package in the rewrite branch with nearly identical API for usage. Here's an example: https://github.com/progrium/macdriver/blob/darwinkit/macos/_examples/widgets/main.go#L33

Merging this for the meantime in main, but this specific code won't make it to release since very little will!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants