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

Provide proc macro attribute to generate generic signal handler functions #19

Closed
sdroege opened this issue Nov 23, 2020 · 1 comment · Fixed by #338
Closed

Provide proc macro attribute to generate generic signal handler functions #19

sdroege opened this issue Nov 23, 2020 · 1 comment · Fixed by #338
Labels
enhancement New feature or request glib-macros

Comments

@sdroege
Copy link
Member

sdroege commented Nov 23, 2020

This would generate a function that works on GValues and automatically converts to the given types in the Rust function signature.

This needs to wait for gtk-rs/gtk3-rs#48 and would be useful for solving gtk-rs/gtk4-rs#24 and gtk-rs/gtk3-rs#128

@bilelmoussaoui
Copy link
Member

bilelmoussaoui commented Apr 10, 2021

I think what would be nice to have at first is a simple macro that would be able to do something similar to

fn main() {
	let callback = gclosure!(move |self_: SomeWidget, some_arg: &str| {
		do_something(self_, some_arg)
	});

	// generates
	// would also need to support new/new_unsafe maybe?
	let callback = glib::Closure::new_local(move |args| {
		let self_ = args.get(0).unwrap().get::<SomeWidget>().unwrap();
		let some_arg = args.get(1).unwrap().get::<&str>().unwrap();
		do_something(self_, some_arg)
	});
}

and have different proc macros make use of it. For the BuilderScope use case in gtk4-rs, we would make use of it this way

use crate::BuilderScope;
use glib::subclass::prelude::*;
use glib::ObjectExt;

mod imp {
    use crate::subclass::prelude::BuilderScopeImpl;

    use super::*;
    #[derive(Debug, Default)]
    pub struct RustBuilderScope {}

    #[glib::object_subclass]
    impl ObjectSubclass for RustBuilderScope {
        const NAME: &'static str = "RustBuilderScope";
        type ParentType = glib::Object;
        type Type = super::RustBuilderScope;
        type Interfaces = (BuilderScope,);
    }

    impl ObjectImpl for RustBuilderScope {}
    impl BuilderScopeImpl for RustBuilderScope {
        fn create_closure(
            &self,
            builder_scope: &Self::Type,
            builder: &crate::Builder,
            function_name: &str,
            flags: crate::BuilderClosureFlags,
            object: Option<&glib::Object>,
        ) -> Result<glib::Closure, glib::Error> {
            let current_object = builder.get_current_object().unwrap();
            unsafe {
                /// get the closure with it's name from the current_object's TypeData?
                /// return it 
                println!("{:#?}", function_name);
                todo!()
            }
        }
    }
}

glib::wrapper! {
    pub struct RustBuilderScope(ObjectSubclass<imp::RustBuilderScope>) @implements BuilderScope;
}

impl RustBuilderScope {
    pub fn new() -> Self {
        glib::Object::new(&[]).expect("Failed to create a RustBuilderScope")
    }
}

So I thought of having at first a proc macro for defining the functions that would be later converted using gclosure!

/// Transforms the trait to a SomeWidgetCallback(Map<function_name, gclosure>); 
#[gtk::callback_handler]
trait SomeWidgetCallback {
	#[gtk::callback]
	pub fn something(&self, some_args: &str) {
		// change the button color
	}
}

and have the user define the struct their *Callback struct using something like

impl WidgetImpl for SomeWidget {
	type Callback = SomeWidgetCallback;
}

or use a different trait for that purpose/make it an option of CompositeTemplate?

Then use Callback type to get the gclosures we need to return from BuilderScope's side.

@GuillaumeGomez GuillaumeGomez transferred this issue from gtk-rs/gtk3-rs May 14, 2021
@sdroege sdroege added enhancement New feature or request glib-macros labels May 18, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request glib-macros
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants