euv-core 0.48.2

Core cross-platform runtime for euv featuring arena-based fine-grained reactivity, deterministic layout, flat scene graph, multi-mode rendering, and direct OS platform adapters.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
use super::*;

/// Default `HeadlessPlatform` at 1.0x scale and Light theme.
impl Default for HeadlessPlatform {
    /// Creates a clean offscreen headless platform backend.
    ///
    /// # Returns
    ///
    /// - `Self` - Default `HeadlessPlatform`.
    fn default() -> Self {
        Self {
            emulated_kind: PlatformKind::Headless,
            scale_factor: 1.0,
            safe_area: SafeAreaInsets::default(),
            theme: ThemeMode::Light,
            refresh_rate_hz: FALLBACK_REFRESH_RATE_HZ,
            next_id: 1,
            windows: HashMap::new(),
            pending_events: Vec::new(),
            clipboard: None,
            pump_paused: false,
            wait_calls: 0,
            capture_last_frame: false,
        }
    }
}

/// Configuration and event injection helpers for `HeadlessPlatform`.
impl HeadlessPlatform {
    /// Creates a headless platform emulating a specific target platform's form factor and DPI.
    ///
    /// # Arguments
    ///
    /// - `PlatformKind` - Target platform to emulate.
    /// - `f32` - Simulated DPI scale factor.
    /// - `SafeAreaInsets` - Simulated safe-area insets.
    ///
    /// # Returns
    ///
    /// - `Self` - Configured headless backend.
    #[must_use]
    pub fn emulating(platform: PlatformKind, scale_factor: f32, safe_area: SafeAreaInsets) -> Self {
        let mut backend: Self = Self::default();
        backend.set_emulated_kind(platform);
        backend.set_scale_factor(scale_factor.max(0.5));
        backend.set_safe_area(safe_area);
        backend
    }

    /// Overrides the simulated hardware refresh rate in Hertz for deterministic high-refresh testing.
    ///
    /// # Arguments
    ///
    /// - `Self` - Headless platform builder.
    /// - `f32` - Simulated display refresh rate in Hertz (`24.0`..=`480.0`).
    ///
    /// # Returns
    ///
    /// - `Self` - Updated headless backend.
    #[must_use]
    pub fn with_refresh_rate_hz(mut self, hz: f32) -> Self {
        let pacing: FramePacingConfig = compute_frame_pacing(hz);
        self.set_refresh_rate_hz(pacing.get_preferred_hz());
        self
    }

    /// Enqueues a synthetic `WindowEvent` to be consumed on the next `poll_events()` call.
    ///
    /// # Arguments
    ///
    /// - `&Self` - Mutable headless platform.
    /// - `WindowEvent` - Synthetic event to inject.
    pub fn push_event(&mut self, event: WindowEvent) {
        self.get_mut_pending_events().push(event);
    }

    /// Enqueues a synthetic `SystemEvent` wrapped as `WindowEvent::System` for deterministic testing.
    ///
    /// # Arguments
    ///
    /// - `&Self` - Mutable headless platform.
    /// - `SystemEvent` - System event to inject.
    pub fn push_system_event(&mut self, event: SystemEvent) {
        self.get_mut_pending_events()
            .push(WindowEvent::System(event));
    }

    /// Enables or disables per-frame `Framebuffer` snapshot capture (`false` by default to avoid dead multi-MB clones).
    ///
    /// # Arguments
    ///
    /// - `bool` - `true` to capture each presented framebuffer for pixel inspection; `false` for zero-clone presentation.
    ///
    /// # Returns
    ///
    /// - `Self` - Updated headless backend.
    #[must_use]
    pub fn with_capture_last_frame(mut self, enabled: bool) -> Self {
        self.set_capture_last_frame(enabled);
        self
    }

    /// Returns a reference to the last presented `Framebuffer` for `id` when `capture_last_frame` is enabled.
    ///
    /// # Arguments
    ///
    /// - `WindowId` - Window identifier.
    ///
    /// # Returns
    ///
    /// - `Option<&Framebuffer>` - Most recently captured pixel buffer, or `None` when snapshot capture is disabled.
    #[must_use]
    pub fn last_framebuffer(&self, id: WindowId) -> Option<&Framebuffer> {
        if !self.get_capture_last_frame() {
            return None;
        }
        self.get_windows()
            .get(&id)
            .map(HeadlessWindowRecord::get_last_frame)
    }
}

