- React SPA: dashboard, data sources CRUD, widgets CRUD, layout builder, presets. TanStack Router + Query, shadcn/ui, Vite proxy to :3000 - wire media + rss adapters into polling loop, remove xtb source type - media adapter: read username/password from headers, proper subsonic auth - event handler: subscribe to LayoutChanged, push screen update to clients - fix clippy warnings across workspace (Default impls, collapsible ifs, redundant closures, is_none_or, unused imports)
207 lines
5.2 KiB
Rust
207 lines
5.2 KiB
Rust
use client_domain::{BoundingBox, LayoutEngine, RenderTree};
|
|
use domain::{ContainerNode, Direction, LayoutChild, LayoutNode, Sizing};
|
|
|
|
fn screen() -> BoundingBox {
|
|
BoundingBox::screen(240, 320)
|
|
}
|
|
|
|
fn leaf(id: u16) -> LayoutChild {
|
|
LayoutChild {
|
|
sizing: Sizing::Flex(1),
|
|
node: LayoutNode::Leaf(id),
|
|
}
|
|
}
|
|
|
|
fn leaf_fixed(id: u16, size: u16) -> LayoutChild {
|
|
LayoutChild {
|
|
sizing: Sizing::Fixed(size),
|
|
node: LayoutNode::Leaf(id),
|
|
}
|
|
}
|
|
|
|
fn row(children: Vec<LayoutChild>) -> LayoutNode {
|
|
LayoutNode::Container(ContainerNode {
|
|
direction: Direction::Row,
|
|
gap: 0,
|
|
padding: 0,
|
|
children,
|
|
})
|
|
}
|
|
|
|
fn column(children: Vec<LayoutChild>) -> LayoutNode {
|
|
LayoutNode::Container(ContainerNode {
|
|
direction: Direction::Column,
|
|
gap: 0,
|
|
padding: 0,
|
|
children,
|
|
})
|
|
}
|
|
|
|
fn row_with_gap(gap: u8, children: Vec<LayoutChild>) -> LayoutNode {
|
|
LayoutNode::Container(ContainerNode {
|
|
direction: Direction::Row,
|
|
gap,
|
|
padding: 0,
|
|
children,
|
|
})
|
|
}
|
|
|
|
fn row_with_padding(padding: u8, children: Vec<LayoutChild>) -> LayoutNode {
|
|
LayoutNode::Container(ContainerNode {
|
|
direction: Direction::Row,
|
|
gap: 0,
|
|
padding,
|
|
children,
|
|
})
|
|
}
|
|
|
|
#[test]
|
|
fn single_leaf_fills_screen() {
|
|
let layout = LayoutNode::Leaf(1);
|
|
let tree = LayoutEngine::compute(&layout, screen());
|
|
|
|
assert_eq!(
|
|
tree.get_widget_bounds(1),
|
|
Some(&BoundingBox::new(0, 0, 240, 320))
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn row_splits_width_among_equal_flex_children() {
|
|
let layout = row(vec![leaf(1), leaf(2), leaf(3)]);
|
|
let tree = LayoutEngine::compute(&layout, screen());
|
|
|
|
assert_eq!(
|
|
tree.get_widget_bounds(1),
|
|
Some(&BoundingBox::new(0, 0, 80, 320))
|
|
);
|
|
assert_eq!(
|
|
tree.get_widget_bounds(2),
|
|
Some(&BoundingBox::new(80, 0, 80, 320))
|
|
);
|
|
assert_eq!(
|
|
tree.get_widget_bounds(3),
|
|
Some(&BoundingBox::new(160, 0, 80, 320))
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn column_splits_height_among_equal_flex_children() {
|
|
let layout = column(vec![leaf(1), leaf(2)]);
|
|
let tree = LayoutEngine::compute(&layout, screen());
|
|
|
|
assert_eq!(
|
|
tree.get_widget_bounds(1),
|
|
Some(&BoundingBox::new(0, 0, 240, 160))
|
|
);
|
|
assert_eq!(
|
|
tree.get_widget_bounds(2),
|
|
Some(&BoundingBox::new(0, 160, 240, 160))
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn fixed_and_flex_children_coexist() {
|
|
let layout = row(vec![leaf_fixed(1, 40), leaf(2)]);
|
|
let tree = LayoutEngine::compute(&layout, screen());
|
|
|
|
assert_eq!(
|
|
tree.get_widget_bounds(1),
|
|
Some(&BoundingBox::new(0, 0, 40, 320))
|
|
);
|
|
assert_eq!(
|
|
tree.get_widget_bounds(2),
|
|
Some(&BoundingBox::new(40, 0, 200, 320))
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn gap_is_subtracted_before_distributing_space() {
|
|
// 240px wide, 2 children, gap=10 → 230px available, 115px each
|
|
let layout = row_with_gap(10, vec![leaf(1), leaf(2)]);
|
|
let tree = LayoutEngine::compute(&layout, screen());
|
|
|
|
assert_eq!(
|
|
tree.get_widget_bounds(1),
|
|
Some(&BoundingBox::new(0, 0, 115, 320))
|
|
);
|
|
assert_eq!(
|
|
tree.get_widget_bounds(2),
|
|
Some(&BoundingBox::new(125, 0, 115, 320))
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn padding_insets_available_area() {
|
|
// padding=10 → inner area starts at (10,10), size (220,300)
|
|
let layout = row_with_padding(10, vec![leaf(1)]);
|
|
let tree = LayoutEngine::compute(&layout, screen());
|
|
|
|
assert_eq!(
|
|
tree.get_widget_bounds(1),
|
|
Some(&BoundingBox::new(10, 10, 220, 300))
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn nested_containers_compute_correctly() {
|
|
// Row with [leaf(1), column([leaf(2), leaf(3)])]
|
|
let inner_col = LayoutChild {
|
|
sizing: Sizing::Flex(1),
|
|
node: column(vec![leaf(2), leaf(3)]),
|
|
};
|
|
let layout = row(vec![leaf(1), inner_col]);
|
|
let tree = LayoutEngine::compute(&layout, screen());
|
|
|
|
assert_eq!(
|
|
tree.get_widget_bounds(1),
|
|
Some(&BoundingBox::new(0, 0, 120, 320))
|
|
);
|
|
assert_eq!(
|
|
tree.get_widget_bounds(2),
|
|
Some(&BoundingBox::new(120, 0, 120, 160))
|
|
);
|
|
assert_eq!(
|
|
tree.get_widget_bounds(3),
|
|
Some(&BoundingBox::new(120, 160, 120, 160))
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn weighted_flex_distributes_proportionally() {
|
|
// weights [1, 2, 1] → 25%, 50%, 25% of 240 = 60, 120, 60
|
|
let layout = LayoutNode::Container(ContainerNode {
|
|
direction: Direction::Row,
|
|
gap: 0,
|
|
padding: 0,
|
|
children: vec![
|
|
LayoutChild {
|
|
sizing: Sizing::Flex(1),
|
|
node: LayoutNode::Leaf(1),
|
|
},
|
|
LayoutChild {
|
|
sizing: Sizing::Flex(2),
|
|
node: LayoutNode::Leaf(2),
|
|
},
|
|
LayoutChild {
|
|
sizing: Sizing::Flex(1),
|
|
node: LayoutNode::Leaf(3),
|
|
},
|
|
],
|
|
});
|
|
let tree = LayoutEngine::compute(&layout, screen());
|
|
|
|
assert_eq!(
|
|
tree.get_widget_bounds(1),
|
|
Some(&BoundingBox::new(0, 0, 60, 320))
|
|
);
|
|
assert_eq!(
|
|
tree.get_widget_bounds(2),
|
|
Some(&BoundingBox::new(60, 0, 120, 320))
|
|
);
|
|
assert_eq!(
|
|
tree.get_widget_bounds(3),
|
|
Some(&BoundingBox::new(180, 0, 60, 320))
|
|
);
|
|
}
|