telar 0.1.5

A modular Rust UI framework with its own template language, reactive signals and a self-contained renderer.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
use std::borrow::Cow;
use std::collections::VecDeque;
use std::time::{Duration, Instant};

use crate::dev_plugin::{DevAction, DevPlugin};
use geometry_core::Rect;
use platform_core::{Key, ModifiersState};
use renderer_core::{
    BorderRadius, Color, DrawCommand, Paint, RectStyle, ShapeStyle, Stroke, TextStyle,
};
use ui_tree::SegmentNodeInfo;

fn rect_command(rect: Rect, style: RectStyle) -> DrawCommand {
    DrawCommand::Rect {
        rect,
        style: std::sync::Arc::new(style),
    }
}

fn text_command(text: std::sync::Arc<str>, rect: Rect, style: TextStyle) -> DrawCommand {
    DrawCommand::Text {
        text,
        rect,
        style: std::sync::Arc::new(style),
    }
}

const BADGE_WIDTH: f32 = 100.0;
const BADGE_HEIGHT: f32 = 24.0;
const MARGIN: f32 = 10.0;
const PANEL_WIDTH: f32 = 200.0;
const PANEL_HEIGHT: f32 = 178.0;
const GAP: f32 = 4.0;

const INSPECTOR_WIDTH: f32 = 300.0;
const INSPECTOR_BACKGROUND: Color = Color::rgba(0.08, 0.08, 0.12, 0.92);
const INSPECTOR_SELECTION_BACKGROUND: Color = Color::rgba(0.2, 0.4, 0.8, 0.25);
const HIGHLIGHT_FILL: Color = Color::rgba(0.2, 0.5, 1.0, 0.18);
const HIGHLIGHT_BORDER: Color = Color::rgba(0.3, 0.6, 1.0, 0.85);
const ROW_HEIGHT: f32 = 20.0;
const INSPECTOR_HEADER_HEIGHT: f32 = 32.0;

const PANEL_BACKGROUND: Color = Color::rgba(0.05, 0.05, 0.05, 0.75);
const BADGE_BACKGROUND: Color = Color::rgba(0.0, 0.0, 0.0, 0.70);
const BACKDROP_BLUR_SIGMA: f32 = 12.0;
const GREEN: Color = Color::rgba(0.0, 1.0, 0.4, 1.0);
const WHITE: Color = Color::rgba(0.9, 0.9, 0.9, 1.0);
const GRAY: Color = Color::rgba(0.5, 0.5, 0.5, 1.0);
const GRAY_DIM: Color = Color::rgba(0.3, 0.3, 0.3, 1.0);

// Rolling window duration — frames older than this are dropped from the count.
const FPS_WINDOW: Duration = Duration::from_secs(1);
const KEEPALIVE_INTERVAL: Duration = Duration::from_millis(1000);

pub struct DevTools {
    frame_times: VecDeque<Instant>,
    last_fps: u32,
    panel_open: bool,
    badge_rect: Rect,
    renderer_info: Option<String>,
    build_error: Option<String>,
    inspector_open: bool,
    selected_node: Option<u64>,
    nodes: Vec<SegmentNodeInfo>,
    inspector_rect: Rect,
    frame_time_millis: f32,
    node_count: usize,
}

impl Default for DevTools {
    fn default() -> Self {
        Self {
            frame_times: VecDeque::new(),
            last_fps: 0,
            panel_open: false,
            badge_rect: Rect::default(),
            renderer_info: None,
            build_error: None,
            inspector_open: false,
            selected_node: None,
            nodes: Vec::new(),
            inspector_rect: Rect::default(),
            frame_time_millis: 0.0,
            node_count: 0,
        }
    }
}

impl DevPlugin for DevTools {
    fn set_renderer_info(&mut self, info: &str) {
        self.renderer_info = Some(info.to_owned());
    }

