Merge pull request #1176 from tylerjenningsw/fix/report-workspace-save-errors

This commit is contained in:
Brandon Thomas
2026-10-13 04:29:16 -04:00
7 changed files with 232 additions and 11 deletions
+11 -3
View File
@@ -430,9 +430,17 @@ impl EffectcraftApp {
}
/// Keep the saved workspaces for the next session (see [`EffectcraftApp::new`]): After
/// Effects' saved workspaces last until they are deleted (#320).
pub(crate) fn store_saved_workspaces(&mut self) {
self.save_workspace_state();
/// Effects' saved workspaces last until they are deleted (#320). `Err` when they weren't
/// written (#696): the change applies for this session and a later save retries it, but until
/// then a restart brings back what is on disk.
pub(crate) fn store_saved_workspaces(&mut self) -> Result<(), String> {
let Err(error) = self.workspace_store.save_now(self.session.config.as_deref(), &self.ui) else { return Ok(()) };
let message = format!("Workspace changes couldn't be saved and may be lost after a restart: {error}");
log::warn!("{message}");
self.ui.status = message.clone();
// The raw error, so the background retry doesn't report the same failure again.
self.workspace_store.error = Some(error);
Err(message)
}
/// Offer the frontend's commands (tools, timeline navigation…) for keyboard shortcuts.
+16 -7
View File
@@ -839,18 +839,26 @@ fn clipboard_note(s: &effectcraft_engine::Session) -> String {
}
pub(crate) fn run_engine(app: &mut EffectcraftApp, ctx: &egui::Context, id: &str, params: Value) -> Result<Value, String> {
let r = app.session.execute(id, params).map_err(|e| e.to_string());
// Events already queued belong to earlier invocations (`engine.execute` defers them).
let queued = app.session.events.len();
let mut r = app.session.execute(id, params).map_err(|e| e.to_string());
if let Err(e) = &r {
app.ui.status = e.clone();
} else if COPY_COMMANDS.contains(&id) && app.session.state.text_edit.is_none() {
ctx.copy_text(clipboard_note(&app.session));
}
let events = app.session.drain_events();
for ev in events {
for (index, ev) in events.into_iter().enumerate() {
match ev {
effectcraft_engine::Event::Frontend { command, params } => {
if let Err(e) = frontend(app, ctx, &command, params) {
app.ui.status = e;
app.ui.status = e.clone();
// A frontend-only command's own failure is the command's result (#696: a
// workspace save that didn't reach disk reported success). An earlier
// invocation's failure only sets the status.
if index >= queued && command == id && r.is_ok() {
r = Err(e);
}
}
}
other => app.session.events.push(other),
@@ -1099,7 +1107,7 @@ pub fn frontend(app: &mut EffectcraftApp, ctx: &egui::Context, id: &str, p: Valu
let name = app.ui.workspace.clone();
app.ui.saved_workspaces.insert(name.clone(), app.ui.dock.clone());
app.ui.saved_floating.insert(name.clone(), app.ui.floating.clone());
app.store_saved_workspaces();
app.store_saved_workspaces()?;
json!({"workspace": name})
}
"window.saveWorkspaceAs" => {
@@ -1116,7 +1124,7 @@ pub fn frontend(app: &mut EffectcraftApp, ctx: &egui::Context, id: &str, p: Valu
app.ui.saved_workspaces.insert(name.clone(), app.ui.dock.clone());
app.ui.saved_floating.insert(name.clone(), app.ui.floating.clone());
app.ui.workspace = name.clone();
app.store_saved_workspaces();
app.store_saved_workspaces()?;
json!({"workspace": name})
}
"window.editWorkspaces" => {
@@ -1141,10 +1149,11 @@ pub fn frontend(app: &mut EffectcraftApp, ctx: &egui::Context, id: &str, p: Valu
if app.ui.saved_workspaces.remove(&name).is_none() && !builtin {
return Err(format!("no workspace `{name}`"));
}
app.store_saved_workspaces();
// Leave the deleted workspace first: a current workspace needs a definition.
if app.ui.workspace == name {
app.set_workspace("Default");
}
app.store_saved_workspaces()?;
return Ok(json!({"deleted": name}));
}
if let Some(new) = p.get("rename").and_then(Value::as_str).map(str::trim).filter(|n| !n.is_empty() && *n != name) {
@@ -1159,7 +1168,7 @@ pub fn frontend(app: &mut EffectcraftApp, ctx: &egui::Context, id: &str, p: Valu
if app.ui.workspace == name {
app.ui.workspace = new.to_string();
}
app.store_saved_workspaces();
app.store_saved_workspaces()?;
return Ok(json!({"renamed": new}));
}
json!({"workspaces": app.workspace_names()})
+1
View File
@@ -429,6 +429,7 @@ mod tests {
#[test]
fn saved_workspaces_are_in_the_native_workspace_menu() {
let mut app = app();
app.session.config = Some(std::sync::Arc::new(effectcraft_engine::config::MemoryConfig::default()));
let ctx = egui::Context::default();
crate::menus::invoke(&mut app, &ctx, "window.saveWorkspaceAs", json!({"name": "My Layout"})).unwrap();
let entry = |app: &EffectcraftApp| build(app).items().into_iter().find(|i| i.label == "My Layout").cloned();
+34
View File
@@ -263,6 +263,19 @@ impl WorkspaceStore {
self.error = None;
Ok(true)
}
/// An explicit save (Save Workspace, Save as New Workspace, Edit Workspaces): unlike
/// [`Self::save`], no settings folder or a file blocked at load is an error, not "nothing to do".
pub(crate) fn save_now(&mut self, config: Option<&dyn ConfigStore>, ui: &UiState) -> Result<(), String> {
let Some(config) = config else {
return Err("there is no settings folder to save them in".into());
};
if !self.writable {
let path = config.dir().map_or_else(|| FILE.to_string(), |dir| dir.join(FILE).display().to_string());
return Err(format!("{path} couldn't be loaded at startup, so it is left untouched; fix or move it and restart"));
}
self.save(Some(config), ui).map(|_| ())
}
}
#[cfg(test)]
@@ -283,10 +296,31 @@ mod tests {
ui.focused = PanelKind::Project;
ui.dock = crate::dock::workspace("Minimal");
assert!(!store.save(Some(&config), &ui).unwrap());
assert!(store.save_now(Some(&config), &ui).unwrap_err().contains("couldn't be loaded"));
assert_eq!(config.read(FILE).as_deref(), Some(text.as_str()));
}
}
#[test]
fn explicit_save_without_a_settings_folder_is_an_error() {
let mut ui = UiState::default();
let mut store = WorkspaceStore::load(None, &mut ui);
ui.focused = PanelKind::Project;
assert!(!store.save(None, &ui).unwrap());
assert!(store.save_now(None, &ui).is_err());
}
#[test]
fn explicit_save_with_nothing_new_succeeds() {
let config = MemoryConfig::default();
let mut ui = UiState::default();
let mut store = WorkspaceStore::load(Some(&config), &mut ui);
ui.focused = PanelKind::Project;
store.save_now(Some(&config), &ui).unwrap();
store.save_now(Some(&config), &ui).unwrap();
assert!(config.read(FILE).is_some());
}
#[test]
fn invalid_tabs_split_and_deep_tree_are_rejected() {
let mut snapshot = Snapshot::capture(&UiState::default());