feat: bootstrap namespace-driven Tessera renderer
This commit is contained in:
@@ -0,0 +1,298 @@
|
||||
use crate::{
|
||||
display_list::{Color, DisplayList, DrawCommand},
|
||||
document::{CodeNode, RenderDocument},
|
||||
geometry::{Point, Rect},
|
||||
};
|
||||
|
||||
const PANEL: Color = Color::rgb(247, 247, 245);
|
||||
const PANEL_ALT: Color = Color::rgb(238, 238, 235);
|
||||
const BORDER: Color = Color::rgb(214, 214, 209);
|
||||
const TEXT: Color = Color::rgb(36, 36, 34);
|
||||
const MUTED: Color = Color::rgb(112, 112, 107);
|
||||
const SELECTED: Color = Color::rgb(224, 229, 247);
|
||||
|
||||
#[derive(Debug, Default)]
|
||||
pub struct DeveloperToolsState {
|
||||
pub open: bool,
|
||||
pub selected_node_id: Option<String>,
|
||||
}
|
||||
|
||||
impl DeveloperToolsState {
|
||||
pub fn toggle(&mut self, document: &RenderDocument) {
|
||||
self.open = !self.open;
|
||||
self.ensure_selection(document);
|
||||
}
|
||||
|
||||
pub fn document_updated(&mut self, document: &RenderDocument) {
|
||||
self.ensure_selection(document);
|
||||
}
|
||||
|
||||
fn ensure_selection(&mut self, document: &RenderDocument) {
|
||||
let selection_exists = self
|
||||
.selected_node_id
|
||||
.as_deref()
|
||||
.is_some_and(|id| document.node(id).is_some());
|
||||
if !selection_exists {
|
||||
self.selected_node_id = document.root.as_ref().map(|node| node.id.clone());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Default)]
|
||||
pub struct DeveloperToolsLayout {
|
||||
rows: Vec<TreeRowHit>,
|
||||
}
|
||||
|
||||
impl DeveloperToolsLayout {
|
||||
pub fn node_at(&self, point: Point) -> Option<&str> {
|
||||
self.rows
|
||||
.iter()
|
||||
.find(|row| row.rect.contains(point))
|
||||
.map(|row| row.node_id.as_str())
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
struct TreeRowHit {
|
||||
rect: Rect,
|
||||
node_id: String,
|
||||
}
|
||||
|
||||
pub fn build(
|
||||
width: u32,
|
||||
height: u32,
|
||||
scale: f32,
|
||||
document: &RenderDocument,
|
||||
state: &DeveloperToolsState,
|
||||
) -> (DisplayList, DeveloperToolsLayout) {
|
||||
if !state.open {
|
||||
return (DisplayList::default(), DeveloperToolsLayout::default());
|
||||
}
|
||||
|
||||
let mut list = DisplayList::default();
|
||||
let width = width as f32;
|
||||
let height = height as f32;
|
||||
let panel_width = (430.0 * scale).min(width * 0.52);
|
||||
let panel_x = width - panel_width;
|
||||
let header_height = 44.0 * scale;
|
||||
let tree_height = ((height - header_height) * 0.42).max(150.0 * scale);
|
||||
let code_y = header_height + tree_height;
|
||||
|
||||
fill(
|
||||
&mut list,
|
||||
Rect::new(panel_x, 0.0, panel_width, height),
|
||||
0.0,
|
||||
PANEL,
|
||||
);
|
||||
fill(
|
||||
&mut list,
|
||||
Rect::new(panel_x, 0.0, 1.0 * scale, height),
|
||||
0.0,
|
||||
BORDER,
|
||||
);
|
||||
fill(
|
||||
&mut list,
|
||||
Rect::new(panel_x, header_height - scale, panel_width, scale),
|
||||
0.0,
|
||||
BORDER,
|
||||
);
|
||||
fill(
|
||||
&mut list,
|
||||
Rect::new(panel_x, code_y, panel_width, height - code_y),
|
||||
0.0,
|
||||
PANEL_ALT,
|
||||
);
|
||||
fill(
|
||||
&mut list,
|
||||
Rect::new(panel_x, code_y, panel_width, scale),
|
||||
0.0,
|
||||
BORDER,
|
||||
);
|
||||
|
||||
text(
|
||||
&mut list,
|
||||
panel_x + 14.0 * scale,
|
||||
15.0 * scale,
|
||||
"DEVELOPER",
|
||||
scale,
|
||||
TEXT,
|
||||
);
|
||||
text(
|
||||
&mut list,
|
||||
width - 116.0 * scale,
|
||||
15.0 * scale,
|
||||
"F12 TOGGLE",
|
||||
scale,
|
||||
MUTED,
|
||||
);
|
||||
|
||||
text(
|
||||
&mut list,
|
||||
panel_x + 14.0 * scale,
|
||||
header_height + 13.0 * scale,
|
||||
"ARCHITECTURE",
|
||||
scale,
|
||||
MUTED,
|
||||
);
|
||||
|
||||
let mut layout = DeveloperToolsLayout::default();
|
||||
if let Some(root) = &document.root {
|
||||
let mut flattened = Vec::new();
|
||||
flatten(root, 0, &mut flattened);
|
||||
let row_height = 24.0 * scale;
|
||||
let first_y = header_height + 34.0 * scale;
|
||||
let maximum_rows = ((tree_height - 40.0 * scale) / row_height).max(0.0) as usize;
|
||||
|
||||
for (index, (depth, node)) in flattened.into_iter().take(maximum_rows).enumerate() {
|
||||
let y = first_y + index as f32 * row_height;
|
||||
let row = Rect::new(
|
||||
panel_x + 7.0 * scale,
|
||||
y,
|
||||
panel_width - 14.0 * scale,
|
||||
row_height,
|
||||
);
|
||||
if state.selected_node_id.as_deref() == Some(node.id.as_str()) {
|
||||
fill(&mut list, row, 5.0 * scale, SELECTED);
|
||||
}
|
||||
text(
|
||||
&mut list,
|
||||
row.x + (9.0 + depth as f32 * 14.0) * scale,
|
||||
row.y + 8.0 * scale,
|
||||
&fit(&format!("{} {}", node.kind, node.name), 44),
|
||||
scale,
|
||||
TEXT,
|
||||
);
|
||||
layout.rows.push(TreeRowHit {
|
||||
rect: row,
|
||||
node_id: node.id.clone(),
|
||||
});
|
||||
}
|
||||
} else {
|
||||
text(
|
||||
&mut list,
|
||||
panel_x + 14.0 * scale,
|
||||
header_height + 48.0 * scale,
|
||||
"NO RENDER TREE",
|
||||
scale,
|
||||
MUTED,
|
||||
);
|
||||
}
|
||||
|
||||
text(
|
||||
&mut list,
|
||||
panel_x + 14.0 * scale,
|
||||
code_y + 16.0 * scale,
|
||||
"SOURCE",
|
||||
scale,
|
||||
MUTED,
|
||||
);
|
||||
draw_source(
|
||||
&mut list,
|
||||
Rect::new(panel_x, code_y, panel_width, height - code_y),
|
||||
scale,
|
||||
document,
|
||||
state.selected_node_id.as_deref(),
|
||||
);
|
||||
|
||||
(list, layout)
|
||||
}
|
||||
|
||||
fn draw_source(
|
||||
list: &mut DisplayList,
|
||||
area: Rect,
|
||||
scale: f32,
|
||||
document: &RenderDocument,
|
||||
selected_id: Option<&str>,
|
||||
) {
|
||||
if document.source.is_empty() {
|
||||
text(
|
||||
list,
|
||||
area.x + 14.0 * scale,
|
||||
area.y + 50.0 * scale,
|
||||
"NO SOURCE LOADED",
|
||||
scale,
|
||||
MUTED,
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
let all_lines: Vec<_> = document.source.lines().collect();
|
||||
let selected = selected_id.and_then(|id| document.node(id));
|
||||
let start = selected
|
||||
.map(|node| node.start_line.saturating_sub(1))
|
||||
.unwrap_or(0)
|
||||
.min(all_lines.len());
|
||||
let declared_end = selected
|
||||
.map(|node| node.end_line)
|
||||
.unwrap_or(all_lines.len());
|
||||
let end = if declared_end == 0 {
|
||||
all_lines.len()
|
||||
} else {
|
||||
declared_end.min(all_lines.len())
|
||||
};
|
||||
let line_height = 18.0 * scale;
|
||||
let first_y = area.y + 42.0 * scale;
|
||||
let maximum_lines =
|
||||
((area.y + area.height - first_y - 10.0 * scale) / line_height).max(0.0) as usize;
|
||||
let max_chars = ((area.width / (8.0 * scale)) - 9.0).max(8.0) as usize;
|
||||
|
||||
for (offset, source_line) in all_lines[start..end].iter().take(maximum_lines).enumerate() {
|
||||
let line_number = start + offset + 1;
|
||||
let y = first_y + offset as f32 * line_height;
|
||||
text(
|
||||
list,
|
||||
area.x + 12.0 * scale,
|
||||
y,
|
||||
&format!("{:>4}", line_number),
|
||||
scale,
|
||||
MUTED,
|
||||
);
|
||||
text(
|
||||
list,
|
||||
area.x + 55.0 * scale,
|
||||
y,
|
||||
&fit(&source_line.replace('\t', " "), max_chars),
|
||||
scale,
|
||||
TEXT,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
fn flatten<'a>(node: &'a CodeNode, depth: usize, output: &mut Vec<(usize, &'a CodeNode)>) {
|
||||
output.push((depth, node));
|
||||
for child in &node.children {
|
||||
flatten(child, depth + 1, output);
|
||||
}
|
||||
}
|
||||
|
||||
fn fit(value: &str, maximum: usize) -> String {
|
||||
let characters: Vec<_> = value.chars().collect();
|
||||
if characters.len() <= maximum {
|
||||
return value.to_owned();
|
||||
}
|
||||
if maximum < 3 {
|
||||
return characters.into_iter().take(maximum).collect();
|
||||
}
|
||||
characters
|
||||
.into_iter()
|
||||
.take(maximum - 3)
|
||||
.chain(['.', '.', '.'])
|
||||
.collect()
|
||||
}
|
||||
|
||||
fn fill(list: &mut DisplayList, rect: Rect, radius: f32, color: Color) {
|
||||
list.push(DrawCommand::FillRoundedRect {
|
||||
rect,
|
||||
radius,
|
||||
color,
|
||||
});
|
||||
}
|
||||
|
||||
fn text(list: &mut DisplayList, x: f32, y: f32, value: &str, scale: f32, color: Color) {
|
||||
list.push(DrawCommand::Text {
|
||||
origin: Point::new(x, y),
|
||||
value: value.to_owned(),
|
||||
scale: scale.round().max(1.0) as u32,
|
||||
color,
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user