euv_core/platform/headless/impl.rs
1use super::*;
2
3/// Default `HeadlessPlatform` at 1.0x scale and Light theme.
4impl Default for HeadlessPlatform {
5 /// Creates a clean offscreen headless platform backend.
6 ///
7 /// # Returns
8 ///
9 /// - `Self` - Default `HeadlessPlatform`.
10 fn default() -> Self {
11 Self {
12 emulated_kind: PlatformKind::Headless,
13 scale_factor: 1.0,
14 safe_area: SafeAreaInsets::default(),
15 theme: ThemeMode::Light,
16 refresh_rate_hz: FALLBACK_REFRESH_RATE_HZ,
17 next_id: 1,
18 windows: HashMap::new(),
19 pending_events: Vec::new(),
20 clipboard: None,
21 pump_paused: false,
22 wait_calls: 0,
23 }
24 }
25}
26
27/// Configuration and event injection helpers for `HeadlessPlatform`.
28impl HeadlessPlatform {
29 /// Creates a headless platform emulating a specific target platform's form factor and DPI.
30 ///
31 /// # Arguments
32 ///
33 /// - `PlatformKind` - Target platform to emulate.
34 /// - `f32` - Simulated DPI scale factor.
35 /// - `SafeAreaInsets` - Simulated safe-area insets.
36 ///
37 /// # Returns
38 ///
39 /// - `Self` - Configured headless backend.
40 #[must_use]
41 pub fn emulating(platform: PlatformKind, scale_factor: f32, safe_area: SafeAreaInsets) -> Self {
42 let mut backend: Self = Self::default();
43 backend.set_emulated_kind(platform);
44 backend.set_scale_factor(scale_factor.max(0.5));
45 backend.set_safe_area(safe_area);
46 backend
47 }
48
49 /// Overrides the simulated hardware refresh rate in Hertz for deterministic high-refresh testing.
50 ///
51 /// # Arguments
52 ///
53 /// - `Self` - Headless platform builder.
54 /// - `f32` - Simulated display refresh rate in Hertz (`24.0`..=`480.0`).
55 ///
56 /// # Returns
57 ///
58 /// - `Self` - Updated headless backend.
59 #[must_use]
60 pub fn with_refresh_rate_hz(mut self, hz: f32) -> Self {
61 let pacing: FramePacingConfig = compute_frame_pacing(hz);
62 self.set_refresh_rate_hz(pacing.get_preferred_hz());
63 self
64 }
65
66 /// Enqueues a synthetic `WindowEvent` to be consumed on the next `poll_events()` call.
67 ///
68 /// # Arguments
69 ///
70 /// - `&Self` - Mutable headless platform.
71 /// - `WindowEvent` - Synthetic event to inject.
72 pub fn push_event(&mut self, event: WindowEvent) {
73 self.get_mut_pending_events().push(event);
74 }
75
76 /// Enqueues a synthetic `SystemEvent` wrapped as `PlatformEvent::System` for deterministic testing.
77 ///
78 /// # Arguments
79 ///
80 /// - `&Self` - Mutable headless platform.
81 /// - `SystemEvent` - System event to inject.
82 pub fn push_system_event(&mut self, event: SystemEvent) {
83 self.get_mut_pending_events()
84 .push(WindowEvent::System(event));
85 }
86
87 /// Returns a reference to the last presented `Framebuffer` for `id`.
88 ///
89 /// # Arguments
90 ///
91 /// - `&Self` - Headless platform reference.
92 /// - `WindowId` - Window identifier.
93 ///
94 /// # Returns
95 ///
96 /// - `Option<&Framebuffer>` - Most recently presented pixel buffer.
97 #[must_use]
98 pub fn last_framebuffer(&self, id: WindowId) -> Option<&Framebuffer> {
99 self.get_windows()
100 .get(&id)
101 .map(HeadlessWindowRecord::get_last_frame)
102 }
103}
104
105/// `WindowController` implementation for `HeadlessPlatform`.
106impl WindowController for HeadlessPlatform {
107 /// Applies a `WindowCommand` against the in-memory headless window record.
108 ///
109 /// # Arguments
110 ///
111 /// - `&Self` - Mutable headless platform.
112 /// - `WindowCommand` - Command to apply.
113 ///
114 /// # Returns
115 ///
116 /// - `Result<(), PlatformError>` - Success or `WindowNotFound`.
117 fn apply_command(&mut self, command: WindowCommand) -> Result<(), PlatformError> {
118 match command {
119 WindowCommand::SetTitle(id, title) => {
120 let record: &mut HeadlessWindowRecord = self
121 .get_mut_windows()
122 .get_mut(&id)
123 .ok_or(PlatformError::WindowNotFound(id.get_value()))?;
124 record.get_mut_config().set_title(title);
125 Ok(())
126 }
127 WindowCommand::SetSize(id, width, height) => {
128 let scale: f32 = {
129 let record: &mut HeadlessWindowRecord = self
130 .get_mut_windows()
131 .get_mut(&id)
132 .ok_or(PlatformError::WindowNotFound(id.get_value()))?;
133 let s: f32 = record.get_state().get_scale_factor();
134 record.get_mut_config().set_width(width.max(1));
135 record.get_mut_config().set_height(height.max(1));
136 record
137 .get_mut_state()
138 .set_logical_width(width.max(1) as f32);
139 record
140 .get_mut_state()
141 .set_logical_height(height.max(1) as f32);
142 record
143 .get_mut_state()
144 .set_physical_width(((width.max(1) as f32) * s).round() as u32);
145 record
146 .get_mut_state()
147 .set_physical_height(((height.max(1) as f32) * s).round() as u32);
148 s
149 };
150 self.push_event(WindowEvent::Resized(id, width.max(1), height.max(1), scale));
151 Ok(())
152 }
153 WindowCommand::SetPosition(id, x, y) => {
154 let record: &mut HeadlessWindowRecord = self
155 .get_mut_windows()
156 .get_mut(&id)
157 .ok_or(PlatformError::WindowNotFound(id.get_value()))?;
158 record.get_mut_config().set_position(Some((x, y)));
159 Ok(())
160 }
161 WindowCommand::SetResizable(id, resizable) => {
162 let record: &mut HeadlessWindowRecord = self
163 .get_mut_windows()
164 .get_mut(&id)
165 .ok_or(PlatformError::WindowNotFound(id.get_value()))?;
166 record.get_mut_config().set_resizable(resizable);
167 Ok(())
168 }
169 WindowCommand::SetDecorations(id, decorations) => {
170 let record: &mut HeadlessWindowRecord = self
171 .get_mut_windows()
172 .get_mut(&id)
173 .ok_or(PlatformError::WindowNotFound(id.get_value()))?;
174 record.get_mut_config().set_decorations(decorations);
175 Ok(())
176 }
177 WindowCommand::SetAlwaysOnTop(id, on_top) => {
178 let record: &mut HeadlessWindowRecord = self
179 .get_mut_windows()
180 .get_mut(&id)
181 .ok_or(PlatformError::WindowNotFound(id.get_value()))?;
182 record.get_mut_config().set_always_on_top(on_top);
183 Ok(())
184 }
185 WindowCommand::SetFullscreen(id, mode) => {
186 let record: &mut HeadlessWindowRecord = self
187 .get_mut_windows()
188 .get_mut(&id)
189 .ok_or(PlatformError::WindowNotFound(id.get_value()))?;
190 record.get_mut_config().set_fullscreen(mode);
191 record.get_mut_state().set_fullscreen(mode);
192 Ok(())
193 }
194 WindowCommand::SetVisible(id, visible) => {
195 let record: &mut HeadlessWindowRecord = self
196 .get_mut_windows()
197 .get_mut(&id)
198 .ok_or(PlatformError::WindowNotFound(id.get_value()))?;
199 record.get_mut_config().set_visible(visible);
200 Ok(())
201 }
202 WindowCommand::SetCursor(id, cursor) => {
203 let record: &mut HeadlessWindowRecord = self
204 .get_mut_windows()
205 .get_mut(&id)
206 .ok_or(PlatformError::WindowNotFound(id.get_value()))?;
207 record.set_cursor(cursor);
208 Ok(())
209 }
210 WindowCommand::SetIme(id, enabled, rect) => {
211 let record: &mut HeadlessWindowRecord = self
212 .get_mut_windows()
213 .get_mut(&id)
214 .ok_or(PlatformError::WindowNotFound(id.get_value()))?;
215 record.set_ime_active(enabled);
216 record.set_ime_rect(rect);
217 Ok(())
218 }
219 WindowCommand::SetStatusBarStyle(id, style) => {
220 let record: &mut HeadlessWindowRecord = self
221 .get_mut_windows()
222 .get_mut(&id)
223 .ok_or(PlatformError::WindowNotFound(id.get_value()))?;
224 record.get_mut_config().set_status_bar(style);
225 Ok(())
226 }
227 WindowCommand::RequestRedraw(id) => {
228 if !self.get_windows().contains_key(&id) {
229 return Err(PlatformError::WindowNotFound(id.get_value()));
230 }
231 self.push_event(WindowEvent::RedrawRequested(id));
232 Ok(())
233 }
234 WindowCommand::SetRenderMode(id, mode) => {
235 let record: &mut HeadlessWindowRecord = self
236 .get_mut_windows()
237 .get_mut(&id)
238 .ok_or(PlatformError::WindowNotFound(id.get_value()))?;
239 record.get_mut_config().get_mut_render().set_mode(mode);
240 Ok(())
241 }
242 WindowCommand::Close(id) => {
243 if self.get_mut_windows().remove(&id).is_none() {
244 return Err(PlatformError::WindowNotFound(id.get_value()));
245 }
246 Ok(())
247 }
248 }
249 }
250}
251
252/// `PlatformBackend` implementation for `HeadlessPlatform`.
253impl PlatformBackend for HeadlessPlatform {
254 /// Returns the emulated `PlatformKind`.
255 ///
256 /// # Arguments
257 ///
258 /// - `&Self` - Platform reference.
259 ///
260 /// # Returns
261 ///
262 /// - `PlatformKind` - Emulated platform kind.
263 fn kind(&self) -> PlatformKind {
264 self.get_emulated_kind()
265 }
266
267 /// Returns normalized capabilities for the emulated platform.
268 ///
269 /// # Arguments
270 ///
271 /// - `&Self` - Platform reference.
272 ///
273 /// # Returns
274 ///
275 /// - `PlatformCapabilities` - Capability matrix.
276 fn capabilities(&self) -> PlatformCapabilities {
277 PlatformCapabilities::for_platform(self.get_emulated_kind())
278 }
279
280 /// Allocates an offscreen window record with simulated DPI and safe-area insets.
281 ///
282 /// # Arguments
283 ///
284 /// - `&Self` - Mutable headless platform.
285 /// - `&WindowConfig` - Window configuration.
286 ///
287 /// # Returns
288 ///
289 /// - `Result<WindowState, PlatformError>` - Created window state.
290 fn create_window(&mut self, config: &WindowConfig) -> Result<WindowState, PlatformError> {
291 let id: WindowId = WindowId::new(self.get_next_id());
292 self.set_next_id(self.get_next_id().wrapping_add(1));
293 let mut state: WindowState = WindowState::from_config(id, config, self.get_scale_factor());
294 state.set_safe_area(self.get_safe_area());
295 state.set_system_theme(self.get_theme());
296 state
297 .get_mut_display()
298 .set_refresh_rate_hz(self.get_refresh_rate_hz().round() as u32);
299 let record: HeadlessWindowRecord = HeadlessWindowRecord {
300 config: config.clone(),
301 state,
302 cursor: CursorIcon::Default,
303 last_frame: Framebuffer::allocate(
304 state.get_physical_width(),
305 state.get_physical_height(),
306 ),
307 present_count: 0,
308 ime_active: false,
309 ime_rect: Rect::default(),
310 };
311 self.get_mut_windows().insert(id, record);
312 Ok(state)
313 }
314
315 /// Queries current `WindowState` for `id`.
316 ///
317 /// # Arguments
318 ///
319 /// - `&Self` - Platform reference.
320 /// - `WindowId` - Window handle.
321 ///
322 /// # Returns
323 ///
324 /// - `Option<WindowState>` - Window state if alive.
325 fn window_state(&self, id: WindowId) -> Option<WindowState> {
326 self.get_windows()
327 .get(&id)
328 .map(HeadlessWindowRecord::get_state)
329 }
330
331 /// Returns `RawWindowHandle::Headless`.
332 ///
333 /// # Arguments
334 ///
335 /// - `&Self` - Platform reference.
336 /// - `WindowId` - Window handle.
337 ///
338 /// # Returns
339 ///
340 /// - `RawWindowHandle` - Headless handle.
341 fn raw_window_handle(&self, _id: WindowId) -> RawWindowHandle {
342 RawWindowHandle::Headless
343 }
344
345 /// Drains injected synthetic events.
346 ///
347 /// # Arguments
348 ///
349 /// - `&Self` - Mutable headless platform.
350 ///
351 /// # Returns
352 ///
353 /// - `Vec<WindowEvent>` - Pending events.
354 fn poll_events(&mut self) -> Vec<WindowEvent> {
355 take(self.get_mut_pending_events())
356 }
357
358 /// Records a background wait invocation and drains any queued synthetic events immediately.
359 ///
360 /// # Arguments
361 ///
362 /// - `&Self` - Mutable headless platform.
363 ///
364 /// # Returns
365 ///
366 /// - `Vec<WindowEvent>` - Pending events.
367 fn wait_for_events(&mut self) -> Vec<WindowEvent> {
368 let next_calls: u64 = self.get_wait_calls().saturating_add(1);
369 self.set_wait_calls(next_calls);
370 self.poll_events()
371 }
372
373 /// Updates headless frame pump pause state for deterministic background freeze testing.
374 ///
375 /// # Arguments
376 ///
377 /// - `&Self` - Mutable headless platform.
378 /// - `bool` - `true` when paused in background; `false` when active.
379 fn set_frame_pump_paused(&mut self, paused: bool) {
380 self.set_pump_paused(paused);
381 }
382
383 /// Returns whether the headless frame pump is currently paused.
384 ///
385 /// # Arguments
386 ///
387 /// - `&Self` - Platform reference.
388 ///
389 /// # Returns
390 ///
391 /// - `bool` - `true` when paused in background.
392 fn is_frame_pump_paused(&self) -> bool {
393 self.get_pump_paused()
394 }
395
396 /// Stores a copy of `framebuffer` inside the headless window record for pixel inspection.
397 ///
398 /// # Arguments
399 ///
400 /// - `&Self` - Mutable headless platform.
401 /// - `WindowId` - Target window handle.
402 /// - `&Framebuffer` - Rendered RGBA8 framebuffer.
403 ///
404 /// # Returns
405 ///
406 /// - `Result<(), PlatformError>` - Presentation status.
407 fn present_framebuffer(
408 &mut self,
409 id: WindowId,
410 framebuffer: &Framebuffer,
411 ) -> Result<(), PlatformError> {
412 let record: &mut HeadlessWindowRecord = self
413 .get_mut_windows()
414 .get_mut(&id)
415 .ok_or(PlatformError::WindowNotFound(id.get_value()))?;
416 record.set_last_frame(framebuffer.clone());
417 record.set_present_count(record.get_present_count().wrapping_add(1));
418 Ok(())
419 }
420
421 /// Reads the simulated clipboard string.
422 ///
423 /// # Arguments
424 ///
425 /// - `&Self` - Platform reference.
426 ///
427 /// # Returns
428 ///
429 /// - `Option<String>` - Stored clipboard contents.
430 fn read_clipboard(&self) -> Option<String> {
431 let Self { clipboard, .. } = self;
432 clipboard.clone()
433 }
434
435 /// Writes `text` into the simulated clipboard.
436 ///
437 /// # Arguments
438 ///
439 /// - `&Self` - Mutable platform.
440 /// - `&str` - Clipboard text.
441 ///
442 /// # Returns
443 ///
444 /// - `Result<(), PlatformError>` - Always succeeds.
445 fn write_clipboard(&mut self, text: &str) -> Result<(), PlatformError> {
446 self.set_clipboard(Some(text.to_string()));
447 Ok(())
448 }
449
450 /// Returns the simulated OS theme mode.
451 ///
452 /// # Arguments
453 ///
454 /// - `&Self` - Platform reference.
455 ///
456 /// # Returns
457 ///
458 /// - `ThemeMode` - Simulated theme mode.
459 fn system_theme(&self) -> ThemeMode {
460 self.get_theme()
461 }
462
463 /// Returns the simulated display refresh rate in Hertz.
464 ///
465 /// # Arguments
466 ///
467 /// - `&Self` - Platform reference.
468 ///
469 /// # Returns
470 ///
471 /// - `f32` - Simulated refresh rate in Hertz.
472 fn display_refresh_rate_hz(&self) -> f32 {
473 self.get_refresh_rate_hz()
474 }
475}