euv-core 0.45.0

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
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
use super::*;

/// Default initialization for `MacOsPlatform`.
impl Default for MacOsPlatform {
    /// Creates an empty macOS native platform backend.
    ///
    /// # Returns
    ///
    /// - `Self` - Initialized macOS backend.
    fn default() -> Self {
        let initial_theme: ThemeMode = macos_system_theme();
        let initial_low_power: bool = macos_low_power_mode();
        let initial_pb: isize = macos_pasteboard_change_count();
        Self {
            next_id: 1,
            windows: HashMap::new(),
            pending_events: Vec::new(),
            clipboard_cache: None,
            cached_color_space: 0,
            last_app_active: true,
            last_miniaturized: false,
            last_low_power: initial_low_power,
            last_pasteboard_count: initial_pb,
            last_theme: initial_theme,
            last_app_hidden: false,
            pump_paused: false,
        }
    }
}

/// AppKit system event injection helpers for `MacOsPlatform`.
impl MacOsPlatform {
    /// Enqueues a normalized macOS `SystemEvent` to be drained on the next `poll_events()` tick.
    ///
    /// # Arguments
    ///
    /// - `&Self` - Mutable macOS platform backend.
    /// - `SystemEvent` - System event to enqueue.
    pub fn push_system_event(&mut self, event: SystemEvent) {
        self.get_mut_pending_events()
            .push(WindowEvent::System(event));
    }
}