/// `WindowController` implementation for `HeadlessPlatform`.
impl WindowController for HeadlessPlatform {
    /// Applies a `WindowCommand` against the in-memory headless window record.
    ///
    /// # Arguments
    ///
    /// - `&Self` - Mutable headless platform.
    /// - `WindowCommand` - Command to apply.
    ///
    /// # Returns
    ///
    /// - `Result<(), PlatformError>` - Success or `WindowNotFound`.
    fn apply_command(&mut self, command: WindowCommand) -> Result<(), PlatformError> {
        match command {
            WindowCommand::SetTitle(id, title) => {
                let record: &mut HeadlessWindowRecord = self
                    .get_mut_windows()
                    .get_mut(&id)
                    .ok_or(PlatformError::WindowNotFound(id.get_value()))?;
                record.get_mut_config().set_title(title);
                Ok(())
            }
            WindowCommand::SetSize(id, width, height) => {
                let scale: f32 = {
                    let record: &mut HeadlessWindowRecord = self
                        .get_mut_windows()
                        .get_mut(&id)
                        .ok_or(PlatformError::WindowNotFound(id.get_value()))?;
                    let s: f32 = record.get_state().get_scale_factor();
                    record.get_mut_config().set_width(width.max(1));
                    record.get_mut_config().set_height(height.max(1));
                    record
                        .get_mut_state()
                        .set_logical_width(width.max(1) as f32);
                    record
                        .get_mut_state()
                        .set_logical_height(height.max(1) as f32);
                    record
                        .get_mut_state()
                        .set_physical_width(((width.max(1) as f32) * s).round() as u32);
                    record
                        .get_mut_state()
                        .set_physical_height(((height.max(1) as f32) * s).round() as u32);
                    s
                };
                self.push_event(WindowEvent::Resized(id, width.max(1), height.max(1), scale));
                Ok(())
            }
            WindowCommand::SetPosition(id, x, y) => {
                let record: &mut HeadlessWindowRecord = self
                    .get_mut_windows()
                    .get_mut(&id)
                    .ok_or(PlatformError::WindowNotFound(id.get_value()))?;
                record.get_mut_config().set_position(Some((x, y)));
                Ok(())
            }
            WindowCommand::SetResizable(id, resizable) => {
                let record: &mut HeadlessWindowRecord = self
                    .get_mut_windows()
                    .get_mut(&id)
                    .ok_or(PlatformError::WindowNotFound(id.get_value()))?;
                record.get_mut_config().set_resizable(resizable);
                Ok(())
            }
            WindowCommand::SetDecorations(id, decorations) => {
                let record: &mut HeadlessWindowRecord = self
                    .get_mut_windows()
                    .get_mut(&id)
                    .ok_or(PlatformError::WindowNotFound(id.get_value()))?;
                record.get_mut_config().set_decorations(decorations);
                Ok(())
            }
            WindowCommand::SetAlwaysOnTop(id, on_top) => {
                let record: &mut HeadlessWindowRecord = self
                    .get_mut_windows()
                    .get_mut(&id)
                    .ok_or(PlatformError::WindowNotFound(id.get_value()))?;
                record.get_mut_config().set_always_on_top(on_top);
                Ok(())
            }
            WindowCommand::SetFullscreen(id, mode) => {
                let record: &mut HeadlessWindowRecord = self
                    .get_mut_windows()
                    .get_mut(&id)
                    .ok_or(PlatformError::WindowNotFound(id.get_value()))?;
                record.get_mut_config().set_fullscreen(mode);
                record.get_mut_state().set_fullscreen(mode);
                Ok(())
            }
            WindowCommand::SetVisible(id, visible) => {
                let record: &mut HeadlessWindowRecord = self
                    .get_mut_windows()
                    .get_mut(&id)
                    .ok_or(PlatformError::WindowNotFound(id.get_value()))?;
                record.get_mut_config().set_visible(visible);
                Ok(())
            }
            WindowCommand::SetCursor(id, cursor) => {
                let record: &mut HeadlessWindowRecord = self
                    .get_mut_windows()
                    .get_mut(&id)
                    .ok_or(PlatformError::WindowNotFound(id.get_value()))?;
                record.set_cursor(cursor);
                Ok(())
            }
            WindowCommand::SetIme(id, enabled, rect) => {
                let record: &mut HeadlessWindowRecord = self
                    .get_mut_windows()
                    .get_mut(&id)
                    .ok_or(PlatformError::WindowNotFound(id.get_value()))?;
                record.set_ime_active(enabled);
                record.set_ime_rect(rect);
                Ok(())
            }
            WindowCommand::SetStatusBarStyle(id, style) => {
                let record: &mut HeadlessWindowRecord = self
                    .get_mut_windows()
                    .get_mut(&id)
                    .ok_or(PlatformError::WindowNotFound(id.get_value()))?;
                record.get_mut_config().set_status_bar(style);
                Ok(())
            }
            WindowCommand::RequestRedraw(id) => {
                if !self.get_windows().contains_key(&id) {
                    return Err(PlatformError::WindowNotFound(id.get_value()));
                }
                self.push_event(WindowEvent::RedrawRequested(id));
                Ok(())
            }
            WindowCommand::SetRenderMode(id, mode) => {
                let record: &mut HeadlessWindowRecord = self
                    .get_mut_windows()
                    .get_mut(&id)
                    .ok_or(PlatformError::WindowNotFound(id.get_value()))?;
                record.get_mut_config().get_mut_render().set_mode(mode);
                Ok(())
            }
            WindowCommand::Close(id) => {
                if self.get_mut_windows().remove(&id).is_none() {
                    return Err(PlatformError::WindowNotFound(id.get_value()));
                }
                Ok(())
            }
        }
    }
}

