M3.9: closing a modified project asks to save it first; the modified mark follows undo
- Quit and the window's close button, File ▸ New Project, Open Project, Open Recent, Close
Project, the demo project and a Home template show Save / Don't Save / Cancel when the
project has unsaved changes (Save asks for a path when untitled; cancelling that keeps the
project open). File ▸ Revert asks to discard. Before, all of them dropped unsaved work
without asking.
- The window title shows the project's path and `*` while it is modified.
- The modified mark follows undo: the session keeps the snapshot it last opened or saved
(`Session::saved_project`), so undoing, jumping in the History panel or rolling back a batch
to the saved state clears it.
- An edit that changes nothing (a value set to what it already is) records no undo step and
doesn't mark the project modified (`Project::same_content`, comparing untouched comps by
pointer).
- Agents' `engine.execute` / MCP calls are never asked; `ui.menu.invoke` shows the prompt like a
click (automation ids `dialog.unsaved.save` / `dontSave` / `revert` / `cancel`), and the
control channel's `app.quit {force: true}` skips it.
- Docs: architecture, agents, control protocol, parity.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
10644a83aa
commit
e0ed28f55d
@@ -17,7 +17,7 @@
|
||||
//! - `ui.playback {action: play|stop|toggle}`
|
||||
//! - `ui.screenshot {path?, panel?, id?}`: PNG of the window (or one panel / element)
|
||||
//! - `render.frame {comp?, time?, max_side?, path?, base64?}`: PNG of a comp frame rendered by the session
|
||||
//! - `ui.resize {width, height}` / `ui.focus` / `app.quit`
|
||||
//! - `ui.resize {width, height}` / `ui.focus` / `app.quit {force?}` (a modified project asks to save first unless `force`)
|
||||
//!
|
||||
//! The full reference with examples is `docs/control-protocol.md`.
|
||||
|
||||
@@ -436,6 +436,10 @@ pub fn handle(app: &mut EffectcraftApp, ctx: &egui::Context, req: &ControlReques
|
||||
ok(Value::Null)
|
||||
}
|
||||
"app.quit" => {
|
||||
// A modified project asks to save first unless `force: true`.
|
||||
if p.get("force").and_then(Value::as_bool).unwrap_or(false) {
|
||||
app.dialog_state.unsaved.quitting = true;
|
||||
}
|
||||
ctx.send_viewport_cmd(egui::ViewportCommand::Close);
|
||||
ok(Value::Null)
|
||||
}
|
||||
|
||||
@@ -77,6 +77,8 @@ pub enum Dialog {
|
||||
LayerStyles,
|
||||
/// Edit ▸ Templates ▸ Render Settings… / Output Module….
|
||||
RenderTemplates,
|
||||
/// Save changes before closing the project? (`panels::unsaved`).
|
||||
UnsavedChanges,
|
||||
}
|
||||
|
||||
/// Host hooks provided by the native app (file pickers etc.).
|
||||
@@ -192,6 +194,8 @@ pub struct EffectcraftApp {
|
||||
pub(crate) home_thumbs: std::collections::HashMap<String, Option<egui::TextureHandle>>,
|
||||
/// The (project path, saved revision) whose thumbnail was stored last.
|
||||
pub(crate) home_thumb_saved: Option<(String, u64)>,
|
||||
/// The window title last sent (project name, `*` while modified).
|
||||
window_title: String,
|
||||
/// Home ▸ Templates: thumbnail textures by template id (None = not renderable).
|
||||
pub(crate) template_thumbs: std::collections::HashMap<String, Option<egui::TextureHandle>>,
|
||||
/// Home ▸ Templates: the gallery, and the user template files it was listed from.
|
||||
@@ -244,6 +248,7 @@ impl EffectcraftApp {
|
||||
dock_rects: vec![],
|
||||
home_thumbs: Default::default(),
|
||||
home_thumb_saved: None,
|
||||
window_title: String::new(),
|
||||
template_thumbs: Default::default(),
|
||||
template_list: None,
|
||||
}
|
||||
@@ -1056,6 +1061,12 @@ impl EffectcraftApp {
|
||||
}
|
||||
self.apply_prefs(&ctx);
|
||||
self.handle_events(&ctx);
|
||||
panels::unsaved::on_close_requested(self, &ctx);
|
||||
let title = panels::unsaved::window_title(self);
|
||||
if title != self.window_title {
|
||||
ctx.send_viewport_cmd(egui::ViewportCommand::Title(title.clone()));
|
||||
self.window_title = title;
|
||||
}
|
||||
self.tick_autosave(&ctx);
|
||||
prefs_live::frame(self, &ctx);
|
||||
if let Some(rx) = self.command_inbox.take() {
|
||||
|
||||
@@ -192,6 +192,10 @@ fn no_params(p: &Value) -> bool {
|
||||
/// Execute a UI or engine command by id.
|
||||
pub fn invoke(app: &mut EffectcraftApp, ctx: &egui::Context, id: &str, params: Value) -> Result<Value, String> {
|
||||
let now = ctx.input(|i| i.time);
|
||||
// Closing a modified project asks to save it first; the command runs once answered.
|
||||
if crate::panels::unsaved::guard(app, id, ¶ms) {
|
||||
return Ok(json!({"dialog": "unsavedChanges"}));
|
||||
}
|
||||
// New Camera/Light and Camera/Light Settings without parameters open their dialogs.
|
||||
if crate::panels::dialogs_3d::route(app, id, ¶ms)? {
|
||||
return Ok(Value::Null);
|
||||
|
||||
@@ -44,6 +44,8 @@ pub struct DialogState {
|
||||
pub precompose: super::precomp::PrecomposeDraft,
|
||||
/// Layer Style dialog.
|
||||
pub layer_style: super::layer_styles_dialog::LayerStyleState,
|
||||
/// The unsaved-changes prompt and the command it holds.
|
||||
pub unsaved: super::unsaved::Pending,
|
||||
}
|
||||
|
||||
pub fn open_new_comp(app: &mut EffectcraftApp) {
|
||||
@@ -130,6 +132,7 @@ pub fn show(app: &mut EffectcraftApp, ctx: &egui::Context) {
|
||||
Dialog::TrackTarget => super::tracker::target_dialog(app, ctx, &t),
|
||||
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),
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -349,6 +349,8 @@ fn run_actions(app: &mut EffectcraftApp, ctx: &egui::Context, actions: Vec<(&str
|
||||
let before = (app.session.path.clone(), app.session.revision);
|
||||
match crate::menus::invoke(app, ctx, id, params) {
|
||||
Err(e) => app.ui.status = e,
|
||||
// Asking to save the open project first: Home stays until it is answered.
|
||||
Ok(r) if r.get("dialog").is_some() => {}
|
||||
// Leave Home unless only the list changed or a file dialog was cancelled.
|
||||
Ok(_) => {
|
||||
let cancelled = matches!(id, "file.open" | "file.import") && (app.session.path.clone(), app.session.revision) == before;
|
||||
|
||||
@@ -46,6 +46,7 @@ pub mod templates;
|
||||
pub mod text_panels;
|
||||
pub mod timeline;
|
||||
pub mod tracker;
|
||||
pub mod unsaved;
|
||||
pub mod viewer;
|
||||
pub mod viewer_overlays;
|
||||
pub mod viewer_text;
|
||||
|
||||
@@ -158,6 +158,10 @@ pub fn home_tab(app: &mut EffectcraftApp, ui: &mut egui::Ui, area: Rect) {
|
||||
Err(e) => app.ui.status = e.to_string(),
|
||||
}
|
||||
} else if let Some(id) = open {
|
||||
// A modified project asks to save first (the template opens once answered).
|
||||
if super::unsaved::guard(app, "templates.create", &json!({"id": id})) {
|
||||
return;
|
||||
}
|
||||
match app.session.execute("templates.create", json!({"id": id})) {
|
||||
Ok(_) => app.ui.start_screen = false,
|
||||
Err(e) => app.ui.status = e.to_string(),
|
||||
|
||||
@@ -0,0 +1,271 @@
|
||||
//! The unsaved-changes prompt: commands that close the open project (Quit and closing the
|
||||
//! window, File ▸ New Project / Open / Open Recent / Close Project / Revert, the demo project, a
|
||||
//! Home template) first ask to save a modified project. Save saves (asking for a path when the
|
||||
//! project is untitled) and continues; Don't Save continues; Cancel keeps the project open.
|
||||
//! Revert only asks to discard. Agents running commands with `engine.execute` are never asked.
|
||||
//!
|
||||
//! Automation ids: `dialog.unsaved.save`, `dialog.unsaved.dontSave` (Revert: `dialog.unsaved.revert`),
|
||||
//! `dialog.unsaved.cancel`.
|
||||
|
||||
use egui::{Color32, vec2};
|
||||
use serde_json::{Value, json};
|
||||
|
||||
use crate::theme::Tokens;
|
||||
use crate::{Dialog, EffectcraftApp};
|
||||
|
||||
/// Commands that replace or close the open project (Quit is asked when the window closes).
|
||||
const CLOSING: &[&str] = &["file.newProject", "file.open", "file.openRecent", "file.closeProject", "file.revert", "file.openDemoProject", "templates.create"];
|
||||
|
||||
/// The command waiting on the prompt, and whether the user already answered it.
|
||||
#[derive(Clone, Debug, Default)]
|
||||
pub struct Pending {
|
||||
pub command: Option<(String, Value)>,
|
||||
/// The next guarded command runs without asking (the user answered Save or Don't Save).
|
||||
pub answered: bool,
|
||||
/// The window closes without asking (the user answered the Quit prompt).
|
||||
pub quitting: bool,
|
||||
}
|
||||
|
||||
/// 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).
|
||||
pub fn guard(app: &mut EffectcraftApp, id: &str, params: &Value) -> bool {
|
||||
if !CLOSING.contains(&id) || std::mem::take(&mut app.dialog_state.unsaved.answered) {
|
||||
return false;
|
||||
}
|
||||
ask(app, id, params)
|
||||
}
|
||||
|
||||
fn ask(app: &mut EffectcraftApp, id: &str, params: &Value) -> bool {
|
||||
if !app.session.is_dirty() {
|
||||
return false;
|
||||
}
|
||||
app.dialog_state.unsaved.command = Some((id.to_string(), params.clone()));
|
||||
app.dialog = Some(Dialog::UnsavedChanges);
|
||||
true
|
||||
}
|
||||
|
||||
/// The window's close button (and Quit, which closes it): keep the window open and ask.
|
||||
pub fn on_close_requested(app: &mut EffectcraftApp, ctx: &egui::Context) {
|
||||
if !ctx.input(|i| i.viewport().close_requested()) || app.dialog_state.unsaved.quitting {
|
||||
return;
|
||||
}
|
||||
if ask(app, "app.quit", &json!({})) {
|
||||
ctx.send_viewport_cmd(egui::ViewportCommand::CancelClose);
|
||||
}
|
||||
}
|
||||
|
||||
/// The project's display name: its file name, or `Untitled Project.ecproj`.
|
||||
pub fn project_name(app: &EffectcraftApp) -> String {
|
||||
app.session
|
||||
.path
|
||||
.as_deref()
|
||||
.and_then(|p| std::path::Path::new(p).file_name())
|
||||
.map(|n| n.to_string_lossy().into_owned())
|
||||
.unwrap_or_else(|| "Untitled Project.ecproj".into())
|
||||
}
|
||||
|
||||
/// The window title: the project's path (or name) and `*` while it has unsaved changes.
|
||||
pub fn window_title(app: &EffectcraftApp) -> String {
|
||||
let doc = app.session.path.clone().unwrap_or_else(|| project_name(app));
|
||||
format!("EffectCraft - {doc}{}", if app.session.is_dirty() { " *" } else { "" })
|
||||
}
|
||||
|
||||
enum Answer {
|
||||
Save,
|
||||
Discard,
|
||||
Cancel,
|
||||
}
|
||||
|
||||
pub fn show(app: &mut EffectcraftApp, ctx: &egui::Context, t: &Tokens) {
|
||||
let Some((id, _)) = app.dialog_state.unsaved.command.clone() else {
|
||||
app.dialog = None;
|
||||
return;
|
||||
};
|
||||
let revert = id == "file.revert";
|
||||
let name = project_name(app);
|
||||
let mut answer = None;
|
||||
let title = if revert { "Revert Project" } else { "Unsaved Changes" };
|
||||
super::dialogs::modal(ctx, title, vec2(460.0, 170.0), t, |ui| {
|
||||
let text = if revert {
|
||||
format!("Revert “{name}” to the last saved version? Changes made since then will be lost.")
|
||||
} else {
|
||||
format!("Save changes to “{name}” before closing?")
|
||||
};
|
||||
ui.label(egui::RichText::new(text).color(t.tab_text_active));
|
||||
if !revert {
|
||||
ui.add_space(4.0);
|
||||
ui.label(egui::RichText::new("Your changes will be lost if you don't save them.").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 (label, key) = if revert { (" Revert ", "dialog.unsaved.revert") } else { (" Save ", "dialog.unsaved.save") };
|
||||
let r = ui.add(egui::Button::new(egui::RichText::new(label).color(Color32::WHITE)).fill(t.accent));
|
||||
app.auto.add(key, r.rect, label.trim());
|
||||
if r.clicked() {
|
||||
answer = Some(if revert { Answer::Discard } else { Answer::Save });
|
||||
}
|
||||
let r = ui.button("Cancel");
|
||||
app.auto.add("dialog.unsaved.cancel", r.rect, "Cancel");
|
||||
if r.clicked() {
|
||||
answer = Some(Answer::Cancel);
|
||||
}
|
||||
if !revert {
|
||||
// Set apart from Save, as is customary.
|
||||
ui.add_space(ui.available_width() - 90.0);
|
||||
let r = ui.button("Don't Save");
|
||||
app.auto.add("dialog.unsaved.dontSave", r.rect, "Don't Save");
|
||||
if r.clicked() {
|
||||
answer = Some(Answer::Discard);
|
||||
}
|
||||
}
|
||||
});
|
||||
});
|
||||
if answer.is_none() && ctx.input(|i| i.key_pressed(egui::Key::Escape)) {
|
||||
answer = Some(Answer::Cancel);
|
||||
}
|
||||
if let Some(a) = answer {
|
||||
answer_with(app, ctx, a);
|
||||
}
|
||||
}
|
||||
|
||||
fn answer_with(app: &mut EffectcraftApp, ctx: &egui::Context, a: Answer) {
|
||||
app.dialog = None;
|
||||
let Some((id, params)) = app.dialog_state.unsaved.command.take() else { return };
|
||||
match a {
|
||||
Answer::Cancel => return,
|
||||
Answer::Save => {
|
||||
// An untitled project asks for a path; cancelling that dialog cancels the command.
|
||||
if let Err(e) = crate::menus::invoke(app, ctx, "file.save", json!({})) {
|
||||
app.ui.status = e;
|
||||
return;
|
||||
}
|
||||
if app.session.is_dirty() {
|
||||
return;
|
||||
}
|
||||
}
|
||||
Answer::Discard => {}
|
||||
}
|
||||
if id == "app.quit" {
|
||||
app.dialog_state.unsaved.quitting = true;
|
||||
ctx.send_viewport_cmd(egui::ViewportCommand::Close);
|
||||
return;
|
||||
}
|
||||
app.dialog_state.unsaved.answered = true;
|
||||
let before = (app.session.path.clone(), app.session.revision);
|
||||
if let Err(e) = crate::menus::invoke(app, ctx, &id, params) {
|
||||
app.ui.status = e;
|
||||
}
|
||||
// Leave Home once something opened from it (not when its file dialog was cancelled).
|
||||
if (app.session.path.clone(), app.session.revision) != before {
|
||||
app.ui.start_screen = false;
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
fn app() -> (EffectcraftApp, egui::Context) {
|
||||
let ctx = egui::Context::default();
|
||||
let app = EffectcraftApp::new(effectcraft_engine::Session::default());
|
||||
crate::theme::install(&ctx, &app.tokens);
|
||||
(app, ctx)
|
||||
}
|
||||
|
||||
/// One frame of the dialogs, with `close` = the window's close button pressed; returns
|
||||
/// whether the frame asked the window to stay open.
|
||||
fn frame(app: &mut EffectcraftApp, ctx: &egui::Context, close: bool) -> bool {
|
||||
let mut raw = egui::RawInput::default();
|
||||
if close {
|
||||
raw.viewports.entry(egui::ViewportId::ROOT).or_default().events.push(egui::ViewportEvent::Close);
|
||||
}
|
||||
let mut out = ctx.run_ui(raw, |ui| {
|
||||
app.auto.begin_frame();
|
||||
on_close_requested(app, ui.ctx());
|
||||
crate::panels::dialogs::show(app, ui.ctx());
|
||||
});
|
||||
out.textures_delta.clear();
|
||||
out.viewport_output.get(&egui::ViewportId::ROOT).is_some_and(|v| v.commands.contains(&egui::ViewportCommand::CancelClose))
|
||||
}
|
||||
|
||||
fn modified(app: &mut EffectcraftApp) {
|
||||
app.session.execute("comp.new", json!({"name": "Main", "width": 64, "height": 64})).unwrap();
|
||||
assert!(app.session.is_dirty());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn closing_a_modified_project_asks_and_cancel_keeps_it() {
|
||||
let (mut app, ctx) = app();
|
||||
// An unmodified project closes without asking.
|
||||
crate::menus::invoke(&mut app, &ctx, "file.newProject", json!({})).unwrap();
|
||||
assert!(app.dialog.is_none());
|
||||
modified(&mut app);
|
||||
assert!(window_title(&app).ends_with("Untitled Project.ecproj *"), "{}", window_title(&app));
|
||||
let r = crate::menus::invoke(&mut app, &ctx, "file.newProject", json!({})).unwrap();
|
||||
assert_eq!(r, json!({"dialog": "unsavedChanges"}));
|
||||
assert_eq!(app.dialog, Some(Dialog::UnsavedChanges));
|
||||
assert_eq!(app.session.project.comps().count(), 1, "nothing closed yet");
|
||||
frame(&mut app, &ctx, false);
|
||||
for id in ["dialog.unsaved.save", "dialog.unsaved.dontSave", "dialog.unsaved.cancel"] {
|
||||
assert!(app.auto.find(id).is_some(), "{id}");
|
||||
}
|
||||
answer_with(&mut app, &ctx, Answer::Cancel);
|
||||
assert!(app.dialog.is_none() && app.session.is_dirty());
|
||||
assert_eq!(app.session.project.comps().count(), 1);
|
||||
// Don't Save runs the command.
|
||||
crate::menus::invoke(&mut app, &ctx, "file.newProject", json!({})).unwrap();
|
||||
answer_with(&mut app, &ctx, Answer::Discard);
|
||||
assert_eq!(app.session.project.comps().count(), 0);
|
||||
assert!(!app.session.is_dirty() && app.dialog.is_none());
|
||||
assert!(!app.dialog_state.unsaved.answered, "the answer is used once");
|
||||
// Agents (`engine.execute`) are never asked.
|
||||
modified(&mut app);
|
||||
app.session.execute("file.closeProject", json!({})).unwrap();
|
||||
assert!(app.dialog.is_none() && app.session.project.comps().count() == 0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn save_saves_then_continues_and_revert_only_asks_to_discard() {
|
||||
let (mut app, ctx) = app();
|
||||
let dir = std::env::temp_dir().join(format!("ec-unsaved-{}", std::process::id()));
|
||||
std::fs::create_dir_all(&dir).unwrap();
|
||||
let path = dir.join("Kept.ecproj");
|
||||
modified(&mut app);
|
||||
app.session.execute("file.saveAs", json!({"path": path.to_string_lossy()})).unwrap();
|
||||
assert!(!app.session.is_dirty());
|
||||
assert!(window_title(&app).ends_with("Kept.ecproj"), "{}", window_title(&app));
|
||||
// Revert: Revert / Cancel.
|
||||
app.session.execute("layer.newNull", json!({})).unwrap();
|
||||
crate::menus::invoke(&mut app, &ctx, "file.revert", json!({})).unwrap();
|
||||
frame(&mut app, &ctx, false);
|
||||
assert!(app.auto.find("dialog.unsaved.revert").is_some() && app.auto.find("dialog.unsaved.save").is_none());
|
||||
answer_with(&mut app, &ctx, Answer::Discard);
|
||||
assert_eq!(app.session.active_comp().unwrap().layers.len(), 0, "reverted");
|
||||
// Save: the change is written, then the project closes.
|
||||
app.session.execute("layer.newNull", json!({})).unwrap();
|
||||
crate::menus::invoke(&mut app, &ctx, "file.closeProject", json!({})).unwrap();
|
||||
answer_with(&mut app, &ctx, Answer::Save);
|
||||
assert_eq!(app.session.project.comps().count(), 0, "closed");
|
||||
app.session.execute("file.open", json!({"path": path.to_string_lossy()})).unwrap();
|
||||
assert_eq!(app.session.active_comp().unwrap().layers.len(), 1, "the null was saved");
|
||||
let _ = std::fs::remove_dir_all(dir);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_window_stays_open_until_the_quit_prompt_is_answered() {
|
||||
let (mut app, ctx) = app();
|
||||
assert!(!frame(&mut app, &ctx, true), "an unmodified project quits at once");
|
||||
modified(&mut app);
|
||||
assert!(frame(&mut app, &ctx, true), "the close is cancelled");
|
||||
assert_eq!(app.dialog, Some(Dialog::UnsavedChanges));
|
||||
answer_with(&mut app, &ctx, Answer::Discard);
|
||||
assert!(app.dialog_state.unsaved.quitting);
|
||||
assert!(!frame(&mut app, &ctx, true), "Don't Save lets the window close");
|
||||
// Undoing back to the saved state needs no prompt.
|
||||
let (mut app, ctx) = self::app();
|
||||
modified(&mut app);
|
||||
app.session.execute("edit.undo", json!({})).unwrap();
|
||||
assert!(!app.session.is_dirty());
|
||||
assert!(!frame(&mut app, &ctx, true));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user