/// `WindowController` implementation translating commands directly to AppKit calls.
impl WindowController for MacOsPlatform {
    /// Applies `command` directly onto the native `NSWindow` via `objc_msgSend`.
    ///
    /// # Arguments
    ///
    /// - `&Self` - Mutable macOS platform backend.
    /// - `WindowCommand` - Command to execute.
    ///
    /// # Returns
    ///
    /// - `Result<(), PlatformError>` - Execution status.
    fn apply_command(&mut self, command: WindowCommand) -> Result<(), PlatformError> {
        match command {
            WindowCommand::SetTitle(id, title) => {
                let record: &mut MacOsWindowRecord = self
                    .get_mut_windows()
                    .get_mut(&id)
                    .ok_or(PlatformError::WindowNotFound(id.get_value()))?;
                let ns_title: *mut c_void = macos_ns_string(&title);
                if record.get_ns_window() != 0 && !ns_title.is_null() {
                    let _: *mut c_void = objc_send_ptr1(
                        record.get_ns_window() as *mut c_void,
                        objc_sel(OBJC_SEL_SET_TITLE),
                        ns_title,
                    );
                }
                record.get_mut_config().set_title(title);
                Ok(())
            }
            WindowCommand::SetSize(id, width, height) => {
                let record: &mut MacOsWindowRecord = self
                    .get_mut_windows()
                    .get_mut(&id)
                    .ok_or(PlatformError::WindowNotFound(id.get_value()))?;
                let w: u32 = width.max(1);
                let h: u32 = height.max(1);
                if record.get_ns_window() != 0 {
                    objc_send_void_size(
                        record.get_ns_window() as *mut c_void,
                        objc_sel(OBJC_SEL_SET_CONTENT_SIZE),
                        CgSize {
                            width: w as f64,
                            height: h as f64,
                        },
                    );
                }
                let scale: f32 = record.get_state().get_scale_factor();
                record.get_mut_config().set_width(w);
                record.get_mut_config().set_height(h);
                record.get_mut_state().set_logical_width(w as f32);
                record.get_mut_state().set_logical_height(h as f32);
                record
                    .get_mut_state()
                    .set_physical_width(((w as f32) * scale).round() as u32);
                record
                    .get_mut_state()
                    .set_physical_height(((h as f32) * scale).round() as u32);
                Ok(())
            }
            WindowCommand::SetAlwaysOnTop(id, on_top) => {
                let record: &mut MacOsWindowRecord = self
                    .get_mut_windows()
                    .get_mut(&id)
                    .ok_or(PlatformError::WindowNotFound(id.get_value()))?;
                if record.get_ns_window() != 0 {
                    let level: isize = if on_top {
                        NS_FLOATING_WINDOW_LEVEL
                    } else {
                        NS_NORMAL_WINDOW_LEVEL
                    };
                    objc_send_void_isize(
                        record.get_ns_window() as *mut c_void,
                        objc_sel(OBJC_SEL_SET_LEVEL),
                        level,
                    );
                }
                record.get_mut_config().set_always_on_top(on_top);
                Ok(())
            }
            WindowCommand::SetVisible(id, visible) => {
                let record: &mut MacOsWindowRecord = self
                    .get_mut_windows()
                    .get_mut(&id)
                    .ok_or(PlatformError::WindowNotFound(id.get_value()))?;
                if record.get_ns_window() != 0 {
                    let sel_name: &'static str = if visible {
                        OBJC_SEL_MAKE_KEY_FRONT
                    } else {
                        OBJC_SEL_ORDER_OUT
                    };
                    let _: *mut c_void = objc_send_ptr1(
                        record.get_ns_window() as *mut c_void,
                        objc_sel(sel_name),
                        null_mut(),
                    );
                }
                record.get_mut_config().set_visible(visible);
                Ok(())
            }
            WindowCommand::SetFullscreen(id, mode) => {
                let record: &mut MacOsWindowRecord = self
                    .get_mut_windows()
                    .get_mut(&id)
                    .ok_or(PlatformError::WindowNotFound(id.get_value()))?;
                let was_fs: bool = record.get_state().get_fullscreen() != FullscreenMode::Windowed;
                let want_fs: bool = mode != FullscreenMode::Windowed;
                if record.get_ns_window() != 0 && was_fs != want_fs {
                    let _: *mut c_void = objc_send_ptr1(
                        record.get_ns_window() as *mut c_void,
                        objc_sel(OBJC_SEL_TOGGLE_FULLSCREEN),
                        null_mut(),
                    );
                }
                record.get_mut_config().set_fullscreen(mode);
                record.get_mut_state().set_fullscreen(mode);
                Ok(())
            }
            WindowCommand::Close(id) => {
                let record: MacOsWindowRecord = self
                    .get_mut_windows()
                    .remove(&id)
                    .ok_or(PlatformError::WindowNotFound(id.get_value()))?;
                if record.get_ns_window() != 0 {
                    let _: *mut c_void = objc_send_ptr0(
                        record.get_ns_window() as *mut c_void,
                        objc_sel(OBJC_SEL_CLOSE),
                    );
                }
                Ok(())
            }
            WindowCommand::SetDecorations(id, decorations) => {
                let record: &mut MacOsWindowRecord = self
                    .get_mut_windows()
                    .get_mut(&id)
                    .ok_or(PlatformError::WindowNotFound(id.get_value()))?;
                let mask: isize = if decorations {
                    (NS_WINDOW_STYLE_TITLED
                        | NS_WINDOW_STYLE_CLOSABLE
                        | NS_WINDOW_STYLE_MINIATURIZABLE
                        | NS_WINDOW_STYLE_RESIZABLE) as isize
                } else {
                    NS_WINDOW_STYLE_BORDERLESS as isize
                };
                if record.get_ns_window() != 0 {
                    objc_send_void_isize(
                        record.get_ns_window() as *mut c_void,
                        objc_sel(OBJC_SEL_SET_STYLE_MASK),
                        mask,
                    );
                }
                record.get_mut_config().set_decorations(decorations);
                Ok(())
            }
            WindowCommand::SetPosition(id, x, y) => {
                let record: &mut MacOsWindowRecord = 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 MacOsWindowRecord = self
                    .get_mut_windows()
                    .get_mut(&id)
                    .ok_or(PlatformError::WindowNotFound(id.get_value()))?;
                record.get_mut_config().set_resizable(resizable);
                Ok(())
            }
            WindowCommand::SetRenderMode(id, mode) => {
                let record: &mut MacOsWindowRecord = 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::RequestRedraw(id) => {
                if !self.get_windows().contains_key(&id) {
                    return Err(PlatformError::WindowNotFound(id.get_value()));
                }
                self.get_mut_pending_events()
                    .push(WindowEvent::RedrawRequested(id));
                Ok(())
            }
            WindowCommand::SetCursor(id, cursor) => {
                if !self.get_windows().contains_key(&id) {
                    return Err(PlatformError::WindowNotFound(id.get_value()));
                }
                let cls: *mut c_void = objc_class(OBJC_CLS_NS_CURSOR);
                if !cls.is_null() {
                    let sel_name: &'static str = match cursor {
                        CursorIcon::Pointer => OBJC_SEL_POINTING_HAND_CURSOR,
                        CursorIcon::Text => OBJC_SEL_IBEAM_CURSOR,
                        CursorIcon::Grab => OBJC_SEL_OPEN_HAND_CURSOR,
                        CursorIcon::Grabbing => OBJC_SEL_CLOSED_HAND_CURSOR,
                        CursorIcon::NotAllowed => OBJC_SEL_NOT_ALLOWED_CURSOR,
                        CursorIcon::Default
                        | CursorIcon::Crosshair
                        | CursorIcon::Move
                        | CursorIcon::EwResize
                        | CursorIcon::NsResize => OBJC_SEL_ARROW_CURSOR,
                    };
                    let ns_cursor: *mut c_void = objc_send_ptr0(cls, objc_sel(sel_name));
                    let _: *mut c_void = objc_send_ptr0(ns_cursor, objc_sel(OBJC_SEL_SET_CURSOR));
                }
                Ok(())
            }
            WindowCommand::SetIme(id, _, _) | WindowCommand::SetStatusBarStyle(id, _) => {
                if !self.get_windows().contains_key(&id) {
                    return Err(PlatformError::WindowNotFound(id.get_value()));
                }
                Ok(())
            }
        }
    }
}

