appkit_derive/
lib.rs

1//! Macros used for `appkit-rs`. Mostly acting as `ShinkWrap`-esque forwarders.
2//! Note that most of this is experimental!
3
4extern crate proc_macro;
5
6use crate::proc_macro::TokenStream;
7use quote::quote;
8use syn::{DeriveInput, parse_macro_input};
9
10/// Derivces an `appkit::prelude::WinWrapper` block, which implements forwarding methods for things
11/// like setting the window title, or showing and closing it. It currently expects that the wrapped
12/// struct has `window` as the field holding the `Window` from `appkit-rs`.
13///
14/// Note that this expects that pointers to Window(s) should not move once created.
15#[proc_macro_derive(WindowWrapper)]
16pub fn impl_window_controller(input: TokenStream) -> TokenStream {
17    let input = parse_macro_input!(input as DeriveInput);
18
19    let name = &input.ident;
20    let generics = input.generics;
21    let (impl_generics, ty_generics, where_clause) = generics.split_for_impl();
22
23    let expanded = quote! {
24        impl #impl_generics appkit::prelude::WinWrapper for #name #ty_generics #where_clause {
25            fn set_title(&self, title: &str) { self.window.set_title(title); }
26            fn show(&self) { self.window.show(self); }
27            fn close(&self) { self.window.close(); }
28        }
29    };
30
31    TokenStream::from(expanded)
32}