#13 cleanup frontend and widgets
This commit is contained in:
+2
-2
@@ -7,7 +7,7 @@ use crate::{
|
|||||||
};
|
};
|
||||||
|
|
||||||
pub enum RsxElement {
|
pub enum RsxElement {
|
||||||
Element(Arc<StaticWidget>, Rsx),
|
Element(StaticWidget, Rsx),
|
||||||
|
|
||||||
DynElement(
|
DynElement(
|
||||||
Box<dyn FnMut() -> WidgetLoad + Send + Sync>,
|
Box<dyn FnMut() -> WidgetLoad + Send + Sync>,
|
||||||
@@ -68,7 +68,7 @@ impl Rsx {
|
|||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn create_element_static(&mut self, element: Arc<StaticWidget>, children: Rsx) {
|
pub fn create_element_static(&mut self, element: StaticWidget, children: Rsx) {
|
||||||
self.0.push(RsxElement::Element(element, children));
|
self.0.push(RsxElement::Element(element, children));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+4
-6
@@ -54,15 +54,13 @@ impl Screen {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn draw<E: Element + 'static + Send + Sync>(self: &Arc<Self>, element: E) -> Arc<Widget> {
|
pub fn draw<E: Element + 'static + Send + Sync>(self: &Arc<Self>, element: E) -> Arc<Widget> {
|
||||||
let w = Arc::new(Widget::Static(Arc::new(StaticWidget::new(Box::new(
|
let w = Arc::new(Widget::Static(StaticWidget::new(Box::new(element))));
|
||||||
element,
|
|
||||||
)))));
|
|
||||||
self.widgets.lock().unwrap().push(w.clone());
|
self.widgets.lock().unwrap().push(w.clone());
|
||||||
w
|
w
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn draw_box(self: &Arc<Self>, element: BoxedElement) -> Arc<Widget> {
|
pub fn draw_box(self: &Arc<Self>, element: BoxedElement) -> Arc<Widget> {
|
||||||
let w = Arc::new(Widget::Static(Arc::new(StaticWidget::new(element))));
|
let w = Arc::new(Widget::Static(StaticWidget::new(element)));
|
||||||
self.widgets.lock().unwrap().push(w.clone());
|
self.widgets.lock().unwrap().push(w.clone());
|
||||||
w
|
w
|
||||||
}
|
}
|
||||||
@@ -75,7 +73,7 @@ impl Screen {
|
|||||||
self: &Arc<Self>,
|
self: &Arc<Self>,
|
||||||
element: F,
|
element: F,
|
||||||
) -> Arc<Widget> {
|
) -> Arc<Widget> {
|
||||||
let w = Arc::new(Widget::Dynamic(Arc::new(DynWidget::new(element))));
|
let w = Arc::new(Widget::Dynamic(DynWidget::new(element)));
|
||||||
self.widgets.lock().unwrap().push(w.clone());
|
self.widgets.lock().unwrap().push(w.clone());
|
||||||
w
|
w
|
||||||
}
|
}
|
||||||
@@ -84,7 +82,7 @@ impl Screen {
|
|||||||
self: &Arc<Self>,
|
self: &Arc<Self>,
|
||||||
element: Box<dyn FnMut() -> WidgetLoad + Send + Sync>,
|
element: Box<dyn FnMut() -> WidgetLoad + Send + Sync>,
|
||||||
) -> Arc<Widget> {
|
) -> Arc<Widget> {
|
||||||
let w = Arc::new(Widget::Dynamic(Arc::new(DynWidget::new(element))));
|
let w = Arc::new(Widget::Dynamic(DynWidget::new(element)));
|
||||||
self.widgets.lock().unwrap().push(w.clone());
|
self.widgets.lock().unwrap().push(w.clone());
|
||||||
w
|
w
|
||||||
}
|
}
|
||||||
|
|||||||
+9
-3
@@ -147,7 +147,9 @@ macro_rules! rsx {
|
|||||||
macro_rules! rsx_inner {
|
macro_rules! rsx_inner {
|
||||||
// static
|
// static
|
||||||
($r:expr, $(@$comp:expr;)* static $s:literal $($rest:tt)*) => {
|
($r:expr, $(@$comp:expr;)* static $s:literal $($rest:tt)*) => {
|
||||||
$r.create_element_static(std::sync::Arc::new($crate::widget::StaticWidget::new(Box::new(format!($s)))) $(.component($comp))* .clone(), $crate::frontend::Rsx(Vec::new()));
|
let w = $crate::widget::StaticWidget::new(Box::new(format!($s)));
|
||||||
|
$(w.component($comp);)*
|
||||||
|
$r.create_element_static(w, $crate::frontend::Rsx(Vec::new()));
|
||||||
$crate::rsx_inner! { $r, $($rest)* };
|
$crate::rsx_inner! { $r, $($rest)* };
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -160,13 +162,17 @@ macro_rules! rsx_inner {
|
|||||||
|
|
||||||
// static
|
// static
|
||||||
($r:expr, $(%$dep:ident)* $(@$comp:expr;)* static $name:path { $($inner:tt)* } ($($e:expr),*) $($rest:tt)*) => {
|
($r:expr, $(%$dep:ident)* $(@$comp:expr;)* static $name:path { $($inner:tt)* } ($($e:expr),*) $($rest:tt)*) => {
|
||||||
$r.create_element_static(std::sync::Arc::new($crate::widget::StaticWidget::new(Box::new(<$name>::new($($e),*)))) $(.component($comp))* .clone(), $crate::frontend::Rsx(Vec::new()));
|
let w = $crate::widget::StaticWidget::new(Box::new(<$name>::new($($e),*)));
|
||||||
|
$(w.component($comp);)*
|
||||||
|
$r.create_element_static(w, $crate::frontend::Rsx(Vec::new()));
|
||||||
$crate::rsx_inner! { $r, $($rest)* };
|
$crate::rsx_inner! { $r, $($rest)* };
|
||||||
};
|
};
|
||||||
|
|
||||||
// static
|
// static
|
||||||
($r:expr, $(@$comp:expr;)* static $name:path { $($inner:tt)* } $($rest:tt)*) => {
|
($r:expr, $(@$comp:expr;)* static $name:path { $($inner:tt)* } $($rest:tt)*) => {
|
||||||
$r.create_element_static(std::sync::Arc::new($crate::widget::StaticWidget::new(Box::new(<$name>::new()))) $(.component($comp))* .clone(), $crate::frontend::Rsx(Vec::new()));
|
let w = $crate::widget::StaticWidget::new(Box::new(<$name>::new()));
|
||||||
|
$(w.component($comp);)*
|
||||||
|
$r.create_element_static(w, $crate::frontend::Rsx(Vec::new()));
|
||||||
$crate::rsx_inner! { $r, $($rest)* };
|
$crate::rsx_inner! { $r, $($rest)* };
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
+38
-33
@@ -50,12 +50,38 @@ impl WidgetLoad {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub struct StaticWidget(Mutex<BoxedElement>, Mutex<HashMap<TypeId, BoxedComponent>>);
|
||||||
|
|
||||||
|
pub struct DynWidget(
|
||||||
|
Mutex<BoxedElement>,
|
||||||
|
Mutex<HashMap<TypeId, BoxedComponent>>,
|
||||||
|
Mutex<Box<dyn FnMut() -> WidgetLoad + Send + Sync>>,
|
||||||
|
Mutex<Vec<Box<dyn DependencyHandler>>>,
|
||||||
|
Mutex<Option<Box<dyn FnMut(WidgetLoad) -> WidgetLoad + Send + Sync>>>,
|
||||||
|
);
|
||||||
|
|
||||||
pub enum Widget {
|
pub enum Widget {
|
||||||
Static(Arc<StaticWidget>),
|
Static(StaticWidget),
|
||||||
Dynamic(Arc<DynWidget>),
|
Dynamic(DynWidget),
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Widget {
|
||||||
|
pub fn new_static(e: BoxedElement) -> Self {
|
||||||
|
Self::Static(StaticWidget(Mutex::new(e), Mutex::new(HashMap::new())))
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn new_dyn<F: FnMut() -> 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),
|
||||||
|
))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// === Trait Implementation ===
|
|
||||||
impl Widget {
|
impl Widget {
|
||||||
pub fn get_elem(&self) -> MutexGuard<BoxedElement> {
|
pub fn get_elem(&self) -> MutexGuard<BoxedElement> {
|
||||||
match self {
|
match self {
|
||||||
@@ -153,9 +179,6 @@ impl Widget {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// === Static Widget ===
|
|
||||||
pub struct StaticWidget(Mutex<BoxedElement>, Mutex<HashMap<TypeId, BoxedComponent>>);
|
|
||||||
|
|
||||||
impl StaticWidget {
|
impl StaticWidget {
|
||||||
fn after_render(&self) {}
|
fn after_render(&self) {}
|
||||||
fn get_elem(&self) -> MutexGuard<BoxedElement> {
|
fn get_elem(&self) -> MutexGuard<BoxedElement> {
|
||||||
@@ -168,18 +191,16 @@ impl StaticWidget {
|
|||||||
Self(Mutex::new(e), Mutex::new(HashMap::new()))
|
Self(Mutex::new(e), Mutex::new(HashMap::new()))
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn component<C: Component + 'static>(self: &Arc<Self>, c: C) -> &Arc<Self> {
|
pub fn component<C: Component + 'static>(&self, c: C) {
|
||||||
self.1
|
self.1
|
||||||
.lock()
|
.lock()
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.entry(c.type_id())
|
.entry(c.type_id())
|
||||||
.or_insert_with(|| Box::new(c));
|
.or_insert_with(|| Box::new(c));
|
||||||
self
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn set_component<C: Component + 'static>(self: &Arc<Self>, c: C) -> &Arc<Self> {
|
pub fn set_component<C: Component + 'static>(&self, c: C) {
|
||||||
self.1.lock().unwrap().insert(c.type_id(), Box::new(c));
|
self.1.lock().unwrap().insert(c.type_id(), Box::new(c));
|
||||||
self
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get<C: Component + 'static + Clone>(&self) -> Option<C> {
|
pub fn get<C: Component + 'static + Clone>(&self) -> Option<C> {
|
||||||
@@ -192,15 +213,6 @@ impl StaticWidget {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// === Dynamic Widget ===
|
|
||||||
pub struct DynWidget(
|
|
||||||
Mutex<BoxedElement>,
|
|
||||||
Mutex<HashMap<TypeId, BoxedComponent>>,
|
|
||||||
Mutex<Box<dyn FnMut() -> WidgetLoad + Send + Sync>>,
|
|
||||||
Mutex<Vec<Box<dyn DependencyHandler>>>,
|
|
||||||
Mutex<Option<Box<dyn FnMut(WidgetLoad) -> WidgetLoad + Send + Sync>>>,
|
|
||||||
);
|
|
||||||
|
|
||||||
impl DynWidget {
|
impl DynWidget {
|
||||||
fn after_render(&self) {}
|
fn after_render(&self) {}
|
||||||
fn get_elem(&self) -> MutexGuard<BoxedElement> {
|
fn get_elem(&self) -> MutexGuard<BoxedElement> {
|
||||||
@@ -220,15 +232,12 @@ impl DynWidget {
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn inject<F: FnMut(WidgetLoad) -> WidgetLoad + 'static + Send + Sync>(
|
pub fn inject<F: FnMut(WidgetLoad) -> WidgetLoad + 'static + Send + Sync>(&self, f: F) {
|
||||||
self: &Arc<Self>,
|
|
||||||
f: F,
|
|
||||||
) {
|
|
||||||
*self.4.lock().unwrap() = Some(Box::new(f));
|
*self.4.lock().unwrap() = Some(Box::new(f));
|
||||||
self.refresh();
|
self.refresh();
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn refresh(self: &Arc<Self>) {
|
pub fn refresh(&self) {
|
||||||
let mut w = (self.2.lock().unwrap())();
|
let mut w = (self.2.lock().unwrap())();
|
||||||
|
|
||||||
if let Some(we) = &mut *self.4.lock().unwrap() {
|
if let Some(we) = &mut *self.4.lock().unwrap() {
|
||||||
@@ -239,7 +248,7 @@ impl DynWidget {
|
|||||||
*self.1.lock().unwrap() = w.1;
|
*self.1.lock().unwrap() = w.1;
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn auto_refresh(self: &Arc<Self>) {
|
pub fn auto_refresh(&self) {
|
||||||
for d in self.3.lock().unwrap().iter() {
|
for d in self.3.lock().unwrap().iter() {
|
||||||
if d.check() {
|
if d.check() {
|
||||||
self.refresh();
|
self.refresh();
|
||||||
@@ -247,30 +256,26 @@ impl DynWidget {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn dependency<D: DependencyHandler + 'static>(self: &Arc<Self>, d: D) -> &Arc<Self> {
|
pub fn dependency<D: DependencyHandler + 'static>(&self, d: D) {
|
||||||
d.add();
|
d.add();
|
||||||
self.3.lock().unwrap().push(Box::new(d));
|
self.3.lock().unwrap().push(Box::new(d));
|
||||||
self
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn dependency_box(self: &Arc<Self>, d: Box<dyn DependencyHandler>) -> &Arc<Self> {
|
pub fn dependency_box(&self, d: Box<dyn DependencyHandler>) {
|
||||||
d.add();
|
d.add();
|
||||||
self.3.lock().unwrap().push(d);
|
self.3.lock().unwrap().push(d);
|
||||||
self
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn component<C: Component + 'static>(self: &Arc<Self>, c: C) -> &Arc<Self> {
|
pub fn component<C: Component + 'static>(&self, c: C) {
|
||||||
self.1
|
self.1
|
||||||
.lock()
|
.lock()
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.entry(c.type_id())
|
.entry(c.type_id())
|
||||||
.or_insert_with(|| Box::new(c));
|
.or_insert_with(|| Box::new(c));
|
||||||
self
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn set_component<C: Component + 'static>(self: &Arc<Self>, c: C) -> &Arc<Self> {
|
pub fn set_component<C: Component + 'static>(&self, c: C) {
|
||||||
self.1.lock().unwrap().insert(c.type_id(), Box::new(c));
|
self.1.lock().unwrap().insert(c.type_id(), Box::new(c));
|
||||||
self
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get<C: Component + 'static + Clone>(&self) -> Option<C> {
|
pub fn get<C: Component + 'static + Clone>(&self) -> Option<C> {
|
||||||
|
|||||||
Reference in New Issue
Block a user