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 });
151 text_y += 16.0;
152
153 let line = format!("frame: {}", self.frame_count);
154 scene.nodes.push(SceneNode::Text {
155 rect: Rect {
156 x: bar_x,
157 y: text_y,
158 w: 80.0,
159 h: 14.0,
160 },
161 text: Arc::<str>::from(line),
162 color: Color::from_hex("#888888"),
163 size: 11.0,
164 font_family: None,
165 text_align: TextAlign::Unspecified,
166 font_weight: FontWeight::NORMAL,
167 font_style: FontStyle::Normal,
168 text_decoration: TextDecoration::default(),
169 letter_spacing: 0.0,
170 line_height: 0.0,
171 });
172 text_y += 14.0;
173
174 let line = format!("build: {:.1}ms", m.build_ms);
175 scene.nodes.push(SceneNode::Text {
176 rect: Rect {
177 x: bar_x,
178 y: text_y,
179 w: 80.0,
180 h: 14.0,
181 },
182 text: Arc::<str>::from(line),
183 color: Color::from_hex("#888888"),
184 size: 11.0,
185 font_family: None,
186 text_align: TextAlign::Unspecified,
187 font_weight: FontWeight::NORMAL,
188 font_style: FontStyle::Normal,
189 text_decoration: TextDecoration::default(),
190 letter_spacing: 0.0,
191 line_height: 0.0,
192 });
193 text_y += 14.0;
194
195 let line = format!("layout: {:.1}ms", m.layout_ms);
196 scene.nodes.push(SceneNode::Text {
197 rect: Rect {
198 x: bar_x,
199 y: text_y,
200 w: 80.0,
201 h: 14.0,
202 },
203 text: Arc::<str>::from(line),
204 color: Color::from_hex("#888888"),
205 size: 11.0,
206 font_family: None,
207 text_align: TextAlign::Unspecified,
208 font_weight: FontWeight::NORMAL,
209 font_style: FontStyle::Normal,
210 text_decoration: TextDecoration::default(),
211 letter_spacing: 0.0,
212 line_height: 0.0,
213 });
214 text_y += 14.0;
215
216 let line = format!("widgets: {}", m.widget_count);
217 scene.nodes.push(SceneNode::Text {
218 rect: Rect {
219 x: bar_x,
220 y: text_y,
221 w: 80.0,
222 h: 14.0,
223 },
224 text: Arc::<str>::from(line),
225 color: Color::from_hex("#888888"),
226 size: 11.0,
227 font_family: None,
228 text_align: TextAlign::Unspecified,
229 font_weight: FontWeight::NORMAL,
230 font_style: FontStyle::Normal,
231 text_decoration: TextDecoration::default(),
232 letter_spacing: 0.0,
233 line_height: 0.0,
234 });
235 text_y += 14.0;
236
237 let line = format!("signals: {}", m.signal_count);
238 scene.nodes.push(SceneNode::Text {
239 rect: Rect {
240 x: bar_x,
241 y: text_y,
242 w: 80.0,
243 h: 14.0,
244 },
245 text: Arc::<str>::from(line),
246 color: Color::from_hex("#888888"),
247 size: 11.0,
248 font_family: None,
249 text_align: TextAlign::Unspecified,
250 font_weight: FontWeight::NORMAL,
251 font_style: FontStyle::Normal,
252 text_decoration: TextDecoration::default(),
253 letter_spacing: 0.0,
254 line_height: 0.0,
255 });
256 text_y += 14.0;
257
258 let line = format!("scene nodes: {}", m.scene_nodes);
259 scene.nodes.push(SceneNode::Text {
260 rect: Rect {
261 x: bar_x,
262 y: text_y,
263 w: 100.0,
264 h: 14.0,
265 },
266 text: Arc::<str>::from(line),
267 color: Color::from_hex("#888888"),
268 size: 11.0,
269 font_family: None,
270 text_align: TextAlign::Unspecified,
271 font_weight: FontWeight::NORMAL,
272 font_style: FontStyle::Normal,
273 text_decoration: TextDecoration::default(),
274 letter_spacing: 0.0,
275 line_height: 0.0,
276 });
277
278 if let Some(hover) = &self.hovered_semantics {
279 text_y += 20.0;
280 let line = format!("↳ {}: {:?}", hover.id, hover.role);
281 scene.nodes.push(SceneNode::Text {
282 rect: Rect {
283 x: bar_x,
284 y: text_y,
285 w: 150.0,
286 h: 14.0,
287 },
288 text: Arc::<str>::from(line),
289 color: Color::from_hex("#44AAFF"),
290 size: 11.0,
291 font_family: None,
292 text_align: TextAlign::Unspecified,
293 font_weight: FontWeight::NORMAL,
294 font_style: FontStyle::Normal,
295 text_decoration: TextDecoration::default(),
296 letter_spacing: 0.0,
297 line_height: 0.0,
298 });
299 if let Some(lbl) = &hover.label {
300 text_y += 14.0;
301 scene.nodes.push(SceneNode::Text {
302 rect: Rect {
303 x: bar_x,
304 y: text_y,
305 w: 150.0,
306 h: 14.0,
307 },
308 text: Arc::<str>::from(format!(" \"{}\"", lbl)),
309 color: Color::from_hex("#66CCFF"),
310 size: 10.0,
311 font_family: None,
312 text_align: TextAlign::Unspecified,
313 font_weight: FontWeight::NORMAL,
314 font_style: FontStyle::Normal,
315 text_decoration: TextDecoration::default(),
316 letter_spacing: 0.0,
317 line_height: 0.0,
318 });
319 }
320 }
321 }
322
323 if let Some(r) = self.hovered {
324 scene.nodes.push(SceneNode::Border {
325 rect: r,
326 color: Color::from_hex("#44AAFF"),
327 width: 2.0,
328 radius: [2.0; 4],
329 });
330 }
331
332 if let Some(sel) = &self.selected_widget {
333 scene.nodes.push(SceneNode::Border {
334 rect: sel.bounds,
335 color: Color::from_hex("#FFAA00"),
336 width: 2.0,
337 radius: [2.0; 4],
338 });
339 }
340 }
341}
342
343#[derive(Clone, Debug, Default)]
344pub struct Metrics {
345 pub build_ms: f32,
346 pub layout_ms: f32,
347 pub scene_nodes: usize,
348 pub widget_count: usize,
349 pub signal_count: usize,
350}
351
352pub struct Inspector {
353 pub hud: Hud,
354}
355impl Default for Inspector {
356 fn default() -> Self {
357 Self::new()
358 }
359}
360
361impl Inspector {
362 pub fn new() -> Self {
363 Self { hud: Hud::new() }
364 }
365 pub fn frame(&mut self, scene: &mut Scene) {
366 if self.hud.inspector_enabled {
367 self.hud.overlay(scene);
368 }
369 }
370}