Skip to content

Commit

Permalink
Create macros crate
Browse files Browse the repository at this point in the history
  • Loading branch information
matthunz committed Sep 29, 2023
1 parent 3513e58 commit 5344de8
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 15 deletions.
9 changes: 9 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ license = "MIT OR Apache-2.0"
description = "Cross-platform UI framework"
repository = "https://github.com/concoct-rs/viewbuilder"

[workspace]
members = [
".",
"macros"
]

[features]
gl = [
"dep:gl",
Expand All @@ -18,6 +24,7 @@ gl = [
default = ["gl"]

[dependencies]
viewbuilder-macros = { path = "macros" }
accesskit = "0.11.2"
kurbo = "0.9.5"
slotmap = "1.0.6"
Expand Down
30 changes: 16 additions & 14 deletions examples/ui.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,23 @@
use std::cell::RefCell;
use viewbuilder::ui::{Scope, View};
use viewbuilder::view;

fn app<'cx>(cx: &'cx Scope) -> View<'cx> {
cx.enter((), || {
let count = cx.use_hook(|| RefCell::new(0));
#[view]
fn app(cx: &Scope) -> View {
let count = cx.use_hook(|| RefCell::new(0));

View::default()
.text(count.borrow().to_string())
.view(View::default().text("Less!").on_click(|| {
dbg!(count.borrow());
*count.borrow_mut() -= 1;
}))
.view(View::default().text("More!").on_click(|| {
dbg!(count.borrow());
*count.borrow_mut() += 1;
}))
})
View::default()
.text(count.borrow().to_string())
.view(
View::default()
.text("Less!")
.on_click(|| *count.borrow_mut() -= 1),
)
.view(
View::default()
.text("More!")
.on_click(|| *count.borrow_mut() += 1),
)
}

fn main() -> Result<(), viewbuilder::Error> {
Expand Down
11 changes: 11 additions & 0 deletions macros/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[package]
name = "viewbuilder-macros"
version = "0.1.0"
edition = "2021"

[lib]
proc-macro = true

[dependencies]
quote = "1.0.33"
syn = { version = "2.0.37", features = ["full"] }
18 changes: 18 additions & 0 deletions macros/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
use proc_macro::TokenStream;
use quote::ToTokens;
use syn::{parse_macro_input, parse_quote, ItemFn};

#[proc_macro_attribute]
pub fn view(_args: TokenStream, input: TokenStream) -> TokenStream {
// Parse the input function as an ItemFn
let mut input = parse_macro_input!(input as ItemFn);

let block = input.block.clone();
input.block = parse_quote!({
cx.enter((), || {
#block
})
});

input.into_token_stream().into()
}
3 changes: 2 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
//! .build(cx)
//! }
//! ```
//!

pub use viewbuilder_macros::view;

use thiserror::Error;

Expand Down

0 comments on commit 5344de8

Please sign in to comment.