/// `PlatformBackend` implementation for `HeadlessPlatform`.
impl PlatformBackend for HeadlessPlatform {
    /// Returns the emulated `PlatformKind`.
    ///
    /// # Arguments
    ///
    /// - `&Self` - Platform reference.
    ///
    /// # Returns
    ///
    /// - `PlatformKind` - Emulated platform kind.
    fn kind(&self) -> PlatformKind {
        self.get_emulated_kind()
    }

    /// Returns normalized capabilities for the emulated platform.
    ///
    /// # Arguments
    ///
    /// - `&Self` - Platform reference.
    ///
    /// # Returns
    ///
    /// - `PlatformCapabilities` - Capability matrix.
    fn capabilities(&self) -> PlatformCapabilities {
        PlatformCapabilities::for_platform(self.get_emulated_kind())
    }

    /// Allocates an offscreen window record with simulated DPI and safe-area insets.
    ///
    /// # Arguments
    ///
    /// - `&Self` - Mutable headless platform.
    /// - `&WindowConfig` - Window configuration.
    ///
    /// # Returns
    ///
    /// - `Result<WindowState, PlatformError>` - Created window state.
    fn create_window(&mut self, config: &WindowConfig) -> Result<WindowState, PlatformError> {
        let id: WindowId = WindowId::new(self.get_next_id());
        self.set_next_id(self.get_next_id().wrapping_add(1));
        let mut state: WindowState = WindowState::from_config(id, config, self.get_scale_factor());
        state.set_safe_area(self.get_safe_area());
        state.set_system_theme(self.get_theme());
        state
            .get_mut_display()
            .set_refresh_rate_hz(self.get_refresh_rate_hz().round() as u32);
        let record: HeadlessWindowRecord = HeadlessWindowRecord {
            config: config.clone(),
            state,
            cursor: CursorIcon::Default,
            last_frame: Framebuffer::allocate(1, 1),
            present_count: 0,
            ime_active: false,
            ime_rect: Rect::default(),
        };
        self.get_mut_windows().insert(id, record);
        Ok(state)
    }

    /// Queries current `WindowState` for `id`.
    ///
    /// # Arguments
    ///
    /// - `&Self` - Platform reference.
    /// - `WindowId` - Window handle.
    ///
    /// # Returns
    ///
    /// - `Option<WindowState>` - Window state if alive.
    fn window_state(&self, id: WindowId) -> Option<WindowState> {
        self.get_windows()
            .get(&id)
            .map(HeadlessWindowRecord::get_state)
    }

    /// Returns `RawWindowHandle::Headless`.
    ///
    /// # Arguments
    ///
    /// - `&Self` - Platform reference.
    /// - `WindowId` - Window handle.
    ///
    /// # Returns
    ///
    /// - `RawWindowHandle` - Headless handle.
    fn raw_window_handle(&self, _id: WindowId) -> RawWindowHandle {
        RawWindowHandle::Headless
    }

