Skip to main content

repose_devtools/
lib.rs

1use std::sync::Arc;
2
3use web_time::Instant;
4
5use repose_core::{Brush, Color, Rect, Scene, SceneNode};
6
7const FPS_HISTORY_LEN: usize = 60;
8
9pub struct Hud {
10    pub inspector_enabled: bool,
11    pub hovered: Option<Rect>,
12    pub hovered_semantics: Option<HoveredInfo>,
13    frame_count: u64,
14    last_frame: Option<Instant>,
15    fps_smooth: f32,
16    fps_history: [f32; FPS_HISTORY_LEN],
17    fps_history_idx: usize,
18    pub metrics: Option<Metrics>,
19    selected_widget: Option<SelectedWidget>,
20}
21
22#[derive(Clone, Debug)]
23pub struct HoveredInfo {
24    pub id: u64,
25    pub role: String,
26    pub label: Option<String>,
27}
28
29#[derive(Clone, Debug)]
30pub struct SelectedWidget {
31    pub id: u64,
32    pub role: String,
33    pub label: Option<String>,
34    pub bounds: Rect,
35}
36
37impl Default for Hud {
38    fn default() -> Self {
39        Self::new()
40    }
41}
42
43impl Hud {
44    pub fn new() -> Self {
45        Self {
46            inspector_enabled: false,
47            hovered: None,
48            hovered_semantics: None,
49            frame_count: 0,
50            last_frame: None,
51            fps_smooth: 0.0,
52            fps_history: [0.0; FPS_HISTORY_LEN],
53            fps_history_idx: 0,
54            metrics: None,
55            selected_widget: None,
56        }
57    }
58    pub fn toggle_inspector(&mut self) {
59        self.inspector_enabled = !self.inspector_enabled;
60    }
61    pub fn set_hovered(&mut self, r: Option<Rect>, info: Option<HoveredInfo>) {
62        self.hovered = r;
63        self.hovered_semantics = info;
64    }
65    pub fn select_widget(&mut self, info: SelectedWidget) {
66        self.selected_widget = Some(info);
67    }
68    pub fn clear_selection(&mut self) {
69        self.selected_widget = None;
70    }
71
72    fn update_fps(&mut self, now: Instant) {
73        if let Some(prev) = self.last_frame.replace(now) {
74            let dt = (now - prev).as_secs_f32();
75            if dt > 0.0 && dt < 1.0 {
76                let fps = 1.0 / dt;
77                let a = 0.3;
78                self.fps_smooth = if self.fps_smooth == 0.0 {
79                    fps
80                } else {
81                    (1.0 - a) * self.fps_smooth + a * fps
82                };
83                self.fps_history[self.fps_history_idx] = fps;
84                self.fps_history_idx = (self.fps_history_idx + 1) % FPS_HISTORY_LEN;
85            }
86        }
87    }
88
89    pub fn overlay(&mut self, scene: &mut Scene) {
90        self.frame_count += 1;
91        self.update_fps(Instant::now());
92
93        let bar_x = 8.0;
94        let bar_y = 8.0;
95        let bar_w = 120.0;
96        let bar_h = 24.0;
97
98        if let Some(m) = &self.metrics {
99            scene.nodes.push(SceneNode::Rect {
100                rect: Rect { x: bar_x, y: bar_y, w: bar_w, h: bar_h },
101                brush: Brush::Solid(Color::from_hex("#1A1A1ACC")),
102                radius: 4.0,
103            });
104
105            let fps_norm = (self.fps_smooth / 60.0).min(1.0);
106            let bar_fill = bar_w * fps_norm;
107            scene.nodes.push(SceneNode::Rect {
108                rect: Rect { x: bar_x + 2.0, y: bar_y + 2.0, w: bar_fill, h: bar_h - 4.0 },
109                brush: Brush::Solid(if self.fps_smooth >= 50.0 { Color::from_hex("#44FF44") } else if self.fps_smooth >= 30.0 { Color::from_hex("#FFAA00") } else { Color::from_hex("#FF4444") }),
110                radius: 2.0,
111            });
112
113            let mut text_y = bar_y + bar_h + 4.0;
114            let line = format!("{:.0} fps", self.fps_smooth);
115            scene.nodes.push(SceneNode::Text {
116                rect: Rect { x: bar_x, y: text_y, w: 80.0, h: 14.0 },
117                text: Arc::<str>::from(line),
118                color: Color::from_hex("#AAAAAA"),
119                size: 12.0,
120                font_family: None,
121            });
122            text_y += 16.0;
123
124            let line = format!("frame: {}", self.frame_count);
125            scene.nodes.push(SceneNode::Text {
126                rect: Rect { x: bar_x, y: text_y, w: 80.0, h: 14.0 },
127                text: Arc::<str>::from(line),
128                color: Color::from_hex("#888888"),
129                size: 11.0,
130                font_family: None,
131            });
132            text_y += 14.0;
133
134            let line = format!("build: {:.1}ms", m.build_ms);
135            scene.nodes.push(SceneNode::Text {
136                rect: Rect { x: bar_x, y: text_y, w: 80.0, h: 14.0 },
137                text: Arc::<str>::from(line),
138                color: Color::from_hex("#888888"),
139                size: 11.0,
140                font_family: None,
141            });
142            text_y += 14.0;
143
144            let line = format!("layout: {:.1}ms", m.layout_ms);
145            scene.nodes.push(SceneNode::Text {
146                rect: Rect { x: bar_x, y: text_y, w: 80.0, h: 14.0 },
147                text: Arc::<str>::from(line),
148                color: Color::from_hex("#888888"),
149                size: 11.0,
150                font_family: None,
151            });
152            text_y += 14.0;
153
154            let line = format!("widgets: {}", m.widget_count);
155            scene.nodes.push(SceneNode::Text {
156                rect: Rect { x: bar_x, y: text_y, w: 80.0, h: 14.0 },
157                text: Arc::<str>::from(line),
158                color: Color::from_hex("#888888"),
159                size: 11.0,
160                font_family: None,
161            });
162            text_y += 14.0;
163
164            let line = format!("signals: {}", m.signal_count);
165            scene.nodes.push(SceneNode::Text {
166                rect: Rect { x: bar_x, y: text_y, w: 80.0, h: 14.0 },
167                text: Arc::<str>::from(line),
168                color: Color::from_hex("#888888"),
169                size: 11.0,
170                font_family: None,
171            });
172            text_y += 14.0;
173
174            let line = format!("scene nodes: {}", m.scene_nodes);
175            scene.nodes.push(SceneNode::Text {
176                rect: Rect { x: bar_x, y: text_y, w: 100.0, h: 14.0 },
177                text: Arc::<str>::from(line),
178                color: Color::from_hex("#888888"),
179                size: 11.0,
180                font_family: None,
181            });
182
183            if let Some(hover) = &self.hovered_semantics {
184                text_y += 20.0;
185                let line = format!("↳ {}: {:?}", hover.id, hover.role);
186                scene.nodes.push(SceneNode::Text {
187                    rect: Rect { x: bar_x, y: text_y, w: 150.0, h: 14.0 },
188                    text: Arc::<str>::from(line),
189                    color: Color::from_hex("#44AAFF"),
190                    size: 11.0,
191                    font_family: None,
192                });
193                if let Some(lbl) = &hover.label {
194                    text_y += 14.0;
195                    scene.nodes.push(SceneNode::Text {
196                        rect: Rect { x: bar_x, y: text_y, w: 150.0, h: 14.0 },
197                        text: Arc::<str>::from(format!("  \"{}\"", lbl)),
198                        color: Color::from_hex("#66CCFF"),
199                        size: 10.0,
200                        font_family: None,
201                    });
202                }
203            }
204        }
205
206        if let Some(r) = self.hovered {
207            scene.nodes.push(SceneNode::Border {
208                rect: r,
209                color: Color::from_hex("#44AAFF"),
210                width: 2.0,
211                radius: 2.0,
212            });
213        }
214
215        if let Some(sel) = &self.selected_widget {
216            scene.nodes.push(SceneNode::Border {
217                rect: sel.bounds,
218                color: Color::from_hex("#FFAA00"),
219                width: 2.0,
220                radius: 2.0,
221            });
222        }
223    }
224}
225
226#[derive(Clone, Debug, Default)]
227pub struct Metrics {
228    pub build_ms: f32,
229    pub layout_ms: f32,
230    pub scene_nodes: usize,
231    pub widget_count: usize,
232    pub signal_count: usize,
233}
234
235pub struct Inspector {
236    pub hud: Hud,
237}
238impl Default for Inspector {
239    fn default() -> Self {
240        Self::new()
241    }
242}
243
244impl Inspector {
245    pub fn new() -> Self {
246        Self { hud: Hud::new() }
247    }
248    pub fn frame(&mut self, scene: &mut Scene) {
249        if self.hud.inspector_enabled {
250            self.hud.overlay(scene);
251        }
252    }
253}