Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## Unreleased

- macOS: a save dialog with a format picker now keeps the extension of the name field in step with the selected format. The format's first extension is added to a name that has none, and replaces the extension of the previous format or of the name given to `set_file_name`. An extension the user typed is kept, and so is one the selected format lists. A format whose first extension is `*` or empty changes nothing. Before, the panel refused a name whose extension the selected format did not allow. A format with an extension for which the system has no type, or a type that prefers another extension (an installed application can declare one), is no longer given to the panel as its allowed types, which would change the extension of the name to the preferred one when saving: the panel accepts any name then and shows the extension (the original settings are restored when an exact format is selected again), and the format's first extension is added to a returned name that has none. The panel can still return the preferred extension of such a type with the extension shown (measured on macOS 26.5 with an application that declares one), so when the name it returned does not end in an extension the format lists and its name field does, the name field's name is used. When a file has the name that is used instead of the one the panel asked about, the person is asked whether to replace it, with Cancel the default, and Cancel makes the save return `None`, as if the person had cancelled.

## 0.18.0

- Fix some GTK3 memory leaks
Expand Down
5 changes: 3 additions & 2 deletions src/backend/macos/file_dialog.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use objc2_app_kit::NSModalResponseOK;
use std::path::PathBuf;

mod panel_ffi;
mod save_name;

