tests passing, and tui updated

This commit is contained in:
Evan Almloff committed 2022-04-12 18:46:16 -05:00
1 parent 92010b0bab
commit 9eaf7212a3
13 files changed
+450 -305

No files matched your search

+65 -58
View File
@@ -1,83 +1,90 @@
use std::cell::RefCell;
use std::rc::Rc;
use dioxus_core::*;
use dioxus_native_core::layout_attributes::apply_layout_attributes;
use dioxus_native_core::real_dom::BubbledUpState;
use dioxus_native_core::state::{AttributeMask, ChildDepState, NodeMask, NodeView};
use stretch2::prelude::*;
/// the size
#[derive(Clone, PartialEq, Default, Debug)]
pub struct StretchLayout {
pub style: Style,
pub node: Option<Node>,
}
impl BubbledUpState for StretchLayout {
type Ctx = Stretch;
impl ChildDepState for StretchLayout {
type Ctx = Rc<RefCell<Stretch>>;
type DepState = Self;
// todo: update mask
const NODE_MASK: NodeMask = NodeMask::new(AttributeMask::All, false, false, true);
/// Setup the layout
fn reduce<'a, I>(&mut self, children: I, vnode: &VNode, stretch: &mut Self::Ctx)
fn reduce<'a>(
&mut self,
node: NodeView,
children: impl Iterator<Item = &'a Self::DepState>,
ctx: &Self::Ctx,
) -> bool
where
I: Iterator<Item = &'a Self>,
Self: 'a,
Self::DepState: 'a,
{
match vnode {
VNode::Text(t) => {
let char_len = t.text.chars().count();
let mut stretch = ctx.borrow_mut();
if let Some(text) = node.text() {
let char_len = text.chars().count();
let style = Style {
size: Size {
// characters are 1 point tall
height: Dimension::Points(1.0),
let style = Style {
size: Size {
// characters are 1 point tall
height: Dimension::Points(1.0),
// text is as long as it is declared
width: Dimension::Points(char_len as f32),
},
..Default::default()
};
// text is as long as it is declared
width: Dimension::Points(char_len as f32),
},
..Default::default()
};
if let Some(n) = self.node {
if self.style != style {
stretch.set_style(n, style).unwrap();
}
} else {
self.node = Some(stretch.new_node(style, &[]).unwrap());
if let Some(n) = self.node {
if self.style != style {
stretch.set_style(n, style).unwrap();
}
self.style = style;
} else {
self.node = Some(stretch.new_node(style, &[]).unwrap());
}
VNode::Element(el) => {
// gather up all the styles from the attribute list
let mut style = Style::default();
for &Attribute { name, value, .. } in el.attributes {
apply_layout_attributes(name, value, &mut style);
}
self.style = style;
} else {
// gather up all the styles from the attribute list
let mut style = Style::default();
// the root node fills the entire area
if el.id.get() == Some(ElementId(0)) {
apply_layout_attributes("width", "100%", &mut style);
apply_layout_attributes("height", "100%", &mut style);
}
// Set all direct nodes as our children
let mut child_layout = vec![];
for l in children {
child_layout.push(l.node.unwrap());
}
if let Some(n) = self.node {
if stretch.children(n).unwrap() != child_layout {
stretch.set_children(n, &child_layout).unwrap();
}
if self.style != style {
stretch.set_style(n, style).unwrap();
}
} else {
self.node = Some(stretch.new_node(style, &child_layout).unwrap());
}
self.style = style;
for &Attribute { name, value, .. } in node.attributes() {
apply_layout_attributes(name, value, &mut style);
}
_ => (),
// the root node fills the entire area
if node.id() == ElementId(0) {
apply_layout_attributes("width", "100%", &mut style);
apply_layout_attributes("height", "100%", &mut style);
}
// Set all direct nodes as our children
let mut child_layout = vec![];
for l in children {
child_layout.push(l.node.unwrap());
}
if let Some(n) = self.node {
if stretch.children(n).unwrap() != child_layout {
stretch.set_children(n, &child_layout).unwrap();
}
if self.style != style {
stretch.set_style(n, style).unwrap();
}
} else {
self.node = Some(stretch.new_node(style, &child_layout).unwrap());
}
self.style = style;
}
true
}
}