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

Code generation for composite templates #86

Open
felinira opened this issue Nov 25, 2020 · 6 comments
Open

Code generation for composite templates #86

felinira opened this issue Nov 25, 2020 · 6 comments
Labels
enhancement New feature or request gtk macros

Comments

@felinira
Copy link
Contributor

Over at gnome gitlab @sophie-h created a python script that automatically generates composite template code for bindings from the ui files by parsing the xml, extracting the objects and creating fields (or functions in this case but that's an easily changed implementation detail) for every object that has an id.

I then rewrote it as a rust build script. It could also be reworked as a macro or something similar. You would only need to use the macro, specify a folder that contains your .ui files and it would automatically generate template code for you.

This has several advantages to manually writing template bindings:

  • Compile time checking of UI files, if a field is missing or has the wrong type your code would not compile
  • Less code to be written and maintained.

Would this be a good addition to gtk-rs? If so, does anyone have any feedback on how to integrate it best? @bilelmoussaoui @sophie-h

@bilelmoussaoui
Copy link
Member

Shouldn't be needed because we do support composite template in gtk4-rs, see the following example https://github.com/gtk-rs/gtk4-rs/blob/master/examples/src/bin/composite_template.rs

@sdroege
Copy link
Member

sdroege commented Nov 25, 2020

If such a script provides more than that and makes things easier, the best approach would probably be to make it a proc macro (you can read the .ui files from the macro code) so it can be directly integrated into the Rust code instead of having external code generation via build.rs (which is rather suboptimal build-system-wise).

@felinira
Copy link
Contributor Author

Yes, rewriting it as macros was the idea. I just didn't want to do it before getting feedback about it. Composite templates don't check your actual ui files for any errors or typos, do they? These errors are only caught at runtime which makes it impossible to catch by CI etc. And you still need to write the template files.

@bilelmoussaoui
Copy link
Member

Yes, rewriting it as macros was the idea. I just didn't want to do it before getting feedback about it. Composite templates don't check your actual ui files for any errors or typos, do they? These errors are only caught at runtime which makes it impossible to catch by CI etc. And you still need to write the template files.

Definitely, see my suggestion in #77, Vala does exactly the same thing by having an attribute to specify either a resource uri or a file path and it does check the types at build time that way.

@bilelmoussaoui bilelmoussaoui added enhancement New feature or request gtk labels Nov 25, 2020
@felinira
Copy link
Contributor Author

Ah yes that looks like a good way to do it. I guess three LOC per template file are a good compromise between magic and not having to write a lot of code. I can look into it.

@sdroege
Copy link
Member

sdroege commented Nov 25, 2020

Sounds great, thanks for looking into this :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request gtk macros
Projects
None yet
Development

No branches or pull requests

3 participants