use self::panel_ffi::Panel;
use super::modal_future::ModalFuture;
Expand Down Expand Up @@ -257,7 +258,7 @@ impl FileSaveDialogImpl for FileDialog {
run_on_main(move |mtm| {
let panel = Panel::build_save_file(&self, mtm);
if panel.run_modal() == NSModalResponseOK {
Some(panel.get_result())
panel.get_save_result()
} else {
None
}
Expand All @@ -276,7 +277,7 @@ impl AsyncFileSaveDialogImpl for FileDialog {
move |mtm| Panel::build_save_file(&self, mtm),
|panel, res_id| {
if res_id == NSModalResponseOK {
Some(panel.get_result().into())
panel.get_save_result().map(FileHandle::wrap)
} else {
None
}
Expand Down
244 changes: 215 additions & 29 deletions src/backend/macos/file_dialog/panel_ffi.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use core::str;
use std::cell::Cell;
use std::path::Path;
use std::path::PathBuf;

Expand All @@ -15,11 +16,14 @@ use objc2_uniform_type_identifiers::UTType;
use raw_window_handle::RawWindowHandle;

use super::super::{
message_dialog::Alert,
modal_future::{AsModal, InnerModal},
utils::{FocusManager, PolicyManager},
};
use super::save_name::{self, Correction};
use crate::backend::macos::utils::window_from_raw_window_handle;
use crate::file_dialog::Filter;
use crate::message_dialog::{MessageButtons, MessageDialog, MessageDialogResult, MessageLevel};
use crate::FileDialog;

extern "C" {
Expand Down Expand Up @@ -93,6 +97,41 @@ impl Panel {
}
}

/// The file a save panel chose, with the extension of the selected format, or `None` when the
/// file that name gives it exists and the person does not want to replace it.
pub fn get_save_result(&self) -> Option<PathBuf> {
let path = self.get_result();
let Some(handler) = &self._save_panel_handler else {
return Some(path);
};
let name_field = unsafe { self.panel.nameFieldStringValue() }.to_string();
match handler.corrected_path(&path, &name_field) {
Correction::Panels => Some(path),
Correction::Use(corrected) => Some(corrected),
// The panel asked about replacing the file it returned, which is not this one
Correction::Ask(corrected) => self.confirm_replace(&corrected).then_some(corrected),
}
}

/// Asks whether to replace the file at `path`, as the panel does for the file it returned.
/// Cancel is the default button.
fn confirm_replace(&self, path: &Path) -> bool {
let (title, description) = save_name::replace_question(path);
let dialog = MessageDialog::new()
.set_level(MessageLevel::Info)
.set_title(title)
.set_description(description)
.set_buttons(MessageButtons::OkCancelCustom(
"Cancel".to_string(),
"Replace".to_string(),
));
let mtm = MainThreadMarker::from(&*self.panel);
matches!(
Alert::new(dialog, mtm).run(),
MessageDialogResult::Custom(answer) if answer == "Replace"
)
}

pub fn get_results(&self) -> Vec<PathBuf> {
unsafe {
let urls = self.as_open_panel().unwrap().URLs();
Expand Down Expand Up @@ -185,11 +224,76 @@ impl PanelExt for Retained<NSOpenPanel> {

struct FormatOption {
types: Retained<NSArray<UTType>>,
extensions: Vec<String>,
// Whether the panel can be given `types`, see `save_name::format_is_exact`. When it can't, the
// panel would change the extension of the name to the one its type prefers, so it is left to
// accept any name.
exact: bool,
}

impl FormatOption {
fn new(filter: &Filter) -> Self {
let exact = save_name::format_is_exact(&filter.extensions, |ext| {
let preferred = unsafe { utype_for_extension(ext)?.preferredFilenameExtension() };
preferred.map(|preferred| preferred.to_string())
});
Self {
types: utypes_for_filter(filter),
extensions: filter.extensions.clone(),
exact,
}
}
}

/// What the panel's own settings were before a format changed them.
#[derive(Clone, Copy)]
struct PanelSettings {
allows_other_file_types: bool,
extension_hidden: bool,
}

impl PanelSettings {
fn of(panel: &NSSavePanel) -> Self {
unsafe {
Self {
allows_other_file_types: panel.allowsOtherFileTypes(),
extension_hidden: panel.isExtensionHidden(),
}
}
}
}

/// Gives the panel what the format needs: its types when it is exact, and its own settings
/// (`original`). When it is not exact, every type, with other file types allowed, and the
/// extension shown: a panel that hides the extension takes the type of the name from the
/// extension in the name field, and returns the preferred extension of that type, whatever types
/// it is allowed.
fn apply_format(panel: &NSSavePanel, option: &FormatOption, original: PanelSettings) {
if option.exact {
unsafe {
panel.setAllowedContentTypes(&option.types);
panel.setAllowsOtherFileTypes(original.allows_other_file_types);
panel.setExtensionHidden(original.extension_hidden);
}
} else {
let any: Vec<Retained<UTType>> = Vec::new();
unsafe {
panel.setAllowedContentTypes(&NSArray::from_retained_slice(&any));
panel.setAllowsOtherFileTypes(true);
panel.setExtensionHidden(false);
}
}
}

struct SavePanelHandlerIvars {
panel: Retained<NSSavePanel>,
options: Vec<FormatOption>,
// The name the panel was opened with, see `FileDialog::set_file_name`
file_name: Option<String>,
// Index in `options` of the selected format
selected: Cell<usize>,
// What the panel's settings were before a format changed them
original: PanelSettings,
}

define_class!(
Expand All @@ -208,8 +312,34 @@ define_class!(
return;
}

if let Some(option) = self.ivars().options.get(idx as usize) {
unsafe { self.ivars().panel.setAllowedContentTypes(&option.types) };
let ivars = self.ivars();
let Some(option) = ivars.options.get(idx as usize) else {
return;
};
let previous = ivars.selected.replace(idx as usize);

// The name is read before the allowed types change, and set after them, so the panel
// never sees a name whose extension is not allowed.
let name = unsafe { ivars.panel.nameFieldStringValue() }.to_string();
let previous_extensions = ivars
.options
.get(previous)
.map_or(&[][..], |previous| &previous.extensions);
let renamed = save_name::name_for_format(
&name,
previous_extensions,
ivars.file_name.as_deref(),
&option.extensions,
);

apply_format(&ivars.panel, option, ivars.original);

if let Some(renamed) = renamed {
unsafe {
ivars
.panel
.setNameFieldStringValue(&NSString::from_str(&renamed))
};
}
}
}
Expand All @@ -220,10 +350,46 @@ impl SavePanelHandler {
mtm: MainThreadMarker,
panel: Retained<NSSavePanel>,
options: Vec<FormatOption>,
file_name: Option<String>,
) -> Retained<Self> {
let this = Self::alloc(mtm).set_ivars(SavePanelHandlerIvars { panel, options });
let original = PanelSettings::of(&panel);
let this = Self::alloc(mtm).set_ivars(SavePanelHandlerIvars {
panel,
options,
file_name,
selected: Cell::new(0),
original,
});
unsafe { msg_send![super(this), init] }
}

/// Gives the panel what the first format needs, the one that is selected when it opens.
fn apply_first_format(&self) {
let ivars = self.ivars();
if let Some(first) = ivars.options.first() {
apply_format(&ivars.panel, first, ivars.original);
}
}

/// What to do with `path`, the file the panel returned, when the selected format is not
/// exact: the name `save_name::returned_name` gives for it, if it is not the panel's, and
/// whether a file has that name. The panel may not have kept the extension of the name field,
/// or the name has none. `name_field` is what the name field holds.
fn corrected_path(&self, path: &Path, name_field: &str) -> Correction {
let ivars = self.ivars();
let Some(option) = ivars.options.get(ivars.selected.get()) else {
return Correction::Panels;
};
if option.exact {
return Correction::Panels;
}
let renamed = path
.file_name()
.and_then(|name| name.to_str())
.and_then(|name| save_name::returned_name(name, name_field, &option.extensions))
.map(|name| path.with_file_name(name));
save_name::correction(path, renamed, Path::exists)
}
}

fn build_format_accessory_view(
Expand Down Expand Up @@ -328,32 +494,25 @@ impl Panel {

let save_panel_handler = match opt.filters.as_slice() {
[] => None,
[single] => {
unsafe { panel.setAllowedContentTypes(&utypes_for_filter(single)) };
None
}
multiple => {
let label_text = opt.format_label.as_deref().unwrap_or("Format:");
let (view, popup) = build_format_accessory_view(mtm, multiple, label_text);

let options = multiple
.iter()
.map(|filter| FormatOption {
types: utypes_for_filter(filter),
})
.collect();

let handler = SavePanelHandler::new(mtm, panel.clone(), options);

unsafe {
popup.setTarget(Some(&handler));
popup.setAction(Some(sel!(formatChanged:)));
}

unsafe {
panel.setAllowedContentTypes(&utypes_for_filter(&multiple[0]));
popup.selectItemAtIndex(0);
panel.setAccessoryView(Some(&view));
filters => {
let options = filters.iter().map(FormatOption::new).collect();
let handler =
SavePanelHandler::new(mtm, panel.clone(), options, opt.file_name.clone());
handler.apply_first_format();

if filters.len() > 1 {
let label_text = opt.format_label.as_deref().unwrap_or("Format:");
let (view, popup) = build_format_accessory_view(mtm, filters, label_text);

unsafe {
popup.setTarget(Some(&handler));
popup.setAction(Some(sel!(formatChanged:)));
}

unsafe {
popup.selectItemAtIndex(0);
panel.setAccessoryView(Some(&view));
}
}

Some(handler)
Expand Down Expand Up @@ -522,3 +681,30 @@ impl Panel {
Self::new(Retained::into_super(panel), opt.parent.as_ref())
}
}

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

fn exact(extensions: &[&str]) -> bool {
FormatOption::new(&Filter {
name: "Format".to_string(),
extensions: extensions.iter().map(|ext| ext.to_string()).collect(),
})
.exact
}

/// The types the system itself declares for these extensions prefer them.
#[test]
fn formats_of_the_systems_own_types_are_exact() {
assert!(exact(&["png"]));
assert!(exact(&["pdf"]));
}

#[test]
fn a_format_that_accepts_any_name_is_not_exact() {
assert!(!exact(&["*"]));
assert!(!exact(&[""]));
assert!(!exact(&[]));
}
}
Loading