Skip to content

Commit

Permalink
Use table in layout
Browse files Browse the repository at this point in the history
  • Loading branch information
nicoburns committed Aug 1, 2024
1 parent ec120ba commit 18e16e0
Showing 1 changed file with 44 additions and 17 deletions.
61 changes: 44 additions & 17 deletions packages/dom/src/layout/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,27 @@ pub struct TableTreeWrapper<'doc> {
#[derive(Debug, Clone)]
pub struct TableContext {
style: taffy::Style,
cells: Vec<TableCell>,
items: Vec<TableItem>,
}

#[derive(Debug, Clone, Eq, PartialEq)]
pub enum TableItemKind {
Row,
Cell,
}

#[derive(Debug, Clone)]
pub struct TableCell {
pub struct TableItem {
kind: TableItemKind,
node_id: usize,
style: taffy::Style,
_row: u16,
_col: u16,
}

pub(crate) fn build_table_context(
doc: &mut Document,
table_root_node_id: usize,
) -> (TableContext, Vec<usize>) {
let mut cells: Vec<TableCell> = Vec::new();
let mut items: Vec<TableItem> = Vec::new();
let mut row = 0u16;
let mut col = 0u16;

Expand All @@ -48,25 +53,29 @@ pub(crate) fn build_table_context(
drop(stylo_styles);

for child_id in children.iter().copied() {
collect_table_cells(doc, child_id, &mut row, &mut col, &mut cells);
collect_table_cells(doc, child_id, &mut row, &mut col, &mut items);
}

style.grid_template_columns = vec![style_helpers::auto(); col as usize];
style.grid_template_rows = vec![style_helpers::auto(); row as usize];

let layout_children = cells.iter().map(|cell| cell.node_id).collect();
let layout_children = items
.iter()
.filter(|item| item.kind == TableItemKind::Cell)
.map(|cell| cell.node_id)
.collect();
let root_node = &mut doc.nodes[table_root_node_id];
root_node.children = children;

(TableContext { style, cells }, layout_children)
(TableContext { style, items }, layout_children)
}

pub(crate) fn collect_table_cells(
doc: &mut Document,
node_id: usize,
row: &mut u16,
col: &mut u16,
cells: &mut Vec<TableCell>,
cells: &mut Vec<TableItem>,
) {
let node = &doc.nodes[node_id];

Expand All @@ -89,6 +98,25 @@ pub(crate) fn collect_table_cells(
DisplayInside::TableRow => {
*row += 1;
*col = 0;

{
let stylo_style = &node.primary_styles().unwrap();
let mut style = stylo_to_taffy::entire_style(stylo_style);
style.grid_column = taffy::Line {
start: style_helpers::line(0),
end: style_helpers::line(-1),
};
style.grid_row = taffy::Line {
start: style_helpers::line(*row as i16),
end: style_helpers::span(1),
};
cells.push(TableItem {
kind: TableItemKind::Row,
node_id,
style,
});
}

let children = std::mem::take(&mut doc.nodes[node_id].children);
for child_id in children.iter().copied() {
collect_table_cells(doc, child_id, row, col, cells);
Expand All @@ -110,11 +138,10 @@ pub(crate) fn collect_table_cells(
start: style_helpers::line(*row as i16),
end: style_helpers::span(1),
};
cells.push(TableCell {
cells.push(TableItem {
kind: TableItemKind::Cell,
node_id,
style,
_row: *row,
_col: (*col + 1),
});
*col += colspan;
}
Expand All @@ -139,7 +166,7 @@ impl taffy::TraversePartialTree for TableTreeWrapper<'_> {

#[inline(always)]
fn child_ids(&self, _node_id: taffy::NodeId) -> Self::ChildIter<'_> {
RangeIter(0..self.ctx.cells.len())
RangeIter(0..self.ctx.items.len())
}

#[inline(always)]
Expand All @@ -163,12 +190,12 @@ impl taffy::LayoutPartialTree for TableTreeWrapper<'_> {
}

fn set_unrounded_layout(&mut self, node_id: taffy::NodeId, layout: &taffy::Layout) {
let node_id = taffy::NodeId::from(self.ctx.cells[usize::from(node_id)].node_id);
let node_id = taffy::NodeId::from(self.ctx.items[usize::from(node_id)].node_id);
self.doc.set_unrounded_layout(node_id, layout)
}

fn get_cache_mut(&mut self, node_id: taffy::NodeId) -> &mut taffy::Cache {
let node_id = taffy::NodeId::from(self.ctx.cells[usize::from(node_id)].node_id);
let node_id = taffy::NodeId::from(self.ctx.items[usize::from(node_id)].node_id);
&mut self.doc.node_from_id_mut(node_id).cache
}

Expand All @@ -177,7 +204,7 @@ impl taffy::LayoutPartialTree for TableTreeWrapper<'_> {
node_id: taffy::NodeId,
inputs: taffy::tree::LayoutInput,
) -> taffy::tree::LayoutOutput {
let node_id = taffy::NodeId::from(self.ctx.cells[usize::from(node_id)].node_id);
let node_id = taffy::NodeId::from(self.ctx.items[usize::from(node_id)].node_id);
self.doc.compute_child_layout(node_id, inputs)
}
}
Expand All @@ -196,6 +223,6 @@ impl taffy::LayoutGridContainer for TableTreeWrapper<'_> {
}

fn get_grid_child_style(&self, child_node_id: taffy::NodeId) -> Self::GridItemStyle<'_> {
&self.ctx.cells[usize::from(child_node_id)].style
&self.ctx.items[usize::from(child_node_id)].style
}
}

0 comments on commit 18e16e0

Please sign in to comment.