diff --git a/src/widget.rs b/src/widget.rs index 4d4cd20..984dda5 100644 --- a/src/widget.rs +++ b/src/widget.rs @@ -66,19 +66,22 @@ pub trait Component: Send + Sync { pub struct WidgetLoad(BoxedElement, HashMap); /// A widget with fixed content and no dynamic behavior. -pub struct StaticWidget(Mutex, Mutex>); +pub struct StaticWidget { + element: Mutex, + components: Mutex>, +} /// A widget with dynamic content and dependency tracking. /// /// This widget supports reactive updates and can be rebuilt using /// a provided `FnMut()` function when dependencies change. -pub struct DynWidget( - Mutex, - Mutex>, - Mutex WidgetLoad + Send + Sync>>, - Mutex>>, - Mutex WidgetLoad + Send + Sync>>>, -); +pub struct DynWidget { + element: Mutex, + components: Mutex>, + load: Mutex WidgetLoad + Send + Sync>>, + dependencies: Mutex>>, + injection: Mutex WidgetLoad + Send + Sync>>>, +} /// A reference-counted wrapper around either a static or dynamic widget. /// @@ -117,18 +120,11 @@ impl WidgetLoad { impl Widget { pub fn new_static(e: BoxedElement) -> Self { - Self::Static(StaticWidget(Mutex::new(e), Mutex::new(HashMap::new()))) + Self::Static(StaticWidget::new(e)) } - pub fn new_dyn WidgetLoad + 'static + Send + Sync>(mut e: F) -> Self { - let wl = e(); - Self::Dynamic(DynWidget( - Mutex::new(wl.0), - Mutex::new(wl.1), - Mutex::new(Box::new(e)), - Mutex::new(Vec::new()), - Mutex::new(None), - )) + pub fn new_dyn WidgetLoad + 'static + Send + Sync>(e: F) -> Self { + Self::Dynamic(DynWidget::new(e)) } } @@ -188,7 +184,7 @@ impl Widget { } Widget::Static(w) => { let wl = WidgetLoad::new(String::new()); - w.1.lock().unwrap().extend(f(wl).1); + w.components.lock().unwrap().extend(f(wl).1); } } } @@ -250,17 +246,20 @@ impl Widget { impl StaticWidget { fn after_render(&self) {} fn get_elem(&self) -> MutexGuard { - self.0.lock().unwrap() + self.element.lock().unwrap() } } impl StaticWidget { pub fn new(e: BoxedElement) -> Self { - Self(Mutex::new(e), Mutex::new(HashMap::new())) + Self { + element: Mutex::new(e), + components: Mutex::new(HashMap::new()), + } } pub fn component(&self, c: C) { - self.1 + self.components .lock() .unwrap() .entry(c.type_id()) @@ -268,11 +267,14 @@ impl StaticWidget { } pub fn set_component(&self, c: C) { - self.1.lock().unwrap().insert(c.type_id(), Box::new(c)); + self.components + .lock() + .unwrap() + .insert(c.type_id(), Box::new(c)); } pub fn get(&self) -> Option { - self.1 + self.components .lock() .unwrap() .get(&TypeId::of::()) @@ -284,43 +286,43 @@ impl StaticWidget { impl DynWidget { fn after_render(&self) {} fn get_elem(&self) -> MutexGuard { - self.0.lock().unwrap() + self.element.lock().unwrap() } } impl DynWidget { pub fn new WidgetLoad + 'static + Send + Sync>(mut e: F) -> Self { let wl = e(); - Self( - Mutex::new(wl.0), - Mutex::new(wl.1), - Mutex::new(Box::new(e)), - Mutex::new(Vec::new()), - Mutex::new(None), - ) + Self { + element: Mutex::new(wl.0), + components: Mutex::new(wl.1), + load: Mutex::new(Box::new(e)), + dependencies: Mutex::new(Vec::new()), + injection: Mutex::new(None), + } } /// Replace or modify the widget's structure on reload and init. pub fn inject WidgetLoad + 'static + Send + Sync>(&self, f: F) { - *self.4.lock().unwrap() = Some(Box::new(f)); + *self.injection.lock().unwrap() = Some(Box::new(f)); self.refresh(); } /// Rebuild the widget's content by re-evaluating the original function. pub fn refresh(&self) { - let mut w = (self.2.lock().unwrap())(); + let mut w = (self.load.lock().unwrap())(); - if let Some(we) = &mut *self.4.lock().unwrap() { + if let Some(we) = &mut *self.injection.lock().unwrap() { w = (we)(w); } - *self.0.lock().unwrap() = w.0; - *self.1.lock().unwrap() = w.1; + *self.element.lock().unwrap() = w.0; + *self.components.lock().unwrap() = w.1; } /// Re-evaluates the widget if any dependency has changed. pub fn auto_refresh(&self) { - for d in self.3.lock().unwrap().iter() { + for d in self.dependencies.lock().unwrap().iter() { if d.check() { self.refresh(); } @@ -330,17 +332,17 @@ impl DynWidget { /// Adds a dependency to this widget. pub fn dependency(&self, d: D) { d.add(); - self.3.lock().unwrap().push(Box::new(d)); + self.dependencies.lock().unwrap().push(Box::new(d)); } /// Adds a boxed dependency. pub fn dependency_box(&self, d: Box) { d.add(); - self.3.lock().unwrap().push(d); + self.dependencies.lock().unwrap().push(d); } pub fn component(&self, c: C) { - self.1 + self.components .lock() .unwrap() .entry(c.type_id()) @@ -348,11 +350,14 @@ impl DynWidget { } pub fn set_component(&self, c: C) { - self.1.lock().unwrap().insert(c.type_id(), Box::new(c)); + self.components + .lock() + .unwrap() + .insert(c.type_id(), Box::new(c)); } pub fn get(&self) -> Option { - self.1 + self.components .lock() .unwrap() .get(&TypeId::of::())