slightly improved state management
This commit is contained in:
+23
-4
@@ -1,6 +1,24 @@
|
|||||||
use crate::prelude::*;
|
pub mod counter_example {
|
||||||
|
use crate::prelude::*;
|
||||||
|
|
||||||
pub fn todo_app() -> Element {
|
pub fn app() -> Element {
|
||||||
|
let count = State::new(0);
|
||||||
|
|
||||||
|
rsx! {
|
||||||
|
button {
|
||||||
|
on_click: fn(_, _, _) @count {
|
||||||
|
count += 1;
|
||||||
|
},
|
||||||
|
|
||||||
|
"The current count is: {count}"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub mod todo_example {
|
||||||
|
use crate::prelude::*;
|
||||||
|
pub fn todo_app() -> Element {
|
||||||
let todo = vec!["Todo", "Hovering", "Completed", "Foo", "Bar"];
|
let todo = vec!["Todo", "Hovering", "Completed", "Foo", "Bar"];
|
||||||
|
|
||||||
rsx! {
|
rsx! {
|
||||||
@@ -18,9 +36,9 @@ pub fn todo_app() -> Element {
|
|||||||
}, "{t}" } )
|
}, "{t}" } )
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn todo_styling() -> Css {
|
pub fn todo_styling() -> Css {
|
||||||
css! {
|
css! {
|
||||||
"todo" {
|
"todo" {
|
||||||
x: 0 px,
|
x: 0 px,
|
||||||
@@ -46,4 +64,5 @@ pub fn todo_styling() -> Css {
|
|||||||
color: Blue
|
color: Blue
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+25
-2
@@ -22,6 +22,7 @@
|
|||||||
use std::{
|
use std::{
|
||||||
collections::HashMap,
|
collections::HashMap,
|
||||||
fmt::Debug,
|
fmt::Debug,
|
||||||
|
ops::{AddAssign, SubAssign},
|
||||||
sync::{Arc, Mutex},
|
sync::{Arc, Mutex},
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -97,6 +98,8 @@ pub struct Writer {
|
|||||||
written: (u16, u16),
|
written: (u16, u16),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub struct State<T>(Arc<Mutex<T>>);
|
||||||
|
|
||||||
//////////////////////////////////////////////////////////////////////////////////////////////////
|
//////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
/// Enums
|
/// Enums
|
||||||
//////////////////////////////////////////////////////////////////////////////////////////////////
|
//////////////////////////////////////////////////////////////////////////////////////////////////
|
||||||
@@ -387,7 +390,6 @@ impl<T> std::fmt::Debug for Handler<T> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct State<T>(Arc<Mutex<T>>);
|
|
||||||
impl<T> State<T> {
|
impl<T> State<T> {
|
||||||
pub fn new<'a>(v: T) -> State<T> {
|
pub fn new<'a>(v: T) -> State<T> {
|
||||||
State(Arc::new(Mutex::new(v)))
|
State(Arc::new(Mutex::new(v)))
|
||||||
@@ -395,7 +397,7 @@ impl<T> State<T> {
|
|||||||
pub fn use_state<'a>(&'a self) -> std::sync::MutexGuard<'a, T> {
|
pub fn use_state<'a>(&'a self) -> std::sync::MutexGuard<'a, T> {
|
||||||
self.0.lock().unwrap()
|
self.0.lock().unwrap()
|
||||||
}
|
}
|
||||||
pub fn copy_state<'a>(&'a self) -> State<T> {
|
pub fn clone<'a>(&'a self) -> State<T> {
|
||||||
State(Arc::clone(&self.0))
|
State(Arc::clone(&self.0))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -409,3 +411,24 @@ impl<T: std::fmt::Display> std::fmt::Display for State<T> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl<T: AddAssign> AddAssign<T> for State<T> {
|
||||||
|
fn add_assign(&mut self, rhs: T) {
|
||||||
|
*self.0.lock().unwrap() += rhs;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T: SubAssign> SubAssign<T> for State<T> {
|
||||||
|
fn sub_assign(&mut self, rhs: T) {
|
||||||
|
*self.0.lock().unwrap() -= rhs;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T: PartialEq> PartialEq<T> for State<T> {
|
||||||
|
fn eq(&self, other: &T) -> bool {
|
||||||
|
self.use_state().eq(other)
|
||||||
|
}
|
||||||
|
fn ne(&self, other: &T) -> bool {
|
||||||
|
self.use_state().ne(other)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
+2
-2
@@ -1,5 +1,5 @@
|
|||||||
use osui::{examples::todo_app, launch};
|
use osui::{examples::counter_example::app, launch};
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
launch!(todo_app());
|
launch!(app());
|
||||||
}
|
}
|
||||||
|
|||||||
+8
-5
@@ -40,12 +40,15 @@ macro_rules! parse_rsx_param {
|
|||||||
osui::parse_rsx_param!($elem, $($($rest)*)?);
|
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({
|
$elem.$($k).+ = $crate::Handler::new({
|
||||||
$(
|
$($(
|
||||||
let $v = $v.copy_state();
|
#[allow(unused_mut)]
|
||||||
)*
|
let mut $v = $v.clone();
|
||||||
move |$($params)*| $code
|
)+)?
|
||||||
|
move |$($params)*| {
|
||||||
|
$code
|
||||||
|
}
|
||||||
});
|
});
|
||||||
osui::parse_rsx_param!($elem, $($($rest)*)?);
|
osui::parse_rsx_param!($elem, $($($rest)*)?);
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user