diff --git a/examples/text_box.rs b/examples/text_box.rs index 6cd17e64fdc91e8b7f5a33725ec34102fb1f6adc..bbf01ecf85817d091a3c009443266ac9daf0efab 100644 --- a/examples/text_box.rs +++ b/examples/text_box.rs @@ -13,7 +13,9 @@ use kayak_widgets::{App, OnChange, TextBox, Window}; #[widget] fn TextBoxExample(context: &mut KayakContext) { - let value = context.create_state("".to_string()).unwrap(); + let value = context + .create_state("I started with a value!".to_string()) + .unwrap(); let value2 = context.create_state("".to_string()).unwrap(); let input_styles = Style { diff --git a/kayak_core/src/binding.rs b/kayak_core/src/binding.rs index fbd4a31d61ed7487818052104d8811dc819f813b..c157376a6598dc482b22a69a49765e4e4e7b3ad3 100644 --- a/kayak_core/src/binding.rs +++ b/kayak_core/src/binding.rs @@ -5,20 +5,20 @@ pub use flo_binding::{bind, notify, Binding, Bound, Changeable, MutableBound, Re #[derive(Debug, Clone, Copy, PartialEq)] pub struct Debouncer { last_updated: Instant, - time: f32, + threshold: f32, } impl Debouncer { - pub fn new(time: f32) -> Self { + pub fn new(threshold: f32) -> Self { Self { - time, + threshold, last_updated: Instant::now(), } } pub fn should_update(&mut self) -> bool { let elapsed_time = self.last_updated.elapsed().as_secs_f32(); - if elapsed_time > self.time { + if elapsed_time > self.threshold { self.last_updated = Instant::now(); return true; diff --git a/kayak_core/src/context.rs b/kayak_core/src/context.rs index a39d73ac8ea35b66cb52b459863ffcab499eb08a..f5b94f775a3b27741b58ee700da884b3f5bbb0a1 100644 --- a/kayak_core/src/context.rs +++ b/kayak_core/src/context.rs @@ -11,10 +11,11 @@ pub struct KayakContext { global_bindings: HashMap<crate::Index, Vec<flo_binding::Uuid>>, widget_state_lifetimes: HashMap<crate::Index, HashMap<flo_binding::Uuid, Box<dyn crate::Releasable>>>, - pub current_id: Index, + current_id: Index, + // TODO: Make widget_manager private. pub widget_manager: WidgetManager, last_mouse_position: (f32, f32), - pub global_state: resources::Resources, + global_state: resources::Resources, previous_events: HashMap<Index, Option<EventType>>, current_focus: Index, last_focus: Index, diff --git a/kayak_core/src/lib.rs b/kayak_core/src/lib.rs index 95f4d48911f5eff51409157e1144924bed7b43a2..cee43bbb79bd66cd40f15f6387dc4fa3688511e1 100644 --- a/kayak_core/src/lib.rs +++ b/kayak_core/src/lib.rs @@ -7,6 +7,7 @@ pub(crate) mod generational_arena; mod input_event; mod keys; pub mod layout_cache; +mod multi_state; pub mod node; pub mod render_command; pub mod render_primitive; @@ -15,7 +16,6 @@ pub mod tree; mod vec; pub mod widget; pub mod widget_manager; -mod multi_state; use std::sync::{Arc, RwLock}; @@ -52,21 +52,6 @@ impl OnEvent { } } -// impl std::ops::Deref for OnEvent { -// type Target = -// Arc<RwLock<dyn FnMut(&mut crate::context::KayakContext, &mut Event) + Send + Sync>>; - -// fn deref(&self) -> &Self::Target { -// &self.0 -// } -// } - -// impl std::ops::DerefMut for OnEvent { -// fn deref_mut(&mut self) -> &mut Self::Target { -// &mut self.0 -// } -// } - pub mod derivative { pub use derivative::*; } diff --git a/kayak_core/src/render_command.rs b/kayak_core/src/render_command.rs index d0c4b5192f62a6ed5d40bd7e6e807bc165c94bfa..c8ebc253f84768b7f2cacc6d39a709ac1f32afa4 100644 --- a/kayak_core/src/render_command.rs +++ b/kayak_core/src/render_command.rs @@ -3,7 +3,6 @@ use crate::layout_cache::Space; #[derive(Debug, Clone, PartialEq)] pub enum RenderCommand { Empty, - Window, /// Represents a node that has no renderable object but contributes to the layout. Layout, Clip, diff --git a/kayak_core/src/render_primitive.rs b/kayak_core/src/render_primitive.rs index 732a8676c657ad9b86bbc6171c4383ab6eb830b8..0fa313e8f622c1b76796373d47ad55138815272b 100644 --- a/kayak_core/src/render_primitive.rs +++ b/kayak_core/src/render_primitive.rs @@ -58,7 +58,6 @@ impl From<&Style> for RenderPrimitive { }; match render_command { - RenderCommand::Window => Self::Empty, RenderCommand::Empty => Self::Empty, RenderCommand::Layout => Self::Empty, RenderCommand::Clip => Self::Clip { diff --git a/kayak_widgets/src/app.rs b/kayak_widgets/src/app.rs index a77f39dce391a987130c28dced02a2392750ea4f..b19bc645de6f26199f91d529447be7342124cdec 100644 --- a/kayak_widgets/src/app.rs +++ b/kayak_widgets/src/app.rs @@ -12,7 +12,7 @@ use crate::Clip; #[widget] pub fn App(children: Children) { *styles = Some(Style { - render_command: StyleProp::Value(RenderCommand::Window), + render_command: StyleProp::Value(RenderCommand::Layout), ..styles.clone().unwrap_or_default() }); diff --git a/kayak_widgets/src/text_box.rs b/kayak_widgets/src/text_box.rs index 8660e7ab34769ee198b0bedef5f224640b6779a8..0da96fe8645b40924699941a2d1b7de7eee9bae6 100644 --- a/kayak_widgets/src/text_box.rs +++ b/kayak_widgets/src/text_box.rs @@ -53,9 +53,9 @@ pub fn TextBox(value: String, on_change: Option<OnChange>) { ..styles.clone().unwrap_or_default() }; - let internal_value = context.create_state("".to_string()).unwrap(); let has_focus = context.create_state(Focus(false)).unwrap(); + let mut current_value = value.clone(); let cloned_on_change = on_change.clone(); let cloned_has_focus = has_focus.clone(); self.on_event = Some(OnEvent::new(move |_, event| match event.event_type { @@ -63,7 +63,6 @@ pub fn TextBox(value: String, on_change: Option<OnChange>) { if !cloned_has_focus.get().0 { return; } - let mut current_value = internal_value.get(); if c == '\u{8}' { if current_value.len() > 0 { current_value.truncate(current_value.len() - 1); @@ -78,22 +77,12 @@ pub fn TextBox(value: String, on_change: Option<OnChange>) { }); } } - internal_value.set(current_value); } EventType::Focus => cloned_has_focus.set(Focus(true)), EventType::Blur => cloned_has_focus.set(Focus(false)), _ => {} })); - // let cloned_has_focus = has_focus.clone(); - // let on_event = Some(OnEvent::new(move |_, event| match event.event_type { - // EventType::Focus => { - // dbg!("Has focus!"); - // cloned_has_focus.set(Focus(true)) - // } - // _ => {} - // })); - let value = value.clone(); rsx! { <Background styles={Some(background_styles)}>