Skip to content

Visual Studio Code, rebuilt in Rust

Rust
9.4 KB
Raw Blame
//! Electron's file_dialog_mac.mm: an NSOpenPanel or NSSavePanel as a sheet on the window, with the filters, the
//! default path, the button label and the title.

use std::cell::Cell;
use std::path::PathBuf;

use block2::RcBlock;
use futures::channel::oneshot;
use gpui::App;
use icu_normalizer::ComposingNormalizerBorrowed;
use objc2::rc::Retained;
use objc2::runtime::{AnyObject, NSObjectProtocol};
use objc2::{DefinedClass, MainThreadMarker, MainThreadOnly, Message, define_class, msg_send, sel};
use objc2_app_kit::{NSApplication, NSModalResponse, NSModalResponseOK, NSOpenPanel, NSPopUpButton, NSSavePanel, NSTextField, NSView};
use objc2_foundation::{NSArray, NSObject, NSPoint, NSRect, NSSize, NSString, NSURL};

use super::{FileDialog, FileDialogLock, default_location, strip_mnemonic};

pub(super) fn show_open_dialog(mut dialog: FileDialog, cx: &App) -> oneshot::Receiver<Vec<PathBuf>> {
    // "Ensure the path exists (if provided)".
    if dialog.default_path.as_ref().is_some_and(|path| !path.exists()) {
        dialog.default_path = None;
    }
    let mtm = MainThreadMarker::new().expect("dialogs open on the main thread");
    let panel = NSOpenPanel::openPanel(mtm);
    panel.setCanChooseFiles(dialog.files);
    panel.setCanChooseDirectories(dialog.folders);
    panel.setAllowsMultipleSelection(dialog.multiple);
    panel.setCanCreateDirectories(true);
    panel.setTreatsFilePackagesAsDirectories(dialog.packages_as_directories);
    let format = setup(&panel, &dialog, mtm);
    if format.is_some() {
        panel.setAccessoryViewDisclosed(true);
    }
    let reader = panel.clone();
    show(&dialog, &panel, format, cx, Vec::new(), move |ok| match ok {
        true => reader.URLs().iter().filter_map(|url| url_path(&url)).collect(),
        false => Vec::new(),
    })
}

pub(super) fn show_save_dialog(dialog: FileDialog, cx: &App) -> oneshot::Receiver<Option<PathBuf>> {
    let mtm = MainThreadMarker::new().expect("dialogs open on the main thread");
    let panel = NSSavePanel::savePanel(mtm);
    let format = setup(&panel, &dialog, mtm);
    let reader = panel.clone();
    show(&dialog, &panel, format, cx, None, move |ok| ok.then(|| reader.URL().and_then(|url| url_path(&url))).flatten())
}

/// `acquireFileDialogLock`, then the sheet on the focused window (`targetWindowId` is the active window), else a
/// panel of its own; `read` turns the response into the answer.
fn show<T: 'static>(dialog: &FileDialog, panel: &NSSavePanel, format: Option<Retained<FormatHandler>>, cx: &App, refused: T, read: impl Fn(bool) -> T + 'static) -> oneshot::Receiver<T> {
    let (sender, receiver) = oneshot::channel();
    let mtm = MainThreadMarker::new().expect("dialogs open on the main thread");
    let app = NSApplication::sharedApplication(mtm);
    let ns_window = app.keyWindow().or_else(|| app.mainWindow());
    let Some(lock) = FileDialogLock::acquire(ns_window.as_ref().map(|window| Retained::as_ptr(window) as usize), dialog) else {
        let _ = sender.send(refused);
        return receiver;
    };
    let sender = Cell::new(Some((sender, crate::dialog::OpenDialog::new(), lock)));
    let handler = RcBlock::new(move |response: NSModalResponse| {
        // The Format pop-up's target lives as long as its sheet.
        let _target = &format;
        if let Some((sender, _open, _lock)) = sender.take() {
            let _ = sender.send(read(response == NSModalResponseOK));
        }
    });
    let panel = panel.retain();
    // Begun after this update returns, as the message box sheets are.
    cx.foreground_executor()
        .spawn(async move {
            match ns_window {
                Some(ns_window) => panel.beginSheetModalForWindow_completionHandler(&ns_window, &handler),
                None => panel.beginWithCompletionHandler(&handler),
            }
        })
        .detach();
    receiver
}

/// file_dialog_mac.mm `SetupDialog` and `SetAllowedFileTypes`; the Format pop-up's target, if any.
fn setup(panel: &NSSavePanel, dialog: &FileDialog, mtm: MainThreadMarker) -> Option<Retained<FormatHandler>> {
    if let Some(title) = &dialog.title {
        panel.setTitle(Some(&NSString::from_str(title)));
    }
    if let Some(label) = &dialog.button_label {
        panel.setPrompt(Some(&NSString::from_str(&strip_mnemonic(label))));
    }
    let (directory, name) = default_location(dialog.default_path.as_deref());
    if let Some(directory) = directory {
        panel.setDirectoryURL(Some(&NSURL::fileURLWithPath(&NSString::from_str(&directory.to_string_lossy()))));
    }
    if let Some(name) = name {
        panel.setNameFieldStringValue(&NSString::from_str(&name));
    }
    let types = filter_types(&dialog.filters);
    if let Some(first) = types.first() {
        set_allowed_types(panel, first);
    }
    (types.len() > 1).then(|| add_format_picker(panel, &dialog.filters, types, mtm))
}

