//! The installed Xcode's app icon, for the New Project wizard's Xcode Project entry. It is read //! from the user's own Xcode at runtime (`NSWorkspace.iconForFile`), so no Apple artwork ships; //! without Xcode, or off macOS, the wizard draws the bundled Simple Icons glyph instead. use std::path::{Path, PathBuf}; use std::sync::OnceLock; /// Pixel size of the rendered icon: 32 px drawn at 16 pt in the list, sharp on Retina screens. #[cfg(target_os = "macos")] const PIXELS: isize = 64; /// The `.app` bundle a developer directory belongs to (`/Applications/Xcode.app` for /// `/Applications/Xcode.app/Contents/Developer`); `None` for the Command Line Tools. pub fn app_of(developer_dir: &Path) -> Option { developer_dir.ancestors().find(|p| p.extension().is_some_and(|e| e == "app")).map(Path::to_path_buf) } /// The active Xcode: the one `DEVELOPER_DIR` or `xcode-select -p` names, else /// `/Applications/Xcode.app` when it exists. fn active_xcode() -> Option { let developer_dir = ide_core::process::host_var_os("DEVELOPER_DIR").map(PathBuf::from).or_else(|| { let out = std::process::Command::new("xcode-select").arg("-p").output().ok()?; out.status.success().then(|| PathBuf::from(String::from_utf8_lossy(&out.stdout).trim())) }); developer_dir .as_deref() .and_then(app_of) .or_else(|| Some(PathBuf::from("/Applications/Xcode.app"))) .filter(|app| app.is_dir()) } /// The active Xcode's icon as a PNG `data:` URL, read once per run; `None` without Xcode, off /// macOS, or with `OPENIDEA_NO_XCODE_ICON` set (to see the fallback). pub fn data_url() -> Option { static ICON: OnceLock> = OnceLock::new(); ICON.get_or_init(|| { if std::env::var_os("OPENIDEA_NO_XCODE_ICON").is_some() { return None; } let png = render(&active_xcode()?)?; use base64::Engine as _; Some(format!("data:image/png;base64,{}", base64::engine::general_purpose::STANDARD.encode(png))) }) .clone() } #[cfg(target_os = "macos")] fn render(app: &Path) -> Option> { use objc2::AnyThread; use objc2_app_kit::{ NSBitmapImageFileType, NSBitmapImageRep, NSCompositingOperation, NSDeviceRGBColorSpace, NSGraphicsContext, NSWorkspace, }; use objc2_foundation::{NSDictionary, NSPoint, NSRect, NSSize, NSString}; let icon = NSWorkspace::sharedWorkspace().iconForFile(&NSString::from_str(&app.to_string_lossy())); let rep = unsafe { NSBitmapImageRep::initWithBitmapDataPlanes_pixelsWide_pixelsHigh_bitsPerSample_samplesPerPixel_hasAlpha_isPlanar_colorSpaceName_bytesPerRow_bitsPerPixel( NSBitmapImageRep::alloc(), std::ptr::null_mut(), PIXELS, PIXELS, 8, 4, true, false, NSDeviceRGBColorSpace, 0, 0, ) }?; let context = NSGraphicsContext::graphicsContextWithBitmapImageRep(&rep)?; NSGraphicsContext::saveGraphicsState_class(); NSGraphicsContext::setCurrentContext(Some(&context)); let size = PIXELS as f64; icon.drawInRect_fromRect_operation_fraction( NSRect::new(NSPoint::new(0.0, 0.0), NSSize::new(size, size)), NSRect::ZERO, NSCompositingOperation::Copy, 1.0, ); context.flushGraphics(); NSGraphicsContext::restoreGraphicsState_class(); let png = unsafe { rep.representationUsingType_properties(NSBitmapImageFileType::PNG, &NSDictionary::new()) }?; Some(png.to_vec()) } #[cfg(not(target_os = "macos"))] fn render(_app: &Path) -> Option> { None } #[cfg(test)] mod tests { use super::*; #[test] fn finds_the_app_of_a_developer_dir() { assert_eq!( app_of(Path::new("/Applications/Xcode-27.1.app/Contents/Developer")), Some(PathBuf::from("/Applications/Xcode-27.1.app")) ); // The Command Line Tools have no app: the wizard falls back to /Applications/Xcode.app, then the glyph. assert_eq!(app_of(Path::new("/Library/Developer/CommandLineTools")), None); } #[cfg(target_os = "macos")] #[test] fn renders_an_app_icon_as_png() { // Any app's icon goes through the same path as Xcode's. let png = render(Path::new("/System/Applications/Calculator.app")).unwrap(); assert!(png.starts_with(b"\x89PNG\r\n\x1a\n")); // IHDR: width and height. assert_eq!(&png[16..24], &[0, 0, 0, 64, 0, 0, 0, 64]); } #[test] fn no_icon_when_switched_off() { // SAFETY: no other test of this binary reads the variable. unsafe { std::env::set_var("OPENIDEA_NO_XCODE_ICON", "1") }; assert_eq!(data_url(), None); } }