diff --git a/examples/text_box.rs b/examples/text_box.rs index 5a91981bc71fdc08ae84097706df3cfff02f77fe..bbf01ecf85817d091a3c009443266ac9daf0efab 100644 --- a/examples/text_box.rs +++ b/examples/text_box.rs @@ -38,8 +38,8 @@ fn TextBoxExample(context: &mut KayakContext) { rsx! { <> <Window position={(50.0, 50.0)} size={(300.0, 300.0)} title={"TextBox Example".to_string()}> - <TextBox styles={Some(input_styles)} value={current_value} on_change={Some(on_change)} placeholder={None as Option<String>} /> - <TextBox styles={Some(input_styles)} value={current_value2} on_change={Some(on_change2)} placeholder={None as Option<String>} /> + <TextBox styles={Some(input_styles)} value={current_value} on_change={Some(on_change)} /> + <TextBox styles={Some(input_styles)} value={current_value2} on_change={Some(on_change2)} /> </Window> </> } diff --git a/examples/todo/todo.rs b/examples/todo/todo.rs index 6685015d2e08a097b7224f17a2fd5242c49c05b5..4e16a2194ad2f4dafe63ccb95278996ad74ae6b6 100644 --- a/examples/todo/todo.rs +++ b/examples/todo/todo.rs @@ -79,11 +79,15 @@ fn TodoApp() { cloned_set_todos(todos_cloned.clone()); }); - let placeholder = Some("Type here to add a new todo!".to_string()); rsx! { <Window position={(415.0, 50.0)} size={(450.0, 600.0)} title={"Todo!".to_string()}> <Element styles={Some(top_area_styles)}> - <TextBox styles={Some(text_box_styles)} value={new_todo_value} placeholder={placeholder} on_change={Some(on_change)} /> + <TextBox + styles={Some(text_box_styles)} + value={new_todo_value} + placeholder={Some("Type here to add a new todo!".to_string())} + on_change={Some(on_change)} + /> <AddButton on_event={Some(add_events)} /> </Element> <Cards cards={todos} on_delete={handle_delete} /> diff --git a/kayak_core/src/fragment.rs b/kayak_core/src/fragment.rs index 9829b5795cd278ef88130280b5c94fcc233a6be3..905f16beee0507224f813c454b85a67b25f2a2d4 100644 --- a/kayak_core/src/fragment.rs +++ b/kayak_core/src/fragment.rs @@ -3,13 +3,14 @@ use derivative::*; use crate::{context::KayakContext, styles::Style, Index, Widget}; #[derive(Derivative)] -#[derivative(Debug, PartialEq)] +#[derivative(Default, Debug, PartialEq)] pub struct Fragment { pub id: Index, + #[derivative(Default(value = "None"))] pub styles: Option<Style>, - #[derivative(Debug = "ignore", PartialEq = "ignore")] + #[derivative(Default(value = "None"), Debug = "ignore", PartialEq = "ignore")] pub children: crate::Children, - #[derivative(Debug = "ignore", PartialEq = "ignore")] + #[derivative(Default(value = "None"), Debug = "ignore", PartialEq = "ignore")] pub on_event: Option<crate::OnEvent>, } diff --git a/kayak_core/src/lib.rs b/kayak_core/src/lib.rs index 0a12999c89e805d03b68373fe2c7b48d559928b6..9aa48f8af93bd76cbe20f455ecf08a0e3a996a20 100644 --- a/kayak_core/src/lib.rs +++ b/kayak_core/src/lib.rs @@ -58,6 +58,12 @@ impl OnEvent { #[derive(Clone)] pub struct Handler<T>(pub Arc<RwLock<dyn FnMut(T) + Send + Sync + 'static>>); +impl<T> Default for Handler<T> { + fn default() -> Self { + Self(Arc::new(RwLock::new(|_| {}))) + } +} + impl<T> Handler<T> { pub fn new<F: FnMut(T) + Send + Sync + 'static>(f: F) -> Handler<T> { Handler(Arc::new(RwLock::new(f))) diff --git a/kayak_render_macros/examples/main.rs b/kayak_render_macros/examples/main.rs index a1885744fd701c21db9af4aabd1984e11dff71e5..9b054892d443059f1da91631fa285e66719645e6 100644 --- a/kayak_render_macros/examples/main.rs +++ b/kayak_render_macros/examples/main.rs @@ -1,17 +1,21 @@ use kayak_core::{context::KayakContext, styles::Style, Children, Index}; -use kayak_core::{derivative::*, Fragment, Widget}; +use kayak_core::{ + derivative::{self, *}, + Fragment, Widget, +}; use kayak_render_macros::rsx; #[derive(Derivative)] -#[derivative(Debug, PartialEq)] +#[derivative(Default, Debug, PartialEq)] #[allow(dead_code)] struct Test { id: Index, + #[derivative(Default(value = "None"))] styles: Option<Style>, foo: u32, - #[derivative(Debug = "ignore", PartialEq = "ignore")] + #[derivative(Debug = "ignore", PartialEq = "ignore", Default(value = "None"))] children: Children, - #[derivative(Debug = "ignore", PartialEq = "ignore")] + #[derivative(Debug = "ignore", PartialEq = "ignore", Default(value = "None"))] pub on_event: Option<kayak_core::OnEvent>, } diff --git a/kayak_render_macros/src/function_component.rs b/kayak_render_macros/src/function_component.rs index 58c4e29fce0e8144e2e8739517a80da1447683da..703f54b45b7115fa9a68a5b5f8bd21807591ad7a 100644 --- a/kayak_render_macros/src/function_component.rs +++ b/kayak_render_macros/src/function_component.rs @@ -87,13 +87,14 @@ pub fn create_function_widget(f: syn::ItemFn, widget_arguments: WidgetArguments) "styles : Option< kayak_ui :: core :: styles :: Style >", ], quote! { + #[derivative(Default(value="None"))] pub styles: Option<#kayak_core::styles::Style> }, ), ( vec!["children : Children"], quote! { - #[derivative(Debug = "ignore", PartialEq = "ignore")] + #[derivative(Default(value="None"), Debug = "ignore", PartialEq = "ignore")] pub children: #kayak_core::Children }, ), @@ -104,7 +105,7 @@ pub fn create_function_widget(f: syn::ItemFn, widget_arguments: WidgetArguments) "on_event : Option <\nkayak_ui :: core :: OnEvent >", ], quote! { - #[derivative(Debug = "ignore", PartialEq = "ignore")] + #[derivative(Default(value="None"), Debug = "ignore", PartialEq = "ignore")] pub on_event: Option<#kayak_core::OnEvent> }, ), @@ -149,7 +150,7 @@ pub fn create_function_widget(f: syn::ItemFn, widget_arguments: WidgetArguments) use #kayak_core::derivative::*; #[derive(Derivative)] - #[derivative(Debug, PartialEq, Clone)] + #[derivative(Default, Debug, PartialEq, Clone)] #vis struct #struct_name #impl_generics { pub id: #kayak_core::Index, #inputs_block diff --git a/kayak_render_macros/src/widget_attributes.rs b/kayak_render_macros/src/widget_attributes.rs index d204fc8c8fa5a8679a00a3780b9b42b7d0b8ad80..0abda2b1932500d7f2126d805713f9f1b42d9d20 100644 --- a/kayak_render_macros/src/widget_attributes.rs +++ b/kayak_render_macros/src/widget_attributes.rs @@ -107,22 +107,22 @@ impl<'a, 'c> ToTokens for CustomWidgetAttributes<'a, 'c> { } } - #[cfg(feature = "internal")] - let kayak_core = quote! { kayak_core }; - #[cfg(not(feature = "internal"))] - let kayak_core = quote! { kayak_ui::core }; + // #[cfg(feature = "internal")] + // let kayak_core = quote! { kayak_core }; + // #[cfg(not(feature = "internal"))] + // let kayak_core = quote! { kayak_ui::core }; let quoted = if attrs.len() == 0 { - quote!({ id: #kayak_core::Index::default(), styles: None, children: None, on_event: None, }) + quote!({ ..Default::default() }) } else { if !self .attributes .iter() .any(|attribute| attribute.ident().to_token_stream().to_string() == "styles") { - quote!({ #(#attrs),*, id: #kayak_core::Index::default() }) + quote!({ #(#attrs),*, ..Default::default() }) } else { - quote!({ #(#attrs),*, id: #kayak_core::Index::default() }) + quote!({ #(#attrs),*, ..Default::default() }) } };