-
Notifications
You must be signed in to change notification settings - Fork 21
Add component macro and example #2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
maan2003
wants to merge
2
commits into
raphlinus:master
Choose a base branch
from
maan2003:macro
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| /target | ||
| Cargo.lock |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| [package] | ||
| name = "crochet-macros" | ||
| version = "0.1.0" | ||
| authors = ["Maan2003 <manmeetmann2003@gmail.com>"] | ||
| license = "Apache-2.0" | ||
| description = "macros for crochet" | ||
| repository = "https://github.com/raphlinus/crochet" | ||
| categories = ["gui"] | ||
| edition = "2018" | ||
|
|
||
| [lib] | ||
| proc-macro = true | ||
|
|
||
| [dependencies] | ||
| syn = { version = "1.0.41", features = ["full"] } | ||
maan2003 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| quote = "1.0.7" | ||
| proc-macro2 = "1.0.22" | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| extern crate proc_macro; | ||
| use proc_macro::TokenStream; | ||
| use quote::quote; | ||
| use syn::{parse_macro_input, FnArg, Ident, ItemFn, Pat, ReturnType, Type}; | ||
|
|
||
| #[proc_macro_attribute] | ||
| pub fn component(_args: TokenStream, input: TokenStream) -> TokenStream { | ||
| let ItemFn { attrs, vis, sig, block } = parse_macro_input!(input as ItemFn); | ||
| let cx = name_of_cx(&sig.inputs).expect("No argument of type `&mut Cx` found"); | ||
|
|
||
| // return_ty is explicit to produce better error for return type mismatch. | ||
| let return_ty = return_ty_or_unit(&sig.output); | ||
|
|
||
| let tokens = quote! { | ||
| #[track_caller] | ||
| #(#attrs)* | ||
| #vis #sig { | ||
| ::crochet::Cx::with_loc( | ||
| #cx, ::std::panic::Location::caller(), move |#cx| #return_ty #block | ||
| ) | ||
| } | ||
| }; | ||
| tokens.into() | ||
| } | ||
|
|
||
| fn name_of_cx<'a>(args: impl IntoIterator<Item = &'a FnArg>) -> Option<Ident> { | ||
| for arg in args { | ||
| if let FnArg::Typed(arg) = arg { | ||
| if let Type::Reference(ty_ref) = &*arg.ty { | ||
| if let (Some(_), Type::Path(path)) = (ty_ref.mutability, &*ty_ref.elem) { | ||
| if let Some(seg) = path.path.segments.first() { | ||
| if seg.ident == "Cx" { | ||
| if let Pat::Ident(pat_ident) = &*arg.pat { | ||
| return Some(pat_ident.ident.clone()); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| None | ||
| } | ||
|
|
||
| fn return_ty_or_unit(ty: &ReturnType) -> proc_macro2::TokenStream { | ||
| if ty == &ReturnType::Default { | ||
| quote! { -> () } | ||
| } else { | ||
| quote! { #ty } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| //! The classic counter example but split up into component | ||
|
|
||
| use druid::{AppLauncher, PlatformError, Widget, WindowDesc}; | ||
|
|
||
| use crochet::{component, AppHolder, Button, Column, Cx, DruidAppData, Label, Row}; | ||
|
|
||
| fn main() -> Result<(), PlatformError> { | ||
| let main_window = WindowDesc::new(ui_builder); | ||
| let data = Default::default(); | ||
| AppLauncher::with_window(main_window) | ||
| .use_simple_logger() | ||
| .launch(data) | ||
| } | ||
|
|
||
| #[derive(Default)] | ||
| struct MyAppLogic { | ||
| count1: usize, | ||
| count2: usize, | ||
| } | ||
|
|
||
| impl MyAppLogic { | ||
| fn run(&mut self, cx: &mut Cx) { | ||
| Row::new().build(cx, |cx| { | ||
| counter(&mut self.count1, cx); | ||
| counter(&mut self.count2, cx); | ||
| }); | ||
| } | ||
| } | ||
|
|
||
| #[component] | ||
| fn counter(count: &mut usize, cx: &mut Cx) { | ||
| cx.if_changed(*count, |cx| { | ||
| Column::new().build(cx, |cx| { | ||
| Label::new(format!("current count: {}", *count)).build(cx); | ||
| if Button::new("Increment").build(cx) { | ||
| *count += 1; | ||
| } | ||
| if *count > 3 && *count < 6 { | ||
| Label::new("You did it!").build(cx); | ||
| } | ||
| }); | ||
| }); | ||
| } | ||
|
|
||
| fn ui_builder() -> impl Widget<DruidAppData> { | ||
| let mut app_logic = MyAppLogic::default(); | ||
|
|
||
| AppHolder::new(move |cx| app_logic.run(cx)) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.