Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Higher level subclassing API #207

Merged
merged 2 commits into from
Aug 26, 2023
Merged

Higher level subclassing API #207

merged 2 commits into from
Aug 26, 2023

Conversation

progrium
Copy link
Owner

@progrium progrium commented Aug 25, 2023

This PR introduces objc.NewClass[T](), which takes a struct type implementing IObject that it uses to allocate a class. It uses reflection to set the class name from the struct type name, and uses NSObject as the super unless specified by a struct tag. It optionally takes selectors that will be added as methods using the struct methods with Go equivalent name. The resulting class can be registered and has a type specific New() that initializes and autoreleases.

Example

type CustomView struct {
	appkit.View `objc:"NSView"`
}

func (v CustomView) AcceptsFirstResponder() bool {
	return true
}

func (v CustomView) KeyDown(event appkit.Event) {
	log.Println("Keydown:", v.Class().Name(), event.Class().Name())
}

func main() {
	// passing a selector that doesn't have a matching Go method will panic
	CustomViewClass := objc.NewClass[CustomView](
		objc.Sel("acceptsFirstResponder"),
		objc.Sel("keyDown:"),
	)
	objc.RegisterClass(CustomViewClass)

	// New() returns a CustomView, but InitWithFrame() will return a View.
	// If you need CustomView, you can assign it before the View init method.
	view := CustomViewClass.New().InitWithFrame(rectOf(0, 0, 150, 99))

	...
}

More examples can be found in the tests or the updated subclass example.

Breaking Changes

Changes the Class() method on objc.Class and objc.IClass to MetaClass().

…ss example to showcase api. CHANGE method Class() on struct Class and interface IClass to MetaClass()
@progrium progrium added this to the 0.5.0 milestone Aug 25, 2023
@progrium progrium requested a review from tmc August 25, 2023 21:29
Copy link
Collaborator

@tmc tmc left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice, I ilke the interface.

@progrium progrium changed the title Higher level subclassing Higher level subclassing API Aug 26, 2023
@progrium progrium merged commit 015b4c9 into main Aug 26, 2023
3 checks passed
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