gtk_rust_app_derive/
lib.rs

1// SPDX-License-Identifier: GPL-3.0-or-later
2
3extern crate proc_macro;
4
5use proc_macro::TokenStream;
6
7mod gobject;
8mod variant;
9mod widget;
10
11/// Define GObject based on a struct
12///
13/// # Example
14/// ```rust,ignore
15/// #[gobject(id, name, selected)]
16/// struct Event {
17///     id: String,
18///     name: String,
19///     selected: bool,
20/// }
21/// ```
22///
23/// ## Supported field types
24/// - String
25/// - bool
26#[proc_macro_attribute]
27pub fn gobject(args: TokenStream, input: TokenStream) -> TokenStream {
28    gobject::gobject(args, input)
29}
30
31/// Define a new GTK widget based on a struct
32/// # Example
33/// ```rust,ignore
34/// use gdk4::subclass::prelude::ObjectSubclassIsExt;
35/// use glib::closure_local;
36/// use gtk::prelude::*;
37/// use std::cell::Cell;
38///
39/// #[widget(extends gtk::Box)]
40/// #[template(file = "card.ui")]
41/// pub struct Card {
42///     #[property_string]
43///     pub text: Cell<String>,
44///     #[signal]
45///     pub card_changed: (),
46///     #[signal]
47///     pub card_clicked: (),
48///
49///     #[template_child]
50///     pub card_button: TemplateChild<gtk::Button>,
51///
52///     #[template_child]
53///     pub card_entry: TemplateChild<gtk::Entry>,
54/// }
55///
56/// impl Card {
57///     pub fn new(&self) {
58///         let s = self;
59///         self.imp()
60///             .card_button
61///             .connect_clicked(glib::clone!(@weak s => move |_| {
62///                 s.emit_card_clicked()
63///             }));
64///         self.imp()
65///             .card_entry
66///             .connect_changed(glib::clone!(@weak s => move |entry| {
67///                 let text = entry.text().to_string();
68///                 s.imp().text.replace(text);
69///                 s.emit_card_changed()
70///             }));
71///     }
72///
73///     // If you want to have a public connector you can define one like this.
74///     // The method _connect_<signal-name> is generated for this purpose
75///     pub fn conenct_card_clicked(&self, f: impl Fn(&Self) + 'static) {
76///         self._connect_card_clicked(f);
77///     }
78///
79///     pub fn connect_card_changed(&self, f: impl Fn(&Self) + 'static) {
80///         self._connect_card_changed(f);
81///     }
82///
83///     pub fn text(&self) -> String {
84///         self.property("text")
85///     }
86/// }
87/// ```
88#[proc_macro_attribute]
89pub fn widget(args: TokenStream, input: TokenStream) -> TokenStream {
90    widget::widget(args, input)
91}
92
93// #[proc_macro_attribute]
94// pub fn with_store(args: TokenStream, input: TokenStream) -> TokenStream {
95//     widget::with_store(args, input)
96// }
97
98/// Auto implement FromVariant and ToVariant for a struct
99#[proc_macro_attribute]
100pub fn variant_serde_json(args: TokenStream, input: TokenStream) -> TokenStream {
101    variant::variant_serde_json(args, input)
102}