Skip to content
Snippets Groups Projects
Commit 323d5a5a authored by StarArawn's avatar StarArawn
Browse files

Require default for every widget prop.

parent d34d7b8e
No related branches found
No related tags found
No related merge requests found
...@@ -38,8 +38,8 @@ fn TextBoxExample(context: &mut KayakContext) { ...@@ -38,8 +38,8 @@ fn TextBoxExample(context: &mut KayakContext) {
rsx! { rsx! {
<> <>
<Window position={(50.0, 50.0)} size={(300.0, 300.0)} title={"TextBox Example".to_string()}> <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_value} on_change={Some(on_change)} />
<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_value2} on_change={Some(on_change2)} />
</Window> </Window>
</> </>
} }
......
...@@ -79,11 +79,15 @@ fn TodoApp() { ...@@ -79,11 +79,15 @@ fn TodoApp() {
cloned_set_todos(todos_cloned.clone()); cloned_set_todos(todos_cloned.clone());
}); });
let placeholder = Some("Type here to add a new todo!".to_string());
rsx! { rsx! {
<Window position={(415.0, 50.0)} size={(450.0, 600.0)} title={"Todo!".to_string()}> <Window position={(415.0, 50.0)} size={(450.0, 600.0)} title={"Todo!".to_string()}>
<Element styles={Some(top_area_styles)}> <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)} /> <AddButton on_event={Some(add_events)} />
</Element> </Element>
<Cards cards={todos} on_delete={handle_delete} /> <Cards cards={todos} on_delete={handle_delete} />
......
...@@ -3,13 +3,14 @@ use derivative::*; ...@@ -3,13 +3,14 @@ use derivative::*;
use crate::{context::KayakContext, styles::Style, Index, Widget}; use crate::{context::KayakContext, styles::Style, Index, Widget};
#[derive(Derivative)] #[derive(Derivative)]
#[derivative(Debug, PartialEq)] #[derivative(Default, Debug, PartialEq)]
pub struct Fragment { pub struct Fragment {
pub id: Index, pub id: Index,
#[derivative(Default(value = "None"))]
pub styles: Option<Style>, pub styles: Option<Style>,
#[derivative(Debug = "ignore", PartialEq = "ignore")] #[derivative(Default(value = "None"), Debug = "ignore", PartialEq = "ignore")]
pub children: crate::Children, pub children: crate::Children,
#[derivative(Debug = "ignore", PartialEq = "ignore")] #[derivative(Default(value = "None"), Debug = "ignore", PartialEq = "ignore")]
pub on_event: Option<crate::OnEvent>, pub on_event: Option<crate::OnEvent>,
} }
......
...@@ -58,6 +58,12 @@ impl OnEvent { ...@@ -58,6 +58,12 @@ impl OnEvent {
#[derive(Clone)] #[derive(Clone)]
pub struct Handler<T>(pub Arc<RwLock<dyn FnMut(T) + Send + Sync + 'static>>); 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> { impl<T> Handler<T> {
pub fn new<F: FnMut(T) + Send + Sync + 'static>(f: F) -> Handler<T> { pub fn new<F: FnMut(T) + Send + Sync + 'static>(f: F) -> Handler<T> {
Handler(Arc::new(RwLock::new(f))) Handler(Arc::new(RwLock::new(f)))
......
use kayak_core::{context::KayakContext, styles::Style, Children, Index}; 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; use kayak_render_macros::rsx;
#[derive(Derivative)] #[derive(Derivative)]
#[derivative(Debug, PartialEq)] #[derivative(Default, Debug, PartialEq)]
#[allow(dead_code)] #[allow(dead_code)]
struct Test { struct Test {
id: Index, id: Index,
#[derivative(Default(value = "None"))]
styles: Option<Style>, styles: Option<Style>,
foo: u32, foo: u32,
#[derivative(Debug = "ignore", PartialEq = "ignore")] #[derivative(Debug = "ignore", PartialEq = "ignore", Default(value = "None"))]
children: Children, children: Children,
#[derivative(Debug = "ignore", PartialEq = "ignore")] #[derivative(Debug = "ignore", PartialEq = "ignore", Default(value = "None"))]
pub on_event: Option<kayak_core::OnEvent>, pub on_event: Option<kayak_core::OnEvent>,
} }
......
...@@ -87,13 +87,14 @@ pub fn create_function_widget(f: syn::ItemFn, widget_arguments: WidgetArguments) ...@@ -87,13 +87,14 @@ pub fn create_function_widget(f: syn::ItemFn, widget_arguments: WidgetArguments)
"styles : Option< kayak_ui :: core :: styles :: Style >", "styles : Option< kayak_ui :: core :: styles :: Style >",
], ],
quote! { quote! {
#[derivative(Default(value="None"))]
pub styles: Option<#kayak_core::styles::Style> pub styles: Option<#kayak_core::styles::Style>
}, },
), ),
( (
vec!["children : Children"], vec!["children : Children"],
quote! { quote! {
#[derivative(Debug = "ignore", PartialEq = "ignore")] #[derivative(Default(value="None"), Debug = "ignore", PartialEq = "ignore")]
pub children: #kayak_core::Children pub children: #kayak_core::Children
}, },
), ),
...@@ -104,7 +105,7 @@ pub fn create_function_widget(f: syn::ItemFn, widget_arguments: WidgetArguments) ...@@ -104,7 +105,7 @@ pub fn create_function_widget(f: syn::ItemFn, widget_arguments: WidgetArguments)
"on_event : Option <\nkayak_ui :: core :: OnEvent >", "on_event : Option <\nkayak_ui :: core :: OnEvent >",
], ],
quote! { quote! {
#[derivative(Debug = "ignore", PartialEq = "ignore")] #[derivative(Default(value="None"), Debug = "ignore", PartialEq = "ignore")]
pub on_event: Option<#kayak_core::OnEvent> pub on_event: Option<#kayak_core::OnEvent>
}, },
), ),
...@@ -149,7 +150,7 @@ pub fn create_function_widget(f: syn::ItemFn, widget_arguments: WidgetArguments) ...@@ -149,7 +150,7 @@ pub fn create_function_widget(f: syn::ItemFn, widget_arguments: WidgetArguments)
use #kayak_core::derivative::*; use #kayak_core::derivative::*;
#[derive(Derivative)] #[derive(Derivative)]
#[derivative(Debug, PartialEq, Clone)] #[derivative(Default, Debug, PartialEq, Clone)]
#vis struct #struct_name #impl_generics { #vis struct #struct_name #impl_generics {
pub id: #kayak_core::Index, pub id: #kayak_core::Index,
#inputs_block #inputs_block
......
...@@ -107,22 +107,22 @@ impl<'a, 'c> ToTokens for CustomWidgetAttributes<'a, 'c> { ...@@ -107,22 +107,22 @@ impl<'a, 'c> ToTokens for CustomWidgetAttributes<'a, 'c> {
} }
} }
#[cfg(feature = "internal")] // #[cfg(feature = "internal")]
let kayak_core = quote! { kayak_core }; // let kayak_core = quote! { kayak_core };
#[cfg(not(feature = "internal"))] // #[cfg(not(feature = "internal"))]
let kayak_core = quote! { kayak_ui::core }; // let kayak_core = quote! { kayak_ui::core };
let quoted = if attrs.len() == 0 { let quoted = if attrs.len() == 0 {
quote!({ id: #kayak_core::Index::default(), styles: None, children: None, on_event: None, }) quote!({ ..Default::default() })
} else { } else {
if !self if !self
.attributes .attributes
.iter() .iter()
.any(|attribute| attribute.ident().to_token_stream().to_string() == "styles") .any(|attribute| attribute.ident().to_token_stream().to_string() == "styles")
{ {
quote!({ #(#attrs),*, id: #kayak_core::Index::default() }) quote!({ #(#attrs),*, ..Default::default() })
} else { } else {
quote!({ #(#attrs),*, id: #kayak_core::Index::default() }) quote!({ #(#attrs),*, ..Default::default() })
} }
}; };
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment