-
-
Notifications
You must be signed in to change notification settings - Fork 114
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
Comments
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 /// 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 impl WidgetImpl for SomeWidget {
type Callback = SomeWidgetCallback;
} or use a different trait for that purpose/make it an option of Then use |
This would generate a function that works on
GValue
s 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
The text was updated successfully, but these errors were encountered: