Import: dropped and picked files show an Importing card (#270)
Files dropped on the window or picked in the Import dialog were probed
one after the other on the UI thread: the app froze while a video was
read, nothing said what was being imported or how many files were
left, and only the last new item was selected. People ended up
importing the same file again and again.
file.import takes `background`: the files are probed in a background
job (Window > Progress, jobs.list / jobs.cancel) that names the file
being read and how many there are ("b.png (2 of 3)"); then they are
added in one undo step, all selected (the Project panel scrolls to
them, a file already in the project arrives again as in After
Effects) and a toast reports the count ("Imported 3 items", with the
first error). Drops and the Import dialog use it, and an Importing
card at the bottom left shows the job with its progress bar and
Cancel. Agents and scripts still import at once by default. The
toast registers an automation id.
Test: background_imports_report_progress_and_select_what_arrived
(engine), dropped_files_show_import_progress_then_the_items_they_made
(ui_drag_drop).
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
285297ed93
commit
3318cb0da1
@@ -1383,7 +1383,7 @@ impl EffectcraftApp {
|
||||
self.session.poll_mask_track();
|
||||
ctx.request_repaint_after(std::time::Duration::from_millis(50));
|
||||
}
|
||||
// Background tasks (Content-Aware Fill, Scene Edit Detection).
|
||||
// Background tasks (Content-Aware Fill, Scene Edit Detection, imports).
|
||||
if !self.session.tasks.is_empty() {
|
||||
self.session.poll_jobs();
|
||||
ctx.request_repaint_after(std::time::Duration::from_millis(100));
|
||||
@@ -1451,10 +1451,13 @@ impl EffectcraftApp {
|
||||
panels::scriptui_view::sync_panels(self);
|
||||
panels::scriptui_view::show_windows(self, &ctx);
|
||||
panels::learn::coach(self, &ctx);
|
||||
self.draw_toast(ui, full);
|
||||
let lift = panels::media_panels::import_card(self, ui, full);
|
||||
self.draw_toast(ui, full, lift);
|
||||
}
|
||||
|
||||
fn draw_toast(&mut self, ui: &mut egui::Ui, full: egui::Rect) {
|
||||
/// The status message or the latest toast, bottom left (`lift` points higher while the
|
||||
/// Importing card is there).
|
||||
fn draw_toast(&mut self, ui: &mut egui::Ui, full: egui::Rect, lift: f32) {
|
||||
let now = ui.input(|i| i.time);
|
||||
let msg = if !self.ui.status.is_empty() {
|
||||
Some(self.ui.status.clone())
|
||||
@@ -1463,11 +1466,12 @@ impl EffectcraftApp {
|
||||
};
|
||||
if let Some(m) = msg {
|
||||
let t = &self.tokens;
|
||||
let galley = ui.painter().layout_no_wrap(m, Tokens::ui(12.0), t.text);
|
||||
let r = egui::Rect::from_min_size(egui::pos2(full.min.x + 16.0, full.max.y - 44.0), galley.size() + egui::vec2(24.0, 14.0));
|
||||
let galley = ui.painter().layout_no_wrap(m.clone(), Tokens::ui(12.0), t.text);
|
||||
let r = egui::Rect::from_min_size(egui::pos2(full.min.x + 16.0, full.max.y - 44.0 - lift), galley.size() + egui::vec2(24.0, 14.0));
|
||||
ui.painter().rect_filled(r, 6.0, t.panel_bg);
|
||||
ui.painter().rect_stroke(r, 6.0, egui::Stroke::new(1.0, t.field_border), egui::StrokeKind::Inside);
|
||||
ui.painter().galley(r.min + egui::vec2(12.0, 7.0), galley, t.text);
|
||||
self.auto.add("toast", r, &m);
|
||||
let resp = ui.interact(r, egui::Id::new("toast"), egui::Sense::click());
|
||||
if resp.clicked() {
|
||||
self.ui.status.clear();
|
||||
|
||||
@@ -1158,6 +1158,10 @@ fn file_dialog(app: &mut EffectcraftApp, id: &str, params: &Value) -> Option<Res
|
||||
};
|
||||
let Some(v) = picked else { return Some(Ok(Value::Null)) };
|
||||
p.insert(key.to_string(), v);
|
||||
// Picked files import in the background, with the Importing card showing progress (#270).
|
||||
if matches!(id, "file.import" | "file.importMultiple") {
|
||||
p.insert("background".into(), Value::Bool(true));
|
||||
}
|
||||
// Photoshop files ask how to import them first.
|
||||
if id == "file.import" && crate::panels::dialogs::open_form(app, id, &Value::Object(p.clone())) {
|
||||
return Some(Ok(json!({"dialog": id})));
|
||||
|
||||
@@ -342,9 +342,7 @@ pub fn progress(app: &mut EffectcraftApp, ui: &mut egui::Ui, rect: Rect) {
|
||||
let frac = j.fraction.unwrap_or(0.0) as f32;
|
||||
let pct = j.fraction.map(|f| format!("{:.0} %", f * 100.0)).unwrap_or_else(|| "…".into());
|
||||
p.text(pos2(x0 + w - 70.0, y + 8.0), Align2::RIGHT_CENTER, format!("{pct} {}", j.message), Tokens::ui(11.0), t.text_dim);
|
||||
let bar = Rect::from_min_size(pos2(x0, y + 20.0), vec2(w - 76.0, 6.0));
|
||||
p.rect_filled(bar, 3.0, t.field_bg);
|
||||
p.rect_filled(Rect::from_min_size(bar.min, vec2(bar.width() * frac.clamp(0.0, 1.0), bar.height())), 3.0, t.accent);
|
||||
progress_bar(&p, Rect::from_min_size(pos2(x0, y + 20.0), vec2(w - 76.0, 6.0)), frac, &t);
|
||||
app.auto.add(&format!("progress.job.{}", j.id), Rect::from_min_size(pos2(x0, y), vec2(w, 28.0)), &j.label);
|
||||
if j.cancellable
|
||||
&& kit::button(app, ui, Rect::from_min_size(pos2(x0 + w - 64.0, y + 12.0), vec2(64.0, 18.0)), &format!("progress.cancel.{}", j.id), "Cancel", false)
|
||||
@@ -374,3 +372,39 @@ pub fn progress(app: &mut EffectcraftApp, ui: &mut egui::Ui, rect: Rect) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// A job's progress bar (`frac` 0…1).
|
||||
fn progress_bar(p: &egui::Painter, bar: Rect, frac: f32, t: &Tokens) {
|
||||
p.rect_filled(bar, 3.0, t.field_bg);
|
||||
p.rect_filled(Rect::from_min_size(bar.min, vec2(bar.width() * frac.clamp(0.0, 1.0), bar.height())), 3.0, t.accent);
|
||||
}
|
||||
|
||||
/// The Importing card, bottom left over the panels while files import in the background (dropped
|
||||
/// or picked in the Import dialog): the file being read, how many are left, and Cancel (#270).
|
||||
/// Returns the height it takes, so toasts sit above it.
|
||||
pub fn import_card(app: &mut EffectcraftApp, ui: &mut egui::Ui, full: Rect) -> f32 {
|
||||
if app.session.tasks.is_empty() {
|
||||
return 0.0;
|
||||
}
|
||||
let t = app.tokens;
|
||||
let w = 320.0_f32.min(full.width() - 32.0).max(120.0);
|
||||
let mut bottom = full.max.y - 16.0;
|
||||
for j in app.session.jobs().into_iter().filter(|j| j.kind == "import" && j.running) {
|
||||
let r = Rect::from_min_max(pos2(full.min.x + 16.0, bottom - 56.0), pos2(full.min.x + 16.0 + w, bottom));
|
||||
let p = ui.painter();
|
||||
p.rect_filled(r, 6.0, t.panel_bg);
|
||||
p.rect_stroke(r, 6.0, Stroke::new(1.0, t.field_border), egui::StrokeKind::Inside);
|
||||
let x0 = r.min.x + 12.0;
|
||||
let inner = w - 24.0;
|
||||
widgets::text_fit(p, pos2(x0, r.min.y + 15.0), Align2::LEFT_CENTER, &j.label, Tokens::medium(12.0), inner - 64.0, t.text);
|
||||
widgets::text_fit(p, pos2(x0, r.min.y + 32.0), Align2::LEFT_CENTER, &j.message, Tokens::ui(11.5), inner, t.text_dim);
|
||||
progress_bar(p, Rect::from_min_size(pos2(x0, r.min.y + 42.0), vec2(inner, 6.0)), j.fraction.unwrap_or(0.0) as f32, &t);
|
||||
app.auto.add(&format!("import.job.{}", j.id), r, &j.message);
|
||||
if kit::button(app, ui, Rect::from_min_size(pos2(r.max.x - 68.0, r.min.y + 6.0), vec2(56.0, 18.0)), &format!("import.cancel.{}", j.id), "Cancel", false)
|
||||
{
|
||||
kit::exec(app, "jobs.cancel", json!({"job": j.id}));
|
||||
}
|
||||
bottom = r.min.y - 8.0;
|
||||
}
|
||||
full.max.y - 16.0 - bottom
|
||||
}
|
||||
|
||||
@@ -17,9 +17,10 @@ pub fn frame(app: &mut EffectcraftApp, ctx: &egui::Context) {
|
||||
video_preview(app, ctx);
|
||||
}
|
||||
|
||||
/// Files dropped on the window are imported (layered files as Default Drag Import As says).
|
||||
/// Dropped on the Composition viewer, they also become layers centred there, as in After
|
||||
/// Effects (#85), when the platform reports where the pointer is during file drags.
|
||||
/// Files dropped on the window are imported (layered files as Default Drag Import As says) in
|
||||
/// the background, with the Importing card showing what is read (#270). Dropped on the
|
||||
/// Composition viewer, they also become layers centred there, as in After Effects (#85), when the
|
||||
/// platform reports where the pointer is during file drags.
|
||||
fn dropped_files(app: &mut EffectcraftApp, ctx: &egui::Context) {
|
||||
let paths: Vec<String> =
|
||||
ctx.input(|i| i.raw.dropped_files.iter().map(|f| f.path().to_string_lossy().to_string()).filter(|p| std::path::Path::new(p).is_absolute()).collect());
|
||||
@@ -39,7 +40,8 @@ fn dropped_files(app: &mut EffectcraftApp, ctx: &egui::Context) {
|
||||
let viewer = app.auto.find("viewer.area").map(|e| Rect::from_min_size(egui::pos2(e.rect[0], e.rect[1]), egui::vec2(e.rect[2], e.rect[3])));
|
||||
let at =
|
||||
ctx.input(|i| i.pointer.hover_pos()).filter(|p| viewer.is_some_and(|v| v.contains(*p))).and_then(|p| crate::panels::viewer::screen_to_comp(ctx, p));
|
||||
let params = json!({"paths": rest, "drag": true, "importAs": app.session.prefs.drag_import_as(), "addToComp": at.is_some(), "position": at});
|
||||
let params =
|
||||
json!({"paths": rest, "drag": true, "importAs": app.session.prefs.drag_import_as(), "addToComp": at.is_some(), "position": at, "background": true});
|
||||
if let Err(e) = crate::menus::invoke(app, ctx, "file.import", params) {
|
||||
app.ui.status = e;
|
||||
}
|
||||
|
||||
@@ -1,7 +1,11 @@
|
||||
//! Drag and drop between panels (egui_kittest, real pointer drags): Project
|
||||
//! items land in the Timeline where they are dropped, between layers and, over the time graph,
|
||||
//! starting there (#89); dropped on the Composition viewer they are centred where they land
|
||||
//! (#85); an effect dropped on the viewer goes on the layer under the pointer (#88).
|
||||
//! (#85); an effect dropped on the viewer goes on the layer under the pointer (#88). Files
|
||||
//! dropped on the window import with visible progress (#270).
|
||||
|
||||
use std::sync::Arc;
|
||||
use std::sync::atomic::{AtomicUsize, Ordering};
|
||||
|
||||
use effectcraft_engine::Session;
|
||||
use effectcraft_engine::keyframe::Value as KV;
|
||||
@@ -165,3 +169,91 @@ fn project_items_dropped_on_new_comp_make_a_composition() {
|
||||
assert!(h.state().auto.find("form.field.single").is_some(), "the New Composition from Selection dialog");
|
||||
assert_eq!(comps(&h), before + 1, "nothing made yet");
|
||||
}
|
||||
|
||||
/// A file dropped on the window (egui hands it over as a dropped file).
|
||||
#[derive(Debug)]
|
||||
struct Dropped(std::path::PathBuf);
|
||||
|
||||
impl egui::DroppedFile for Dropped {
|
||||
fn path(&self) -> &std::path::Path {
|
||||
&self.0
|
||||
}
|
||||
fn bytes(&self) -> Result<Vec<u8>, String> {
|
||||
Err("not read".into())
|
||||
}
|
||||
}
|
||||
|
||||
/// Footage probes that hold file k until `allow` > k (slow video decoding), 5 s at most (an
|
||||
/// import that blocks the app then fails the test instead of hanging it).
|
||||
struct Gated {
|
||||
started: AtomicUsize,
|
||||
allow: Arc<AtomicUsize>,
|
||||
}
|
||||
|
||||
impl effectcraft_engine::Importer for Gated {
|
||||
fn probe(&self, path: &str) -> Result<effectcraft_engine::project::Footage, String> {
|
||||
let k = self.started.fetch_add(1, Ordering::SeqCst);
|
||||
let until = std::time::Instant::now() + std::time::Duration::from_secs(5);
|
||||
while self.allow.load(Ordering::SeqCst) <= k && std::time::Instant::now() < until {
|
||||
std::thread::sleep(std::time::Duration::from_millis(1));
|
||||
}
|
||||
Ok(effectcraft_engine::project::Footage { path: path.into(), width: 64, height: 32, has_video: true, ..Default::default() })
|
||||
}
|
||||
}
|
||||
|
||||
/// Step the app until `done` holds (the import runs on another thread).
|
||||
fn step_until(h: &mut Harness<'_, EffectcraftApp>, done: impl Fn(&Harness<'_, EffectcraftApp>) -> bool) {
|
||||
let deadline = std::time::Instant::now() + std::time::Duration::from_secs(10);
|
||||
while !done(h) {
|
||||
assert!(std::time::Instant::now() < deadline, "timed out");
|
||||
std::thread::sleep(std::time::Duration::from_millis(2));
|
||||
h.step();
|
||||
}
|
||||
}
|
||||
|
||||
/// #270: files dropped on the window import in the background with an Importing card that names
|
||||
/// the file being read and how many are left (the app keeps drawing meanwhile); then they are
|
||||
/// selected and shown in the Project panel, with the count in a toast. The same file dropped
|
||||
/// again arrives again, selected.
|
||||
#[test]
|
||||
fn dropped_files_show_import_progress_then_the_items_they_made() {
|
||||
let (mut h, _) = harness();
|
||||
let allow = Arc::new(AtomicUsize::new(1));
|
||||
h.state_mut().session.importer = Some(Arc::new(Gated { started: AtomicUsize::new(0), allow: allow.clone() }));
|
||||
let file = |n: &str| std::env::temp_dir().join(n);
|
||||
for n in ["a.png", "b.png", "c.png"] {
|
||||
h.input_mut().dropped_files.push(Arc::new(Dropped(file(n))));
|
||||
}
|
||||
h.run_steps(2);
|
||||
let card = |h: &Harness<'_, EffectcraftApp>| h.state().auto.query("import.job.").first().map(|e| e.label.clone());
|
||||
step_until(&mut h, |h| card(h).as_deref() == Some("b.png (2 of 3)"));
|
||||
let r = rect(&h, &card_id(&h));
|
||||
assert!(r.min.x < 40.0 && r.max.y > 960.0, "bottom left, over the panels (where toasts show): {r:?}");
|
||||
assert!(h.state().auto.find(&card_id(&h).replace("import.job.", "import.cancel.")).is_some(), "with Cancel");
|
||||
let items = |h: &Harness<'_, EffectcraftApp>| h.state().session.project.items.values().filter(|i| i.name.ends_with(".png")).count();
|
||||
assert_eq!(items(&h), 0, "nothing added until the import finishes");
|
||||
allow.store(usize::MAX, Ordering::SeqCst);
|
||||
step_until(&mut h, |h| card(h).is_none() && items(h) == 3);
|
||||
h.run_steps(2);
|
||||
let st = &h.state().session.state;
|
||||
let names: Vec<String> = st.project_selection.iter().map(|i| h.state().session.project.item(*i).unwrap().name.clone()).collect();
|
||||
assert_eq!(names, ["a.png", "b.png", "c.png"], "what arrived is selected");
|
||||
let last = st.project_selection[2].0;
|
||||
assert!(h.state().auto.find(&format!("project.item.{last}")).is_some(), "and in view in the Project panel");
|
||||
assert_eq!(h.state().auto.find("toast").map(|e| e.label.as_str()), Some("Imported 3 items"));
|
||||
|
||||
// The same file again: imported again, and that one is selected.
|
||||
h.input_mut().dropped_files.push(Arc::new(Dropped(file("a.png"))));
|
||||
h.run_steps(2);
|
||||
step_until(&mut h, |h| items(h) == 4);
|
||||
h.run_steps(2);
|
||||
let sel = &h.state().session.state.project_selection;
|
||||
assert_eq!(sel.len(), 1);
|
||||
assert_eq!(h.state().session.project.item(sel[0]).unwrap().name, "a.png");
|
||||
assert_eq!(h.state().auto.find("toast").map(|e| e.label.as_str()), Some("Imported 1 item"));
|
||||
}
|
||||
|
||||
/// The automation id of the Importing card on screen.
|
||||
fn card_id(h: &Harness<'_, EffectcraftApp>) -> String {
|
||||
h.state().auto.query("import.job.").first().map(|e| e.id.clone()).unwrap_or_default()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user