added variables

This commit is contained in:
2024-12-02 22:59:25 +01:00
parent 36744b9b3e
commit 21cd343eaa
4 changed files with 30 additions and 3 deletions
+8 -1
View File
@@ -4,11 +4,18 @@ pub fn examples() -> Element {
rsx! { rsx! {
styling: Some(styling()), styling: Some(styling()),
button { class: "btn", on_click: fn(_, _, document) { button { class: "btn", on_click: fn(btn: &mut Button, _, document) {
let count = document.use_state::<u32>("count");
*count += 1;
btn.children.set_text(&count.to_string());
if *count == 10 {
document.exit(); document.exit();
}
}, "Click me!" } }, "Click me!" }
text { "Welcome!" } text { "Welcome!" }
@count: i32;
} }
} }
+9
View File
@@ -154,6 +154,10 @@ impl Document {
pub fn draw(&mut self, element: &mut Element) { pub fn draw(&mut self, element: &mut Element) {
self.element = element; self.element = element;
} }
pub fn use_state<T>(&self, name: &str) -> &mut T {
&mut self.get_element_by_id::<crate::ui::DataHolder<T>>(name).unwrap().data
}
} }
impl Default for Children { impl Default for Children {
@@ -238,6 +242,11 @@ impl RenderWriter {
pub fn result(&self) -> RenderResult { pub fn result(&self) -> RenderResult {
RenderResult(self.w.clone(), self.style.get(self.focused).clone()) RenderResult(self.w.clone(), self.style.get(self.focused).clone())
} }
pub fn hidden(&mut self) -> RenderResult {
self.style.0.visible = false;
RenderResult(self.w.clone(), self.style.get(self.focused).clone())
}
} }
impl<T> Default for Handler<T> { impl<T> Default for Handler<T> {
+11
View File
@@ -12,6 +12,17 @@ macro_rules! check_expr {
} }
#[macro_export] #[macro_export]
macro_rules! parse_rsx_param { macro_rules! parse_rsx_param {
// Data fields
($elem:expr, @$name:ident: $type:ty; $($rest:tt)*) => {
if $elem.children.is_none() {$elem.children = $crate::Children::Children(Vec::new(), 0)}
if let $crate::Children::Children(children, _) = &mut $elem.children {
let mut $name = $crate::ui::data_holder::<$type>();
$name.id = stringify!($name);
children.push($name);
}
osui::parse_rsx_param!($elem, $($rest)*)
};
($elem:expr, for ($($for:tt)*) $code:block $($rest:tt)*) => { ($elem:expr, for ($($for:tt)*) $code:block $($rest:tt)*) => {
if $elem.children.is_none() {$elem.children = $crate::Children::Children(Vec::new(), 0)} if $elem.children.is_none() {$elem.children = $crate::Children::Children(Vec::new(), 0)}
if let $crate::Children::Children(children, _) = &mut $elem.children { if let $crate::Children::Children(children, _) = &mut $elem.children {
+1 -1
View File
@@ -114,6 +114,6 @@ pub fn data_holder<'a, T: std::default::Default>() -> Box<DataHolder<'a, T>> {
impl<'a, T: std::fmt::Debug + Send + Sync> ElementWidget for DataHolder<'a, T> { impl<'a, T: std::fmt::Debug + Send + Sync> ElementWidget for DataHolder<'a, T> {
fn render(&self, focused: bool, _: usize, _: usize) -> RenderResult { fn render(&self, focused: bool, _: usize, _: usize) -> RenderResult {
RenderWriter::new(focused, self.style.clone()).result() RenderWriter::new(focused, self.style.clone()).hidden()
} }
} }