Join GitHub today
GitHub is home to over 50 million developers working together to host and review code, manage projects, and build software together.
Sign updelegate! macro, a higher-level way to create Cocoa delegates (e.g. NSWindowDelegate) #314
Conversation
|
This is a good change. Although at the time I was thinking more about type-safety of the interface than the amount of typing required to set things up. |
|
In fact, i tried looking for such thing, but i saw that NSWindowDelegate is just a protocol, which doesn't really mean something, so i thought making a macro like this for any delegate would be great |
|
Looks like a nice improvement! |
|
|
delegate! macro, a higher-level way to create Cocoa delegates (e.g. NSWindowDelegate) As @vbo mentioned in #205 the only way to create cocoa delegates such as NSWindowDelegate is to use the objc crate and it's a bit hard. I thought a macro for this would be useful. ## Example with NSWindowDelegate Without macro : ```rust let mut decl = ClassDecl::new("MyWindowDelegate", class!(NSObject)).unwrap(); decl.add_ivar::<id>("window"); decl.add_method(sel!(windowWillEnterFullScreen:), on_enter_fullscreen as extern fn(&Object, Sel, id)); let cl = decl.register(); let delegate: id = msg_send![cl, alloc]; (*delegate).set_ivar("window", window); window.setDelegate_(delegate); ``` With macro : ```rust window.setDelegate_(delegate!("MyWindowDelegate", { window: id = window, (windowWillEnterFullScreen:) => on_enter_fullscreen as extern fn(&Object, Sel, id) })); ``` <!-- Reviewable:start --> --- This change is [<img src="https://reviewable.io/review_button.svg" height="34" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/servo/core-foundation-rs/314) <!-- Reviewable:end -->
|
|
|
Can we add a test/example code with this? Or maybe update one of the existing examples? |
|
I added an example in the doc comment, the same as the one in my first message, but if you want to add something feel free to do it @vbo (i agree this is a small example) |
Litarvan commentedMay 2, 2019
•
edited
As @vbo mentioned in #205 the only way to create cocoa delegates such as NSWindowDelegate is to use the objc crate and it's a bit hard.
I thought a macro for this would be useful.
Example with NSWindowDelegate
Without macro :
With macro :
This change is