/// `PlatformBackend` implementation for native macOS.
impl PlatformBackend for MacOsPlatform {
    /// Returns `PlatformKind::MacOs`.
    ///
    /// # Arguments
    ///
    /// - `&Self` - Platform backend reference.
    ///
    /// # Returns
    ///
    /// - `PlatformKind` - `PlatformKind::MacOs`.
    fn kind(&self) -> PlatformKind {
        PlatformKind::MacOs
    }

    /// Returns macOS desktop capability flags.
    ///
    /// # Arguments
    ///
    /// - `&Self` - Platform backend reference.
    ///
    /// # Returns
    ///
    /// - `PlatformCapabilities` - macOS capabilities.
    fn capabilities(&self) -> PlatformCapabilities {
        PlatformCapabilities::for_platform(PlatformKind::MacOs)
    }

    /// Creates a real AppKit `NSWindow` + `CAMetalLayer`.
    ///
    /// # Arguments
    ///
    /// - `&Self` - Mutable macOS backend.
    /// - `&WindowConfig` - Declarative window settings.
    ///
    /// # Returns
    ///
    /// - `Result<WindowState, PlatformError>` - Initial window state.
    fn create_window(&mut self, config: &WindowConfig) -> Result<WindowState, PlatformError> {
        let (ns_window, ns_view, metal_layer): (usize, usize, usize) =
            macos_create_native_window(config)?;
        let id: WindowId = WindowId::new(self.get_next_id());
        self.set_next_id(self.get_next_id().wrapping_add(1));
        let scale: f32 = macos_backing_scale_factor();
        let hz: f32 = macos_window_refresh_rate_hz(ns_window as *mut c_void);
        let mut state: WindowState = WindowState::from_config(id, config, scale);
        state
            .get_mut_display()
            .set_refresh_rate_hz(hz.round() as u32);
        state.set_system_theme(macos_system_theme());
        if config.get_decorations() {
            state.set_safe_area(SafeAreaInsets::new(28.0, 0.0, 0.0, 0.0));
        }
        self.get_mut_windows().insert(
            id,
            MacOsWindowRecord {
                config: config.clone(),
                state,
                ns_window,
                ns_view,
                metal_layer,
            },
        );
        Ok(state)
    }

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

    /// Returns `RawWindowHandle::AppKit` carrying `NSWindow`, `NSView`, and `CAMetalLayer` pointers.
    ///
    /// # Arguments
    ///
    /// - `&Self` - Platform reference.
    /// - `WindowId` - Window handle.
    ///
    /// # Returns
    ///
    /// - `RawWindowHandle` - Native AppKit pointers.
    fn raw_window_handle(&self, id: WindowId) -> RawWindowHandle {
        match self.get_windows().get(&id) {
            Some(rec) => RawWindowHandle::AppKit {
                ns_window: rec.get_ns_window(),
                ns_view: rec.get_ns_view(),
                metal_layer: rec.get_metal_layer(),
            },
            None => RawWindowHandle::Headless,
        }
    }

