new rendering engine

This commit is contained in:
2026-06-19 02:55:33 +02:00
parent 0a90d6a5d7
commit 81a4167382
53 changed files with 1668 additions and 378 deletions

View File

@@ -1,55 +1,63 @@
use std::sync::mpsc;
use client_domain::{BoundingBox, DisplayPort};
use client_application::ClientApp;
use std::time::{Duration, Instant};
use std::collections::HashMap;
use client_domain::{
BoundingBox, DisplayPort, FontMetrics, RenderEngine, ScrollState, ThemeConfig,
};
use client_application::{ClientApp, RepaintCommand};
use domain::{DisplayHint, Value};
use protocol::ServerMessage;
use crate::config::{RENDER_POLL_INTERVAL, SCREEN};
use crate::config::RENDER_POLL_INTERVAL;
use crate::adapters::display::Esp32DisplayAdapter;
use log::*;
const LINE_HEIGHT: u16 = 12;
const TEXT_PADDING: u16 = 4;
const SCROLL_TICK: Duration = Duration::from_millis(50);
struct WidgetCache {
hint: DisplayHint,
data: Vec<(String, Value)>,
bounds: BoundingBox,
scroll: ScrollState,
}
pub fn run(
screen: BoundingBox,
mut display: Esp32DisplayAdapter,
rx: mpsc::Receiver<ServerMessage>,
) {
let metrics = FontMetrics {
small: (6, 10),
large: (10, 20),
};
let mut engine = RenderEngine::new(metrics, ThemeConfig::default());
let mut app = ClientApp::new(screen);
let mut widgets: HashMap<u16, WidgetCache> = HashMap::new();
let mut first_update = true;
let mut last_tick = Instant::now();
info!("Render loop started");
loop {
match rx.recv_timeout(RENDER_POLL_INTERVAL) {
let timeout = RENDER_POLL_INTERVAL.min(SCROLL_TICK);
match rx.recv_timeout(timeout) {
Ok(msg) => {
let is_screen_update = matches!(msg, ServerMessage::ScreenUpdate { .. });
let repaints = app.handle_message(msg);
if app.take_theme_changed() {
engine.set_theme(app.theme().clone());
}
if !repaints.is_empty() && (first_update || is_screen_update) {
display.fill_background(SCREEN).unwrap();
let bg = engine.theme().background;
display.fill_rect(screen, bg).unwrap();
first_update = false;
}
for cmd in &repaints {
display.clear_region(cmd.bounds).unwrap();
let mut y_offset = TEXT_PADDING;
for kv in &cmd.state.data {
if let protocol::WireValue::String(s) = &kv.value {
let text_bounds = BoundingBox::new(
cmd.bounds.x + TEXT_PADDING,
cmd.bounds.y + y_offset,
cmd.bounds.width.saturating_sub(TEXT_PADDING * 2),
LINE_HEIGHT,
);
display.draw_text(
&format!("{}: {s}", kv.key),
text_bounds.x,
text_bounds.y,
text_bounds,
).unwrap();
y_offset += LINE_HEIGHT + 2;
}
}
let cache = update_cache(&engine, cmd);
draw_widget(&engine, &mut display, &cache);
widgets.insert(cmd.widget_id, cache);
}
if !repaints.is_empty() {
@@ -62,5 +70,57 @@ pub fn run(
break;
}
}
let now = Instant::now();
let elapsed = now.duration_since(last_tick);
last_tick = now;
let mut needs_flush = false;
for cache in widgets.values_mut() {
if cache.scroll.tick(elapsed) {
let bg = engine.theme().background;
display.fill_rect(cache.bounds, bg).unwrap();
draw_widget(&engine, &mut display, cache);
needs_flush = true;
}
}
if needs_flush {
display.flush().unwrap();
}
}
}
fn update_cache(engine: &RenderEngine, cmd: &RepaintCommand) -> WidgetCache {
let hint: DisplayHint = cmd.display_hint.clone().into();
let data: Vec<(String, Value)> = cmd.state.data
.iter()
.map(|kv| (kv.key.clone(), kv.value.clone().into()))
.collect();
let content_h = engine.content_height(&hint, &data, cmd.bounds.width);
let scroll = ScrollState::new(cmd.bounds.height, content_h);
WidgetCache {
hint,
data,
bounds: cmd.bounds,
scroll,
}
}
fn draw_widget(
engine: &RenderEngine,
display: &mut Esp32DisplayAdapter,
cache: &WidgetCache,
) {
let draw_cmds = engine.render_widget(
&cache.hint,
&cache.data,
cache.bounds,
cache.scroll.offset(),
);
for dc in &draw_cmds {
display.draw_text_span(&dc.text, dc.x, dc.y, dc.color, dc.font).unwrap();
}
}