Skip to main content

gpui_macos/
gpui_macos.rs

1#![doc = include_str!("../README.md")]
2#![cfg(target_os = "macos")]
3#![allow(unused_mut)] // False positives in platform-specific state handling
4
5mod mac;
6
7pub(crate) use gpui::*;
8pub(crate) use mac::*;
9
10use std::rc::Rc;
11
12/// Returns the default macOS platform implementation for this process.
13pub fn current_platform(headless: bool) -> Rc<dyn gpui::Platform> {
14    Rc::new(mac::MacPlatform::new(headless))
15}
16
17#[cfg(test)]
18mod tests {
19    use super::{MacTextSystem, current_platform};
20    use gpui::{TestApp, TextRun, VisualTestCapabilities, WindowTextSystem, black, font, px};
21    use std::sync::{
22        Arc,
23        atomic::{AtomicBool, Ordering},
24    };
25    use std::time::Duration;
26
27    #[test]
28    fn headless_mac_text_layout_produces_nonzero_metrics() {
29        let app = TestApp::with_platform_text_system(Arc::new(MacTextSystem::new()));
30        let text_system = app.text_system();
31        let family = text_system
32            .all_font_names()
33            .into_iter()
34            .find(|name| !name.starts_with('.'))
35            .unwrap_or_else(|| ".SystemUIFont".to_string());
36        let window_text_system = WindowTextSystem::new(text_system.clone());
37        let text = "Headless text";
38        let layout = window_text_system.layout_line(
39            text,
40            px(16.),
41            &[TextRun {
42                len: text.len(),
43                font: font(family),
44                color: black(),
45                background_color: None,
46                underline: None,
47                strikethrough: None,
48            }],
49            None,
50        );
51
52        assert!(layout.width > px(0.));
53        assert!(layout.ascent > px(0.));
54        assert!(layout.runs.iter().any(|run| !run.glyphs.is_empty()));
55    }
56
57    #[test]
58    #[ignore]
59    fn real_visual_context_advance_clock_works() {
60        let capabilities = VisualTestCapabilities::detect();
61        if !capabilities.real_renderer || !capabilities.screenshot_capture {
62            eprintln!("skipping: real visual renderer is not available");
63            return;
64        }
65        let cx = gpui::RealVisualTestContext::with_platform(current_platform(false));
66        let completed = Arc::new(AtomicBool::new(false));
67        let timer = cx.executor().timer(Duration::from_secs(1));
68        let completed_task = completed.clone();
69        cx.spawn(async move {
70            timer.await;
71            completed_task.store(true, Ordering::SeqCst);
72        })
73        .detach();
74
75        cx.run_until_parked();
76        assert!(!completed.load(Ordering::SeqCst));
77
78        cx.advance_clock(Duration::from_secs(1));
79        cx.run_until_parked();
80        assert!(completed.load(Ordering::SeqCst));
81    }
82}