    /// Pumps pending AppKit events, detects closed windows, and drains queued macOS window events.
    ///
    /// # Arguments
    ///
    /// - `&Self` - Mutable macOS platform.
    ///
    /// # Returns
    ///
    /// - `Vec<WindowEvent>` - Pending events.
    fn poll_events(&mut self) -> Vec<WindowEvent> {
        let mut pumped: Vec<WindowEvent> = Vec::new();
        macos_pump_events(self.get_windows(), &mut pumped);
        let mut resized_events: Vec<(WindowId, u32, u32, f32)> = Vec::new();
        let mut focus_events: Vec<(WindowId, bool)> = Vec::new();
        let mut closed_ids: Vec<WindowId> = Vec::new();
        let app_hidden: bool = macos_app_is_hidden();
        let mut any_miniaturized: bool = false;
        let mut primary_wid: Option<WindowId> = None;
        for (id, rec) in self.get_mut_windows().iter_mut() {
            if primary_wid.is_none() {
                primary_wid = Some(*id);
            }
            let win_ptr: *mut c_void = rec.get_ns_window() as *mut c_void;
            if !win_ptr.is_null() {
                let is_mini: bool = objc_send_bool0(win_ptr, objc_sel(OBJC_SEL_IS_MINIATURIZED));
                if is_mini {
                    any_miniaturized = true;
                    continue;
                }
                if app_hidden {
                    continue;
                }
                if rec.get_config().get_visible()
                    && !objc_send_bool0(win_ptr, objc_sel(OBJC_SEL_IS_VISIBLE))
                {
                    closed_ids.push(*id);
                    continue;
                }
                let is_key: bool = objc_send_bool0(win_ptr, objc_sel(OBJC_SEL_IS_KEY_WINDOW));
                if is_key != rec.get_state().get_focused() {
                    rec.get_mut_state().set_focused(is_key);
                    focus_events.push((*id, is_key));
                }
            }
            let view_ptr: *mut c_void = rec.get_ns_view() as *mut c_void;
            if let Some((w, h)) = macos_view_logical_size(view_ptr) {
                let prev_w: u32 = rec.get_state().get_logical_width().round() as u32;
                let prev_h: u32 = rec.get_state().get_logical_height().round() as u32;
                let prev_scale: f32 = rec.get_state().get_scale_factor();
                let scale: f32 = macos_window_backing_scale(win_ptr);
                if w != prev_w || h != prev_h || (scale - prev_scale).abs() > 0.01 {
                    let layer_ptr: *mut c_void = rec.get_metal_layer() as *mut c_void;
                    if !layer_ptr.is_null() && (scale - prev_scale).abs() > 0.01 {
                        objc_send_void_f64(
                            layer_ptr,
                            objc_sel(OBJC_SEL_SET_CONTENTS_SCALE),
                            scale as f64,
                        );
                    }
                    rec.get_mut_state().set_logical_width(w as f32);
                    rec.get_mut_state().set_logical_height(h as f32);
                    rec.get_mut_state().set_scale_factor(scale);
                    rec.get_mut_state()
                        .set_physical_width(((w as f32) * scale).round() as u32);
                    rec.get_mut_state()
                        .set_physical_height(((h as f32) * scale).round() as u32);
                    resized_events.push((*id, w, h, scale));
                }
            }
        }
        let was_background: bool = self.get_last_miniaturized() || self.get_last_app_hidden();
        let in_background: bool = any_miniaturized || app_hidden;
        self.set_last_miniaturized(any_miniaturized);
        self.set_last_app_hidden(app_hidden);
        if in_background != was_background {
            let lc_ev: SystemEvent = if in_background {
                SystemEvent::Background
            } else {
                SystemEvent::Foreground
            };
            self.get_mut_pending_events()
                .push(WindowEvent::System(lc_ev));
        }
        let app_active: bool = macos_app_is_active();
        if app_active != self.get_last_app_active() {
            self.set_last_app_active(app_active);
            if !in_background {
                let act_ev: SystemEvent = if app_active {
                    SystemEvent::Foreground
                } else {
                    SystemEvent::Inactive
                };
                self.get_mut_pending_events()
                    .push(WindowEvent::System(act_ev));
            }
        }
        let cur_theme: ThemeMode = macos_system_theme();
        if cur_theme != self.get_last_theme() {
            self.set_last_theme(cur_theme);
            if let Some(wid) = primary_wid {
                self.get_mut_pending_events()
                    .push(WindowEvent::ThemeChanged(wid, cur_theme));
            } else {
                self.get_mut_pending_events()
                    .push(WindowEvent::System(SystemEvent::ThemeChanged(cur_theme)));
            }
        }
        let cur_low_power: bool = macos_low_power_mode();
        if cur_low_power != self.get_last_low_power() {
            self.set_last_low_power(cur_low_power);
            self.get_mut_pending_events().push(WindowEvent::System(
                SystemEvent::LowPowerModeChanged(cur_low_power),
            ));
        }
        let cur_pb: isize = macos_pasteboard_change_count();
        let prev_pb: isize = self.get_last_pasteboard_count();
        if prev_pb >= 0 && cur_pb != prev_pb {
            self.get_mut_pending_events()
                .push(WindowEvent::System(SystemEvent::ClipboardChanged));
        }
        self.set_last_pasteboard_count(cur_pb);
        for (id, focused) in focus_events.into_iter() {
            self.get_mut_pending_events()
                .push(WindowEvent::Focused(id, focused));
        }
        for (id, w, h, scale) in resized_events.into_iter() {
            self.get_mut_pending_events()
                .push(WindowEvent::Resized(id, w, h, scale));
        }
        self.get_mut_pending_events().extend(pumped);
        for id in closed_ids.into_iter() {
            self.get_mut_pending_events()
                .push(WindowEvent::CloseRequested(id));
        }
        take(self.get_mut_pending_events())
    }

