Skip to content

Commit

Permalink
Add initial glib::signals! macro
Browse files Browse the repository at this point in the history
  • Loading branch information
PJungkamp committed Nov 22, 2024
1 parent c3fffca commit 7e0eff9
Show file tree
Hide file tree
Showing 5 changed files with 525 additions and 8 deletions.
16 changes: 11 additions & 5 deletions examples/object_subclass/author.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,13 @@
use glib::prelude::*;
use glib::subclass::prelude::*;
use glib::subclass::Signal;
use glib::Properties;
use glib::subclass::object::DerivedObjectSignals;
use std::cell::RefCell;
use std::sync::OnceLock;

mod imp {
use super::*;

#[derive(Properties, Default)]
#[derive(glib::Properties, Default)]
#[properties(wrapper_type = super::Author)]
pub struct Author {
#[property(get, set)]
Expand All @@ -20,11 +19,17 @@ mod imp {
surname: RefCell<String>,
}

#[glib::signals(wrapper_type = super::Author)]
impl Author {

#[signal]
fn awarded(&self) {}
}

#[glib::derived_properties]
impl ObjectImpl for Author {
fn signals() -> &'static [Signal] {
static SIGNALS: OnceLock<Vec<Signal>> = OnceLock::new();
SIGNALS.get_or_init(|| vec![Signal::builder("awarded").build()])
Self::derived_signals()
}
}

Expand All @@ -38,6 +43,7 @@ mod imp {
glib::wrapper! {
pub struct Author(ObjectSubclass<imp::Author>);
}

impl Author {
pub fn new(name: &str, surname: &str) -> Self {
glib::Object::builder()
Expand Down
19 changes: 18 additions & 1 deletion glib-macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,14 @@ mod properties;
mod shared_boxed_derive;
mod value_delegate_derive;
mod variant_derive;
mod signals;

mod utils;

use flags_attribute::AttrInput;
use proc_macro::TokenStream;
use proc_macro2::Span;
use syn::{parse_macro_input, DeriveInput};
use syn::{parse_macro_input, DeriveInput, Error};
use utils::{parse_nested_meta_items_from_stream, NestedMetaItem};

/// Macro for passing variables as strong or weak references into a closure.
Expand Down Expand Up @@ -1587,3 +1588,19 @@ pub fn derive_value_delegate(input: TokenStream) -> TokenStream {
pub fn async_test(args: TokenStream, item: TokenStream) -> TokenStream {
async_test::async_test(args, item)
}

#[proc_macro_attribute]
pub fn signals(attr: TokenStream, item: TokenStream) -> TokenStream {
let args = parse_macro_input!(attr as signals::SignalsArgs);
match syn::parse::<syn::ItemImpl>(item) {
Ok(input) => signals::impl_signals(input, args)
.unwrap_or_else(syn::Error::into_compile_error)
.into(),
Err(_) => Error::new(
Span::call_site(),
signals::WRONG_PLACE_MSG,
)
.into_compile_error()
.into(),
}
}
Loading

0 comments on commit 7e0eff9

Please sign in to comment.