Skip to content

Visual Studio Code, rebuilt in Rust

Rust
3.2 KB
Raw Blame
//! `window.appIcon`, a VSRustium setting: the Dock icon look, its light or dark rendering following the system
//! appearance. Finder and Launchpad keep the bundle's icon; only `NSApplication.applicationIconImage` changes.

#[cfg(target_os = "macos")]
const DEFAULT_LIGHT: &[u8] = include_bytes!(concat!(env!("OUT_DIR"), "/AppIcon-Default.png"));
#[cfg(target_os = "macos")]
const DEFAULT_DARK: &[u8] = include_bytes!(concat!(env!("OUT_DIR"), "/AppIcon-Dark.png"));
#[cfg(target_os = "macos")]
pub const BARS_LIGHT: &[u8] = include_bytes!(concat!(env!("OUT_DIR"), "/AppIconBars-Default.png"));
#[cfg(target_os = "macos")]
pub const BARS_DARK: &[u8] = include_bytes!(concat!(env!("OUT_DIR"), "/AppIconBars-Dark.png"));
#[cfg(target_os = "macos")]
pub const BARS_CREAM_LIGHT: &[u8] = include_bytes!(concat!(env!("OUT_DIR"), "/AppIconBarsCream-Default.png"));
#[cfg(target_os = "macos")]
pub const BARS_CREAM_DARK: &[u8] = include_bytes!(concat!(env!("OUT_DIR"), "/AppIconBarsCream-Dark.png"));

/// The PNG the Dock shows for the setting's `look`; `None` is the bundle's own icon (its Assets.car follows the
/// appearance itself), which a run outside VSRustium.app does not have.
#[cfg(target_os = "macos")]
pub fn image(look: &str, dark: bool, bundled: bool) -> Option<&'static [u8]> {
    match (look, dark) {
        ("bars", false) => Some(BARS_LIGHT),
        ("bars", true) => Some(BARS_DARK),
        ("barsCream", false) => Some(BARS_CREAM_LIGHT),
        ("barsCream", true) => Some(BARS_CREAM_DARK),
        _ if bundled => None,
        (_, false) => Some(DEFAULT_LIGHT),
        (_, true) => Some(DEFAULT_DARK),
    }
}

/// Linux and Windows have no Dock: the window icon is the desktop file's or the executable's, as for VS Code there.
#[cfg(any(target_os = "linux", target_os = "windows"))]
pub fn image(_look: &str, _dark: bool, _bundled: bool) -> Option<&'static [u8]> {
    None
}

/// The product logo of the update tooltip, VS Code's branded code-icon.svg (updateTooltip.css `.product-logo`): the
/// Dock's icon for `look`.
#[cfg(target_os = "macos")]
pub fn logo(look: &str, dark: bool) -> &'static [u8] {
    image(look, dark, false).expect("a run outside the bundle has a PNG for every look")
}

/// The desktop file's icon, as package-linux installs it.
#[cfg(any(target_os = "linux", target_os = "windows"))]
pub fn logo(_look: &str, _dark: bool) -> &'static [u8] {
    include_bytes!("../../../../port/AppIcon.png")
}

/// Sets the Dock icon; `None` restores the bundle's.
#[cfg(all(not(test), target_os = "macos"))]
pub fn set(image: Option<&[u8]>) {
    use objc2::AllocAnyThread;
    let mtm = objc2::MainThreadMarker::new().expect("the Dock icon is set on the main thread");
    let image = image.map(|png| {
        let data = objc2_foundation::NSData::with_bytes(png);
        objc2_app_kit::NSImage::initWithData(objc2_app_kit::NSImage::alloc(), &data).expect("build.rs renders readable PNGs")
    });
    // SAFETY: on the main thread, with a valid image or nil.
    unsafe { objc2_app_kit::NSApplication::sharedApplication(mtm).setApplicationIconImage(image.as_deref()) };
}

/// Tests run off the main thread without an NSApplication; the workbench's `app_icon` records the swap.
#[cfg(any(test, target_os = "linux", target_os = "windows"))]
pub fn set(_image: Option<&[u8]>) {}