diff --git a/src/examples.rs b/src/examples.rs index 85bac1d..f441e4d 100644 --- a/src/examples.rs +++ b/src/examples.rs @@ -1,49 +1,68 @@ -use crate::prelude::*; +pub mod counter_example { + use crate::prelude::*; -pub fn todo_app() -> Element { - let todo = vec!["Todo", "Hovering", "Completed", "Foo", "Bar"]; + pub fn app() -> Element { + let count = State::new(0); - rsx! { - @SetStyle(todo_styling()) + rsx! { + button { + on_click: fn(_, _, _) @count { + count += 1; + }, - text { class: "title", static "Todo list app made with OSUI" } - text { class: "title", "{}", "-".repeat(crossterm::terminal::size().unwrap().0 as usize) } - for (t in todo) { - ersx!(button { class: "todo", on_click: fn(btn: &mut Button, _, _) { - btn.class = if btn.class == "todo" { - "complete" - } else { - "todo" - }; - }, "{t}" } ) + "The current count is: {count}" + } } } } -pub fn todo_styling() -> Css { - css! { - "todo" { - x: 0 px, - y: Auto, - color: Red, +pub mod todo_example { + use crate::prelude::*; + pub fn todo_app() -> Element { + let todo = vec!["Todo", "Hovering", "Completed", "Foo", "Bar"]; + + rsx! { + @SetStyle(todo_styling()) + + text { class: "title", static "Todo list app made with OSUI" } + text { class: "title", "{}", "-".repeat(crossterm::terminal::size().unwrap().0 as usize) } + for (t in todo) { + ersx!(button { class: "todo", on_click: fn(btn: &mut Button, _, _) { + btn.class = if btn.class == "todo" { + "complete" + } else { + "todo" + }; + }, "{t}" } ) + } } - "todo": "hover" { - color: Blue, - } - "complete" { - x: 0 px, - y: Auto, - color: Green, - } - "complete": "hover" { - color: Cyan - } - "title" { - x: 0 px, - y: Auto, - } - "title": "hover" { - color: Blue + } + + pub fn todo_styling() -> Css { + css! { + "todo" { + x: 0 px, + y: Auto, + color: Red, + } + "todo": "hover" { + color: Blue, + } + "complete" { + x: 0 px, + y: Auto, + color: Green, + } + "complete": "hover" { + color: Cyan + } + "title" { + x: 0 px, + y: Auto, + } + "title": "hover" { + color: Blue + } } } } diff --git a/src/lib.rs b/src/lib.rs index 726041b..976576c 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -22,6 +22,7 @@ use std::{ collections::HashMap, fmt::Debug, + ops::{AddAssign, SubAssign}, sync::{Arc, Mutex}, }; @@ -97,6 +98,8 @@ pub struct Writer { written: (u16, u16), } +pub struct State(Arc>); + ////////////////////////////////////////////////////////////////////////////////////////////////// /// Enums ////////////////////////////////////////////////////////////////////////////////////////////////// @@ -387,7 +390,6 @@ impl std::fmt::Debug for Handler { } } -pub struct State(Arc>); impl State { pub fn new<'a>(v: T) -> State { State(Arc::new(Mutex::new(v))) @@ -395,7 +397,7 @@ impl State { pub fn use_state<'a>(&'a self) -> std::sync::MutexGuard<'a, T> { self.0.lock().unwrap() } - pub fn copy_state<'a>(&'a self) -> State { + pub fn clone<'a>(&'a self) -> State { State(Arc::clone(&self.0)) } } @@ -409,3 +411,24 @@ impl std::fmt::Display for State { } } } + +impl AddAssign for State { + fn add_assign(&mut self, rhs: T) { + *self.0.lock().unwrap() += rhs; + } +} + +impl SubAssign for State { + fn sub_assign(&mut self, rhs: T) { + *self.0.lock().unwrap() -= rhs; + } +} + +impl PartialEq for State { + fn eq(&self, other: &T) -> bool { + self.use_state().eq(other) + } + fn ne(&self, other: &T) -> bool { + self.use_state().ne(other) + } +} diff --git a/src/main.rs b/src/main.rs index 5cd4e91..e7b091e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,5 +1,5 @@ -use osui::{examples::todo_app, launch}; +use osui::{examples::counter_example::app, launch}; fn main() { - launch!(todo_app()); + launch!(app()); } diff --git a/src/rsx.rs b/src/rsx.rs index 988e9d8..bda1c75 100644 --- a/src/rsx.rs +++ b/src/rsx.rs @@ -40,12 +40,15 @@ macro_rules! parse_rsx_param { osui::parse_rsx_param!($elem, $($($rest)*)?); }; - ($elem:expr, $($k:ident).+: fn($($params:tt)*) @$($v:ident),* $code:block $(, $($rest:tt)*)?) => { + ($elem:expr, $($k:ident).+: fn($($params:tt)*) $(@$($v:ident),+)? $code:block $(, $($rest:tt)*)?) => { $elem.$($k).+ = $crate::Handler::new({ - $( - let $v = $v.copy_state(); - )* - move |$($params)*| $code + $($( + #[allow(unused_mut)] + let mut $v = $v.clone(); + )+)? + move |$($params)*| { + $code + } }); osui::parse_rsx_param!($elem, $($($rest)*)?); };