/// Per filter its extensions, only the last part of `tar.gz`-like ones; `None` for a filter with `*` or with no
/// extension left, which allows every file as an empty UTType list does.
fn filter_types(filters: &[(String, Vec<String>)]) -> Vec<Option<Vec<String>>> {
    let mut types = Vec::new();
    for (_, extensions) in filters {
        let mut filter = Vec::new();
        let mut any = false;
        for extension in extensions.iter().filter(|extension| !extension.is_empty()) {
            let last = extension.rsplit('.').next().unwrap_or(extension);
            if last == "*" {
                any = true;
                break;
            }
            if !filter.iter().any(|seen| seen == last) {
                filter.push(last.to_owned());
            }
        }
        types.push((!any && !filter.is_empty()).then_some(filter));
    }
    types
}

// The UTType API Electron uses needs the UniformTypeIdentifiers framework; the extension list picks the same files.
#[allow(deprecated)]
fn set_allowed_types(panel: &NSSavePanel, types: &Option<Vec<String>>) {
    match types {
        Some(types) => {
            let types: Vec<Retained<NSString>> = types.iter().map(|extension| NSString::from_str(extension)).collect();
            panel.setAllowedFileTypes(Some(&NSArray::from_retained_slice(&types)));
        }
        None => {
            panel.setAllowedFileTypes(None);
            panel.setAllowsOtherFileTypes(true);
        }
    }
}

/// `ElectronAccessoryView`: "Format:" and a pop-up of the filter names that switches the allowed types.
fn add_format_picker(panel: &NSSavePanel, filters: &[(String, Vec<String>)], types: Vec<Option<Vec<String>>>, mtm: MainThreadMarker) -> Retained<FormatHandler> {
    let rect = |x: f64, y: f64, width: f64, height: f64| NSRect::new(NSPoint::new(x, y), NSSize::new(width, height));
    let view = NSView::initWithFrame(NSView::alloc(mtm), rect(0., 0., 200., 32.));
    let label = NSTextField::initWithFrame(NSTextField::alloc(mtm), rect(0., 0., 60., 22.));
    label.setEditable(false);
    label.setStringValue(&NSString::from_str("Format:"));
    label.setBordered(false);
    label.setBezeled(false);
    label.setDrawsBackground(false);
    let popup = NSPopUpButton::initWithFrame_pullsDown(NSPopUpButton::alloc(mtm), rect(50., 2., 140., 22.), false);
    let names: Vec<Retained<NSString>> = filters.iter().map(|(name, _)| NSString::from_str(name)).collect();
    popup.addItemsWithTitles(&NSArray::from_retained_slice(&names));
    let handler = FormatHandler::new(mtm, FormatState { panel: panel.retain(), types });
    let target: &AnyObject = &handler;
    // SAFETY: the handler answers `selectFormat:` and lives until the sheet ends (`show`); a target is not retained.
    unsafe {
        popup.setTarget(Some(target));
        popup.setAction(Some(sel!(selectFormat:)));
    }
    view.addSubview(&label);
    view.addSubview(&popup);
    panel.setAccessoryView(Some(&view));
    handler
}

struct FormatState {
    panel: Retained<NSSavePanel>,
    types: Vec<Option<Vec<String>>>,
}

define_class!(
    #[unsafe(super(NSObject))]
    #[thread_kind = MainThreadOnly]
    #[name = "VSRustiumFileDialogFormatHandler"]
    #[ivars = FormatState]
    struct FormatHandler;

    // SAFETY: the superclass is NSObject, which implements NSObjectProtocol.
    unsafe impl NSObjectProtocol for FormatHandler {}

    impl FormatHandler {
        #[unsafe(method(selectFormat:))]
        fn select_format(&self, sender: &NSPopUpButton) {
            let state = self.ivars();
            if let Some(types) = usize::try_from(sender.indexOfSelectedItem()).ok().and_then(|index| state.types.get(index)) {
                set_allowed_types(&state.panel, types);
            }
        }
    }
);

impl FormatHandler {
    fn new(mtm: MainThreadMarker, state: FormatState) -> Retained<Self> {
        let this = Self::alloc(mtm).set_ivars(state);
        // SAFETY: `init` of NSObject on the object allocated above, whose ivars are set.
        unsafe { msg_send![super(this), init] }
    }
}

/// `normalizePath`: macOS answers paths in NFD, upstream hands them on in NFC.
fn url_path(url: &NSURL) -> Option<PathBuf> {
    let path = url.path()?.to_string();
    Some(PathBuf::from(ComposingNormalizerBorrowed::new_nfc().normalize(&path).into_owned()))
}


#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn filters_keep_the_last_extension_part_and_star_allows_all() {
        let filters = vec![("Archives".to_owned(), vec!["tar.gz".to_owned(), "zip".to_owned(), "gz".to_owned()]), ("All Files".to_owned(), vec!["*".to_owned()])];
        assert_eq!(filter_types(&filters), vec![Some(vec!["gz".to_owned(), "zip".to_owned()]), None]);
    }
}