diff --git a/README.md b/README.md index 39bf71a213c3ae98b3c62cbd02abd1b885706e7c..446d7e121141daad3f02c14b98b14fc144dbd9f7 100644 --- a/README.md +++ b/README.md @@ -88,7 +88,7 @@ Widget's can create their own state and will re-render when that state changes. ```rust #[widget] fn Counter(context: &mut KayakContext) { - let (count, set_count) = use_state!(0i32); + let (count, set_count, ..) = use_state!(0i32); let on_event = OnEvent::new(move |_, event| match event.event_type { EventType::Click => set_count(count + 1), _ => {} diff --git a/examples/counter.rs b/examples/counter.rs index 4c829c4893365ba250d766525bc69c8f3d2858af..bcff8980289c7529baa0cbc3f1eecd634f731822 100644 --- a/examples/counter.rs +++ b/examples/counter.rs @@ -33,7 +33,7 @@ fn Counter(context: &mut KayakContext) { ..Default::default() }; - let (count, set_count) = use_state!(0i32); + let (count, set_count, ..) = use_state!(0i32); let on_event = OnEvent::new(move |_, event| match event.event_type { EventType::Click => set_count(count + 1), _ => {} diff --git a/examples/hooks.rs b/examples/hooks.rs index 38f5140eb4964367c91f6c7c927f19d6b57db018..0b994bfe0acce02d7840207d43ddcd93982ccd6e 100644 --- a/examples/hooks.rs +++ b/examples/hooks.rs @@ -16,7 +16,7 @@ use bevy::{ }; use kayak_ui::{ bevy::{BevyContext, BevyKayakUIPlugin, FontMapping, UICameraBundle}, - core::{EventType, Index, OnEvent, render, rsx, use_effect, use_raw_state, use_state, widget}, + core::{EventType, Index, OnEvent, render, rsx, use_effect, use_state, widget}, widgets::{App, Button, Text, Window}, }; @@ -32,7 +32,7 @@ fn StateCounter() { // Here, we create a state with an initial value of 0. Right now the value of `count` is 0. If we call `set_count(10)`, // then the new value of `count` will be 10. - let (count, set_count) = use_state!(0); + let (count, set_count, ..) = use_state!(0); // We can create an event callback that uodates the state using the state variables defined above. // Keep the borrow checker in mind! We can pass both `count` and `set_count` to this closure because they @@ -61,15 +61,15 @@ fn EffectCounter() { // In our case, we want to create a side-effect that updates a counter when another state is updated. // In order to create this side-effect, we need access to the raw state binding. This is easily done by using - // the `use_raw_state` macro in place of the regular `use_state` one. - let (count, set_count, raw_count) = use_raw_state!(0); + // the third field in the tuple returned from the `use_state` macro. + let (count, set_count, raw_count) = use_state!(0); let on_event = OnEvent::new(move |_, event| match event.event_type { EventType::Click => set_count(count + 1), _ => {} }); // This is the state our side-effect will update in response to changes on `raw_count`. - let (effect_count, set_effect_count) = use_state!(0); + let (effect_count, set_effect_count, ..) = use_state!(0); // This hook defines a side-effect that calls a function only when one of its dependencies is updated. // They will also always run upon first render (i.e., when the widget is first added to the layout). diff --git a/examples/todo/add_button.rs b/examples/todo/add_button.rs index 3aeec1229d3d9ff2b4921300cad97aaffe60b6b4..d198a92d40802661f198ca2db76949408085e3f4 100644 --- a/examples/todo/add_button.rs +++ b/examples/todo/add_button.rs @@ -10,7 +10,7 @@ use kayak_ui::widgets::{Background, Text}; #[widget] pub fn AddButton(children: Children, styles: Option<Style>) { - let (color, set_color) = use_state!(Color::new(0.0781, 0.0898, 0.101, 1.0)); + let (color, set_color, ..) = use_state!(Color::new(0.0781, 0.0898, 0.101, 1.0)); let base_styles = styles.clone().unwrap_or_default(); *styles = Some(Style { diff --git a/examples/todo/delete_button.rs b/examples/todo/delete_button.rs index 3720ba4872cbec95583f8ac0520861a872c2eba2..4c8f064c02d8804b1b372ce0643a56781cf240c7 100644 --- a/examples/todo/delete_button.rs +++ b/examples/todo/delete_button.rs @@ -10,7 +10,7 @@ use kayak_ui::widgets::{Background, Text}; #[widget] pub fn DeleteButton(children: Children, styles: Option<Style>) { - let (color, set_color) = use_state!(Color::new(0.0781, 0.0898, 0.101, 1.0)); + let (color, set_color, ..) = use_state!(Color::new(0.0781, 0.0898, 0.101, 1.0)); let base_styles = styles.clone().unwrap_or_default(); *styles = Some(Style { diff --git a/examples/todo/todo.rs b/examples/todo/todo.rs index de0c5943c949be051c0fbabee273e311ce2ae7ec..2b82498c99a99cb63c583657bfa95d733d66037d 100644 --- a/examples/todo/todo.rs +++ b/examples/todo/todo.rs @@ -25,7 +25,7 @@ pub struct Todo { #[widget] fn TodoApp() { - let (todos, set_todos) = use_state!(vec![ + let (todos, set_todos, ..) = use_state!(vec![ Todo { name: "Use bevy to make a game!".to_string(), }, @@ -37,7 +37,7 @@ fn TodoApp() { }, ]); - let (new_todo_value, set_new_todo_value) = use_state!("".to_string()); + let (new_todo_value, set_new_todo_value, ..) = use_state!("".to_string()); let text_box_styles = Style { right: StyleProp::Value(Units::Pixels(10.0)), diff --git a/kayak_render_macros/src/lib.rs b/kayak_render_macros/src/lib.rs index 8e851859101b10cf7b95cbd89d901a631cf66fba..9706adc8d64fb2869570b1e500b2d156154b0137 100644 --- a/kayak_render_macros/src/lib.rs +++ b/kayak_render_macros/src/lib.rs @@ -114,24 +114,27 @@ pub fn dyn_partial_eq(_: TokenStream, input: TokenStream) -> TokenStream { .into() } -/// Create a state and its setter +/// Register some state data with an initial value. +/// +/// Once the state is created, this macro returns the current value, a closure for updating the current value, and +/// the raw Binding in a tuple. /// /// For more details, check out [React's documentation](https://reactjs.org/docs/hooks-state.html), /// upon which this macro is based. /// /// # Arguments /// -/// * `initial_state`: The expression +/// * `initial_state`: The initial value for the state /// -/// returns: (state, set_state) +/// returns: (state, set_state, state_binding) /// /// # Examples /// /// ``` /// # use kayak_core::{EventType, OnEvent}; -/// use kayak_render_macros::use_state; +/// # use kayak_render_macros::use_state; /// -/// let (count, set_count) = use_state!(0); +/// let (count, set_count, ..) = use_state!(0); /// /// let on_event = OnEvent::new(move |_, event| match event.event_type { /// EventType::Click => { @@ -150,20 +153,6 @@ pub fn dyn_partial_eq(_: TokenStream, input: TokenStream) -> TokenStream { /// ``` #[proc_macro] pub fn use_state(initial_state: TokenStream) -> TokenStream { - let state: proc_macro2::TokenStream = use_raw_state(initial_state).into(); - let result = quote! {{ - let (state, set_state, ..) = #state; - (state, set_state) - }}; - TokenStream::from(result) -} - -/// This macro works exactly like its [use_state] counterpart, except that it also returns -/// the raw state Binding as the third field in the tuple. -/// -/// returns: (state, set_state, state_binding) -#[proc_macro] -pub fn use_raw_state(initial_state: TokenStream) -> TokenStream { let initial_state = parse_macro_input!(initial_state as syn::Expr); let result = quote! {{ use kayak_core::{Bound, MutableBound}; @@ -204,9 +193,9 @@ pub fn use_raw_state(initial_state: TokenStream) -> TokenStream { /// /// ``` /// # use kayak_core::{EventType, OnEvent}; -/// use kayak_render_macros::{use_effect, use_raw_state}; +/// # use kayak_render_macros::{use_effect, use_state}; /// -/// let (count, set_count, count_state) = use_raw_state!(0); +/// let (count, set_count, count_state) = use_state!(0); /// /// use_effect!(move || { /// println!("Count: {}", count_state.get()); diff --git a/src/lib.rs b/src/lib.rs index 96421c781185399bfcfc5b3bfba51acf9d43916d..2cbf229107c260d594f0a8eee600b7ac697bb2e4 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,6 +1,6 @@ pub mod core { pub use kayak_core::*; - pub use kayak_render_macros::{constructor, render, rsx, use_effect, use_raw_state, use_state, widget}; + pub use kayak_render_macros::{constructor, render, rsx, use_effect, use_state, widget}; } #[cfg(feature = "bevy_renderer")]