Update example to show props deriving, text macro, etc

This commit is contained in:
Ryan McGrath 2019-06-05 21:49:41 -07:00
parent 290b57d336
commit 241cae3c93
No known key found for this signature in database
GPG key ID: 811674B62B666830
7 changed files with 50 additions and 24 deletions

View file

@ -23,7 +23,7 @@ pub struct FragmentProps;
pub struct Fragment; pub struct Fragment;
impl Fragment { impl Fragment {
fn default_props() -> FragmentProps { pub fn default_props() -> FragmentProps {
FragmentProps {} FragmentProps {}
} }
} }

View file

@ -5,7 +5,7 @@
use std::sync::Mutex; use std::sync::Mutex;
use alchemy_styles::{Appearance, Layout, StylesList}; use alchemy_styles::{Appearance, Layout};
use alchemy_lifecycle::ComponentKey; use alchemy_lifecycle::ComponentKey;
use alchemy_lifecycle::error::Error; use alchemy_lifecycle::error::Error;

View file

@ -9,9 +9,9 @@ use std::sync::Arc;
pub use lazy_static::lazy_static; pub use lazy_static::lazy_static;
use proc_macro_hack::proc_macro_hack; use proc_macro_hack::proc_macro_hack;
pub use alchemy_lifecycle::ComponentKey; pub use alchemy_lifecycle::{ComponentKey, text};
pub use alchemy_lifecycle::traits::{ pub use alchemy_lifecycle::traits::{
AppDelegate, Component, Props, WindowDelegate AppDelegate, Component, Props as ComponentProps, WindowDelegate
}; };
pub use alchemy_lifecycle::error::Error; pub use alchemy_lifecycle::error::Error;
@ -24,6 +24,7 @@ pub use alchemy_macros::rsx;
#[proc_macro_hack] #[proc_macro_hack]
pub use alchemy_macros::styles; pub use alchemy_macros::styles;
pub use alchemy_macros::Props;
pub use alchemy_styles::{Color, styles as style_attributes, SpacedSet, StyleSheet, StylesList}; pub use alchemy_styles::{Color, styles as style_attributes, SpacedSet, StyleSheet, StylesList};

View file

@ -8,7 +8,7 @@
/// @created March 26th, 2019 /// @created March 26th, 2019
use alchemy::{ 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 Text, View, Window, WindowDelegate
}; };
@ -24,26 +24,26 @@ impl AppDelegate for AppState {
} }
} }
/*#[derive(Default)] #[derive(Default)]
pub struct Banner; struct BannerProps {}
#[derive(Props)]
struct Banner;
impl Component for Banner { impl Component for Banner {
type Props = Box<()>; fn new(_key: ComponentKey) -> Banner {
type State = Box<()>;
fn constructor(_key: ComponentKey) -> Banner {
Banner {} Banner {}
} }
fn render(&self, props: &Self::Props) -> Result<RSX, Error> { fn render(&self, children: Vec<RSX>) -> Result<RSX, Error> {
Ok(rsx! { Ok(rsx! {
<Fragment> <Fragment>
<View styles=["wut1"]></View> <View styles=["wut1"]></View>
//{props.children.clone()} {children}
</Fragment> </Fragment>
}) })
} }
}*/ }
pub struct WindowState; pub struct WindowState;
@ -55,17 +55,16 @@ impl WindowDelegate for WindowState {
fn render(&self) -> Result<RSX, Error> { fn render(&self) -> Result<RSX, Error> {
let messages = vec!["LOL"]; //, "wut", "BERT"]; let messages = vec!["LOL"]; //, "wut", "BERT"];
Ok(rsx! { Ok(rsx! {
<View styles={messages}> <View styles={&messages}>
<Text styles=["message"]>"Hello there, my name is Bert"</Text> <Text styles=["message"]>"Hello there, my name is Bert"</Text>
<View styles=["boxxx"] /> <View styles=["boxxx"] />
/*{messages.iter().map(|message| rsx! { {messages.iter().map(|message| rsx! {
<View>{text!("{}", message)}</View> <Text styles=["text"]>{text!("{}", message)}</Text>
})}*/ })}
<View styles=["box1"]> <View styles=["box1"]>
//<View styles=["box1"]></View> <Banner>
//<Banner> <View styles=["innermostBox"] />
//<View styles=["innermostBox"] /> </Banner>
//</Banner>
</View> </View>
</View> </View>
}) })
@ -88,6 +87,7 @@ fn main() {
} }
message { width: 500; height: 100; background-color: yellow; color: black; } message { width: 500; height: 100; background-color: yellow; color: black; }
text { width: 500; height: 100; background-color: blue; color: white; }
boxxx { boxxx {
background-color: rgba(245, 217, 28, .8); background-color: rgba(245, 217, 28, .8);

View file

@ -26,3 +26,13 @@ pub use reconciler::key::ComponentKey;
lazy_static! { lazy_static! {
pub static ref RENDER_ENGINE: RenderEngine = RenderEngine::new(); 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),*))
};
}

View file

@ -90,13 +90,13 @@ pub fn writable_props_derive(input: TokenStream) -> TokenStream {
TokenStream::from(quote! { TokenStream::from(quote! {
impl #impl_generics #name #ty_generics #where_clause { impl #impl_generics #name #ty_generics #where_clause {
fn default_props() -> #name_props { pub fn default_props() -> #name_props {
#name_props::default() #name_props::default()
} }
} }
impl #impl_generics alchemy::ComponentProps for #name #ty_generics #where_clause { 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>() { match new_props.downcast_ref::<#name_props>() {
Some(props) => { }, Some(props) => { },
None => { panic!("Woah there, somehow the wrong props were being passed!"); } None => { panic!("Woah there, somehow the wrong props were being passed!"); }

View file

@ -121,6 +121,21 @@ where
} }
} }
impl<'a, A: Ord + FromStr> From<&Vec<&'a str>> for SpacedSet<A>
where
<A as FromStr>::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<A> impl<'a, 'b, A: Ord + FromStr> From<(&'a str, &'b str)> for SpacedSet<A>
where where
<A as FromStr>::Err: Debug, <A as FromStr>::Err: Debug,