add all crates: domain, protocol, application, client, adapters, ESP32 firmware
Server: domain (entities, value objects, ports), protocol (postcard wire types), application (config service, data projection), adapters (config-memory, tcp-server), bootstrap (composition root with fake data). Client: client-domain (layout engine, render tree, HAL ports), client-application (message handling, repaint commands), adapters (tcp-client, display-terminal), client-desktop (end-to-end working). ESP32: client-esp32 firmware with ILI9341 display over SPI, WiFi networking. Display test verified on hardware — landscape orientation, text rendering works. 60 workspace tests, all passing.
This commit is contained in:
9
crates/client-domain/Cargo.toml
Normal file
9
crates/client-domain/Cargo.toml
Normal file
@@ -0,0 +1,9 @@
|
||||
[package]
|
||||
name = "client-domain"
|
||||
version = "0.1.0"
|
||||
edition = "2024"
|
||||
|
||||
[dependencies]
|
||||
domain.workspace = true
|
||||
|
||||
[dev-dependencies]
|
||||
17
crates/client-domain/src/bounding_box.rs
Normal file
17
crates/client-domain/src/bounding_box.rs
Normal file
@@ -0,0 +1,17 @@
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub struct BoundingBox {
|
||||
pub x: u16,
|
||||
pub y: u16,
|
||||
pub width: u16,
|
||||
pub height: u16,
|
||||
}
|
||||
|
||||
impl BoundingBox {
|
||||
pub fn new(x: u16, y: u16, width: u16, height: u16) -> Self {
|
||||
Self { x, y, width, height }
|
||||
}
|
||||
|
||||
pub fn screen(width: u16, height: u16) -> Self {
|
||||
Self { x: 0, y: 0, width, height }
|
||||
}
|
||||
}
|
||||
96
crates/client-domain/src/layout_engine.rs
Normal file
96
crates/client-domain/src/layout_engine.rs
Normal file
@@ -0,0 +1,96 @@
|
||||
use std::collections::HashMap;
|
||||
use domain::{LayoutNode, ContainerNode, Direction, Sizing};
|
||||
use crate::{BoundingBox, RenderTree};
|
||||
|
||||
pub struct LayoutEngine;
|
||||
|
||||
impl LayoutEngine {
|
||||
pub fn compute(layout: &LayoutNode, bounds: BoundingBox) -> RenderTree {
|
||||
let mut widget_bounds = HashMap::new();
|
||||
Self::compute_node(layout, bounds, &mut widget_bounds);
|
||||
RenderTree { widget_bounds }
|
||||
}
|
||||
|
||||
fn compute_node(
|
||||
node: &LayoutNode,
|
||||
bounds: BoundingBox,
|
||||
out: &mut HashMap<u16, BoundingBox>,
|
||||
) {
|
||||
match node {
|
||||
LayoutNode::Leaf(id) => {
|
||||
out.insert(*id, bounds);
|
||||
}
|
||||
LayoutNode::Container(container) => {
|
||||
Self::compute_container(container, bounds, out);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn compute_container(
|
||||
container: &ContainerNode,
|
||||
bounds: BoundingBox,
|
||||
out: &mut HashMap<u16, BoundingBox>,
|
||||
) {
|
||||
let inner = BoundingBox::new(
|
||||
bounds.x + container.padding as u16,
|
||||
bounds.y + container.padding as u16,
|
||||
bounds.width.saturating_sub(container.padding as u16 * 2),
|
||||
bounds.height.saturating_sub(container.padding as u16 * 2),
|
||||
);
|
||||
|
||||
let children = &container.children;
|
||||
if children.is_empty() {
|
||||
return;
|
||||
}
|
||||
|
||||
let is_row = container.direction == Direction::Row;
|
||||
let total_axis = if is_row { inner.width } else { inner.height };
|
||||
let total_gap = container.gap as u16 * (children.len() as u16).saturating_sub(1);
|
||||
let available = total_axis.saturating_sub(total_gap);
|
||||
|
||||
let fixed_total: u16 = children.iter().map(|c| match c.sizing {
|
||||
Sizing::Fixed(px) => px,
|
||||
Sizing::Flex(_) => 0,
|
||||
}).sum();
|
||||
|
||||
let flex_space = available.saturating_sub(fixed_total);
|
||||
let flex_total: u16 = children.iter().map(|c| match c.sizing {
|
||||
Sizing::Flex(w) => w as u16,
|
||||
Sizing::Fixed(_) => 0,
|
||||
}).sum();
|
||||
|
||||
let mut offset = 0u16;
|
||||
|
||||
for child in children {
|
||||
let child_size = match child.sizing {
|
||||
Sizing::Fixed(px) => px,
|
||||
Sizing::Flex(w) => {
|
||||
if flex_total > 0 {
|
||||
(flex_space as u32 * w as u32 / flex_total as u32) as u16
|
||||
} else {
|
||||
0
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
let child_bounds = if is_row {
|
||||
BoundingBox::new(
|
||||
inner.x + offset,
|
||||
inner.y,
|
||||
child_size,
|
||||
inner.height,
|
||||
)
|
||||
} else {
|
||||
BoundingBox::new(
|
||||
inner.x,
|
||||
inner.y + offset,
|
||||
inner.width,
|
||||
child_size,
|
||||
)
|
||||
};
|
||||
|
||||
Self::compute_node(&child.node, child_bounds, out);
|
||||
offset += child_size + container.gap as u16;
|
||||
}
|
||||
}
|
||||
}
|
||||
9
crates/client-domain/src/lib.rs
Normal file
9
crates/client-domain/src/lib.rs
Normal file
@@ -0,0 +1,9 @@
|
||||
mod bounding_box;
|
||||
mod layout_engine;
|
||||
mod render_tree;
|
||||
pub mod ports;
|
||||
|
||||
pub use bounding_box::BoundingBox;
|
||||
pub use layout_engine::LayoutEngine;
|
||||
pub use render_tree::RenderTree;
|
||||
pub use ports::{DisplayPort, NetworkPort, StoragePort, ClientConfig};
|
||||
11
crates/client-domain/src/ports/display.rs
Normal file
11
crates/client-domain/src/ports/display.rs
Normal file
@@ -0,0 +1,11 @@
|
||||
use crate::BoundingBox;
|
||||
|
||||
pub trait DisplayPort {
|
||||
type Error;
|
||||
|
||||
fn clear_region(&mut self, bounds: BoundingBox) -> Result<(), Self::Error>;
|
||||
fn draw_text(&mut self, text: &str, x: u16, y: u16, bounds: BoundingBox) -> Result<(), Self::Error>;
|
||||
fn draw_icon(&mut self, icon: &str, x: u16, y: u16) -> Result<(), Self::Error>;
|
||||
fn fill_background(&mut self, bounds: BoundingBox) -> Result<(), Self::Error>;
|
||||
fn flush(&mut self) -> Result<(), Self::Error>;
|
||||
}
|
||||
7
crates/client-domain/src/ports/mod.rs
Normal file
7
crates/client-domain/src/ports/mod.rs
Normal file
@@ -0,0 +1,7 @@
|
||||
mod display;
|
||||
mod network;
|
||||
mod storage;
|
||||
|
||||
pub use display::DisplayPort;
|
||||
pub use network::NetworkPort;
|
||||
pub use storage::{StoragePort, ClientConfig};
|
||||
9
crates/client-domain/src/ports/network.rs
Normal file
9
crates/client-domain/src/ports/network.rs
Normal file
@@ -0,0 +1,9 @@
|
||||
pub trait NetworkPort {
|
||||
type Error;
|
||||
|
||||
fn connect(&mut self, addr: &str) -> Result<(), Self::Error>;
|
||||
fn disconnect(&mut self) -> Result<(), Self::Error>;
|
||||
fn send(&mut self, data: &[u8]) -> Result<(), Self::Error>;
|
||||
fn receive(&mut self) -> Result<Option<Vec<u8>>, Self::Error>;
|
||||
fn is_connected(&self) -> bool;
|
||||
}
|
||||
11
crates/client-domain/src/ports/storage.rs
Normal file
11
crates/client-domain/src/ports/storage.rs
Normal file
@@ -0,0 +1,11 @@
|
||||
pub struct ClientConfig {
|
||||
pub wifi_ssid: String,
|
||||
pub wifi_password: String,
|
||||
pub server_addr: String,
|
||||
}
|
||||
|
||||
pub trait StoragePort {
|
||||
type Error;
|
||||
|
||||
fn load_config(&self) -> Result<ClientConfig, Self::Error>;
|
||||
}
|
||||
29
crates/client-domain/src/render_tree.rs
Normal file
29
crates/client-domain/src/render_tree.rs
Normal file
@@ -0,0 +1,29 @@
|
||||
use std::collections::HashMap;
|
||||
use domain::WidgetId;
|
||||
use crate::BoundingBox;
|
||||
|
||||
pub struct RenderTree {
|
||||
pub widget_bounds: HashMap<WidgetId, BoundingBox>,
|
||||
}
|
||||
|
||||
impl RenderTree {
|
||||
pub fn get_widget_bounds(&self, id: WidgetId) -> Option<&BoundingBox> {
|
||||
self.widget_bounds.get(&id)
|
||||
}
|
||||
|
||||
pub fn diff(&self, other: &RenderTree) -> Vec<WidgetId> {
|
||||
let mut changed = Vec::new();
|
||||
for (id, bounds) in &self.widget_bounds {
|
||||
match other.widget_bounds.get(id) {
|
||||
Some(prev) if prev == bounds => {}
|
||||
_ => changed.push(*id),
|
||||
}
|
||||
}
|
||||
for id in other.widget_bounds.keys() {
|
||||
if !self.widget_bounds.contains_key(id) {
|
||||
changed.push(*id);
|
||||
}
|
||||
}
|
||||
changed
|
||||
}
|
||||
}
|
||||
151
crates/client-domain/tests/layout_engine_tests.rs
Normal file
151
crates/client-domain/tests/layout_engine_tests.rs
Normal file
@@ -0,0 +1,151 @@
|
||||
use domain::{
|
||||
LayoutNode, ContainerNode, LayoutChild, Direction, Sizing,
|
||||
};
|
||||
use client_domain::{BoundingBox, LayoutEngine, RenderTree};
|
||||
|
||||
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)));
|
||||
}
|
||||
85
crates/client-domain/tests/render_tree_tests.rs
Normal file
85
crates/client-domain/tests/render_tree_tests.rs
Normal file
@@ -0,0 +1,85 @@
|
||||
use domain::{
|
||||
LayoutNode, ContainerNode, LayoutChild, Direction, Sizing,
|
||||
};
|
||||
use client_domain::{BoundingBox, LayoutEngine};
|
||||
|
||||
fn screen() -> BoundingBox {
|
||||
BoundingBox::screen(240, 320)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn diff_detects_moved_widget_after_layout_change() {
|
||||
let layout_a = 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(1), node: LayoutNode::Leaf(2) },
|
||||
],
|
||||
});
|
||||
|
||||
let layout_b = LayoutNode::Container(ContainerNode {
|
||||
direction: Direction::Column,
|
||||
gap: 0,
|
||||
padding: 0,
|
||||
children: vec![
|
||||
LayoutChild { sizing: Sizing::Flex(1), node: LayoutNode::Leaf(1) },
|
||||
LayoutChild { sizing: Sizing::Flex(1), node: LayoutNode::Leaf(2) },
|
||||
],
|
||||
});
|
||||
|
||||
let tree_a = LayoutEngine::compute(&layout_a, screen());
|
||||
let tree_b = LayoutEngine::compute(&layout_b, screen());
|
||||
|
||||
let mut changed = tree_b.diff(&tree_a);
|
||||
changed.sort();
|
||||
assert_eq!(changed, vec![1, 2]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn diff_returns_empty_for_identical_layouts() {
|
||||
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(1), node: LayoutNode::Leaf(2) },
|
||||
],
|
||||
});
|
||||
|
||||
let tree_a = LayoutEngine::compute(&layout, screen());
|
||||
let tree_b = LayoutEngine::compute(&layout, screen());
|
||||
|
||||
assert!(tree_b.diff(&tree_a).is_empty());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn diff_detects_added_and_removed_widgets() {
|
||||
let layout_a = LayoutNode::Container(ContainerNode {
|
||||
direction: Direction::Row,
|
||||
gap: 0,
|
||||
padding: 0,
|
||||
children: vec![
|
||||
LayoutChild { sizing: Sizing::Flex(1), node: LayoutNode::Leaf(1) },
|
||||
],
|
||||
});
|
||||
|
||||
let layout_b = LayoutNode::Container(ContainerNode {
|
||||
direction: Direction::Row,
|
||||
gap: 0,
|
||||
padding: 0,
|
||||
children: vec![
|
||||
LayoutChild { sizing: Sizing::Flex(1), node: LayoutNode::Leaf(2) },
|
||||
],
|
||||
});
|
||||
|
||||
let tree_a = LayoutEngine::compute(&layout_a, screen());
|
||||
let tree_b = LayoutEngine::compute(&layout_b, screen());
|
||||
|
||||
let mut changed = tree_b.diff(&tree_a);
|
||||
changed.sort();
|
||||
// widget 1 was removed (in old but not new), widget 2 was added (in new but not old)
|
||||
assert_eq!(changed, vec![1, 2]);
|
||||
}
|
||||
Reference in New Issue
Block a user