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

delegate! macro, a higher-level way to create Cocoa delegates (e.g. NSWindowDelegate) #314

Merged
merged 2 commits into from May 2, 2019

Conversation

@Litarvan
Copy link
Contributor

Litarvan commented May 2, 2019

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 :

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 :

window.setDelegate_(delegate!("MyWindowDelegate", {
    window: id = window,
    (windowWillEnterFullScreen:) => on_enter_fullscreen as extern fn(&Object, Sel, id)
}));

This change is Reviewable

@vbo
Copy link
Contributor

vbo commented May 2, 2019

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.

@Litarvan
Copy link
Contributor Author

Litarvan commented May 2, 2019

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

@jdm
Copy link
Member

jdm commented May 2, 2019

Looks like a nice improvement!
@bors-servo r+

@bors-servo
Copy link
Contributor

bors-servo commented May 2, 2019

📌 Commit f65f59b has been approved by jdm

bors-servo added a commit that referenced this pull request May 2, 2019
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 -->
@bors-servo
Copy link
Contributor

bors-servo commented May 2, 2019

Testing commit f65f59b with merge 965cc88...

@bors-servo
Copy link
Contributor

bors-servo commented May 2, 2019

☀️ Test successful - checks-travis
Approved by: jdm
Pushing 965cc88 to master...

@bors-servo bors-servo merged commit f65f59b into servo:master May 2, 2019
2 checks passed
2 checks passed
Travis CI - Pull Request Build Passed
Details
homu Test successful
Details
@vbo
Copy link
Contributor

vbo commented May 2, 2019

Can we add a test/example code with this? Or maybe update one of the existing examples?

@Litarvan
Copy link
Contributor Author

Litarvan commented May 2, 2019

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)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked issues

Successfully merging this pull request may close these issues.

None yet

4 participants
You can’t perform that action at this time.