From 241cae3c9333fd26c5aa1adb54422278367a9ed8 Mon Sep 17 00:00:00 2001 From: Ryan McGrath Date: Wed, 5 Jun 2019 21:49:41 -0700 Subject: [PATCH] Update example to show props deriving, text macro, etc --- alchemy/src/components/fragment.rs | 2 +- alchemy/src/components/view.rs | 2 +- alchemy/src/lib.rs | 5 +++-- examples/layout/src/main.rs | 36 +++++++++++++++--------------- lifecycle/src/lib.rs | 10 +++++++++ macros/src/lib.rs | 4 ++-- styles/src/spacedset.rs | 15 +++++++++++++ 7 files changed, 50 insertions(+), 24 deletions(-) diff --git a/alchemy/src/components/fragment.rs b/alchemy/src/components/fragment.rs index d447440..a3c940e 100644 --- a/alchemy/src/components/fragment.rs +++ b/alchemy/src/components/fragment.rs @@ -23,7 +23,7 @@ pub struct FragmentProps; pub struct Fragment; impl Fragment { - fn default_props() -> FragmentProps { + pub fn default_props() -> FragmentProps { FragmentProps {} } } diff --git a/alchemy/src/components/view.rs b/alchemy/src/components/view.rs index b9213e1..a8ccdaf 100644 --- a/alchemy/src/components/view.rs +++ b/alchemy/src/components/view.rs @@ -5,7 +5,7 @@ use std::sync::Mutex; -use alchemy_styles::{Appearance, Layout, StylesList}; +use alchemy_styles::{Appearance, Layout}; use alchemy_lifecycle::ComponentKey; use alchemy_lifecycle::error::Error; diff --git a/alchemy/src/lib.rs b/alchemy/src/lib.rs index ce9b625..3bf0bfd 100644 --- a/alchemy/src/lib.rs +++ b/alchemy/src/lib.rs @@ -9,9 +9,9 @@ use std::sync::Arc; pub use lazy_static::lazy_static; use proc_macro_hack::proc_macro_hack; -pub use alchemy_lifecycle::ComponentKey; +pub use alchemy_lifecycle::{ComponentKey, text}; pub use alchemy_lifecycle::traits::{ - AppDelegate, Component, Props, WindowDelegate + AppDelegate, Component, Props as ComponentProps, WindowDelegate }; pub use alchemy_lifecycle::error::Error; @@ -24,6 +24,7 @@ pub use alchemy_macros::rsx; #[proc_macro_hack] pub use alchemy_macros::styles; +pub use alchemy_macros::Props; pub use alchemy_styles::{Color, styles as style_attributes, SpacedSet, StyleSheet, StylesList}; diff --git a/examples/layout/src/main.rs b/examples/layout/src/main.rs index f1a5e50..2ea858f 100644 --- a/examples/layout/src/main.rs +++ b/examples/layout/src/main.rs @@ -8,7 +8,7 @@ /// @created March 26th, 2019 use alchemy::{ - AppDelegate, Component, ComponentKey, Fragment, Props, Error, rsx, RSX, styles, + AppDelegate, Component, ComponentKey, Fragment, Error, Props, rsx, RSX, styles, text, Text, View, Window, WindowDelegate }; @@ -24,26 +24,26 @@ impl AppDelegate for AppState { } } -/*#[derive(Default)] -pub struct Banner; +#[derive(Default)] +struct BannerProps {} + +#[derive(Props)] +struct Banner; impl Component for Banner { - type Props = Box<()>; - type State = Box<()>; - - fn constructor(_key: ComponentKey) -> Banner { + fn new(_key: ComponentKey) -> Banner { Banner {} } - fn render(&self, props: &Self::Props) -> Result { + fn render(&self, children: Vec) -> Result { Ok(rsx! { - //{props.children.clone()} + {children} }) } -}*/ +} pub struct WindowState; @@ -55,17 +55,16 @@ impl WindowDelegate for WindowState { fn render(&self) -> Result { let messages = vec!["LOL"]; //, "wut", "BERT"]; Ok(rsx! { - + "Hello there, my name is Bert" - /*{messages.iter().map(|message| rsx! { - {text!("{}", message)} - })}*/ + {messages.iter().map(|message| rsx! { + {text!("{}", message)} + })} - // - // - // - // + + + }) @@ -88,6 +87,7 @@ fn main() { } message { width: 500; height: 100; background-color: yellow; color: black; } + text { width: 500; height: 100; background-color: blue; color: white; } boxxx { background-color: rgba(245, 217, 28, .8); diff --git a/lifecycle/src/lib.rs b/lifecycle/src/lib.rs index 8fdc032..17b44a5 100644 --- a/lifecycle/src/lib.rs +++ b/lifecycle/src/lib.rs @@ -26,3 +26,13 @@ pub use reconciler::key::ComponentKey; lazy_static! { pub static ref RENDER_ENGINE: RenderEngine = RenderEngine::new(); } + +#[macro_export] +macro_rules! text { + ($t:expr) => { + alchemy::RSX::text($t) + }; + ($format:tt, $($tail:expr),*) => { + alchemy::RSX::text(format!($format, $($tail),*)) + }; +} diff --git a/macros/src/lib.rs b/macros/src/lib.rs index c738691..60e31e0 100644 --- a/macros/src/lib.rs +++ b/macros/src/lib.rs @@ -90,13 +90,13 @@ pub fn writable_props_derive(input: TokenStream) -> TokenStream { TokenStream::from(quote! { impl #impl_generics #name #ty_generics #where_clause { - fn default_props() -> #name_props { + pub fn default_props() -> #name_props { #name_props::default() } } impl #impl_generics alchemy::ComponentProps for #name #ty_generics #where_clause { - fn set_props(&mut self, new_props: &mut Any) { + fn set_props(&mut self, new_props: &mut std::any::Any) { match new_props.downcast_ref::<#name_props>() { Some(props) => { }, None => { panic!("Woah there, somehow the wrong props were being passed!"); } diff --git a/styles/src/spacedset.rs b/styles/src/spacedset.rs index 8096b56..115f8ef 100644 --- a/styles/src/spacedset.rs +++ b/styles/src/spacedset.rs @@ -121,6 +121,21 @@ where } } +impl<'a, A: Ord + FromStr> From<&Vec<&'a str>> for SpacedSet +where + ::Err: Debug, +{ + fn from(s: &Vec<&'a str>) -> Self { + let mut list = Self::new(); + + for key in s { + list.insert(FromStr::from_str(key).unwrap()); + } + + list + } +} + impl<'a, 'b, A: Ord + FromStr> From<(&'a str, &'b str)> for SpacedSet where ::Err: Debug,