fix rendering

This commit is contained in:
Evan Almloff committed 2022-04-13 11:39:38 -05:00
1 parent 628638b9af
commit 3b06059834
11 files changed
+509 -65

No files matched your search

+18 -12
View File
@@ -28,11 +28,13 @@ impl ChildDepState for StretchLayout {
where
Self::DepState: 'a,
{
let mut changed = false;
let mut stretch = ctx.borrow_mut();
let mut style = Style::default();
if let Some(text) = node.text() {
let char_len = text.chars().count();
let style = Style {
style = Style {
size: Size {
// characters are 1 point tall
height: Dimension::Points(1.0),
@@ -42,7 +44,6 @@ impl ChildDepState for StretchLayout {
},
..Default::default()
};
if let Some(n) = self.node {
if self.style != style {
stretch.set_style(n, style).unwrap();
@@ -50,12 +51,8 @@ impl ChildDepState for StretchLayout {
} else {
self.node = Some(stretch.new_node(style, &[]).unwrap());
}
self.style = style;
} else {
// gather up all the styles from the attribute list
let mut style = Style::default();
for &Attribute { name, value, .. } in node.attributes() {
apply_layout_attributes(name, value, &mut style);
}
@@ -73,18 +70,27 @@ impl ChildDepState for StretchLayout {
}
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.node = Some(stretch.new_node(style, &[]).unwrap());
}
if let Some(n) = self.node {
if self.style != style {
stretch.set_style(n, style).unwrap();
}
if stretch.children(n).unwrap() != child_layout {
stretch.set_children(n, &child_layout).unwrap();
}
} else {
self.node = Some(stretch.new_node(style, &[]).unwrap());
}
}
if self.style != style {
changed = true;
self.style = style;
}
true
changed
}
}
+2 -3
View File
@@ -148,8 +148,7 @@ fn render_vdom(
fn resize(dims: Rect, stretch: &mut Stretch, rdom: &Dom) {
let width = dims.width;
let height = dims.height;
let root_id = rdom.root_id();
let root_node = rdom[root_id].state.layout.node.unwrap();
let root_node = rdom[0].state.layout.node.unwrap();
stretch
.compute_layout(
@@ -165,7 +164,7 @@ fn render_vdom(
terminal.draw(|frame| {
// size is guaranteed to not change when rendering
resize(frame.size(), &mut stretch.borrow_mut(), &rdom);
let root = &rdom[rdom.root_id()];
let root = &rdom[0];
render::render_vnode(frame, &stretch.borrow(), &rdom, &root, cfg);
})?;
} else {
+1
View File
@@ -30,6 +30,7 @@ pub(crate) fn render_vnode(
}
let Layout { location, size, .. } = layout.layout(node.state.layout.node.unwrap()).unwrap();
// println!("rendering {node:?} {location:?} {size:?}");
let Point { x, y } = location;
let Size { width, height } = size;
+19 -14
View File
@@ -50,22 +50,22 @@ impl ParentDepState for StyleModifier {
const NODE_MASK: NodeMask = NodeMask::new(AttributeMask::All, true, true, false);
fn reduce(&mut self, node: NodeView, parent: Option<&Self::DepState>, _: &Self::Ctx) -> bool {
*self = StyleModifier::default();
let mut new = StyleModifier::default();
if parent.is_some() {
self.style.fg = None;
new.style.fg = None;
}
// handle text modifier elements
if node.namespace().is_none() {
if let Some(tag) = node.tag() {
match tag {
"b" => apply_style_attributes("font-weight", "bold", self),
"strong" => apply_style_attributes("font-weight", "bold", self),
"u" => apply_style_attributes("text-decoration", "underline", self),
"ins" => apply_style_attributes("text-decoration", "underline", self),
"del" => apply_style_attributes("text-decoration", "line-through", self),
"i" => apply_style_attributes("font-style", "italic", self),
"em" => apply_style_attributes("font-style", "italic", self),
"b" => apply_style_attributes("font-weight", "bold", &mut new),
"strong" => apply_style_attributes("font-weight", "bold", &mut new),
"u" => apply_style_attributes("text-decoration", "underline", &mut new),
"ins" => apply_style_attributes("text-decoration", "underline", &mut new),
"del" => apply_style_attributes("text-decoration", "line-through", &mut new),
"i" => apply_style_attributes("font-style", "italic", &mut new),
"em" => apply_style_attributes("font-style", "italic", &mut new),
"mark" => {
apply_style_attributes("background-color", "rgba(241, 231, 64, 50%)", self)
}
@@ -76,16 +76,21 @@ impl ParentDepState for StyleModifier {
// gather up all the styles from the attribute list
for &Attribute { name, value, .. } in node.attributes() {
apply_style_attributes(name, value, self);
apply_style_attributes(name, value, &mut new);
}
// keep the text styling from the parent element
if let Some(parent) = parent {
let mut new_style = self.style.merge(parent.style);
new_style.bg = self.style.bg;
self.style = new_style;
let mut new_style = new.style.merge(parent.style);
new_style.bg = new.style.bg;
new.style = new_style;
}
if &mut new != self {
*self = new;
true
} else {
false
}
true
}
}