Merge #15801 into the merge queue batch

File › Place Embedded: the picker is titled Place Embedded (#14110)
This commit is contained in:
David Mann committed 2026-11-25 19:05:46 +01:00
commit fa3ce3fc6c
3 files changed
+119 -26

No files matched your search

+27 -13
View File
@@ -312,31 +312,41 @@ impl PhotocraftApp {
extensions: &[&str],
then: impl FnOnce(&mut Self, String, Vec<u8>) -> Result<Value, String> + 'static,
) -> Result<Value, String> {
let mut request = open_filters(PickRequest::open("file"), extensions);
if let Some(dir) = last_used_dir(&self.ui.recent_files) {
request = request.directory(dir);
}
let request = self.open_file_request(PickKind::Open, None, extensions);
self.ask_file(request, move |app, picked| {
let (name, bytes) = read_picked(picked)?;
then(app, name, bytes)
})
}
/// [`Self::pick_file_bytes`] letting the user choose several files (File › Place Embedded):
/// `then` gets every chosen file, in the order the dialog returns them. A desktop file is
/// handed over by path and read only when [`PickedFile::read`] is called, so files waiting
/// their turn hold no contents; the web hands over each file's contents.
pub(crate) fn pick_files_bytes(&mut self, then: impl FnOnce(&mut Self, Vec<PickedFile>) -> Result<Value, String> + 'static) -> Result<Value, String> {
let mut request = open_filters(PickRequest::open_many("file"), &[]);
if let Some(dir) = last_used_dir(&self.ui.recent_files) {
request = request.directory(dir);
}
/// [`Self::pick_file_bytes`] letting the user choose several files (File › Place Embedded,
/// optionally titled as in #14110 instead of the system's default title): `then` gets every
/// chosen file, in the order the dialog returns them. A desktop file is handed over by path and
/// read only when [`PickedFile::read`] is called, so files waiting their turn hold no contents;
/// the web hands over each file's contents.
pub(crate) fn pick_files_bytes(
&mut self,
title: Option<String>,
then: impl FnOnce(&mut Self, Vec<PickedFile>) -> Result<Value, String> + 'static,
) -> Result<Value, String> {
let request = self.open_file_request(PickKind::OpenMany, title, &[]);
self.ask_file(request, move |app, picked| {
let files = picked_files(picked)?;
then(app, files)
})
}
fn open_file_request(&self, kind: PickKind, title: Option<String>, extensions: &[&str]) -> PickRequest {
let mut request = open_filters(PickRequest::new(kind, "file"), extensions);
if let Some(title) = title {
request = request.title(title);
}
if let Some(dir) = last_used_dir(&self.ui.recent_files) {
request = request.directory(dir);
}
request
}
/// File › Open: opens every chosen file in the order the dialog returns them, reporting each
/// failure (see [`Self::open_paths_in_order`]).
pub fn open_dialog_file(&mut self) -> Result<Value, String> {
@@ -584,3 +594,7 @@ fn picked_files(picked: Picked) -> Result<Vec<PickedFile>, String> {
#[cfg(test)]
#[path = "file_dialog/tests.rs"]
mod tests;
#[cfg(test)]
#[path = "file_dialog/place_title_tests.rs"]
mod place_title_tests;
@@ -0,0 +1,77 @@
//! #14110: File › Place Embedded's picker is titled "Place Embedded"; every other file picker keeps
//! the system's own title. The requests are read from a scripted file service, so no dialog opens.
use super::*;
use crate::{Services, menus};
use craft::host::ScriptedFiles;
use std::sync::Arc;
/// An app with an untitled document, over a file service that keeps its dialogs open.
fn app() -> (Box<PhotocraftApp>, Arc<ScriptedFiles>) {
let files = Arc::new(ScriptedFiles::held());
let services = Services { files: Some(files.clone()), ..Default::default() };
let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), services);
app.run("file.new", json!({"width": 4, "height": 4})).unwrap();
(app, files)
}
/// The newest request the file service was given.
fn last_request(files: &ScriptedFiles) -> PickRequest {
files.requests().pop().expect("a request")
}
/// Cancels the newest dialog, so the next picker can open (one dialog at a time).
fn cancel(app: &mut PhotocraftApp, files: &ScriptedFiles) {
files.answer(Picked::Cancelled).expect("a dialog is open");
app.poll_file_dialog();
}
#[test]
fn place_embedded_asks_for_a_picker_titled_place_embedded() {
let (mut app, files) = app();
menus::invoke(&mut app, &egui::Context::default(), "file.placeEmbedded", json!({})).unwrap();
let request = last_request(&files);
assert_eq!(request.kind, PickKind::OpenMany);
assert_eq!(request.title.as_deref(), Some("Place Embedded"));
}
#[test]
fn place_embedded_keeps_the_pickers_other_settings() {
// Only the title is new: several files, with no extension filter, from the last-used folder.
let (mut app, files) = app();
menus::invoke(&mut app, &egui::Context::default(), "file.placeEmbedded", json!({})).unwrap();
let titled = last_request(&files);
cancel(&mut app, &files);
app.pick_files_bytes(None, |_, _| Ok(Value::Null)).unwrap();
let plain = last_request(&files);
assert_eq!(PickRequest { title: None, ..titled }, plain);
}
#[test]
fn the_other_open_pickers_keep_the_system_title() {
let (mut app, files) = app();
let ctx = egui::Context::default();
menus::invoke(&mut app, &ctx, "file.placeLinked", json!({})).unwrap();
assert_eq!(last_request(&files).title, None, "Place Linked");
cancel(&mut app, &files);
app.open_dialog_file().unwrap();
assert_eq!(last_request(&files).title, None, "File › Open");
cancel(&mut app, &files);
app.open_dialog_as_smart_object().unwrap();
assert_eq!(last_request(&files).title, None, "File › Open as Smart Object");
cancel(&mut app, &files);
app.pick_file_bytes_filtered(&["cube", "3dl", "look"], |_, _, _| Ok(Value::Null)).unwrap();
assert_eq!(last_request(&files).title, None, "a filtered picker (LUTs, scripts)");
cancel(&mut app, &files);
app.pick_file_bytes(|_, _, _| Ok(Value::Null)).unwrap();
assert_eq!(last_request(&files).title, None, "the plain single-file picker");
cancel(&mut app, &files);
app.pick_files_bytes(None, |_, _| Ok(Value::Null)).unwrap();
assert_eq!(last_request(&files).title, None, "the untitled multi-file picker");
}
+15 -13
View File
@@ -855,20 +855,22 @@ pub(super) fn front(app: &mut PhotocraftApp, ctx: &egui::Context, id: &str, para
if linked {
return Some(app.pick_file_bytes(place));
}
// Place Embedded takes several files (#14107); one file is placed as before.
Some(app.pick_files_bytes(move |app, files| match <[crate::file_dialog::PickedFile; 1]>::try_from(files) {
Ok([file]) => {
let (name, bytes) = file.read()?;
place(app, name, bytes)
}
Err(files) => {
if app.ui.transform.is_some() {
return Err("Commit or cancel the transform first".into());
// Place Embedded takes several files (#14107) and names its picker (#14110); one file is placed as before.
Some(app.pick_files_bytes(Some(tl!("Place Embedded").to_string()), move |app, files| {
match <[crate::file_dialog::PickedFile; 1]>::try_from(files) {
Ok([file]) => {
let (name, bytes) = file.read()?;
place(app, name, bytes)
}
Err(files) => {
if app.ui.transform.is_some() {
return Err("Commit or cancel the transform first".into());
}
app.refocus(doc)?;
let count = files.len();
app.place_picked(&queue_ctx, doc, files);
Ok(json!({ "files": count }))
}
app.refocus(doc)?;
let count = files.len();
app.place_picked(&queue_ctx, doc, files);
Ok(json!({ "files": count }))
}
}))
}