    fn on_frame<'a>(
        &mut self,
        base: &'a [DrawCommand],
        window_w: f32,
        window_h: f32,
        tree_dirty: bool,
    ) -> Cow<'a, [DrawCommand]> {
        let now = Instant::now();
        // Only sample on content frames so hardware keepalive blits don't pollute the count.
        if tree_dirty {
            self.frame_times.push_back(now);
        }
        let cutoff = now - FPS_WINDOW;
        while self.frame_times.front().is_some_and(|t| *t <= cutoff) {
            self.frame_times.pop_front();
        }
        self.last_fps = self.frame_times.len() as u32;

        self.frame_time_millis = if self.frame_times.len() >= 2 {
            let oldest = self.frame_times[0];
            let newest = self.frame_times[self.frame_times.len() - 1];
            let elapsed_ms = newest.duration_since(oldest).as_secs_f32() * 1000.0;
            elapsed_ms / (self.frame_times.len() - 1) as f32
        } else {
            0.0
        };

        let badge_x = window_w - BADGE_WIDTH - MARGIN;
        let badge_y = window_h - BADGE_HEIGHT - MARGIN;
        self.badge_rect = Rect::new(badge_x, badge_y, BADGE_WIDTH, BADGE_HEIGHT);

        let mut cmds = Vec::with_capacity(base.len() + 16);
        cmds.extend_from_slice(base);

        if self.inspector_open {
            // Selected node highlight drawn on the canvas, behind the inspector panel.
            if let Some(selected_id) = self.selected_node
                && let Some(node) = self.nodes.iter().find(|n| n.id == selected_id)
                && node.rect.width > 0.0
                && node.rect.height > 0.0
            {
                cmds.push(rect_command(
                    node.rect,
                    RectStyle::default()
                        .with_fill(Paint::Solid(HIGHLIGHT_FILL))
                        .with_stroke(Stroke::new(HIGHLIGHT_BORDER, 1.5)),
                ));
            }

            let panel_rect = Rect::new(0.0, 0.0, INSPECTOR_WIDTH, window_h);
            self.inspector_rect = panel_rect;
            cmds.push(rect_command(
                panel_rect,
                RectStyle::default().with_fill(Paint::Solid(INSPECTOR_BACKGROUND)),
            ));

            let header_text = format!("Inspector  {} nodes", self.nodes.len());
            cmds.push(text_command(
                header_text.into(),
                Rect::new(12.0, 8.0, INSPECTOR_WIDTH - 24.0, 18.0),
                TextStyle::new(12.0, WHITE),
            ));
            cmds.push(rect_command(
                Rect::new(0.0, INSPECTOR_HEADER_HEIGHT, INSPECTOR_WIDTH, 1.0),
                RectStyle::default().with_fill(Paint::Solid(GRAY_DIM)),
            ));

            let max_visible = ((window_h - INSPECTOR_HEADER_HEIGHT) / ROW_HEIGHT) as usize;
            for (i, node) in self.nodes.iter().take(max_visible).enumerate() {
                let row_y = INSPECTOR_HEADER_HEIGHT + i as f32 * ROW_HEIGHT;
                let is_selected = self.selected_node == Some(node.id);

                if is_selected {
                    cmds.push(rect_command(
                        Rect::new(0.0, row_y, INSPECTOR_WIDTH, ROW_HEIGHT),
                        RectStyle::default()
                            .with_fill(Paint::Solid(INSPECTOR_SELECTION_BACKGROUND)),
                    ));
                }

                let indent = node.depth as f32 * 8.0;
                let r = node.rect;
                let label = format!("{}  {:.0}\u{00d7}{:.0}", node.name, r.width, r.height);
                cmds.push(text_command(
                    label.into(),
                    Rect::new(
                        12.0 + indent,
                        row_y + 3.0,
                        INSPECTOR_WIDTH - 24.0 - indent,
                        ROW_HEIGHT - 6.0,
                    ),
                    TextStyle::new(10.0, if is_selected { WHITE } else { GRAY }),
                ));
            }
        }

        // Badge wrapped in a clip + backdrop-blur layer so the semi-transparent fill samples a blurred copy of the underlying content.
        cmds.push(DrawCommand::PushClip {
            rect: self.badge_rect,
            radius: BorderRadius::all(4.0),
        });
        cmds.push(DrawCommand::PushLayer {
            opacity: 1.0,
            backdrop_blur: BACKDROP_BLUR_SIGMA,
        });

        cmds.push(rect_command(
            self.badge_rect,
            RectStyle::default()
                .with_fill(Paint::Solid(BADGE_BACKGROUND))
                .with_radius(BorderRadius::all(4.0)),
        ));

        let badge_label = format!("DEV \u{2022} {} fps", self.last_fps);
        cmds.push(text_command(
            badge_label.into(),
            Rect::new(
                badge_x + 8.0,
                badge_y + 5.0,
                BADGE_WIDTH - 16.0,
                BADGE_HEIGHT - 10.0,
            ),
            TextStyle::new(12.0, GREEN),
        ));

        cmds.push(DrawCommand::PopLayer);
        cmds.push(DrawCommand::PopClip);

        if self.panel_open {
            let panel_x = window_w - PANEL_WIDTH - MARGIN;
            let panel_y = badge_y - PANEL_HEIGHT - GAP;

            cmds.push(DrawCommand::PushClip {
                rect: Rect::new(panel_x, panel_y, PANEL_WIDTH, PANEL_HEIGHT),
                radius: BorderRadius::all(8.0),
            });
            cmds.push(DrawCommand::PushLayer {
                opacity: 1.0,
                backdrop_blur: BACKDROP_BLUR_SIGMA,
            });

            cmds.push(rect_command(
                Rect::new(panel_x, panel_y, PANEL_WIDTH, PANEL_HEIGHT),
                RectStyle::default()
                    .with_fill(Paint::Solid(PANEL_BACKGROUND))
                    .with_radius(BorderRadius::all(8.0)),
            ));

            cmds.push(text_command(
                "rsx devtools".into(),
                Rect::new(panel_x + 12.0, panel_y + 12.0, PANEL_WIDTH - 24.0, 16.0),
                TextStyle::new(11.0, WHITE),
            ));

            cmds.push(rect_command(
                Rect::new(panel_x + 12.0, panel_y + 36.0, PANEL_WIDTH - 24.0, 1.0),
                RectStyle::default().with_fill(Paint::Solid(GRAY_DIM)),
            ));

            let fps_label = format!("{} fps", self.last_fps);
            cmds.push(text_command(
                fps_label.into(),
                Rect::new(panel_x + 12.0, panel_y + 44.0, PANEL_WIDTH - 24.0, 28.0),
                TextStyle::new(20.0, GREEN),
            ));

            let frame_time_label = format!(
                "{:.1} ms/frame  {} nodes",
                self.frame_time_millis, self.node_count
            );
            cmds.push(text_command(
                frame_time_label.into(),
                Rect::new(panel_x + 12.0, panel_y + 70.0, PANEL_WIDTH - 24.0, 14.0),
                TextStyle::new(10.0, GRAY),
            ));

            let renderer_text_y = if let Some(ref info) = self.renderer_info {
                let renderer_label = format!("renderer: {}", info);
                cmds.push(text_command(
                    renderer_label.into(),
                    Rect::new(panel_x + 12.0, panel_y + 90.0, PANEL_WIDTH - 24.0, 16.0),
                    TextStyle::new(11.0, GRAY),
                ));
                108.0
            } else {
                90.0
            };

            cmds.push(text_command(
                "ctrl+shift+b  toggle renderer".into(),
                Rect::new(
                    panel_x + 12.0,
                    panel_y + renderer_text_y,
                    PANEL_WIDTH - 24.0,
                    14.0,
                ),
                TextStyle::new(10.0, GRAY_DIM),
            ));

            cmds.push(text_command(
                "ctrl+shift+i  inspector".into(),
                Rect::new(
                    panel_x + 12.0,
                    panel_y + renderer_text_y + 16.0,
                    PANEL_WIDTH - 24.0,
                    14.0,
                ),
                TextStyle::new(10.0, GRAY_DIM),
            ));

            cmds.push(text_command(
                "click badge  close".into(),
                Rect::new(
                    panel_x + 12.0,
                    panel_y + renderer_text_y + 32.0,
                    PANEL_WIDTH - 24.0,
                    14.0,
                ),
                TextStyle::new(10.0, GRAY_DIM),
            ));

            cmds.push(DrawCommand::PopLayer);
            cmds.push(DrawCommand::PopClip);
        }

        // Error banner — shown on top of everything when a build fails
        if let Some(ref error_msg) = self.build_error {
            const BANNER_PAD: f32 = 16.0;
            const BANNER_LINE_HEIGHT: f32 = 16.0;
            const ERROR_BACKGROUND: Color = Color::rgba(0.7, 0.1, 0.1, 0.92);
            const ERROR_TEXT: Color = Color::rgba(1.0, 0.9, 0.9, 1.0);
            const TITLE_COLOR: Color = Color::rgba(1.0, 0.5, 0.5, 1.0);

            let lines: Vec<&str> = error_msg.lines().take(20).collect();
            let banner_h = BANNER_PAD * 2.0
                + BANNER_LINE_HEIGHT
                + lines.len() as f32 * (BANNER_LINE_HEIGHT + 2.0);
            let banner_rect = Rect::new(0.0, 0.0, window_w, banner_h);

            cmds.push(rect_command(
                banner_rect,
                RectStyle::default().with_fill(Paint::Solid(ERROR_BACKGROUND)),
            ));
            cmds.push(text_command(
                "Build failed".into(),
                Rect::new(
                    BANNER_PAD,
                    BANNER_PAD,
                    window_w - BANNER_PAD * 2.0,
                    BANNER_LINE_HEIGHT,
                ),
                TextStyle::new(13.0, TITLE_COLOR),
            ));
            for (i, line) in lines.iter().enumerate() {
                let y =
                    BANNER_PAD + BANNER_LINE_HEIGHT + 4.0 + i as f32 * (BANNER_LINE_HEIGHT + 2.0);
                cmds.push(text_command(
                    (*line).to_string().into(),
                    Rect::new(
                        BANNER_PAD,
                        y,
                        window_w - BANNER_PAD * 2.0,
                        BANNER_LINE_HEIGHT,
                    ),
                    TextStyle::new(11.0, ERROR_TEXT),
                ));
            }
        }

        Cow::Owned(cmds)
    }

    fn keepalive_interval(&self) -> Option<Duration> {
        Some(KEEPALIVE_INTERVAL)
    }

    fn on_key(&mut self, key: &Key, modifiers: ModifiersState) -> DevAction {
        if modifiers.is_ctrl && modifiers.is_shift {
            match key {
                Key::Char('b' | 'B') => return DevAction::ToggleBackend,
                Key::Char('d' | 'D') => {
                    self.panel_open = !self.panel_open;
                    return DevAction::Redraw;
                }
                Key::Char('i' | 'I') => {
                    self.inspector_open = !self.inspector_open;
                    return DevAction::Redraw;
                }
                _ => {}
            }
        }
        DevAction::None
    }

    fn on_pointer_pressed(&mut self, x: f32, y: f32) -> bool {
        if self.inspector_open && self.inspector_rect.contains(x, y) {
            let list_y = y - INSPECTOR_HEADER_HEIGHT;
            if list_y >= 0.0 {
                let idx = (list_y / ROW_HEIGHT) as usize;
                if let Some(node) = self.nodes.get(idx) {
                    self.selected_node = Some(node.id);
                }
            }
            return true;
        }

        // Canvas click with inspector open: select the deepest node under the cursor.
        if self.inspector_open {
            let clicked = self
                .nodes
                .iter()
                .filter(|n| n.rect.width > 0.0 && n.rect.height > 0.0 && n.rect.contains(x, y))
                .max_by_key(|n| n.depth);
            if let Some(node) = clicked {
                self.selected_node = Some(node.id);
                return true;
            }
        }

        if self.badge_rect.contains(x, y) {
            self.panel_open = !self.panel_open;
            return true;
        }
        false
    }

    fn set_build_error(&mut self, error: Option<String>) {
        self.build_error = error;
    }

    fn on_tree(&mut self, nodes: &[SegmentNodeInfo]) {
        self.node_count = nodes.len();
        self.nodes.clear();
        self.nodes.extend_from_slice(nodes);
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    fn open_panel(dev: &mut DevTools) {
        dev.on_key(
            &Key::Char('d'),
            ModifiersState {
                is_ctrl: true,
                is_shift: true,
                ..ModifiersState::default()
            },
        );
    }

    fn panel_text(dev: &mut DevTools) -> Vec<String> {
        dev.on_frame(&[], 800.0, 600.0, false)
            .iter()
            .filter_map(|c| match c {
                DrawCommand::Text { text, .. } => Some(text.to_string()),
                _ => None,
            })
            .collect()
    }

    // The panel offers `ctrl+shift+b toggle renderer` one line below this one, so a reader who cannot see which backend is live is being asked to toggle blind. Nothing called `set_renderer_info`, so the line never drew.
    #[test]
    fn the_panel_names_the_backend_that_is_drawing() {
        let mut dev = DevTools::default();
        open_panel(&mut dev);
        dev.set_renderer_info("software");
        assert!(
            panel_text(&mut dev)
                .iter()
                .any(|t| t == "renderer: software"),
            "the panel must name the live backend"
        );
    }

    #[test]
    fn the_backend_line_follows_a_switch() {
        let mut dev = DevTools::default();
        open_panel(&mut dev);
        dev.set_renderer_info("software");
        dev.set_renderer_info("hardware (wgpu)");
        let text = panel_text(&mut dev);
        assert!(text.iter().any(|t| t == "renderer: hardware (wgpu)"));
        assert!(!text.iter().any(|t| t == "renderer: software"));
    }
}