state management as a child

This commit is contained in:
2025-07-29 17:51:03 +02:00
parent de9039dbaf
commit db66803347
5 changed files with 19 additions and 4 deletions
+5 -1
View File
@@ -1,6 +1,9 @@
use std::sync::{Arc, Mutex}; use std::sync::{Arc, Mutex};
use crate::widget::{Element, Widget}; use crate::{
widget::{Element, Widget},
NoRender,
};
pub struct Div { pub struct Div {
color: u32, color: u32,
@@ -40,6 +43,7 @@ impl Element for Arc<Div> {
fn draw_child(&self, element: &Arc<Widget>) { fn draw_child(&self, element: &Arc<Widget>) {
self.children.lock().unwrap().push(element.clone()); self.children.lock().unwrap().push(element.clone());
element.inject(|w| w.component(NoRender));
} }
fn as_any(&self) -> &dyn std::any::Any { fn as_any(&self) -> &dyn std::any::Any {
+3 -1
View File
@@ -1,6 +1,6 @@
use std::sync::{Arc, Mutex}; use std::sync::{Arc, Mutex};
use crate::widget::{Element, Widget}; use crate::{widget::{Element, Widget}, NoRender};
pub struct FlexRow { pub struct FlexRow {
color: u32, color: u32,
@@ -55,6 +55,7 @@ impl Element for FlexRow {
fn draw_child(&self, element: &Arc<Widget>) { fn draw_child(&self, element: &Arc<Widget>) {
self.children.lock().unwrap().push(element.clone()); self.children.lock().unwrap().push(element.clone());
element.inject(|w| w.component(NoRender));
} }
fn as_any(&self) -> &dyn std::any::Any { fn as_any(&self) -> &dyn std::any::Any {
@@ -116,6 +117,7 @@ impl Element for FlexCol {
fn draw_child(&self, element: &Arc<Widget>) { fn draw_child(&self, element: &Arc<Widget>) {
self.children.lock().unwrap().push(element.clone()); self.children.lock().unwrap().push(element.clone());
element.inject(|w| w.component(NoRender));
} }
fn as_any(&self) -> &dyn std::any::Any { fn as_any(&self) -> &dyn std::any::Any {
+1 -1
View File
@@ -26,7 +26,7 @@ impl Rsx {
match rsx_elem { match rsx_elem {
RsxElement::Element(f, dep, child) => { RsxElement::Element(f, dep, child) => {
let w = if let Some(parent) = &parent { let w = if let Some(parent) = &parent {
let w = Arc::new(Widget::new(Box::new(f))); let w = screen.draw_box(f);
parent.0.lock().unwrap().draw_child(&w); parent.0.lock().unwrap().draw_child(&w);
w w
} else { } else {
+9
View File
@@ -22,6 +22,7 @@ pub struct Screen {
} }
event!(RenderWrapperEvent(*mut RenderScope)); event!(RenderWrapperEvent(*mut RenderScope));
component!(NoRender);
impl RenderWrapperEvent { impl RenderWrapperEvent {
pub fn get_scope(&self) -> &mut RenderScope { pub fn get_scope(&self) -> &mut RenderScope {
@@ -85,6 +86,14 @@ impl Screen {
utils::clear()?; utils::clear()?;
for elem in self.widgets.lock().unwrap().iter() { for elem in self.widgets.lock().unwrap().iter() {
if let Some(NoRender) = elem.get() {
for ext in self.extensions.lock().unwrap().iter() {
ext.lock().unwrap().render_widget(&mut scope, elem);
}
elem.auto_refresh();
continue;
}
if let Some(wrapper) = elem.get::<Handler<RenderWrapperEvent>>() { if let Some(wrapper) = elem.get::<Handler<RenderWrapperEvent>>() {
wrapper.call(elem, &RenderWrapperEvent(&mut scope)); wrapper.call(elem, &RenderWrapperEvent(&mut scope));
} else { } else {
+1 -1
View File
@@ -31,7 +31,7 @@ fn test(inc: i32) -> Rsx {
std::thread::spawn({ std::thread::spawn({
let count = count.clone(); let count = count.clone();
move || loop { move || loop {
std::thread::sleep(std::time::Duration::from_millis(300)); std::thread::sleep(std::time::Duration::from_millis(50));
**count.get() += inc; **count.get() += inc;
} }
}); });