    /// Blocks inside AppKit's kernel run loop until an OS event arrives, consuming zero CPU while backgrounded.
    ///
    /// # Arguments
    ///
    /// - `&Self` - Mutable macOS platform.
    ///
    /// # Returns
    ///
    /// - `Vec<WindowEvent>` - Events received upon wake.
    fn wait_for_events(&mut self) -> Vec<WindowEvent> {
        if self.get_pending_events().is_empty() && !self.get_windows().is_empty() {
            let mut waited: Vec<WindowEvent> = Vec::new();
            macos_wait_next_event(self.get_windows(), &mut waited);
            self.get_mut_pending_events().extend(waited);
        }
        self.poll_events()
    }

    /// Updates macOS background frame-pump pause state.
    ///
    /// # Arguments
    ///
    /// - `&Self` - Mutable macOS 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 macOS frame pump is currently paused for background freeze.
    ///
    /// # Arguments
    ///
    /// - `&Self` - Platform reference.
    ///
    /// # Returns
    ///
    /// - `bool` - `true` when paused in background.
    fn is_frame_pump_paused(&self) -> bool {
        self.get_pump_paused()
    }

    /// Blits the RGBA8 `Framebuffer` directly onto the window's `CALayer` via CoreGraphics.
    ///
    /// # Arguments
    ///
    /// - `&Self` - Mutable macOS 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 layer_ptr: usize = self
            .get_windows()
            .get(&id)
            .map(MacOsWindowRecord::get_metal_layer)
            .ok_or(PlatformError::WindowNotFound(id.get_value()))?;
        let mut color_space: usize = self.get_cached_color_space();
        if color_space == 0 {
            color_space = macos_create_device_rgb_space();
            self.set_cached_color_space(color_space);
        }
        macos_present_cg_image(layer_ptr, color_space, framebuffer)
    }

    /// Reads cached clipboard text.
    ///
    /// # Arguments
    ///
    /// - `&Self` - Platform reference.
    ///
    /// # Returns
    ///
    /// - `Option<String>` - Clipboard text if present.
    fn read_clipboard(&self) -> Option<String> {
        let Self {
            clipboard_cache, ..
        } = self;
        clipboard_cache.clone()
    }

    /// Writes `text` into the clipboard cache.
    ///
    /// # Arguments
    ///
    /// - `&Self` - Mutable macOS platform.
    /// - `&str` - Text to store.
    ///
    /// # Returns
    ///
    /// - `Result<(), PlatformError>` - Write status.
    fn write_clipboard(&mut self, text: &str) -> Result<(), PlatformError> {
        self.set_clipboard_cache(Some(text.to_string()));
        let _: bool = write_system_clipboard(text);
        Ok(())
    }

    /// Queries macOS dark/light appearance directly from AppKit.
    ///
    /// # Arguments
    ///
    /// - `&Self` - Platform reference.
    ///
    /// # Returns
    ///
    /// - `ThemeMode` - Active macOS system appearance.
    fn system_theme(&self) -> ThemeMode {
        macos_system_theme()
    }

    /// Queries the native macOS display refresh rate via `NSScreen.maximumFramesPerSecond`.
    ///
    /// # Arguments
    ///
    /// - `&Self` - Platform reference.
    ///
    /// # Returns
    ///
    /// - `f32` - Active display refresh rate in Hertz.
    fn display_refresh_rate_hz(&self) -> f32 {
        let win_ptr: *mut c_void = self
            .get_windows()
            .values()
            .next()
            .map(|rec: &MacOsWindowRecord| rec.get_ns_window() as *mut c_void)
            .unwrap_or(null_mut());
        macos_window_refresh_rate_hz(win_ptr)
    }
}