From 323d5a5a57955c3b1e6390946b9cab355138f5d7 Mon Sep 17 00:00:00 2001 From: StarArawn <toasterthegamer@gmail.com> Date: Mon, 20 Dec 2021 18:18:19 -0500 Subject: [PATCH] Require default for every widget prop. --- examples/text_box.rs | 4 ++-- examples/todo/todo.rs | 8 ++++++-- kayak_core/src/fragment.rs | 7 ++++--- kayak_core/src/lib.rs | 6 ++++++ kayak_render_macros/examples/main.rs | 12 ++++++++---- kayak_render_macros/src/function_component.rs | 7 ++++--- kayak_render_macros/src/widget_attributes.rs | 14 +++++++------- 7 files changed, 37 insertions(+), 21 deletions(-) diff --git a/examples/text_box.rs b/examples/text_box.rs index 5a91981..bbf01ec 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 6685015..4e16a21 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 9829b57..905f16b 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 0a12999..9aa48f8 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 a188574..9b05489 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 58c4e29..703f54b 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 d204fc8..0abda2b 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() }) } }; -- GitLab