1use std::sync::Arc;
2
3use web_time::Instant;
4
5use repose_core::{
6 Brush, Color, FontStyle, FontWeight, Rect, Scene, SceneNode, TextAlign, TextDecoration,
7};
8
9const FPS_HISTORY_LEN: usize = 60;
10
11pub struct Hud {
12 pub inspector_enabled: bool,
13 pub hovered: Option<Rect>,
14 pub hovered_semantics: Option<HoveredInfo>,
15 frame_count: u64,
16 last_frame: Option<Instant>,
17 fps_smooth: f32,
18 fps_history: [f32; FPS_HISTORY_LEN],
19 fps_history_idx: usize,
20 pub metrics: Option<Metrics>,
21 selected_widget: Option<SelectedWidget>,
22}
23
24#[derive(Clone, Debug)]
25pub struct HoveredInfo {
26 pub id: u64,
27 pub role: String,
28 pub label: Option<String>,
29}
30
31#[derive(Clone, Debug)]
32pub struct SelectedWidget {
33 pub id: u64,
34 pub role: String,
35 pub label: Option<String>,
36 pub bounds: Rect,
37}
38
39impl Default for Hud {
40 fn default() -> Self {
41 Self::new()
42 }
43}
44
45impl Hud {
46 pub fn new() -> Self {
47 Self {
48 inspector_enabled: false,
49 hovered: None,
50 hovered_semantics: None,
51 frame_count: 0,
52 last_frame: None,
53 fps_smooth: 0.0,
54 fps_history: [0.0; FPS_HISTORY_LEN],
55 fps_history_idx: 0,
56 metrics: None,
57 selected_widget: None,
58 }
59 }
60 pub fn toggle_inspector(&mut self) {
61 self.inspector_enabled = !self.inspector_enabled;
62 }
63 pub fn set_hovered(&mut self, r: Option<Rect>, info: Option<HoveredInfo>) {
64 self.hovered = r;
65 self.hovered_semantics = info;
66 }
67 pub fn select_widget(&mut self, info: SelectedWidget) {
68 self.selected_widget = Some(info);
69 }
70 pub fn clear_selection(&mut self) {
71 self.selected_widget = None;
72 }
73
74 fn update_fps(&mut self, now: Instant) {
75 if let Some(prev) = self.last_frame.replace(now) {
76 let dt = (now - prev).as_secs_f32();
77 if dt > 0.0 && dt < 1.0 {
78 let fps = 1.0 / dt;
79 let a = 0.3;
80 self.fps_smooth = if self.fps_smooth == 0.0 {
81 fps
82 } else {
83 (1.0 - a) * self.fps_smooth + a * fps
84 };
85 self.fps_history[self.fps_history_idx] = fps;
86 self.fps_history_idx = (self.fps_history_idx + 1) % FPS_HISTORY_LEN;
87 }
88 }
89 }
90
91 pub fn overlay(&mut self, scene: &mut Scene) {
92 self.frame_count += 1;
93 self.update_fps(Instant::now());
94
95 let bar_x = 8.0;
96 let bar_y = 8.0;
97 let bar_w = 120.0;
98 let bar_h = 24.0;
99
100 if let Some(m) = &self.metrics {
101 scene.nodes.push(SceneNode::Rect {
102 rect: Rect {
103 x: bar_x,
104 y: bar_y,
105 w: bar_w,
106 h: bar_h,
107 },
108 brush: Brush::Solid(Color::from_hex("#1A1A1ACC")),
109 radius: [4.0; 4],
110 });
111
112 let fps_norm = (self.fps_smooth / 60.0).min(1.0);
113 let bar_fill = bar_w * fps_norm;
114 scene.nodes.push(SceneNode::Rect {
115 rect: Rect {
116 x: bar_x + 2.0,
117 y: bar_y + 2.0,
118 w: bar_fill,
119 h: bar_h - 4.0,
120 },
121 brush: Brush::Solid(if self.fps_smooth >= 50.0 {
122 Color::from_hex("#44FF44")
123 } else if self.fps_smooth >= 30.0 {
124 Color::from_hex("#FFAA00")
125 } else {
126 Color::from_hex("#FF4444")
127 }),
128 radius: [2.0; 4],
129 });
130
131 let mut text_y = bar_y + bar_h + 4.0;
132 let line = format!("{:.0} fps", self.fps_smooth);
133 scene.nodes.push(SceneNode::Text {
134 rect: Rect {
135 x: bar_x,
136 y: text_y,
137 w: 80.0,
138 h: 14.0,
139 },
140 text: Arc::<str>::from(line),
141 color: Color::from_hex("#AAAAAA"),
142 size: 12.0,
143 font_family: None,
144 text_align: TextAlign::Unspecified,
145 font_weight: FontWeight::NORMAL,
146 font_style: FontStyle::Normal,
147 text_decoration: TextDecoration::default(),
148 letter_spacing: 0.0,
149 line_height: 0.0,
150 url: None,
151 });
152 text_y += 16.0;
153
154 let line = format!("frame: {}", self.frame_count);
155 scene.nodes.push(SceneNode::Text {
156 rect: Rect {
157 x: bar_x,
158 y: text_y,
159 w: 80.0,
160 h: 14.0,
161 },
162 text: Arc::<str>::from(line),
163 color: Color::from_hex("#888888"),
164 size: 11.0,
165 font_family: None,
166 text_align: TextAlign::Unspecified,
167 font_weight: FontWeight::NORMAL,
168 font_style: FontStyle::Normal,
169 text_decoration: TextDecoration::default(),
170 letter_spacing: 0.0,
171 line_height: 0.0,
172 url: None,
173 });
174 text_y += 14.0;
175
176 let line = format!("build: {:.1}ms", m.build_ms);
177 scene.nodes.push(SceneNode::Text {
178 rect: Rect {
179 x: bar_x,
180 y: text_y,
181 w: 80.0,
182 h: 14.0,
183 },
184 text: Arc::<str>::from(line),
185 color: Color::from_hex("#888888"),
186 size: 11.0,
187 font_family: None,
188 text_align: TextAlign::Unspecified,
189 font_weight: FontWeight::NORMAL,
190 font_style: FontStyle::Normal,
191 text_decoration: TextDecoration::default(),
192 letter_spacing: 0.0,
193 line_height: 0.0,
194 url: None,
195 });
196 text_y += 14.0;
197
198 let line = format!("layout: {:.1}ms", m.layout_ms);
199 scene.nodes.push(SceneNode::Text {
200 rect: Rect {
201 x: bar_x,
202 y: text_y,
203 w: 80.0,
204 h: 14.0,
205 },
206 text: Arc::<str>::from(line),
207 color: Color::from_hex("#888888"),
208 size: 11.0,
209 font_family: None,
210 text_align: TextAlign::Unspecified,
211 font_weight: FontWeight::NORMAL,
212 font_style: FontStyle::Normal,
213 text_decoration: TextDecoration::default(),
214 letter_spacing: 0.0,
215 line_height: 0.0,
216 url: None,
217 });
218 text_y += 14.0;
219
220 let line = format!("widgets: {}", m.widget_count);
221 scene.nodes.push(SceneNode::Text {
222 rect: Rect {
223 x: bar_x,
224 y: text_y,
225 w: 80.0,
226 h: 14.0,
227 },
228 text: Arc::<str>::from(line),
229 color: Color::from_hex("#888888"),
230 size: 11.0,
231 font_family: None,
232 text_align: TextAlign::Unspecified,
233 font_weight: FontWeight::NORMAL,
234 font_style: FontStyle::Normal,
235 text_decoration: TextDecoration::default(),
236 letter_spacing: 0.0,
237 line_height: 0.0,
238 url: None,
239 });
240 text_y += 14.0;
241
242 let line = format!("signals: {}", m.signal_count);
243 scene.nodes.push(SceneNode::Text {
244 rect: Rect {
245 x: bar_x,
246 y: text_y,
247 w: 80.0,
248 h: 14.0,
249 },
250 text: Arc::<str>::from(line),
251 color: Color::from_hex("#888888"),
252 size: 11.0,
253 font_family: None,
254 text_align: TextAlign::Unspecified,
255 font_weight: FontWeight::NORMAL,
256 font_style: FontStyle::Normal,
257 text_decoration: TextDecoration::default(),
258 letter_spacing: 0.0,
259 line_height: 0.0,
260 url: None,
261 });
262 text_y += 14.0;
263
264 let line = format!("scene nodes: {}", m.scene_nodes);
265 scene.nodes.push(SceneNode::Text {
266 rect: Rect {
267 x: bar_x,
268 y: text_y,
269 w: 100.0,
270 h: 14.0,
271 },
272 text: Arc::<str>::from(line),
273 color: Color::from_hex("#888888"),
274 size: 11.0,
275 font_family: None,
276 text_align: TextAlign::Unspecified,
277 font_weight: FontWeight::NORMAL,
278 font_style: FontStyle::Normal,
279 text_decoration: TextDecoration::default(),
280 letter_spacing: 0.0,
281 line_height: 0.0,
282 url: None,
283 });
284
285 if let Some(hover) = &self.hovered_semantics {
286 text_y += 20.0;
287 let line = format!("↳ {}: {:?}", hover.id, hover.role);
288 scene.nodes.push(SceneNode::Text {
289 rect: Rect {
290 x: bar_x,
291 y: text_y,
292 w: 150.0,
293 h: 14.0,
294 },
295 text: Arc::<str>::from(line),
296 color: Color::from_hex("#44AAFF"),
297 size: 11.0,
298 font_family: None,
299 text_align: TextAlign::Unspecified,
300 font_weight: FontWeight::NORMAL,
301 font_style: FontStyle::Normal,
302 text_decoration: TextDecoration::default(),
303 letter_spacing: 0.0,
304 line_height: 0.0,
305 url: None,
306 });
307 if let Some(lbl) = &hover.label {
308 text_y += 14.0;
309 scene.nodes.push(SceneNode::Text {
310 rect: Rect {
311 x: bar_x,
312 y: text_y,
313 w: 150.0,
314 h: 14.0,
315 },
316 text: Arc::<str>::from(format!(" \"{}\"", lbl)),
317 color: Color::from_hex("#66CCFF"),
318 size: 10.0,
319 font_family: None,
320 text_align: TextAlign::Unspecified,
321 font_weight: FontWeight::NORMAL,
322 font_style: FontStyle::Normal,
323 text_decoration: TextDecoration::default(),
324 letter_spacing: 0.0,
325 line_height: 0.0,
326 url: None,
327 });
328 }
329 }
330 }
331
332 if let Some(r) = self.hovered {
333 scene.nodes.push(SceneNode::Border {
334 rect: r,
335 color: Color::from_hex("#44AAFF"),
336 width: 2.0,
337 radius: [2.0; 4],
338 });
339 }
340
341 if let Some(sel) = &self.selected_widget {
342 scene.nodes.push(SceneNode::Border {
343 rect: sel.bounds,
344 color: Color::from_hex("#FFAA00"),
345 width: 2.0,
346 radius: [2.0; 4],
347 });
348 }
349 }
350}
351
352#[derive(Clone, Debug, Default)]
353pub struct Metrics {
354 pub build_ms: f32,
355 pub layout_ms: f32,
356 pub scene_nodes: usize,
357 pub widget_count: usize,
358 pub signal_count: usize,
359}
360
361pub struct Inspector {
362 pub hud: Hud,
363}
364impl Default for Inspector {
365 fn default() -> Self {
366 Self::new()
367 }
368}
369
370impl Inspector {
371 pub fn new() -> Self {
372 Self { hud: Hud::new() }
373 }
374 pub fn frame(&mut self, scene: &mut Scene) {
375 if self.hud.inspector_enabled {
376 self.hud.overlay(scene);
377 }
378 }
379}