    /// Drains injected synthetic events.
    ///
    /// # Arguments
    ///
    /// - `&Self` - Mutable headless platform.
    ///
    /// # Returns
    ///
    /// - `Vec<WindowEvent>` - Pending events.
    fn poll_events(&mut self) -> Vec<WindowEvent> {
        take(self.get_mut_pending_events())
    }

    /// Records a background wait invocation and drains any queued synthetic events immediately.
    ///
    /// # Arguments
    ///
    /// - `&Self` - Mutable headless platform.
    ///
    /// # Returns
    ///
    /// - `Vec<WindowEvent>` - Pending events.
    fn wait_for_events(&mut self) -> Vec<WindowEvent> {
        let next_calls: u64 = self.get_wait_calls().saturating_add(1);
        self.set_wait_calls(next_calls);
        self.poll_events()
    }

    /// Updates headless frame pump pause state for deterministic background freeze testing.
    ///
    /// # Arguments
    ///
    /// - `&Self` - Mutable headless platform.
    /// - `bool` - `true` when paused in background; `false` when active.
    fn set_frame_pump_paused(&mut self, paused: bool) {
        self.set_pump_paused(paused);
    }

    /// Returns whether the headless frame pump is currently paused.
    ///
    /// # Arguments
    ///
    /// - `&Self` - Platform reference.
    ///
    /// # Returns
    ///
    /// - `bool` - `true` when paused in background.
    fn is_frame_pump_paused(&self) -> bool {
        self.get_pump_paused()
    }

    /// Stores a copy of `framebuffer` inside the headless window record for pixel inspection.
    ///
    /// # Arguments
    ///
    /// - `&Self` - Mutable headless platform.
    /// - `WindowId` - Target window handle.
    /// - `&Framebuffer` - Rendered RGBA8 framebuffer.
    ///
    /// # Returns
    ///
    /// - `Result<(), PlatformError>` - Presentation status.
    fn present_framebuffer(
        &mut self,
        id: WindowId,
        framebuffer: &Framebuffer,
    ) -> Result<(), PlatformError> {
        let capture: bool = self.get_capture_last_frame();
        let record: &mut HeadlessWindowRecord = self
            .get_mut_windows()
            .get_mut(&id)
            .ok_or(PlatformError::WindowNotFound(id.get_value()))?;
        if capture {
            record.set_last_frame(framebuffer.clone());
        }
        record.set_present_count(record.get_present_count().wrapping_add(1));
        Ok(())
    }

    /// Reads the simulated clipboard string.
    ///
    /// # Arguments
    ///
    /// - `&Self` - Platform reference.
    ///
    /// # Returns
    ///
    /// - `Option<String>` - Stored clipboard contents.
    fn read_clipboard(&self) -> Option<String> {
        let Self { clipboard, .. } = self;
        clipboard.clone()
    }

    /// Writes `text` into the simulated clipboard.
    ///
    /// # Arguments
    ///
    /// - `&Self` - Mutable platform.
    /// - `&str` - Clipboard text.
    ///
    /// # Returns
    ///
    /// - `Result<(), PlatformError>` - Always succeeds.
    fn write_clipboard(&mut self, text: &str) -> Result<(), PlatformError> {
        self.set_clipboard(Some(text.to_string()));
        let _: bool = dispatch_native_action(NATIVE_ACTION_CLIPBOARD, CLIPBOARD_ACTION_WRITE, text);
        Ok(())
    }

    /// Returns the simulated OS theme mode.
    ///
    /// # Arguments
    ///
    /// - `&Self` - Platform reference.
    ///
    /// # Returns
    ///
    /// - `ThemeMode` - Simulated theme mode.
    fn system_theme(&self) -> ThemeMode {
        self.get_theme()
    }

    /// Returns the simulated display refresh rate in Hertz.
    ///
    /// # Arguments
    ///
    /// - `&Self` - Platform reference.
    ///
    /// # Returns
    ///
    /// - `f32` - Simulated refresh rate in Hertz.
    fn display_refresh_rate_hz(&self) -> f32 {
        self.get_refresh_rate_hz()
    }
}