Skip to content

Commit 2fd3218

Browse files
committed
Add initial glib::signals! macro
1 parent c3fffca commit 2fd3218

File tree

5 files changed

+525
-8
lines changed

5 files changed

+525
-8
lines changed

examples/object_subclass/author.rs

+11-5
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,13 @@
44
use glib::prelude::*;
55
use glib::subclass::prelude::*;
66
use glib::subclass::Signal;
7-
use glib::Properties;
7+
use glib::subclass::object::DerivedObjectSignals;
88
use std::cell::RefCell;
9-
use std::sync::OnceLock;
109

1110
mod imp {
1211
use super::*;
1312

14-
#[derive(Properties, Default)]
13+
#[derive(glib::Properties, Default)]
1514
#[properties(wrapper_type = super::Author)]
1615
pub struct Author {
1716
#[property(get, set)]
@@ -20,11 +19,17 @@ mod imp {
2019
surname: RefCell<String>,
2120
}
2221

22+
#[glib::signals(wrapper_type = super::Author)]
23+
impl Author {
24+
25+
#[signal]
26+
fn awarded(&self) {}
27+
}
28+
2329
#[glib::derived_properties]
2430
impl ObjectImpl for Author {
2531
fn signals() -> &'static [Signal] {
26-
static SIGNALS: OnceLock<Vec<Signal>> = OnceLock::new();
27-
SIGNALS.get_or_init(|| vec![Signal::builder("awarded").build()])
32+
Self::derived_signals()
2833
}
2934
}
3035

@@ -38,6 +43,7 @@ mod imp {
3843
glib::wrapper! {
3944
pub struct Author(ObjectSubclass<imp::Author>);
4045
}
46+
4147
impl Author {
4248
pub fn new(name: &str, surname: &str) -> Self {
4349
glib::Object::builder()

glib-macros/src/lib.rs

+18-1
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,14 @@ mod properties;
1414
mod shared_boxed_derive;
1515
mod value_delegate_derive;
1616
mod variant_derive;
17+
mod signals;
1718

1819
mod utils;
1920

2021
use flags_attribute::AttrInput;
2122
use proc_macro::TokenStream;
2223
use proc_macro2::Span;
23-
use syn::{parse_macro_input, DeriveInput};
24+
use syn::{parse_macro_input, DeriveInput, Error};
2425
use utils::{parse_nested_meta_items_from_stream, NestedMetaItem};
2526

2627
/// Macro for passing variables as strong or weak references into a closure.
@@ -1587,3 +1588,19 @@ pub fn derive_value_delegate(input: TokenStream) -> TokenStream {
15871588
pub fn async_test(args: TokenStream, item: TokenStream) -> TokenStream {
15881589
async_test::async_test(args, item)
15891590
}
1591+
1592+
#[proc_macro_attribute]
1593+
pub fn signals(attr: TokenStream, item: TokenStream) -> TokenStream {
1594+
let args = parse_macro_input!(attr as signals::SignalsArgs);
1595+
match syn::parse::<syn::ItemImpl>(item) {
1596+
Ok(input) => signals::impl_signals(input, args)
1597+
.unwrap_or_else(syn::Error::into_compile_error)
1598+
.into(),
1599+
Err(_) => Error::new(
1600+
Span::call_site(),
1601+
signals::WRONG_PLACE_MSG,
1602+
)
1603+
.into_compile_error()
1604+
.into(),
1605+
}
1606+
}

0 commit comments

Comments
 (0)