M3.16: deleting Project items that compositions use asks first

As in After Effects, the Delete key and the Project panel's trash button ask before deleting
items that layers in other compositions use, saying how many layers in how many compositions go
with them (Delete / Cancel; Escape cancels). Unused items are deleted at once; agents running
project.delete directly are not asked. project.usage {items?} reports the count, sharing the
deletion's item and usage helpers. Follow-up to #72.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
bflatastic
2026-10-06 11:35:57 +07:00
co-authored by Claude Opus 5.5
parent b46be83ab4
commit 9624024dc1
7 changed files with 176 additions and 8 deletions
+2
View File
@@ -84,6 +84,8 @@ pub enum Dialog {
RenderTemplates,
/// Save changes before closing the project? (`panels::unsaved`).
UnsavedChanges,
/// Delete Project items that compositions use? (`panels::delete_items`).
DeleteItems,
}
/// Host hooks provided by the native app (file pickers etc.).
+4
View File
@@ -196,6 +196,10 @@ pub fn invoke(app: &mut EffectcraftApp, ctx: &egui::Context, id: &str, params: V
if crate::panels::unsaved::guard(app, id, &params) {
return Ok(json!({"dialog": "unsavedChanges"}));
}
// Deleting Project items that compositions use asks first, as in After Effects.
if crate::panels::delete_items::guard(app, id, &params) {
return Ok(json!({"dialog": "deleteItems"}));
}
// J / K and Select All Keyframes act on what the Timeline shows (its revealed properties).
if matches!(id, "time.nextKey" | "time.previousKey" | "keys.selectAll")
&& ["visible", "layers", "layer", "prop"].iter().all(|k| params.get(*k).is_none())
+86
View File
@@ -0,0 +1,86 @@
//! Deleting Project items that compositions use asks first, as in After Effects: the Delete key
//! and the Project panel's trash button (`project.delete` through the UI) show how many layers in
//! how many compositions go with the items (`project.usage`). Items nothing uses are deleted at
//! once. Agents running `project.delete` with `engine.execute` are never asked.
//!
//! Automation ids: `dialog.deleteItems.delete`, `dialog.deleteItems.cancel`.
use egui::{Color32, vec2};
use serde_json::Value;
use crate::theme::Tokens;
use crate::{Dialog, EffectcraftApp};
/// The deletion waiting on the prompt: its params and what it takes with it.
#[derive(Clone, Debug, Default)]
pub struct Pending {
pub command: Option<(Value, Value)>,
/// The next `project.delete` runs without asking (the user answered Delete).
pub answered: bool,
}
fn plural(n: u64, one: &str, many: &str) -> String {
format!("{n} {}", if n == 1 { one } else { many })
}
/// Whether `id` needs the prompt first. When it does, the prompt opens holding the command and
/// the caller must not run it (it runs once the user answers Delete).
pub fn guard(app: &mut EffectcraftApp, id: &str, params: &Value) -> bool {
if id != "project.delete" || std::mem::take(&mut app.dialog_state.delete_items.answered) {
return false;
}
let Ok(usage) = app.session.execute("project.usage", params.clone()) else { return false };
if usage["layers"].as_u64().unwrap_or(0) == 0 {
return false;
}
app.dialog_state.delete_items.command = Some((params.clone(), usage));
app.dialog = Some(Dialog::DeleteItems);
true
}
pub fn show(app: &mut EffectcraftApp, ctx: &egui::Context, t: &Tokens) {
let Some((params, usage)) = app.dialog_state.delete_items.command.clone() else {
app.dialog = None;
return;
};
let n = |k: &str| usage[k].as_u64().unwrap_or(0);
let mut answer: Option<bool> = None;
super::dialogs::modal(ctx, "Delete Items", vec2(460.0, 160.0), t, |ui| {
let text = format!(
"Delete {}? {} in {} {} {} and will be deleted too.",
plural(n("items"), "item", "items"),
plural(n("layers"), "layer", "layers"),
plural(n("comps"), "composition", "compositions"),
if n("layers") == 1 { "uses" } else { "use" },
if n("items") == 1 { "it" } else { "them" },
);
ui.label(egui::RichText::new(text).color(t.tab_text_active));
ui.add_space(4.0);
ui.label(egui::RichText::new("You can undo this.").color(t.text_dim).size(11.0));
ui.add_space(18.0);
ui.with_layout(egui::Layout::right_to_left(egui::Align::Center), |ui| {
let r = ui.add(egui::Button::new(egui::RichText::new(" Delete ").color(Color32::WHITE)).fill(t.danger));
app.auto.add("dialog.deleteItems.delete", r.rect, "Delete");
if r.clicked() {
answer = Some(true);
}
let r = ui.button("Cancel");
app.auto.add("dialog.deleteItems.cancel", r.rect, "Cancel");
if r.clicked() {
answer = Some(false);
}
});
});
if answer.is_none() && ctx.input(|i| i.key_pressed(egui::Key::Escape)) {
answer = Some(false);
}
let Some(delete) = answer else { return };
app.dialog = None;
app.dialog_state.delete_items.command = None;
if delete {
app.dialog_state.delete_items.answered = true;
if let Err(e) = crate::menus::invoke(app, ctx, "project.delete", params) {
app.ui.status = e;
}
}
}
+3
View File
@@ -57,6 +57,8 @@ pub struct DialogState {
pub layer_style: super::layer_styles_dialog::LayerStyleState,
/// The unsaved-changes prompt and the command it holds.
pub unsaved: super::unsaved::Pending,
/// Deleting Project items in use: the deletion waiting on the prompt.
pub delete_items: super::delete_items::Pending,
}
pub fn open_new_comp(app: &mut EffectcraftApp) {
@@ -188,6 +190,7 @@ pub fn show(app: &mut EffectcraftApp, ctx: &egui::Context) {
Dialog::TrackApply => super::tracker::apply_dialog(app, ctx, &t),
Dialog::RenderTemplates => super::rq_templates::show(app, ctx, &t),
Dialog::UnsavedChanges => super::unsaved::show(app, ctx, &t),
Dialog::DeleteItems => super::delete_items::show(app, ctx, &t),
}
}
+1
View File
@@ -4,6 +4,7 @@ pub mod anim_tools;
pub mod camera_tracker_ui;
pub mod comp_settings;
pub mod content_fill_panel;
pub mod delete_items;
pub mod dialogs;
pub mod dialogs_3d;
pub mod effect_controls;