EffectCraft: An open-source motion graphics and compositing tool written by scratch in pure Rust
Fix egui 0.37 text ownership and simplify UI lifecycle handlers
skylar taylor-barrick57220d4d6ad2
diff --git a/crates/engine/src/commands/app_more.rs b/crates/engine/src/commands/app_more.rsindex 1bb94952f..0758b9bc7 100644--- a/crates/engine/src/commands/app_more.rs+++ b/crates/engine/src/commands/app_more.rs@@ -38,7 +38,7 @@ pub(crate) fn grouped<T>(s: &mut Session, label: &str, f: impl FnOnce(&mut SessiErr(e) => {if changed {s.project = before;- s.sanitize_state();+ s.sanitize_after_replacement();s.bump();}Err(e)diff --git a/crates/engine/src/commands/batch.rs b/crates/engine/src/commands/batch.rsindex 4bfd2b2b1..fd6f4f49b 100644--- a/crates/engine/src/commands/batch.rs+++ b/crates/engine/src/commands/batch.rs@@ -78,7 +78,7 @@ fn batch(s: &mut Session, p: &Value) -> Result<Value> {s.project = project;s.history = history;s.state = state;- s.sanitize_state();+ s.sanitize_after_replacement();s.bump();"; the batch was rolled back (nothing changed)".to_string()} else {diff --git a/crates/engine/src/commands/edit.rs b/crates/engine/src/commands/edit.rsindex cde77b1e8..05015e6b4 100644--- a/crates/engine/src/commands/edit.rs+++ b/crates/engine/src/commands/edit.rs@@ -227,8 +227,8 @@ fn delete(s: &mut Session, p: &Value) -> Result<Value> {st.selected_keys.clear();Ok(())})?;- // A removed layer may have been the edited one (possibly with the same id restored by an- // immediate undo): end or re-epoch that session before another command can reuse it.+ // A removed layer may have been the edited one: end that session before another command+ // can reuse it. Undo restores a layer as a new editor, never by reviving the old IME session.s.sanitize_state();Ok(json!(ids.len()))}diff --git a/crates/engine/src/commands/layer.rs b/crates/engine/src/commands/layer.rsindex e220e7d93..8ff43f32e 100644--- a/crates/engine/src/commands/layer.rs+++ b/crates/engine/src/commands/layer.rs@@ -1481,7 +1481,9 @@ fn set_text(s: &mut Session, p: &Value) -> Result<Value> {}s.bump();}- let replacing_text = p.get("text").is_some() && s.state.text_edit.as_ref().is_some_and(|e| e.layer == lid);+ // A changed Source Text is handled by `Session::edit`: its boundary clamps the selection and+ // starts a new IME epoch only when the CTI document actually changed, so an identical text+ // write stays a no-op and keeps the editor's owner.s.edit("Edit Text", merge_p(p), |proj, _| {let l = layer_mut(proj, cid, lid)?;let lt = l.layer_time(t);@@ -1502,9 +1504,6 @@ fn set_text(s: &mut Session, p: &Value) -> Result<Value> {}Ok(())})?;- if replacing_text {- super::text_edit::invalidate_session(s);- }Ok(Value::Null)}diff --git a/crates/engine/src/commands/text_edit.rs b/crates/engine/src/commands/text_edit.rsindex 4f47d3bad..5550ee68c 100644--- a/crates/engine/src/commands/text_edit.rs+++ b/crates/engine/src/commands/text_edit.rs@@ -248,7 +248,7 @@ pub(crate) fn edit_doc() -> Result<()> {let cid = s.active_comp_id().ok_or(EngineError::NoComp)?;let t = s.time();- s.edit(label, merge, |proj, st| {+ s.edit_text_doc(label, merge, |proj, st| {let l = layer_mut(proj, cid, lid)?;let lt = l.layer_time(t);let pr = l.props.prop_mut("text/sourceText").ok_or_else(|| bad("text", "not a text layer"))?;@@ -257,13 +257,19 @@ pub(crate) fn edit_doc(KV::Str(t) => TextDoc::plain(&t),_ => TextDoc::default(),};- let before = first_line_name(&doc.text);+ let original = doc.clone();f(&mut doc, st)?;- let after = first_line_name(&doc.text);- pr.set_value_at(lt, KV::Text(Box::new(doc)));- // Text layers are named after their text until renamed.- if before != after && !after.is_empty() && (l.name == before || l.name.starts_with("Text") || l.name.is_empty()) {- l.name = after;+ // A no-op (an IME deletion with nothing around the selection, a set to the value it+ // already has) must not turn a static Str into Text or add a keyframe at the CTI: that+ // would change the project and record an empty undo step.+ if doc != original {+ let before = first_line_name(&original.text);+ let after = first_line_name(&doc.text);+ pr.set_value_at(lt, KV::Text(Box::new(doc)));+ // Text layers are named after their text until renamed.+ if before != after && !after.is_empty() && (l.name == before || l.name.starts_with("Text") || l.name.is_empty()) {+ l.name = after;+ }}Ok(())})@@ -674,12 +680,51 @@ pub(crate) fn invalidate_session(s: &mut Session) {}}-/// Keep the session valid after undo / external edits: clamp the selection, drop it when the+/// Clamp the editor selection and give it a fresh IME epoch after its document was replaced.+///+/// This is the one replacement path for document changes outside `edit_doc`:+/// [`crate::Session::edit`] calls [`after_external_change`] after every edit, [`crate::Session::set_time`]+/// and [`crate::Session::sync_related_times`] call it after moving the CTI, and+/// [`crate::Session::sanitize_after_replacement`] calls it when it restores a whole document.+/// Clamping first means a shortened document leaves the caret in range, so the next Backspace+/// deletes instead of being swallowed by an out-of-range selection.+pub(crate) fn document_replaced(s: &mut Session) {+ sanitize(s);+ invalidate_session(s);+}++/// The document the active on-canvas editor is showing, with its layer.+///+/// [`crate::Session::edit`] and the time setters snapshot this before a change and compare it+/// afterwards: only a change that actually replaces this document starts a new epoch, while a+/// write to another property, layer or composition, a move to a time showing the same text, or a+/// write that leaves the text as it was, keeps the editor's owner. With no editor open this is a+/// single `Option` check, with no document copy.+pub(crate) fn active_editor_doc(s: &Session) -> Option<(LayerId, TextDoc)> {+ let e = s.state.text_edit.as_ref()?;+ Some((e.layer, layer_doc(s, e.layer)?))+}++/// Finish a change outside the editor: if it replaced the document the editor owned, clamp the+/// selection and start a new IME epoch.+pub(crate) fn after_external_change(s: &mut Session, before: Option<&(LayerId, TextDoc)>) {+ let Some((lid, before)) = before else { return };+ // The change ended the editor or moved it to another layer: that path owns its own session.+ if !s.state.text_edit.as_ref().is_some_and(|e| e.layer == *lid) {+ return;+ }+ if layer_doc(s, *lid).as_ref() != Some(before) {+ document_replaced(s);+ }+}++/// Keep the session valid after routine state cleanup: clamp the selection, drop it when the/// layer is gone or not in the active comp.///-/// Any call to this function follows an undo/redo, a project-level deletion or a rolled-back-/// batch. The document may have been replaced even when the same layer id exists again, so the-/// composition epoch is advanced before the session is kept.+/// This is also called after project-item cleanup that leaves the edited document alone, so it+/// must not advance [`TextEdit::session`]: that would make the viewer treat unrelated cleanup as+/// a document replacement and interrupt the active IME composition. Replacement paths call+/// [`document_replaced`] instead.pub(crate) fn sanitize(s: &mut Session) {let Some(e) = s.state.text_edit.clone() else { return };match layer_doc(s, e.layer) {@@ -688,7 +733,6 @@ pub(crate) fn sanitize(s: &mut Session) {if let Some(ed) = s.state.text_edit.as_mut() {ed.anchor = ed.anchor.min(n);ed.caret = ed.caret.min(n);- ed.session = next_edit_session();}}None => s.state.text_edit = None,diff --git a/crates/engine/src/history.rs b/crates/engine/src/history.rsindex 8b178a05c..2c49e182b 100644--- a/crates/engine/src/history.rs+++ b/crates/engine/src/history.rs@@ -347,7 +347,7 @@ impl Session {self.history.redo = redo;self.history.branches = branches;self.history.merge_key = None;- self.sanitize_state();+ self.sanitize_after_replacement();self.bump();true}diff --git a/crates/engine/src/lib.rs b/crates/engine/src/lib.rsindex d6be19fd8..65ac57bfa 100644--- a/crates/engine/src/lib.rs+++ b/crates/engine/src/lib.rs@@ -686,7 +686,25 @@ impl Session {}/// Apply an undoable edit. `merge`: consecutive edits with the same key fold into one step.+ ///+ /// Every edit is an external boundary for the on-canvas text editor: if it replaced the+ /// document the editor is showing, the selection is clamped and the editor gets a fresh IME+ /// epoch. The editor's own typing goes through `edit_text_doc`, which owns that+ /// document and must not treat its own keystrokes as a replacement.pub fn edit<T>(&mut self, label: &str, merge: Option<&str>, f: impl FnOnce(&mut Project, &mut EditorState) -> Result<T>) -> Result<T> {+ let edited = commands::text_edit::active_editor_doc(self);+ let out = self.edit_inner(label, merge, f)?;+ commands::text_edit::after_external_change(self, edited.as_ref());+ Ok(out)+ }++ /// [`Session::edit`] for the on-canvas editor's own text changes. Its document is the one+ /// the editor owns, so a change here is typing, not a replacement behind the IME's back.+ pub(crate) fn edit_text_doc<T>(&mut self, label: &str, merge: Option<&str>, f: impl FnOnce(&mut Project, &mut EditorState) -> Result<T>) -> Result<T> {+ self.edit_inner(label, merge, f)+ }++ fn edit_inner<T>(&mut self, label: &str, merge: Option<&str>, f: impl FnOnce(&mut Project, &mut EditorState) -> Result<T>) -> Result<T> {let before = self.project.clone();let mut p = (*self.project).clone();let mut st = self.state.clone();@@ -809,7 +827,7 @@ impl Session {self.keep_view_settings(&cur);self.history.redo.push((label, cur));self.history.merge_key = None;- self.sanitize_state();+ self.sanitize_after_replacement();self.bump();true}@@ -820,11 +838,19 @@ impl Session {self.keep_view_settings(&cur);self.history.undo.push((label, cur));self.history.merge_key = None;- self.sanitize_state();+ self.sanitize_after_replacement();self.bump();true}+ /// Keep selections valid after the project was replaced by another state (undo, redo, a+ /// history jump, a rollback): the same layer id can name a different document, so the+ /// on-canvas editor gets a fresh IME epoch.+ pub fn sanitize_after_replacement(&mut self) {+ self.sanitize_state();+ commands::text_edit::document_replaced(self);+ }+/// Drop selections that no longer exist. Each comp keeps its own selection (#1001); a layer id/// counts only inside the comp that owns it, so the active comp never sees another comp's layers.pub fn sanitize_state(&mut self) {@@ -900,13 +926,22 @@ impl Session {self.state.active_comp.and_then(|c| self.state.times.get(&c).copied()).unwrap_or(Tick::ZERO)}+ /// Move the active comp's current time.+ ///+ /// Time is an external boundary for the on-canvas text editor too: an animated Source Text+ /// can show a different document at another time. The document is snapshotted only while an+ /// editor is open, so playback with no text editor does no text copying.pub fn set_time(&mut self, t: Tick) {if let Some(c) = self.state.active_comp {let t = self.project.comp(c).map_or(t, |comp| comp_frame_at(comp, t));+ let edited = commands::text_edit::active_editor_doc(self);self.state.times.insert(c, t);if self.prefs.general.sync_time_related_items {- self.sync_related_times(c, t);+ // Direct call: this `set_time` owns the editor snapshot for both time changes,+ // so a synced move does not take a second snapshot or start a second epoch.+ self.sync_related_times_inner(c, t);}+ commands::text_edit::after_external_change(self, edited.as_ref());}}@@ -914,6 +949,14 @@ impl Session {/// `comp` moves the current time of the comps nested in it (through their precomp layers'/// timing) and of the comps it is nested in, so every related viewer shows the same moment.pub fn sync_related_times(&mut self, comp: ItemId, t: Tick) {+ // Moving a related comp can move the active one, so the editor's document is checked+ // here too; the snapshot is skipped entirely when no text editor is open.+ let edited = commands::text_edit::active_editor_doc(self);+ self.sync_related_times_inner(comp, t);+ commands::text_edit::after_external_change(self, edited.as_ref());+ }++ fn sync_related_times_inner(&mut self, comp: ItemId, t: Tick) {let mut seen = std::collections::BTreeSet::from([comp]);let mut todo = vec![(comp, t)];let mut found = vec![];diff --git a/crates/engine/src/tests_text_edit.rs b/crates/engine/src/tests_text_edit.rsindex a664ca369..1b2fcc955 100644--- a/crates/engine/src/tests_text_edit.rs+++ b/crates/engine/src/tests_text_edit.rs@@ -235,6 +235,260 @@ fn ime_session_epoch_tracks_replaced_text_and_layer_restoration() {assert_ne!(s.state.text_edit.as_ref().unwrap().session, first);}+#[test]+fn no_op_ime_deletion_keeps_a_static_plain_text_value_and_records_no_step() {+ use effectcraft_keyframe::Value as KV;+ let (mut s, t) = session("abc");+ // Legacy/imported text can still be stored as a plain Str rather than a TextDoc.+ let cid = s.active_comp_id().unwrap();+ let pr = std::sync::Arc::make_mut(&mut s.project)+ .comp_mut(cid)+ .unwrap()+ .layer_mut(effectcraft_project::LayerId(t))+ .unwrap()+ .props+ .prop_mut("text/sourceText")+ .unwrap();+ pr.value = KV::Str("abc".into());+ s.execute("text.edit", json!({"layer": t, "caret": 0})).unwrap();+ let steps = s.history.undo.len();++ // A non-zero before count at the document start deletes nothing.+ s.execute("text.deleteSurrounding", json!({"before": 1, "after": 0})).unwrap();++ let pr = s.active_comp().unwrap().layer(effectcraft_project::LayerId(t)).unwrap().props.prop("text/sourceText").unwrap();+ assert!(matches!(&pr.value, KV::Str(_)), "a no-op keeps the static Str variant");+ assert!(pr.keys.is_empty());+ assert_eq!(s.history.undo.len(), steps, "a no-op adds no undo step");+}++#[test]+fn no_op_ime_deletion_does_not_add_a_keyframe_to_animated_source_text() {+ let (mut s, t) = session("abc");+ s.execute("text.edit", json!({"layer": t, "caret": 0})).unwrap();+ s.execute("prop.toggleAnimation", json!({"layer": t, "path": "text/sourceText"})).unwrap();+ let lid = effectcraft_project::LayerId(t);+ let keys = |s: &Session| s.active_comp().unwrap().layer(lid).unwrap().props.prop("text/sourceText").unwrap().keys.len();+ assert_eq!(keys(&s), 1, "one key from the stopwatch");+ s.execute("time.set", json!({"time": 1.0})).unwrap();+ let steps = s.history.undo.len();++ // A non-zero before count at the document start deletes nothing, at a time with no key.+ s.execute("text.deleteSurrounding", json!({"before": 1, "after": 0})).unwrap();++ assert_eq!(keys(&s), 1, "a no-op does not key Source Text at the CTI");+ assert_eq!(s.history.undo.len(), steps, "a no-op adds no undo step");+}++#[test]+fn property_writes_replace_the_edited_document_only_when_the_text_changes() {+ let (mut s, t) = session("abc");+ let other = s.execute("layer.newText", json!({"text": "other"})).unwrap()["layer"].as_u64().unwrap();+ s.execute("text.edit", json!({"layer": t, "caret": 1})).unwrap();+ let owner = s.state.text_edit.as_ref().unwrap().session;+ let steps = s.history.undo.len();++ // Writing the text the document already has is a no-op: same document, same owner, no step.+ s.execute("prop.set", json!({"layer": t, "path": "text/sourceText", "value": "abc"})).unwrap();+ assert_eq!(s.state.text_edit.as_ref().unwrap().session, owner, "a no-op write keeps the IME owner");+ assert_eq!(s.history.undo.len(), steps, "a no-op write records no undo step");+ s.execute("layer.setText", json!({"layer": t, "text": "abc"})).unwrap();+ assert_eq!(s.state.text_edit.as_ref().unwrap().session, owner, "a no-op layer.setText keeps the IME owner");+ assert_eq!(s.history.undo.len(), steps, "a no-op layer.setText records no undo step");++ // Another property of the edited layer leaves its document alone.+ s.execute("prop.set", json!({"layer": t, "path": "transform/opacity", "value": 50})).unwrap();+ assert_eq!(s.state.text_edit.as_ref().unwrap().session, owner, "an unrelated property keeps the IME owner");++ // Source Text of another layer is not the editor's document either.+ s.execute("prop.set", json!({"layer": other, "path": "text/sourceText", "value": "changed"})).unwrap();+ assert_eq!(s.state.text_edit.as_ref().unwrap().session, owner, "another layer's text keeps the IME owner");++ // Replacing the edited layer's Source Text is a document replacement: a new IME epoch.+ s.execute("prop.set", json!({"layer": t, "path": "text/sourceText", "value": "replaced"})).unwrap();+ assert_eq!(doc(&s, t).text, "replaced");+ assert_ne!(s.state.text_edit.as_ref().unwrap().session, owner, "replacing the document changes the IME owner");+}++#[test]+fn a_replacing_property_write_clamps_the_selection_so_the_next_backspace_deletes() {+ let (mut s, t) = session("abcdef");+ s.execute("text.edit", json!({"layer": t, "caret": 6})).unwrap();+ let owner = s.state.text_edit.as_ref().unwrap().session;++ s.execute("prop.set", json!({"layer": t, "path": "text/sourceText", "value": "a"})).unwrap();+ assert_eq!(doc(&s, t).text, "a");+ assert_eq!(sel(&s), (1, 1), "the shortened document leaves the caret in range");+ assert_ne!(s.state.text_edit.as_ref().unwrap().session, owner);++ // Backspace at the clamped caret removes the character instead of no-opping on 5..6.+ s.execute("text.delete", json!({})).unwrap();+ assert_eq!(doc(&s, t).text, "");+ assert_eq!(sel(&s), (0, 0));+}++#[test]+fn keyframe_writes_on_source_text_replace_the_edited_document_only_when_the_cti_value_changes() {+ let (mut s, t) = session("abc");+ s.execute("text.edit", json!({"layer": t, "caret": 1})).unwrap();+ // Keying the text the CTI already shows leaves the document equal.+ s.execute("prop.toggleAnimation", json!({"layer": t, "path": "text/sourceText"})).unwrap();+ let owner = s.state.text_edit.as_ref().unwrap().session;++ s.execute("keys.set", json!({"layer": t, "path": "text/sourceText", "time": 0.0, "value": "abc"})).unwrap();+ assert_eq!(s.state.text_edit.as_ref().unwrap().session, owner, "a no-op key write keeps the IME owner");++ // `keys.set` changes the key at the CTI: a document replacement.+ s.execute("keys.set", json!({"layer": t, "path": "text/sourceText", "time": 0.0, "value": "replaced"})).unwrap();+ assert_eq!(doc(&s, t).text, "replaced");+ assert_ne!(s.state.text_edit.as_ref().unwrap().session, owner, "a changed keyframe changes the IME owner");+}++#[test]+fn deleting_the_cti_keyframe_first_replaces_the_document_then_the_last_key_is_a_noop() {+ let (mut s, t) = session("abc");+ s.execute("text.edit", json!({"layer": t, "caret": 1})).unwrap();+ s.execute("prop.toggleAnimation", json!({"layer": t, "path": "text/sourceText"})).unwrap();+ s.execute("prop.addKey", json!({"layer": t, "path": "text/sourceText", "time": 1.0, "value": "later"})).unwrap();+ let owner = s.state.text_edit.as_ref().unwrap().session;+ let lid = effectcraft_project::LayerId(t);+ let pr = s.active_comp().unwrap().layer(lid).unwrap().props.prop("text/sourceText").unwrap();+ let uid = pr.uid;+ let times: Vec<_> = pr.keys.iter().map(|k| k.time).collect();+ assert_eq!(times.len(), 2);++ // Deleting the key at the CTI (0s) while the 1s key remains shows the later text: replacing+ // the document starts a new epoch.+ s.state.selected_keys = vec![crate::KeyRef { layer: lid, prop: uid, time: times[0] }];+ assert_eq!(s.execute("keys.delete", json!({})).unwrap(), json!(1));+ assert_eq!(doc(&s, t).text, "later");+ let replaced = s.state.text_edit.as_ref().unwrap().session;+ assert_ne!(replaced, owner, "deleting the CTI key changed the document");++ // Removing the last key turns its value into the static CTI value. That value is what the+ // CTI already showed, so the document does not change and the new owner stays.+ s.state.selected_keys = vec![crate::KeyRef { layer: lid, prop: uid, time: times[1] }];+ assert_eq!(s.execute("keys.delete", json!({})).unwrap(), json!(1));+ assert_eq!(doc(&s, t).text, "later");+ assert_eq!(s.state.text_edit.as_ref().unwrap().session, replaced, "removing the last key is a document no-op");+}++#[test]+fn moving_the_cti_to_a_shorter_animated_key_clamps_and_starts_a_new_epoch() {+ let (mut s, t) = session("abcdef");+ s.execute("prop.toggleAnimation", json!({"layer": t, "path": "text/sourceText"})).unwrap();+ s.execute("prop.addKey", json!({"layer": t, "path": "text/sourceText", "time": 1.0, "value": "a"})).unwrap();+ s.execute("text.edit", json!({"layer": t, "caret": 6})).unwrap();+ let owner = s.state.text_edit.as_ref().unwrap().session;++ // The same time still shows the same document: no new epoch.+ s.execute("time.set", json!({"time": 0.0})).unwrap();+ assert_eq!(doc(&s, t).text, "abcdef");+ assert_eq!(s.state.text_edit.as_ref().unwrap().session, owner, "an unchanged CTI document keeps the IME owner");++ // The key at 1s holds shorter text: the document changes, the selection is clamped and the+ // editor gets a new epoch.+ s.execute("time.set", json!({"time": 1.0})).unwrap();+ assert_eq!(doc(&s, t).text, "a");+ assert_eq!(sel(&s), (1, 1), "the shortened document leaves the caret in range");+ let replaced = s.state.text_edit.as_ref().unwrap().session;+ assert_ne!(replaced, owner, "a changed CTI document starts a new IME epoch");++ // Backspace at the clamped caret removes the character instead of no-opping on 5..6.+ s.execute("text.delete", json!({})).unwrap();+ assert_eq!(doc(&s, t).text, "");+ assert_eq!(sel(&s), (0, 0));+ assert_eq!(s.state.text_edit.as_ref().unwrap().session, replaced, "typing after the replacement keeps its owner");++ // Staying at the same time after the edit is a no-op for the document too.+ s.execute("time.set", json!({"time": 1.0})).unwrap();+ assert_eq!(doc(&s, t).text, "");+ assert_eq!(s.state.text_edit.as_ref().unwrap().session, replaced, "a no-op time move keeps the IME owner");+}++#[test]+fn sync_related_times_replaces_the_edited_document_when_it_moves_the_active_comp() {+ let (mut s, t) = session("abcdef");+ s.execute("prop.toggleAnimation", json!({"layer": t, "path": "text/sourceText"})).unwrap();+ s.execute("prop.addKey", json!({"layer": t, "path": "text/sourceText", "time": 1.0, "value": "a"})).unwrap();+ let outer = s.active_comp_id().unwrap();+ let r = s.execute("layer.precompose", json!({"layers": [t], "name": "Inner", "open": true})).unwrap();+ let inner = effectcraft_project::ItemId(r["comp"].as_u64().unwrap());+ assert_eq!(s.active_comp_id(), Some(inner));++ s.execute("text.edit", json!({"layer": t, "caret": 6})).unwrap();+ let owner = s.state.text_edit.as_ref().unwrap().session;+ s.sync_related_times(outer, effectcraft_time::Tick::from_seconds_f64(1.0));++ assert_eq!(doc(&s, t).text, "a");+ assert_eq!(sel(&s), (1, 1), "the related time move clamps the selection");+ assert_ne!(s.state.text_edit.as_ref().unwrap().session, owner, "the related time move changed the document");+}++#[test]+fn applying_a_preset_that_writes_source_text_replaces_the_edited_document() {+ let (mut s, t) = session("abc");+ s.execute("text.edit", json!({"layer": t, "caret": 1})).unwrap();+ let owner = s.state.text_edit.as_ref().unwrap().session;+ let preset = json!({+ "effectcraftAnimationPreset": 1,+ "properties": [{"path": "text/sourceText", "value": {"t": "Text", "v": {"text": "replaced"}}, "keys": []}],+ });+ s.execute("anim.applyPreset", json!({"preset": preset, "layers": [t]})).unwrap();+ assert_eq!(doc(&s, t).text, "replaced");+ assert_ne!(s.state.text_edit.as_ref().unwrap().session, owner, "a preset that replaces source text changes the IME owner");+}++#[test]+fn a_failed_property_write_keeps_the_edited_document_and_its_owner() {+ let (mut s, t) = session("abc");+ s.execute("text.edit", json!({"layer": t, "caret": 1})).unwrap();+ let owner = s.state.text_edit.as_ref().unwrap().session;+ let steps = s.history.undo.len();++ // null cannot coerce to a Text value: the edit fails before anything is committed.+ assert!(s.execute("prop.set", json!({"layer": t, "path": "text/sourceText", "value": null})).is_err());+ assert_eq!(doc(&s, t).text, "abc");+ assert_eq!(s.state.text_edit.as_ref().unwrap().session, owner, "a failed write keeps the IME owner");+ assert_eq!(s.history.undo.len(), steps, "a failed write records no undo step");+}++#[test]+fn property_writes_off_the_cti_keep_the_owner_until_they_change_the_cti_document() {+ let (mut s, t) = session("abc");+ s.execute("text.edit", json!({"layer": t, "caret": 1})).unwrap();+ s.execute("prop.toggleAnimation", json!({"layer": t, "path": "text/sourceText"})).unwrap();+ s.execute("prop.addKey", json!({"layer": t, "path": "text/sourceText", "time": 1.0, "value": "later"})).unwrap();+ let owner = s.state.text_edit.as_ref().unwrap().session;++ // Writing a key at 1s leaves the CTI (0s) value as it was.+ s.execute("prop.set", json!({"layer": t, "path": "text/sourceText", "time": 1.0, "value": "changed"})).unwrap();+ assert_eq!(doc(&s, t).text, "abc");+ assert_eq!(s.state.text_edit.as_ref().unwrap().session, owner, "an off-CTI write keeps the IME owner");++ // Writing the CTI value itself replaces the document.+ s.execute("prop.set", json!({"layer": t, "path": "text/sourceText", "value": "cti"})).unwrap();+ assert_eq!(doc(&s, t).text, "cti");+ assert_ne!(s.state.text_edit.as_ref().unwrap().session, owner, "a CTI write changes the IME owner");+}++#[test]+fn unrelated_project_cleanup_keeps_the_ime_owner_but_replacement_changes_it() {+ let (mut s, t) = session("abc");+ s.execute("text.edit", json!({"layer": t, "caret": 1})).unwrap();+ let owner = s.state.text_edit.as_ref().unwrap().session;++ // file.removeUnusedFootage calls sanitize_state while the edited document is untouched.+ s.execute("file.importSolid", json!({"name": "Unused"})).unwrap();+ let removed = s.execute("file.removeUnusedFootage", json!({})).unwrap();+ assert_eq!(removed["removed"], json!(1), "the unused solid was actually removed");+ assert_eq!(s.state.text_edit.as_ref().unwrap().session, owner, "unrelated cleanup keeps the IME owner");++ // Replacing the Source Text is a document replacement: a new owner.+ s.execute("layer.setText", json!({"layer": t, "text": "replaced"})).unwrap();+ assert_ne!(s.state.text_edit.as_ref().unwrap().session, owner, "replacing the document changes the owner");+}+#[test]fn ime_session_does_not_cross_a_composition_switch() {let (mut s, t) = session("abc");diff --git a/crates/ui-egui/src/control.rs b/crates/ui-egui/src/control.rsindex fa31886cc..a481d96ac 100644--- a/crates/ui-egui/src/control.rs+++ b/crates/ui-egui/src/control.rs@@ -172,351 +172,427 @@ fn point(app: &EffectcraftApp, p: &Value) -> Result<egui::Pos2, String> {pub fn handle(app: &mut EffectcraftApp, ctx: &egui::Context, req: &ControlRequest, now: f64) -> Outcome {let p = &req.params;- let s = |k: &str| p.get(k).and_then(Value::as_str);match req.method.as_str() {- "engine.execute" | "ui.menu.invoke" => {- let Some(id) = s("command").or(s("id")) else { return err("missing `command`") };- let params = p.get("params").cloned().filter(|v| !v.is_null()).unwrap_or(json!({}));- // `engine.execute` runs engine commands exactly as headless (never opens a dialog, e.g.- // `comp.new {}` creates a default comp); UI-only ids (`tool.*`, `view.*`, `window.*`,- // `playback.*` …) and `ui.menu.invoke` go through the menu dispatcher like a click.- let r = if req.method == "engine.execute" && effectcraft_engine::find_command(id).is_some() {- execute_engine(app, id, params)- } else {- crate::menus::invoke(app, ctx, id, params).map(ok)- };- match r {- Ok(o) => o,- Err(e) => {- app.ui.status = e.clone();- err(e)- }- }- }- "engine.commands" => match app.session.execute("command.list", json!({"filter": p.get("filter"), "enabledOnly": p.get("enabledOnly")})) {- Ok(v) => ok(v),- Err(e) => err(e),- },+ "engine.execute" | "ui.menu.invoke" => execute_command(app, ctx, &req.method, p),+ "engine.commands" => engine_commands(app, p),"ui.menu.list" => ok(serde_json::to_value(crate::menus::menu_items(app)).unwrap_or_default()),"ui.inspect" => ok(inspect(app, ctx)),- "ui.elements" => {- let prefix = s("prefix").unwrap_or("");- ok(serde_json::to_value(app.auto.query(prefix)).unwrap_or_default())- }- "ui.set" => {- if let Some(t) = s("tool") {- match Tool::from_name(t) {- Some(t) => app.ui.tool = t,- None => return err(format!("unknown tool `{t}`")),- }- }- if let Some(w) = s("workspace") {- match crate::dock::WORKSPACES.iter().find(|x| x.eq_ignore_ascii_case(w)) {- Some(w) => app.set_workspace(w),- None => return err(format!("unknown workspace `{w}`")),- }- }- if let Some(th) = s("theme") {- match crate::theme::ThemeKind::from_name(th) {- Some(k) => app.set_theme(ctx, k),- None => return err("unknown theme (dark, darker, light)"),- }- }- if let Some(f) = s("focused").and_then(PanelKind::from_name) {- app.ui.focused = f;- }- if let Some(v) = p.get("viewer") {- if let Some(z) = v.get("zoom") {- app.ui.viewer.zoom = z.as_f64().map(|z| z as f32);- }- // The active comp's Resolution (`view.res.*`), saved with the project.- if let Some(r) = v.get("res").and_then(Value::as_str) {- let Some(res) = Resolution::ALL.into_iter().find(|x| x.label().eq_ignore_ascii_case(r)) else {- return err(format!("unknown resolution `{r}` (auto, full, half, third, quarter)"));- };- if let Some(cid) = app.session.active_comp_id()- && let Err(e) = app.session.set_resolution(cid, res)- {- return err(e);- }- }- if let Some(Value::Array(a)) = v.get("pan") {- app.ui.viewer.pan = [a.first().and_then(Value::as_f64).unwrap_or(0.0) as f32, a.get(1).and_then(Value::as_f64).unwrap_or(0.0) as f32];- }- for (k, slot) in [- ("grid", &mut app.ui.viewer.grid),- ("rulers", &mut app.ui.viewer.rulers),- ("safeMargins", &mut app.ui.viewer.safe_margins),- ("transparencyGrid", &mut app.ui.viewer.transparency_grid),- ] {- if let Some(b) = v.get(k).and_then(Value::as_bool) {- *slot = b;- }- }- }- if let Some(tl) = p.get("timeline") {- if let Some(v) = tl.get("pps") {- app.ui.timeline.pps = v.as_f64();- }- if let Some(v) = tl.get("start").and_then(Value::as_f64) {- app.ui.timeline.start = v;- }- if let Some(v) = tl.get("graphEditor").and_then(Value::as_bool) {- app.ui.timeline.graph_editor = v;- }- if let Some(v) = tl.get("showModes").and_then(Value::as_bool) {- app.ui.timeline.show_modes = v;- }- if let Some(Value::Array(a)) = tl.get("openLayers") {- app.ui.timeline.open_layers = a.iter().filter_map(Value::as_u64).collect();- }- if let Some(Value::Array(a)) = tl.get("openGroups") {- app.ui.timeline.open_groups = a.iter().filter_map(Value::as_u64).collect();- }- }- if let Some(b) = p.get("menuBar").and_then(Value::as_bool) {- app.ui.show_menu_bar = b;- }- // Project panel optional columns, e.g. ["type", "duration", "path", "comment"].- if let Some(Value::Array(a)) = p.get("projectColumns") {- let mut cols = vec![];- for k in a.iter().filter_map(Value::as_str) {- crate::panels::project::set_column(&mut cols, k, true);- }- app.ui.project_columns = cols;- }- // Wiggler / Smoother / Motion Sketch settings (fields of `AnimToolsState`).- if let Some(Value::Object(m)) = p.get("animTools") {- let mut cur = serde_json::to_value(&app.ui.anim_tools).unwrap_or_default();- for (k, v) in m {- cur[k] = v.clone();- }- match serde_json::from_value(cur) {- Ok(v) => app.ui.anim_tools = v,- Err(e) => return err(format!("animTools: {e}")),- }- }- if let Some(b) = p.get("home").and_then(Value::as_bool) {- app.ui.start_screen = b;- }- // Home screen tab: "home", "templates" or "learn".- if let Some(tab) = p.get("homeTab").and_then(Value::as_str) {- app.ui.home_learn = tab == "learn";- app.ui.home_templates = tab == "templates";- }- ok(Value::Null)- }- "ui.panel.show" | "ui.panel.close" => {- let Some(panel) = s("panel").and_then(PanelKind::from_name) else { return err("unknown `panel`") };- if req.method == "ui.panel.show" {- app.show_panel(panel);- } else {- app.ui.dock.close(panel);- }- ok(Value::Null)- }- "ui.click" | "ui.move" => {- let pos = match point(app, p) {- Ok(p) => p,- Err(e) if e.starts_with(RETRY) => return Outcome::Retry(e.trim_start_matches(RETRY).to_string()),- Err(e) => return err(e),- };- let m = modifiers(p);- // Validate the complete gesture before mutating the queue; the loopback control- // channel is externally reachable and must not amplify tiny requests into huge work.- const MAX_PENDING_EVENTS: usize = 4096;- const MAX_CLICK_COUNT: u64 = 512;- let pending = app.synthetic.len();- let gesture_events = if req.method == "ui.click" {- let count = p.get("count").and_then(Value::as_u64).unwrap_or(1);- if count == 0 || count > MAX_CLICK_COUNT {- return err(format!("`count` must be between 1 and {MAX_CLICK_COUNT}"));- }- let Some(events) = usize::try_from(count).ok().and_then(|n| n.checked_mul(2)).and_then(|n| n.checked_add(3)) else {- return err("click event count overflow");- };- events- } else {- 3- };- if pending.checked_add(gesture_events).is_none_or(|n| n > MAX_PENDING_EVENTS) {- return err("synthetic input queue is full");- }- // egui keeps the modifiers from `ModifiersChanged` (not the ones on a pointer event)- // and the synthetic events arrive one per frame: hold them for the whole gesture.- app.synthetic.push(egui::Event::ModifiersChanged(m));- app.synthetic.push(egui::Event::PointerMoved(pos));- if req.method == "ui.click" {- let button = match s("button") {- Some("right") => egui::PointerButton::Secondary,- Some("middle") => egui::PointerButton::Middle,- _ => egui::PointerButton::Primary,- };- let count = p.get("count").and_then(Value::as_u64).unwrap_or(1);- for _ in 0..count {- app.synthetic.push(egui::Event::PointerButton { pos, button, pressed: true, modifiers: m });- app.synthetic.push(egui::Event::PointerButton { pos, button, pressed: false, modifiers: m });- }- }- app.synthetic.push(egui::Event::ModifiersChanged(egui::Modifiers::NONE));- Outcome::AfterInput- }- "ui.drag" => {- let (Some(from), Some(to)) = (p.get("from"), p.get("to")) else { return err("need `from` and `to`") };- let (a, b) = match (point(app, from), point(app, to)) {- (Ok(a), Ok(b)) => (a, b),- (Err(e), _) | (_, Err(e)) if e.starts_with(RETRY) => return Outcome::Retry(e.trim_start_matches(RETRY).to_string()),- (Err(e), _) | (_, Err(e)) => return err(e),- };- let steps = p.get("steps").and_then(Value::as_u64).unwrap_or(12).max(2);- const MAX_PENDING_EVENTS: usize = 4096;- const MAX_DRAG_STEPS: u64 = 2048;- if steps > MAX_DRAG_STEPS {- return err(format!("`steps` must be at most {MAX_DRAG_STEPS}"));- }- let Some(gesture_events) = usize::try_from(steps).ok().and_then(|n| n.checked_add(5)) else { return err("drag event count overflow") };- if app.synthetic.len().checked_add(gesture_events).is_none_or(|n| n > MAX_PENDING_EVENTS) {- return err("synthetic input queue is full");- }- let m = modifiers(p);- app.synthetic.push(egui::Event::ModifiersChanged(m));- app.synthetic.push(egui::Event::PointerMoved(a));- app.synthetic.push(egui::Event::PointerButton { pos: a, button: egui::PointerButton::Primary, pressed: true, modifiers: m });- for i in 1..=steps {- let f = i as f32 / steps as f32;- app.synthetic.push(egui::Event::PointerMoved(a + (b - a) * f));- }- app.synthetic.push(egui::Event::PointerButton { pos: b, button: egui::PointerButton::Primary, pressed: false, modifiers: m });- app.synthetic.push(egui::Event::ModifiersChanged(egui::Modifiers::NONE));- Outcome::AfterInput- }- "ui.scroll" => {- let pos = match point(app, p) {- Ok(p) => p,- Err(e) => return err(e),- };- let dx = p.get("dx").and_then(Value::as_f64).unwrap_or(0.0) as f32;- let dy = p.get("dy").and_then(Value::as_f64).unwrap_or(0.0) as f32;- app.synthetic.push(egui::Event::PointerMoved(pos));- app.synthetic.push(egui::Event::MouseWheel {- unit: egui::MouseWheelUnit::Point,- delta: egui::vec2(dx, dy),- modifiers: modifiers(p),- phase: egui::TouchPhase::Move,- source: egui::MouseWheelSource::Wheel,- });- app.synthetic.push(egui::Event::PointerButton {- pos: egui::pos2(-100.0, -100.0),- button: egui::PointerButton::Extra1,- pressed: false,- modifiers: Default::default(),- });- Outcome::AfterInput- }- "ui.key" => {- let Some(name) = s("key") else { return err("missing `key`") };- let Some((mut m, key)) = crate::menus::parse_shortcut(name) else { return err(format!("unknown key `{name}`")) };- let extra = modifiers(p);- m |= extra;- let physical_key = name.starts_with("Physical+").then_some(key);- app.synthetic.push(egui::Event::Key { key, physical_key, pressed: true, repeat: false, modifiers: m });- app.synthetic.push(egui::Event::Key { key, physical_key, pressed: false, repeat: false, modifiers: m });- Outcome::AfterInput- }- "ui.type" => {- let Some(text) = s("text") else { return err("missing `text`") };- app.synthetic.push(egui::Event::Text(text.to_string()));- app.synthetic.push(egui::Event::Key { key: egui::Key::F35, physical_key: None, pressed: false, repeat: false, modifiers: Default::default() });- Outcome::AfterInput- }- "ui.viewer.locate" => {- let (Some(x), Some(y)) = (p.get("x").and_then(Value::as_f64), p.get("y").and_then(Value::as_f64)) else {- return err("need `x`, `y` (comp pixels)");- };- match crate::panels::viewer::comp_to_screen(ctx, [x as f32, y as f32]) {- Some(pt) => ok(json!({"x": pt.x, "y": pt.y})),- None => Outcome::Retry("the composition viewer is not visible".into()),- }+ "ui.elements" => elements(app, p),+ "ui.set" => set_ui(app, ctx, p),+ "ui.panel.show" | "ui.panel.close" => panel(app, &req.method, p),+ "ui.click" | "ui.move" => click_or_move(app, &req.method, p),+ "ui.drag" => drag(app, p),+ "ui.scroll" => scroll(app, p),+ "ui.key" => key(app, p),+ "ui.type" => type_text(app, p),+ "ui.viewer.locate" => viewer_locate(ctx, p),+ "ui.viewer.hit" => viewer_hit(app, ctx, p),+ "ui.timeline.locate" => timeline_locate(app, p),+ "ui.playback" => playback(app, p, now),+ "ui.screenshot" => screenshot(app, ctx, p),+ "render.frame" => render_frame(app, p),+ "ui.resize" => resize(ctx, p),+ "ui.focus" => focus(ctx),+ "app.quit" => quit(app, ctx, p),+ m => err(format!("unknown method `{m}`")),+ }+}++fn execute_command(app: &mut EffectcraftApp, ctx: &egui::Context, method: &str, p: &Value) -> Outcome {+ let s = |k: &str| p.get(k).and_then(Value::as_str);+ let Some(id) = s("command").or(s("id")) else { return err("missing `command`") };+ let params = p.get("params").cloned().filter(|v| !v.is_null()).unwrap_or(json!({}));+ // `engine.execute` runs engine commands exactly as headless (never opens a dialog, e.g.+ // `comp.new {}` creates a default comp); UI-only ids (`tool.*`, `view.*`, `window.*`,+ // `playback.*` …) and `ui.menu.invoke` go through the menu dispatcher like a click.+ let r = if method == "engine.execute" && effectcraft_engine::find_command(id).is_some() {+ execute_engine(app, id, params)+ } else {+ crate::menus::invoke(app, ctx, id, params).map(ok)+ };+ match r {+ Ok(o) => o,+ Err(e) => {+ app.ui.status = e.clone();+ err(e)}- "ui.viewer.hit" => {- let pos = match point(app, p) {- Ok(p) => p,- Err(e) => return err(e),- };- match crate::panels::viewer::screen_to_comp(ctx, pos) {- Some(c) => ok(json!({"x": c[0], "y": c[1]})),- None => err("the composition viewer is not visible"),- }+ }+}++fn engine_commands(app: &mut EffectcraftApp, p: &Value) -> Outcome {+ match app.session.execute("command.list", json!({"filter": p.get("filter"), "enabledOnly": p.get("enabledOnly")})) {+ Ok(v) => ok(v),+ Err(e) => err(e),+ }+}++fn elements(app: &EffectcraftApp, p: &Value) -> Outcome {+ let prefix = p.get("prefix").and_then(Value::as_str).unwrap_or("");+ ok(serde_json::to_value(app.auto.query(prefix)).unwrap_or_default())+}++/// Explicit request: activate the app and take keyboard focus.+fn focus(ctx: &egui::Context) -> Outcome {+ ctx.send_viewport_cmd(egui::ViewportCommand::Focus);+ ok(Value::Null)+}++fn set_ui(app: &mut EffectcraftApp, ctx: &egui::Context, p: &Value) -> Outcome {+ let s = |k: &str| p.get(k).and_then(Value::as_str);+ if let Some(t) = s("tool") {+ match Tool::from_name(t) {+ Some(t) => app.ui.tool = t,+ None => return err(format!("unknown tool `{t}`")),}- "ui.timeline.locate" => {- let Some(l) = p.get("layer").and_then(Value::as_u64) else { return err("need `layer` (id)") };- match crate::panels::timeline::locate(app, l, p.get("time").and_then(Value::as_f64)) {- Some((x, y)) => ok(json!({"x": x, "y": y})),- None => Outcome::Retry("layer not visible in the timeline".into()),- }+ }+ if let Some(w) = s("workspace") {+ match crate::dock::WORKSPACES.iter().find(|x| x.eq_ignore_ascii_case(w)) {+ Some(w) => app.set_workspace(w),+ None => return err(format!("unknown workspace `{w}`")),}- "ui.playback" => {- match s("action").unwrap_or("toggle") {- "play" => app.play(now),- "stop" => app.stop(),- "status" => {}- _ => app.toggle_play(now),- }- ok(- json!({"playing": app.playback.playing, "time": app.session.time().seconds(), "audio": app.audio.is_some(), "levelsDb": app.meter.level_db, "peaksDb": app.meter.peak_db}),- )+ }+ if let Some(th) = s("theme") {+ match crate::theme::ThemeKind::from_name(th) {+ Some(k) => app.set_theme(ctx, k),+ None => return err("unknown theme (dark, darker, light)"),}- "ui.screenshot" => {- // The panel/element is an identity, not a rectangle: it is resolved when the capture- // is issued (after the UI settles), so a panel that moves meanwhile is not cropped- // with its old rectangle.- let target = match s("panel") {- Some(name) => match PanelKind::from_name(name) {- Some(pk) => crate::ShotTarget::Panel(pk),- None => return err(format!("unknown panel `{name}`")),- },- None => s("id").map_or(crate::ShotTarget::Window, |id| crate::ShotTarget::Id(id.to_string())),- };- // Asking for one element and silently capturing the whole window would hide a missing- // panel: wait for it like any other not-yet-laid-out control.- let missing = match &target {- crate::ShotTarget::Panel(pk) => app.auto.find(&format!("panel.{}", pk.id())).is_none(),- crate::ShotTarget::Id(id) => app.auto.find(id).is_none(),- crate::ShotTarget::Window | crate::ShotTarget::Rect(_) => false,- };- if missing {- return Outcome::Retry(match &target {- crate::ShotTarget::Panel(pk) => format!("the {} panel is not on screen", pk.title()),- crate::ShotTarget::Id(id) => format!("no element `{id}` is on screen"),- crate::ShotTarget::Window | crate::ShotTarget::Rect(_) => "the element is not on screen".into(),- });- }- // Bring the window on screen so a frame is presented, without taking keyboard focus.- app.raise_for_control(ctx);- Outcome::Screenshot { path: s("path").map(str::to_string), target }+ }+ if let Some(f) = s("focused").and_then(PanelKind::from_name) {+ app.ui.focused = f;+ }+ if let Some(v) = p.get("viewer")+ && let Err(e) = apply_viewer(app, v)+ {+ return err(e);+ }+ set_ui_layout(app, p)+}++/// The remaining `ui.set` fields, applied in the same order after the viewer settings.+fn set_ui_layout(app: &mut EffectcraftApp, p: &Value) -> Outcome {+ if let Some(tl) = p.get("timeline") {+ apply_timeline(app, tl);+ }+ if let Some(b) = p.get("menuBar").and_then(Value::as_bool) {+ app.ui.show_menu_bar = b;+ }+ // Project panel optional columns, e.g. ["type", "duration", "path", "comment"].+ if let Some(Value::Array(a)) = p.get("projectColumns") {+ apply_project_columns(app, a);+ }+ // Wiggler / Smoother / Motion Sketch settings (fields of `AnimToolsState`).+ if let Some(Value::Object(m)) = p.get("animTools")+ && let Err(e) = apply_anim_tools(app, m)+ {+ return err(e);+ }+ if let Some(b) = p.get("home").and_then(Value::as_bool) {+ app.ui.start_screen = b;+ }+ // Home screen tab: "home", "templates" or "learn".+ if let Some(tab) = p.get("homeTab").and_then(Value::as_str) {+ app.ui.home_learn = tab == "learn";+ app.ui.home_templates = tab == "templates";+ }+ ok(Value::Null)+}++fn apply_viewer(app: &mut EffectcraftApp, v: &Value) -> Result<(), String> {+ if let Some(z) = v.get("zoom") {+ app.ui.viewer.zoom = z.as_f64().map(|z| z as f32);+ }+ // The active comp's Resolution (`view.res.*`), saved with the project.+ if let Some(r) = v.get("res").and_then(Value::as_str) {+ let Some(res) = Resolution::ALL.into_iter().find(|x| x.label().eq_ignore_ascii_case(r)) else {+ return Err(format!("unknown resolution `{r}` (auto, full, half, third, quarter)"));+ };+ if let Some(cid) = app.session.active_comp_id()+ && let Err(e) = app.session.set_resolution(cid, res)+ {+ return Err(e.to_string());}- "render.frame" => render_frame(app, p),- "ui.resize" => {- let w = p.get("width").and_then(Value::as_f64).unwrap_or(1600.0) as f32;- let h = p.get("height").and_then(Value::as_f64).unwrap_or(1000.0) as f32;- ctx.send_viewport_cmd(egui::ViewportCommand::InnerSize(egui::vec2(w, h)));- ok(Value::Null)+ }+ if let Some(Value::Array(a)) = v.get("pan") {+ app.ui.viewer.pan = [a.first().and_then(Value::as_f64).unwrap_or(0.0) as f32, a.get(1).and_then(Value::as_f64).unwrap_or(0.0) as f32];+ }+ for (k, slot) in [+ ("grid", &mut app.ui.viewer.grid),+ ("rulers", &mut app.ui.viewer.rulers),+ ("safeMargins", &mut app.ui.viewer.safe_margins),+ ("transparencyGrid", &mut app.ui.viewer.transparency_grid),+ ] {+ if let Some(b) = v.get(k).and_then(Value::as_bool) {+ *slot = b;}- "ui.focus" => {- // Explicit request: activate the app and take keyboard focus.- ctx.send_viewport_cmd(egui::ViewportCommand::Focus);- ok(Value::Null)+ }+ Ok(())+}++fn apply_timeline(app: &mut EffectcraftApp, tl: &Value) {+ if let Some(v) = tl.get("pps") {+ app.ui.timeline.pps = v.as_f64();+ }+ if let Some(v) = tl.get("start").and_then(Value::as_f64) {+ app.ui.timeline.start = v;+ }+ if let Some(v) = tl.get("graphEditor").and_then(Value::as_bool) {+ app.ui.timeline.graph_editor = v;+ }+ if let Some(v) = tl.get("showModes").and_then(Value::as_bool) {+ app.ui.timeline.show_modes = v;+ }+ if let Some(Value::Array(a)) = tl.get("openLayers") {+ app.ui.timeline.open_layers = a.iter().filter_map(Value::as_u64).collect();+ }+ if let Some(Value::Array(a)) = tl.get("openGroups") {+ app.ui.timeline.open_groups = a.iter().filter_map(Value::as_u64).collect();+ }+}++fn apply_project_columns(app: &mut EffectcraftApp, a: &[Value]) {+ let mut cols = vec![];+ for k in a.iter().filter_map(Value::as_str) {+ crate::panels::project::set_column(&mut cols, k, true);+ }+ app.ui.project_columns = cols;+}++fn apply_anim_tools(app: &mut EffectcraftApp, m: &serde_json::Map<String, Value>) -> Result<(), String> {+ let mut cur = serde_json::to_value(&app.ui.anim_tools).unwrap_or_default();+ for (k, v) in m {+ cur[k] = v.clone();+ }+ match serde_json::from_value(cur) {+ Ok(v) => {+ app.ui.anim_tools = v;+ Ok(())}- "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)+ Err(e) => Err(format!("animTools: {e}")),+ }+}++fn panel(app: &mut EffectcraftApp, method: &str, p: &Value) -> Outcome {+ let s = |k: &str| p.get(k).and_then(Value::as_str);+ let Some(panel) = s("panel").and_then(PanelKind::from_name) else { return err("unknown `panel`") };+ if method == "ui.panel.show" {+ app.show_panel(panel);+ } else {+ app.ui.dock.close(panel);+ }+ ok(Value::Null)+}++fn click_gesture_events(method: &str, p: &Value) -> Result<usize, String> {+ if method != "ui.click" {+ return Ok(3);+ }+ const MAX_CLICK_COUNT: u64 = 512;+ let count = p.get("count").and_then(Value::as_u64).unwrap_or(1);+ if count == 0 || count > MAX_CLICK_COUNT {+ return Err(format!("`count` must be between 1 and {MAX_CLICK_COUNT}"));+ }+ usize::try_from(count).ok().and_then(|n| n.checked_mul(2)).and_then(|n| n.checked_add(3)).ok_or_else(|| "click event count overflow".to_string())+}++fn click_or_move(app: &mut EffectcraftApp, method: &str, p: &Value) -> Outcome {+ let s = |k: &str| p.get(k).and_then(Value::as_str);+ let pos = match point(app, p) {+ Ok(p) => p,+ Err(e) if e.starts_with(RETRY) => return Outcome::Retry(e.trim_start_matches(RETRY).to_string()),+ Err(e) => return err(e),+ };+ let m = modifiers(p);+ // Validate the complete gesture before mutating the queue; the loopback control+ // channel is externally reachable and must not amplify tiny requests into huge work.+ const MAX_PENDING_EVENTS: usize = 4096;+ let pending = app.synthetic.len();+ let gesture_events = match click_gesture_events(method, p) {+ Ok(events) => events,+ Err(e) => return err(e),+ };+ if pending.checked_add(gesture_events).is_none_or(|n| n > MAX_PENDING_EVENTS) {+ return err("synthetic input queue is full");+ }+ // egui keeps the modifiers from `ModifiersChanged` (not the ones on a pointer event)+ // and the synthetic events arrive one per frame: hold them for the whole gesture.+ app.synthetic.push(egui::Event::ModifiersChanged(m));+ app.synthetic.push(egui::Event::PointerMoved(pos));+ if method == "ui.click" {+ let button = match s("button") {+ Some("right") => egui::PointerButton::Secondary,+ Some("middle") => egui::PointerButton::Middle,+ _ => egui::PointerButton::Primary,+ };+ let count = p.get("count").and_then(Value::as_u64).unwrap_or(1);+ for _ in 0..count {+ app.synthetic.push(egui::Event::PointerButton { pos, button, pressed: true, modifiers: m });+ app.synthetic.push(egui::Event::PointerButton { pos, button, pressed: false, modifiers: m });}- m => err(format!("unknown method `{m}`")),}+ app.synthetic.push(egui::Event::ModifiersChanged(egui::Modifiers::NONE));+ Outcome::AfterInput+}++fn drag(app: &mut EffectcraftApp, p: &Value) -> Outcome {+ let (Some(from), Some(to)) = (p.get("from"), p.get("to")) else { return err("need `from` and `to`") };+ let (a, b) = match (point(app, from), point(app, to)) {+ (Ok(a), Ok(b)) => (a, b),+ (Err(e), _) | (_, Err(e)) if e.starts_with(RETRY) => return Outcome::Retry(e.trim_start_matches(RETRY).to_string()),+ (Err(e), _) | (_, Err(e)) => return err(e),+ };+ let steps = p.get("steps").and_then(Value::as_u64).unwrap_or(12).max(2);+ const MAX_PENDING_EVENTS: usize = 4096;+ const MAX_DRAG_STEPS: u64 = 2048;+ if steps > MAX_DRAG_STEPS {+ return err(format!("`steps` must be at most {MAX_DRAG_STEPS}"));+ }+ let Some(gesture_events) = usize::try_from(steps).ok().and_then(|n| n.checked_add(5)) else { return err("drag event count overflow") };+ if app.synthetic.len().checked_add(gesture_events).is_none_or(|n| n > MAX_PENDING_EVENTS) {+ return err("synthetic input queue is full");+ }+ let m = modifiers(p);+ app.synthetic.push(egui::Event::ModifiersChanged(m));+ app.synthetic.push(egui::Event::PointerMoved(a));+ app.synthetic.push(egui::Event::PointerButton { pos: a, button: egui::PointerButton::Primary, pressed: true, modifiers: m });+ for i in 1..=steps {+ let f = i as f32 / steps as f32;+ app.synthetic.push(egui::Event::PointerMoved(a + (b - a) * f));+ }+ app.synthetic.push(egui::Event::PointerButton { pos: b, button: egui::PointerButton::Primary, pressed: false, modifiers: m });+ app.synthetic.push(egui::Event::ModifiersChanged(egui::Modifiers::NONE));+ Outcome::AfterInput+}++fn scroll(app: &mut EffectcraftApp, p: &Value) -> Outcome {+ let pos = match point(app, p) {+ Ok(p) => p,+ Err(e) => return err(e),+ };+ let dx = p.get("dx").and_then(Value::as_f64).unwrap_or(0.0) as f32;+ let dy = p.get("dy").and_then(Value::as_f64).unwrap_or(0.0) as f32;+ app.synthetic.push(egui::Event::PointerMoved(pos));+ app.synthetic.push(egui::Event::MouseWheel {+ unit: egui::MouseWheelUnit::Point,+ delta: egui::vec2(dx, dy),+ modifiers: modifiers(p),+ phase: egui::TouchPhase::Move,+ source: egui::MouseWheelSource::Wheel,+ });+ app.synthetic.push(egui::Event::PointerButton {+ pos: egui::pos2(-100.0, -100.0),+ button: egui::PointerButton::Extra1,+ pressed: false,+ modifiers: Default::default(),+ });+ Outcome::AfterInput+}++fn key(app: &mut EffectcraftApp, p: &Value) -> Outcome {+ let s = |k: &str| p.get(k).and_then(Value::as_str);+ let Some(name) = s("key") else { return err("missing `key`") };+ let Some((mut m, key)) = crate::menus::parse_shortcut(name) else { return err(format!("unknown key `{name}`")) };+ let extra = modifiers(p);+ m |= extra;+ let physical_key = name.starts_with("Physical+").then_some(key);+ app.synthetic.push(egui::Event::Key { key, physical_key, pressed: true, repeat: false, modifiers: m });+ app.synthetic.push(egui::Event::Key { key, physical_key, pressed: false, repeat: false, modifiers: m });+ Outcome::AfterInput+}++fn type_text(app: &mut EffectcraftApp, p: &Value) -> Outcome {+ let Some(text) = p.get("text").and_then(Value::as_str) else { return err("missing `text`") };+ app.synthetic.push(egui::Event::Text(text.to_string()));+ app.synthetic.push(egui::Event::Key { key: egui::Key::F35, physical_key: None, pressed: false, repeat: false, modifiers: Default::default() });+ Outcome::AfterInput+}++fn viewer_locate(ctx: &egui::Context, p: &Value) -> Outcome {+ let (Some(x), Some(y)) = (p.get("x").and_then(Value::as_f64), p.get("y").and_then(Value::as_f64)) else {+ return err("need `x`, `y` (comp pixels)");+ };+ match crate::panels::viewer::comp_to_screen(ctx, [x as f32, y as f32]) {+ Some(pt) => ok(json!({"x": pt.x, "y": pt.y})),+ None => Outcome::Retry("the composition viewer is not visible".into()),+ }+}++fn viewer_hit(app: &mut EffectcraftApp, ctx: &egui::Context, p: &Value) -> Outcome {+ let pos = match point(app, p) {+ Ok(p) => p,+ Err(e) => return err(e),+ };+ match crate::panels::viewer::screen_to_comp(ctx, pos) {+ Some(c) => ok(json!({"x": c[0], "y": c[1]})),+ None => err("the composition viewer is not visible"),+ }+}++fn timeline_locate(app: &EffectcraftApp, p: &Value) -> Outcome {+ let Some(l) = p.get("layer").and_then(Value::as_u64) else { return err("need `layer` (id)") };+ match crate::panels::timeline::locate(app, l, p.get("time").and_then(Value::as_f64)) {+ Some((x, y)) => ok(json!({"x": x, "y": y})),+ None => Outcome::Retry("layer not visible in the timeline".into()),+ }+}++fn playback(app: &mut EffectcraftApp, p: &Value, now: f64) -> Outcome {+ let action = p.get("action").and_then(Value::as_str).unwrap_or("toggle");+ match action {+ "play" => app.play(now),+ "stop" => app.stop(),+ "status" => {}+ _ => app.toggle_play(now),+ }+ ok(+ json!({"playing": app.playback.playing, "time": app.session.time().seconds(), "audio": app.audio.is_some(), "levelsDb": app.meter.level_db, "peaksDb": app.meter.peak_db}),+ )+}++fn screenshot(app: &mut EffectcraftApp, ctx: &egui::Context, p: &Value) -> Outcome {+ let s = |k: &str| p.get(k).and_then(Value::as_str);+ // The panel/element is an identity, not a rectangle: it is resolved when the capture+ // is issued (after the UI settles), so a panel that moves meanwhile is not cropped+ // with its old rectangle.+ let target = match s("panel") {+ Some(name) => match PanelKind::from_name(name) {+ Some(pk) => crate::ShotTarget::Panel(pk),+ None => return err(format!("unknown panel `{name}`")),+ },+ None => s("id").map_or(crate::ShotTarget::Window, |id| crate::ShotTarget::Id(id.to_string())),+ };+ // Asking for one element and silently capturing the whole window would hide a missing+ // panel: wait for it like any other not-yet-laid-out control.+ let missing = match &target {+ crate::ShotTarget::Panel(pk) => app.auto.find(&format!("panel.{}", pk.id())).is_none(),+ crate::ShotTarget::Id(id) => app.auto.find(id).is_none(),+ crate::ShotTarget::Window | crate::ShotTarget::Rect(_) => false,+ };+ if missing {+ return Outcome::Retry(match &target {+ crate::ShotTarget::Panel(pk) => format!("the {} panel is not on screen", pk.title()),+ crate::ShotTarget::Id(id) => format!("no element `{id}` is on screen"),+ crate::ShotTarget::Window | crate::ShotTarget::Rect(_) => "the element is not on screen".into(),+ });+ }+ // Bring the window on screen so a frame is presented, without taking keyboard focus.+ app.raise_for_control(ctx);+ Outcome::Screenshot { path: s("path").map(str::to_string), target }+}++fn resize(ctx: &egui::Context, p: &Value) -> Outcome {+ let w = p.get("width").and_then(Value::as_f64).unwrap_or(1600.0) as f32;+ let h = p.get("height").and_then(Value::as_f64).unwrap_or(1000.0) as f32;+ ctx.send_viewport_cmd(egui::ViewportCommand::InnerSize(egui::vec2(w, h)));+ ok(Value::Null)+}++fn quit(app: &mut EffectcraftApp, ctx: &egui::Context, p: &Value) -> Outcome {+ // 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)}/// `render.frame {comp?, time?, max_side?, path?, base64?}`: render a comp frame through the sessiondiff --git a/crates/ui-egui/src/lib.rs b/crates/ui-egui/src/lib.rsindex 531d7dd6e..2eed3678c 100644--- a/crates/ui-egui/src/lib.rs+++ b/crates/ui-egui/src/lib.rs@@ -1402,11 +1402,37 @@ impl EffectcraftApp {let video = plan.is_none_or(|p| p.video);// Prefetch ahead.let cur = fr.frame_at(self.session.time());- // While paused (scrubbing, editing) the viewer's frame comes first: prefetch only a few- // frames, only once it is done, and not while edits keep coming (a layer dragged in the- // viewer): those frames would be stale at the next step and would hold the CPU and GPU- // the next viewer frame needs.- let ahead = if self.playback.playing {+ let ahead = self.prefetch_ahead(ctx, now);+ let queued = self.frames.inflight();+ let series = self.frame_series(cid, scale);+ // The preview's position orders prefetch and eviction (#595).+ let playhead = plan.filter(|p| p.video).map(|p| frames::Playhead { series, cur, start: p.start, end: p.end, step: p.step, looping: p.looping });+ self.frames.set_playhead(playhead);+ if self.cache_preview(ctx, &c, cid, scale, now, fr, series, plan, ahead, queued) {+ return;+ }+ let step = plan.map_or(1, |p| p.step);+ let fit_ahead = self.frames_that_fit(&c, scale) - 1;+ self.prefetch_playback(cid, scale, plan, cur, wa, wb, ahead, fit_ahead, series, queued, video);+ self.pump_meters(ctx, now);+ if !self.playback.playing {+ self.cache_when_idle(ctx, &c, &series, now, input_active, (wa, wb));+ return;+ }+ let fit = self.frames_that_fit(&c, scale);+ self.start_held_audio(now, plan, series, cur, ahead, fit, fr);+ if self.advance_audio_clock(ctx, now, series, wa, wb, video, fr, cur, step) {+ return;+ }+ self.advance_frame_clock(ctx, now, plan, video, looping, wa, wb, fr, cur, step, series);+ }++ /// While paused (scrubbing, editing) the viewer's frame comes first: prefetch only a few+ /// frames, only once it is done, and not while edits keep coming (a layer dragged in the+ /// viewer): those frames would be stale at the next step and would hold the CPU and GPU+ /// the next viewer frame needs.+ fn prefetch_ahead(&mut self, ctx: &egui::Context, now: f64) -> i64 {+ if self.playback.playing {(self.frames_parallelism() * 2).max(4) as i64} else if self.frames.urgent_pending() {0@@ -1417,59 +1443,83 @@ impl EffectcraftApp {// Keep workers available for a newly edited viewer frame. Running obsolete// background renders cannot be preempted by the priority queue.self.frames_parallelism().saturating_sub(1).min(2) as i64- };- let mut queued = self.frames.inflight();- let series = self.frame_series(cid, scale);- // The preview's position orders prefetch and eviction (#595).- let playhead = plan.filter(|p| p.video).map(|p| frames::Playhead { series, cur, start: p.start, end: p.end, step: p.step, looping: p.looping });- self.frames.set_playhead(playhead);- if self.playback.caching- && let Some(p) = plan- {- // Cache Before Playback: the range in play order, then start the clock. A range- // larger than the RAM preview budget is cut to the frames that fit (caching more- // would evict the first ones and never finish).- let mut all = true;- let n = (p.end - p.start) / p.step + 1;- let fit = self.frames_that_fit(&c, scale);- if n > fit {- let mut cut = p;- (cut.start, cut.end) = (p.first, (p.first + (fit - 1) * p.step).min(p.end));- self.playback.plan = Some(cut);- self.toast = Some((format!("Preview cut to {fit} of {n} frames: no more fit in the RAM preview (Settings ▸ Memory & CPU)"), now));- ctx.request_repaint();- return;- }- let order: Vec<i64> = (0..n).filter_map(|k| p.frame_after(p.first, k)).collect();- for f in order {- if self.frames.is_cached(&FrameKey { frame: f, ..series }) {- continue;- }- all = false;- if queued < ahead as usize {- self.request_frame(cid, f, scale);- queued += 1;- }+ }+ }++ /// Cache Before Playback: the range in play order, then start the clock. A range larger than+ /// the RAM preview budget is cut to the frames that fit (caching more would evict the first+ /// ones and never finish). Returns true when this pass handled the preview.+ fn cache_preview(+ &mut self,+ ctx: &egui::Context,+ comp: &effectcraft_engine::project::Comp,+ cid: ItemId,+ scale: f64,+ now: f64,+ fr: effectcraft_engine::time::FrameRate,+ series: FrameKey,+ plan: Option<effectcraft_engine::preview::PreviewPlan>,+ ahead: i64,+ mut queued: usize,+ ) -> bool {+ if !self.playback.caching {+ return false;+ }+ let Some(p) = plan else { return false };+ let mut all = true;+ let n = (p.end - p.start) / p.step + 1;+ let fit = self.frames_that_fit(comp, scale);+ if n > fit {+ let mut cut = p;+ (cut.start, cut.end) = (p.first, (p.first + (fit - 1) * p.step).min(p.end));+ self.playback.plan = Some(cut);+ self.toast = Some((format!("Preview cut to {fit} of {n} frames: no more fit in the RAM preview (Settings ▸ Memory & CPU)"), now));+ ctx.request_repaint();+ return true;+ }+ let order: Vec<i64> = (0..n).filter_map(|k| p.frame_after(p.first, k)).collect();+ for f in order {+ if self.frames.is_cached(&FrameKey { frame: f, ..series }) {+ continue;}- self.playback.waiting = !all;- if all {- self.playback.caching = false;- self.playback.start_wall = now;- self.playback.start_frame = p.first;- self.session.set_time(fr.tick_of(p.first));- self.start_audio(fr.tick_of(p.first));+ all = false;+ if queued < ahead as usize {+ self.request_frame(cid, f, scale);+ queued += 1;}- ctx.request_repaint();- return;}- let step = plan.map_or(1, |p| p.step);- // Never further ahead than the RAM preview holds: those frames would evict the ones the- // preview shows first, or be evicted before it gets there (#595). The window starts at the- // first uncached frame ahead: after a loop or a restart the frames just ahead are cached,- // and a window from the playhead left the workers idle until it came near the end of the- // cached run. It stays ahead × 3 frames wide: workers spread over the whole preview render- // frames with different layers at once (on the GPU, an atlas each: #1812).- let fit_ahead = self.frames_that_fit(&c, scale) - 1;+ self.playback.waiting = !all;+ if all {+ self.playback.caching = false;+ self.playback.start_wall = now;+ self.playback.start_frame = p.first;+ self.session.set_time(fr.tick_of(p.first));+ self.start_audio(fr.tick_of(p.first));+ }+ ctx.request_repaint();+ true+ }++ /// Never further ahead than the RAM preview holds: those frames would evict the ones the+ /// preview shows first, or be evicted before it gets there (#595). The window starts at the+ /// first uncached frame ahead: after a loop or a restart the frames just ahead are cached,+ /// and a window from the playhead left the workers idle until it came near the end of the+ /// cached run. It stays ahead × 3 frames wide: workers spread over the whole preview render+ /// frames with different layers at once (on the GPU, an atlas each: #1812).+ fn prefetch_playback(+ &self,+ cid: ItemId,+ scale: f64,+ plan: Option<effectcraft_engine::preview::PreviewPlan>,+ cur: i64,+ wa: i64,+ wb: i64,+ ahead: i64,+ fit_ahead: i64,+ series: FrameKey,+ mut queued: usize,+ video: bool,+ ) {let frame = |k: i64| match plan {Some(p) => p.frame_after(cur, k),None => {@@ -1496,6 +1546,9 @@ impl EffectcraftApp {queued += 1;}}+ }++ fn pump_meters(&mut self, ctx: &egui::Context, now: f64) {if let Some(a) = &mut self.audio {a.pump();self.meter.update(a.feed.take_peaks(), now);@@ -1511,57 +1564,97 @@ impl EffectcraftApp {self.meter.update([0.0; 2], now);ctx.request_repaint();}- if !self.playback.playing {- self.cache_when_idle(ctx, &c, &series, now, input_active, (wa, wb));+ }++ /// Held sound starts once the frames ahead are cached (see `Playback::audio_held`): the+ /// prefetch window when the silent preview keeps real time, else the rest of the preview.+ fn start_held_audio(+ &mut self,+ now: f64,+ plan: Option<effectcraft_engine::preview::PreviewPlan>,+ series: FrameKey,+ cur: i64,+ ahead: i64,+ fit: i64,+ fr: effectcraft_engine::time::FrameRate,+ ) {+ if !self.playback.audio_held {return;}- let snap = |f: i64| wa + (f - wa).div_euclid(step) * step;- // Held sound starts once the frames ahead are cached (see `Playback::audio_held`): the- // prefetch window when the silent preview keeps real time, else the rest of the preview.- let fit = self.frames_that_fit(&c, scale);- if self.playback.audio_held- && let Some(p) = plan- && ((self.playback.keeps_up(now) && self.cached_ahead(&p, &series, cur, ahead.min(fit))) || self.cached_ahead(&p, &series, cur, fit))- {+ let Some(p) = plan else { return };+ if (self.playback.keeps_up(now) && self.cached_ahead(&p, &series, cur, ahead.min(fit))) || self.cached_ahead(&p, &series, cur, fit) {self.playback.audio_held = false;self.playback.start_wall = now;self.playback.start_frame = cur;self.start_audio(fr.tick_of(cur));}- // Audio clock drives playback: show the frame under the audible sample.- if let Some(a) = &self.audio {- if a.finished() {- if video {- self.session.set_time(fr.tick_of(wb));- } else {- self.playback.audio_frame = Some(wb);- }- self.stop();- return;- }- let target = snap(audio::frame_of_sample(a.clock(), a.rate, fr).clamp(wa, wb));- if !video {- self.playback.audio_frame = Some(target);- } else if self.frames.is_cached(&FrameKey { frame: target, ..series }) {- self.playback.waiting = false;- if target != cur {- self.session.set_time(fr.tick_of(target));- self.playback.frame_shown(now);- }+ }++ /// Audio clock drives playback: show the frame under the audible sample. Returns true when+ /// the audio path handled this pass (including stopping at the end).+ fn advance_audio_clock(+ &mut self,+ ctx: &egui::Context,+ now: f64,+ series: FrameKey,+ wa: i64,+ wb: i64,+ video: bool,+ fr: effectcraft_engine::time::FrameRate,+ cur: i64,+ step: i64,+ ) -> bool {+ let Some(a) = &self.audio else { return false };+ if a.finished() {+ if video {+ self.session.set_time(fr.tick_of(wb));} else {- // Rendering can't keep up: rather than skip frames, the sound stops and every- // frame shows as it renders until the frames ahead are cached again.- self.audio = None;- self.playback.audio_held = true;- self.playback.waiting = true;- // The rate measured before the stall no longer says it keeps up.- self.playback.shown_at.clear();- self.playback.start_wall = now;- self.playback.start_frame = cur;+ self.playback.audio_frame = Some(wb);}- ctx.request_repaint();- return;+ self.stop();+ return true;+ }+ let snap = |f: i64| wa + (f - wa).div_euclid(step) * step;+ let target = snap(audio::frame_of_sample(a.clock(), a.rate, fr).clamp(wa, wb));+ if !video {+ self.playback.audio_frame = Some(target);+ } else if self.frames.is_cached(&FrameKey { frame: target, ..series }) {+ self.playback.waiting = false;+ if target != cur {+ self.session.set_time(fr.tick_of(target));+ self.playback.frame_shown(now);+ }+ } else {+ // Rendering can't keep up: rather than skip frames, the sound stops and every+ // frame shows as it renders until the frames ahead are cached again.+ self.audio = None;+ self.playback.audio_held = true;+ self.playback.waiting = true;+ // The rate measured before the stall no longer says it keeps up.+ self.playback.shown_at.clear();+ self.playback.start_wall = now;+ self.playback.start_frame = cur;}+ ctx.request_repaint();+ true+ }++ /// The wall clock advances the preview when no audio device drives it, and shows the target+ /// frame (or holds when it is not cached yet).+ fn advance_frame_clock(+ &mut self,+ ctx: &egui::Context,+ now: f64,+ plan: Option<effectcraft_engine::preview::PreviewPlan>,+ video: bool,+ looping: bool,+ wa: i64,+ wb: i64,+ fr: effectcraft_engine::time::FrameRate,+ cur: i64,+ step: i64,+ series: FrameKey,+ ) {let elapsed = now - self.playback.start_wall;// The plan carries the rate the preview was started with (and `play_with` copies it to// `Playback::fps`). Deriving the frame from the plan keeps a logic-only tick correct even@@ -1919,9 +2012,23 @@ impl EffectcraftApp {/// viewport, and everything that can write a file or reply happens here, on the app thread.fn tick_screenshots(&mut self, ctx: &egui::Context) {let Some(geom) = self.current_geometry(ctx) else { return };- // 1. Drain completed slots. A completion whose surface epoch changed since issuance is- // discarded even if the geometry changed back: it was cropped with another frame's- // pixels-per-point.+ self.drain_completed_screenshots(geom);+ self.timeout_screenshots();+ self.issue_screenshots(ctx, geom);+ // 4. Wake up for the earliest deadline even when no further pass is otherwise needed: an+ // issued capture whose callback never arrives (hidden window, display asleep, renderer+ // failure) would otherwise never reach its timeout. This is a timer, not a paint+ // request: the issued screenshot command already forces the frame it needs.+ if let Some(next) = self.pending_screenshots.iter().map(|s| s.deadline).reduce(f64::min) {+ let wait = (next - self.now).max(0.001);+ ctx.request_repaint_after(std::time::Duration::from_secs_f64(wait));+ }+ }++ /// 1. Drain completed slots. A completion whose surface epoch changed since issuance is+ /// discarded even if the geometry changed back: it was cropped with another frame's+ /// pixels-per-point.+ fn drain_completed_screenshots(&mut self, geom: DrawGeometry) {let mut i = 0;while i < self.pending_screenshots.len() {let taken = self.pending_screenshots.get(i).and_then(|s| s.slot.as_ref()).and_then(craft_ui::capture::CaptureSlot::take);@@ -1977,7 +2084,10 @@ impl EffectcraftApp {}}}- // 2. Timeouts: a late callback finds its slot cancelled and writes nothing.+ }++ /// 2. Timeouts: a late callback finds its slot cancelled and writes nothing.+ fn timeout_screenshots(&mut self) {let mut i = 0;while i < self.pending_screenshots.len() {if self.now > self.pending_screenshots[i].deadline {@@ -1986,8 +2096,11 @@ impl EffectcraftApp {i += 1;}}- // 3. Issue settled requests. The command itself requests the paint; a request still- // waiting for its passes needs `want_paint` so a hidden window keeps painting.+ }++ /// 3. Issue settled requests. The command itself requests the paint; a request still+ /// waiting for its passes needs `want_paint` so a hidden window keeps painting.+ fn issue_screenshots(&mut self, ctx: &egui::Context, geom: DrawGeometry) {let mut i = 0;while i < self.pending_screenshots.len() {if self.pending_screenshots[i].slot.is_some() {@@ -2038,14 +2151,6 @@ impl EffectcraftApp {s.crop = crop;i += 1;}- // 4. Wake up for the earliest deadline even when no further pass is otherwise needed: an- // issued capture whose callback never arrives (hidden window, display asleep, renderer- // failure) would otherwise never reach its timeout. This is a timer, not a paint- // request: the issued screenshot command already forces the frame it needs.- if let Some(next) = self.pending_screenshots.iter().map(|s| s.deadline).reduce(f64::min) {- let wait = (next - self.now).max(0.001);- ctx.request_repaint_after(std::time::Duration::from_secs_f64(wait));- }}// ---------------------------------------------------------------- framediff --git a/crates/ui-egui/src/panels/layer_styles_dialog.rs b/crates/ui-egui/src/panels/layer_styles_dialog.rsindex c0b03f77e..c615bc4f4 100644--- a/crates/ui-egui/src/panels/layer_styles_dialog.rs+++ b/crates/ui-egui/src/panels/layer_styles_dialog.rs@@ -260,9 +260,11 @@ pub fn finish(app: &mut EffectcraftApp, ok: bool) {let changed = !std::sync::Arc::ptr_eq(&app.session.project, &checkpoint.project);app.session.project = checkpoint.project;app.session.history = checkpoint.history;- app.session.sanitize_state();if changed {+ app.session.sanitize_after_replacement();app.session.bump();+ } else {+ app.session.sanitize_state();}}app.session.history.merge_key = None;