Skip to content

Commit 969b478

Browse files
committed
add TypeAutoProps trait
1 parent 19f6a9c commit 969b478

File tree

2 files changed

+30
-4
lines changed

2 files changed

+30
-4
lines changed

glib-macros/src/props.rs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,7 @@ fn expand_properties_fn(props: &[PropDesc]) -> TokenStream2 {
290290
}
291291
});
292292
quote!(
293-
fn properties() -> &'static [glib::ParamSpec] {
293+
fn auto_properties() -> &'static [glib::ParamSpec] {
294294
use glib::once_cell::sync::Lazy;
295295
static PROPERTIES: Lazy<[glib::ParamSpec; #n_props]> = Lazy::new(|| [
296296
#(#properties_build_phase,)*
@@ -322,7 +322,7 @@ fn expand_property_fn(props: &[PropDesc]) -> TokenStream2 {
322322
}
323323
});
324324
quote!(
325-
fn property(&self, _obj: &Self::Type, _id: usize, pspec: &glib::ParamSpec) -> glib::Value {
325+
fn auto_property(&self, _obj: &Self::Type, _id: usize, pspec: &glib::ParamSpec) -> glib::Value {
326326
match pspec.name() {
327327
#(#match_branch_get,)*
328328
p => unreachable!("Invalid property {}", p)
@@ -355,7 +355,7 @@ fn expand_set_property_fn(props: &[PropDesc]) -> TokenStream2 {
355355
}
356356
});
357357
quote!(
358-
fn set_property(&self, _obj: &Self::Type, _id: usize, value: &glib::Value, pspec: &glib::ParamSpec) {
358+
fn auto_set_property(&self, _obj: &Self::Type, _id: usize, value: &glib::Value, pspec: &glib::ParamSpec) {
359359
match pspec.name() {
360360
#(#match_branch_set,)*
361361
p => unreachable!("Invalid property {}", p)
@@ -470,6 +470,7 @@ fn expand_getset_properties_impl(imp_type_ident: &syn::Ident, props: &[PropDesc]
470470
pub fn impl_derive_props(input: PropsMacroInput) -> TokenStream {
471471
let struct_ident = &input.ident;
472472
let struct_ident_ext = format_ident!("{}Ext", &input.ident);
473+
let struct_ident_props = format_ident!("{}AutoProps", &input.ident);
473474
let wrapper_type = quote!(<#struct_ident as glib::subclass::types::ObjectSubclass>::Type);
474475
let fn_properties = expand_properties_fn(&input.props);
475476
let fn_property = expand_property_fn(&input.props);
@@ -478,7 +479,13 @@ pub fn impl_derive_props(input: PropsMacroInput) -> TokenStream {
478479
let getset_properties_impl = expand_getset_properties_impl(struct_ident, &input.props);
479480
let expanded = quote! {
480481
use glib::{PropRead, PropWrite};
481-
impl ObjectImpl for #struct_ident {
482+
483+
pub trait #struct_ident_props: ObjectSubclass {
484+
fn auto_properties() -> &'static [ParamSpec];
485+
fn auto_set_property(&self, _obj: &Self::Type, _id: usize, _value: &glib::Value, _pspec: &glib::ParamSpec);
486+
fn auto_property(&self, _obj: &Self::Type, _id: usize, _pspec: &glib::ParamSpec) -> glib::Value;
487+
}
488+
impl #struct_ident_props for #struct_ident {
482489
#fn_properties
483490
#fn_property
484491
#fn_set_property

glib-macros/tests/props.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ fn props() {
2323
}
2424

2525
pub mod imp {
26+
use glib::{ParamSpec, Value};
2627
use std::rc::Rc;
2728

2829
use super::*;
@@ -59,6 +60,24 @@ fn props() {
5960
smart_pointer: Rc<RefCell<String>>,
6061
}
6162

63+
impl ObjectImpl for Foo {
64+
fn properties() -> &'static [ParamSpec] {
65+
Self::auto_properties()
66+
}
67+
fn set_property(
68+
&self,
69+
_obj: &Self::Type,
70+
_id: usize,
71+
_value: &Value,
72+
_pspec: &ParamSpec,
73+
) {
74+
Self::auto_set_property(self, _obj, _id, _value, _pspec)
75+
}
76+
fn property(&self, _obj: &Self::Type, _id: usize, _pspec: &ParamSpec) -> Value {
77+
Self::auto_property(self, _obj, _id, _pspec)
78+
}
79+
}
80+
6281
#[glib::object_subclass]
6382
impl ObjectSubclass for Foo {
6483
const NAME: &'static str = "MyFoo";

0 commit comments

Comments
 (0)