Skip to content

Commit 0e44f5d

Browse files
committed
generate connect_prop_notify
1 parent 969b478 commit 0e44f5d

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

glib-macros/src/props.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -467,6 +467,28 @@ fn expand_getset_properties_impl(imp_type_ident: &syn::Ident, props: &[PropDesc]
467467
quote!(#(#defs)*)
468468
}
469469

470+
fn expand_connect_prop_notify(p: &PropDesc) -> TokenStream2 {
471+
let name = &p.name;
472+
let fn_ident = format_ident!("connect_{}_notify", name_to_ident(name));
473+
quote!(fn #fn_ident<F: Fn(&Self) + 'static>(&self, f: F) -> glib::SignalHandlerId)
474+
}
475+
fn expand_connect_prop_notify_def(props: &[PropDesc]) -> TokenStream2 {
476+
let connection_fns = props.iter().map(expand_connect_prop_notify);
477+
quote!(#(#connection_fns;)*)
478+
}
479+
fn expand_connect_prop_notify_impl(props: &[PropDesc]) -> TokenStream2 {
480+
let connection_fns = props.iter().map(|p| {
481+
let name = &p.name;
482+
let fn_signature = expand_connect_prop_notify(p);
483+
quote!(#fn_signature {
484+
self.connect_notify_local(Some(#name), move |this, _| {
485+
f(this)
486+
})
487+
})
488+
});
489+
quote!(#(#connection_fns)*)
490+
}
491+
470492
pub fn impl_derive_props(input: PropsMacroInput) -> TokenStream {
471493
let struct_ident = &input.ident;
472494
let struct_ident_ext = format_ident!("{}Ext", &input.ident);
@@ -477,6 +499,8 @@ pub fn impl_derive_props(input: PropsMacroInput) -> TokenStream {
477499
let fn_set_property = expand_set_property_fn(&input.props);
478500
let getset_properties_def = expand_getset_properties_def(&input.props);
479501
let getset_properties_impl = expand_getset_properties_impl(struct_ident, &input.props);
502+
let connect_prop_notify_def = expand_connect_prop_notify_def(&input.props);
503+
let connect_prop_notify_impl = expand_connect_prop_notify_impl(&input.props);
480504
let expanded = quote! {
481505
use glib::{PropRead, PropWrite};
482506

@@ -493,9 +517,11 @@ pub fn impl_derive_props(input: PropsMacroInput) -> TokenStream {
493517

494518
pub trait #struct_ident_ext {
495519
#getset_properties_def
520+
#connect_prop_notify_def
496521
}
497522
impl #struct_ident_ext for #wrapper_type {
498523
#getset_properties_impl
524+
#connect_prop_notify_impl
499525
}
500526

501527
};

glib-macros/tests/props.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,8 @@ fn props() {
173173
// optional
174174
assert_eq!(myfoo.property::<Option<String>>("optional"), None,);
175175

176+
myfoo.connect_optional_notify(|_| println!("notified"));
177+
176178
// Test `FooExt`
177179
// getters
178180
{

0 commit comments

Comments
 (0)