Skip to content

Higher-level interface to create delegates #205

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

Open
Tracked by #719
vbo opened this issue Apr 17, 2018 · 7 comments
Open
Tracked by #719

Higher-level interface to create delegates #205

vbo opened this issue Apr 17, 2018 · 7 comments

Comments

@vbo
Copy link
Contributor

vbo commented Apr 17, 2018

It seems currently the only way to create e.g. an NSWindowDelegate is through objc::declare::ClassDecl interface:

let superclass = Class::get("NSObject").unwrap();
let mut decl = ClassDecl::new("MyWindowDelegate", superclass).unwrap();
decl.add_method ...

Should this crate expose trait NSWindowDelegate and make the creation of such delegate more type-safe?

@nox
Copy link
Contributor

nox commented Apr 17, 2018

Neither NSObject nor MyWindowDelegate are about Core Foundation, so I'm not sure why such a thing should exist in that crate. What makes you think so?

@nox
Copy link
Contributor

nox commented Apr 17, 2018

Did you mean to file that on the objc crate, maybe?

@vbo
Copy link
Contributor Author

vbo commented Apr 17, 2018

True, this is not about Core Foundation, but it is about Cocoa, which is a part of this crate.

This crate defines trait NSWindow for one of the core Cocoa interfaces. The interface provides setDelegate method to set NSWindowDelegate implementation which controls window behavior. Now currently the binding for this method looks like setDelegate_(self, delegate: id) so I can put any object there. I can create this object via objc crate, but I was wondering if this is too low-level and if we want to provide type-safe API in this crate to create such a delegate object.

@nox
Copy link
Contributor

nox commented Apr 17, 2018

Oh I see.

@Litarvan
Copy link
Contributor

Litarvan commented May 1, 2019

Any update ? I was also using setDelegate and didn't find any other way than the one mentionned by @vbo , may I create a pull request to implement a more high level inteface ?

@jdm
Copy link
Member

jdm commented May 1, 2019

A higher level interface sounds like a useful improvement, yes.

@Litarvan
Copy link
Contributor

Litarvan commented May 2, 2019

I tried something in #314

bors-servo pushed a commit that referenced this issue 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 -->
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

No branches or pull requests

4 participants