ryanmcgrath / cacao

Rust bindings for AppKit (macOS) and UIKit (iOS/tvOS). Experimental, but working!
MIT License
1.79k stars 65 forks source link

Fixes possible duplicate objc class registration. #79

Closed MerlinDE closed 1 year ago

MerlinDE commented 1 year ago

This can happen in environments like plugins, where cacao can exist multiple times and thus tries to create and register its objc classes more than once.

I'm using cacao to implement interface elements in Adobe Illustrator plugins. Whenever I had more than one of those added in parallel to Illustrator, it crashed. Reason was, that the objc classes for the interface elements like button, label or image were registered multiple times. This lead to a None returned that was unwrap() in a panic.

This code just checks if a to-be-registered class already exists and if so, returns it. Obviously, it can't change any added elements and just assumes we want to register the same class.

I didn't touch the fn load_or_register_class(), as this seems to already handle the situation.

ryanmcgrath commented 1 year ago

Hmmm, I'm mobile at the moment but is there any reason these changes couldn't just be deferring to load_or_register?

(Appreciate the contribution and can look more tomorrow, just commenting off the cuff right now)

MerlinDE commented 1 year ago

Good point! Let me check and refactor.

MerlinDE commented 1 year ago

Refactored using load_or_register_class

ryanmcgrath commented 1 year ago

Nice! So this works for your expected use-case?

If you could do a formatting pass it'd be great, just to satisfy CI - I'll need to find time to look into why the iOS examples are currently breaking...

ryanmcgrath commented 1 year ago

Heya - just wanted to confirm: this change still fixes the issue you were encountering?

MerlinDE commented 1 year ago

Yes, it does fix my issue.