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 });
121 text_y += 16.0;
122
123 let line = format!("frame: {}", self.frame_count);
124 scene.nodes.push(SceneNode::Text {
125 rect: Rect { x: bar_x, y: text_y, w: 80.0, h: 14.0 },
126 text: Arc::<str>::from(line),
127 color: Color::from_hex("#888888"),
128 size: 11.0,
129 });
130 text_y += 14.0;
131
132 let line = format!("build: {:.1}ms", m.build_ms);
133 scene.nodes.push(SceneNode::Text {
134 rect: Rect { x: bar_x, y: text_y, w: 80.0, h: 14.0 },
135 text: Arc::<str>::from(line),
136 color: Color::from_hex("#888888"),
137 size: 11.0,
138 });
139 text_y += 14.0;
140
141 let line = format!("layout: {:.1}ms", m.layout_ms);
142 scene.nodes.push(SceneNode::Text {
143 rect: Rect { x: bar_x, y: text_y, w: 80.0, h: 14.0 },
144 text: Arc::<str>::from(line),
145 color: Color::from_hex("#888888"),
146 size: 11.0,
147 });
148 text_y += 14.0;
149
150 let line = format!("widgets: {}", m.widget_count);
151 scene.nodes.push(SceneNode::Text {
152 rect: Rect { x: bar_x, y: text_y, w: 80.0, h: 14.0 },
153 text: Arc::<str>::from(line),
154 color: Color::from_hex("#888888"),
155 size: 11.0,
156 });
157 text_y += 14.0;
158
159 let line = format!("signals: {}", m.signal_count);
160 scene.nodes.push(SceneNode::Text {
161 rect: Rect { x: bar_x, y: text_y, w: 80.0, h: 14.0 },
162 text: Arc::<str>::from(line),
163 color: Color::from_hex("#888888"),
164 size: 11.0,
165 });
166 text_y += 14.0;
167
168 let line = format!("scene nodes: {}", m.scene_nodes);
169 scene.nodes.push(SceneNode::Text {
170 rect: Rect { x: bar_x, y: text_y, w: 100.0, h: 14.0 },
171 text: Arc::<str>::from(line),
172 color: Color::from_hex("#888888"),
173 size: 11.0,
174 });
175
176 if let Some(hover) = &self.hovered_semantics {
177 text_y += 20.0;
178 let line = format!("↳ {}: {:?}", hover.id, hover.role);
179 scene.nodes.push(SceneNode::Text {
180 rect: Rect { x: bar_x, y: text_y, w: 150.0, h: 14.0 },
181 text: Arc::<str>::from(line),
182 color: Color::from_hex("#44AAFF"),
183 size: 11.0,
184 });
185 if let Some(lbl) = &hover.label {
186 text_y += 14.0;
187 scene.nodes.push(SceneNode::Text {
188 rect: Rect { x: bar_x, y: text_y, w: 150.0, h: 14.0 },
189 text: Arc::<str>::from(format!(" \"{}\"", lbl)),
190 color: Color::from_hex("#66CCFF"),
191 size: 10.0,
192 });
193 }
194 }
195 }
196
197 if let Some(r) = self.hovered {
198 scene.nodes.push(SceneNode::Border {
199 rect: r,
200 color: Color::from_hex("#44AAFF"),
201 width: 2.0,
202 radius: 2.0,
203 });
204 }
205
206 if let Some(sel) = &self.selected_widget {
207 scene.nodes.push(SceneNode::Border {
208 rect: sel.bounds,
209 color: Color::from_hex("#FFAA00"),
210 width: 2.0,
211 radius: 2.0,
212 });
213 }
214 }
215}
216
217#[derive(Clone, Debug, Default)]
218pub struct Metrics {
219 pub build_ms: f32,
220 pub layout_ms: f32,
221 pub scene_nodes: usize,
222 pub widget_count: usize,
223 pub signal_count: usize,
224}
225
226pub struct Inspector {
227 pub hud: Hud,
228}
229impl Default for Inspector {
230 fn default() -> Self {
231 Self::new()
232 }
233}
234
235impl Inspector {
236 pub fn new() -> Self {
237 Self { hud: Hud::new() }
238 }
239 pub fn frame(&mut self, scene: &mut Scene) {
240 if self.hud.inspector_enabled {
241 self.hud.overlay(scene);
242 }
243 }
244}