From afdcf956fbe4a328c1714846a46420565a2d0e47 Mon Sep 17 00:00:00 2001 From: vangel76 Date: Sat, 7 Nov 2026 12:13:18 +0100 Subject: [PATCH 01/74] Effects: Grid, Checkerboard and Circle keep the layer's mask with Blending Mode None Masks are applied to a layer before its effects, but with the default Blending Mode None these generators replaced each pixel with the generated pattern, so the pattern drew over the whole layer and the mask was lost. With None the generated alpha is now scaled by the layer's alpha, as 4-Color Gradient already did (#1381). A shared gen_alpha helper (CPU) and its WGSL twin hold the rule for Grid, Checkerboard, Circle and 4-Color Gradient. Only None keeps the layer's alpha: the other modes composite the pattern onto the layer as After Effects' blending modes do, and their alpha outside the layer is not verified against After Effects, so they are unchanged. Fixes #3121 --- crates/effects/src/generate.rs | 71 ++++++++++++++++++++++--- crates/gpu/src/shaders/fx_extra.wgsl | 5 +- crates/gpu/src/shaders/fx_generate.wgsl | 18 +++++-- crates/render/src/tests.rs | 24 +++++++++ 4 files changed, 104 insertions(+), 14 deletions(-) diff --git a/crates/effects/src/generate.rs b/crates/effects/src/generate.rs index ae0d61861..2c772219a 100644 --- a/crates/effects/src/generate.rs +++ b/crates/effects/src/generate.rs @@ -53,7 +53,8 @@ fn gradient_ramp(ctx: &EffectCtx, mut b: Buf) -> Buf { } /// Blending Mode options of the shape generators (Checkerboard, Grid, Circle): "None" shows the -/// generated pattern only; the others composite it onto the layer like layer blending modes. +/// generated pattern only, inside the layer's alpha (see [`gen_alpha`]); the others composite it +/// onto the layer like layer blending modes. pub(crate) const GEN_BLEND_MODES: [&str; 20] = [ "None", "Normal", @@ -103,7 +104,8 @@ pub fn gen_mode(i: u32) -> Option { } /// Composite a generated premultiplied pixel `g` onto the original `o` with blending mode -/// index `mode` (into [`GEN_BLEND_MODES`]). +/// index `mode` (into [`GEN_BLEND_MODES`]). None returns `g` as is: the caller keeps the layer's +/// alpha in `g` with [`gen_alpha`]. #[inline] pub(crate) fn gen_blend(o: Px, g: Px, mode: u32) -> Px { match gen_mode(mode) { @@ -112,6 +114,16 @@ pub(crate) fn gen_blend(o: Px, g: Px, mode: u32) -> Px { } } +/// The generated alpha `a` with Blending Mode None (`keep_alpha`, `gen_mode(mode).is_none()`): +/// scaled by the layer's alpha `layer_a`, so the pattern stays inside the layer's shape and its +/// masks, which are applied before effects (#1381, #3121). The other modes composite onto the +/// layer as After Effects' blending modes do and keep `a`; their alpha outside the layer is not +/// verified against After Effects. Mirrored by `gen_alpha` in `fx_generate.wgsl`. +#[inline] +pub(crate) fn gen_alpha(a: f32, layer_a: f32, keep_alpha: bool) -> f32 { + if keep_alpha { a * layer_a } else { a } +} + /// 4-Color Gradient's weights are inverse squared distances softened by Blend²: each point /// weighs 1 / (d² + Blend²) (pixels). Fitted to After Effects 2026 renders at Blend 10, 50, 100 /// and 300 (power 2.00 at every Blend, softening 0 / 2367 / 9817 / 90127 ≈ Blend²; mean error @@ -128,6 +140,7 @@ fn four_color(ctx: &EffectCtx, mut b: Buf) -> Buf { let jitter = (ctx.params.f("jitter") / 100.0).clamp(0.0, 1.0) as f32; let op = ctx.params.f("opacity") as f32 / 100.0; let mode = ctx.params.e("blendingMode"); + let keep_alpha = gen_mode(mode).is_none(); let seed = ctx.seed; b.img.rows_mut().for_each(|(y, row)| { for (x, px) in row.iter_mut().enumerate() { @@ -149,7 +162,7 @@ fn four_color(ctx: &EffectCtx, mut b: Buf) -> Buf { } // None keeps the layer's alpha (#1381), as the Gradient Ramp does: the gradient fills // a shape or text, not its bounding box. - let a = if gen_mode(mode).is_none() { op * px[3] } else { op }; + let a = gen_alpha(op, px[3], keep_alpha); *px = gen_blend(*px, [c[0] * a, c[1] * a, c[2] * a, a], mode); } }); @@ -179,6 +192,7 @@ fn checkerboard(ctx: &EffectCtx, mut b: Buf) -> Buf { let c = ctx.params.color("color"); let op = ctx.params.f("opacity") as f32 / 100.0; let mode = ctx.params.e("blendingMode"); + let keep_alpha = gen_mode(mode).is_none(); // Signed distance (in feather units) to the nearest cell edge, positive in even cells. let axis = |p: f64, size: f64, f: f64| -> f64 { let u = p / size; @@ -191,8 +205,8 @@ fn checkerboard(ctx: &EffectCtx, mut b: Buf) -> Buf { for (x, px) in row.iter_mut().enumerate() { let sx = axis(x as f64 + 0.5 - anchor.0, w, fw); let sy = axis(y as f64 + 0.5 - anchor.1, h, fh); - let cov = ((0.5 + 0.5 * sx * sy) as f32).clamp(0.0, 1.0) * op; - *px = gen_blend(*px, [c[0] * cov, c[1] * cov, c[2] * cov, cov], mode); + let a = gen_alpha(((0.5 + 0.5 * sx * sy) as f32).clamp(0.0, 1.0) * op, px[3], keep_alpha); + *px = gen_blend(*px, [c[0] * a, c[1] * a, c[2] * a, a], mode); } }); b @@ -208,6 +222,7 @@ fn grid(ctx: &EffectCtx, mut b: Buf) -> Buf { let c = ctx.params.color("color"); let op = ctx.params.f("opacity") as f32 / 100.0; let mode = ctx.params.e("blendingMode"); + let keep_alpha = gen_mode(mode).is_none(); b.img.rows_mut().for_each(|(y, row)| { for (x, px) in row.iter_mut().enumerate() { let gx = (x as f64 + 0.5 - anchor.0).rem_euclid(w); @@ -219,7 +234,7 @@ fn grid(ctx: &EffectCtx, mut b: Buf) -> Buf { if invert { cov = 1.0 - cov; } - let a = cov * op; + let a = gen_alpha(cov * op, px[3], keep_alpha); *px = gen_blend(*px, [c[0] * a, c[1] * a, c[2] * a, a], mode); } }); @@ -256,6 +271,7 @@ fn circle(ctx: &EffectCtx, mut b: Buf) -> Buf { let op = ctx.params.f("opacity") as f32 / 100.0; let invert = ctx.params.b("invert"); let mode = ctx.params.e("blendingMode"); + let keep_alpha = gen_mode(mode).is_none(); b.img.rows_mut().for_each(|(y, row)| { for (x, px) in row.iter_mut().enumerate() { let d = ((x as f64 + 0.5 - c.0).powi(2) + (y as f64 + 0.5 - c.1).powi(2)).sqrt(); @@ -266,7 +282,7 @@ fn circle(ctx: &EffectCtx, mut b: Buf) -> Buf { if invert { cov = 1.0 - cov; } - let a = cov * op; + let a = gen_alpha(cov * op, px[3], keep_alpha); *px = gen_blend(*px, [color[0] * a, color[1] * a, color[2] * a, a], mode); } }); @@ -572,6 +588,47 @@ mod generator_tests { assert!(o.img.get(0, 5)[0] > 0.9 && o.img.get(5, 5)[3] < 0.01); } + /// With Blending Mode None, a shape generator's pattern shows only where the layer has alpha + /// (#1381, #3121: masks come before effects). `set` makes the pattern cover (5, 5) and (35, 5). + fn assert_none_stays_inside_the_layer_alpha(id: &str, set: &[(&'static str, Value)]) { + // Three bands: transparent, half and fully opaque, premultiplied. + let mut img = Image::filled(40, 24, [1.0, 0.0, 0.0, 1.0]); + for (i, p) in img.data.iter_mut().enumerate() { + let a = [0.0, 0.5, 1.0][i % 40 / 14]; + *p = p.map(|c| c * a); + } + for op in [100.0, 50.0] { + let mut set = set.to_vec(); + set.push(("opacity", num(op))); + let o = run_fx(id, &set, img.clone(), 0.0, EffectEnv::default()); + let k = op as f32 / 100.0; + for (src, out) in img.data.iter().zip(&o.img.data) { + assert!(out[3] <= src[3] * k + 1e-6, "{id} opacity {op}: alpha {} became {}", src[3], out[3]); + assert!(out[..3].iter().all(|&c| c <= out[3] + 1e-6), "{id} opacity {op}: premultiplied colour above alpha"); + } + assert!(o.img.get(5, 5)[3] < 1e-6, "{id} opacity {op}: the pattern shows where the layer is transparent"); + assert!(o.img.get(35, 5)[3] > 0.9 * k, "{id} opacity {op}: the pattern shows on the opaque layer: {:?}", o.img.get(35, 5)); + } + } + + #[test] + fn grid_none_stays_inside_the_layer_alpha() { + // Inverted, the grid covers nearly every pixel, wherever the lines fall. + assert_none_stays_inside_the_layer_alpha("ec.generate.grid", &[("width", num(10.0)), ("border", num(2.0)), ("invertGrid", Value::Bool(true))]); + } + + #[test] + fn checkerboard_none_stays_inside_the_layer_alpha() { + // 8-pixel squares from the anchor at 0: (5, 5) and (35, 5) lie in on squares. + assert_none_stays_inside_the_layer_alpha("ec.generate.checkerboard", &[("width", num(8.0))]); + } + + #[test] + fn circle_none_stays_inside_the_layer_alpha() { + // A disk larger than the image covers every pixel. + assert_none_stays_inside_the_layer_alpha("ec.generate.circle", &[("center", Value::Vec2([20.0, 12.0])), ("radius", num(100.0))]); + } + #[test] fn circle_edge_makes_a_ring() { let c = ("center", Value::Vec2([16.0, 16.0])); diff --git a/crates/gpu/src/shaders/fx_extra.wgsl b/crates/gpu/src/shaders/fx_extra.wgsl index e1425ddcd..35170b24c 100644 --- a/crates/gpu/src/shaders/fx_extra.wgsl +++ b/crates/gpu/src/shaders/fx_extra.wgsl @@ -29,8 +29,9 @@ fn fxe_circle(@builtin(global_invocation_id) gid: vec3) { if (P.u[0].z != 0u) { cov = 1.0 - cov; } - let a = cov * P.f[1].z; - textureStore(out, p, gen_blend(textureLoad(src, p, 0), vec4(P.f[2].xyz * a, a), P.u[0].x)); + let o = textureLoad(src, p, 0); + let a = gen_alpha(cov * P.f[1].z, o.w, P.u[0].x); + textureStore(out, p, gen_blend(o, vec4(P.f[2].xyz * a, a), P.u[0].x)); } // ---------------------------------------------------------------- Ellipse (generate2::ellipse) diff --git a/crates/gpu/src/shaders/fx_generate.wgsl b/crates/gpu/src/shaders/fx_generate.wgsl index 7a2a026bd..876428435 100644 --- a/crates/gpu/src/shaders/fx_generate.wgsl +++ b/crates/gpu/src/shaders/fx_generate.wgsl @@ -32,6 +32,12 @@ fn gen_blend(o: vec4, g: vec4, mode: u32) -> vec4 { return blend_pixel(mode, o, g, 0.5); } +// generate::gen_alpha: with None (mode 255) the generated alpha `a` is scaled by the layer's +// alpha, so the pattern stays inside the layer and its masks; other modes keep `a`. +fn gen_alpha(a: f32, layer_a: f32, mode: u32) -> f32 { + return select(a, a * layer_a, mode == 255u); +} + // A (hi, lo) pair from `data`. fn gen_hl(i: u32) -> vec2 { return vec2(data[i], data[i + 1u]); @@ -466,9 +472,10 @@ fn gen_checker(@builtin(global_invocation_id) gid: vec3) { } let sx = gen_axis(f32(p.x) + P.f[0].x, P.f[0].z, P.f[1].x); let sy = gen_axis(f32(p.y) + P.f[0].y, P.f[0].w, P.f[1].y); - let cov = clamp(0.5 + 0.5 * sx * sy, 0.0, 1.0) * P.f[1].z; + let o = textureLoad(src, p, 0); + let a = gen_alpha(clamp(0.5 + 0.5 * sx * sy, 0.0, 1.0) * P.f[1].z, o.w, P.u[0].x); let c = P.f[2].xyz; - textureStore(out, p, gen_blend(textureLoad(src, p, 0), vec4(c * cov, cov), P.u[0].x)); + textureStore(out, p, gen_blend(o, vec4(c * a, a), P.u[0].x)); } // f32::rem_euclid. @@ -502,8 +509,9 @@ fn gen_grid(@builtin(global_invocation_id) gid: vec3) { if (P.u[0].y != 0u) { cov = 1.0 - cov; } - let a = cov * P.f[1].z; - textureStore(out, p, gen_blend(textureLoad(src, p, 0), vec4(P.f[2].xyz * a, a), P.u[0].x)); + let o = textureLoad(src, p, 0); + let a = gen_alpha(cov * P.f[1].z, o.w, P.u[0].x); + textureStore(out, p, gen_blend(o, vec4(P.f[2].xyz * a, a), P.u[0].x)); } // u[0] = (mode, seed); f[0..4] = points (x, y); f[4..8] = colours; @@ -535,7 +543,7 @@ fn gen_fourcolor(@builtin(global_invocation_id) gid: vec3) { } let o = textureLoad(src, p, 0); // None keeps the layer's alpha, as the CPU path does (#1381). - let op = select(P.f[8].z, P.f[8].z * o.w, P.u[0].x == 255u); + let op = gen_alpha(P.f[8].z, o.w, P.u[0].x); textureStore(out, p, gen_blend(o, vec4(c * op, op), P.u[0].x)); } diff --git a/crates/render/src/tests.rs b/crates/render/src/tests.rs index c77fc2d94..ab38651a3 100644 --- a/crates/render/src/tests.rs +++ b/crates/render/src/tests.rs @@ -784,6 +784,30 @@ fn effects_see_layer_masks() { assert!(img.get(150, 50)[3] > 0.99, "mode None mask does not cut the layer"); } +/// Masks come before effects: Grid (Blending Mode None) draws only inside a masked solid (#3121). +#[test] +fn grid_stays_inside_the_layer_mask() { + let (mut p, cid, comp) = setup(); + let mut l = solid(&mut p, &comp, [1.0, 1.0, 1.0], 200, 100); + let mut next = p.next_id; + let m = build::mask(&mut Ids(&mut next), "Mask 1", ShapePath::rect([50.0, 50.0], 40.0, 40.0), MaskMode::Add, [255, 255, 0]); + p.next_id = next; + l.props.sub_mut("masks").unwrap().children.push(m.into()); + // Inverted, the grid covers nearly every pixel, so the check does not depend on where the + // lines fall. + let set = [("width", Value::Scalar(10.0)), ("border", Value::Scalar(2.0)), ("invertGrid", Value::Bool(true))]; + add_effect(&mut p, &mut l, "ec.generate.grid", [200.0, 100.0], &set); + p.comp_mut(cid).unwrap().layers.push(l); + let img = render_frame(&p, cid, Tick::ZERO, 1.0); + for y in 0..100 { + for x in 100..200 { + assert!(img.get(x, y)[3] < 0.01, "grid outside the mask at ({x}, {y}): {:?}", img.get(x, y)); + } + } + assert!(img.get(15, 15)[3] < 0.01, "grid left of and above the mask: {:?}", img.get(15, 15)); + assert!(img.get(55, 55)[3] > 0.5, "grid inside the mask: {:?}", img.get(55, 55)); +} + /// An open mask (the Pen still drawing it) leaves the layer whole; closed, it masks (#290). #[test] fn open_masks_make_no_transparency() { From af50ad43f32a1bd55643da276a6deaa250b0f9a0 Mon Sep 17 00:00:00 2001 From: vangel76 Date: Sat, 7 Nov 2026 12:22:14 +0100 Subject: [PATCH 02/74] Effect Controls: renaming an effect opens with the whole name selected Enter on a selected effect (in Effect Controls, or in the Timeline while Effect Controls shows the effect) focused the effect's rename field before the field existed. The field was then drawn in the same pass as that Enter, took it and gave the keyboard straight back, so it stayed open without the keyboard and with the caret after the name: typing didn't replace the name. The field now takes the keyboard and selects its whole name the pass it is first drawn, as a double-click or the header menu's Rename already opened it, and that pass doesn't commit it. It also always opens on the name the header draws: Enter opened it on the English registry name, so in a translated interface the field showed a different name than the header, and committing it untouched counted as a rename. Fixes #3198 --- .../src/panels/effect_controls/panel.rs | 30 +++++++---- crates/ui-egui/tests/it/ui_effect_controls.rs | 53 +++++++++++++++++++ crates/ui-egui/tests/it/ui_effect_names.rs | 33 +++++++++++- crates/ui-egui/tests/it/ui_group_rename.rs | 3 +- 4 files changed, 106 insertions(+), 13 deletions(-) diff --git a/crates/ui-egui/src/panels/effect_controls/panel.rs b/crates/ui-egui/src/panels/effect_controls/panel.rs index 0857dfa3f..cf12711e3 100644 --- a/crates/ui-egui/src/panels/effect_controls/panel.rs +++ b/crates/ui-egui/src/panels/effect_controls/panel.rs @@ -5,12 +5,14 @@ use super::{Align2, Color32, EffectcraftApp, EvalCtx, GroupKind, Icon, Layer, Pr // --------------------------------------------------------------------------------------------- -/// Open the rename field on effect `uid` (Enter with the effect selected), name selected. +/// Open the rename field on effect `uid` (Enter with the effect selected). It takes the keyboard, +/// its whole name selected, the pass it is first drawn. pub fn start_rename(ctx: &egui::Context, uid: u64, name: &str) { let id = egui::Id::unique(("ec-rename", uid)); - ctx.data_mut(|d| d.insert_temp(id, name.to_string())); - ctx.data_mut(|d| d.insert_temp(id.with("select"), true)); - ctx.memory_mut(|m| m.request_focus(id.with("field"))); + ctx.data_mut(|d| { + d.insert_temp(id, name.to_string()); + d.insert_temp(id.with("start"), true); + }); } /// Where an effect header's parts sit, for its rename field. @@ -29,16 +31,22 @@ fn rename_field(app: &mut EffectcraftApp, ui: &mut egui::Ui, h: &EffectHdr, mut let EffectHdr { g, shown, r, tw, reset, rename_id, layer_id } = *h; let ctx = ui.ctx().clone(); let name_rect = Rect::from_min_max(pos2(tw.max.x + 6.0, r.min.y + 2.0), pos2(reset.min.x - 6.0, r.max.y - 2.0)); - let response = ui.put(name_rect, egui::TextEdit::singleline(&mut value).id(rename_id.with("field")).font(Tokens::semibold(12.0))); - // Focus the field the frame it first exists: focusing an id not yet on screen leaves - // the accessibility tree with a focus it doesn't contain. - if ctx.data_mut(|d| d.remove_temp::(rename_id.with("start"))).is_some() { - crate::widgets::select_all(&ctx, response.id, &value); - response.request_focus(); + // The field opens with the name the header draws (in the interface language), however it + // was opened, so committing it untouched is not a rename. + let opening = ctx.data_mut(|d| d.remove_temp::(rename_id.with("start"))).is_some(); + if opening { + value = shown.to_string(); } + let response = ui.put(name_rect, egui::TextEdit::singleline(&mut value).id(rename_id.with("field")).font(Tokens::semibold(12.0))); app.auto.add(&format!("effectControls.effect.{}.nameEdit", g.uid), name_rect, "Effect name"); - if ctx.data_mut(|d| d.remove_temp::(rename_id.with("select"))).unwrap_or(false) { + // Focus the field the frame it first exists, its whole name selected so typing replaces it: + // focusing an id not yet on screen leaves the accessibility tree with a focus it doesn't + // contain. The Enter that opened it (it is drawn in that pass) doesn't also commit it. + if opening { widgets::select_all(&ctx, response.id, &value); + response.request_focus(); + ctx.data_mut(|d| d.insert_temp(rename_id, value)); + return; } // Escape cancels. egui drops the field's focus on Escape in the same frame, so the // key is checked on the focus being lost as well as held. diff --git a/crates/ui-egui/tests/it/ui_effect_controls.rs b/crates/ui-egui/tests/it/ui_effect_controls.rs index 1db059220..22f9a3d50 100644 --- a/crates/ui-egui/tests/it/ui_effect_controls.rs +++ b/crates/ui-egui/tests/it/ui_effect_controls.rs @@ -866,6 +866,59 @@ fn effect_control_name_can_be_renamed_from_its_header() { assert_eq!(current_name(&h), "Point Control"); } +/// #3198: the effect rename field opens with the whole name selected, so typing replaces it (no +/// Ctrl/Cmd+A first), whether Enter or a double-click on the header opens it, as in After +/// Effects. Enter used to leave the field open without the keyboard, the caret after the name. +#[test] +fn effect_rename_opens_with_the_name_selected() { + let (app, x) = app(); + let mut h = Harness::builder().with_size(egui::vec2(1700.0, 1100.0)).with_step_dt(1.0 / 60.0).build_eframe(|_| app); + settle(&mut h); + let edit = format!("effectControls.effect.{}.nameEdit", x.point_fx); + let current_name = + |h: &Harness<'_, EffectcraftApp>| layer(&h.state().session, x.small).effects().unwrap().groups().find(|g| g.uid == x.point_fx).unwrap().name.clone(); + let type_and_commit = |h: &mut Harness<'_, EffectcraftApp>, text: &str| { + assert!(ids(h).contains(&edit), "the rename field is open"); + assert!(h.ctx.egui_wants_keyboard_input(), "the rename field has the keyboard"); + h.event(egui::Event::Text(text.into())); + h.step(); + h.key_press(egui::Key::Enter); + h.run_steps(3); + assert!(!ids(h).contains(&edit), "Enter closes the rename field"); + }; + // Enter with the effect selected. + h.state_mut().session.execute("prop.select", json!({"layer": x.small, "prop": x.point_fx, "selectKeys": false})).unwrap(); + h.run_steps(3); + h.key_press(egui::Key::Enter); + h.run_steps(3); + type_and_commit(&mut h, "Speed"); + assert_eq!(current_name(&h), "Speed", "the typed text replaced the whole name"); + // A double-click on the effect's name. + let point = rect_of(&h, &format!("effectControls.effect.{}.name", x.point_fx)).center(); + double_click(&mut h, point); + h.run_steps(3); + type_and_commit(&mut h, "Drift"); + assert_eq!(current_name(&h), "Drift", "the typed text replaced the whole name"); + // The header's context menu ▸ Rename. + let header = rect_of(&h, &format!("effectControls.effect.{}", x.point_fx)).center(); + right_click(&mut h, header); + let rename = h.query_by_label("Rename").expect("the effect menu's Rename").rect().center(); + click(&mut h, rename); + h.run_steps(3); + type_and_commit(&mut h, "Wander"); + assert_eq!(current_name(&h), "Wander", "the typed text replaced the whole name"); + // Enter and Enter again without typing: no rename, no undo step. + let undo = h.state().session.history.undo.len(); + h.key_press(egui::Key::Enter); + h.run_steps(3); + assert!(ids(&h).contains(&edit) && h.ctx.egui_wants_keyboard_input(), "Enter opens the rename field with the keyboard"); + h.key_press(egui::Key::Enter); + h.run_steps(3); + assert!(!ids(&h).contains(&edit), "Enter closes the rename field"); + assert_eq!(current_name(&h), "Wander", "the name stays"); + assert_eq!(h.state().session.history.undo.len(), undo, "no undo step"); +} + // Regression test for #1497: double-clicking an effect parameter's name in Effect Controls did // nothing; it should reveal that property in the Timeline, as After Effects does. #[test] diff --git a/crates/ui-egui/tests/it/ui_effect_names.rs b/crates/ui-egui/tests/it/ui_effect_names.rs index d57f9c581..01b57178b 100644 --- a/crates/ui-egui/tests/it/ui_effect_names.rs +++ b/crates/ui-egui/tests/it/ui_effect_names.rs @@ -9,7 +9,7 @@ use effectcraft_ui_egui::dock::PanelKind; use effectcraft_ui_egui::native_menu::{NativeNode, build as native_menu}; use egui::vec2; use egui_kittest::Harness; -use egui_kittest::kittest::Queryable; +use egui_kittest::kittest::{NodeT, Queryable}; use serde_json::json; /// The effect under test, and the names the registry and the catalog give it. @@ -122,6 +122,37 @@ fn the_effect_context_menu_follows_the_interface_language() { } } +/// #3198: Enter opens an effect's rename field on its translated name, as a double-click does, +/// so Enter again without typing renames nothing: the effect keeps its English group name (and +/// so its translation) and there is no undo step. +#[test] +fn renaming_a_translated_effect_without_typing_keeps_its_name() { + let (mut h, fx) = harness("zh-hans"); + let name = |h: &Harness<'_, EffectcraftApp>| { + let s = &h.state().session; + let layer = s.state.selected_layers.first().copied().expect("the selected layer"); + s.active_comp().and_then(|c| c.layer(layer)).and_then(|l| l.props.find_group(fx)).map(|g| g.name.clone()) + }; + let edit = format!("effectControls.effect.{fx}.nameEdit"); + let editing = |h: &Harness<'_, EffectcraftApp>| h.state().auto.elements.iter().any(|e| e.id == edit); + let layer = h.state().session.state.selected_layers.first().map(|l| l.0).expect("the selected layer"); + h.state_mut().session.execute("prop.select", json!({"layer": layer, "prop": fx, "selectKeys": false})).unwrap(); + h.run_steps(3); + let undo = h.state().session.history.undo.len(); + h.key_press(egui::Key::Enter); + h.run_steps(3); + assert!(editing(&h), "Enter opens the rename field"); + assert!(h.ctx.egui_wants_keyboard_input(), "the rename field has the keyboard"); + let field = h.query_by(|n| n.is_focused()).and_then(|n| n.accesskit_node().value()); + assert_eq!(field.as_deref(), Some(BLUR_ZH), "the field opens on the drawn (Chinese) name"); + h.key_press(egui::Key::Enter); + h.run_steps(3); + assert!(!editing(&h), "Enter closes the rename field"); + assert_eq!(name(&h).as_deref(), Some(BLUR_EN), "the effect keeps its name"); + assert_eq!(h.state().session.history.undo.len(), undo, "no undo step"); + assert!(drawn(&h, BLUR_ZH) > 0, "the header still draws the Chinese name"); +} + /// Everything an agent or a script addresses stays English: the automation label, the property /// tree's group name and `effect.list`. #[test] diff --git a/crates/ui-egui/tests/it/ui_group_rename.rs b/crates/ui-egui/tests/it/ui_group_rename.rs index cf56ac6f4..999ed1e3e 100644 --- a/crates/ui-egui/tests/it/ui_group_rename.rs +++ b/crates/ui-egui/tests/it/ui_group_rename.rs @@ -63,7 +63,8 @@ fn rename_with_enter(h: &mut Harness<'_, EffectcraftApp>, layer: u64, uid: u64, select(h, layer, uid); key(h, Key::Enter); assert!(editing(h, uid), "Enter opens a rename field on group {uid}"); - h.key_press_modifiers(Modifiers::COMMAND, Key::A); + // The field has the keyboard with the whole name selected: typing replaces it (#3198). + assert!(h.ctx.egui_wants_keyboard_input(), "the rename field on group {uid} has the keyboard"); h.event(Event::Text(new.into())); h.step(); key(h, Key::Enter); From 82cef516176f5f9c0aa38f4a78806be0b59b4948 Mon Sep 17 00:00:00 2001 From: vangel76 Date: Sat, 7 Nov 2026 12:24:04 +0100 Subject: [PATCH 03/74] Frame blending averages the source frames a comp frame skips over (#3180) Footage interpreted at a much higher frame rate than the comp (for example a 60 fps clip interpreted as 600 fps in a 25 fps comp) showed no frame blending. Frame Mix only cross-faded the two source frames around the comp frame's source time, and only when that time fell between frames. With a whole-number ratio every comp frame lands exactly on a source frame, so nothing blended, and at other ratios at most two of the many skipped frames were used. When a comp frame runs through at least four source frames (conformed rate, time stretch or time remap), Frame Mix and Pixel Motion now average the source frames that comp frame spans, each weighted by how much of it the span covers; spans over 16 frames are sampled at 16 evenly spread frames. Played backwards, the span ends with the comp frame's own source frame. The speed is measured locally (over 1/16 of a comp frame) and must agree with the difference to the next comp frame, so a time remap jump (a hold followed by a cut, a loop wrapping back) stays crisp instead of smearing across the jump. Reduced-resolution previews mix frames at the reduced size. Below four source frames per comp frame (slow-downs, equal rates, 60p in a 30, 25 or 24 fps comp, 50p in 25) nothing changes. The threshold and the 16-frame cap are named constants, pending a comparison with After Effects. The layer cache key, and the key of a layer that selects a frame-blended footage layer as an effect input, now include how far the source runs during the comp frame, which the mix depends on. Fixes #3180 --- crates/render/src/cache.rs | 10 ++ crates/render/src/cache/dependencies.rs | 5 +- crates/render/src/frames.rs | 78 +++++++++++ crates/render/src/lib.rs | 2 +- crates/render/src/pipeline.rs | 91 ++++++++++-- crates/render/src/tests/frame_blend.rs | 175 +++++++++++++++++++++++- 6 files changed, 341 insertions(+), 20 deletions(-) diff --git a/crates/render/src/cache.rs b/crates/render/src/cache.rs index cf1a84e82..b6c38f80d 100644 --- a/crates/render/src/cache.rs +++ b/crates/render/src/cache.rs @@ -477,6 +477,15 @@ fn group_has(g: &PropGroup, match_id: &str) -> bool { }) } +/// A frame-blended layer's pixels also depend on how far its source runs during the comp frame: +/// a fast-conformed clip mixes every source frame the comp frame spans (#3180), so a time +/// stretch or remap change can alter the pixels at an unchanged source time. +fn hash_blend_spans(h: &mut KeyHasher, ctx: &EvalCtx, layer: &Layer) { + if crate::frames::frame_blend_mode(ctx, layer) != effectcraft_project::FrameBlend::Off { + crate::frames::blend_spans(ctx, layer).hash(h); + } +} + /// Feed every property of `g` that can change over time (keyframes or expression) as its value /// at the context time. The static structure is hashed separately. /// Mix the collapsed-instance overrides that target `id` into a cache key. They replace values @@ -646,6 +655,7 @@ fn key_any(ctx: &EvalCtx, layer: &Layer, scale: f64, draft: bool, blur: bool, fo } // Frame blending mixes neighbouring source frames. ctx.comp.enable_frame_blending.hash(&mut h); + hash_blend_spans(&mut h, ctx, layer); } _ => return None, } diff --git a/crates/render/src/cache/dependencies.rs b/crates/render/src/cache/dependencies.rs index db1465c07..c8c483f99 100644 --- a/crates/render/src/cache/dependencies.rs +++ b/crates/render/src/cache/dependencies.rs @@ -165,7 +165,10 @@ impl Dependencies<'_> { hash_debug(self.h, &source); if let Some(source) = source { match &source.kind { - ItemKind::Footage(_) => ctx.source_time(layer).0.hash(self.h), + ItemKind::Footage(_) => { + ctx.source_time(layer).0.hash(self.h); + super::hash_blend_spans(self.h, ctx, layer); + } ItemKind::Comp(comp) => { let time = ctx.source_time(layer); time.0.hash(self.h); diff --git a/crates/render/src/frames.rs b/crates/render/src/frames.rs index 20c655694..7d072438c 100644 --- a/crates/render/src/frames.rs +++ b/crates/render/src/frames.rs @@ -89,6 +89,84 @@ pub fn frame_position(t: Tick, rate: FrameRate) -> (i64, f64) { (num.div_euclid(den) as i64, num.rem_euclid(den) as f64 / den as f64) } +/// The fewest source frames one comp frame must run through before frame blending averages all +/// of them ([`mix_window`]); below this the two neighbouring frames blend as they always have. +/// Conservative on purpose: common footage (60p in a 30, 25 or 24 fps comp, 50p in 25) keeps +/// its familiar look, while fast-conformed footage (600 fps in a 25 fps comp, #3180) gets the +/// mix. Pending a comparison with After Effects, whose exact threshold is not documented. +pub const MIN_MIX_FRAMES: f64 = 4.0; + +/// The most source frames one comp frame mixes; a wider span is sampled at this many evenly +/// spaced frames (After Effects is reported to blend about a dozen; this also bounds the +/// decoding per comp frame). Pending a comparison with After Effects. +pub const MAX_MIX_FRAMES: usize = 16; + +/// A span of more source frames than this in one comp frame is a jump (a time remap cut), not +/// motion to blur. +pub const MAX_MIX_SPAN: f64 = 1000.0; + +/// How finely [`blend_spans`] probes the source's speed: at 1/16 of a comp frame. +const SPEED_PROBE: i64 = 16; + +/// How far a frame-blended layer's source runs during one comp frame from the context time +/// (Interpret Footage's conformed rate, time stretch and time remap all show up in it), as +/// `(local, whole)`: the local speed just after the comp frame's time scaled to a whole frame, +/// and the actual difference to the next comp frame. They disagree across a time remap jump +/// (a hold keyframe followed by a cut, a `loopOut` wrapping back), which then does not mix. +/// Measured on the shown source time ([`EvalCtx::nested_time`]: a precomp proxy that preserves +/// its frame rate is floored to its frames). +pub(crate) fn blend_spans(ctx: &EvalCtx, layer: &Layer) -> (Tick, Tick) { + let frame = ctx.comp.frame_duration(); + let t0 = ctx.nested_time(layer); + let probe = Tick(frame.0 / SPEED_PROBE); + let local = ctx.at(ctx.time + probe).nested_time(layer) - t0; + let whole = ctx.at(ctx.time + frame).nested_time(layer) - t0; + (Tick(local.0.saturating_mul(SPEED_PROBE)), whole) +} + +/// The source frames a frame-blended comp frame averages when the layer runs through many +/// source frames per comp frame (footage interpreted at a much higher frame rate than the comp, +/// a sped-up time stretch or time remap), so a comp frame skips whole source frames: the frames +/// at `rate` that one comp frame's worth of source from `t` overlaps, each weighted by the share +/// of a frame it covers. `spans` is [`blend_spans`]' `(local, whole)`; playing forwards the span +/// is `[t, t + span)`, backwards it ends with the frame at `t` (the unblended frame). `None` — +/// the two neighbouring frames then blend ([`frame_position`], [`blend_frames`]) — when the span +/// is shorter than [`MIN_MIX_FRAMES`], longer than [`MAX_MIX_SPAN`], or the local speed and the +/// whole frame's difference disagree by more than a source frame (a time remap jump). +pub fn mix_window(t: Tick, spans: (Tick, Tick), rate: FrameRate) -> Option> { + if rate.num <= 0 || rate.den <= 0 { + return None; + } + let frames_of = |s: Tick| s.0 as f64 * rate.num as f64 / (TICKS_PER_SECOND as f64 * rate.den as f64); + let width = frames_of(spans.0); + let whole = frames_of(spans.1); + if !width.is_finite() || !whole.is_finite() || (width - whole).abs() > 1.0 + 1e-9 { + return None; + } + if width.abs() < MIN_MIX_FRAMES - 1e-9 || width.abs() > MAX_MIX_SPAN { + return None; + } + let (i, w) = frame_position(t, rate); + let p = i as f64 + w; + let lo = if width < 0.0 { p + 1.0 + width } else { p }; + let width = width.abs(); + let hi = lo + width; + let first = lo.floor(); + let n = hi.ceil() - first; + if n <= MAX_MIX_FRAMES as f64 { + let frames = (0..n as usize) + .filter_map(|k| { + let j = first + k as f64; + let cover = (j + 1.0).min(hi) - j.max(lo); + (cover > 1e-9).then_some((j as i64, cover)) + }) + .collect(); + return Some(frames); + } + let m = MAX_MIX_FRAMES as f64; + Some((0..MAX_MIX_FRAMES).map(|k| ((lo + width * (k as f64 + 0.5) / m).floor() as i64, 1.0)).collect()) +} + /// Blend two neighbouring frames `w` of the way from `a` to `b`: a cross-fade (Frame Mix) or a /// motion-compensated interpolation (Pixel Motion). pub fn blend_frames(mode: FrameBlend, a: &Image, b: &Image, w: f32) -> Image { diff --git a/crates/render/src/lib.rs b/crates/render/src/lib.rs index dbd948320..0a8476344 100644 --- a/crates/render/src/lib.rs +++ b/crates/render/src/lib.rs @@ -49,7 +49,7 @@ pub use accel::{Accelerator, Backend, FxStep, FxTarget}; pub use footage::{CancellableFootage, FloatCurve, FootageSource, NoFootage, is_vector_footage, missing_item_problem}; pub use frames::{ adaptive_samples, blend_frames, cineon_frame, content_bounds, extended_region, field_frame, frame_position, frame_size, interpret_pixels, kurbo_path, - render_frame, roi_inside, + mix_window, render_frame, roi_inside, }; #[cfg(test)] use fx_host::FxHost; diff --git a/crates/render/src/pipeline.rs b/crates/render/src/pipeline.rs index e84348fdf..6012a7404 100644 --- a/crates/render/src/pipeline.rs +++ b/crates/render/src/pipeline.rs @@ -50,6 +50,23 @@ pub struct Collapsed { pub(crate) media_overrides: Vec, } +/// Cache key of a frame-blended footage frame: `seed` (which frames, how they mix, the footage +/// file) and the Interpret Footage settings that change the decoded pixels. +fn blended_frame_key(f: &Footage, seed: impl std::hash::Hash) -> u64 { + use std::hash::{Hash, Hasher}; + let mut h = std::collections::hash_map::DefaultHasher::new(); + seed.hash(&mut h); + (f.alpha as u8).hash(&mut h); + if f.alpha == effectcraft_project::AlphaMode::Premultiplied { + f.premul_color.map(f32::to_bits).hash(&mut h); + } + if f.cineon_applies() { + let c = &f.cineon; + [c.converted_black, c.converted_white, c.black, c.white, c.gamma, c.rolloff].map(f32::to_bits).hash(&mut h); + } + h.finish() +} + /// The property and media overrides of a collapsed precomp layer. `None` when its Essential /// Properties group or the source comp's definitions are missing (the flattened path then /// preserves the old behaviour). @@ -848,23 +865,16 @@ impl<'a> Renderer<'a> { let mode = frame_blend_mode(ctx, layer); let float = self.pipe.float_curve(f); if mode != FrameBlend::Off && matches!(f.kind, FootageKind::Video | FootageKind::Sequence) { + // Many source frames per comp frame (footage interpreted much faster than the comp, + // #3180): the frames the comp frame spans are averaged. + if let Some(img) = self.mixed_span_frame(ctx, layer, item, f, t, size) { + return Some(img); + } let (i, w) = frame_position(t, f.frame_rate); if w > 1e-6 { // Blended frames are cached (Pixel Motion is expensive). - let key = self.cache.map(|_| { - use std::hash::{Hash, Hasher}; - let mut h = std::collections::hash_map::DefaultHasher::new(); - (0x00f8_a3e5_u64, item, i, (w as f32).to_bits(), mode as u8, f.frame_rate.num, f.frame_rate.den, &f.path, float).hash(&mut h); - (f.alpha as u8).hash(&mut h); - if f.alpha == effectcraft_project::AlphaMode::Premultiplied { - f.premul_color.map(f32::to_bits).hash(&mut h); - } - if f.cineon_applies() { - let c = &f.cineon; - [c.converted_black, c.converted_white, c.black, c.white, c.gamma, c.rolloff].map(f32::to_bits).hash(&mut h); - } - h.finish() - }); + let seed = (0x00f8_a3e5_u64, item, i, (w as f32).to_bits(), mode as u8, f.frame_rate.num, f.frame_rate.den, &f.path, float); + let key = self.cache.map(|_| blended_frame_key(f, seed)); if let (Some(c), Some(k)) = (self.cache, key) && let Some(b) = c.get(k) { @@ -885,6 +895,59 @@ impl<'a> Renderer<'a> { self.footage.frame_with(item, f, t, size, float).map(|img| cineon_frame(img, f)) } + /// A frame-blended footage frame at source time `t` when one comp frame runs through many + /// source frames: the average of the source frames the comp frame spans, each weighted by + /// the share of it the span covers ([`super::frames::mix_window`]). Both Frame Mix and + /// Pixel Motion average real frames here (there is no in-between frame to synthesise). + /// `size`: the frames may come resampled to it ([`FootageSource::frame_at_size`]). `None` + /// when the span is too short (or a jump) to mix, or no frame decodes. + fn mixed_span_frame(&self, ctx: &EvalCtx, layer: &Layer, item: ItemId, f: &Footage, t: Tick, size: Option<(u32, u32)>) -> Option> { + use rayon::prelude::*; + let float = self.pipe.float_curve(f); + let spans = super::frames::blend_spans(ctx, layer); + let frames = super::frames::mix_window(t, spans, f.frame_rate)?; + let seed = (0x00f8_a3e6_u64, item, t.0, spans, size, f.frame_rate.num, f.frame_rate.den, &f.path, float); + let key = self.cache.map(|_| blended_frame_key(f, seed)); + if let (Some(c), Some(k)) = (self.cache, key) + && let Some(b) = c.get(k) + { + return Some(Arc::new(b.img.clone())); + } + // A weighted sum in one buffer, divided by the total weight at the end. A frame that does + // not decode, or has another size than the first (a sequence of mixed sizes), is left + // out and the mix renormalises over the rest. Log scans are converted before they mix. + let mut acc: Option = None; + let mut total = 0.0; + for (j, w) in frames { + let Some(img) = self.footage.frame_with(item, f, f.frame_rate.tick_of(j), size, float).map(|a| cineon_frame(a, f)) else { continue }; + let sum = acc.get_or_insert_with(|| Image::new(img.width, img.height)); + if (sum.width, sum.height) != (img.width, img.height) { + continue; + } + let wf = w as f32; + sum.data.par_iter_mut().zip(img.data.par_iter()).for_each(|(s, p)| { + for (a, b) in s.iter_mut().zip(p) { + *a += b * wf; + } + }); + total += w; + } + let mut img = acc?; + if total <= 1e-9 { + return None; + } + let norm = (1.0 / total) as f32; + img.data.par_iter_mut().for_each(|s| { + for c in s.iter_mut() { + *c *= norm; + } + }); + if let (Some(c), Some(k)) = (self.cache_to_keep(), key) { + c.insert(k, Arc::new(Buf { img: img.clone(), offset: [0.0; 2], scale: 1.0 })); + } + Some(Arc::new(img)) + } + /// Source → masks, clamped/quantised to the project depth. fn masked_source(&self, ctx: &EvalCtx, layer: &Layer) -> Option { let mut buf = match self.content_times(ctx, layer) { diff --git a/crates/render/src/tests/frame_blend.rs b/crates/render/src/tests/frame_blend.rs index ac3555af1..e80731d35 100644 --- a/crates/render/src/tests/frame_blend.rs +++ b/crates/render/src/tests/frame_blend.rs @@ -5,12 +5,13 @@ use std::sync::Arc; use rayon::prelude::*; use effectcraft_color::Label; -use effectcraft_keyframe::{Keyframe, Value}; +use effectcraft_keyframe::{Interp, Keyframe, Value}; use effectcraft_project::build; use effectcraft_project::{AlphaMode, BitDepth, Comp, Footage, FootageKind, FrameBlend, ItemId, ItemKind, LayerSource, Project, Solid}; use effectcraft_time::{FrameRate, Tick}; -use crate::{FootageSource, Image, RenderOpts, Renderer, frame_position}; +use crate::frames::MAX_MIX_FRAMES; +use crate::{FootageSource, Image, RenderOpts, Renderer, frame_position, mix_window}; #[test] fn frame_mix_weights_for_24_to_30_conform() { @@ -58,6 +59,11 @@ impl FootageSource for Frames { } fn setup(mode: FrameBlend, size: (u32, u32)) -> (Project, ItemId) { + setup_rated(mode, size, FrameRate::FPS_24, None) +} + +/// [`setup`] with footage at `rate` (its Interpret Footage frame rate) recorded at `native`. +fn setup_rated(mode: FrameBlend, size: (u32, u32), rate: FrameRate, native: Option) -> (Project, ItemId) { let mut p = Project::default(); p.settings.bit_depth = BitDepth::Bpc32; let comp = Comp::new(size.0, size.1, FrameRate::FPS_30, Tick::from_seconds_f64(2.0)); @@ -68,8 +74,8 @@ fn setup(mode: FrameBlend, size: (u32, u32)) -> (Project, ItemId) { width: size.0, height: size.1, pixel_aspect: 1.0, - frame_rate: FrameRate::FPS_24, - native_rate: None, + frame_rate: rate, + native_rate: native, duration: Tick::from_seconds_f64(2.0), has_video: true, has_audio: false, @@ -168,6 +174,167 @@ fn time_stretched_precomp_frame_mixes() { assert!(img.get(12, 10)[3] < 1e-3 && (img.get(17, 10)[3] - 1.0).abs() < 1e-3 && (img.get(22, 10)[3] - 1.0).abs() < 1e-3 && img.get(27, 10)[3] < 1e-3); } +#[test] +fn frame_mix_averages_every_frame_of_fast_conformed_footage() { + // #3180: 30 fps footage interpreted as 300 fps in a 30 fps comp. Every comp frame lands + // exactly on a source frame, but it spans ten of them: frame blending averages all ten + // (greys 0.0 … 0.9 → 0.45), not just the frame it starts on (0.0). + let fast = FrameRate::new(300, 1); + let (p, cid) = setup_rated(FrameBlend::FrameMix, (16, 8), fast, Some(FrameRate::FPS_30)); + let v = frame(&p, cid, 0, false).get(4, 4)[0]; + assert!((v - 0.45).abs() < 1e-5, "{v}"); + // Pixel Motion has no in-between frame to make either: it averages the same frames. + let (p, cid) = setup_rated(FrameBlend::PixelMotion, (16, 8), fast, Some(FrameRate::FPS_30)); + let v = frame(&p, cid, 0, false).get(4, 4)[0]; + assert!((v - 0.45).abs() < 1e-5, "{v}"); + // Off (layer switch or comp switch): the one frame at the comp frame's time. + let (p, cid) = setup_rated(FrameBlend::Off, (16, 8), fast, Some(FrameRate::FPS_30)); + assert!(frame(&p, cid, 0, false).get(4, 4)[0].abs() < 1e-6); + let (mut p, cid) = setup_rated(FrameBlend::FrameMix, (16, 8), fast, Some(FrameRate::FPS_30)); + p.comp_mut(cid).unwrap().enable_frame_blending = false; + assert!(frame(&p, cid, 0, false).get(4, 4)[0].abs() < 1e-6); +} + +#[test] +fn frame_mix_of_common_footage_is_unchanged() { + // Footage at the comp's rate: each comp frame is exactly one source frame, blended or not. + let (p, cid) = setup_rated(FrameBlend::FrameMix, (16, 8), FrameRate::FPS_30, None); + for k in 0..4 { + let v = frame(&p, cid, k, false).get(4, 4)[0]; + assert!((v - k as f32 / 10.0).abs() < 1e-6, "comp frame {k}: {v}"); + } + // A mild speed-up (36 fps footage in a 30 fps comp) keeps the two-frame cross-fade: comp + // frame 1 → source frame 1.2, 0.8·frame 1 + 0.2·frame 2 = 0.12. + let (p, cid) = setup_rated(FrameBlend::FrameMix, (16, 8), FrameRate::new(36, 1), None); + let v = frame(&p, cid, 1, false).get(4, 4)[0]; + assert!((v - 0.12).abs() < 1e-6, "{v}"); + // 60 and 90 fps in a 30 fps comp (two and three source frames per comp frame) stay crisp: + // comp frame 1 is source frame 2 or 3, as before. + for (rate, want) in [(FrameRate::FPS_60, 0.2), (FrameRate::new(90, 1), 0.3)] { + let (p, cid) = setup_rated(FrameBlend::FrameMix, (16, 8), rate, None); + let v = frame(&p, cid, 1, false).get(4, 4)[0]; + assert!((v - want).abs() < 1e-6, "{rate:?}: {v}"); + } +} + +/// Adds Time Remap with `keys` (layer time → source seconds) to the comp's footage layer. +fn remap(p: &mut Project, cid: ItemId, keys: Vec) { + let mut next = p.next_id; + let mut tr = build::Ids(&mut next).prop("timeRemap", "Time Remap", Value::Scalar(0.0)); + p.next_id = next; + tr.keys = keys; + p.comp_mut(cid).unwrap().layers[0].props.children.push(effectcraft_project::Node::Prop(tr)); +} + +#[test] +fn time_remap_jumps_stay_crisp() { + let fast = FrameRate::new(300, 1); + let s = |frames: f64| Value::Scalar(frames / 300.0); + let at = |k: i64| FrameRate::FPS_30.tick_of(k); + // Hold on source frame 0, then cut to source frame 10 at comp frame 1: comp frame 0 is a + // still frame, not a smear of the ten frames the cut skips. + let (mut p, cid) = setup_rated(FrameBlend::FrameMix, (16, 8), fast, Some(FrameRate::FPS_30)); + remap(&mut p, cid, vec![Keyframe::new(at(0), s(0.0)).hold(), Keyframe::new(at(1), s(10.0)).hold()]); + let v = frame(&p, cid, 0, false).get(4, 4)[0]; + assert!(v.abs() < 1e-6, "hold then cut: {v}"); + // A loop wrapping back (like `loopOut("cycle")`): from source frame 10 at normal speed, + // back to frame 0 by the next comp frame. Comp frame 0 shows frame 10 (grey 1.0), not + // frames 1 … 10 run backwards. + let (mut p, cid) = setup_rated(FrameBlend::FrameMix, (16, 8), fast, Some(FrameRate::FPS_30)); + let mut top = Keyframe::new(FrameRate::FPS_60.tick_of(1), s(15.0)); + top.out_interp = Interp::Hold; + remap(&mut p, cid, vec![Keyframe::new(at(0), s(10.0)), top, Keyframe::new(at(1), s(0.0))]); + let v = frame(&p, cid, 0, false).get(4, 4)[0]; + assert!((v - 1.0).abs() < 1e-6, "loop wrap: {v}"); + // A remap running at normal speed through fast-conformed footage still mixes its frames. + let (mut p, cid) = setup_rated(FrameBlend::FrameMix, (16, 8), fast, Some(FrameRate::FPS_30)); + remap(&mut p, cid, vec![Keyframe::new(at(0), s(0.0)), Keyframe::new(at(30), s(300.0))]); + let v = frame(&p, cid, 0, false).get(4, 4)[0]; + assert!((v - 0.45).abs() < 1e-5, "steady remap: {v}"); +} + +#[test] +fn a_frame_blended_dependency_keys_how_far_its_source_runs() { + // A footage layer selected by another layer's effect: at comp frames 0 and 1 its source time + // is the same (0), but at frame 0 it holds and at frame 1 it runs at speed, which mixes ten + // source frames. The owner's cache key must tell the two apart. + let fast = FrameRate::new(300, 1); + let (mut p, cid) = setup_rated(FrameBlend::FrameMix, (16, 8), fast, Some(FrameRate::FPS_30)); + let at = |k: i64| FrameRate::FPS_30.tick_of(k); + let keys = vec![Keyframe::new(at(0), Value::Scalar(0.0)).hold(), Keyframe::new(at(1), Value::Scalar(0.0)), Keyframe::new(at(31), Value::Scalar(1.0))]; + remap(&mut p, cid, keys); + let comp = p.comp(cid).unwrap().clone(); + let footage = comp.layers[0].id; + let sid = p.add_item("Solid", Label::Red, None, ItemKind::Solid(Solid { color: [1.0; 3], width: 16, height: 8, pixel_aspect: 1.0 })); + let mut owner = build::layer(&mut p, &comp, "Solid", LayerSource::Solid { item: sid }, (16, 8), None); + super::add_effect(&mut p, &mut owner, "ec.channel.setmatte", [16.0, 8.0], &[("takeMatteFromLayer", Value::Layer(Some(footage.0)))]); + p.comp_mut(cid).unwrap().layers.push(owner); + let key = |k: i64| { + let c = p.comp(cid).unwrap(); + crate::cache::layer_key(&crate::EvalCtx::new(&p, cid, c, at(k)), &c.layers[1], 1.0, false, false).unwrap() + }; + assert_ne!(key(0), key(1)); +} + +#[test] +fn mix_window_covers_the_source_span_of_one_comp_frame() { + let comp_frame = |r: FrameRate| r.frame_duration(); + let steady = |r: FrameRate| (comp_frame(r), comp_frame(r)); + // Fewer than four source frames per comp frame (slow-down, same rate, 60p in 30, 24 or 25, + // 50p in 25, 90 in 30): the two-frame blend applies, as before. + for (comp, footage) in [ + (FrameRate::FPS_30, FrameRate::FPS_30), + (FrameRate::FPS_30, FrameRate::FPS_24), + (FrameRate::FPS_30, FrameRate::new(36, 1)), + (FrameRate::FPS_25, FrameRate::FPS_30), + (FrameRate::FPS_30, FrameRate::FPS_60), + (FrameRate::FPS_24, FrameRate::FPS_60), + (FrameRate::FPS_25, FrameRate::FPS_60), + (FrameRate::FPS_25, FrameRate::FPS_50), + (FrameRate::FPS_29_97, FrameRate::FPS_59_94), + (FrameRate::FPS_30, FrameRate::new(90, 1)), + ] { + assert_eq!(mix_window(Tick::ZERO, steady(comp), footage), None, "{footage:?} in {comp:?}"); + } + // Four source frames per comp frame: 120 fps in a 30 fps comp mixes all four. + assert_eq!(mix_window(Tick::ZERO, steady(FrameRate::FPS_30), FrameRate::FPS_120), Some(vec![(0, 1.0), (1, 1.0), (2, 1.0), (3, 1.0)])); + // The report's case: 600 fps in a 25 fps comp, 24 source frames per comp frame, sampled at + // 16 evenly spread frames. + let w = mix_window(FrameRate::FPS_25.tick_of(3), steady(FrameRate::FPS_25), FrameRate::new(600, 1)).unwrap(); + assert_eq!(w.len(), MAX_MIX_FRAMES); + assert!(w.iter().all(|x| (72..96).contains(&x.0) && x.1 == 1.0), "{w:?}"); + assert!(w.windows(2).all(|p| p[0].0 < p[1].0), "{w:?}"); + // 150 fps in a 30 fps comp: comp frame 1 spans source frames 5 … 10. + let w = mix_window(FrameRate::FPS_30.tick_of(1), steady(FrameRate::FPS_30), FrameRate::new(150, 1)); + assert_eq!(w, Some((5..10).map(|j| (j, 1.0)).collect())); + // 135 fps in a 30 fps comp: comp frame 1 spans source frames 4.5 … 9.0. + let w = mix_window(FrameRate::FPS_30.tick_of(1), steady(FrameRate::FPS_30), FrameRate::new(135, 1)).unwrap(); + assert_eq!(w.iter().map(|x| x.0).collect::>(), vec![4, 5, 6, 7, 8]); + for (got, want) in w.iter().map(|x| x.1).zip([0.5, 1.0, 1.0, 1.0, 1.0]) { + assert!((got - want).abs() < 1e-9, "{w:?}"); + } + // Playing backwards: the span ends with the comp frame's own source frame. + let back = -comp_frame(FrameRate::FPS_30); + let w = mix_window(FrameRate::FPS_30.tick_of(1), (back, back), FrameRate::new(300, 1)).unwrap(); + assert_eq!(w.iter().map(|x| x.0).collect::>(), (1..=10).collect::>()); + // A hold (no local speed) or a jump (local speed and the whole frame disagree) does not mix. + let f = comp_frame(FrameRate::FPS_30); + assert_eq!(mix_window(Tick::ZERO, (Tick::ZERO, f), FrameRate::new(300, 1)), None); + assert_eq!(mix_window(Tick::ZERO, (f, -f), FrameRate::new(300, 1)), None); + // A long span is sampled at a bounded number of frames; a span beyond any footage's speed is + // a jump. + let ten = Tick::from_seconds_f64(10.0); + let w = mix_window(Tick::ZERO, (ten, ten), FrameRate::FPS_60).unwrap(); + assert_eq!(w.len(), MAX_MIX_FRAMES); + assert!(w.iter().all(|x| (0..600).contains(&x.0))); + let far = Tick::from_seconds_f64(100.0); + assert_eq!(mix_window(Tick::ZERO, (far, far), FrameRate::FPS_60), None); + // Degenerate rates and huge spans don't panic. + assert_eq!(mix_window(Tick::ZERO, (Tick::MAX, Tick::MAX), FrameRate { num: 0, den: 1 }), None); + assert_eq!(mix_window(Tick::MIN, (Tick::MAX, Tick::MAX), FrameRate::FPS_60), None); + assert_eq!(mix_window(Tick::MAX, (Tick::MIN, Tick::MAX), FrameRate::FPS_60), None); +} + #[test] fn frame_position_survives_degenerate_rates() { for rate in [FrameRate { num: 24, den: 0 }, FrameRate { num: 0, den: 1 }, FrameRate { num: -24, den: 1 }] { From e30f4f64358553eddf2c56051ed7de281ca6d7d7 Mon Sep 17 00:00:00 2001 From: vangel76 Date: Sat, 7 Nov 2026 12:56:59 +0100 Subject: [PATCH 04/74] Vector footage: faster, budget-safe clipping in the SVG/PDF rasteriser Continuously rasterised vector footage (an A4 PDF scaled 200-400 %) is a page of millions of pixels, and the rasteriser did page-sized work per element: - Every path, run of text and image computed and walked a coverage mask the size of the whole page. Coverage and paint now run over the pixels the shape (fill, or stroke outline) or image can reach. A generated A4 page of 720 text-run paths and 60 strokes rasterises at 300 % in 0.18 s instead of 4.97 s (200 %: 0.13 s instead of 2.44 s), with the same pixels. - Every clipping group copied the whole backdrop and drew on a second page-size surface, so deeply nested clips ran past the 512 MB surface budget and their artwork was dropped (at 200 % from the eighth level, at 400 % from the second). A plain clipping group (no soft mask, blend mode, group opacity or knockout, Normal content throughout) is now drawn straight onto the page when every clip is an axis-aligned rectangle covering each pixel the content can reach, and otherwise on a surface the size of its clip. Both give the same pixels; groups with a blend mode or knockout inside keep the backdrop path. Never crash: drawing stops below 256 levels of groups, and the PDF/EPS builder keeps groups at most 200 deep (a crafted page nesting `q ... W n` twenty thousand deep overflowed the stack when the document was drawn or dropped). The 596 x 842 px import size of an A4 page is unchanged: the page in points at 72 dpi, rounded up, as After Effects takes a point as a pixel. Refs #2895 --- crates/host/tests/it/vector_import.rs | 75 +++++++ crates/pdf/src/build.rs | 22 +- crates/pdf/src/tests.rs | 29 +++ crates/svg/src/raster.rs | 290 ++++++++++++++++++++++++-- crates/svg/src/tests.rs | 190 +++++++++++++++++ 5 files changed, 583 insertions(+), 23 deletions(-) diff --git a/crates/host/tests/it/vector_import.rs b/crates/host/tests/it/vector_import.rs index badd60b66..c54c61550 100644 --- a/crates/host/tests/it/vector_import.rs +++ b/crates/host/tests/it/vector_import.rs @@ -596,3 +596,78 @@ fn ai_composition_retains_layer_sizes_and_positions() { assert!(frame.get(39, 10)[0] > 0.99, "reload invalidates cached pixels"); } } + +/// An A4 page (595.28 × 841.89 pt) whose artwork sits inside a page clip and ten nested +/// clipping groups, as PDFs exported from layout and office applications nest them: a black bar +/// whose left edge is at x = 30 pt, from 41.89 to 191.89 pt down the page. +fn a4_clipped_pdf() -> Vec { + let mut content = String::from( + "q 0 0 595.28 841.89 re W n +", + ); + for i in 0..10 { + content.push_str(&format!( + "q {} {} {} {} re W n +", + 20 + i, + 600 + i, + 300 - 2 * i, + 230 - 2 * i + )); + } + content.push_str( + "0 0 0 rg 30 650 40 150 re f +", + ); + content.push_str( + &"Q +" + .repeat(11), + ); + effectcraft_pdf::write::pdf( + &[ + (1, "<< /Type /Catalog /Pages 2 0 R >>".into(), None), + (2, "<< /Type /Pages /Kids [3 0 R] /Count 1 >>".into(), None), + (3, "<< /Type /Page /Parent 2 0 R /MediaBox [0 0 595.28 841.89] /Resources << >> /Contents 4 0 R >>".into(), None), + (4, "<< >>".into(), Some(content.into_bytes())), + ], + 1, + ) +} + +/// #2895: an A4 PDF imports at its size in points, rounded up (596 × 842 px; After Effects +/// also takes a point as a pixel), and scaled 200 % with Continuously Rasterize on it is drawn +/// at that scale: the bar's left edge stays one pixel wide. It used to come out empty: at +/// 200 % (a 32 MB page) each nested clipping group copied the whole page and drew on a second +/// page-size surface, so the eighth level of clips ran past the rasteriser's 512 MB surface +/// budget. With the switch off the layer is the page's pixels enlarged, soft as before. +#[test] +fn a4_pdf_scaled_200_percent_continuously_rasterizes_sharp() { + let path = tmp("a4-clipped.pdf"); + std::fs::write(&path, a4_clipped_pdf()).unwrap(); + let mut s = effectcraft_host::session(); + let r = s.execute_checked("file.import", json!({"paths": [path]})).unwrap(); + assert_eq!(r["errors"], json!([]), "{r}"); + let item = r["items"][0].as_u64().unwrap(); + let effectcraft_project::ItemKind::Footage(f) = &s.project.item(ItemId(item)).unwrap().kind else { panic!("footage") }; + assert_eq!((f.codec.as_str(), f.width, f.height), ("PDF", 596, 842), "A4 in points"); + s.execute("comp.new", json!({"name": "A4", "width": 400, "height": 400, "frameRate": 30, "duration": 1})).unwrap(); + let lid = s.execute_checked("layer.addItem", json!({"item": item})).unwrap()["layer"].as_u64().unwrap(); + for (k, v) in [("transform/anchor", json!([0, 0])), ("transform/position", json!([0, 0])), ("transform/scale", json!([200, 200]))] { + s.execute("prop.set", json!({"layer": lid, "path": k, "value": v})).unwrap(); + } + let cid = s.active_comp_id().unwrap(); + let t = effectcraft_time::Tick::ZERO; + // Row 240 (120 pt down the page) crosses the bar's left edge at x = 60 (30 pt × 2). + let soft_pixels = |img: &Image| (50..70).filter(|&x| (0.05..0.95).contains(&img.get(x, 240)[3])).count(); + let off = s.render(cid, t, RenderOpts::default()); + assert!(off.get(100, 240)[3] > 0.99, "the bar shows with the switch off"); + assert!(soft_pixels(&off) >= 2, "off: the page's pixels enlarged, a soft edge ({} partial pixels)", soft_pixels(&off)); + s.execute("layer.setSwitch", json!({"layers": [lid], "switch": "collapse", "value": true})).unwrap(); + let on = s.render(cid, t, RenderOpts::default()); + assert!(on.get(100, 240)[3] > 0.99, "the clipped bar is drawn at 200 %: {:?}", on.get(100, 240)); + assert!(on.get(61, 240)[3] > 0.99 && on.get(58, 240)[3] < 0.01, "a hard edge at x = 60: {:?} {:?}", on.get(58, 240), on.get(61, 240)); + assert!(soft_pixels(&on) <= 1, "on: the edge is at most one pixel wide ({} partial pixels)", soft_pixels(&on)); + // Outside the clips (x = 10 pt) nothing is drawn. + assert!(on.get(20, 240)[3] < 0.01); +} diff --git a/crates/pdf/src/build.rs b/crates/pdf/src/build.rs index 0b1586f89..497d567a6 100644 --- a/crates/pdf/src/build.rs +++ b/crates/pdf/src/build.rs @@ -7,6 +7,10 @@ use effectcraft_svg::{Affine, BezPath, FillRule, Geom, Group, Node, Paint, Shape /// PostScript program, or the PDF content stream. pub(crate) const MAX_PATH: usize = 1 << 20; +/// The deepest groups (clips, layers, transparency groups) nest in the document; below the +/// renderer's own limit (256), so everything kept is drawn. +pub(crate) const MAX_GROUP_DEPTH: usize = 200; + #[derive(Clone, Copy, PartialEq, Eq)] enum Kind { Plain, @@ -68,17 +72,29 @@ impl Builder { g.clip.push((path, rule)); // Clipping is not a transparency group: blend modes inside reach the backdrop. g.isolated = false; - self.stack.push((g, Kind::Clip)); + self.push_group(g, Kind::Clip); } /// Open an optional-content layer (or a named group). pub fn open(&mut self, name: &str, layer: bool) { - self.stack.push((group(name), if layer { Kind::Layer } else { Kind::Plain })); + self.push_group(group(name), if layer { Kind::Layer } else { Kind::Plain }); } /// Open a group (blend mode, soft mask) closed with the graphics state. pub fn open_group(&mut self, g: Group) { - self.stack.push((g, Kind::Plain)); + self.push_group(g, Kind::Plain); + } + + /// Open a group, unless groups are already nested [`MAX_GROUP_DEPTH`] deep: the document + /// is a tree that is drawn, measured, copied and dropped recursively, and a crafted file + /// can nest `q … W n` without end. Deeper groups are left out (their content is drawn + /// in the group around them) and the document says so. + fn push_group(&mut self, g: Group, kind: Kind) { + if self.stack.len() >= MAX_GROUP_DEPTH { + self.skip("groups nested deeper than 200 levels"); + return; + } + self.stack.push((g, kind)); } /// Add a finished node (an image, pattern tiles). diff --git a/crates/pdf/src/tests.rs b/crates/pdf/src/tests.rs index 2d09a7406..275e7021f 100644 --- a/crates/pdf/src/tests.rs +++ b/crates/pdf/src/tests.rs @@ -1139,3 +1139,32 @@ fn cmap_entries_at_the_top_of_the_code_range_do_not_overflow() { // Code 0 is CID u32::MAX; code 2 would be past it, so it falls back to CID 0 like an unmapped code. assert_eq!((f.width(0), f.width(2)), (0.5, 0.7)); } + +/// Never crash: a crafted page nests `q … W n` twenty thousand deep. The document is a tree +/// that is drawn, measured and dropped recursively, so groups past the nesting limit are left +/// out (said in the skip notes) and the page parses, draws and drops on a thread with a small +/// stack; the artwork inside them still draws, clipped by the groups that were kept. +#[test] +fn clip_groups_nested_without_end_are_capped() { + let run = std::thread::Builder::new().stack_size(2 << 20).spawn(|| { + let n = 20_000; + let mut content = "q 0 0 50 50 re W n\n".repeat(n); + content.push_str("0 0 1 rg 0 0 50 50 re f\n"); + content.push_str(&"Q\n".repeat(n)); + let bytes = pdf( + &[ + (1, "<< /Type /Catalog /Pages 2 0 R >>".to_string(), None), + (2, "<< /Type /Pages /Kids [3 0 R] /Count 1 >>".to_string(), None), + (3, "<< /Type /Page /Parent 2 0 R /MediaBox [0 0 100 100] /Contents 4 0 R >>".to_string(), None), + (4, "<< >>".to_string(), Some(content.into_bytes())), + ], + 1, + ); + let doc = parse(&bytes).unwrap(); + (doc.skipped.clone(), px(&doc, 25, 75), px(&doc, 75, 25)) + }); + let (skipped, inside, outside) = run.unwrap().join().unwrap(); + assert!(skipped.iter().any(|s| s.contains("nested deeper")), "{skipped:?}"); + assert!(inside[2] > 0.99 && inside[3] > 0.99, "the artwork draws inside the clips: {inside:?}"); + assert!(outside[3] < 0.01, "and is clipped: {outside:?}"); +} diff --git a/crates/svg/src/raster.rs b/crates/svg/src/raster.rs index 568b10b25..59deae964 100644 --- a/crates/svg/src/raster.rs +++ b/crates/svg/src/raster.rs @@ -20,7 +20,13 @@ pub fn rasterize(doc: &Doc, w: u32, h: u32, scale: f64) -> Image { /// Rasterise `doc` with `m` mapping document pixels to image pixels. pub fn rasterize_with(doc: &Doc, w: u32, h: u32, m: Affine) -> Image { - let mut budget = SurfaceBudget { live_bytes: 0, limit_bytes: MAX_LIVE_SURFACE_BYTES }; + rasterize_with_budget(doc, w, h, m, MAX_LIVE_SURFACE_BYTES) +} + +/// [`rasterize_with`] with at most `limit_bytes` of image surfaces (the destination included) +/// live at once. +pub(crate) fn rasterize_with_budget(doc: &Doc, w: u32, h: u32, m: Affine, limit_bytes: usize) -> Image { + let mut budget = SurfaceBudget { live_bytes: 0, limit_bytes, depth: 0, plain: false }; let Some(mut img) = budget.image(w, h, None) else { return Image::default() }; draw_group(&doc.root, m, 1.0, &mut img, &mut budget); img @@ -29,9 +35,19 @@ pub fn rasterize_with(doc: &Doc, w: u32, h: u32, m: Affine) -> Image { /// Bound the simultaneously live offscreen image surfaces created by nested groups. const MAX_LIVE_SURFACE_BYTES: usize = 512 * 1024 * 1024; +/// The deepest group nesting drawn: drawing recurses, and a crafted document can nest groups +/// without end, so groups deeper than this are not drawn. +const MAX_GROUP_DEPTH: usize = 256; + +/// The state of one rasterisation: the surface budget and where the drawing is in the tree. struct SurfaceBudget { live_bytes: usize, limit_bytes: usize, + /// Groups entered on the way to the one being drawn. + depth: usize, + /// Every group being drawn below the current one composites Normal, without knockout + /// (known once for a whole subtree, so it isn't walked again at every level). + plain: bool, } impl SurfaceBudget { @@ -61,6 +77,15 @@ impl SurfaceBudget { } fn draw_group(g: &Group, m: Affine, opacity: f64, dst: &mut Image, budget: &mut SurfaceBudget) { + if budget.depth >= MAX_GROUP_DEPTH { + return; + } + budget.depth += 1; + draw_group_entered(g, m, opacity, dst, budget); + budget.depth -= 1; +} + +fn draw_group_entered(g: &Group, m: Affine, opacity: f64, dst: &mut Image, budget: &mut SurfaceBudget) { let m = m * g.transform; let o = opacity * g.opacity; if o <= 0.0 { @@ -77,9 +102,192 @@ fn draw_group(g: &Group, m: Affine, opacity: f64, dst: &mut Image, budget: &mut } return; } + let only_clip = !g.clip.is_empty() && g.mask.is_none() && g.blend == BlendMode::Normal && o >= 1.0 && !g.knockout; + let plain = only_clip && (budget.plain || !needs_backdrop(&g.children, MAX_GROUP_DEPTH.saturating_sub(budget.depth))); + if plain { + let outer = std::mem::replace(&mut budget.plain, true); + let left = MAX_GROUP_DEPTH.saturating_sub(budget.depth); + if clips_cover_content(g, m, dst.width, dst.height, left) { + for c in &g.children { + draw_node(c, m, 1.0, dst, budget); + } + } else { + draw_clip_local(g, m, dst, budget); + } + budget.plain = outer; + return; + } draw_offscreen_group(g, m, o, dst, budget); } +/// Whether every clip of `g` is a rectangle along the pixel grid's axes that fully covers each +/// pixel of a `w`×`h` image its children can draw on: the clips change nothing there, so the +/// children are drawn straight onto the image, without a surface (a page or artboard clip +/// around the artwork inside it, the outermost groups of most PDF pages). `levels`: how many +/// levels of groups below are drawn. +fn clips_cover_content(g: &Group, m: Affine, w: u32, h: u32, levels: usize) -> bool { + let Some(rects) = g.clip.iter().map(|(p, _)| axis_rect(p, m)).collect::>>() else { return false }; + // Children that draw nothing need no clip. + let Some(b) = draw_bounds(&g.children, m, levels) else { return true }; + let (fw, fh) = (f64::from(w), f64::from(h)); + // The pixels the children can touch (a pixel of margin for anti-aliasing), on the image. + let (bx0, by0) = ((b.x0.floor() - 1.0).max(0.0), (b.y0.floor() - 1.0).max(0.0)); + let (bx1, by1) = ((b.x1.ceil() + 1.0).min(fw), (b.y1.ceil() + 1.0).min(fh)); + if !(bx0 < bx1 && by0 < by1) { + // Nothing reaches the image (or the bounds aren't numbers): keep the clip. + return false; + } + rects.iter().all(|r| r.x0.ceil() <= bx0 && bx1 <= r.x1.floor() && r.y0.ceil() <= by0 && by1 <= r.y1.floor()) +} + +/// A device-space rectangle holding every pixel `nodes` drawn with `m` can cover, `None` when +/// they draw nothing. Cheap rather than tight: a stroke counts as its path's bounds grown by half +/// its width times its miter limit (at least √2, for square caps), and anything it can't +/// bound (more than `levels` levels deep, a transform that isn't finite) as the whole plane. +fn draw_bounds(nodes: &[Node], m: Affine, levels: usize) -> Option { + let everything = kurbo::Rect::new(f64::NEG_INFINITY, f64::NEG_INFINITY, f64::INFINITY, f64::INFINITY); + if !m.as_coeffs().iter().all(|v| v.is_finite()) { + return Some(everything); + } + let mut out: Option = None; + for n in nodes { + let r = match n { + Node::Group(g) if levels == 0 => (!g.children.is_empty()).then_some(everything), + Node::Group(g) => { + let gm = m * g.transform; + let mut r = draw_bounds(&g.children, gm, levels - 1); + for (clip, _) in &g.clip { + r = r.map(|r| r.intersect(gm.transform_rect_bbox(clip.bounding_box()))); + } + r + } + Node::Shape(s) => { + let sm = m * s.transform; + let b = s.geom.to_path().bounding_box(); + let fill = s.fill.as_ref().map(|_| sm.transform_rect_bbox(b)); + let stroke = s.stroke.as_ref().map(|st| { + let pad = (st.width.abs() * 0.5 * st.miter.max(std::f64::consts::SQRT_2)).max(0.0); + sm.transform_rect_bbox(b.inflate(pad, pad)) + }); + match (fill, stroke) { + (Some(a), Some(b)) => Some(a.union(b)), + (a, b) => a.or(b), + } + } + Node::Image(im) => Some((m * im.transform).transform_rect_bbox(kurbo::Rect::new(0.0, 0.0, f64::from(im.width), f64::from(im.height)))), + }; + if let Some(r) = r { + let r = if [r.x0, r.y0, r.x1, r.y1].iter().any(|v| v.is_nan()) { everything } else { r }; + out = Some(out.map_or(r, |o| o.union(r))); + } + } + out +} + +/// The device-space rectangle a clip path is when it is one along the pixel grid's axes: a +/// move, three or four lines and an optional close, its sides alternately horizontal and +/// vertical. +pub(crate) fn axis_rect(path: &kurbo::BezPath, m: Affine) -> Option { + use kurbo::PathEl; + let els = path.elements(); + let mut pts: Vec = Vec::with_capacity(5); + for (i, el) in els.iter().enumerate() { + match el { + PathEl::MoveTo(p) if i == 0 => pts.push(m * *p), + PathEl::LineTo(p) if i > 0 && pts.len() < 5 => pts.push(m * *p), + PathEl::ClosePath if i > 0 && i + 1 == els.len() => {} + _ => return None, + } + } + const EPS: f64 = 1e-6; + let same = |a: Point, b: Point| (a.x - b.x).abs() <= EPS && (a.y - b.y).abs() <= EPS; + if pts.len() == 5 && pts.first().zip(pts.last()).is_some_and(|(a, b)| same(*a, *b)) { + pts.pop(); + } + let p: [Point; 4] = pts.try_into().ok()?; + let sides = [(p[0], p[1]), (p[1], p[2]), (p[2], p[3]), (p[3], p[0])]; + // Each side horizontal or vertical (and not a point), the next one the other way. + let horizontal = |(a, b): (Point, Point)| -> Option { + let (dx, dy) = ((b.x - a.x).abs(), (b.y - a.y).abs()); + match (dx > EPS, dy > EPS) { + (true, false) => Some(true), + (false, true) => Some(false), + _ => None, + } + }; + let kinds = sides.map(horizontal); + let [Some(k0), Some(k1), Some(k2), Some(k3)] = kinds else { return None }; + if k0 == k1 || k1 == k2 || k2 == k3 || k3 == k0 { + return None; + } + let r = kurbo::Rect::from_points(p[0], p[2]); + [r.x0, r.y0, r.x1, r.y1].iter().all(|v| v.is_finite()).then_some(r) +} + +/// Whether drawing `nodes` reads what is below them beyond Normal compositing: a blend mode or +/// a knockout group anywhere inside. Past `levels` levels of groups the answer is yes. +fn needs_backdrop(nodes: &[Node], levels: usize) -> bool { + nodes.iter().any(|n| match n { + Node::Group(g) => levels == 0 || g.blend != BlendMode::Normal || g.knockout || needs_backdrop(&g.children, levels - 1), + Node::Shape(_) | Node::Image(_) => false, + }) +} + +/// A plain clipping group (no soft mask, blend mode, group opacity or knockout) whose content +/// composites Normal throughout: drawn into a surface the size of its clip's bounds and +/// composited over `dst`. With Normal content that is the same as drawing it over a copy of +/// the backdrop and keeping the clip (`(C over B)·c + B·(1 − c) = (C·c) over B`), without two +/// full-size copies per nesting level: a page continuously rasterised at 400 % lost its +/// nested clipped artwork to the surface budget (#2895). +fn draw_clip_local(g: &Group, m: Affine, dst: &mut Image, budget: &mut SurfaceBudget) { + let mut bounds: Option = None; + for (path, _) in &g.clip { + let b = m.transform_rect_bbox(path.bounding_box()); + bounds = Some(bounds.map_or(b, |a| a.intersect(b))); + } + let Some((x0, y0, w, h)) = bounds.and_then(|b| pixel_rect(b, dst.width, dst.height)) else { return }; + let Some(mut tmp) = budget.image(w, h, None) else { return }; + let local = to_local((x0, y0), m); + for c in &g.children { + draw_node(c, local, 1.0, &mut tmp, budget); + } + let cov = clip_coverage(g, local, w, h); + tmp.data.par_iter_mut().zip(cov.par_iter()).for_each(|(p, c)| { + for v in p.iter_mut() { + *v *= c; + } + }); + over_at(dst, &tmp, (x0, y0), 1.0); + budget.release(&tmp); +} + +/// The pixels of a `w`×`h` image that the device-space rectangle `r` touches, with a pixel of +/// margin for anti-aliasing, as `(x, y, width, height)`; `None` when it misses the image. A +/// rectangle that isn't finite stands for the whole image. +fn pixel_rect(r: kurbo::Rect, w: u32, h: u32) -> Option<(u32, u32, u32, u32)> { + if w == 0 || h == 0 { + return None; + } + if ![r.x0, r.y0, r.x1, r.y1].iter().all(|v| v.is_finite()) { + return Some((0, 0, w, h)); + } + let (fw, fh) = (f64::from(w), f64::from(h)); + let x0 = (r.x0.min(r.x1) - 1.0).floor().clamp(0.0, fw); + let y0 = (r.y0.min(r.y1) - 1.0).floor().clamp(0.0, fh); + let x1 = (r.x0.max(r.x1) + 1.0).ceil().clamp(0.0, fw); + let y1 = (r.y0.max(r.y1) + 1.0).ceil().clamp(0.0, fh); + if x1 <= x0 || y1 <= y0 { + return None; + } + Some((x0 as u32, y0 as u32, (x1 - x0) as u32, (y1 - y0) as u32)) +} + +/// A translation by `-origin`: maps device space onto a surface whose top-left pixel sits at +/// `origin` of the device image. +fn to_local(origin: (u32, u32), m: Affine) -> Affine { + Affine::translate((-f64::from(origin.0), -f64::from(origin.1))) * m +} + /// Draws a group through an offscreen buffer, then composites it onto `dst`. fn draw_offscreen_group(g: &Group, m: Affine, o: f64, dst: &mut Image, budget: &mut SurfaceBudget) { let only_clip = g.mask.is_none() && g.blend == BlendMode::Normal && o >= 1.0 && !g.knockout; @@ -191,7 +399,10 @@ fn apply_soft_mask(sm: &SoftMask, m: Affine, tmp: &mut Image, budget: &mut Surfa let Some(mut mk) = budget.image(tmp.width, tmp.height, None) else { return false; }; + // The mask's content is a tree of its own: what is known of the masked group's isn't. + let plain = std::mem::replace(&mut budget.plain, false); draw_group(&sm.content, m, 1.0, &mut mk, budget); + budget.plain = plain; let bc = sm.backdrop.map(|v| v as f32); let lum = sm.luminosity; tmp.data.par_iter_mut().zip(mk.data.par_iter()).for_each(|(p, q)| { @@ -421,12 +632,16 @@ fn draw_image(im: &crate::Image, m: Affine, opacity: f64, dst: &mut Image) { if !inv.as_coeffs().iter().all(|v| v.is_finite()) { return; } - let quad = kurbo::Rect::new(0.0, 0.0, iw as f64, ih as f64).to_path(0.01); - let cov = effectcraft_path::fill_coverage(std::slice::from_ref(&quad), &mat3(full), dst.width, dst.height, effectcraft_path::FillRule::NonZero); - let w = dst.width as usize; - if w == 0 { + // Coverage over the pixels the image's quadrilateral reaches only (see `draw_shape`). + let rect = kurbo::Rect::new(0.0, 0.0, iw as f64, ih as f64); + let Some((x0, y0, rw, rh)) = pixel_rect(full.transform_rect_bbox(rect), dst.width, dst.height) else { return }; + let quad = rect.to_path(0.01); + let cov = effectcraft_path::fill_coverage(std::slice::from_ref(&quad), &mat3(to_local((x0, y0), full)), rw, rh, effectcraft_path::FillRule::NonZero); + let (w, cw) = (dst.width as usize, cov.width as usize); + if w == 0 || cw == 0 { return; } + let (ox, oy) = (x0 as usize, y0 as usize); let px = |x: usize, y: usize| -> [f32; 4] { let i = (y * iw + x) * 4; let a = im.rgba[i + 3] as f32 / 255.0; @@ -438,9 +653,10 @@ fn draw_image(im: &crate::Image, m: Affine, opacity: f64, dst: &mut Image) { let fy = (c[2] * c[2] + c[3] * c[3]).sqrt(); let taps = (fx.max(fy).ceil() as usize).clamp(1, 8); let k = opacity as f32; - dst.data.par_chunks_mut(w).enumerate().for_each(|(y, row)| { - for (x, d) in row.iter_mut().enumerate() { - let cv = cov.data[y * w + x]; + dst.data.par_chunks_mut(w).enumerate().skip(oy).zip(cov.data.par_chunks(cw)).for_each(|((y, row), crow)| { + let Some(seg) = row.get_mut(ox..) else { return }; + for (lx, (d, &cv)) in seg.iter_mut().zip(crow).enumerate() { + let x = ox + lx; if cv <= 0.0 { continue; } @@ -486,6 +702,30 @@ fn over(dst: &mut Image, src: &Image, k: f32) { }); } +/// `dst = src·k over dst` for `src` placed with its top-left pixel at `origin` of `dst`. +fn over_at(dst: &mut Image, src: &Image, origin: (u32, u32), k: f32) { + let (w, sw) = (dst.width as usize, src.width as usize); + if w == 0 || sw == 0 { + return; + } + let (x0, y0) = (origin.0 as usize, origin.1 as usize); + dst.data.par_chunks_mut(w).skip(y0).zip(src.data.par_chunks(sw)).for_each(|(row, srow)| { + let Some(seg) = row.get_mut(x0..) else { return }; + for (d, s) in seg.iter_mut().zip(srow) { + let a = s[3] * k; + if a > 0.0 { + for c in 0..3 { + d[c] = s[c] * k + d[c] * (1.0 - a); + } + d[3] = a + d[3] * (1.0 - a); + } + } + }); +} + +/// Draw a shape. Its coverage is computed over the pixels its fill or stroke can reach only, +/// not the whole image: a page drawn at a large scale cost a full-size coverage pass per path +/// and per run of text (#2895). fn draw_shape(s: &Shape, m: Affine, opacity: f64, dst: &mut Image) { let m = m * s.transform; let path = s.geom.to_path(); @@ -496,22 +736,32 @@ fn draw_shape(s: &Shape, m: Affine, opacity: f64, dst: &mut Image) { FillRule::NonZero => effectcraft_path::FillRule::NonZero, FillRule::EvenOdd => effectcraft_path::FillRule::EvenOdd, }; - let cov = effectcraft_path::fill_coverage(std::slice::from_ref(&path), &mat3(m), w, h, rule); - paint(dst, &cov, fill, (opacity * s.fill_opacity) as f32, m, bbox); + if let Some((x0, y0, rw, rh)) = pixel_rect(m.transform_rect_bbox(bbox), w, h) { + let cov = effectcraft_path::fill_coverage(std::slice::from_ref(&path), &mat3(to_local((x0, y0), m)), rw, rh, rule); + paint(dst, &cov, (x0, y0), fill, (opacity * s.fill_opacity) as f32, m, bbox); + } } if let Some(st) = &s.stroke { + // The outline as `effectcraft_path::stroke_coverage` makes it (the resolution scale of + // the whole transform, which a translation doesn't change). let style = st.style(); - let cov = effectcraft_path::stroke_coverage(std::slice::from_ref(&path), &style, &mat3(m), w, h); - paint(dst, &cov, &st.paint, (opacity * st.opacity) as f32, m, bbox); + if let Some(outline) = effectcraft_path::stroke_outline(std::slice::from_ref(&path), &style, mat3(m).mean_scale()) + && let Some((x0, y0, rw, rh)) = pixel_rect(m.transform_rect_bbox(outline.bounding_box()), w, h) + { + let cov = + effectcraft_path::fill_coverage(std::slice::from_ref(&outline), &mat3(to_local((x0, y0), m)), rw, rh, effectcraft_path::FillRule::NonZero); + paint(dst, &cov, (x0, y0), &st.paint, (opacity * st.opacity) as f32, m, bbox); + } } } -/// Composite paint through coverage. -fn paint(dst: &mut Image, cov: &Mask, p: &Paint, opacity: f32, m: Affine, bbox: kurbo::Rect) { - let w = dst.width as usize; - if w == 0 { +/// Composite paint through coverage `cov`, whose top-left pixel sits at `origin` of `dst`. +fn paint(dst: &mut Image, cov: &Mask, origin: (u32, u32), p: &Paint, opacity: f32, m: Affine, bbox: kurbo::Rect) { + let (w, cw) = (dst.width as usize, cov.width as usize); + if w == 0 || cw == 0 { return; } + let (x0, y0) = (origin.0 as usize, origin.1 as usize); let shader = match p { Paint::Color(c) => Shader::Solid([c[0] as f32, c[1] as f32, c[2] as f32, 1.0]), Paint::Gradient(g) => { @@ -523,13 +773,13 @@ fn paint(dst: &mut Image, cov: &Mask, p: &Paint, opacity: f32, m: Affine, bbox: Shader::Gradient(g, to_grad) } }; - dst.data.par_chunks_mut(w).enumerate().for_each(|(y, row)| { - for (x, d) in row.iter_mut().enumerate() { - let c = cov.data[y * w + x]; + dst.data.par_chunks_mut(w).enumerate().skip(y0).zip(cov.data.par_chunks(cw)).for_each(|((y, row), crow)| { + let Some(seg) = row.get_mut(x0..) else { return }; + for (lx, (d, &c)) in seg.iter_mut().zip(crow).enumerate() { if c <= 0.0 { continue; } - let col = shader.at(x as f64 + 0.5, y as f64 + 0.5); + let col = shader.at((x0 + lx) as f64 + 0.5, y as f64 + 0.5); let a = col[3] * c * opacity; if a <= 0.0 { continue; diff --git a/crates/svg/src/tests.rs b/crates/svg/src/tests.rs index 06dfbd333..0b4b02832 100644 --- a/crates/svg/src/tests.rs +++ b/crates/svg/src/tests.rs @@ -446,3 +446,193 @@ fn at_rule_blocks_are_skipped_by_brace_depth() { assert_eq!(sheet.rules.len(), 1); assert_eq!(sheet.rules[0].classes, vec!["e".to_string()]); } + +fn solid_rect(r: kurbo::Rect, c: [f64; 3]) -> Node { + Node::Shape(Shape { + name: "rect".into(), + geom: Geom::Rect { x: r.x0, y: r.y0, w: r.width(), h: r.height(), rx: 0.0, ry: 0.0 }, + transform: Affine::IDENTITY, + fill: Some(Paint::Color(c)), + fill_opacity: 1.0, + fill_rule: FillRule::NonZero, + stroke: None, + opacity: 1.0, + }) +} + +/// A PDF-style clipping group (not isolated: blend modes inside reach the backdrop). +fn clip_group(r: kurbo::Rect, children: Vec) -> Node { + let mut g = Group::new("Clip Group"); + g.clip.push((kurbo::Shape::to_path(&r, 0.1), FillRule::NonZero)); + g.isolated = false; + g.children = children; + Node::Group(g) +} + +fn doc_of(w: f64, h: f64, children: Vec) -> Doc { + let mut root = Group::new("page"); + root.children = children; + Doc { width: w, height: h, root, skipped: vec![] } +} + +/// Nested clipping groups over a backdrop: the content shows where every clip covers, half +/// where a clip edge halves a pixel, and the backdrop everywhere else (#2895: plain clipping +/// groups are drawn on surfaces the size of their clip). +#[test] +fn nested_clip_groups_keep_the_backdrop_outside_their_clips() { + let blue = [0.0, 0.0, 1.0]; + let red = [1.0, 0.0, 0.0]; + let inner = clip_group(kurbo::Rect::new(10.5, 0.0, 20.0, 40.0), vec![solid_rect(kurbo::Rect::new(0.0, 0.0, 40.0, 40.0), red)]); + let doc = + doc_of(40.0, 40.0, vec![solid_rect(kurbo::Rect::new(0.0, 0.0, 40.0, 40.0), blue), clip_group(kurbo::Rect::new(5.0, 5.0, 35.0, 35.0), vec![inner])]); + let img = rasterize(&doc, 40, 40, 1.0); + let (r, b) = ([1.0, 0.0, 0.0, 1.0], [0.0, 0.0, 1.0, 1.0]); + for (x, y, want) in [(15, 20, r), (11, 6, r), (19, 34, r), (25, 20, b), (15, 2, b), (15, 36, b), (9, 20, b), (21, 20, b)] { + assert!(near(px(&img, x, y), want, 1e-3), "({x}, {y}): {:?}", px(&img, x, y)); + } + assert!(near(px(&img, 10, 20), [0.5, 0.0, 0.5, 1.0], 0.02), "half-covered edge pixel {:?}", px(&img, 10, 20)); + // Scaled 4×: the clip edge at 10.5 lands on a pixel boundary (42), hard on both sides. + let big = rasterize(&doc, 160, 160, 4.0); + assert!(near(px(&big, 41, 80), b, 0.01) && near(px(&big, 42, 80), r, 0.01), "{:?} {:?}", px(&big, 41, 80), px(&big, 42, 80)); +} + +/// A clipping group whose content blends (Multiply) still composites with the real backdrop: +/// red multiplied over blue is black inside the clip, blue outside. +#[test] +fn a_blend_mode_inside_a_clip_group_reaches_the_backdrop() { + let mut multiply = Group::new("Multiply"); + multiply.blend = BlendMode::Multiply; + multiply.children = vec![solid_rect(kurbo::Rect::new(0.0, 0.0, 20.0, 20.0), [1.0, 0.0, 0.0])]; + let doc = doc_of( + 20.0, + 20.0, + vec![ + solid_rect(kurbo::Rect::new(0.0, 0.0, 20.0, 20.0), [0.0, 0.0, 1.0]), + clip_group(kurbo::Rect::new(5.0, 5.0, 15.0, 15.0), vec![Node::Group(multiply)]), + ], + ); + let img = rasterize(&doc, 20, 20, 1.0); + assert!(near(px(&img, 10, 10), [0.0, 0.0, 0.0, 1.0], 1e-3), "{:?}", px(&img, 10, 10)); + assert!(near(px(&img, 2, 10), [0.0, 0.0, 1.0, 1.0], 1e-3), "{:?}", px(&img, 2, 10)); +} + +/// A stroke reaches past its path's bounds by half its width: a horizontal line (a path +/// without height) stroked 4 px wide covers 4 rows, and its butt caps end at the path's ends. +#[test] +fn a_stroke_draws_its_full_width_past_the_path_bounds() { + let mut line = BezPath::new(); + line.move_to((5.0, 20.0)); + line.line_to((35.0, 20.0)); + let shape = Node::Shape(Shape { + name: "line".into(), + geom: Geom::Path(line), + transform: Affine::IDENTITY, + fill: None, + fill_opacity: 1.0, + fill_rule: FillRule::NonZero, + stroke: Some(Stroke { paint: Paint::Color([0.0, 0.0, 0.0]), opacity: 1.0, width: 4.0, cap: Cap::Butt, join: Join::Miter, miter: 4.0, dash: None }), + opacity: 1.0, + }); + let img = rasterize(&doc_of(40.0, 40.0, vec![shape]), 40, 40, 1.0); + for y in 18..22 { + assert!(px(&img, 20, y)[3] > 0.99, "row {y}: {:?}", px(&img, 20, y)); + } + for (x, y) in [(20, 17), (20, 22), (3, 20), (36, 20)] { + assert!(px(&img, x, y)[3] < 0.01, "({x}, {y}): {:?}", px(&img, x, y)); + } +} + +/// Bytes of `n` RGBA f32 surfaces of `w`×`h` pixels (a surface budget for the tests below). +fn surfaces(n: f64, w: u32, h: u32) -> usize { + (n * f64::from(w) * f64::from(h) * 16.0) as usize +} + +/// #2895: an A4 page continuously rasterised at 400 % is a 128 MB surface, and every nested +/// clipping group used to copy the backdrop and draw on a second surface of that size, so the +/// second level of clips ran past the 512 MB surface budget and its artwork was dropped. Clip +/// groups now draw on surfaces the size of their clip: with room for four canvas-size surfaces, +/// artwork inside four nested clips still draws (it used to need nine). +#[test] +fn deeply_clipped_artwork_draws_within_the_surface_budget() { + let red = [1.0, 0.0, 0.0]; + let mut node = solid_rect(kurbo::Rect::new(0.0, 0.0, 300.0, 300.0), red); + for i in 1..=4 { + let inset = 10.0 * f64::from(i); + node = clip_group(kurbo::Rect::new(inset, inset, 300.0 - inset, 300.0 - inset), vec![node]); + } + let img = crate::raster::rasterize_with_budget(&doc_of(300.0, 300.0, vec![node]), 300, 300, Affine::IDENTITY, surfaces(4.0, 300, 300)); + assert_eq!((img.width, img.height), (300, 300)); + assert!(near(px(&img, 150, 150), [1.0, 0.0, 0.0, 1.0], 1e-3), "centre {:?}", px(&img, 150, 150)); + assert!(near(px(&img, 45, 150), [1.0, 0.0, 0.0, 1.0], 1e-3), "just inside every clip {:?}", px(&img, 45, 150)); + assert!(px(&img, 35, 150)[3] < 0.01, "outside the outermost clip {:?}", px(&img, 35, 150)); +} + +/// #2895: clips that cover the whole canvas around the artwork inside them (a page clip, an +/// artboard clip, the outermost groups of most PDF pages) change no pixel, so they are drawn +/// without a surface: with room for two and a half canvas-size surfaces, the artwork inside two +/// of them still draws, and an inner clip still cuts it. +#[test] +fn canvas_covering_clips_draw_without_a_surface() { + let red = [1.0, 0.0, 0.0]; + let art = clip_group(kurbo::Rect::new(150.0, 0.0, 300.0, 350.0), vec![solid_rect(kurbo::Rect::new(100.0, 100.0, 250.0, 250.0), red)]); + let page = clip_group(kurbo::Rect::new(0.0, 0.0, 350.0, 350.0), vec![art]); + let outer = clip_group(kurbo::Rect::new(-1.0, -1.0, 351.0, 351.0), vec![page]); + let img = crate::raster::rasterize_with_budget(&doc_of(350.0, 350.0, vec![outer]), 350, 350, Affine::IDENTITY, surfaces(2.5, 350, 350)); + assert_eq!((img.width, img.height), (350, 350)); + for (x, y) in [(200, 200), (151, 101), (248, 248)] { + assert!(near(px(&img, x, y), [1.0, 0.0, 0.0, 1.0], 1e-3), "({x}, {y}): {:?}", px(&img, x, y)); + } + for (x, y) in [(120, 200), (148, 200), (200, 90), (260, 200)] { + assert!(px(&img, x, y)[3] < 0.01, "({x}, {y}): {:?}", px(&img, x, y)); + } +} + +/// Take a tree apart without recursing (a deep one would overflow the stack when dropped). +fn dismantle(mut nodes: Vec) { + while let Some(n) = nodes.pop() { + if let Node::Group(mut g) = n { + nodes.append(&mut g.children); + } + } +} + +/// Never crash: drawing recurses, and clipping groups nested twenty thousand deep (a crafted +/// file) are drawn without running out of stack, on a thread with a small one; groups past the +/// nesting limit are not drawn, shallower artwork is. +#[test] +fn clip_groups_nested_without_end_do_not_overflow_the_stack() { + let run = std::thread::Builder::new().stack_size(4 << 20).spawn(|| { + let mut node = solid_rect(kurbo::Rect::new(0.0, 0.0, 64.0, 64.0), [1.0, 0.0, 0.0]); + for _ in 0..20_000 { + node = clip_group(kurbo::Rect::new(0.0, 0.0, 64.0, 64.0), vec![node]); + } + let shallow = solid_rect(kurbo::Rect::new(0.0, 0.0, 8.0, 8.0), [0.0, 0.0, 1.0]); + let doc = doc_of(64.0, 64.0, vec![node, shallow]); + let img = rasterize(&doc, 64, 64, 1.0); + let out = (img.width, img.height, px(&img, 4, 4), px(&img, 40, 40)); + dismantle(vec![Node::Group(doc.root)]); + out + }); + let (w, h, shallow, deep) = run.unwrap().join().unwrap(); + assert_eq!((w, h), (64, 64)); + assert!(near(shallow, [0.0, 0.0, 1.0, 1.0], 1e-3), "{shallow:?}"); + assert!(deep[3] < 0.01, "artwork past the nesting limit is not drawn: {deep:?}"); +} + +/// Only a rectangle along the pixel grid's axes counts as a clip that can be skipped: a +/// rotated square or a path that folds back on itself does not. +#[test] +fn axis_rect_recognises_only_axis_aligned_rectangles() { + use crate::raster::axis_rect; + let rect = kurbo::Shape::to_path(&kurbo::Rect::new(1.0, 2.0, 5.0, 7.0), 0.1); + assert_eq!(axis_rect(&rect, Affine::IDENTITY), Some(kurbo::Rect::new(1.0, 2.0, 5.0, 7.0))); + assert_eq!(axis_rect(&rect, Affine::scale(2.0)), Some(kurbo::Rect::new(2.0, 4.0, 10.0, 14.0))); + assert_eq!(axis_rect(&rect, Affine::rotate(0.3)), None); + let mut folded = BezPath::new(); + folded.move_to((0.0, 0.0)); + folded.line_to((10.0, 0.0)); + folded.line_to((10.0, 10.0)); + folded.line_to((10.0, 0.0)); + folded.close_path(); + assert_eq!(axis_rect(&folded, Affine::IDENTITY), None); +} From b33f5f62a8fd19f3c52699d8189cd54b9b3d8e99 Mon Sep 17 00:00:00 2001 From: vangel76 Date: Sat, 7 Nov 2026 13:58:35 +0100 Subject: [PATCH 05/74] Expressions: a function declaration ends at its brace, so a next line starting with '-' is a new statement (#3212) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The array-math rewrite folded `function f(x){…}\n-50 + 100` into `__add(__sub(function f(x){…}, 50), 100)`: the declaration became a function expression operand, the result was NaN and the property fell back to its static value. In ECMAScript an expression statement cannot start with `function`, so at statement start it is a FunctionDeclaration that ends at its `}`. The rewriter now emits a function in statement position as a declaration on its own and never as an operand of what follows; function expressions (after `=`, `(`, `,`, `:`, `return` …) stay operands. The empty expressionError for evaluation errors is tracked in #3219. Fixes #3212 --- crates/expr/src/rewrite.rs | 115 +++++++++++++++++++++++++++++-------- crates/expr/src/tests.rs | 16 ++++++ 2 files changed, 106 insertions(+), 25 deletions(-) diff --git a/crates/expr/src/rewrite.rs b/crates/expr/src/rewrite.rs index 34b52439b..69762d21e 100644 --- a/crates/expr/src/rewrite.rs +++ b/crates/expr/src/rewrite.rs @@ -334,7 +334,7 @@ fn raw(item: &Item) -> Piece { Item::Tok(t) => Piece { ws: t.ws.clone(), body: t.text.clone() }, Item::Group { open, inner, close } => { let mut body = open.text.clone(); - body.push_str(&rewrite_seq(inner)); + body.push_str(&rewrite_seq(inner, open.text == "{")); if let Some(c) = close { body.push_str(&c.ws); body.push_str(&c.text); @@ -378,25 +378,7 @@ fn operand(items: &[Item], i: usize) -> Option<(Piece, usize)> { }; return Some((Piece { ws: t.ws.clone(), body }, j)); } - let mut p = raw(first); - let mut j = i + 1; - // `function name(args) { body }` is one atom. - if first.tok().is_some_and(|t| t.is_kw(&["function"])) { - if items.get(j).is_some_and(|x| x.is("*")) { - push(&mut p, &items[j]); - j += 1; - } - if items.get(j).and_then(Item::tok).is_some_and(|t| t.kind == Kind::Ident) { - push(&mut p, &items[j]); - j += 1; - } - for g in ["(", "{"] { - if items.get(j).and_then(Item::group) == Some(g) { - push(&mut p, &items[j]); - j += 1; - } - } - } + let (mut p, mut j) = if is_function(first) { function_atom(items, i) } else { (raw(first), i + 1) }; // Postfix: member access, calls, indexing, tagged templates, `x++`. while let Some(it) = items.get(j) { let member = it.is(".") || it.is("?."); @@ -428,6 +410,59 @@ fn operand(items: &[Item], i: usize) -> Option<(Piece, usize)> { Some((p, j)) } +fn is_function(item: &Item) -> bool { + item.tok().is_some_and(|t| t.is_kw(&["function"])) +} + +/// `function [*] [name] (args) { body }` from the `function` keyword at `i`, as one atom. +fn function_atom(items: &[Item], i: usize) -> (Piece, usize) { + let Some(first) = items.get(i) else { + // Unreachable for callers (they pass a `function` item); still advance so no loop stalls. + return (Piece { ws: String::new(), body: String::new() }, i.saturating_add(1)); + }; + let mut p = raw(first); + let mut j = i + 1; + if items.get(j).is_some_and(|x| x.is("*")) { + push(&mut p, &items[j]); + j += 1; + } + if items.get(j).and_then(Item::tok).is_some_and(|t| t.kind == Kind::Ident) { + push(&mut p, &items[j]); + j += 1; + } + for g in ["(", "{"] { + if items.get(j).and_then(Item::group) == Some(g) { + push(&mut p, &items[j]); + j += 1; + } + } + (p, j) +} + +/// Is the `function` keyword at `i` at the start of a statement, so a function declaration +/// (ECMAScript: an expression statement cannot start with `function`)? A declaration ends at its +/// `}`: whatever follows is a new statement, never an operand of it. `stmts` says whether +/// `items` is a statement list (the whole expression or a `{ … }` block) rather than the inside +/// of `( … )` or `[ … ]`. +fn at_statement_start(items: &[Item], i: usize, stmts: bool) -> bool { + // `async function`: the statement starts at `async` (no line break may come between them). + let is_async = |k: usize| items.get(k).and_then(Item::tok).is_some_and(|a| a.is_kw(&["async"])); + let at = match i.checked_sub(1) { + Some(k) if is_async(k) && !items.get(i).is_some_and(Item::nl) => k, + _ => i, + }; + let Some(prev) = at.checked_sub(1).and_then(|k| items.get(k)) else { + return stmts; + }; + match prev { + // A block, an `if (…)`/`while (…)` header or a value the line break ends. + Item::Group { .. } => true, + // Known limit: after a label, `case …:` or `default:` a declaration is still read as an + // expression (that `:` cannot be told from an object literal's or a conditional's here). + Item::Tok(t) => t.is(";") || t.is_kw(&["else", "do"]) || ends_value(t), + } +} + fn push(p: &mut Piece, item: &Item) { let r = raw(item); p.body.push_str(&r.ws); @@ -510,7 +545,9 @@ fn assignment_end(items: &[Item], i: usize) -> usize { j } -fn rewrite_seq(items: &[Item]) -> String { +/// Rewrite a sequence of items. `stmts`: the sequence is a statement list (the whole expression +/// or the inside of `{ … }`), not the inside of `( … )` or `[ … ]`. +fn rewrite_seq(items: &[Item], stmts: bool) -> String { let mut out = String::new(); let mut i = 0; while i < items.len() { @@ -521,12 +558,20 @@ fn rewrite_seq(items: &[Item]) -> String { i += 2; continue; } + // A function declaration ends at its `}` (#3212): `function f() {}\n-1` is two statements, + // so the next line must not become an operand (`__sub(function f() {}, 1)` is NaN). + if is_function(it) && at_statement_start(items, i, stmts) { + let (p, j) = function_atom(items, i); + push_piece(&mut out, p); + i = j; + continue; + } if let Some((p, j)) = chain(items, i) { // Compound assignment: `x += v` → `x = __add(x, (v))` (the target is evaluated twice, // which only matters for targets with side effects). if let Some(op) = items.get(j).and_then(Item::tok).filter(|t| t.kind == Kind::Punct && matches!(t.text.as_str(), "+=" | "-=" | "*=" | "/=")) { let end = assignment_end(items, j + 1); - let rhs = rewrite_seq(&items[j + 1..end]); + let rhs = rewrite_seq(&items[j + 1..end], false); let h = helper(&op.text[..1]).unwrap_or("__add"); let target = compound_assignment_target(items, i, j); out.push_str(&p.ws); @@ -555,7 +600,7 @@ fn raw_ref(item: &Item) -> String { out.push_str(&raw_ref(item)); } } else { - out.push_str(&rewrite_seq(inner)); + out.push_str(&rewrite_seq(inner, open.text == "{")); } if let Some(close) = close { out.push_str(&close.text); @@ -794,7 +839,7 @@ pub fn check_nesting(src: &str) -> Result<(), String> { if too_nested(&toks) { return Err(too_deep()); } - let mut out = rewrite_seq(&build(toks)); + let mut out = rewrite_seq(&build(toks), true); out.push_str(&tail); if over_rewrite_limit(&out) { return Err(too_large()); @@ -825,7 +870,7 @@ pub fn rewrite(src: &str) -> String { if too_nested(&toks) { return throw_too_deep(); } - let mut out = rewrite_seq(&build(toks)); + let mut out = rewrite_seq(&build(toks), true); out.push_str(&tail); if over_rewrite_limit(&out) { return throw_too_large(); @@ -897,6 +942,26 @@ mod tests { assert_eq!(n(&rewrite("2 ** 3 * 2")), n("__mul(2 ** 3, 2)")); } + #[test] + fn a_function_declaration_is_not_an_operand_of_the_next_statement() { + // #3212: a declaration ends at its `}` (an expression statement cannot start with + // `function`), so a following `-`, `+` or `[` starts a new statement. + assert_eq!(n(&rewrite("function f(x){ return x; }\n-50 + 100")), n("function f(x){ return x; }\n__add(-50, 100)")); + assert_eq!(n(&rewrite("function f(){}\n+a - b")), n("function f(){}\n__sub(+a, b)")); + assert_eq!(n(&rewrite("function f(){}\n[1, 2] - [1, 1]")), n("function f(){}\n__sub([1, 2], [1, 1])")); + assert_eq!(n(&rewrite("function f(){}\n(a) - b")), n("function f(){}\n__sub((a), b)")); + assert_eq!(n(&rewrite("function* g(){} -x")), n("function* g(){} __neg(x)")); + assert_eq!(n(&rewrite("async function f(){}\n-x")), n("async function f(){}\n__neg(x)")); + assert_eq!(n(&rewrite("var a = 1;function f(){}\n-x")), n("var a = 1;function f(){}\n__neg(x)")); + assert_eq!(n(&rewrite("if (a) { function f(){}\n-b }")), n("if (a) { function f(){}\n__neg(b) }")); + // Function expressions stay operands, whatever follows on the next line. + assert_eq!(n(&rewrite("x = function(a){ return a } - 1")), n("x = __sub(function(a){ return a }, 1)")); + assert_eq!(n(&rewrite("var g = function(){}\n- 1")), n("var g = __sub(function(){},\n 1)")); + assert_eq!(n(&rewrite("(function f(){} - 1)")), n("(__sub(function f(){}, 1))")); + assert_eq!(n(&rewrite("[function(){} - 1]")), n("[__sub(function(){}, 1)]")); + assert_eq!(n(&rewrite("o = {k: function(){} - 1}")), n("o = {k: __sub(function(){}, 1)}")); + } + #[test] fn source_expansion_is_bounded_before_rewriting() { let src = "x += 1;".repeat(super::MAX_REWRITTEN_BYTES / 6 + 1); diff --git a/crates/expr/src/tests.rs b/crates/expr/src/tests.rs index a7d2489c7..b3a18f6ee 100644 --- a/crates/expr/src/tests.rs +++ b/crates/expr/src/tests.rs @@ -193,6 +193,22 @@ fn functions_and_let_are_reusable_across_evaluations() { assert_close!(f.num(e, 2.0), 9.0); } +#[test] +fn a_function_declaration_ends_at_its_brace_so_a_leading_minus_starts_a_new_statement() { + // #3212: the array-math rewrite took `function … {…}\n-50 + 100` for one expression, + // `(function … {…}) - 50 + 100`, which is NaN (and left the function undeclared). + let f = fx(); + assert_close!(f.num("function same3212(x){ return x; }\n-50 + 100", 0.0), 50.0); + // The declaration really declared the function (globals do not outlive one evaluation). + assert_close!(f.num("function same3212(x){ return x; }\n-50 + 100;\nsame3212(7)", 0.0), 7.0); + assert_close!(f.num("function twice3212(x){ return x * 2; }\n-twice3212(10) + 100", 0.0), 80.0); + assert_close!(f.num("function seven3212(){ return 7; }\n+seven3212()", 0.0), 7.0); + assert_close!(f.num("function one3212(){ return 1; } -one3212() * 3", 0.0), -3.0); + assert_close!(f.num("if (time >= 0) { function neg3212(x){ return -x; }\n-neg3212(4) }", 0.0), 4.0); + // A function expression stays an operand: `function - 1` is NaN in JavaScript too. + assert_close!(f.num("var g3212 = function(){ return 1; }\n- 1;\ntypeof g3212 == 'number' ? 5 : 6", 0.0), 5.0); +} + #[test] fn an_expression_cannot_rebind_a_prelude_name_for_the_next_one() { let f = fx(); From e0d9e024bd077f610b8d3878165e8cf6cd8945b2 Mon Sep 17 00:00:00 2001 From: vangel76 Date: Sat, 7 Nov 2026 14:13:23 +0100 Subject: [PATCH 06/74] Graph Editor: follow the theme (plot, bar buttons, grid, keys, handles, curves) (#3202) The Graph Editor painted its own surfaces with fixed dark greys: the plot #1d1d1d and the unlit bar buttons #2c2c2c under the theme's text colour. In the Light theme the plot stayed dark and the button labels (#222 on #2c2c2c, 1.1:1) only showed while hovered; lit buttons put white on a pale blue. GraphPalette builds the editor's colours from the theme tokens once per frame: the plot takes the Timeline's time graph background, the buttons a fill just off the panel with the more readable label, the grid an opaque tint of the plot. Keys, Bezier handles, the transform box and the curves have a darker set for a pale plot, picked by the plot colour itself so it holds under the brightness slider. theme.rs gains WCAG relative_luminance/contrast_ratio, also used by the expression editor's contrast test. Fixes #3202 --- crates/ui-egui/src/panels/expr_editor.rs | 15 +- crates/ui-egui/src/panels/graph.rs | 218 ++++++++++++++++++---- crates/ui-egui/src/panels/graph_tools.rs | 5 +- crates/ui-egui/src/theme.rs | 15 ++ crates/ui-egui/tests/it/main.rs | 1 + crates/ui-egui/tests/it/ui_graph_theme.rs | 102 ++++++++++ 6 files changed, 307 insertions(+), 49 deletions(-) create mode 100644 crates/ui-egui/tests/it/ui_graph_theme.rs diff --git a/crates/ui-egui/src/panels/expr_editor.rs b/crates/ui-egui/src/panels/expr_editor.rs index 6d742dc4e..b7f05f023 100644 --- a/crates/ui-egui/src/panels/expr_editor.rs +++ b/crates/ui-egui/src/panels/expr_editor.rs @@ -419,20 +419,7 @@ pub fn editor(ui: &mut egui::Ui, id: egui::Id, buf: &mut String, rect: Rect, opt #[cfg(test)] mod contrast_tests { use super::*; - use crate::theme::ThemeKind; - - /// WCAG contrast ratio of two opaque sRGB colours. - fn contrast(a: Color32, b: Color32) -> f32 { - let lum = |c: Color32| { - let f = |v: u8| { - let v = v as f32 / 255.0; - if v <= 0.04045 { v / 12.92 } else { ((v + 0.055) / 1.055).powf(2.4) } - }; - 0.2126 * f(c.r()) + 0.7152 * f(c.g()) + 0.0722 * f(c.b()) - }; - let (x, y) = (lum(a), lum(b)); - (x.max(y) + 0.05) / (x.min(y) + 0.05) - } + use crate::theme::{ThemeKind, contrast_ratio as contrast}; /// #644: on every theme, effect names stay readable on Effect Controls' headers, and the /// expression field's text (every syntax colour, enabled and disabled expressions, matched diff --git a/crates/ui-egui/src/panels/graph.rs b/crates/ui-egui/src/panels/graph.rs index be18999d4..3b883d191 100644 --- a/crates/ui-egui/src/panels/graph.rs +++ b/crates/ui-egui/src/panels/graph.rs @@ -14,7 +14,7 @@ use egui::{Align2, Color32, Pos2, Rect, Sense, Stroke, StrokeKind, pos2, vec2}; use serde_json::json; use super::timeline::TMap; -use crate::theme::Tokens; +use crate::theme::{Tokens, contrast_ratio}; use crate::{EffectcraftApp, widgets}; type Actions = Vec<(String, serde_json::Value)>; @@ -29,6 +29,84 @@ const PROP_COLORS: [Color32; 6] = [ Color32::from_rgb(0x9c, 0xd8, 0x50), Color32::from_rgb(0xf0, 0x70, 0x90), ]; +/// The same hues, darker, for the Light theme's pale graph (the bright ones vanish on it). +const DIM_COLORS_LIGHT: [Color32; 4] = + [Color32::from_rgb(0xb0, 0x20, 0x20), Color32::from_rgb(0x1a, 0x7a, 0x1a), Color32::from_rgb(0x1c, 0x50, 0xc0), Color32::from_rgb(0x8a, 0x6a, 0x00)]; +const PROP_COLORS_LIGHT: [Color32; 6] = [ + Color32::from_rgb(0x8a, 0x6a, 0x00), + Color32::from_rgb(0x90, 0x30, 0xa8), + Color32::from_rgb(0x00, 0x78, 0x78), + Color32::from_rgb(0xb0, 0x50, 0x00), + Color32::from_rgb(0x4a, 0x80, 0x10), + Color32::from_rgb(0xc0, 0x30, 0x60), +]; + +/// The Graph Editor's colours for the active theme (#3202): its surfaces come from the theme's +/// tokens (the plot is the Timeline's time graph, the buttons sit on the panel), and the curve, +/// key and handle colours have a darker set for a pale plot (Light), so all of it stays readable. +/// Built once per frame in [`show`] and passed down. +#[derive(Clone, Copy, Debug)] +pub(crate) struct GraphPalette { + /// The plot's background. + pub bg: Color32, + /// The horizontal value grid lines. + pub grid: Color32, + /// The value labels on the grid and the "select animated properties" hint. + pub label: Color32, + /// An unlit bar button's fill and label. + pub button: Color32, + pub button_text: Color32, + /// A lit bar button's fill and label. + pub button_on: Color32, + pub button_on_text: Color32, + /// Bezier handles (line and end). + pub handle: Color32, + /// A key's fill, unselected and muted-unselected. + pub key: Color32, + pub key_muted: Color32, + /// The transform box's outline and its handles' fill. + pub box_line: Color32, + pub box_fill: Color32, + /// Curve colours: one per dimension, or one per property. + pub dims: [Color32; 4], + pub props: [Color32; 6], +} + +/// The lowest contrast of any of `colors` against `bg`. +fn min_contrast(colors: &[Color32], bg: Color32) -> f32 { + colors.iter().map(|c| contrast_ratio(*c, bg)).fold(f32::INFINITY, f32::min) +} + +impl GraphPalette { + pub(crate) fn new(t: &Tokens) -> Self { + let bg = t.tl_bg; + // The darker marks for a pale plot: chosen by the plot itself, not the theme's name, as + // the brightness slider moves the plot (Dark lightened, Light darkened). Whichever set + // stands out more from the plot wins. + let bright: Vec = DIM_COLORS.iter().chain(PROP_COLORS.iter()).copied().collect(); + let dark: Vec = DIM_COLORS_LIGHT.iter().chain(PROP_COLORS_LIGHT.iter()).copied().collect(); + let light = min_contrast(&dark, bg) > min_contrast(&bright, bg); + let button_on = t.panel_bg.lerp_to_gamma(t.accent, 0.55); + // White on a deep blue; on a pale one (Light) the theme's dark text. + let button_on_text = if contrast_ratio(Color32::WHITE, button_on) >= contrast_ratio(t.text, button_on) { Color32::WHITE } else { t.text }; + GraphPalette { + bg, + grid: bg.lerp_to_gamma(t.text, 0.06), + label: t.text_dim, + button: t.panel_bg.lerp_to_gamma(t.text, 0.06), + button_text: t.text, + button_on, + button_on_text, + handle: if light { Color32::from_rgb(0x80, 0x70, 0x10) } else { Color32::from_rgb(0xd8, 0xd8, 0x60) }, + key: if light { t.keyframe } else { Color32::from_gray(0xe0) }, + key_muted: if light { Color32::from_gray(0xa0) } else { Color32::from_gray(0x70) }, + box_line: if light { Color32::from_gray(0x40) } else { Color32::from_gray(0xd8) }, + box_fill: if light { Color32::from_gray(0xf2) } else { Color32::from_gray(0x20) }, + dims: if light { DIM_COLORS_LIGHT } else { DIM_COLORS }, + props: if light { PROP_COLORS_LIGHT } else { PROP_COLORS }, + } + } +} struct Handle { out: bool, @@ -53,12 +131,12 @@ struct KeyPt { } /// A graph key's fill and edge colours: muted keys stay visible and selectable, dimmed. -fn key_colors(t: &Tokens, edge: Color32, k: &KeyPt) -> (Color32, Color32) { +fn key_colors(t: &Tokens, pal: &GraphPalette, edge: Color32, k: &KeyPt) -> (Color32, Color32) { let fill = match (k.muted, k.sel) { (true, true) => t.keyframe_selected.gamma_multiply(0.5), - (true, false) => Color32::from_gray(0x70), + (true, false) => pal.key_muted, (false, true) => t.keyframe_selected, - (false, false) => Color32::from_gray(0xe0), + (false, false) => pal.key, }; (fill, if k.muted { edge.gamma_multiply(0.4) } else { edge }) } @@ -131,7 +209,7 @@ fn speed_at(pr: &Property, spatial: bool, d: usize, lt: Tick) -> f64 { } /// Build the curves for the visible time span. -fn curves<'a>(app: &EffectcraftApp, comp: &'a Comp, ectx: &EvalCtx, tm: TMap, plot: Rect, speed: bool) -> Vec> { +fn curves<'a>(app: &EffectcraftApp, comp: &'a Comp, ectx: &EvalCtx, tm: TMap, plot: Rect, speed: bool, pal: &GraphPalette) -> Vec> { let sel = &app.session.state.selected_keys; let n = ((plot.width() / 2.0) as usize).max(2); let mut out = vec![]; @@ -141,7 +219,7 @@ fn curves<'a>(app: &EffectcraftApp, comp: &'a Comp, ectx: &EvalCtx, tm: TMap, pl let spatial = pr.spatial && matches!(pr.value, Value::Vec2(_) | Value::Vec3(_)); let dims = if speed && spatial { 1 } else { shown_dims(l, pr) }; for d in 0..dims { - let color = if dims > 1 { DIM_COLORS[d % 4] } else { PROP_COLORS[pi % PROP_COLORS.len()] }; + let color = if dims > 1 { pal.dims[d % pal.dims.len()] } else { pal.props[pi % pal.props.len()] }; let mut pts = Vec::with_capacity(n); for i in 0..n { let x = plot.min.x + plot.width() * i as f32 / (n - 1) as f32; @@ -193,6 +271,9 @@ struct ButtonBar { hidden: Vec<(String, String, bool, String)>, /// The id picked from the "»" menu last frame, run by its button this frame. chosen: Option, + /// The bar's rectangle and the theme's Graph Editor colours. + bar: Rect, + pal: GraphPalette, } /// The "»" menu's popup and the id it picked (read by the next frame's buttons). @@ -204,22 +285,22 @@ impl ButtonBar { /// A bar starting 8 points into `bar`, stopping short of its right end by the "»" button's /// width. It takes the id picked from the "»" menu in the previous frame, so the button whose /// id that was reports a click on the frame after the pick. - fn new(ctx: &egui::Context, bar: Rect) -> Self { + fn new(ctx: &egui::Context, bar: Rect, pal: GraphPalette) -> Self { let chosen = ctx.data_mut(|d| d.remove_temp::(egui::Id::unique(MORE_POPUP).with("chosen"))); - ButtonBar { x: bar.min.x + 8.0, limit: bar.max.x - 8.0 - Self::MORE_W - 4.0, hidden: Vec::new(), chosen } + ButtonBar { x: bar.min.x + 8.0, limit: bar.max.x - 8.0 - Self::MORE_W - 4.0, hidden: Vec::new(), chosen, bar, pal } } /// The "»" button and its menu, when buttons did not fit. - fn more(&mut self, app: &mut EffectcraftApp, ui: &mut egui::Ui, p: &egui::Painter, bar: Rect) { + fn more(&mut self, app: &mut EffectcraftApp, ui: &mut egui::Ui, p: &egui::Painter) { if self.hidden.is_empty() { return; } - let t = app.tokens; + let (t, pal, bar) = (app.tokens, self.pal, self.bar); let r = Rect::from_min_size(pos2(bar.max.x - 8.0 - Self::MORE_W, bar.center().y - 9.0), vec2(Self::MORE_W, 18.0)); let pid = egui::Id::unique(MORE_POPUP); let resp = ui.interact(r, pid.with("button"), Sense::click()).on_hover_text(crate::i18n::tr("More")); - p.rect_filled(r, 3.0, if resp.hovered() { t.hover } else { Color32::from_rgb(0x2c, 0x2c, 0x2c) }); - p.text(r.center(), Align2::CENTER_CENTER, "»", Tokens::ui(12.0), t.text); + p.rect_filled(r, 3.0, if resp.hovered() { t.hover } else { pal.button }); + p.text(r.center(), Align2::CENTER_CENTER, "»", Tokens::ui(12.0), pal.button_text); app.auto.add("timeline.graph.more", r, "More"); if resp.clicked() { let open = widgets::popup_is_open(ui, pid); @@ -273,18 +354,19 @@ fn text_button(app: &mut EffectcraftApp, ui: &mut egui::Ui, p: &egui::Painter, b } let r = Rect::from_min_size(pos2(bar.x, y - 9.0), vec2(w, 18.0)); let resp = ui.interact(r, egui::Id::unique(("graph-btn", id)), Sense::click()).on_hover_text(shown_tip); + let pal = &bar.pal; p.rect_filled( r, 3.0, if on { - t.accent.gamma_multiply(0.55) + pal.button_on } else if resp.hovered() { t.hover } else { - Color32::from_rgb(0x2c, 0x2c, 0x2c) + pal.button }, ); - p.text(r.center(), Align2::CENTER_CENTER, shown, Tokens::ui(11.0), if on { Color32::WHITE } else { t.text }); + p.text(r.center(), Align2::CENTER_CENTER, shown, Tokens::ui(11.0), if on { pal.button_on_text } else { pal.button_text }); app.auto.add(&format!("timeline.graph.{id}"), r, tip); bar.x += w + 4.0; resp.clicked() @@ -299,9 +381,9 @@ pub(crate) struct GraphView<'a> { } /// Show Reference Graph: the other graph type, faint, scaled to the plot. -fn reference_graph(app: &EffectcraftApp, view: GraphView, plot: Rect, speed: bool, pc: &egui::Painter) { +fn reference_graph(app: &EffectcraftApp, view: GraphView, plot: Rect, speed: bool, pc: &egui::Painter, pal: &GraphPalette) { let GraphView { comp, ectx, tm } = view; - let other = curves(app, comp, ectx, tm, plot, !speed); + let other = curves(app, comp, ectx, tm, plot, !speed, pal); let (mut rlo, mut rhi) = (f64::INFINITY, f64::NEG_INFINITY); for c in &other { for (_, v) in &c.pts { @@ -407,16 +489,17 @@ fn fit_on_open(app: &mut EffectcraftApp, ctx: &egui::Context, cs: &[Curve], tm: pub(crate) fn show(app: &mut EffectcraftApp, ui: &mut egui::Ui, p: &egui::Painter, view: GraphView, area: Rect, actions: &mut Actions) { let GraphView { comp, ectx, tm } = view; let t = app.tokens; + let pal = GraphPalette::new(&t); let ctx = ui.ctx().clone(); let footer_h = 28.0; let plot = Rect::from_min_max(area.min + vec2(0.0, 6.0), pos2(area.max.x, area.max.y - footer_h)); let bar = Rect::from_min_max(pos2(area.min.x, area.max.y - footer_h), area.max); // Background first so keys and handles (added later) sit on top for hit-testing. let bg = ui.interact(plot, egui::Id::unique("graph-bg"), Sense::click_and_drag()); - p.rect_filled(area, 0.0, Color32::from_rgb(0x1d, 0x1d, 0x1d)); + p.rect_filled(area, 0.0, pal.bg); app.auto.add("timeline.graph", plot, &app.ui.timeline.graph_mode.clone()); let speed = speed_graph(app, comp); - let cs = curves(app, comp, ectx, tm, plot, speed); + let cs = curves(app, comp, ectx, tm, plot, speed, &pal); fit_on_open(app, &ctx, &cs, tm, plot, speed); // Vertical range: auto-zoom to the curves + handles, or the stored manual range. @@ -437,8 +520,8 @@ pub(crate) fn show(app: &mut EffectcraftApp, ui: &mut egui::Ui, p: &egui::Painte let mut gv = (lo / step).ceil() * step; while gv <= hi { let y = ymap(gv); - pc.line_segment([pos2(plot.min.x, y), pos2(plot.max.x, y)], Stroke::new(1.0, Color32::from_white_alpha(12))); - pc.text(pos2(plot.min.x + 4.0, y - 1.0), Align2::LEFT_BOTTOM, fmt(gv, step), Tokens::ui(10.0), t.text_faint); + pc.line_segment([pos2(plot.min.x, y), pos2(plot.max.x, y)], Stroke::new(1.0, pal.grid)); + pc.text(pos2(plot.min.x + 4.0, y - 1.0), Align2::LEFT_BOTTOM, fmt(gv, step), Tokens::ui(10.0), pal.label); gv += step; } if cs.is_empty() { @@ -447,12 +530,12 @@ pub(crate) fn show(app: &mut EffectcraftApp, ui: &mut egui::Ui, p: &egui::Painte Align2::CENTER_CENTER, crate::i18n::tr("Select animated properties to show them in the Graph Editor"), Tokens::ui(12.0), - t.text_faint, + pal.label, ); } if app.ui.timeline.graph_reference && !cs.is_empty() { - reference_graph(app, view, plot, speed, &pc); + reference_graph(app, view, plot, speed, &pc, &pal); } // Curves. for c in &cs { @@ -465,7 +548,7 @@ pub(crate) fn show(app: &mut EffectcraftApp, ui: &mut egui::Ui, p: &egui::Painte .flat_map(|c| c.keys.iter().filter(|k| k.sel).map(move |k| super::graph_tools::SelKey { t: k.t, v: k.v, dim: c.dim, editable: c.editable })) .collect(); let all_key_times: Vec<(u64, usize, f64)> = cs.iter().flat_map(|c| c.keys.iter().map(move |k| (c.prop.uid, k.idx, k.t))).collect(); - super::graph_tools::transform_box(app, ui, &pc, &sel_keys, tm, &ymap, &vmap, speed, false, actions); + super::graph_tools::transform_box(app, ui, &pc, &sel_keys, tm, &ymap, &vmap, speed, false, &pal, actions); // Keys and handles. let mut key_screens: Vec<(serde_json::Value, Pos2)> = vec![]; for c in &cs { @@ -484,8 +567,8 @@ pub(crate) fn show(app: &mut EffectcraftApp, ui: &mut egui::Ui, p: &egui::Painte // Handles (selected keys). for h in &k.handles { let hp = pos2(tm.x(h.t), ymap(h.v)); - pc.line_segment([kp, hp], Stroke::new(1.0, Color32::from_rgb(0xd8, 0xd8, 0x60))); - pc.circle_filled(hp, 3.5, Color32::from_rgb(0xd8, 0xd8, 0x60)); + pc.line_segment([kp, hp], Stroke::new(1.0, pal.handle)); + pc.circle_filled(hp, 3.5, pal.handle); let side = if h.out { "out" } else { "in" }; let hr = Rect::from_center_size(hp, vec2(10.0, 10.0)); let hresp = ui.interact(hr, egui::Id::unique(("ghandle", uid, d, k.idx, side)), Sense::drag()); @@ -539,7 +622,7 @@ pub(crate) fn show(app: &mut EffectcraftApp, ui: &mut egui::Ui, p: &egui::Painte } } // The key itself. - let (col, edge) = key_colors(&t, c.color, k); + let (col, edge) = key_colors(&t, &pal, c.color, k); pc.rect_filled(Rect::from_center_size(kp, vec2(7.0, 7.0)), 0.0, col); pc.rect_stroke(Rect::from_center_size(kp, vec2(7.0, 7.0)), 0.0, Stroke::new(1.0, edge), StrokeKind::Outside); let kr = Rect::from_center_size(kp, vec2(11.0, 11.0)); @@ -597,25 +680,24 @@ pub(crate) fn show(app: &mut EffectcraftApp, ui: &mut egui::Ui, p: &egui::Painte } } - super::graph_tools::transform_box(app, ui, &pc, &sel_keys, tm, &ymap, &vmap, speed, true, actions); + super::graph_tools::transform_box(app, ui, &pc, &sel_keys, tm, &ymap, &vmap, speed, true, &pal, actions); box_select(ui, &pc, &bg, &key_screens, t.accent, actions); wheel_zoom(app, ui, plot, (lo, hi)); - if button_bar(app, ui, p, comp, bar, speed, actions) { + if button_bar(app, ui, p, comp, ButtonBar::new(&ctx, bar, pal), speed, actions) { fit_selection(app, &cs, tm, plot, speed); } } /// The footer button bar: graph type, display toggles, fit, snap and keyframe interpolation. /// Returns whether Fit Selection was pressed. -fn button_bar(app: &mut EffectcraftApp, ui: &mut egui::Ui, p: &egui::Painter, comp: &Comp, bar: Rect, speed: bool, actions: &mut Actions) -> bool { +fn button_bar(app: &mut EffectcraftApp, ui: &mut egui::Ui, p: &egui::Painter, comp: &Comp, mut buttons: ButtonBar, speed: bool, actions: &mut Actions) -> bool { let t = app.tokens; - let ctx = ui.ctx().clone(); + let bar = buttons.bar; let mut fit_sel = false; p.rect_filled(bar, 0.0, t.panel_bg); p.line_segment([bar.left_top(), bar.right_top()], Stroke::new(1.0, t.separator)); let y = bar.center().y; - let mut buttons = ButtonBar::new(&ctx, bar); let tl = &app.ui.timeline; let (auto_type, show_sel, auto_on) = (tl.graph_mode == "auto", tl.graph_show_selected, tl.graph_auto_zoom); if text_button( @@ -706,7 +788,7 @@ fn button_bar(app: &mut EffectcraftApp, ui: &mut egui::Ui, p: &egui::Painter, co actions.push((cmd.into(), params)); } } - buttons.more(app, ui, p, bar); + buttons.more(app, ui, p); fit_sel } @@ -890,6 +972,76 @@ fn shift_axes(shift: bool, dx: f32, dy: f32) -> (bool, bool) { if shift { (dx.abs() >= dy.abs(), dy.abs() > dx.abs()) } else { (true, true) } } +#[cfg(test)] +mod theme_tests { + use super::{Color32, DIM_COLORS, DIM_COLORS_LIGHT, GraphPalette, PROP_COLORS, PROP_COLORS_LIGHT, min_contrast}; + use crate::theme::{ThemeKind, Tokens, contrast_ratio as contrast}; + + /// #3202: on every theme the Graph Editor's surfaces come from the theme, the bar buttons' + /// labels (lit and unlit) are readable on their fills, and the curves, keys, handles and the + /// transform box show on the plot. Before, the plot was a fixed #1d1d1d and the unlit buttons + /// a fixed #2c2c2c under the theme's text colour: Light's #222 text on them measured 1.1:1. + #[test] + fn the_graph_editor_follows_the_theme_and_stays_readable() { + let mut fills = vec![]; + for kind in [ThemeKind::Dark, ThemeKind::Darker, ThemeKind::Light] { + let t = Tokens::for_kind(kind); + let pal = GraphPalette::new(&t); + assert_eq!(pal.bg, t.tl_bg, "{kind:?}: the plot is the Timeline's time graph background"); + for (what, fg, bg, min) in [ + ("unlit button label", pal.button_text, pal.button, 4.5), + ("lit button label", pal.button_on_text, pal.button_on, 4.5), + ("hovered button label", pal.button_text, t.hover, 4.5), + ("grid value labels", pal.label, pal.bg, 4.5), + ("Bezier handles", pal.handle, pal.bg, 3.0), + ("unselected keys", pal.key, pal.bg, 3.0), + ("transform box", pal.box_line, pal.bg, 3.0), + ("transform box handles", pal.box_line, pal.box_fill, 3.0), + ("unlit button on the bar", pal.button, t.panel_bg, 1.05), + ("grid lines", pal.grid, pal.bg, 1.08), + ] { + let r = contrast(fg, bg); + assert!(r >= min, "{kind:?}: {what}: {r:.2}:1, needs {min}:1"); + } + for (i, c) in pal.dims.iter().chain(pal.props.iter()).enumerate() { + let r = contrast(*c, pal.bg); + assert!(r >= 3.0, "{kind:?}: curve colour {i} {c:?} on the plot: {r:.2}:1"); + } + fills.push((pal.bg, pal.button)); + } + assert_ne!(fills[0], fills[2], "Dark and Light draw the graph differently, not one fixed grey"); + // The dark curve colours would not do on the Light plot: that is why it has its own set. + let light = GraphPalette::new(&Tokens::for_kind(ThemeKind::Light)); + assert!(DIM_COLORS.iter().chain(PROP_COLORS.iter()).any(|c| contrast(*c, light.bg) < 3.0)); + } + + /// The brightness slider moves the plot (Dark lightened towards mid grey, Light darkened): + /// the plot still follows it, the curve set is whichever stands out more from that plot (not + /// the one the theme's name picks), and the lit button's label stays the more readable one. + #[test] + fn the_curve_set_follows_the_plot_under_the_brightness_slider() { + let bright: Vec = DIM_COLORS.iter().chain(PROP_COLORS.iter()).copied().collect(); + let dark: Vec = DIM_COLORS_LIGHT.iter().chain(PROP_COLORS_LIGHT.iter()).copied().collect(); + for kind in [ThemeKind::Dark, ThemeKind::Darker, ThemeKind::Light] { + for b in [-1.0, -0.5, 0.0, 0.5, 1.0] { + let t = Tokens::for_kind(kind).with_brightness(b); + let pal = GraphPalette::new(&t); + assert_eq!(pal.bg, t.tl_bg, "{kind:?} at {b}: the plot follows the slider"); + let shown: Vec = pal.dims.iter().chain(pal.props.iter()).copied().collect(); + let best = min_contrast(&bright, pal.bg).max(min_contrast(&dark, pal.bg)); + assert_eq!(min_contrast(&shown, pal.bg), best, "{kind:?} at {b}: the curves are the set that stands out more from {:?}", pal.bg); + let lit = contrast(pal.button_on_text, pal.button_on); + assert!(lit >= contrast(Color32::WHITE, pal.button_on).max(contrast(t.text, pal.button_on)), "{kind:?} at {b}: lit label {lit:.2}:1"); + } + } + // Light darkened all the way is a dark plot: it gets the bright curves, as Dark does. + let dimmed = GraphPalette::new(&Tokens::for_kind(ThemeKind::Light).with_brightness(-1.0)); + assert_eq!(dimmed.props, PROP_COLORS); + let lit = GraphPalette::new(&Tokens::for_kind(ThemeKind::Light)); + assert_eq!(lit.props, PROP_COLORS_LIGHT); + } +} + #[cfg(test)] mod tests { use super::shift_axes; diff --git a/crates/ui-egui/src/panels/graph_tools.rs b/crates/ui-egui/src/panels/graph_tools.rs index 7950d4a66..d611051a0 100644 --- a/crates/ui-egui/src/panels/graph_tools.rs +++ b/crates/ui-egui/src/panels/graph_tools.rs @@ -71,6 +71,7 @@ pub(crate) fn transform_box( vmap: &dyn Fn(f32) -> f64, speed: bool, handles: bool, + pal: &super::graph::GraphPalette, actions: &mut Actions, ) { if !app.ui.timeline.graph_transform_box { @@ -79,7 +80,7 @@ pub(crate) fn transform_box( let Some(b) = bounds(keys) else { return }; let r = Rect::from_min_max(pos2(tm.x(b[0]), ymap(b[3])), pos2(tm.x(b[2]), ymap(b[1]))).expand(6.0); let value_ok = !speed && keys.iter().all(|k| k.editable); - let col = Color32::from_rgb(0xd8, 0xd8, 0xd8); + let col = pal.box_line; let ctx = ui.ctx().clone(); let drag_id = egui::Id::unique("graph-dragging"); let hp = handle_points(r); @@ -116,7 +117,7 @@ pub(crate) fn transform_box( continue; } let hr = Rect::from_center_size(*h, vec2(7.0, 7.0)); - p.rect_filled(hr, 0.0, Color32::from_gray(0x20)); + p.rect_filled(hr, 0.0, pal.box_fill); p.rect_stroke(hr, 0.0, Stroke::new(1.0, col), StrokeKind::Middle); let resp = ui.interact(hr.expand(2.0), egui::Id::unique(("graph-box-handle", i)), Sense::drag()); app.auto.add(&format!("timeline.graph.transformBox.{i}"), hr, "Transform box handle"); diff --git a/crates/ui-egui/src/theme.rs b/crates/ui-egui/src/theme.rs index e67bd2d7d..e18d834ee 100644 --- a/crates/ui-egui/src/theme.rs +++ b/crates/ui-egui/src/theme.rs @@ -111,6 +111,21 @@ pub struct Tokens { pub gradients: bool, } +/// WCAG 2 relative luminance of an opaque sRGB colour (alpha is ignored). +pub fn relative_luminance(c: Color32) -> f32 { + let f = |v: u8| { + let v = f32::from(v) / 255.0; + if v <= 0.04045 { v / 12.92 } else { ((v + 0.055) / 1.055).powf(2.4) } + }; + 0.2126 * f(c.r()) + 0.7152 * f(c.g()) + 0.0722 * f(c.b()) +} + +/// WCAG 2 contrast ratio of two opaque sRGB colours (1 to 21). +pub fn contrast_ratio(a: Color32, b: Color32) -> f32 { + let (x, y) = (relative_luminance(a), relative_luminance(b)); + (x.max(y) + 0.05) / (x.min(y) + 0.05) +} + /// A vertical gradient fill (`top` → `bottom`) over `rect`. pub fn gradient_rect(painter: &egui::Painter, rect: egui::Rect, top: Color32, bottom: Color32) { let mut m = egui::Mesh::default(); diff --git a/crates/ui-egui/tests/it/main.rs b/crates/ui-egui/tests/it/main.rs index 422f0223e..d8cdc5cb3 100644 --- a/crates/ui-egui/tests/it/main.rs +++ b/crates/ui-egui/tests/it/main.rs @@ -46,6 +46,7 @@ mod ui_gradient_editor; mod ui_gradient_handles; mod ui_gradient_value; mod ui_graph_fit; +mod ui_graph_theme; mod ui_group_rename; mod ui_guide_layers; mod ui_info_tracker_a11y; diff --git a/crates/ui-egui/tests/it/ui_graph_theme.rs b/crates/ui-egui/tests/it/ui_graph_theme.rs new file mode 100644 index 000000000..49c6689a0 --- /dev/null +++ b/crates/ui-egui/tests/it/ui_graph_theme.rs @@ -0,0 +1,102 @@ +//! #3202: the Graph Editor follows the theme (egui_kittest, a real render). Its plot used to be a +//! fixed dark grey in every theme, and its bar buttons a fixed dark fill under the theme's text +//! colour, so in Light their labels (dark text) only showed while hovered. Now the plot takes the +//! Timeline's time graph background and every bar button's label is readable on its fill, lit or +//! not, in Dark and Light. `GRAPH_THEME_SNAPSHOT=/abs/dir` also saves the rendered window. + +use effectcraft_engine::Session; +use effectcraft_ui_egui::EffectcraftApp; +use effectcraft_ui_egui::theme::ThemeKind; +use egui::Color32; +use egui_kittest::Harness; +use serde_json::json; + +/// WCAG contrast ratio of two rendered pixels. +fn contrast(a: [u8; 4], b: [u8; 4]) -> f32 { + effectcraft_ui_egui::theme::contrast_ratio(Color32::from_rgb(a[0], a[1], a[2]), Color32::from_rgb(b[0], b[1], b[2])) +} + +fn close(got: [u8; 4], want: Color32) -> bool { + got[..3].iter().zip(want.to_array()).all(|(a, b)| a.abs_diff(b) <= 3) +} + +#[test] +fn the_graph_editor_follows_the_theme() { + let _one_at_a_time = crate::gpu_test::lock(); + let mut s = Session::default(); + s.execute("comp.new", json!({"name": "Theme", "width": 640, "height": 360, "duration": 4})).unwrap(); + s.execute("layer.newSolid", json!({"name": "Box", "color": "#204060", "width": 80, "height": 80})).unwrap(); + // An animated property on the selected layer, so the graph shows a curve and its keys. + let id = s.active_comp().unwrap().layers[0].id; + for (t, v) in [(0.5, 0.0), (2.0, 180.0), (3.5, 90.0)] { + s.execute("prop.addKey", json!({"layer": id.0, "path": "transform/rotation", "time": t, "value": v})).unwrap(); + } + s.execute("layer.select", json!({"layers": [id.0]})).unwrap(); + + let mut h = Harness::builder().with_size(egui::vec2(1600.0, 1000.0)).build_eframe(move |_| EffectcraftApp::new(s)); + h.state_mut().ui.timeline.graph_editor = true; + h.run_steps(4); + let ctx = h.ctx.clone(); + + let mut plots = vec![]; + let mut failures = vec![]; + for (kind, name) in [(ThemeKind::Dark, "dark"), (ThemeKind::Light, "light")] { + h.state_mut().set_theme(&ctx, kind); + h.run_steps(4); + let tokens = h.state().tokens; + assert_eq!(tokens.kind, kind); + let plot = h.state().auto.find("timeline.graph").unwrap_or_else(|| panic!("the Graph Editor is shown ({name})")).rect; + let buttons: Vec<(&str, [f32; 4])> = ["valueGraph", "speedGraph"] + .iter() + .map(|id| (*id, h.state().auto.find(&format!("timeline.graph.{id}")).unwrap_or_else(|| panic!("{id} is on the bar ({name})")).rect)) + .collect(); + let image = h.render().expect("the window renders"); + if let Some(dir) = std::env::var_os("GRAPH_THEME_SNAPSHOT") { + image.save(std::path::Path::new(&dir).join(format!("graph-theme-{name}.png"))).unwrap(); + } + let scale = image.width() as f32 / 1600.0; + let at = |px: f32, py: f32| { + let (x, y) = (((px * scale) as u32).min(image.width() - 1), ((py * scale) as u32).min(image.height() - 1)); + image.get_pixel(x, y).0 + }; + + // The plot: the commonest colour down a column near its right edge (clear of the value + // labels on the left and of the scroll bar; grid lines and the curve cross it only at a + // sample or two). + let x = plot[0] + plot[2] - 20.0; + let mut column: Vec<[u8; 4]> = (0..24).map(|i| at(x, plot[1] + 8.0 + (plot[3] - 16.0) * i as f32 / 23.0)).collect(); + column.sort_unstable(); + let mut runs: Vec<([u8; 4], usize)> = vec![]; + for px in column { + match runs.last_mut() { + Some((c, n)) if *c == px => *n += 1, + _ => runs.push((px, 1)), + } + } + let bg = runs.iter().max_by_key(|(_, n)| *n).map(|(c, _)| *c).unwrap(); + if !close(bg, tokens.tl_bg) { + failures.push(format!("{name}: the plot is {bg:?}, the theme's time graph background is {:?}", tokens.tl_bg)); + } + plots.push(bg); + + // Each button (Value is lit, Speed is not): its fill left of the label, and the label's + // ink, the pixel across its middle rows that stands out most from the fill. Only the + // label's span is scanned (7 points of padding each side), not the button's feathered + // edges, which blend the fill with the bar behind it. + for (id, r) in buttons { + let mid = r[1] + r[3] / 2.0; + let fill = at(r[0] + 2.5, mid); + let ink = (-3..=3) + .flat_map(|dy| (3..(r[2] as i32 - 3)).map(move |dx| (r[0] + dx as f32, mid + dy as f32))) + .map(|(px, py)| at(px, py)) + .max_by(|a, b| contrast(*a, fill).total_cmp(&contrast(*b, fill))) + .unwrap(); + let c = contrast(ink, fill); + if c < 3.0 { + failures.push(format!("{name}: the {id} button's label {ink:?} on its fill {fill:?} is {c:.2}:1")); + } + } + } + assert!(failures.is_empty(), "{}", failures.join("\n")); + assert_ne!(plots[0], plots[1], "the plot looks different in Dark and in Light, so it is not a fixed grey"); +} From 795b019deb3dcafc329d29ce03250813840fc1f1 Mon Sep 17 00:00:00 2001 From: vangel76 Date: Sat, 7 Nov 2026 14:11:02 +0100 Subject: [PATCH 07/74] Timeline waveform: read keyframed Audio Levels at layer time The waveform scaled each column by Audio Levels evaluated at comp time, but keyframe times are layer time. On a layer with a start offset or a stretch the drawn level was shifted against what the mix plays. Read the levels at layer.layer_time(t), as the render evaluator does. Fixes #3224 --- crates/ui-egui/src/panels/waveform.rs | 79 ++++++++++++++++++++++++--- 1 file changed, 72 insertions(+), 7 deletions(-) diff --git a/crates/ui-egui/src/panels/waveform.rs b/crates/ui-egui/src/panels/waveform.rs index 4e355f951..91c5a8458 100644 --- a/crates/ui-egui/src/panels/waveform.rs +++ b/crates/ui-egui/src/panels/waveform.rs @@ -76,11 +76,14 @@ pub fn summary(app: &EffectcraftApp, ctx: &egui::Context, item: ItemId) -> Optio None } -/// Left/right gain of the layer's Audio Levels (dB) at comp time `t`; unity without the property. -fn level_gains(levels: Option<&Property>, t: Tick) -> [f32; 2] { +/// Left/right gain of `layer`'s Audio Levels (`levels`, dB) at comp time `t`; unity without the +/// property. Keyframes are in layer time, so they are read at the layer time of `t` (start +/// offset and stretch), as the mix reads them. A reversed layer (negative stretch) is still +/// drawn, though the mix leaves it silent. +fn level_gains(layer: &Layer, levels: Option<&Property>, t: Tick) -> [f32; 2] { match levels { Some(p) => { - let [l, r] = p.value_at(t).as_vec2(); + let [l, r] = p.value_at(layer.layer_time(t)).as_vec2(); [db_to_gain(l), db_to_gain(r)] } None => [1.0, 1.0], @@ -114,7 +117,7 @@ pub(crate) fn draw(p: &Painter, rect: Rect, tm: &TMap, layer: &Layer, s: &Summar agg[2] = agg[2].min(b[2]); agg[3] = agg[3].max(b[3]); } - let gain = level_gains(levels, Tick::from_seconds_f64((ta + tb) * 0.5)); + let gain = level_gains(layer, levels, Tick::from_seconds_f64((ta + tb) * 0.5)); for c in 0..2 { let (lo, hi) = ((agg[c * 2] * gain[c]).clamp(-1.0, 1.0), (agg[c * 2 + 1] * gain[c]).clamp(-1.0, 1.0)); let (ya, yb) = (mid[c] - hi * half, mid[c] - lo * half); @@ -126,14 +129,76 @@ pub(crate) fn draw(p: &Painter, rect: Rect, tm: &TMap, layer: &Layer, s: &Summar #[cfg(test)] mod tests { use super::*; - use effectcraft_engine::project::Value; + use effectcraft_engine::color::Label; + use effectcraft_engine::project::{Comp, Footage, FootageKind, Keyframe, Value, build}; + use effectcraft_engine::render::{FootageSource, Image, audio::mix_comp}; + use effectcraft_engine::time::FrameRate; #[test] fn waveform_follows_audio_levels() { let p = Property::new(1, "levels", "Audio Levels", Value::Vec2([-6.0206, -96.0])); - let g = level_gains(Some(&p), Tick::ZERO); + let mut project = Project::default(); + let comp = Comp::new(64, 64, FrameRate::new(25, 1), Tick::from_seconds_f64(2.0)); + let l = build::layer(&mut project, &comp, "a", LayerSource::Null, (0, 0), None); + let g = level_gains(&l, Some(&p), Tick::ZERO); assert!((g[0] - 0.5).abs() < 1e-3, "{}", g[0]); assert_eq!(g[1], 0.0); - assert_eq!(level_gains(None, Tick::ZERO), [1.0, 1.0]); + assert_eq!(level_gains(&l, None, Tick::ZERO), [1.0, 1.0]); + } + + /// Constant 0.5 audio on both channels, at any source time. + struct Half; + impl FootageSource for Half { + fn frame(&self, _: ItemId, _: &Footage, _: Tick) -> Option> { + None + } + fn audio(&self, _: ItemId, _: &Footage, _: Tick, frames: usize, _: u32) -> Option> { + Some(vec![0.5; frames * 2]) + } + } + + /// #3224: keyframed Audio Levels scale the drawn waveform at the layer time the mix reads + /// them, so on a layer that starts later (or is stretched) a column's gain is the gain the + /// rendered audio has at that comp time. + #[test] + fn keyed_levels_scale_the_waveform_as_the_mix_does() { + let mut p = Project::default(); + let f = Footage { + path: "x.wav".into(), + kind: FootageKind::Audio, + has_audio: true, + frame_rate: FrameRate::new(25, 1), + duration: Tick::from_seconds_f64(10.0), + ..Default::default() + }; + let fid = p.add_item("x.wav", Label::SeaFoam, None, ItemKind::Footage(f)); + let base = Comp::new(64, 64, FrameRate::new(25, 1), Tick::from_seconds_f64(10.0)); + let mut l = build::layer(&mut p, &base, "a", LayerSource::Footage { item: fid }, (0, 0), None); + // -6 dB at layer time 0 s, 0 dB at layer time 1 s. + let Some(levels) = l.props.sub_mut("audio").and_then(|g| g.get_mut("levels")) else { panic!("no Audio Levels") }; + levels.keys = vec![Keyframe::new(Tick::ZERO, Value::Vec2([-6.0206, -6.0206])), Keyframe::new(Tick::from_seconds_f64(1.0), Value::Vec2([0.0, 0.0]))]; + for stretch in [100.0, 200.0] { + let mut l = l.clone(); + l.start_time = Tick::from_seconds_f64(2.0); + l.stretch = stretch; + l.in_point = l.start_time; + l.out_point = Tick::from_seconds_f64(9.0); + let mut comp = base.clone(); + comp.layers = vec![l.clone()].into(); + let cid = p.add_item("C", Label::Sandstone, None, ItemKind::Comp(comp.into())); + let levels = l.props.sub("audio").and_then(|g| g.get("levels")); + // The layer's start, a quarter and half of its ramp, its end, and after it. + for s in [2.0, 2.25, 2.5, 3.0, 4.0, 6.0] { + let t = Tick::from_seconds_f64(s); + let drawn = level_gains(&l, levels, t); + let mixed = mix_comp(&p, &Half, None, cid, t, 1, 1000); + for c in 0..2 { + let heard = mixed.get(c).copied().unwrap_or(f32::NAN) / 0.5; + assert!((drawn[c] - heard).abs() < 2e-3, "stretch {stretch}, {s} s, channel {c}: drawn {} heard {heard}", drawn[c]); + } + } + // At the layer's start (layer time 0) it is the -6 dB key, not the 0 dB one. + assert!((level_gains(&l, levels, Tick::from_seconds_f64(2.0))[0] - 0.5).abs() < 1e-3); + } } } From cdbb884f28e29221e910654ca641b43b18887f81 Mon Sep 17 00:00:00 2001 From: vangel76 Date: Sat, 7 Nov 2026 14:25:32 +0100 Subject: [PATCH 08/74] Scripting: expressionError reports evaluation errors, as After Effects Property.expressionError only compiled the expression, so one that compiles but throws or yields a non-finite number read as "" while expr.errors reported it. It now evaluates the property's expression at the comp's current time through the same per-property check expr.errors uses (expr_tools::property_error), without rendering anything. Without an evaluator, an enabled expression falls back to the syntax check. Fixes #3219 --- crates/engine/src/cmd/expr_tools.rs | 24 ++++++++++++-------- crates/script/src/model.rs | 23 +++++++++++++------ crates/script/src/prelude.js | 4 ++-- crates/script/src/tests.rs | 35 +++++++++++++++++++++++++++++ 4 files changed, 68 insertions(+), 18 deletions(-) diff --git a/crates/engine/src/cmd/expr_tools.rs b/crates/engine/src/cmd/expr_tools.rs index cb39077af..e55dc7b20 100644 --- a/crates/engine/src/cmd/expr_tools.rs +++ b/crates/engine/src/cmd/expr_tools.rs @@ -65,15 +65,7 @@ pub fn errors_in(ctx: &EvalCtx, check: Option std::result::Result<() if !keep_going() { return None; } - let msg = if e.enabled { - match &ctx.expr { - Some(h) => h.eval(ctx, l, p, &p.value_at(l.layer_time(ctx.time))).err(), - None => None, - } - } else { - check.and_then(|check| check(&e.text).err()) - }; - if let Some(message) = msg { + if let Some(message) = property_error(ctx, check, l, p) { out.push(error_of(ctx.comp_id, i, l, p, message, !e.enabled)); } } @@ -81,6 +73,20 @@ pub fn errors_in(ctx: &EvalCtx, check: Option std::result::Result<() keep_going().then_some(out) } +/// Why property `p` of layer `l` fails at `ctx.time` (comp time), as `expr.errors` reports it and +/// scripts read it as `Property.expressionError`: the evaluation error (an exception or a +/// non-finite result) of an enabled expression, or the syntax error that disabled one. `None` +/// without an expression or when it evaluates. Evaluates this one expression; renders nothing. +/// Without an evaluator an enabled expression gets the syntax check, so a broken one (from a +/// project file) still reports why. +pub fn property_error(ctx: &EvalCtx, check: Option std::result::Result<(), String>>, l: &Layer, p: &Property) -> Option { + let e = p.expr.as_ref().filter(|e| !e.text.trim().is_empty())?; + match ctx.expr { + Some(h) if e.enabled => h.eval(ctx, l, p, &p.value_at(l.layer_time(ctx.time))).err(), + _ => check.and_then(|check| check(&e.text).err()), + } +} + fn error_of(cid: ItemId, i: usize, l: &Layer, p: &Property, message: String, disabled: bool) -> ExprError { ExprError { comp: cid.0, diff --git a/crates/script/src/model.rs b/crates/script/src/model.rs index e662c1cdc..1140820c6 100644 --- a/crates/script/src/model.rs +++ b/crates/script/src/model.rs @@ -51,13 +51,7 @@ pub fn query(s: &mut Session, kind: &str, a: &J) -> R { let i = u(a, "index").unwrap_or(0) as usize; Ok(effectcraft_engine::command_specs().get(i.wrapping_sub(1)).map(|c| json!(c.id)).unwrap_or(J::Null)) } - "exprError" => { - let code = a.get("code").and_then(J::as_str).unwrap_or(""); - Ok(match s.expr_check { - Some(check) => json!(check(code).err().unwrap_or_default()), - None => json!(""), - }) - } + "exprError" => expr_error(s, a), "labels" => Ok(json!(Label::ALL.iter().map(|l| l.name()).collect::>())), "effects" => Ok(json!( effectcraft_engine::effects::all() @@ -529,6 +523,21 @@ fn value(s: &Session, a: &J) -> R { Ok(js_value(c, &v)) } +/// `Property.expressionError`: why the property's expression fails at the comp's current time +/// (where After Effects evaluates it; the prelude sends no `time`, a direct query may give one +/// in comp seconds). The message is the one `expr.errors` gives: an evaluation error (an +/// exception or a non-finite result) or the syntax error that disabled it; `""` when it +/// evaluates (#3219). +fn expr_error(s: &Session, a: &J) -> R { + let (cid, c, l) = layer_of(s, a)?; + let uid = u(a, "uid").ok_or("missing uid")?; + let p = l.props.find(uid).ok_or("the property no longer exists (it was deleted)")?; + let t = f(a, "time").map(Tick::from_seconds_f64).unwrap_or_else(|| s.time_of(cid)); + // With the footage source, like `value`: `sampleImage` must not fail for want of it (#1000). + let ctx = EvalCtx { footage: Some(s.footage.as_ref()), expr: s.expr.as_deref(), ..EvalCtx::new(&s.project, cid, c, t) }; + Ok(json!(effectcraft_engine::commands::expr_tools::property_error(&ctx, s.expr_check, l, p).unwrap_or_default())) +} + fn interp(i: Interp) -> &'static str { match i { Interp::Linear => "LINEAR", diff --git a/crates/script/src/prelude.js b/crates/script/src/prelude.js index b9108b2a1..a25a37a4b 100644 --- a/crates/script/src/prelude.js +++ b/crates/script/src/prelude.js @@ -799,8 +799,8 @@ Property.prototype.property = function () { return null; }; __call("prop.setExpression", this.__ref({ enabled: !!v })); }); def("expressionError", function () { - var e = this.expression; - return e ? __get("exprError", { code: e }) : ""; + // Evaluated at the comp's current time, so a runtime error or a non-finite result shows too. + return this.expression ? __get("exprError", { comp: this.__layer.__comp, layer: this.__layer.__id, uid: this.__uid }) : ""; }); def("canSetExpression", function () { return !!this.__info().canSetExpression; }); def("canVaryOverTime", function () { return !!this.__info().canVaryOverTime; }); diff --git a/crates/script/src/tests.rs b/crates/script/src/tests.rs index 9e934b555..44b561d5d 100644 --- a/crates/script/src/tests.rs +++ b/crates/script/src/tests.rs @@ -1252,6 +1252,41 @@ fn sample_image_reads_footage_pixels_in_scripts() { assert_eq!(out.result, json!([0, 1, 0, 1, ""]), "green footage sampled, not transparent black"); } +/// `expressionError` reports what evaluating the expression at the comp's current time does, as +/// After Effects: a non-finite result and an exception, not only a syntax error (#3219). +#[test] +fn expression_error_reports_evaluation_errors() { + let mut s = session(); + let out = ok( + &mut s, + r#" + var c = app.project.items.addComp("E", 320, 240, 1, 5, 30); + var p = c.layers.addSolid([1, 1, 1], "S", 320, 240, 1).transform.opacity; + p.expression = "Math.sqrt(-1)"; + var nan = p.expressionError; + p.expression = 'throw new Error("boom")'; + var thrown = p.expressionError; + p.expression = "value"; + var fine = p.expressionError; + p.expression = "thisLayer.nope("; + var syntax = p.expressionError; + p.expression = "time > 1 ? Math.sqrt(-1) : value"; + c.time = 0; + var early = p.expressionError; + c.time = 2; + var late = p.expressionError; + [nan, thrown, fine, syntax.length > 0, early, late]; + "#, + ); + let v = out.result; + assert!(v[0].as_str().is_some_and(|e| e.contains("not a finite number")), "{v}"); + assert!(v[1].as_str().is_some_and(|e| e.contains("boom")), "{v}"); + assert_eq!(v[2], json!(""), "{v}"); + assert_eq!(v[3], json!(true), "a syntax error is still reported: {v}"); + assert_eq!(v[4], json!(""), "evaluated at the current time (0), where it is fine: {v}"); + assert!(v[5].as_str().is_some_and(|e| e.contains("not a finite number")), "evaluated at the current time (2): {v}"); +} + #[test] fn binary_file_io_preserves_all_byte_values() { let dir = std::env::temp_dir().join(format!("ec-script-binary-{}", std::process::id())); From d733a699fcfcf3d90811a96f1626f9e857746552 Mon Sep 17 00:00:00 2001 From: vangel76 Date: Sat, 7 Nov 2026 13:51:32 +0100 Subject: [PATCH 09/74] Viewer snapping: a point beats a line pair through it unless the pair is clearly nearer (#2681) Moving a circle so its edge point met another circle's point snapped at 200 % but missed at 100 %. A layer's point lies on its own edge line and centre row, so a vertical and a horizontal line through it tie with it, and the point only won that tie by 1e-9. Any other line a little nearer on one axis (the comp's centre row, a guide) then made a pair nearer than the point, taking x from the edge and y from that line and leaving the moved circle off the point. At 100 % the tolerance (8 screen points) covers more comp pixels and reaches those lines; zoomed in they were out of reach. Before #2855 a single nearer line was enough, which is how it was reported on 0.18.0. snap() now lets a line pair with a line through the point beat that point only when the pair is nearer by POINT_BIAS of the tolerance (2 of the 8 screen points at any zoom), in place of the exact tie. A pair whose lines both miss the point gets no margin, so a guide crossing right beside a layer's point stays reachable; two clearly nearer lines still win, as #2855 intended. Tests: two Ellipse-tool circles, parametric and converted to Bezier paths, at 1x, 2x and 4x on device scales 1, 1.25 and 1.5, dropped just off the point and nearer the comp's centre row than the circle's own row, with and without snapping to a guide between them: the moved point lands exactly on the other's, a single point hit (a path point for the Bezier circles). The margin: a pair through the point only a little nearer leaves the point, a clearly nearer one wins; a guide crossing 1 px beside a point is reached next to the crossing. Moving a layer snaps the feature nearest where it was grabbed, as before (the snap handle, as in After Effects): grab the circle near its edge point to snap that point; grabbed in the middle, its centre snaps. Fixes #2681 --- crates/engine/src/tests/viewer.rs | 142 +++++++++++++++++++++++++++++- crates/engine/src/viewer.rs | 27 +++++- 2 files changed, 164 insertions(+), 5 deletions(-) diff --git a/crates/engine/src/tests/viewer.rs b/crates/engine/src/tests/viewer.rs index 31197b2a8..d2c2180c1 100644 --- a/crates/engine/src/tests/viewer.rs +++ b/crates/engine/src/tests/viewer.rs @@ -8,7 +8,9 @@ use effectcraft_time::Tick; use serde_json::json; use crate::Session; -use crate::viewer::{Channel, DisplayPass, FrameEncoding, PRI_GRID, PRI_GUIDE, PRI_LAYER, PRI_VERTEX, PassCurve, SnapKind, SnapSource, SnapTarget, snap}; +use crate::viewer::{ + Channel, DisplayPass, FrameEncoding, PRI_COMP, PRI_GRID, PRI_GUIDE, PRI_LAYER, PRI_VERTEX, PassCurve, SnapKind, SnapSource, SnapTarget, snap, +}; fn comp() -> Session { let mut s = Session::default(); @@ -84,6 +86,46 @@ fn snap_point_beats_one_line_and_loses_only_to_two_nearer_lines() { assert_eq!(s.hits, [v, h]); } +/// #2681: a pair of lines, one of them through the point (the layer's own edge line), beats the +/// point only when it is clearly nearer (by `POINT_BIAS` of the tolerance, 2 of 8 here); a pair +/// only a little nearer leaves the feature on the point. +#[test] +fn a_line_pair_through_a_point_beats_it_only_by_a_clear_margin() { + use crate::viewer::POINT_BIAS; + assert_eq!(8.0 * POINT_BIAS, 2.0); + let src = SnapSource::Layer(LayerId(1)); + let edge = SnapTarget::vline(100.0, PRI_LAYER, src); + let row = SnapTarget::hline(51.5, PRI_COMP, SnapSource::Comp); + // The point is 5 away; the pair (3 and ½) about 3.04, under 2 nearer: the point. + let pt = SnapTarget::point([100.0, 47.0], PRI_LAYER, src); + let s = snap(&[[103.0, 51.0]], &[edge, row, pt], 8.0).unwrap(); + assert_eq!(s.hits, [pt]); + assert_eq!(s.at, [100.0, 47.0]); + // The point about 6.7 away, the same pair more than 2 nearer: the pair. + let far = SnapTarget::point([100.0, 45.0], PRI_LAYER, src); + let s = snap(&[[103.0, 51.0]], &[edge, row, far], 8.0).unwrap(); + assert_eq!(s.hits, [edge, row]); + assert_eq!(s.at, [100.0, 51.5]); +} + +/// #2681: two guides crossing 1 px beside a layer's point are reachable right next to it: a pair +/// of lines that both miss the point gets no margin, only the nearer one wins. +#[test] +fn a_guide_intersection_beside_a_point_stays_reachable() { + let src = SnapSource::Layer(LayerId(1)); + let pt = SnapTarget::point([100.0, 50.0], PRI_LAYER, src); + let own = [SnapTarget::vline(100.0, PRI_LAYER, src), SnapTarget::hline(50.0, PRI_LAYER, src)]; + let guides = [SnapTarget::vline(101.0, PRI_GUIDE, SnapSource::Guide), SnapTarget::hline(51.0, PRI_GUIDE, SnapSource::Guide)]; + let t = [own[0], own[1], guides[0], guides[1], pt]; + // Nearer the intersection (about 0.71) than the point (about 2.12): the intersection. + let s = snap(&[[101.5, 51.5]], &t, 8.0).unwrap(); + assert_eq!(s.at, [101.0, 51.0]); + assert_eq!(s.hits, guides); + // Nearer the point: the point. + let s = snap(&[[100.3, 50.3]], &t, 8.0).unwrap(); + assert_eq!(s.hits, [pt]); +} + /// #2855: an unrotated layer's centre lines follow its edges' span rule, reach the outer comp /// through a collapsed precomp layer, and a rotated layer keeps its points only. #[test] @@ -116,6 +158,104 @@ fn layer_centre_lines_follow_the_edges() { assert_eq!(lines(&at(&s, off), pre), (None, None)); } +/// #2681: two Ellipse-tool circles, one moved so its left point meets the other's right point. +/// The point must land exactly on the other circle's point at 100 % as at 200 % and 400 %, on +/// HiDPI displays too. The tolerance is the viewer's (8 screen points over the zoom in points per +/// comp pixel), so at 100 % it reaches the comp's centre row 6 px below the circles and a guide +/// 3 px below them. Neither may pull the circle off the point: the other circle's own edge line +/// and row pass through its point, so a pair of lines made with that row or guide is only a +/// little nearer than the point (the row or guide a few pixels nearer on y), and a point wins +/// unless a pair is clearly nearer (`POINT_BIAS`). Before #2855 one nearer line was enough, and +/// even after it any pair nearer by a hair; zoomed in the row and guide were out of reach, so +/// the snap looked right there and not at 100 %. +#[test] +fn circles_snap_point_to_point_at_every_zoom() { + circles_snap_point_to_point(false); +} + +/// #2681 with the circles converted to Bezier paths (Convert To Bezier Path): the dragged path +/// point lands on the other circle's path point (a vertex target) the same way. +#[test] +fn bezier_circles_snap_path_point_to_path_point_at_every_zoom() { + circles_snap_point_to_point(true); +} + +/// The #2681 drops: parametric circles (their edge midpoints snap) or, with `bezier`, the same +/// circles as Bezier paths (their path points snap). +fn circles_snap_point_to_point(bezier: bool) { + use crate::viewer::{SnapOptions, layer_features, layer_vertices, targets}; + let mut s = comp(); + // Circle A: 100 px, centred at (200, 174), its right point at (250, 174); the comp's centre + // row is y = 180, a horizontal guide at y = 177. + s.execute("view.addGuide", json!({"orientation": "horizontal", "position": 177})).unwrap(); + let a = s.execute("shape.newShape", json!({"kind": "ellipse", "size": [100, 100], "position": [200, 174]})).unwrap()["layer"].as_u64().unwrap(); + s.execute("edit.deselectAll", json!({})).unwrap(); + let b = s.execute("shape.newShape", json!({"kind": "ellipse", "size": [100, 100], "position": [400, 300]})).unwrap()["layer"].as_u64().unwrap(); + assert_ne!(a, b, "two shape layers"); + if bezier { + for l in [a, b] { + fn ellipse(g: &effectcraft_project::PropGroup) -> Option { + if g.match_id == "ellipse" { + return Some(g.uid); + } + g.groups().find_map(ellipse) + } + let uid = ellipse(layer(&s, l).props.sub("contents").unwrap()).unwrap(); + s.state.selected_props = vec![(LayerId(l), uid)]; + s.execute("path.convertToBezier", json!({})).unwrap(); + } + } + let cid = s.active_comp_id().unwrap(); + // The circle's right (`right`) or left point: an edge midpoint of its box (features: + // corners, edge midpoints top, right, bottom, left, centre, anchor point), or a path point. + let point = |s: &Session, id: u64, right: bool| { + let comp = s.project.comp(cid).unwrap().clone(); + let ctx = effectcraft_render::EvalCtx::new(&s.project, cid, &comp, Tick::ZERO); + let l = comp.layer(LayerId(id)).unwrap(); + if !bezier { + return layer_features(&ctx, l)[if right { 5 } else { 7 }]; + } + let v = layer_vertices(&ctx, l); + assert!(v.len() >= 4, "a Bezier circle: {v:?}"); + let x = |p: &&[f64; 2]| p[0]; + *(if right { v.iter().max_by(|p, q| x(p).total_cmp(&x(q))) } else { v.iter().min_by(|p, q| x(p).total_cmp(&x(q))) }).unwrap() + }; + let near = |p: [f64; 2], q: [f64; 2], e: f64| (p[0] - q[0]).abs() < e && (p[1] - q[1]).abs() < e; + let right_of_a = point(&s, a, true); + assert!(near(right_of_a, [250.0, 174.0], 1e-6), "{right_of_a:?}"); + let left_of_b = point(&s, b, false); + assert!(near(left_of_b, [350.0, 300.0], 1e-6), "{left_of_b:?}"); + let to_left = [left_of_b[0] - 400.0, left_of_b[1] - 300.0]; + let source = if bezier { SnapSource::Vertex(LayerId(a)) } else { SnapSource::Layer(LayerId(a)) }; + // B's left point dropped right of and below A's right point (screen points): just off it, + // and nearer the comp's centre row (at 100 %) than A's own row. With and without snapping to + // the guide. + for (dx, dy) in [(3.0, 1.0), (3.0, 3.5)] { + for guides in [false, true] { + for ppp in [1.0, 1.25, 1.5] { + for magnification in [1.0, 2.0, 4.0] { + let case = format!("bezier {bezier}, drop ({dx}, {dy}) pt, guides {guides}, {magnification}x, ppp {ppp}"); + // The viewer's zoom (points per comp pixel) and snap tolerance (`SNAP_PX / zoom`). + let zoom: f64 = magnification / ppp; + let tol = 8.0 / zoom; + let want = [right_of_a[0] + dx / zoom, right_of_a[1] + dy / zoom]; + s.execute("prop.set", json!({"layer": b, "path": "transform/position", "value": [want[0] - to_left[0], want[1] - to_left[1], 0]})).unwrap(); + let src = point(&s, b, false); + assert!(near(src, want, 1e-9), "{case}: {src:?} {want:?}"); + let comp = s.project.comp(cid).unwrap().clone(); + let ctx = effectcraft_render::EvalCtx::new(&s.project, cid, &comp, Tick::ZERO); + let t = targets(&ctx, &[LayerId(b)], SnapOptions { layers: true, guides, ..Default::default() }); + let hit = snap(&[src], &t, tol).unwrap_or_else(|| panic!("{case}: nothing snapped")); + assert!(near(hit.at, right_of_a, 1e-9), "{case}: B's left point lands at {:?}, not on A's right point {right_of_a:?}", hit.at); + assert_eq!(hit.hits.len(), 1, "{case}: one point, not two lines: {:?}", hit.hits); + assert_eq!(hit.hits[0].kind, SnapKind::Point, "{case}"); + assert_eq!(hit.hits[0].source, source, "{case}"); + } + } + } + } +} + #[test] fn snap_targets_cover_layers_comp_guides_and_grid() { let mut s = comp(); diff --git a/crates/engine/src/viewer.rs b/crates/engine/src/viewer.rs index f8e4c7740..5b4064a23 100644 --- a/crates/engine/src/viewer.rs +++ b/crates/engine/src/viewer.rs @@ -128,11 +128,23 @@ fn better(a: (f64, u8), b: (f64, u8)) -> bool { if (a.0 - b.0).abs() < 1e-6 { a.1 < b.1 } else { a.0 < b.0 } } +/// How much nearer (as a share of the tolerance: 2 of the viewer's 8 screen points, whatever the +/// zoom) a vertical and a horizontal line together must be than a point target to win over it +/// when one of the two lines runs through the point. A layer's point lies on that layer's own +/// edge and centre lines, so such a pair is only as near as the point itself plus whatever a +/// foreign line (the comp's centre row, a guide) gains on the other axis; without the margin that +/// pair took the foreign axis and left the feature just off the point, more often the wider the +/// tolerance is in comp pixels, i.e. at 100 % rather than zoomed in (#2681). A pair of lines that +/// both miss the point (a guide crossing beside a layer's point) gets no margin, so such an +/// intersection stays reachable right next to the point. +pub const POINT_BIAS: f64 = 0.25; + /// Snap feature points `sources` (comp pixels) to the nearest target within `tol` comp pixels. /// A point target snaps both axes and wins unless a vertical and a horizontal line both resolve -/// and together lie nearer than it (one line alone leaves the other axis free, so it never beats -/// a point: a layer's edge midpoint lies on its edge line); otherwise the nearest vertical and -/// horizontal lines snap x and y independently. Ties go to the lower priority value. +/// and together lie nearer than it, by more than [`POINT_BIAS`] of `tol` when one of them runs +/// through the point (one line alone leaves the other axis free, so it never beats a point: a +/// layer's edge midpoint lies on its edge line); otherwise the nearest vertical and horizontal +/// lines snap x and y independently. Ties go to the lower priority value. pub fn snap(sources: &[[f64; 2]], targets: &[SnapTarget], tol: f64) -> Option { let mut best_pt: Option<((f64, u8), [f64; 2], SnapTarget)> = None; let mut best_x: Option<((f64, u8), [f64; 2], SnapTarget)> = None; @@ -155,8 +167,15 @@ pub fn snap(sources: &[[f64; 2]], targets: &[SnapTarget], tol: f64) -> Option, at: usize| { + best_pt.zip(line).is_some_and(|((_, _, p), (_, _, l))| (l.pos[at] - p.pos[at]).abs() < 1e-6) + }; + // (At least a hair, so an exact tie goes to the point.) + let bias = if through(best_x, 0) || through(best_y, 1) { (tol * POINT_BIAS).max(1e-9) } else { 1e-9 }; if let Some((k, s, t)) = best_pt - && k.0 <= both_lines + 1e-9 + && k.0 <= both_lines + bias { return Some(Snap { delta: [t.pos[0] - s[0], t.pos[1] - s[1]], hits: vec![t], at: t.pos }); } From 0cb5bfefb5874247898751387f515c8ce74ffd69 Mon Sep 17 00:00:00 2001 From: vangel76 Date: Sat, 7 Nov 2026 15:06:13 +0100 Subject: [PATCH 10/74] Number fields: typed arithmetic (960/2, (3+4)*2) and a leading *, / or + applied to the value (#3195) Number fields read + - * /, brackets and signs through a small bounded parser (crates/ui-egui/src/num_math.rs): no code is run, length and nesting are capped, every step must stay finite and a division by zero keeps the old value with no undo step. Hot-text values (Timeline, Effect Controls, Properties and the other panels) also take a leading *, / or + applied to the value shown, as the reporter describes for After Effects (not confirmed in public docs). A leading - stays a negative number. Hot text used to read +50 as 50; it now adds 50. On an angle the step goes from the signed remainder (+10 on 0x-30 is 0x-20). With several layers selected each layer takes the step from its own value: + through the offset path, * and / through a new prop.set factor. Typed results in whole-number hot text round instead of truncating. Dialog fields (the shared drag_value wrapper, Essential Graphics, Gradient Editor, colour picker) re-read their text per keystroke, so they take absolute arithmetic only. Fixes #3195 --- crates/engine/src/cmd/prop.rs | 29 +- crates/engine/src/tests/timeline.rs | 23 + crates/ui-egui/src/color_picker.rs | 7 +- crates/ui-egui/src/lib.rs | 1 + crates/ui-egui/src/num_math.rs | 394 ++++++++++++++++++ .../src/panels/effect_controls/rows.rs | 9 +- crates/ui-egui/src/panels/essential.rs | 8 +- crates/ui-egui/src/panels/fx_widgets.rs | 32 +- crates/ui-egui/src/panels/gradient_editor.rs | 18 +- crates/ui-egui/src/panels/properties.rs | 9 +- crates/ui-egui/src/panels/timeline.rs | 9 +- crates/ui-egui/src/widgets.rs | 62 ++- crates/ui-egui/tests/it/ui_angles.rs | 15 + crates/ui-egui/tests/it/ui_inline_edit.rs | 78 ++++ 14 files changed, 658 insertions(+), 36 deletions(-) create mode 100644 crates/ui-egui/src/num_math.rs diff --git a/crates/engine/src/cmd/prop.rs b/crates/engine/src/cmd/prop.rs index b4432dd2f..dc043d78f 100644 --- a/crates/engine/src/cmd/prop.rs +++ b/crates/engine/src/cmd/prop.rs @@ -371,20 +371,35 @@ fn set_selected(s: &mut Session, p: &Value) -> Result { }; let follow: Vec = p.get("follow").and_then(Value::as_array).into_iter().flatten().filter_map(Value::as_u64).filter_map(|d| usize::try_from(d).ok()).collect(); + // A typed `*2` or `/2` (#3195): each layer's own value times it, not the edited layer's result. + let factor = p.get("factor").and_then(Value::as_f64).filter(|k| k.is_finite() && delta.is_none()); // Each layer's own value where `set` writes it (a `time` other than the CTI keys there). let typed = |s: &Session, q: &mut Value, olid: LayerId, ouid: Uid| { - if let (Some(dims), Some(n)) = (&dims, &new) - && let Some(mut c) = components_at(s, cid, olid, ouid, snapped_key_time(s, cid, olid, q, "prop.set").ok().flatten()) + let at = snapped_key_time(s, cid, olid, q, "prop.set").ok().flatten(); + if let (true, Some(k)) = (scalar, factor) + && let Some(own) = components_at(s, cid, olid, ouid, at).and_then(|c| c.first().copied()) + && (own * k).is_finite() { - let change = dims.first().and_then(|&d| Some(n.get(d)? - c.get(d)?)).unwrap_or(0.0); + q["value"] = json!(own * k); + } + if let (Some(dims), Some(n)) = (&dims, &new) + && let Some(mut c) = components_at(s, cid, olid, ouid, at) + { + // The typed components' new values on this layer. + let typed_value = |c: &[f64], d: usize| match factor { + Some(k) => c.get(d).map(|v| v * k).filter(|v| v.is_finite()), + None => n.get(d).copied(), + }; + let change = dims.first().and_then(|&d| Some(typed_value(&c, d)? - c.get(d)?)).unwrap_or(0.0); + let values: Vec> = dims.iter().map(|&d| typed_value(&c, d)).collect(); for &d in &follow { if let Some(slot) = c.get_mut(d) { *slot += change; } } - for &d in dims { - if let (Some(slot), Some(v)) = (c.get_mut(d), n.get(d)) { - *slot = *v; + for (&d, v) in dims.iter().zip(values) { + if let (Some(slot), Some(v)) = (c.get_mut(d), v) { + *slot = v; } } q["value"] = json!(c); @@ -1421,7 +1436,7 @@ pub fn specs() -> Vec { "Set Property Value", [], None, - "{layer?, path|prop, value, time? (layer s by default), timeBase?: layer|comp, merge?, selected? (also the other selected layers), offset? (them by the same change), proportional? (with offset: Proportional Scrubbing, each layer by its share in selection order, the first none, the last all), dims? (with selected, a typed value: the components edited, e.g. [2] for Z; the other layers keep the rest of their own value), follow? (with dims: components that move on each other layer by the change its first typed component takes)}", + "{layer?, path|prop, value, time? (layer s by default), timeBase?: layer|comp, merge?, selected? (also the other selected layers), offset? (them by the same change), proportional? (with offset: Proportional Scrubbing, each layer by its share in selection order, the first none, the last all), dims? (with selected, a typed value: the components edited, e.g. [2] for Z; the other layers keep the rest of their own value), follow? (with dims: components that move on each other layer by the change its first typed component takes), factor? (with selected, a typed `*` or `/`: the other layers take their own value times it, in the dims components of a vector)}", has_layers, set_selected ), diff --git a/crates/engine/src/tests/timeline.rs b/crates/engine/src/tests/timeline.rs index 55475eef3..6ea4d1336 100644 --- a/crates/engine/src/tests/timeline.rs +++ b/crates/engine/src/tests/timeline.rs @@ -1531,6 +1531,29 @@ fn a_typed_component_applies_only_that_component_to_the_selected_layers() { assert_eq!(pos(&s, c), vec![1.0, 2.0, 3.0]); } +/// #3195: a typed `*2` or `/2` with several layers selected scales each layer's own value (in +/// the typed components of a vector), not the edited layer's result. +#[test] +fn a_typed_factor_scales_each_selected_layers_own_value() { + let (mut s, a) = setup(); + let b = s.execute("layer.newSolid", json!({"color": "#ff0000", "width": 50, "height": 50})).unwrap()["layer"].as_u64().unwrap(); + s.execute("prop.set", json!({"layer": a, "path": "transform/opacity", "value": 40.0})).unwrap(); + s.execute("prop.set", json!({"layer": b, "path": "transform/opacity", "value": 30.0})).unwrap(); + s.execute("prop.set", json!({"layer": a, "path": "transform/position", "value": [10.0, 20.0]})).unwrap(); + s.execute("prop.set", json!({"layer": b, "path": "transform/position", "value": [100.0, 50.0]})).unwrap(); + s.execute("layer.select", json!({"layers": [a, b]})).unwrap(); + let opacity = prop(&s, a, "transform/opacity").uid; + let undo = s.history.undo.len(); + s.execute_checked("prop.set", json!({"layer": a, "prop": opacity, "value": 80.0, "selected": true, "factor": 2.0})).unwrap(); + assert_eq!(prop(&s, a, "transform/opacity").value.as_f64(), 80.0); + assert_eq!(prop(&s, b, "transform/opacity").value.as_f64(), 60.0, "B doubles its own 30"); + assert_eq!(s.history.undo.len(), undo + 1, "one undo step"); + let position = prop(&s, a, "transform/position").uid; + s.execute_checked("prop.set", json!({"layer": a, "prop": position, "value": [5.0, 20.0], "selected": true, "dims": [0], "factor": 0.5})).unwrap(); + assert_eq!(prop(&s, a, "transform/position").value.components()[..2], [5.0, 20.0]); + assert_eq!(prop(&s, b, "transform/position").value.components()[..2], [50.0, 50.0], "B halves its own X, keeps its Y"); +} + /// #2485: linked Audio Levels typed with several layers selected: each takes the typed Left, and /// its Right moves by its own change, so every layer keeps its own stereo balance. #[test] diff --git a/crates/ui-egui/src/color_picker.rs b/crates/ui-egui/src/color_picker.rs index d434538e6..2b9982cec 100644 --- a/crates/ui-egui/src/color_picker.rs +++ b/crates/ui-egui/src/color_picker.rs @@ -447,7 +447,12 @@ fn color_picker(ui: &mut Ui, id: egui::Id, c: &mut [f32; 3], original: [f32; 3]) if ch == Chan::H && v >= 360.0 { v = 0.0; } - let drag = egui::DragValue::new(&mut v).range(0.0..=scale).fixed_decimals(0).suffix(suffix).speed(scale / 255.0); + let drag = egui::DragValue::new(&mut v) + .range(0.0..=scale) + .fixed_decimals(0) + .suffix(suffix) + .speed(scale / 255.0) + .custom_parser(|text| crate::num_math::calc(text, None)); if ui.add_sized(vec2(56.0, 18.0), drag).labelled_by(radio.id).changed() { pick = pick.set(ch, v.round() / scale); } diff --git a/crates/ui-egui/src/lib.rs b/crates/ui-egui/src/lib.rs index 795a01e1e..0dc02f7d1 100644 --- a/crates/ui-egui/src/lib.rs +++ b/crates/ui-egui/src/lib.rs @@ -36,6 +36,7 @@ pub mod icons; pub mod menu_keys; pub mod menus; pub mod native_menu; +pub mod num_math; pub mod panels; mod pointer_wrap; pub mod prefs_live; diff --git a/crates/ui-egui/src/num_math.rs b/crates/ui-egui/src/num_math.rs new file mode 100644 index 000000000..3e329c9e4 --- /dev/null +++ b/crates/ui-egui/src/num_math.rs @@ -0,0 +1,394 @@ +//! Basic arithmetic typed into a number field (#3195): `960/2`, `(3+4)*2`, `100*2`, and, in a +//! field that knows the value it showed, a leading operator that applies to it: `*2` doubles, +//! `/2` halves, `+50` adds 50, as the issue's reporter describes for After Effects (not confirmed +//! in public docs). A leading `-` is a negative number (`-50`), not a subtraction. (Hot text used +//! to read `+50` as 50; it now adds 50.) +//! +//! This is a small parser for numbers and `+ - * / ( )` only: no names, no functions, nothing is +//! run as code (expressions, `crates/expr`, are a different thing). The text is bounded in +//! length and nesting, every step must stay finite, and a division by zero is no value. Text it +//! can't read is `None`, so the field keeps its value. + +/// The longest text read as arithmetic (characters, after removing spaces). +const MAX_LEN: usize = 256; +/// How deeply brackets and signs (`-(-(…))`, `--5`) may nest. +const MAX_DEPTH: usize = 32; + +/// A typed leading operator: what it does to the value the field showed. +#[derive(Clone, Copy, Debug, PartialEq)] +pub enum Step { + /// `+n` + Add(f64), + /// `*n` + Mul(f64), + /// `/n` (never zero) + Div(f64), +} + +impl Step { + /// `current` changed by the step; `None` when that isn't a finite number. + pub fn apply(self, current: f64) -> Option { + match self { + Step::Add(n) => finite(current + n), + Step::Mul(k) => finite(current * k), + Step::Div(n) => divide(current, n), + } + } + + /// The factor a `*` or `/` step multiplies by (`None` for `+`), for other layers that scale + /// their own value by it. + pub fn factor(self) -> Option { + match self { + Step::Add(_) => None, + Step::Mul(k) => finite(k), + Step::Div(n) => divide(1.0, n), + } + } +} + +/// What typed text says: a value, or (with a leading `+`, `*` or `/`) a step from the value shown. +#[derive(Clone, Copy, Debug, PartialEq)] +pub enum Typed { + Value(f64), + Step(Step), +} + +/// Read typed `text`. With `steps`, a leading `+`, `*` or `/` is a [`Step`]; without (a field +/// that reads its text on every keystroke, where a step would compound) a leading `+` is a plain +/// sign and a leading `*` or `/` is no value. Spaces, including thousands groups, are ignored +/// (as egui's own parser did) and `−` (U+2212) is a minus. `,` is a decimal point: hot text +/// always read it so, and the dialog fields, whose egui parser rejected it, now do too (so +/// `1,5` is 1.5 and `1,000` is 1, not a thousand). `None` for anything else that isn't a finite +/// number. +pub fn read(text: &str, steps: bool) -> Option { + let s: String = text + .chars() + .filter(|c| !c.is_whitespace()) + .map(|c| match c { + '−' => '-', + ',' => '.', + c => c, + }) + .collect(); + if s.is_empty() || s.chars().count() > MAX_LEN { + return None; + } + let first = s.chars().next()?; + if steps && matches!(first, '+' | '*' | '/') { + let operand = parse(s.get(first.len_utf8()..)?)?; + return Some(Typed::Step(match first { + '+' => Step::Add(operand), + '*' => Step::Mul(operand), + _ => Step::Div((operand != 0.0).then_some(operand)?), + })); + } + parse(&s).map(Typed::Value) +} + +/// The value typed `text` stands for: [`read`], with a step applied to `current` (the value the +/// field showed; without it a leading `*` or `/` is no value and `+` only a sign). +pub fn calc(text: &str, current: Option) -> Option { + match read(text, current.is_some())? { + Typed::Value(v) => Some(v), + Typed::Step(step) => step.apply(current.filter(|c| c.is_finite())?), + } +} + +/// The whole of `s` as an expression. +fn parse(s: &str) -> Option { + let mut p = Parser { chars: s.chars().peekable(), depth: 0 }; + let v = p.expr()?; + if p.chars.next().is_some() { + return None; + } + finite(v) +} + +/// `a / b`; `None` for a division by zero. +fn divide(a: f64, b: f64) -> Option { + if b == 0.0 { + return None; + } + finite(a / b) +} + +/// A finite result, or `None`. +fn finite(v: f64) -> Option { + v.is_finite().then_some(v) +} + +/// A recursive-descent reader of `expr := term (('+'|'-') term)*`, +/// `term := factor (('*'|'/') factor)*`, `factor := ('+'|'-') factor | '(' expr ')' | number`. +struct Parser<'a> { + chars: std::iter::Peekable>, + /// The brackets and signs open around the current point. + depth: usize, +} + +impl Parser<'_> { + fn expr(&mut self) -> Option { + let mut v = self.term()?; + while let Some(&op) = self.chars.peek() { + match op { + '+' => { + self.chars.next(); + v = finite(v + self.term()?)?; + } + '-' => { + self.chars.next(); + v = finite(v - self.term()?)?; + } + _ => break, + } + } + Some(v) + } + + fn term(&mut self) -> Option { + let mut v = self.factor()?; + while let Some(&op) = self.chars.peek() { + match op { + '*' => { + self.chars.next(); + v = finite(v * self.factor()?)?; + } + '/' => { + self.chars.next(); + v = divide(v, self.factor()?)?; + } + _ => break, + } + } + Some(v) + } + + fn factor(&mut self) -> Option { + let c = *self.chars.peek()?; + match c { + '+' | '-' | '(' => { + self.depth = self.depth.checked_add(1)?; + if self.depth > MAX_DEPTH { + return None; + } + self.chars.next(); + let v = if c == '(' { + let v = self.expr()?; + if self.chars.next() != Some(')') { + return None; + } + v + } else { + let v = self.factor()?; + if c == '-' { -v } else { v } + }; + self.depth = self.depth.saturating_sub(1); + Some(v) + } + _ => self.number(), + } + } + + /// Digits with at most one decimal point and an optional exponent (`12`, `1.5`, `.5`, `2.`, + /// `2e2`, `1.5E-3`). + fn number(&mut self) -> Option { + let mut text = String::new(); + let mut point = false; + while let Some(&c) = self.chars.peek() { + if c.is_ascii_digit() { + text.push(c); + } else if c == '.' && !point { + point = true; + text.push(c); + } else { + break; + } + self.chars.next(); + } + if !text.chars().any(|c| c.is_ascii_digit()) { + return None; + } + if let Some(&e) = self.chars.peek() + && matches!(e, 'e' | 'E') + { + self.chars.next(); + text.push('e'); + if let Some(&sign) = self.chars.peek() + && matches!(sign, '+' | '-') + { + self.chars.next(); + text.push(sign); + } + let mut digits = false; + while let Some(&c) = self.chars.peek() { + if !c.is_ascii_digit() { + break; + } + digits = true; + text.push(c); + self.chars.next(); + } + if !digits { + return None; + } + } + finite(text.parse::().ok()?) + } +} + +#[cfg(test)] +mod tests { + use super::calc as eval; + + fn abs(text: &str) -> Option { + eval(text, None) + } + + #[test] + fn plain_numbers_read_as_before() { + assert_eq!(abs("100"), Some(100.0)); + assert_eq!(abs("-12.5"), Some(-12.5)); + assert_eq!(abs(" 12,5 "), Some(12.5), "a decimal comma"); + assert_eq!(abs("1 000"), Some(1000.0), "a thousands space"); + assert_eq!(abs("−3"), Some(-3.0), "the minus sign U+2212"); + assert_eq!(abs("1e3"), Some(1000.0)); + assert_eq!(abs(".5"), Some(0.5)); + } + + #[test] + fn arithmetic() { + assert_eq!(abs("960/2"), Some(480.0)); + assert_eq!(abs("100*2"), Some(200.0)); + assert_eq!(abs("(3+4)*2"), Some(14.0)); + assert_eq!(abs("3+4*2"), Some(11.0), "* before +"); + assert_eq!(abs("10-2-3"), Some(5.0), "left to right"); + assert_eq!(abs("8/4/2"), Some(1.0), "left to right"); + assert_eq!(abs("-(2+3)"), Some(-5.0)); + assert_eq!(abs("2*-3"), Some(-6.0)); + assert_eq!(abs("1.5 * 2"), Some(3.0)); + assert_eq!(abs("((((1))))"), Some(1.0)); + assert_eq!(abs("100+50"), Some(150.0)); + assert_eq!(abs("100-50"), Some(50.0)); + assert_eq!(abs("2e2*2"), Some(400.0), "an exponent inside arithmetic"); + assert_eq!(abs("1E-3*1000"), Some(1.0)); + assert_eq!(abs("1e+2/4"), Some(25.0)); + } + + #[test] + fn steps_are_read_as_steps() { + use super::{Step, Typed, read}; + assert_eq!(read("*2", true), Some(Typed::Step(Step::Mul(2.0)))); + assert_eq!(read("/4", true), Some(Typed::Step(Step::Div(4.0)))); + assert_eq!(read("+(1+2)", true), Some(Typed::Step(Step::Add(3.0)))); + assert_eq!(read("-5", true), Some(Typed::Value(-5.0))); + assert_eq!(read("+5", false), Some(Typed::Value(5.0))); + assert_eq!(read("/0", true), None); + assert_eq!(Step::Div(4.0).factor(), Some(0.25)); + assert_eq!(Step::Mul(3.0).factor(), Some(3.0)); + assert_eq!(Step::Add(3.0).factor(), None); + assert_eq!(Step::Div(1e-320).factor(), None, "a factor that overflows"); + assert_eq!(Step::Div(3.0).apply(3.0), Some(1.0), "a division, not a reciprocal factor"); + } + + #[test] + fn a_leading_operator_applies_to_the_current_value() { + assert_eq!(eval("*2", Some(100.0)), Some(200.0)); + assert_eq!(eval("/2", Some(100.0)), Some(50.0)); + assert_eq!(eval("+50", Some(100.0)), Some(150.0)); + assert_eq!(eval("*(1+1)", Some(3.0)), Some(6.0)); + assert_eq!(eval("+-50", Some(100.0)), Some(50.0)); + // A leading minus is a negative number, not a subtraction. + assert_eq!(eval("-50", Some(100.0)), Some(-50.0)); + assert_eq!(eval("-50*2", Some(100.0)), Some(-100.0)); + // Without the current value there is nothing to apply it to; a `+` is only a sign. + assert_eq!(eval("*2", None), None); + assert_eq!(eval("/2", None), None); + assert_eq!(eval("+50", None), Some(50.0)); + assert_eq!(eval("*2", Some(f64::NAN)), None); + assert_eq!(eval("+1", Some(f64::INFINITY)), None); + assert_eq!(eval("/0", Some(100.0)), None); + assert_eq!(eval("/(1-1)", Some(100.0)), None); + assert_eq!(eval("*", Some(100.0)), None); + assert_eq!(eval("*1e400", Some(1.0)), None); + // Overflow past the largest finite value. + assert_eq!(eval(&format!("*{}", "9".repeat(20)), Some(1e300)), None); + assert_eq!(eval("/0.0000000001", Some(1e300)), None); + } + + #[test] + fn invalid_or_hostile_text_is_no_value() { + for text in [ + "", " ", "*", "/", "1+", "+", "-", "2**3", "2//3", "1/0", "0/0", "-1/0", "1/(2-2)", "(1", "1)", "()", "(1+2", "abc", "NaN", "nan", "inf", "-inf", + "infinity", "1e999", "-1e999", "1..2", "1.2.3", ".", "1e", "2x", "x*2", "1+a", "5;rm", "1,2,3", "0x10", "1_000", "%", "50%", + ] { + assert_eq!(abs(text), None, "{text:?}"); + assert_eq!(eval(text, Some(10.0)), None, "{text:?} with a current value"); + } + // Far too long: rejected by length before any reading. + assert_eq!(abs(&format!("{}1{}", "(".repeat(1000), ")".repeat(1000))), None); + assert_eq!(abs(&format!("{}5", "-".repeat(10_000))), None); + assert_eq!(abs(&"(".repeat(10_000)), None); + assert_eq!(abs(&"1".repeat(10_000)), None); + assert_eq!(abs(&"1+".repeat(5_000)), None); + // Short enough, but nested past the depth bound. + assert_eq!(abs(&format!("{}1{}", "(".repeat(100), ")".repeat(100))), None); + assert_eq!(abs(&format!("{}5", "-".repeat(200))), None); + assert_eq!(abs(&format!("2*{}5", "-+".repeat(100))), None); + // Nesting within the bound reads. + assert_eq!(abs(&format!("{}1{}", "(".repeat(20), ")".repeat(20))), Some(1.0)); + assert_eq!(abs("--5"), Some(5.0)); + // Overflow on the way is no value. + assert_eq!(abs("1e308*10"), None); + assert_eq!(abs("1e308+1e308"), None); + assert_eq!(abs("1e200*1e200/1e200"), None, "an infinite step is rejected even if the result would be finite"); + // The largest numbers the length allows still multiply to a finite value. + let big = "9".repeat(120); + assert!(abs(&format!("{big}*{big}")).is_some_and(f64::is_finite)); + } + + /// The result is always finite (no NaN reaches a property). + #[test] + fn results_are_finite() { + for text in ["0*1e308", "1e-320/1e10", "0-0", "-0"] { + if let Some(v) = abs(text) { + assert!(v.is_finite(), "{text:?} → {v}"); + } + } + } + + /// A standard numeric editor (dialogs) reads typed arithmetic too. + /// (egui's own parser read `960/2` as no value, so the field stayed 10.) + #[test] + fn a_drag_value_reads_typed_arithmetic() { + use egui::{Context, Event, Key, Modifiers, PointerButton, Pos2, RawInput, Rect, pos2, vec2}; + let ctx = Context::default(); + let mut value = 10.0_f64; + let mut time = 0.0; + let field = Rect::from_min_size(pos2(100.0, 100.0), vec2(80.0, 20.0)); + let mut frame = |events: Vec, value: &mut f64| { + time += 0.02; + let raw = RawInput { + screen_rect: Some(Rect::from_min_size(Pos2::ZERO, vec2(400.0, 300.0))), + time: Some(time), + events, + focused: true, + ..Default::default() + }; + let mut out = ctx.run_ui(raw, |ui| { + ui.put(field, crate::widgets::drag_value(egui::DragValue::new(&mut *value).speed(1.0))); + }); + out.textures_delta.clear(); + }; + let p = field.center(); + frame(vec![], &mut value); + frame( + vec![Event::PointerMoved(p), Event::PointerButton { pos: p, button: PointerButton::Primary, pressed: true, modifiers: Modifiers::NONE }], + &mut value, + ); + frame(vec![Event::PointerButton { pos: p, button: PointerButton::Primary, pressed: false, modifiers: Modifiers::NONE }], &mut value); + frame(vec![], &mut value); + frame(vec![Event::Text("960/2".into())], &mut value); + frame(vec![Event::Key { key: Key::Enter, physical_key: None, pressed: true, repeat: false, modifiers: Modifiers::NONE }], &mut value); + frame(vec![], &mut value); + assert_eq!(value, 480.0); + } +} diff --git a/crates/ui-egui/src/panels/effect_controls/rows.rs b/crates/ui-egui/src/panels/effect_controls/rows.rs index c14630af4..e2634d6b8 100644 --- a/crates/ui-egui/src/panels/effect_controls/rows.rs +++ b/crates/ui-egui/src/panels/effect_controls/rows.rs @@ -265,11 +265,12 @@ fn value_angle(app: &mut EffectcraftApp, ui: &mut egui::Ui, cx: &PropCx, value: return; }; let PropCx { uid, cy, vx, prop, .. } = *cx; - let (rr, dr, nv) = fw::angle_field(ui, pos2(vx, cy - 9.0), egui::Id::unique(("ec-v", uid)), *v, 1, &cx.value_tokens()); + let (rr, dr, nv, step) = fw::angle_field_step(ui, pos2(vx, cy - 9.0), egui::Id::unique(("ec-v", uid)), *v, 1, &cx.value_tokens()); app.auto.add(&format!("effectControls.prop.{uid}.value"), rr.union(dr), &fw::format_angle(*v, 1)); app.auto.add(&format!("effectControls.prop.{uid}.revolutions"), rr, &prop.name); if let Some(nv) = nv { cx.set(actions, json!(nv)); + widgets::mark_step(actions.last_mut(), step); } cx.twirl_open(app, ui); } @@ -288,7 +289,7 @@ fn value_scalar(app: &mut EffectcraftApp, ui: &mut egui::Ui, cx: &PropCx, value: _ => "", }; let speed = widgets::slider_scrub_speed(&prop.ui); - let (vr, nv, _) = widgets::hot_number_at( + let (vr, nv, _, step) = widgets::hot_number_step_at( ui, pos2(vx, cy - 9.0), egui::Id::unique(("ec-v", uid)), @@ -299,6 +300,7 @@ fn value_scalar(app: &mut EffectcraftApp, ui: &mut egui::Ui, cx: &PropCx, value: app.auto.add(&format!("effectControls.prop.{uid}.value"), vr, &prop.name); if let Some(nv) = nv { cx.set(actions, json!(nv)); + widgets::mark_step(actions.last_mut(), step); } // AE hides a slider param's slider until its twirl is opened (see `slider_row`). if matches!(prop.ui, ParamUi::Slider { .. }) { @@ -326,7 +328,7 @@ fn value_vector(app: &mut EffectcraftApp, ui: &mut egui::Ui, cx: &PropCx, value: x += 22.0; } for d in 0..n.min(c.len()) { - let (vr, nv, _) = widgets::hot_number_at( + let (vr, nv, _, step) = widgets::hot_number_step_at( ui, pos2(x, cy - 9.0), egui::Id::unique(("ec-v", uid, d)), @@ -345,6 +347,7 @@ fn value_vector(app: &mut EffectcraftApp, ui: &mut egui::Ui, cx: &PropCx, value: if let Some((_, a)) = actions.last_mut() { a["dims"] = json!([d]); } + widgets::mark_step(actions.last_mut(), step); } x = vr.max.x + 8.0; } diff --git a/crates/ui-egui/src/panels/essential.rs b/crates/ui-egui/src/panels/essential.rs index 02e077508..5df6f6b8c 100644 --- a/crates/ui-egui/src/panels/essential.rs +++ b/crates/ui-egui/src/panels/essential.rs @@ -399,7 +399,13 @@ fn value_widget(app: &mut EffectcraftApp, ui: &mut egui::Ui, auto: &str, ty: Opt let mut rect = lr; for (d, &old) in comps.iter().enumerate() { let mut x = old; - let r = ui.add(egui::DragValue::new(&mut x).speed(0.5).suffix(if d + 1 == comps.len() { "%" } else { "" }).max_decimals(1)); + let r = ui.add( + egui::DragValue::new(&mut x) + .speed(0.5) + .suffix(if d + 1 == comps.len() { "%" } else { "" }) + .max_decimals(1) + .custom_parser(|text| crate::num_math::calc(text, None)), + ); app.auto.add(&format!("{auto}.{d}"), r.rect, &p.name); rect = rect.union(r.rect); if r.changed() { diff --git a/crates/ui-egui/src/panels/fx_widgets.rs b/crates/ui-egui/src/panels/fx_widgets.rs index b3140d394..c39009fd3 100644 --- a/crates/ui-egui/src/panels/fx_widgets.rs +++ b/crates/ui-egui/src/panels/fx_widgets.rs @@ -8,6 +8,7 @@ use effectcraft_engine::geom::{Mat3, vec2 as gv2}; use egui::{Align2, Color32, Pos2, Rect, Sense, Stroke, StrokeKind, pos2, vec2}; +use crate::num_math::Step; use crate::theme::Tokens; use crate::widgets; @@ -56,18 +57,35 @@ fn degrees_edit(v: f64, shown: f64, new: f64, dragging: bool) -> f64 { /// are hot numbers of their own (drag to scrub, click to type) and changing one keeps the other. /// Returns the revolutions' rect, the degrees' rect and the new angle when either changed. pub fn angle_field(ui: &mut egui::Ui, at: Pos2, id: egui::Id, v: f64, decimals: usize, t: &Tokens) -> (Rect, Rect, Option) { + let (rr, dr, out, _) = angle_field_step(ui, at, id, v, decimals, t); + (rr, dr, out) +} + +/// [`angle_field`], also returning a typed `+n` into the degrees (#3195): other selected layers +/// can turn their own angle by it. A typed step applies to the signed remainder, so `+10` on +/// `0x-30°` is `0x-20°`. +pub fn angle_field_step(ui: &mut egui::Ui, at: Pos2, id: egui::Id, v: f64, decimals: usize, t: &Tokens) -> (Rect, Rect, Option, Option) { let (rev, deg) = split_angle(v); let (rr, new_rev, _) = widgets::hot_int_at(ui, at, id.with("rev"), rev, 0.1, "x", t); let sign = ui.painter().text(pos2(rr.max.x - 2.0, rr.center().y), Align2::LEFT_CENTER, if deg < 0.0 { "-" } else { "+" }, Tokens::ui(12.0), t.hot_text); - let (dr, new_deg, _) = - widgets::hot_number_at(ui, pos2(sign.max.x - 2.0, at.y), id, deg.abs(), &widgets::HotSpec { speed: 0.5, range: (-1e9, 1e9), decimals, suffix: "°" }, t); + let (dr, new_deg, _, step) = widgets::hot_number_step_at( + ui, + pos2(sign.max.x - 2.0, at.y), + id, + deg.abs(), + &widgets::HotSpec { speed: 0.5, range: (-1e9, 1e9), decimals, suffix: "°" }, + t, + ); let dragging = new_deg.is_some() && ui.ctx().is_being_dragged(id); - let out = match (new_rev, new_deg) { - (Some(r), _) => Some(r as f64 * 360.0 + deg), - (None, Some(d)) => Some(degrees_edit(v, deg.abs(), d, dragging)), - (None, None) => None, + let out = match (new_rev, new_deg, step) { + (Some(r), _, _) => Some(r as f64 * 360.0 + deg), + // (The field shows the remainder's size; the step goes from the signed remainder.) + (None, Some(_), Some(s)) => s.apply(deg).map(|d| rev as f64 * 360.0 + d), + (None, Some(d), None) => Some(degrees_edit(v, deg.abs(), d, dragging)), + (None, None, _) => None, }; - (rr, dr, out) + let step = step.filter(|s| matches!(s, Step::Add(_)) && new_rev.is_none() && out.is_some()); + (rr, dr, out, step) } /// Dial angle (degrees, 0 = up, clockwise) of `p` around `c` in screen space. diff --git a/crates/ui-egui/src/panels/gradient_editor.rs b/crates/ui-egui/src/panels/gradient_editor.rs index 0f0bbe412..6f88519b7 100644 --- a/crates/ui-egui/src/panels/gradient_editor.rs +++ b/crates/ui-egui/src/panels/gradient_editor.rs @@ -440,7 +440,14 @@ pub fn show(app: &mut EffectcraftApp, ctx: &egui::Context, t: &Tokens) { } .unwrap_or(0.0); let mut pct = pos * 100.0; - let r = ui.add(egui::DragValue::new(&mut pct).range(0.0..=100.0).speed(0.5).max_decimals(1).suffix("%")); + let r = ui.add( + egui::DragValue::new(&mut pct) + .range(0.0..=100.0) + .speed(0.5) + .max_decimals(1) + .suffix("%") + .custom_parser(|text| crate::num_math::calc(text, None)), + ); app.auto.add("dialog.gradient.location", r.rect, "Location"); if r.changed() { app.dialog_state.gradient.move_to(track, id, pct / 100.0); @@ -452,7 +459,14 @@ pub fn show(app: &mut EffectcraftApp, ctx: &egui::Context, t: &Tokens) { Track::Opacity => { ui.label(tr("Opacity:")); let mut a = app.dialog_state.gradient.opacities.get(i).map_or(1.0, |s| s.value) as f64 * 100.0; - let r = ui.add(egui::DragValue::new(&mut a).range(0.0..=100.0).speed(0.5).max_decimals(1).suffix("%")); + let r = ui.add( + egui::DragValue::new(&mut a) + .range(0.0..=100.0) + .speed(0.5) + .max_decimals(1) + .suffix("%") + .custom_parser(|text| crate::num_math::calc(text, None)), + ); app.auto.add("dialog.gradient.opacity", r.rect, "Opacity"); if r.changed() { app.dialog_state.gradient.set_opacity(id, (a / 100.0) as f32); diff --git a/crates/ui-egui/src/panels/properties.rs b/crates/ui-egui/src/panels/properties.rs index 25829be7d..e6d3be35b 100644 --- a/crates/ui-egui/src/panels/properties.rs +++ b/crates/ui-egui/src/panels/properties.rs @@ -291,16 +291,17 @@ fn transform_row(app: &mut EffectcraftApp, ui: &mut egui::Ui, p: &egui::Painter, match &value { Value::Scalar(v) if pr.name == "Rotation" || pr.name.ends_with(" Rotation") => { let (_, deg) = super::fx_widgets::split_angle(*v); - let (rr, dr, nv) = super::fx_widgets::angle_field(ui, pos2(vx, cy - 9.0), egui::Id::unique(("props-v", uid)), *v, decimals(deg), &vt); + let (rr, dr, nv, step) = super::fx_widgets::angle_field_step(ui, pos2(vx, cy - 9.0), egui::Id::unique(("props-v", uid)), *v, decimals(deg), &vt); app.auto.add(&format!("{base}.value"), dr, &pr.name); app.auto.add(&format!("{base}.revolutions"), rr, &pr.name); if let Some(nv) = nv { set(actions, json!(nv)); + widgets::mark_step(actions.last_mut(), step); } } Value::Scalar(v) => { let range = if pr.name == "Opacity" { (0.0, 100.0) } else { (-1e9, 1e9) }; - let (vr, nv, _) = widgets::hot_number_at( + let (vr, nv, _, step) = widgets::hot_number_step_at( ui, pos2(vx, cy - 9.0), egui::Id::unique(("props-v", uid)), @@ -311,6 +312,7 @@ fn transform_row(app: &mut EffectcraftApp, ui: &mut egui::Ui, p: &egui::Painter, app.auto.add(&format!("{base}.value"), vr, &pr.name); if let Some(nv) = nv { set(actions, json!(nv)); + widgets::mark_step(actions.last_mut(), step); } } Value::Vec2(_) | Value::Vec3(_) => { @@ -332,7 +334,7 @@ fn transform_row(app: &mut EffectcraftApp, ui: &mut egui::Ui, p: &egui::Painter, } } for d in 0..n.min(c.len()) { - let (vr, nv, _) = widgets::hot_number_at( + let (vr, nv, _, step) = widgets::hot_number_step_at( ui, pos2(x, cy - 9.0), egui::Id::unique(("props-v", uid, d)), @@ -357,6 +359,7 @@ fn transform_row(app: &mut EffectcraftApp, ui: &mut egui::Ui, p: &egui::Painter, if let Some((_, a)) = actions.last_mut() { a["dims"] = json!((from..from + shown).collect::>()); } + widgets::mark_step(actions.last_mut(), step); } x = vr.max.x + 10.0; } diff --git a/crates/ui-egui/src/panels/timeline.rs b/crates/ui-egui/src/panels/timeline.rs index 038dcc9b9..c6da2ccbd 100644 --- a/crates/ui-egui/src/panels/timeline.rs +++ b/crates/ui-egui/src/panels/timeline.rs @@ -5831,17 +5831,18 @@ fn scalar_editor(app: &mut EffectcraftApp, ui: &mut egui::Ui, ve: &ValueEdit<'_> _ => (-1e9, 1e9, 1, 1.0), }; if matches!(prop.ui, ParamUi::Angle) { - let (rr, dr, nv) = super::fx_widgets::angle_field(ui, pos2(x, y), egui::Id::unique(("v", uid, 0)), *v, 1, &vt); + let (rr, dr, nv, step) = super::fx_widgets::angle_field_step(ui, pos2(x, y), egui::Id::unique(("v", uid, 0)), *v, 1, &vt); app.auto.add(&format!("timeline.prop.{uid}.value"), dr, &prop.name); app.auto.add(&format!("timeline.prop.{uid}.revolutions"), rr, &prop.name); if let Some(nv) = nv { ve.set(actions, json!(nv)); + widgets::mark_step(actions.last_mut(), step); } } else { // A Range Selector's Start, End and Offset count characters with Units = Index (#1814). let pct = (matches!(prop.ui, ParamUi::Percent) || prop.match_id == "opacity") && !effectcraft_engine::render::text::in_index_units(layer, uid); let suffix = if pct { "%" } else { "" }; - let (r, nv, _) = widgets::hot_number_at( + let (r, nv, _, step) = widgets::hot_number_step_at( ui, pos2(x, y), egui::Id::unique(("v", uid, 0)), @@ -5852,6 +5853,7 @@ fn scalar_editor(app: &mut EffectcraftApp, ui: &mut egui::Ui, ve: &ValueEdit<'_> app.auto.add(&format!("timeline.prop.{uid}.value"), r, &prop.name); if let Some(nv) = nv { ve.set(actions, json!(nv)); + widgets::mark_step(actions.last_mut(), step); } } } @@ -5890,7 +5892,7 @@ fn vector_editor(app: &mut EffectcraftApp, ui: &mut egui::Ui, ve: &ValueEdit<'_> } for d in 0..n.min(c.len()) { let suffix = if pct && d + 1 == n { "%" } else { "" }; - let (r, nv, _) = widgets::hot_number_at( + let (r, nv, _, step) = widgets::hot_number_step_at( ui, pos2(x, y), egui::Id::unique(("v", uid, d)), @@ -5924,6 +5926,7 @@ fn vector_editor(app: &mut EffectcraftApp, ui: &mut egui::Ui, ve: &ValueEdit<'_> a["dims"] = json!((from..from + shown).collect::>()); } } + widgets::mark_step(actions.last_mut(), step); } x = r.max.x + if d + 1 < n { 2.0 } else { 0.0 }; if d + 1 < n { diff --git a/crates/ui-egui/src/widgets.rs b/crates/ui-egui/src/widgets.rs index 162a86c6a..c5b7223da 100644 --- a/crates/ui-egui/src/widgets.rs +++ b/crates/ui-egui/src/widgets.rs @@ -63,8 +63,23 @@ pub struct HotSpec<'a> { /// "Hot text": a blue number you drag horizontally to scrub, click to type. Tab / Shift+Tab /// while typing commits and types in the panel's next / previous one, as in After Effects. +/// Typed text may be arithmetic, `960/2` or `*2` for twice the value ([`crate::num_math`]). /// Returns (response, Some(new value) when changed, drag finished). pub fn hot_number_at(ui: &mut Ui, rect_min: egui::Pos2, id: egui::Id, value: f64, spec: &HotSpec, t: &Tokens) -> (Rect, Option, bool) { + let (rect, out, done, _) = hot_number_step_at(ui, rect_min, id, value, spec, t); + (rect, out, done) +} + +/// [`hot_number_at`], also returning the typed step (`*2`, `+50`) the new value came from, so +/// that other selected layers can take the same step from their own value (#3195). +pub fn hot_number_step_at( + ui: &mut Ui, + rect_min: egui::Pos2, + id: egui::Id, + value: f64, + spec: &HotSpec, + t: &Tokens, +) -> (Rect, Option, bool, Option) { let HotSpec { speed, range, decimals, suffix } = *spec; let editing_id = id.with("editing"); let mut editing: Option = ui.data(|d| d.get_temp(editing_id)); @@ -87,14 +102,18 @@ pub fn hot_number_at(ui: &mut Ui, rect_min: egui::Pos2, id: egui::Id, value: f64 .lock_focus(true), ); let tab = r.has_focus().then(|| ui.input_mut(|i| i.consume_key(egui::Modifiers::NONE, egui::Key::Tab).then_some(i.modifiers.shift))).flatten(); - let mut out = None; + let (mut out, mut step) = (None, None); if r.lost_focus() || tab.is_some() { - let txt = buf.trim().trim_end_matches(suffix).trim().replace(',', "."); + // Typed arithmetic reads too (`960/2`), a leading `*`, `/` or `+` applied to the value + // shown (#3195). if !ui.input(|i| i.key_pressed(egui::Key::Escape)) - && let Ok(v) = txt.parse::() - && v.is_finite() + && let Some(typed) = crate::num_math::read(buf.trim().trim_end_matches(suffix), true) { - out = Some(v.clamp(range.0, range.1)); + let v = match typed { + crate::num_math::Typed::Value(v) => Some(v), + crate::num_math::Typed::Step(s) => s.apply(value).inspect(|_| step = Some(s)), + }; + out = v.map(|v| v.clamp(range.0, range.1)); } ui.data_mut(|d| d.remove::(editing_id)); if let Some(back) = tab { @@ -106,7 +125,7 @@ pub fn hot_number_at(ui: &mut Ui, rect_min: egui::Pos2, id: egui::Id, value: f64 keep_focus(ui.ctx(), &r, &buf); ui.data_mut(|d| d.insert_temp(editing_id, buf)); } - return (er, out, out.is_some()); + return (er, out, out.is_some(), step); } let resp = ui.interact(rect, id, Sense::click_and_drag()); crate::pointer_wrap::track(&resp); @@ -140,7 +159,20 @@ pub fn hot_number_at(ui: &mut Ui, rect_min: egui::Pos2, id: egui::Id, value: f64 if resp.clicked() { ui.data_mut(|d| d.insert_temp(editing_id, format!("{value:.decimals$}"))); } - (rect, out, resp.drag_stopped()) + (rect, out, resp.drag_stopped(), None) +} + +/// Mark a queued `prop.set` (with `selected`) as the typed `step` it came from, so every other +/// selected layer takes that step from its own value rather than the edited layer's result +/// (#3195): `+n` by the same change (`offset`), `*n` and `/n` by the same `factor`. +pub fn mark_step(action: Option<&mut (String, serde_json::Value)>, step: Option) { + let (Some((_, a)), Some(step)) = (action, step) else { return }; + match step.factor() { + Some(k) => a["factor"] = serde_json::json!(k), + None if matches!(step, crate::num_math::Step::Add(_)) => a["offset"] = serde_json::json!(true), + // (A factor too large to send: the other layers take the typed result.) + None => {} + } } /// Ctrl+Alt (Cmd+Opt on macOS) held while scrubbing hot text: After Effects' Proportional @@ -162,10 +194,19 @@ pub fn slider_scrub_speed(ui: &ParamUi) -> f64 { /// [`hot_number_at`] for a whole number (an angle's revolutions): a drag steps it once every /// `1 / speed` pixels however slowly the pointer moves (the fractions add up between frames), -/// and a typed value is truncated. Returns (rect, Some(new value) when changed, drag finished). +/// and a typed value is rounded (`5/2` is 3). Returns (rect, Some(new value) when changed, drag +/// finished). pub fn hot_int_at(ui: &mut Ui, rect_min: egui::Pos2, id: egui::Id, value: i64, speed: f64, suffix: &str, t: &Tokens) -> (Rect, Option, bool) { let (rect, nv, done) = hot_number_at(ui, rect_min, id, value as f64, &HotSpec { speed, range: (-1e6, 1e6), decimals: 0, suffix }, t); let acc_id = id.with("acc"); + if let Some(n) = nv + && !ui.ctx().is_being_dragged(id) + { + // Typed (a drag changes the value only while it is dragged). (Within ±1e6: the cast is exact.) + ui.data_mut(|d| d.remove::(acc_id)); + let typed = n.round() as i64; + return (rect, (typed != value).then_some(typed), done); + } let acc = ui.data(|d| d.get_temp::(acc_id)).unwrap_or(0.0) + nv.map_or(0.0, |n| n - value as f64); // (Ten 0.1 steps add up to just under 1.) let step = (acc + acc.signum() * 1e-9).trunc(); @@ -177,8 +218,11 @@ pub fn hot_int_at(ui: &mut Ui, rect_min: egui::Pos2, id: egui::Id, value: i64, s (rect, (step != 0.0).then(|| value.saturating_add(step as i64)), done) } -/// Standard numeric editors use the same continuous native scrubbing as hot text. +/// Standard numeric editors use the same continuous native scrubbing as hot text, and read typed +/// arithmetic (`960/2`) too. They read their text on every keystroke, so a leading `*` or `/` +/// (relative to the value) is left to hot text, where it would otherwise compound (#3195). pub fn drag_value(value: egui::DragValue<'_>) -> impl egui::Widget { + let value = value.custom_parser(|text| crate::num_math::calc(text, None)); move |ui: &mut Ui| { let response = ui.add(value); crate::pointer_wrap::track(&response); diff --git a/crates/ui-egui/tests/it/ui_angles.rs b/crates/ui-egui/tests/it/ui_angles.rs index 007e11286..37eb01c8d 100644 --- a/crates/ui-egui/tests/it/ui_angles.rs +++ b/crates/ui-egui/tests/it/ui_angles.rs @@ -172,6 +172,21 @@ fn typing_replaces_the_clicked_value() { } } +/// #3195: a typed `+10` turns the angle by 10°, from its signed remainder: `0x-30°` becomes +/// `0x-20°` (the degrees field shows the remainder's size, 30, which would have given -40°). +#[test] +fn a_typed_step_applies_to_the_signed_angle() { + for panel in [Panel::EffectControls, Panel::Timeline, Panel::Properties] { + for (text, expected) in [("+10", -20.0), ("*2", -60.0), ("/3", -10.0)] { + let (mut h, layer, uid) = harness(panel, -30.0); + let p = component(&h, panel, uid, false); + begin_edit(&mut h, p, text); + key(&mut h, Key::Enter, Modifiers::NONE); + assert_eq!(angle(&h, layer, uid), expected, "{panel:?}: {text}"); + } + } +} + fn commit_edits(click_away: bool) { for panel in [Panel::EffectControls, Panel::Timeline, Panel::Properties] { for (revolutions, text, expected) in [(true, "3", 1110.5), (false, "45.5", 405.5)] { diff --git a/crates/ui-egui/tests/it/ui_inline_edit.rs b/crates/ui-egui/tests/it/ui_inline_edit.rs index 6c29045bb..c845863e4 100644 --- a/crates/ui-egui/tests/it/ui_inline_edit.rs +++ b/crates/ui-egui/tests/it/ui_inline_edit.rs @@ -404,6 +404,84 @@ fn tab_commits_a_value_and_moves_to_the_next() { assert_eq!(value(&h, "effects/#2/slider"), [42.0]); } +/// #3195: a value field reads typed arithmetic, as in After Effects: `960/2` sets 480, a leading +/// `*` applies to the value shown, and text that is no number (a division by zero) keeps the +/// value with no undo step. In the Timeline and in Effect Controls. +#[test] +fn typed_arithmetic_sets_a_value() { + let (mut h, _) = harness(); + let (layer, transform) = { + let s = &mut h.state_mut().session; + let l = s.execute("layer.newSolid", json!({"name": "Plate", "color": "#406080"})).unwrap()["layer"].as_u64().unwrap(); + s.execute("layer.select", json!({"layers": [l]})).unwrap(); + s.execute("effect.apply", json!({"layers": [l], "effect": "Point Control"})).unwrap(); + s.execute("prop.set", json!({"layer": l, "path": "transform/position", "value": [160.0, 90.0]})).unwrap(); + let tr = s.active_comp().unwrap().layer(effectcraft_engine::project::LayerId(l)).unwrap().props.sub("transform").unwrap().uid; + (l, tr) + }; + h.state_mut().ui.timeline.open_layers.insert(layer); + h.state_mut().ui.timeline.open_groups.insert(transform); + h.state_mut().show_panel(effectcraft_ui_egui::dock::PanelKind::EffectControls); + h.run_steps(3); + let props = + |h: &Harness<'_, EffectcraftApp>| h.state().session.active_comp().unwrap().layer(effectcraft_engine::project::LayerId(layer)).unwrap().props.clone(); + let value = |h: &Harness<'_, EffectcraftApp>, path: &str| props(h).prop(path).unwrap().value.components(); + let position = props(&h).prop("transform/position").unwrap().uid; + assert_eq!(value(&h, "transform/position")[..2], [160.0, 90.0]); + let type_into = |h: &mut Harness<'_, EffectcraftApp>, id: &str, text: &str| { + // (Settle first: a click right after the last one's would count as part of a double click.) + h.run_steps(5); + let p = center(h, id); + click(h, p, 1); + type_text(h, text); + key(h, Key::Enter); + }; + type_into(&mut h, &format!("timeline.prop.{position}.value.0"), "960/2"); + assert_eq!(value(&h, "transform/position")[..2], [480.0, 90.0], "960/2 is 480"); + type_into(&mut h, &format!("timeline.prop.{position}.value.1"), "*2"); + assert_eq!(value(&h, "transform/position")[..2], [480.0, 180.0], "*2 doubles the value shown"); + let undo = h.state().session.history.undo.len(); + type_into(&mut h, &format!("timeline.prop.{position}.value.0"), "1/0"); + assert_eq!(value(&h, "transform/position")[..2], [480.0, 180.0], "a division by zero keeps the value"); + assert_eq!(h.state().session.history.undo.len(), undo, "and adds no undo step"); + let point = props(&h).prop("effects/#1/point").unwrap().uid; + type_into(&mut h, &format!("effectControls.prop.{point}.value.1"), "(3+4)*2"); + assert_eq!(value(&h, "effects/#1/point")[1], 14.0); +} + +/// #3195: with several layers selected, a typed `+10` or `*2` takes each layer's own value one +/// step on; it doesn't give them all the edited layer's result. +#[test] +fn a_typed_step_applies_to_each_selected_layer() { + let (mut h, _) = harness(); + let (a, b, transform) = { + let s = &mut h.state_mut().session; + let mut ids = vec![]; + for (name, opacity) in [("A", 40.0), ("B", 30.0)] { + let l = s.execute("layer.newSolid", json!({"name": name, "color": "#406080"})).unwrap()["layer"].as_u64().unwrap(); + s.execute("prop.set", json!({"layer": l, "path": "transform/opacity", "value": opacity})).unwrap(); + ids.push(l); + } + s.execute("layer.select", json!({"layers": ids})).unwrap(); + let tr = s.active_comp().unwrap().layer(effectcraft_engine::project::LayerId(ids[0])).unwrap().props.sub("transform").unwrap().uid; + (ids[0], ids[1], tr) + }; + h.state_mut().ui.timeline.open_layers.insert(a); + h.state_mut().ui.timeline.open_groups.insert(transform); + h.run_steps(3); + let opacity_of = |h: &Harness<'_, EffectcraftApp>, l: u64| { + h.state().session.active_comp().unwrap().layer(effectcraft_engine::project::LayerId(l)).unwrap().props.prop("transform/opacity").unwrap().value.as_f64() + }; + let opacity = h.state().session.active_comp().unwrap().layer(effectcraft_engine::project::LayerId(a)).unwrap().props.prop("transform/opacity").unwrap().uid; + for (text, expected) in [("+10", (50.0, 40.0)), ("*2", (100.0, 80.0)), ("/4", (25.0, 20.0)), ("60", (60.0, 60.0))] { + let p = center(&h, &format!("timeline.prop.{opacity}.value")); + click(&mut h, p, 1); + type_text(&mut h, text); + key(&mut h, Key::Enter); + assert_eq!((opacity_of(&h, a), opacity_of(&h, b)), expected, "{text}"); + } +} + /// A click at `p` with `modifiers` held. fn click_with(h: &mut Harness<'_, EffectcraftApp>, p: Pos2, modifiers: Modifiers) { h.input_mut().events.push(Event::PointerMoved(p)); From dd92908bc82eecc32896dd6b9db80068ae39ab16 Mon Sep 17 00:00:00 2001 From: vangel76 Date: Sat, 7 Nov 2026 15:06:49 +0100 Subject: [PATCH 11/74] Project panel: items dropped on New Folder go into a new folder, as After Effects project.newFolder takes optional `items` (ids or names) and moves them into the new folder in the same undo step, out of any folder they were in; a folder can't go into a new folder made inside itself. Dropping dragged Project items (the dragged item, or the selection it belongs to) on the panel's New Folder button runs it, with the drop outline New Composition shows; the new folder opens with its name ready to type, like one made with the button. Fixes #3196 --- crates/engine/src/cmd/file_more.rs | 36 +++++++++++- crates/engine/src/cmd/project_items.rs | 2 +- crates/engine/src/tests/project_items.rs | 46 +++++++++++++++ crates/ui-egui/src/menus/invoke.rs | 12 +++- crates/ui-egui/src/panels/project.rs | 20 +++++-- .../ui-egui/tests/it/ui_project_new_folder.rs | 58 +++++++++++++++++++ 6 files changed, 162 insertions(+), 12 deletions(-) diff --git a/crates/engine/src/cmd/file_more.rs b/crates/engine/src/cmd/file_more.rs index 168529c47..104907f74 100644 --- a/crates/engine/src/cmd/file_more.rs +++ b/crates/engine/src/cmd/file_more.rs @@ -56,15 +56,39 @@ fn items_p(s: &Session, p: &Value) -> Vec { } } +/// `project.newFolder {name?, parent?, items?}`. With `items` (ids or names), they move into the +/// new folder in the same undo step: Project items dropped on the panel's New Folder button, as +/// in After Effects (#3196). A folder can't end up inside itself. fn new_folder(s: &mut Session, p: &Value) -> Result { + let c = "project.newFolder"; let name = str_p(p, "name").unwrap_or("Untitled Folder").to_string(); let parent = p.get("parent").and_then(Value::as_u64).map(ItemId).filter(|i| s.project.item(*i).is_some_and(|x| x.is_folder())); + let mut items: Vec = vec![]; + if let Some(v) = p.get("items") { + let Value::Array(a) = v else { return Err(bad(c, "`items` must be an array of item ids or names")) }; + for v in a { + let id = super::project_items::item_ref(s, v, c)?; + if !items.contains(&id) { + items.push(id); + } + } + if let Some(f) = parent + && let Some(inside) = items.iter().find(|i| super::project_items::is_within(&s.project, f, **i)) + { + return Err(bad(c, format!("can't move folder {} into a folder inside itself", inside.0))); + } + } let id = s.edit("New Folder", None, |proj, st| { let id = proj.add_item(&name, Label::Yellow, parent, ItemKind::Folder); + for i in &items { + if let Some(it) = proj.item_mut(*i) { + it.parent = Some(id); + } + } st.project_selection = vec![id]; Ok(id) })?; - Ok(json!({"item": id.0})) + if p.get("items").is_some() { Ok(json!({"item": id.0, "moved": items.len()})) } else { Ok(json!({"item": id.0})) } } fn close(s: &mut Session, p: &Value) -> Result { @@ -1202,7 +1226,15 @@ fn reveal_in_finder(s: &mut Session, p: &Value) -> Result { pub fn specs() -> Vec { vec![ - cmd!("project.newFolder", "New Folder", ["File", "New"], Some("Cmd+Alt+Shift+N"), "{name?, parent?}", always, new_folder), + cmd!( + "project.newFolder", + "New Folder", + ["File", "New"], + Some("Cmd+Alt+Shift+N"), + "{name?, parent?, items?: [id|name] (moved into it)}", + always, + new_folder + ), cmd!("file.close", "Close", ["File"], Some("Cmd+W"), "{comp?}", has_comp, close), cmd!("file.closeProject", "Close Project", ["File"], None, "{}", always, close_project), cmd!("file.saveCopy", "Save a Copy...", ["File", "Save As"], None, "{path}", always, save_copy), diff --git a/crates/engine/src/cmd/project_items.rs b/crates/engine/src/cmd/project_items.rs index 4d76861dc..e513a0511 100644 --- a/crates/engine/src/cmd/project_items.rs +++ b/crates/engine/src/cmd/project_items.rs @@ -8,7 +8,7 @@ use super::{CommandSpec, always, b_p, bad, str_p}; use crate::{EngineError, Result, Session, cmd, query}; /// `item` (id or name) → id. -fn item_ref(s: &Session, v: &Value, cmd: &str) -> Result { +pub(super) fn item_ref(s: &Session, v: &Value, cmd: &str) -> Result { match v { Value::Number(n) => n.as_u64().map(ItemId).filter(|i| s.project.item(*i).is_some()), Value::String(name) => s.project.find_by_name(name).map(|i| i.id), diff --git a/crates/engine/src/tests/project_items.rs b/crates/engine/src/tests/project_items.rs index 105174c35..27e8e7818 100644 --- a/crates/engine/src/tests/project_items.rs +++ b/crates/engine/src/tests/project_items.rs @@ -40,6 +40,52 @@ fn move_into_folder_rename_label_comment_with_undo() { assert_eq!(s.project.item(comp).unwrap().comment, ""); } +/// #3196: `project.newFolder` with `items` (items dropped on the Project panel's New Folder +/// button) makes the folder and moves them into it, out of the folder they were in, in one undo +/// step. A folder can't go into a new folder made inside itself. +#[test] +fn new_folder_with_items_moves_them_into_it_in_one_undo_step() { + let mut s = Session::default(); + let a = ItemId(s.execute("comp.new", json!({"name": "A", "width": 64, "height": 64, "duration": 1})).unwrap()["comp"].as_u64().unwrap()); + let b = ItemId(s.execute("comp.new", json!({"name": "B", "width": 64, "height": 64, "duration": 1})).unwrap()["comp"].as_u64().unwrap()); + let bin = ItemId(s.execute("project.newFolder", json!({"name": "Bin"})).unwrap()["item"].as_u64().unwrap()); + s.execute("project.move", json!({"items": [b.0], "folder": bin.0})).unwrap(); + let before = s.project.clone(); + let undo = s.history.undo.len(); + + let r = s.execute("project.newFolder", json!({"items": [a.0, "B", a.0]})).unwrap(); + let f = ItemId(r["item"].as_u64().unwrap()); + assert_eq!(r["moved"], 2, "duplicates count once"); + let folder = s.project.item(f).unwrap(); + assert!(folder.is_folder()); + assert_eq!((folder.name.as_str(), folder.parent), ("Untitled Folder", None), "the New Folder button's name and place"); + assert_eq!(s.project.item(a).unwrap().parent, Some(f)); + assert_eq!(s.project.item(b).unwrap().parent, Some(f), "out of the folder it was in"); + assert_eq!(s.state.project_selection, [f], "the new folder is selected"); + assert_eq!(s.history.undo.len(), undo + 1, "one undo step"); + s.execute("edit.undo", json!({})).unwrap(); + assert_eq!(s.project, before, "one undo takes back the folder and the move"); + + // A folder dropped on New Folder goes into the new folder; a new folder made inside the + // folder that is moving into it is refused, and nothing changes. + let r = s.execute("project.newFolder", json!({"items": [bin.0]})).unwrap(); + let f = ItemId(r["item"].as_u64().unwrap()); + assert_eq!(s.project.item(bin).unwrap().parent, Some(f)); + s.execute("edit.undo", json!({})).unwrap(); + // Inside another folder (`parent`), and an empty list moves nothing. + let r = s.execute("project.newFolder", json!({"parent": bin.0, "items": [a.0]})).unwrap(); + let f = ItemId(r["item"].as_u64().unwrap()); + assert_eq!((s.project.item(f).unwrap().parent, s.project.item(a).unwrap().parent), (Some(bin), Some(f))); + s.execute("edit.undo", json!({})).unwrap(); + assert_eq!(s.execute("project.newFolder", json!({"items": []})).unwrap()["moved"], 0); + s.execute("edit.undo", json!({})).unwrap(); + let before = s.project.clone(); + assert!(s.execute("project.newFolder", json!({"parent": bin.0, "items": [bin.0]})).is_err()); + assert!(s.execute("project.newFolder", json!({"items": [999_999]})).is_err(), "an unknown item"); + assert!(s.execute("project.newFolder", json!({"items": a.0})).is_err(), "not an array"); + assert_eq!(s.project, before); +} + #[test] fn delete_and_duplicate_items() { let mut s = Session::default(); diff --git a/crates/ui-egui/src/menus/invoke.rs b/crates/ui-egui/src/menus/invoke.rs index 0c7573c91..0f53a8372 100644 --- a/crates/ui-egui/src/menus/invoke.rs +++ b/crates/ui-egui/src/menus/invoke.rs @@ -195,10 +195,16 @@ pub fn invoke(app: &mut EffectcraftApp, ctx: &egui::Context, id: &str, params: V } // A new folder (File ▸ New, its shortcut, the Project panel's button and context menu) opens // with its name ready to type, as in After Effects (#1658). The command selects it. - // `engine.execute` keeps the headless default. - if id == "project.newFolder" && no_params(¶ms) { + // `engine.execute` keeps the headless default. Items dropped on the New Folder button (only + // `items`, #3196) open the name too. + if id == "project.newFolder" && (no_params(¶ms) || params.as_object().is_some_and(|m| m.len() == 1 && m.contains_key("items"))) { + let dropped = params.get("items").is_some(); let r = run_engine(app, ctx, id, params); - if r.is_ok() { + if let Ok(v) = &r { + // Open a folder that items were dropped into, so they stay in view. + if dropped && let Some(f) = v.get("item").and_then(Value::as_u64) { + app.ui.project_open_folders.insert(f); + } app.show_panel(PanelKind::Project); crate::panels::project::begin_rename(app, ctx); } diff --git a/crates/ui-egui/src/panels/project.rs b/crates/ui-egui/src/panels/project.rs index 8d67fd9ee..21163391c 100644 --- a/crates/ui-egui/src/panels/project.rs +++ b/crates/ui-egui/src/panels/project.rs @@ -3,7 +3,8 @@ //! Media Duration, Frame Rate, File Path, Comment — shown or hidden from the header's context //! menu), folders (drag items into and out of them), renaming (Enter, or double-click the name), //! the label colour picker, and the bottom bar (interpret, new folder, new comp: click, or drop -//! items on it for a comp from them, bit depth, delete), and the keyboard (the arrow keys select, +//! items on New Folder for a folder holding them, or on New Comp for a comp from them; bit depth, +//! delete), and the keyboard (the arrow keys select, //! extend the selection and open / close folders; rows and buttons carry AccessKit names and //! states for screen readers). Edits are engine commands, so they are undoable. @@ -905,17 +906,24 @@ fn footer(app: &mut EffectcraftApp, ui: &mut egui::Ui, p: &egui::Painter, foot: } } // Items dropped on Create a new Composition: New Comp from Selection with them (After - // Effects; several items ask how in its dialog). - if id == "newComp" + // Effects; several items ask how in its dialog). Dropped on Create a new Folder: a new + // folder holding them (#3196). The dragged item, or the selection it belongs to. + if matches!(id, "newComp" | "newFolder") && let Some(DragPayload::Item(iid)) = egui::DragAndDrop::payload::(&ctx).as_deref() && ui.rect_contains_pointer(r) { p.rect_stroke(r, 4.0, Stroke::new(2.0, t.accent), egui::StrokeKind::Inside); if ctx.input(|i| i.pointer.any_released()) { - if !app.session.state.project_selection.contains(&ItemId(*iid)) { - app.session.state.project_selection = vec![ItemId(*iid)]; + let selected = app.session.state.project_selection.contains(&ItemId(*iid)); + if id == "newComp" { + if !selected { + app.session.state.project_selection = vec![ItemId(*iid)]; + } + actions.push(("file.newCompFromSelection".into(), json!({}))); + } else { + let items: Vec = if selected { app.session.state.project_selection.iter().map(|i| i.0).collect() } else { vec![*iid] }; + actions.push(("project.newFolder".into(), json!({"items": items}))); } - actions.push(("file.newCompFromSelection".into(), json!({}))); egui::DragAndDrop::clear_payload(&ctx); } } diff --git a/crates/ui-egui/tests/it/ui_project_new_folder.rs b/crates/ui-egui/tests/it/ui_project_new_folder.rs index 63a1c6ae3..b86bf663d 100644 --- a/crates/ui-egui/tests/it/ui_project_new_folder.rs +++ b/crates/ui-egui/tests/it/ui_project_new_folder.rs @@ -143,6 +143,64 @@ fn the_file_menu_opens_the_name_in_view() { assert_eq!(name(&h, folder).as_deref(), Some("Renders")); } +/// Press on `from`, move to `to` in steps, release there. +fn drag(h: &mut Harness<'_, EffectcraftApp>, from: Pos2, to: Pos2) { + h.input_mut().events.push(Event::PointerMoved(from)); + h.step(); + h.input_mut().events.push(Event::PointerButton { pos: from, button: PointerButton::Primary, pressed: true, modifiers: Modifiers::NONE }); + h.step(); + for k in 1..=10 { + h.input_mut().events.push(Event::PointerMoved(from + (to - from) * (k as f32 / 10.0))); + h.step(); + } + h.input_mut().events.push(Event::PointerButton { pos: to, button: PointerButton::Primary, pressed: false, modifiers: Modifiers::NONE }); + h.run_steps(3); +} + +fn item_named(h: &Harness<'_, EffectcraftApp>, name: &str) -> ItemId { + h.state().session.project.items.values().find(|i| i.name == name).map(|i| i.id).unwrap_or_else(|| panic!("no item {name}")) +} + +fn parent(h: &Harness<'_, EffectcraftApp>, id: ItemId) -> Option { + h.state().session.project.item(id).and_then(|i| i.parent) +} + +/// #3196: two selected items (one at the root, one in a folder) dragged onto the New Folder +/// button go into a new folder, out of the folder they were in, with its name open as the +/// button's; one undo takes it all back. +#[test] +fn items_dropped_on_the_new_folder_button_go_into_a_new_folder() { + let (mut h, bin) = harness(2); + let (a, b) = (item_named(&h, "Comp 000"), item_named(&h, "Comp 001")); + h.state_mut().session.execute("project.move", json!({"items": [b.0], "folder": bin.0})).unwrap(); + h.state_mut().session.state.project_selection = vec![a, b]; + h.run_steps(3); + let before = folders(&h); + let project = h.state().session.project.clone(); + let from = rect(&h, &format!("project.item.{}.name", a.0)).center(); + let to = rect(&h, "project.newFolder").center(); + drag(&mut h, from, to); + let [folder] = new_folders(&h, &before)[..] else { panic!("one new folder") }; + assert_eq!((parent(&h, a), parent(&h, b)), (Some(folder), Some(folder)), "both dragged items are in it"); + assert_eq!(parent(&h, folder), None, "where the button makes folders"); + assert_eq!(h.state().session.state.project_selection, [folder]); + assert!(renaming(&h, folder), "its name is open, as from the button"); + assert!(h.state().ui.project_open_folders.contains(&folder.0), "open, so the moved items stay in view"); + key(&mut h, Key::Escape); + assert_eq!(name(&h, folder).as_deref(), Some("Untitled Folder")); + h.state_mut().session.execute("edit.undo", json!({})).unwrap(); + assert_eq!(h.state().session.project, project, "one undo takes back the folder and the move"); + + // An item that is not selected goes alone. + h.state_mut().session.state.project_selection = vec![b]; + h.run_steps(3); + let before = folders(&h); + let from = rect(&h, &format!("project.item.{}.name", a.0)).center(); + drag(&mut h, from, to); + let [folder] = new_folders(&h, &before)[..] else { panic!("one new folder") }; + assert_eq!((parent(&h, a), parent(&h, b)), (Some(folder), Some(bin)), "only the dragged item"); +} + /// Agents and scripts: `engine.execute` only creates the folder. #[test] fn engine_execute_only_creates_the_folder() { From f3a9afda1475d0b3df893278913226acf11cb8a7 Mon Sep 17 00:00:00 2001 From: vangel76 Date: Sat, 7 Nov 2026 15:14:09 +0100 Subject: [PATCH 12/74] Effect Controls: lock the panel to the layer it shows (#3193) The Effect Controls tab gets the lock icon the Composition viewers and the Timeline have (panel.tab.EffectControls.lock). Locked, the panel and its tab name keep the layer they showed while that layer's comp is active, whatever is selected next, so a pick whip from another layer's property can reach its parameters. An effect dropped on the locked panel goes to its layer and leaves the selection alone; one applied to another layer says the panel is locked. Deleting the layer or its comp, or replacing the project, unlocks the panel, which follows the selection again. The lock is UI state (UiState::effect_controls_lock) and agents set it with ui.set {effectControlsLocked}. Fixes #3193 --- crates/ui-egui/src/app/frame.rs | 1 + crates/ui-egui/src/control.rs | 6 + crates/ui-egui/src/dock.rs | 4 +- crates/ui-egui/src/dock_ui.rs | 30 +++- crates/ui-egui/src/panels/effect_controls.rs | 11 +- .../src/panels/effect_controls/common.rs | 74 ++++++++- .../src/panels/effect_controls/panel.rs | 9 +- crates/ui-egui/src/state.rs | 21 +++ crates/ui-egui/tests/it/ui_effect_controls.rs | 155 ++++++++++++++++++ docs/control-protocol.md | 2 +- 10 files changed, 296 insertions(+), 17 deletions(-) diff --git a/crates/ui-egui/src/app/frame.rs b/crates/ui-egui/src/app/frame.rs index e77f07840..56794cfe4 100644 --- a/crates/ui-egui/src/app/frame.rs +++ b/crates/ui-egui/src/app/frame.rs @@ -421,6 +421,7 @@ impl EffectcraftApp { crate::color_picker::note_dialog(&ctx, self.dialog); // After the shortcuts, menus and header, which can replace the project (New, Open). self.ui.timeline.scope_links(self.session.script_ui.project_epoch); + panels::effect_controls::scope_lock(self); self.dock_area(ui, body); #[cfg(not(target_arch = "wasm32"))] prefs_live::dropped_files(self, ui, full); diff --git a/crates/ui-egui/src/control.rs b/crates/ui-egui/src/control.rs index 86bd20c93..f6c255d69 100644 --- a/crates/ui-egui/src/control.rs +++ b/crates/ui-egui/src/control.rs @@ -281,6 +281,12 @@ fn set_ui(app: &mut EffectcraftApp, ctx: &egui::Context, p: &Value) -> Outcome { { return err(e); } + // Effect Controls' tab lock: on locks it to the layer it shows. + if let Some(on) = p.get("effectControlsLocked").and_then(Value::as_bool) + && crate::panels::effect_controls::set_lock(app, on) != on + { + return err("effectControlsLocked: Effect Controls shows no layer to lock"); + } set_ui_layout(app, p) } diff --git a/crates/ui-egui/src/dock.rs b/crates/ui-egui/src/dock.rs index c8ada9bf8..5f03af14c 100644 --- a/crates/ui-egui/src/dock.rs +++ b/crates/ui-egui/src/dock.rs @@ -676,8 +676,8 @@ pub fn doc_tabs( out } -/// A viewer lock button (Composition viewers and the Timeline keep their comp while locked): a -/// closed padlock while locked, an open one while unlocked. +/// A panel lock button (Composition viewers and the Timeline keep their comp while locked, +/// Effect Controls its layer): a closed padlock while locked, an open one while unlocked. /// Returns true when clicked. Automation: `panel.tab..lock`. pub fn lock_button(ui: &mut egui::Ui, rect: Rect, panel: PanelKind, locked: bool, t: &Tokens, reg: &mut crate::automation::Registry) -> bool { let resp = ui.interact(rect, egui::Id::unique(("tab-lock", panel.id())), Sense::click()); diff --git a/crates/ui-egui/src/dock_ui.rs b/crates/ui-egui/src/dock_ui.rs index 6523ff757..1fbaa6f83 100644 --- a/crates/ui-egui/src/dock_ui.rs +++ b/crates/ui-egui/src/dock_ui.rs @@ -333,8 +333,11 @@ impl EffectcraftApp { } } let Some(comp) = self.session.active_comp() else { return out }; - if let Some(l) = self.session.state.selected_layers.first().and_then(|id| comp.layer(*id)) { + // Effect Controls names the layer it shows (the one it is locked to, if any). + if let Some(l) = panels::effect_controls::shown_layer_id(self).and_then(|id| comp.layer(id)) { out.push((PanelKind::EffectControls, format!("{} {}", crate::i18n::command_label("window.panel", "Effect Controls"), l.name))); + } + if let Some(l) = self.session.state.selected_layers.first().and_then(|id| comp.layer(*id)) { out.push((PanelKind::Properties, format!("{}: {}", crate::i18n::command_label("window.panel", "Properties"), l.name))); } out @@ -492,7 +495,13 @@ impl EffectcraftApp { } } for p in locks { - if !self.ui.locked_tabs.remove(&p.id()) { + if p == PanelKind::EffectControls { + // Locks on the layer the panel shows (nothing to lock without one). + let on = self.ui.effect_controls_lock.is_none(); + if panels::effect_controls::set_lock(self, on) != on { + self.ui.status = crate::i18n::tr("Select a layer to lock Effect Controls").to_string(); + } + } else if !self.ui.locked_tabs.remove(&p.id()) { self.ui.locked_tabs.insert(p.id().to_string()); } } @@ -638,10 +647,19 @@ fn dock_style(ui: &egui::Ui, t: &Tokens) -> DockStyle { style } -/// Panels with a viewer lock: the Composition viewers and the Timeline keep their comp while -/// locked. +/// Panels with a lock: the Composition viewers and the Timeline keep their comp while locked, +/// Effect Controls its layer. fn has_lock(panel: PanelKind) -> bool { - matches!(panel, PanelKind::Composition | PanelKind::Viewer(_) | PanelKind::Timeline) + matches!(panel, PanelKind::Composition | PanelKind::Viewer(_) | PanelKind::Timeline | PanelKind::EffectControls) +} + +/// Whether `panel`'s tab lock is on. Effect Controls keeps the layer it is locked to +/// (`UiState::effect_controls_lock`), the others are in `UiState::locked_tabs`. +fn is_locked(app: &EffectcraftApp, panel: PanelKind) -> bool { + match panel { + PanelKind::EffectControls => app.ui.effect_controls_lock.is_some(), + _ => app.ui.locked_tabs.contains(&panel.id()), + } } /// EffectCraft's side of the dock area for one frame. @@ -865,7 +883,7 @@ impl DockContent for EffectContent<'_> { let menu = Rect::from_min_max(pos2((header.max.x - MENU_W).max(header.min.x), header.min.y), header.max); if has_lock(*active) { let lock = Rect::from_center_size(pos2(menu.min.x - LOCK_W / 2.0, header.center().y), vec2(14.0, 14.0)); - let locked = self.app.ui.locked_tabs.contains(&active.id()); + let locked = is_locked(self.app, *active); if dock::lock_button(ui, lock, *active, locked, &t, &mut self.app.auto) { self.locks.push(*active); } diff --git a/crates/ui-egui/src/panels/effect_controls.rs b/crates/ui-egui/src/panels/effect_controls.rs index d9706f147..83f502988 100644 --- a/crates/ui-egui/src/panels/effect_controls.rs +++ b/crates/ui-egui/src/panels/effect_controls.rs @@ -1,6 +1,13 @@ //! Effect Controls: the selected layer's effects with all parameters, in After Effects' //! conventions: //! +//! - the tab's lock icon keeps the panel on the layer it shows whatever is selected next (a +//! pick whip from another layer's property can then reach its parameters); deleting that +//! layer or replacing the project unlocks it (see [`set_lock`]). The lock holds while the +//! locked layer's comp is the active comp: with another comp active the panel shows that +//! comp's selection (its edits address layers of the active comp), and the locked layer +//! again when its comp is back. The Effect menu of a right-click on empty space applies to +//! the selected layers, as in After Effects; //! - effect headers with the fx switch, twirl, Reset and About…; click selects the effect //! (Edit ▸ Copy / Paste / Duplicate and Delete then act on it), drag reorders the stack, the //! context menu copies, pastes, duplicates, resets and removes; @@ -34,8 +41,8 @@ mod panel; mod rows; mod viewer; -pub(crate) use common::gesture_key; -pub use common::{choose_files_after_apply, reveal_applied}; +pub use common::{choose_files_after_apply, reveal_applied, set_lock}; +pub(crate) use common::{gesture_key, scope_lock, shown_layer_id}; pub use panel::{show, start_rename}; pub(crate) use rows::toggle_pick; pub use viewer::viewer_hook; diff --git a/crates/ui-egui/src/panels/effect_controls/common.rs b/crates/ui-egui/src/panels/effect_controls/common.rs index ab46a07ac..49bb514dd 100644 --- a/crates/ui-egui/src/panels/effect_controls/common.rs +++ b/crates/ui-egui/src/panels/effect_controls/common.rs @@ -27,10 +27,66 @@ pub(super) fn effect_active(g: &PropGroup, layer: effectcraft_engine::project::L selected.iter().any(|(l, uid)| *l == layer && contains(g, *uid, 0)) } -pub(super) fn selected_layer(app: &EffectcraftApp) -> Option { +/// The layer Effect Controls shows: the layer it is locked to (its tab's lock) while that layer's +/// comp is the active comp, else the first selected layer of the active comp. +pub(crate) fn shown_layer_id(app: &EffectcraftApp) -> Option { let comp = app.session.active_comp()?; - let id = app.session.state.selected_layers.first()?; - comp.layer(*id).cloned() + if let Some(lock) = app.ui.effect_controls_lock + && app.session.active_comp_id().is_some_and(|c| c.0 == lock.comp) + && comp.layer(effectcraft_engine::project::LayerId(lock.layer)).is_some() + { + return Some(effectcraft_engine::project::LayerId(lock.layer)); + } + app.session.state.selected_layers.first().copied().filter(|id| comp.layer(*id).is_some()) +} + +pub(super) fn shown_layer(app: &EffectcraftApp) -> Option { + let id = shown_layer_id(app)?; + app.session.active_comp()?.layer(id).cloned() +} + +/// Lock Effect Controls on the layer it shows (`on`), or unlock it. Locking with no layer shown +/// does nothing; locking while already locked keeps the layer it is locked to. Returns whether +/// the panel is locked afterwards. The tab's lock icon and `ui.set {effectControlsLocked}`. +pub fn set_lock(app: &mut EffectcraftApp, on: bool) -> bool { + if !on { + app.ui.effect_controls_lock = None; + return false; + } + if app.ui.effect_controls_lock.is_some() { + return true; + } + let Some(comp) = app.session.active_comp_id() else { return false }; + let Some(layer) = shown_layer_id(app) else { return false }; + app.ui.effect_controls_lock = Some(crate::state::EffectControlsLock { comp: comp.0, layer: layer.0 }); + app.ui.effect_controls_lock_epoch = app.session.script_ui.project_epoch; + true +} + +/// Once a frame: a lock made in another project (New, Open: the ids start over) or on a layer or +/// comp since deleted is dropped, whether or not Effect Controls is on screen. +pub(crate) fn scope_lock(app: &mut EffectcraftApp) { + let epoch = app.session.script_ui.project_epoch; + if app.ui.effect_controls_lock_epoch != epoch { + app.ui.effect_controls_lock_epoch = epoch; + app.ui.effect_controls_lock = None; + } + prune_lock(app); +} + +/// A lock whose layer or comp no longer exists (deleted, or its comp deleted) is dropped, so the +/// panel follows the selection again and its tab shows it unlocked. Another comp being active +/// keeps the lock: the panel shows the locked layer again when its comp comes back. +pub(super) fn prune_lock(app: &mut EffectcraftApp) { + let Some(lock) = app.ui.effect_controls_lock else { return }; + let alive = app + .session + .project + .comp(effectcraft_engine::project::ItemId(lock.comp)) + .is_some_and(|c| c.layer(effectcraft_engine::project::LayerId(lock.layer)).is_some()); + if !alive { + app.ui.effect_controls_lock = None; + } } /// An effect was just applied (`effect.apply` / `effect.applyLast` with `params`): Effect @@ -39,10 +95,14 @@ pub(super) fn selected_layer(app: &EffectcraftApp) -> Option { pub fn reveal_applied(app: &mut EffectcraftApp, params: &serde_json::Value) { // `effect.apply` selects the new effect on (the last of) the layers it went to. let target = app.session.state.selected_props.last().map(|(l, _)| *l); + // Effect Controls locked to a layer of the active comp keeps showing that layer. + let locked = app.ui.effect_controls_lock.filter(|k| app.session.active_comp_id().is_some_and(|c| c.0 == k.comp)); if let Some(l) = target && !app.session.state.selected_layers.contains(&l) && params.get("comp").is_none() && app.session.active_comp().is_some_and(|c| c.layer(l).is_some()) + // Dropped on the locked panel: it shows that layer already, the selection stays. + && locked.is_none_or(|k| k.layer != l.0) { // Show that layer (dropped on an unselected one), keeping the new effect selected. let props = app.session.state.selected_props.clone(); @@ -51,6 +111,14 @@ pub fn reveal_applied(app: &mut EffectcraftApp, params: &serde_json::Value) { Err(e) => app.ui.status = e.to_string(), } } + // The effect went to another layer than the one the locked panel shows: say why it isn't + // there (After Effects opens another Effect Controls tab). + if let (Some(k), Some(l)) = (locked, target) + && k.layer != l.0 + && let Some(name) = app.session.active_comp().and_then(|c| c.layer(effectcraft_engine::project::LayerId(k.layer))).map(|x| x.name.clone()) + { + app.ui.status = crate::i18n::tr_args("Effect Controls is locked to {}", &[&name]); + } app.raise_panel(crate::dock::PanelKind::EffectControls); } diff --git a/crates/ui-egui/src/panels/effect_controls/panel.rs b/crates/ui-egui/src/panels/effect_controls/panel.rs index 0857dfa3f..95f810d1a 100644 --- a/crates/ui-egui/src/panels/effect_controls/panel.rs +++ b/crates/ui-egui/src/panels/effect_controls/panel.rs @@ -1,4 +1,4 @@ -use super::common::{Actions, PARAM_NAME_X, ROW, effect_active, selected_layer}; +use super::common::{Actions, PARAM_NAME_X, ROW, effect_active, prune_lock, shown_layer}; use super::editors::{curves_editor, editor_height, histogram_only, levels_editor, object_matte_editor, roto_editor, warp_editor}; use super::rows::{FxScope, group_rows, toggle_closed}; use super::{Align2, Color32, EffectcraftApp, EvalCtx, GroupKind, Icon, Layer, PropGroup, Rect, Sense, Stroke, Tokens, json, pos2, vec2, widgets}; @@ -158,7 +158,8 @@ fn about_button(app: &mut EffectcraftApp, ui: &mut egui::Ui, bp: &egui::Painter, } } -/// The Effect Controls panel for the selected layer: a header with the comp and layer names, then +/// The Effect Controls panel for the selected layer (or the layer the panel is locked to, see +/// `common::shown_layer_id`): a header with the comp and layer names, then /// one header per effect (Fx toggle, twirl, rename, Reset, About, label bar, reorder drag and its /// right-click menu) followed by that effect's custom editor and its property rows. Dragging /// effects or presets onto the panel applies them; a right-click on empty space opens the Effect @@ -168,7 +169,9 @@ pub fn show(app: &mut EffectcraftApp, ui: &mut egui::Ui, rect: Rect) { let t = app.tokens; let p = ui.painter().with_clip_rect(rect); let ctx = ui.ctx().clone(); - let Some(layer) = selected_layer(app) else { + // A locked layer that was deleted unlocks the panel, which then follows the selection. + prune_lock(app); + let Some(layer) = shown_layer(app) else { p.text(rect.center(), Align2::CENTER_CENTER, crate::i18n::tr("Select a layer to see its effects"), Tokens::ui(12.0), t.text_faint); return; }; diff --git a/crates/ui-egui/src/state.rs b/crates/ui-egui/src/state.rs index 60a30e97e..34336c297 100644 --- a/crates/ui-egui/src/state.rs +++ b/crates/ui-egui/src/state.rs @@ -594,6 +594,14 @@ impl Default for AnimToolsState { } } +/// The layer a locked Effect Controls panel keeps showing (the tab's lock icon, as After Effects' +/// panel lock): comp item id and layer id. +#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)] +pub struct EffectControlsLock { + pub comp: u64, + pub layer: u64, +} + #[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Serialize, Deserialize)] #[serde(rename_all = "camelCase")] pub enum ProjectStatus { @@ -651,6 +659,17 @@ pub struct UiState { /// bring a locked panel forward (the tab's lock icon, as After Effects' viewer lock). #[serde(default)] pub locked_tabs: BTreeSet, + /// Effect Controls locked to a layer (its tab's lock icon): the panel keeps showing that + /// layer whatever is selected while its comp is the active comp. Cleared when the layer or + /// its comp is deleted, and when the project is replaced (see + /// `panels::effect_controls::set_lock`). + #[serde(default)] + pub effect_controls_lock: Option, + /// The project the lock was made in (`ScriptUi::project_epoch`): a new or opened project + /// reuses comp and layer ids, so its lock is dropped (see + /// `panels::effect_controls::scope_lock`). + #[serde(skip)] + pub effect_controls_lock_epoch: u64, /// The comp each Composition viewer shows, by viewer id (0 = the Composition panel; see /// `panels::viewers`). #[serde(default)] @@ -773,6 +792,8 @@ impl Default for UiState { native_outer: Vec::new(), maximized: None, locked_tabs: BTreeSet::new(), + effect_controls_lock: None, + effect_controls_lock_epoch: 0, viewers: Default::default(), active_viewer: 0, focused: PanelKind::Composition, diff --git a/crates/ui-egui/tests/it/ui_effect_controls.rs b/crates/ui-egui/tests/it/ui_effect_controls.rs index 1db059220..d72febf01 100644 --- a/crates/ui-egui/tests/it/ui_effect_controls.rs +++ b/crates/ui-egui/tests/it/ui_effect_controls.rs @@ -912,3 +912,158 @@ fn double_click(h: &mut Harness<'_, EffectcraftApp>, pos: egui::Pos2) { } h.run_steps(2); } + +/// Two solids in one comp, `A` with a Point Control and `B` with an Angle Control, `A` selected +/// and Effect Controls open: the app, A, B and their effects. +fn two_layers() -> (EffectcraftApp, u64, u64, u64, u64) { + let mut s = Session::default(); + s.execute("comp.new", json!({"name": "Main", "width": 320, "height": 180, "frameRate": 30, "duration": 5})).unwrap(); + let a = s.execute("layer.newSolid", json!({"name": "A", "color": "#ff0000"})).unwrap()["layer"].as_u64().unwrap(); + let b = s.execute("layer.newSolid", json!({"name": "B", "color": "#0000ff"})).unwrap()["layer"].as_u64().unwrap(); + let fa = s.execute("effect.apply", json!({"layers": [a], "effect": "Point Control"})).unwrap()["effects"][0].as_u64().unwrap(); + let fb = s.execute("effect.apply", json!({"layers": [b], "effect": "Angle Control"})).unwrap()["effects"][0].as_u64().unwrap(); + s.state.selected_layers = vec![LayerId(a)]; + s.state.selected_props.clear(); + let mut app = EffectcraftApp::new(s); + app.show_panel(PanelKind::EffectControls); + (app, a, b, fa, fb) +} + +/// Whether Effect Controls drew effect `fx`'s header in the last frame (not the frame before: +/// a stale id must not pass). +fn shows_effect(h: &Harness<'_, EffectcraftApp>, fx: u64) -> bool { + let id = format!("effectControls.effect.{fx}"); + h.state().auto.elements.iter().any(|e| e.id == id) +} + +fn select_layer(h: &mut Harness<'_, EffectcraftApp>, layer: u64) { + h.state_mut().session.execute("layer.select", json!({"layers": [layer]})).unwrap(); + h.run_steps(3); +} + +/// #3193: the Effect Controls tab's lock keeps the panel (and its tab name) on the layer it showed +/// when locked whatever is selected next, through undo; deleting that layer unlocks it without a +/// crash and the panel follows the selection again. +#[test] +fn locked_effect_controls_keep_their_layer_until_it_is_deleted() { + let (app, a, b, fa, fb) = two_layers(); + let mut h = Harness::builder().with_size(egui::vec2(1600.0, 1000.0)).build_eframe(|_| app); + h.run_steps(4); + assert!(shows_effect(&h, fa) && !shows_effect(&h, fb), "A is shown before locking"); + let comp = h.state().session.active_comp_id().unwrap().0; + let lock = rect_of(&h, "panel.tab.EffectControls.lock").center(); + click(&mut h, lock); + assert_eq!(h.state().ui.effect_controls_lock, Some(effectcraft_ui_egui::state::EffectControlsLock { comp, layer: a })); + // Selecting B keeps A in the panel and its tab. + select_layer(&mut h, b); + assert!(shows_effect(&h, fa) && !shows_effect(&h, fb), "the locked panel keeps A"); + let tab = h.state().auto.elements.iter().find(|e| e.id == "panel.tab.EffectControls").map(|e| e.label.clone()).unwrap_or_default(); + assert!(tab.ends_with(" A"), "the tab names the locked layer: {tab:?}"); + // An edit and its undo leave the lock alone. + h.state_mut().session.execute("prop.set", json!({"layer": b, "path": "transform/opacity", "value": 50.0})).unwrap(); + h.state_mut().session.execute("edit.undo", json!({})).unwrap(); + h.run_steps(3); + assert!(shows_effect(&h, fa) && !shows_effect(&h, fb), "undo keeps the lock"); + // The lock state is UI state: it survives a serde round trip. + let ui = serde_json::to_value(&h.state().ui).unwrap(); + let back: effectcraft_ui_egui::state::UiState = serde_json::from_value(ui).unwrap(); + assert_eq!(back.effect_controls_lock, h.state().ui.effect_controls_lock); + // Unlocking follows the selection again. + let lock = rect_of(&h, "panel.tab.EffectControls.lock").center(); + click(&mut h, lock); + assert_eq!(h.state().ui.effect_controls_lock, None); + assert!(shows_effect(&h, fb) && !shows_effect(&h, fa), "unlocked, the panel shows the selection"); + // Lock on A again, select B, delete A: no crash, the lock is gone, B is shown. + select_layer(&mut h, a); + let lock = rect_of(&h, "panel.tab.EffectControls.lock").center(); + click(&mut h, lock); + assert_eq!(h.state().ui.effect_controls_lock.map(|l| l.layer), Some(a)); + select_layer(&mut h, b); + h.state_mut().session.execute("edit.clear", json!({"layers": [a]})).unwrap(); + h.run_steps(3); + assert_eq!(h.state().ui.effect_controls_lock, None, "deleting the locked layer unlocks the panel"); + assert!(!shows_effect(&h, fa), "A is gone from the panel"); + // Deleting clears the selection; the panel follows the next one. + select_layer(&mut h, b); + assert!(shows_effect(&h, fb), "the panel follows the selection"); + // Undoing the delete brings A back without bringing back (or breaking) the lock. + h.state_mut().session.execute("edit.undo", json!({})).unwrap(); + h.run_steps(3); + assert_eq!(h.state().ui.effect_controls_lock, None); + let selected = h.state().session.state.selected_layers.first().map(|l| l.0); + let (on, off) = if selected == Some(a) { (fa, fb) } else { (fb, fa) }; + assert!(shows_effect(&h, on) && !shows_effect(&h, off), "the panel shows the selection ({selected:?})"); +} + +/// #3193: a new or opened project reuses comp and layer ids, so a lock does not carry into it, +/// even onto a layer with the locked one's ids. +#[test] +fn replacing_the_project_unlocks_effect_controls() { + let (app, a, b, _, _) = two_layers(); + let mut h = Harness::builder().with_size(egui::vec2(1600.0, 1000.0)).build_eframe(|_| app); + h.run_steps(4); + let lock = rect_of(&h, "panel.tab.EffectControls.lock").center(); + click(&mut h, lock); + assert_eq!(h.state().ui.effect_controls_lock.map(|l| l.layer), Some(a)); + select_layer(&mut h, b); + // The same project opened again: every id is alive. + let project = (*h.state().session.project).clone(); + h.state_mut().session.replace_project(project, None); + h.run_steps(3); + assert_eq!(h.state().ui.effect_controls_lock, None, "the lock belongs to the replaced project"); +} + +/// #3193: an effect dropped on the locked panel goes to the locked layer and leaves the selection +/// alone; one applied to another layer says why the panel doesn't show it. +#[test] +fn applying_an_effect_while_locked_keeps_the_selection() { + let (app, a, b, fa, _) = two_layers(); + let mut h = Harness::builder().with_size(egui::vec2(1600.0, 1000.0)).build_eframe(|_| app); + h.run_steps(4); + let lock = rect_of(&h, "panel.tab.EffectControls.lock").center(); + click(&mut h, lock); + select_layer(&mut h, b); + let ctx = h.ctx.clone(); + effectcraft_ui_egui::menus::invoke(h.state_mut(), &ctx, "effect.apply", json!({"effect": "Color Control", "layers": [a]})).unwrap(); + h.run_steps(3); + assert_eq!(h.state().session.state.selected_layers, vec![LayerId(b)], "the selection stays on B"); + assert!(shows_effect(&h, fa), "the panel still shows A"); + assert_eq!(h.state().ui.effect_controls_lock.map(|l| l.layer), Some(a)); + // Applied to the selected B: the panel stays on A and says so. + h.state_mut().ui.status.clear(); + effectcraft_ui_egui::menus::invoke(h.state_mut(), &ctx, "effect.apply", json!({"effect": "Color Control", "layers": [b]})).unwrap(); + h.run_steps(3); + assert!(shows_effect(&h, fa)); + assert_eq!(h.state().ui.status, "Effect Controls is locked to A"); +} + +/// #3193: agents lock Effect Controls with `ui.set {effectControlsLocked}`; with no layer shown +/// there is nothing to lock. +#[test] +fn effect_controls_lock_over_the_control_channel() { + use effectcraft_ui_egui::control::{self, ControlRequest, Outcome}; + fn send(h: &mut Harness<'_, EffectcraftApp>, params: serde_json::Value) -> serde_json::Value { + let ctx = h.ctx.clone(); + let (req, _rx) = ControlRequest::new("ui.set", params); + let now = h.state().now(); + match control::handle(h.state_mut(), &ctx, &req, now) { + Outcome::Done(v) => v, + _ => panic!("ui.set: no immediate reply"), + } + } + let (app, a, b, fa, fb) = two_layers(); + let mut h = Harness::builder().with_size(egui::vec2(1600.0, 1000.0)).build_eframe(|_| app); + h.run_steps(4); + assert_eq!(send(&mut h, json!({"effectControlsLocked": true}))["ok"], true); + assert_eq!(h.state().ui.effect_controls_lock.map(|l| l.layer), Some(a)); + select_layer(&mut h, b); + assert!(shows_effect(&h, fa) && !shows_effect(&h, fb)); + assert_eq!(send(&mut h, json!({"effectControlsLocked": false}))["ok"], true); + h.run_steps(3); + assert!(shows_effect(&h, fb) && !shows_effect(&h, fa)); + // Nothing selected: nothing to lock. + h.state_mut().session.state.selected_layers.clear(); + h.run_steps(2); + assert_eq!(send(&mut h, json!({"effectControlsLocked": true}))["ok"], false); + assert_eq!(h.state().ui.effect_controls_lock, None); +} diff --git a/docs/control-protocol.md b/docs/control-protocol.md index 9f3d69ea5..8af641a3a 100644 --- a/docs/control-protocol.md +++ b/docs/control-protocol.md @@ -66,7 +66,7 @@ panel registers an element id; both are enforced by `crates/ui-egui/tests/it/ui_ |---|---|---| | `ui.inspect` | `{}` | Window size, pixels per point, fps, serialized `UiState`, playback, time, active comp, selection, open dialog, last render ms, `keyboardFocus`, `windowFocused`, `workspaceStorageError`. | | `ui.elements` | `{prefix?}` | `[{id, label, rect: [x, y, w, h]}]`: the widgets drawn in the last frame, in points. | -| `ui.set` | `{tool?, workspace?, theme?, focused?, viewer?, timeline?, menuBar?, home?}` | `viewer: {zoom (null = fit), res: Full\|Half\|Third\|Quarter\|Auto, pan: [x,y], grid, rulers, safeMargins, transparencyGrid}`; `timeline: {pps (null = fit), start, graphEditor, showModes, openLayers: [ids], openGroups: [uids]}`. | +| `ui.set` | `{tool?, workspace?, theme?, focused?, viewer?, effectControlsLocked?, timeline?, menuBar?, home?}` | `viewer: {zoom (null = fit), res: Full\|Half\|Third\|Quarter\|Auto, pan: [x,y], grid, rulers, safeMargins, transparencyGrid}`; `effectControlsLocked: true` locks Effect Controls on the layer it shows (an error when it shows none), `false` unlocks it (the tab's lock icon, `panel.tab.EffectControls.lock`; `ui.inspect` reports it as `effect_controls_lock: {comp, layer}`); `timeline: {pps (null = fit), start, graphEditor, showModes, openLayers: [ids], openGroups: [uids]}`. | | `ui.panel.show` / `ui.panel.close` | `{panel}` | Panels: `Project`, `Composition`, `Timeline`, `EffectControls`, `EffectsPresets`, `Info`, `Preview`, `Character`, `Paragraph`, `Align` and others. | | `ui.playback` | `{action: play\|stop\|toggle\|status}` | `{playing, time, audio, levelsDb, peaksDb}` (audio: an audio preview is running; L/R meter levels and held peaks in dBFS) | | `ui.resize` | `{width, height}` | Resizes the window (points). | From 714ff82fd64a7e3d8550590aad209377e21b94a5 Mon Sep 17 00:00:00 2001 From: vangel76 Date: Sat, 7 Nov 2026 15:35:14 +0100 Subject: [PATCH 13/74] Viewer: a Shift-constrained drag snaps only along its axis A layer Move, path-vertex drag or Pan Behind drag held to one axis with Shift snapped with the full 2D rule, so a point target (another layer's corner or midpoint, a path vertex, the comp centre) gave the snap both of its coordinates and pulled the dragged feature off the axis. The new engine function viewer::snap_along snaps only the free coordinate: a point acts as a line through it across the drag, reaching it within the tolerance on each axis; lines along the drag are left out. A point hit reports the point and the short line from it to where the feature landed, so the viewer draws the alignment. The viewer's SnapQuery carries the axis one_axis picks (drag_axis); Move, Vertices and Pan Behind pass it. snap() itself is unchanged. Fixes #3229 --- crates/engine/src/tests/viewer.rs | 33 ++++++++ crates/engine/src/viewer.rs | 44 ++++++++++ crates/ui-egui/src/panels/viewer.rs | 24 ++++-- crates/ui-egui/src/panels/viewer/geometry.rs | 16 ++-- crates/ui-egui/src/panels/viewer_tools.rs | 15 +++- crates/ui-egui/tests/it/ui_viewer.rs | 87 ++++++++++++++++++++ 6 files changed, 201 insertions(+), 18 deletions(-) diff --git a/crates/engine/src/tests/viewer.rs b/crates/engine/src/tests/viewer.rs index 31197b2a8..9d3087f20 100644 --- a/crates/engine/src/tests/viewer.rs +++ b/crates/engine/src/tests/viewer.rs @@ -920,3 +920,36 @@ fn polystar_undo_sanitizes_live_and_parked_property_selections_before_uid_reuse( assert_eq!(layer(&s, lid).props.prop("transform/positionZ").unwrap(), &position_z, "the reused UID must not become an implicit expression target"); } } + +/// #3229: a drag Shift keeps to one axis snaps only that axis. A point a few pixels off the axis +/// lines the feature up with it (along x: its x) without taking it off the axis, as a line +/// through the point would; lines along the drag are left out, and the point reaches only `tol` +/// off the axis. Unconstrained (`snap`) the same point takes both coordinates, the feature leaving +/// the axis. +#[test] +fn a_shift_drag_snaps_only_along_its_axis() { + use crate::viewer::snap_along; + let src = SnapSource::Layer(LayerId(1)); + let corner = SnapTarget::point([450.0, 150.0], PRI_LAYER, src); + let row = SnapTarget::hline(151.0, PRI_GUIDE, SnapSource::Guide); + let column = SnapTarget::vline(449.0, PRI_GUIDE, SnapSource::Guide); + let feature = [[448.0, 153.0]]; + assert_eq!(snap(&feature, &[corner, row], 8.0).unwrap().at, [450.0, 150.0]); + // Along x: x onto the corner's, y kept (the nearer row does not pull it either). + let s = snap_along(&feature, &[corner, row], 8.0, true).unwrap(); + assert_eq!(s.delta, [2.0, 0.0]); + assert_eq!(s.at, [450.0, 153.0]); + // The point, and the line from it to the feature (the feedback). + assert_eq!(s.hits, [corner, SnapTarget::vline(450.0, PRI_LAYER, src).within(Some([150.0, 153.0]))]); + // Along y: y onto the corner's, x kept (the column is left out). + let s = snap_along(&feature, &[corner, column], 8.0, false).unwrap(); + assert_eq!(s.delta, [0.0, -3.0]); + assert_eq!(s.at, [448.0, 150.0]); + assert_eq!(s.hits, [corner, SnapTarget::hline(150.0, PRI_LAYER, src).within(Some([450.0, 448.0]))]); + // A line across the drag still snaps; a nearer one wins over the point. + let s = snap_along(&feature, &[corner, column], 8.0, true).unwrap(); + assert_eq!(s.at, [449.0, 153.0]); + assert_eq!(s.hits, [column]); + // The point reaches only the tolerance off the axis. + assert_eq!(snap_along(&[[448.0, 170.0]], &[corner], 8.0, true), None); +} diff --git a/crates/engine/src/viewer.rs b/crates/engine/src/viewer.rs index f8e4c7740..fd9cb220a 100644 --- a/crates/engine/src/viewer.rs +++ b/crates/engine/src/viewer.rs @@ -189,6 +189,50 @@ pub fn layer_features(ctx: &EvalCtx, layer: &Layer) -> Vec<[f64; 2]> { out } +/// [`snap`] for a drag Shift keeps to one axis (`along_x`: it moves along x and y stays put, else +/// the other way round): only the free coordinate snaps, so the feature never leaves the axis +/// (#3229). A point target acts as a line through it across the drag (a vertical line at its x +/// for a drag along x) that reaches it only within `tol` on each axis (a square around the point), +/// so the feature lines up with a point near the axis, not with every point in the comp; lines +/// along the drag (horizontal ones for a drag along x) would only move the locked coordinate and +/// are left out. The nearest target wins, ties to the lower priority value. A hit on a point +/// reports the point (the feedback box) and the line from it to where the feature landed. +pub fn snap_along(sources: &[[f64; 2]], targets: &[SnapTarget], tol: f64, along_x: bool) -> Option { + // (Key, source, target, and the free coordinate it snaps to.) + let mut best: Option<((f64, u8), [f64; 2], SnapTarget, f64)> = None; + for s in sources { + let (free, along) = if along_x { (s[0], s[1]) } else { (s[1], s[0]) }; + for t in targets { + // The line the target snaps as: its coordinate and extent along it. + let (line, span) = match (t.kind, along_x) { + (SnapKind::Point, true) => (t.pos[0], Some([t.pos[1] - tol, t.pos[1] + tol])), + (SnapKind::Point, false) => (t.pos[1], Some([t.pos[0] - tol, t.pos[0] + tol])), + (SnapKind::VLine, true) => (t.pos[0], t.span), + (SnapKind::HLine, false) => (t.pos[1], t.span), + (SnapKind::VLine, false) | (SnapKind::HLine, true) => continue, + }; + let d = (line - free).abs(); + if d > tol || span.is_some_and(|[a, b]| along < a || along > b) { + continue; + } + let key = (d, t.priority); + if best.as_ref().is_none_or(|(k, _, _, _)| better(key, *k)) { + best = Some((key, *s, *t, line)); + } + } + } + let (_, s, t, line) = best?; + let (delta, at) = if along_x { ([line - s[0], 0.0], [line, s[1]]) } else { ([0.0, line - s[1]], [s[0], line]) }; + let mut hits = vec![t]; + if t.kind == SnapKind::Point { + // The alignment, drawn from the point to the feature. + let (from, to) = if along_x { (t.pos[1], at[1]) } else { (t.pos[0], at[0]) }; + let l = if along_x { SnapTarget::vline(line, t.priority, t.source) } else { SnapTarget::hline(line, t.priority, t.source) }; + hits.push(l.within(Some([from, to]))); + } + Some(Snap { delta, hits, at }) +} + /// A layer's content box in comp space: corners (top left, top right, bottom right, bottom /// left), edge midpoints (top, right, bottom, left) and centre. struct BoxPoints { diff --git a/crates/ui-egui/src/panels/viewer.rs b/crates/ui-egui/src/panels/viewer.rs index b1899e1f8..d84626b45 100644 --- a/crates/ui-egui/src/panels/viewer.rs +++ b/crates/ui-egui/src/panels/viewer.rs @@ -1448,10 +1448,13 @@ fn phase_drag(app: &mut EffectcraftApp, ui: &mut egui::Ui, ectx: &EvalCtx, resp: } } Gesture::Move { layers, start, snap_src } => { - let mut d = one_axis([cpt[0] - start[0], cpt[1] - start[1]], mods.shift); + let raw = [cpt[0] - start[0], cpt[1] - start[1]]; + let mut d = one_axis(raw, mods.shift); if let Some(src) = snap_src { let ids: Vec = layers.iter().map(|x| x.0).collect(); - let c = vt::snap(app, &ctx, ectx, &map, &ids, &[[src[0] + d[0], src[1] + d[1]]], mods); + // Shift: only the coordinate along the axis snaps, so the layer stays on it (#3229). + let q = vt::SnapQuery { exclude: &ids, sources: &[[src[0] + d[0], src[1] + d[1]]], mods, extra: &[], along_x: drag_axis(raw, mods.shift) }; + let c = vt::snap_with(app, &ctx, ectx, &map, &q); d = [d[0] + c[0], d[1] + c[1]]; if vt::snapping_on(app, mods) { vt::draw_handle(&painter, map.to_screen([src[0] + d[0], src[1] + d[1]])); @@ -1491,10 +1494,12 @@ fn phase_drag(app: &mut EffectcraftApp, ui: &mut egui::Ui, ectx: &EvalCtx, resp: Gesture::Anchor { layer, start_anchor, start_pos, start, inv, l2p, own, anchor_only } => { // Pan Behind snaps the anchor point (to its own layer's box, other layers' features, // guides and the grid); Shift keeps the move to one axis. - let d = one_axis([cpt[0] - start[0], cpt[1] - start[1]], mods.shift); + let raw = [cpt[0] - start[0], cpt[1] - start[1]]; + let d = one_axis(raw, mods.shift); let anchor_c = inv.inverse().map(|m| m.apply(gv2(start_anchor[0], start_anchor[1]))).unwrap_or(gv2(start[0], start[1])); let want = [anchor_c.x + d[0], anchor_c.y + d[1]]; - let sc = vt::snap_with(app, &ctx, ectx, &map, &vt::SnapQuery { exclude: &[layer], sources: &[want], mods, extra: &own }); + let q = vt::SnapQuery { exclude: &[layer], sources: &[want], mods, extra: &own, along_x: drag_axis(raw, mods.shift) }; + let sc = vt::snap_with(app, &ctx, ectx, &map, &q); let a = inv.apply(gv2(start[0], start[1])); let b = inv.apply(gv2(start[0] + d[0] + sc[0], start[1] + d[1] + sc[1])); let dl = gv2(b.x - a.x, b.y - a.y); @@ -1528,9 +1533,12 @@ fn phase_drag(app: &mut EffectcraftApp, ui: &mut egui::Ui, ectx: &EvalCtx, resp: } Gesture::Vertices { start, src, applied, inv, layer } => { // The grabbed vertex snaps (to other layers' vertices and features, guides, grid); - // Shift keeps it to one axis, as a layer move does (#1964). - let want = one_axis([cpt[0] - start[0], cpt[1] - start[1]], mods.shift); - let c = vt::snap(app, &ctx, ectx, &map, &[layer], &[[src[0] + want[0], src[1] + want[1]]], mods); + // Shift keeps it to one axis, as a layer move does (#1964), snapping too (#3229). + let raw = [cpt[0] - start[0], cpt[1] - start[1]]; + let want = one_axis(raw, mods.shift); + let at = [src[0] + want[0], src[1] + want[1]]; + let q = vt::SnapQuery { exclude: &[layer], sources: &[at], mods, extra: &[], along_x: drag_axis(raw, mods.shift) }; + let c = vt::snap_with(app, &ctx, ectx, &map, &q); let total = [want[0] + c[0], want[1] + c[1]]; let d = inv.apply_vec(gv2(total[0] - applied[0], total[1] - applied[1])); if d.x != 0.0 || d.y != 0.0 { @@ -1993,7 +2001,7 @@ mod view; use frame::{empty_state, rect_of, show_frame}; pub(crate) use frame::{extended_region, minify_factor, snap_to_pixels, styled_line, zoom_sampler, zoom_texture_options}; use geometry::{ - anchor_in_comp, cam_state, drag_ratio, handle_axes, handle_cursor, handle_point, one_axis, project_segment, quad_intersects_box, rotate_value, + anchor_in_comp, cam_state, drag_axis, drag_ratio, handle_axes, handle_cursor, handle_point, one_axis, project_segment, quad_intersects_box, rotate_value, rotation_sign, selected_others, snap_handle, turn_delta, }; #[cfg(test)] diff --git a/crates/ui-egui/src/panels/viewer/geometry.rs b/crates/ui-egui/src/panels/viewer/geometry.rs index 144047f32..e597b50b3 100644 --- a/crates/ui-egui/src/panels/viewer/geometry.rs +++ b/crates/ui-egui/src/panels/viewer/geometry.rs @@ -260,15 +260,19 @@ pub(super) fn rotate_value(start_rot: f64, turned: f64, shift: bool) -> f64 { /// A drag `d` (comp pixels), kept to the axis it moves along most when `shift` is held. pub(super) fn one_axis(d: [f64; 2], shift: bool) -> [f64; 2] { - if !shift { - d - } else if d[0].abs() > d[1].abs() { - [d[0], 0.0] - } else { - [0.0, d[1]] + match drag_axis(d, shift) { + None => d, + Some(true) => [d[0], 0.0], + Some(false) => [0.0, d[1]], } } +/// The axis [`one_axis`] keeps a drag `d` to: `Some(true)` along x, `Some(false)` along y, `None` +/// without `shift` (both axes free). Its snapping keeps to the same axis (#3229). +pub(super) fn drag_axis(d: [f64; 2], shift: bool) -> Option { + shift.then(|| d[0].abs() > d[1].abs()) +} + pub(super) fn point_in_quad(p: [f64; 2], q: &[[f64; 2]; 4]) -> bool { let mut sign = 0.0; for i in 0..4 { diff --git a/crates/ui-egui/src/panels/viewer_tools.rs b/crates/ui-egui/src/panels/viewer_tools.rs index 563657e91..ab415fd9a 100644 --- a/crates/ui-egui/src/panels/viewer_tools.rs +++ b/crates/ui-egui/src/panels/viewer_tools.rs @@ -48,21 +48,23 @@ pub(crate) fn snap( sources: &[[f64; 2]], mods: egui::Modifiers, ) -> [f64; 2] { - snap_with(app, ctx, ectx, map, &SnapQuery { exclude, sources, mods, extra: &[] }) + snap_with(app, ctx, ectx, map, &SnapQuery { exclude, sources, mods, extra: &[], along_x: None }) } /// What [`snap_with`] snaps: the dragged features and the layers to ignore, plus `extra` layer -/// targets (the Pan Behind layer's own box). +/// targets (the Pan Behind layer's own box). `along_x` is the axis a Shift drag keeps to +/// (`Some(true)`: x), whose coordinate alone snaps ([`vw::snap_along`], #3229). pub(crate) struct SnapQuery<'a> { pub exclude: &'a [LayerId], pub sources: &'a [[f64; 2]], pub mods: egui::Modifiers, pub extra: &'a [vw::SnapTarget], + pub along_x: Option, } /// [`snap`] with extra layer targets. pub(crate) fn snap_with(app: &EffectcraftApp, ctx: &egui::Context, ectx: &EvalCtx, map: &ViewerMap, q: &SnapQuery) -> [f64; 2] { - let SnapQuery { exclude, sources, mods, extra } = *q; + let SnapQuery { exclude, sources, mods, extra, along_x } = *q; let layers = snapping_on(app, mods); let v = &app.ui.viewer; let guides = v.snap_guides && v.guides; @@ -75,7 +77,12 @@ pub(crate) fn snap_with(app: &EffectcraftApp, ctx: &egui::Context, ectx: &EvalCt if layers { targets.extend_from_slice(extra); } - match vw::snap(sources, &targets, SNAP_PX / map.zoom.max(1e-3) as f64) { + let tol = SNAP_PX / map.zoom.max(1e-3) as f64; + let hit = match along_x { + Some(x) => vw::snap_along(sources, &targets, tol, x), + None => vw::snap(sources, &targets, tol), + }; + match hit { Some(s) => { let d = s.delta; ctx.data_mut(|dd| dd.insert_temp(snap_id(), s)); diff --git a/crates/ui-egui/tests/it/ui_viewer.rs b/crates/ui-egui/tests/it/ui_viewer.rs index 7b32fa2ec..7cc504bd6 100644 --- a/crates/ui-egui/tests/it/ui_viewer.rs +++ b/crates/ui-egui/tests/it/ui_viewer.rs @@ -456,6 +456,37 @@ fn layer_drag_snaps_to_other_layers_midpoints() { } } +/// #3229: a layer Shift-dragged along x past another layer's corner a few pixels off the axis +/// lines up with the corner's x and stays on the axis (it used to jump onto the corner), with +/// Snap Edges Extended on and off. +#[test] +fn shift_drag_snaps_a_layer_along_its_axis_only() { + let mut h = harness(); + let box_id = h.state().session.active_comp().unwrap().layers[0].id; + // A 100×100 target at x 450–550, y 50–150: its bottom left corner at (450, 150). + let target = h.state_mut().session.execute("layer.newSolid", json!({"name": "Target", "color": "#20e040", "width": 100, "height": 100})).unwrap()["layer"] + .as_u64() + .unwrap(); + for extended in [true, false] { + // The 80×80 box at x 60–140, y 153–233: its top right corner (140, 153) 3 px below the + // target's corner. + let s = &mut h.state_mut().session; + s.execute("view.snappingOptions", json!({"edgesExtended": extended})).unwrap(); + s.execute("prop.set", json!({"layer": target, "path": "transform/position", "value": [500, 100, 0]})).unwrap(); + s.execute("prop.set", json!({"layer": box_id.0, "path": "transform/position", "value": [100, 193, 0]})).unwrap(); + s.execute("edit.deselectAll", json!({})).unwrap(); + h.run_steps(2); + // Grab the box by that corner and Shift-drag it right (and a pixel down), the corner to + // 2 px short of the target corner's x. + let (from, to) = (screen(&h, [138.0, 155.0]), screen(&h, [446.0, 156.0])); + hold_drag(&mut h, from, to, egui::Modifiers::SHIFT); + let l = h.state().session.active_comp().unwrap().layer(box_id).unwrap().clone(); + let p = l.props.prop("transform/position").unwrap().value.as_vec3(); + assert!((p[0] - 410.0).abs() < 0.01, "the corner lines up with the target's, extended {extended}: {p:?}"); + assert!((p[1] - 193.0).abs() < 1e-6, "and the box stays on the axis, extended {extended}: {p:?}"); + } +} + /// With a shape layer selected the Rectangle tool draws a new group into its Contents (After /// Effects' behaviour); with nothing selected it draws a new shape layer (#227). #[test] @@ -927,6 +958,32 @@ fn shift_drag_keeps_a_mask_vertex_to_one_axis() { assert!(free[0] > shifted[0] && free[1] > shifted[1], "without Shift both axes move: {shifted:?} -> {free:?}"); } +/// #3229: a mask vertex Shift-dragged along x past another layer's corner a few pixels off the +/// axis lines up with the corner's x and stays on the axis (it used to jump onto the corner). +#[test] +fn shift_drag_snaps_a_mask_vertex_along_its_axis_only() { + let mut h = harness(); + let plate = layer_id(&h, "Plate"); + // The Box (80×80 at the comp centre) has its top left corner at (280, 140); the mask's first + // vertex starts 3 px below that row. + let corners = [[200.0, 143.0], [240.0, 100.0], [240.0, 60.0], [200.0, 60.0]]; + let mask = { + let s = &mut h.state_mut().session; + s.execute("layer.select", json!({"layers": [plate]})).unwrap(); + let m = s.execute("mask.new", json!({"layer": plate, "vertices": corners, "closed": true})).unwrap()["mask"].as_u64().unwrap(); + s.execute("mask.selectVertices", json!({"vertices": [{"layer": plate, "mask": m, "index": 0}]})).unwrap(); + m + }; + h.run_steps(2); + // Shift-drag it right (and a pixel down) to 2 px short of the corner's x. + let from = rect(&h, &format!("viewer.mask.{mask}.vertex.0")).center(); + let to = from + (screen(&h, [278.0, 144.0]) - screen(&h, [200.0, 143.0])); + drag_path(&mut h, &[from, to], egui::Modifiers::SHIFT); + let v = mask_path(&h, "Plate").vertices[0]; + assert!((v[0] - 280.0).abs() < 0.01, "the vertex lines up with the corner: {v:?}"); + assert!((v[1] - 143.0).abs() < 1e-6, "and stays on the axis: {v:?}"); +} + /// #999: a mask vertex dragged against the edge of the Composition panel pans the view, and the /// vertex keeps moving while the pointer is held there, as in After Effects. #[test] @@ -1392,6 +1449,36 @@ fn pan_behind_snaps_the_anchor_point() { assert!((a[0] - 260.0).abs() < 0.01 && (a[1] - 120.0).abs() < 0.01, "{a:?}"); } +/// #3229: Pan Behind Shift-dragging the anchor point along x past another layer's corner a few +/// pixels off the axis lines it up with the corner's x and keeps it on the axis (it used to jump +/// onto the corner). +#[test] +fn pan_behind_shift_drag_snaps_the_anchor_along_its_axis_only() { + let mut h = harness(); + let box_id = h.state().session.active_comp().unwrap().layers[0].id; + // A 100×100 target at x 450–550, y -3–97: its bottom left corner at (450, 97), 3 px above + // the anchor point's row. The anchor point (100, 100) sits 10 px above the box's centre, so + // none of the box's own lines runs along that row. + let target = h.state_mut().session.execute("layer.newSolid", json!({"name": "Target", "color": "#20e040", "width": 100, "height": 100})).unwrap()["layer"] + .as_u64() + .unwrap(); + let s = &mut h.state_mut().session; + s.execute("prop.set", json!({"layer": target, "path": "transform/position", "value": [500, 47, 0]})).unwrap(); + s.execute("prop.set", json!({"layer": box_id.0, "path": "transform/anchor", "value": [40, 30, 0]})).unwrap(); + s.execute("prop.set", json!({"layer": box_id.0, "path": "transform/position", "value": [100, 100, 0]})).unwrap(); + s.execute("layer.select", json!({"layers": [box_id.0]})).unwrap(); + h.state_mut().ui.tool = Tool::PanBehind; + h.run_steps(2); + // Shift-drag the anchor right (and a pixel down) to 2 px short of the corner's x. The drag + // starts in small steps (Pan Behind picks the layer where the drag starts, still inside the + // box) and holds the end point (the gesture applies the previous frame's pointer). + let path = [screen(&h, [100.0, 100.0]), screen(&h, [110.0, 100.0]), screen(&h, [448.0, 101.0]), screen(&h, [448.0, 101.0])]; + drag_path(&mut h, &path, egui::Modifiers::SHIFT); + let (p, a) = position_and_anchor(&h, box_id); + assert!((p[0] - 450.0).abs() < 0.01 && (a[0] - 390.0).abs() < 0.01, "the anchor point lines up with the corner: {p:?} {a:?}"); + assert!((p[1] - 100.0).abs() < 1e-6 && (a[1] - 30.0).abs() < 1e-6, "and stays on the axis: {p:?} {a:?}"); +} + /// Issue #147: Pan Behind snaps the anchor to its own layer's corners, edge midpoints and centre /// (only the anchor itself is left out: the box stays put while the anchor moves). #[test] From fc9eca99a0832ceac83fba2217c8bc59595741d5 Mon Sep 17 00:00:00 2001 From: vangel76 Date: Sat, 7 Nov 2026 15:42:40 +0100 Subject: [PATCH 14/74] #3231: evaluate prop.get and expression errors with the footage source prop.get and Session::expression_errors (Reveal Expression Errors, the viewer's error banner) built their expression context without the footage source, so an expression that reads footage pixels (sampleImage) gave a different value or a spurious error there while it worked in the render and in expr.errors (#1000). expr_tools::eval_ctx builds the render's context (expression host and footage source) for a comp at a time; expr.errors, prop.get, Session::expression_errors and the script value query all use it. Fixes #3231 --- crates/engine/src/cmd/expr_tools.rs | 14 ++++++-- crates/engine/src/cmd/layer_menu.rs | 4 +-- crates/engine/src/cmd/prop.rs | 5 +-- crates/engine/src/tests/track.rs | 54 +++++++++++++++++++++++++++++ crates/script/src/model.rs | 5 +-- 5 files changed, 71 insertions(+), 11 deletions(-) diff --git a/crates/engine/src/cmd/expr_tools.rs b/crates/engine/src/cmd/expr_tools.rs index cb39077af..bd62a8e46 100644 --- a/crates/engine/src/cmd/expr_tools.rs +++ b/crates/engine/src/cmd/expr_tools.rs @@ -2,7 +2,7 @@ //! Language menu (the ⓕ button next to an expression), written from the public Expression //! Language Reference. -use effectcraft_project::{Expression, ItemId, Layer, Property}; +use effectcraft_project::{Comp, Expression, ItemId, Layer, Property}; use effectcraft_render::EvalCtx; use effectcraft_time::Tick; use serde::Serialize; @@ -42,8 +42,16 @@ impl ExprError { /// expressions and the syntax errors that disabled others. pub fn errors(s: &Session, cid: ItemId, t: Tick) -> Vec { let Some(comp) = s.project.comp(cid) else { return vec![] }; - let ctx = EvalCtx { footage: Some(s.footage.as_ref()), expr: s.expr.as_deref(), ..EvalCtx::new(&s.project, cid, comp, t) }; - errors_in(&ctx, s.expr_check, || true).unwrap_or_default() + errors_in(&eval_ctx(s, cid, comp, t), s.expr_check, || true).unwrap_or_default() +} + +/// The context to evaluate expressions of comp `cid` at comp time `t` as the render does for the +/// comp at top level: with the session's expression host and its footage source, so +/// `sampleImage()` reads footage pixels instead of failing or sampling transparent (#1000, +/// #3231). `expr.errors`, `prop.get`, [`Session::expression_errors`] and the script value query +/// all evaluate through this one context. +pub fn eval_ctx<'a>(s: &'a Session, cid: ItemId, comp: &'a Comp, t: Tick) -> EvalCtx<'a> { + EvalCtx { footage: Some(s.footage.as_ref()), expr: s.expr.as_deref(), ..EvalCtx::new(&s.project, cid, comp, t) } } /// Evaluate diagnostics against an immutable snapshot. Background callers can cancel between diff --git a/crates/engine/src/cmd/layer_menu.rs b/crates/engine/src/cmd/layer_menu.rs index 11a5a3665..8047baa39 100644 --- a/crates/engine/src/cmd/layer_menu.rs +++ b/crates/engine/src/cmd/layer_menu.rs @@ -867,8 +867,8 @@ impl Session { }); for pr in has { let text = pr.expr.as_ref().map(|e| e.text.clone()).unwrap_or_default(); - let mut ctx = effectcraft_render::EvalCtx::new(&self.project, cid, comp, t); - ctx.expr = Some(host.as_ref()); + // With the footage source, so `sampleImage()` doesn't fail here only (#3231). + let ctx = super::expr_tools::eval_ctx(self, cid, comp, t); if let Err(e) = host.eval(&ctx, l, pr, &pr.value_at(l.layer_time(t))) { errors.push(json!({"layer": l.id.0, "prop": pr.uid, "name": pr.name, "expression": text, "error": e})); } diff --git a/crates/engine/src/cmd/prop.rs b/crates/engine/src/cmd/prop.rs index b4432dd2f..4b91649cf 100644 --- a/crates/engine/src/cmd/prop.rs +++ b/crates/engine/src/cmd/prop.rs @@ -558,8 +558,9 @@ fn get(s: &mut Session, p: &Value) -> Result { } else if pr.has_expression() && let (Some(h), Some(comp)) = (s.expr.as_deref(), s.project.comp(cid)) { - let mut ctx = crate::render::EvalCtx::new(&s.project, cid, comp, t); - ctx.expr = Some(h); + // The render's context, footage source included, so `sampleImage()` evaluates here as it + // renders and as `expr.errors` reports it (#3231). + let ctx = super::expr_tools::eval_ctx(s, cid, comp, t); match h.eval(&ctx, l, pr, &raw) { Ok(v) => out["evaluated"] = v.to_json(), Err(e) => { diff --git a/crates/engine/src/tests/track.rs b/crates/engine/src/tests/track.rs index 8c7037b69..bd1adaeca 100644 --- a/crates/engine/src/tests/track.rs +++ b/crates/engine/src/tests/track.rs @@ -433,3 +433,57 @@ fn analyze_1080p_single_point_speed() { assert!(dist(fc.value_at(frame_time(30)).as_vec2(), [990.0, 555.0]) < 0.25); assert!(fps > 30.0, "{fps} frames/s"); } + +/// A stand-in for `sampleImage()` on a footage layer (the engine has no expression language): the +/// pixel at (0, 0) of the layer's footage, read through the context's footage source. The real +/// one samples transparent without the source; this one fails, so either symptom shows. +struct SampleFootage; + +impl crate::render::ExprHost for SampleFootage { + fn eval( + &self, + ctx: &EvalCtx, + layer: &effectcraft_project::Layer, + _: &effectcraft_project::Property, + _: &effectcraft_keyframe::Value, + ) -> Result { + let item = layer.source.item().ok_or("not a footage layer")?; + let Some(ItemKind::Footage(footage)) = ctx.project.item(item).map(|i| &i.kind) else { return Err("not a footage layer".into()) }; + let src = ctx.footage.ok_or("sampleImage: no footage source")?; + let img = src.frame(item, footage, ctx.time).ok_or("no frame")?; + Ok(effectcraft_keyframe::Value::Scalar(f64::from(img.get(0, 0)[0]) * 100.0)) + } +} + +/// The clip of [`setup`] (uniform 25 % grey footage) with an opacity expression that samples its +/// footage: 25 with the footage source. Here for `setup`'s footage layer (#3231). +fn sampling_clip() -> (Session, ItemId, LayerId) { + let (mut s, clip, _) = setup(|_| Image::filled(W, H, [0.25, 0.25, 0.25, 1.0])); + let cid = s.active_comp_id().unwrap(); + s.expr = Some(Arc::new(SampleFootage)); + s.execute("prop.setExpression", json!({"layer": clip.0, "path": "transform/opacity", "expression": "sampleImage([0, 0])[0] * 100"})).unwrap(); + (s, cid, clip) +} + +/// #3231: `prop.get` evaluates an expression that samples footage pixels with the footage source, +/// as the render and `expr.errors` do: the same value and no spurious `expressionError`. +#[test] +fn prop_get_evaluates_footage_sampling_expressions_with_the_footage_source() { + let (mut s, cid, clip) = sampling_clip(); + let got = s.execute("prop.get", json!({"layer": clip.0, "path": "transform/opacity", "time": 0.0})).unwrap(); + assert!(got.get("expressionError").is_none(), "no spurious error: {got}"); + assert_eq!(got["evaluated"].as_f64(), Some(25.0), "the footage pixel, not the keyframed 100: {got}"); + // The render applies the same opacity: the clip alone (outside the solid) covers 25 %. + let img = s.render(cid, Tick::ZERO, crate::render::RenderOpts::default()); + let a = img.get(5, 5)[3]; + assert!((a - 0.25).abs() < 0.01, "rendered opacity {a}"); + assert!(crate::cmd::expr_tools::errors(&s, cid, Tick::ZERO).is_empty()); +} + +/// #3231: Reveal Expression Errors and the viewer's error banner evaluate with the footage source, +/// so an expression that samples footage pixels isn't reported as failing. +#[test] +fn expression_errors_evaluate_footage_sampling_expressions_with_the_footage_source() { + let (s, cid, _) = sampling_clip(); + assert_eq!(s.expression_errors(cid), Vec::::new()); +} diff --git a/crates/script/src/model.rs b/crates/script/src/model.rs index e662c1cdc..694d82d39 100644 --- a/crates/script/src/model.rs +++ b/crates/script/src/model.rs @@ -519,12 +519,9 @@ fn value(s: &Session, a: &J) -> R { let v = if pre || !p.has_expression() { p.value_at(l.layer_time(t)) } else { - let mut ctx = EvalCtx::new(&s.project, cid, c, t); - ctx.expr = s.expr.as_deref(); // Sample expressions (`sampleImage`) read footage through the session's source; without // it footage layers sample as transparent (#1000). - ctx.footage = Some(s.footage.as_ref()); - ctx.value(l, p) + effectcraft_engine::commands::expr_tools::eval_ctx(s, cid, c, t).value(l, p) }; Ok(js_value(c, &v)) } From 6ff41bcb10356f43ebb4d19659a4ed6a88424c7d Mon Sep 17 00:00:00 2001 From: vangel76 Date: Sat, 7 Nov 2026 15:26:41 +0100 Subject: [PATCH 15/74] Timeline: Convert To Bezier Path in the context menu of a parametric shape path Right-clicking a Rectangle, Ellipse or Polystar Path row offers After Effects' Convert To Bezier Path, running path.convertToBezier with that layer and path. A shape group's row offers it too and converts the static parametric paths inside it; keyframed or expression-driven ones are left alone, since converting drops their animation. Fixes #3237 --- crates/ui-egui/src/panels/timeline.rs | 71 +++++++++++++++- .../tests/it/ui_blank_context_menus.rs | 84 ++++++++++++++++++- 2 files changed, 153 insertions(+), 2 deletions(-) diff --git a/crates/ui-egui/src/panels/timeline.rs b/crates/ui-egui/src/panels/timeline.rs index 038dcc9b9..0770f5e1f 100644 --- a/crates/ui-egui/src/panels/timeline.rs +++ b/crates/ui-egui/src/panels/timeline.rs @@ -3716,12 +3716,14 @@ fn group_row_click(app: &mut EffectcraftApp, ui: &mut egui::Ui, g: &mut Rw, rc: let RowKind::Group { uid, name, fx, .. } = &rc.row.kind else { return }; let gr_rect = Rect::from_min_max(pos2(indent + 8.0, r.min.y), pos2(cw.switches, r.max.y)); // Effects, masks, shape items and animators drag to a new place among their siblings. - let movable = !layer.switches.locked && layer.props.find_group(*uid).is_some_and(PropGroup::is_instance); + let group = layer.props.find_group(*uid); + let movable = !layer.switches.locked && group.is_some_and(PropGroup::is_instance); let gr = ui.interact(gr_rect, egui::Id::unique(("grow", uid)), if movable { Sense::click_and_drag() } else { Sense::click() }); if movable && gr.drag_started_by(egui::PointerButton::Primary) { ui.ctx().data_mut(|d| d.insert_temp(group_drag_id(), GroupDrag { comp: cid, owner: gr.id, layer: layer.id, uid: *uid })); } app.auto.add(&format!("timeline.group.{uid}.name"), gr_rect, name); + shape_path_menu(&mut app.auto, &gr, layer, group, &mut *actions); if gr.clicked() && uid & (WAVE_BIT | GEOM_BIT) == 0 { // Shift or Cmd/Ctrl adds the group to the selection, or takes a selected one out, as // property rows and Effect Controls headers do (#2552: several effects and styles for @@ -3745,6 +3747,73 @@ fn group_row_click(app: &mut EffectcraftApp, ui: &mut egui::Ui, g: &mut Rw, rc: } } +/// The shape items Convert To Bezier Path turns into editable paths. +const PARAMETRIC_PATHS: [&str; 3] = ["rect", "ellipse", "star"]; + +/// The parametric paths (Rectangle, Ellipse, Polystar Path) inside shape group `g`, nested groups +/// included, that convert without losing anything: a keyframed or expression-driven one would +/// lose its animation, so the group leaves it alone. +fn static_parametric_paths(g: &PropGroup) -> Vec { + // Depth-first, without recursion; only groups and their contents hold paths. + let mut out = Vec::new(); + let mut stack = vec![g]; + while let Some(n) = stack.pop() { + for c in n.groups() { + match c.match_id.as_str() { + m if PARAMETRIC_PATHS.contains(&m) => { + if !c.any_animated() { + out.push(c.uid); + } + } + "group" | "contents" => stack.push(c), + _ => {} + } + } + } + out +} + +/// Right-click on a Rectangle, Ellipse or Polystar Path row, or on a shape group holding such +/// paths: After Effects' Convert To Bezier Path, acting on that row (#3237). A path's own row +/// converts it even when animated (After Effects makes a static path at the current time); the +/// group's entry, an addition to After Effects, converts the static paths inside it. +fn shape_path_menu( + auto: &mut crate::automation::Registry, + resp: &egui::Response, + layer: &Layer, + group: Option<&PropGroup>, + actions: &mut Vec<(String, serde_json::Value)>, +) { + let Some(g) = group.filter(|_| matches!(layer.source, LayerSource::Shape)) else { return }; + let uid = g.uid; + let items = if PARAMETRIC_PATHS.contains(&g.match_id.as_str()) { + vec![uid] + } else if g.match_id == "group" { + // Walked once, when the menu opens, not for every group row on every frame. + let key = egui::Id::unique(("tl-shape-path-menu", layer.id.0, uid)); + if resp.secondary_clicked() { + let found = static_parametric_paths(g); + resp.ctx.data_mut(|d| d.insert_temp(key, found)); + } + resp.ctx.data(|d| d.get_temp::>(key)).unwrap_or_default() + } else { + return; + }; + if items.is_empty() { + return; + } + let (cmd, label) = ("path.convertToBezier", "Convert To Bezier Path"); + let params = json!({"layer": layer.id.0, "items": items}); + crate::widgets::context_menu(resp, |ui| { + let r = ui.add_enabled(!layer.switches.locked, egui::Button::new(crate::i18n::context_item(cmd, ¶ms, label))); + auto.add(&format!("timeline.group.{uid}.menu.convertToBezier"), r.rect, label); + if r.clicked() { + actions.push((cmd.into(), params.clone())); + ui.close(); + } + }); +} + fn waveform_row(app: &mut EffectcraftApp, g: &Rw, rc: &RowCtx<'_>) { let t = g.tl.t; let graph_x0 = g.tl.graph_x0; diff --git a/crates/ui-egui/tests/it/ui_blank_context_menus.rs b/crates/ui-egui/tests/it/ui_blank_context_menus.rs index 84080be08..31312d5b2 100644 --- a/crates/ui-egui/tests/it/ui_blank_context_menus.rs +++ b/crates/ui-egui/tests/it/ui_blank_context_menus.rs @@ -1,4 +1,5 @@ -//! Right-click actions in the blank Project and Timeline areas, and on Timeline property names. +//! Right-click actions in the blank Project and Timeline areas, and on Timeline property and +//! shape path names. use crate::gpu_test::RenderSerial; use effectcraft_engine::Session; use effectcraft_engine::project::ItemKind; @@ -12,6 +13,8 @@ fn harness() -> Harness<'static, EffectcraftApp> { let mut session = Session::default(); session.execute("comp.new", json!({"name":"Empty", "width":640,"height":360,"duration":4})).unwrap(); let mut h = Harness::builder().with_size(vec2(1600.0, 1000.0)).build_eframe(|_| EffectcraftApp::new(session)); + // English whatever the system language: the menu items found below by label are translated. + h.state_mut().session.prefs.general.language = "en".into(); h.run_steps(3); h } @@ -230,6 +233,85 @@ fn timeline_property_names_have_the_property_menu() { assert_eq!(h.state().dialog, Some(Dialog::Form)); } +/// #3237: right-clicking a Rectangle Path row in the Timeline offers After Effects' Convert To +/// Bezier Path, which turns that path into an editable one; the shape group holding it offers the +/// same and converts the parametric paths inside it. +#[test] +fn timeline_parametric_shape_path_menu_converts_to_bezier() { + use effectcraft_engine::project::{LayerId, PropGroup}; + let mut h = harness(); + let layer = h.state_mut().session.execute("layer.newShape", json!({"kind": "rect", "size": [120, 80]})).unwrap()["layer"].as_u64().unwrap(); + let contents = + |h: &Harness<'_, EffectcraftApp>| h.state().session.active_comp().unwrap().layer(LayerId(layer)).unwrap().props.sub("contents").unwrap().clone(); + fn groups(g: &PropGroup, out: &mut Vec<(u64, String)>) { + out.push((g.uid, g.match_id.clone())); + for c in g.groups() { + groups(c, out); + } + } + let all = |h: &Harness<'_, EffectcraftApp>| { + let mut out = Vec::new(); + groups(&contents(h), &mut out); + out + }; + let find = |h: &Harness<'_, EffectcraftApp>, ids: &[&str]| all(h).into_iter().find(|(_, m)| ids.contains(&m.as_str())).map(|(u, _)| u); + let open = |h: &mut Harness<'_, EffectcraftApp>| { + h.state_mut().ui.timeline.open_layers.insert(layer); + for (uid, _) in all(&*h) { + h.state_mut().ui.timeline.open_groups.insert(uid); + } + h.run_steps(3); + }; + let choose = |h: &mut Harness<'_, EffectcraftApp>, row: u64| { + let p = rect(h, &format!("timeline.group.{row}.name")).center(); + right_click(h, p); + assert!(h.query_by_label("Convert To Bezier Path").is_some(), "the menu offers Convert To Bezier Path"); + let p = rect(h, &format!("timeline.group.{row}.menu.convertToBezier")).center(); + for pressed in [true, false] { + h.input_mut().events.push(Event::PointerMoved(p)); + h.input_mut().events.push(Event::PointerButton { pos: p, button: PointerButton::Primary, pressed, modifiers: Modifiers::NONE }); + h.step(); + } + h.run_steps(3); + }; + let group = find(&h, &["group"]).expect("a shape group"); + let path = find(&h, &["rect"]).expect("a Rectangle Path"); + // The Rectangle Path's own row. + open(&mut h); + snapshot(&mut h, "shape-path-rows"); + choose(&mut h, path); + assert!(find(&h, &["rect"]).is_none(), "the rectangle is converted"); + assert!(find(&h, &["path"]).is_some(), "it is a bezier path now"); + assert_eq!(h.state().session.history.undo.last().map(|u| u.0.as_str()), Some("Convert To Bezier Path")); + // A bezier path has nothing to convert: its row has no such entry. + let bezier = find(&h, &["path"]).unwrap(); + open(&mut h); + let p = rect(&h, &format!("timeline.group.{bezier}.name")).center(); + right_click(&mut h, p); + assert!(h.query_by_label("Convert To Bezier Path").is_none(), "a bezier path has no Convert To Bezier Path"); + egui::Popup::close_all(&h.ctx); + // The shape group's row converts the rectangle inside it. + h.state_mut().session.execute("edit.undo", json!({})).unwrap(); + assert_eq!(find(&h, &["rect"]), Some(path), "undo restores the rectangle"); + open(&mut h); + choose(&mut h, group); + assert!(find(&h, &["rect"]).is_none(), "the group's rectangle is converted"); + assert!(find(&h, &["path"]).is_some()); + // A keyframed rectangle would lose its animation: the group leaves it out (and so has nothing + // to offer), while the path's own row still converts it, as After Effects does. + h.state_mut().session.execute("edit.undo", json!({})).unwrap(); + let size = contents(&h).find_group(path).unwrap().get("size").unwrap().uid; + h.state_mut().session.execute("prop.addKey", json!({"layer": layer, "path": format!("@{size}"), "time": 0.0, "value": [120, 80]})).unwrap(); + open(&mut h); + let p = rect(&h, &format!("timeline.group.{group}.name")).center(); + right_click(&mut h, p); + assert!(h.query_by_label("Convert To Bezier Path").is_none(), "nothing static to convert in the group"); + egui::Popup::close_all(&h.ctx); + h.run_steps(3); + choose(&mut h, path); + assert!(find(&h, &["rect"]).is_none(), "the path's own row converts an animated rectangle"); +} + /// The rows of a context menu touch: a gap between two entries highlighted neither and /// swallowed a click there. #[test] From f4af2c093bcf4f10edaef0ad88ca5278c499d550 Mon Sep 17 00:00:00 2001 From: vangel76 Date: Sat, 7 Nov 2026 15:40:33 +0100 Subject: [PATCH 16/74] Timeline: Add Expression from the stopwatch's right-click menu (#3192) Linux window managers (Cinnamon, KDE, GNOME) take Alt+click to move the window, so Alt-clicking a stopwatch never reached the app. Right-clicking the stopwatch now opens the property's menu (the one its name has), and Add Expression picked there or from the name's menu opens the new expression's editor with the keyboard, as the Alt-click does (#1072). Fixes #3192 --- crates/ui-egui/src/panels/timeline.rs | 55 +++++++++++++++++------ crates/ui-egui/tests/it/ui_inline_edit.rs | 44 ++++++++++++++++++ 2 files changed, 86 insertions(+), 13 deletions(-) diff --git a/crates/ui-egui/src/panels/timeline.rs b/crates/ui-egui/src/panels/timeline.rs index 038dcc9b9..81784a373 100644 --- a/crates/ui-egui/src/panels/timeline.rs +++ b/crates/ui-egui/src/panels/timeline.rs @@ -4053,6 +4053,7 @@ fn prop_key_navigator(app: &mut EffectcraftApp, ui: &mut egui::Ui, g: &mut Rw, r /// The stopwatch. fn prop_stopwatch(app: &mut EffectcraftApp, ui: &mut egui::Ui, g: &mut Rw, rc: &RowCtx<'_>, prop: &Property) { let t = g.tl.t; + let cid = g.tl.cid; let ctx = g.tl.ctx.clone(); let actions = &mut g.tl.actions; let lp = g.lp.clone(); @@ -4062,8 +4063,9 @@ fn prop_stopwatch(app: &mut EffectcraftApp, ui: &mut egui::Ui, g: &mut Rw, rc: & let indent = rc.indent; let uid = &prop.uid; - // Stopwatch: Alt+click adds an expression; a press toggles the animation, and - // dragging on over other stopwatches gives them the same state (#284). + // Stopwatch: Alt+click (or Add Expression in its right-click menu) adds an expression; a + // press toggles the animation, and dragging on over other stopwatches gives them the same + // state (#284). if !prop.static_only { let swr = Rect::from_center_size(pos2(indent, cy), vec2(15.0, 15.0)); let resp = ui.interact(swr, egui::Id::unique(("stopwatch", uid)), Sense::click_and_drag()); @@ -4076,18 +4078,13 @@ fn prop_stopwatch(app: &mut EffectcraftApp, ui: &mut egui::Ui, g: &mut Rw, rc: & }; icons::paint(&lp, swr, Icon::Stopwatch, col); app.auto.add(&format!("timeline.prop.{uid}.stopwatch"), swr, &prop.name); + // Right-click: the property's menu, whose Add Expression needs no Alt, which Linux + // window managers take for moving the window (#3192). + prop_context_menu(app, &ctx, &resp, cid, layer, prop, &mut *actions); if ui.input(|i| i.modifiers.alt) { if resp.clicked() { actions.push(("prop.setExpression".into(), json!({"layer": layer.id.0, "prop": uid}))); - if prop.expr.is_none() { - // Adding one: its editor opens, comes into view and takes the - // keyboard with the new expression selected (#1072). - app.ui.timeline.expr_closed.remove(uid); - focus_expression(&ctx, *uid, [usize::MAX, 0]); - } else if ctx.data(|d| d.get_temp::(expr_focus_id())).is_some_and(|f| f.prop == *uid) { - // Removing it: nothing is left to take the keyboard. - ctx.data_mut(|d| d.remove::(expr_focus_id())); - } + expression_toggled(app, &ctx, prop); } } else if let Some((value, merge)) = switch_gesture(ui, &resp, r, "stopwatch", prop.is_animated()) { actions.push(("prop.toggleAnimation".into(), json!({"layer": layer.id.0, "prop": uid, "value": value, "merge": merge}))); @@ -4131,10 +4128,41 @@ fn prop_name(app: &mut EffectcraftApp, ui: &mut egui::Ui, g: &mut Rw, rc: &RowCt if name_resp.clicked() { actions.push(("prop.select".into(), json!({"layer": layer.id.0, "prop": uid, "add": ui.input(|i| i.modifiers.shift)}))); } - crate::widgets::context_menu(&name_resp, |ui| prop_menu(ui, &mut app.auto, cid, layer, prop, &mut *actions)); + prop_context_menu(app, &ctx, &name_resp, cid, layer, prop, &mut *actions); name_x } +/// `prop`'s context menu ([`prop_menu`]) on `resp`, its name or its stopwatch. Its Add or +/// Remove Expression does what an Alt-click on the stopwatch does to the editor. +fn prop_context_menu( + app: &mut EffectcraftApp, + ctx: &egui::Context, + resp: &egui::Response, + cid: ItemId, + layer: &Layer, + prop: &Property, + actions: &mut Vec<(String, serde_json::Value)>, +) { + let before = actions.len(); + crate::widgets::context_menu(resp, |ui| prop_menu(ui, &mut app.auto, cid, layer, prop, &mut *actions)); + if actions.get(before..).is_some_and(|picked| picked.iter().any(|(id, _)| id == "prop.setExpression")) { + expression_toggled(app, ctx, prop); + } +} + +/// An expression was just added to `prop` (it had none) or removed: adding one opens its +/// editor, which comes into view and takes the keyboard with the new expression selected +/// (#1072); removing it leaves nothing to take the keyboard. +fn expression_toggled(app: &mut EffectcraftApp, ctx: &egui::Context, prop: &Property) { + let uid = prop.uid; + if prop.expr.is_none() { + app.ui.timeline.expr_closed.remove(&uid); + focus_expression(ctx, uid, [usize::MAX, 0]); + } else if ctx.data(|d| d.get_temp::(expr_focus_id())).is_some_and(|f| f.prop == uid) { + ctx.data_mut(|d| d.remove::(expr_focus_id())); + } +} + /// The value editors and the property pick whip. fn prop_value_cell(app: &mut EffectcraftApp, ui: &mut egui::Ui, g: &mut Rw, rc: &RowCtx<'_>, ectx: &EvalCtx<'_>, prop: &Property, name_x: f32) { let vis = g.tl.vis; @@ -5496,7 +5524,8 @@ pub(crate) fn is_position(layer: &Layer, uid: u64) -> bool { layer.transform().is_some_and(|tr| ["position", "positionX", "positionY", "positionZ"].iter().any(|m| tr.get(m).is_some_and(|p| p.uid == uid))) } -/// A property's context menu (right-click its name in the Timeline), as in After Effects. +/// A property's context menu: right-click its name in the Timeline, as in After Effects, or its +/// stopwatch (an Add Expression that needs no Alt-click, #3192). fn prop_menu( ui: &mut egui::Ui, auto: &mut crate::automation::Registry, diff --git a/crates/ui-egui/tests/it/ui_inline_edit.rs b/crates/ui-egui/tests/it/ui_inline_edit.rs index 6c29045bb..5d338118b 100644 --- a/crates/ui-egui/tests/it/ui_inline_edit.rs +++ b/crates/ui-egui/tests/it/ui_inline_edit.rs @@ -634,6 +634,50 @@ fn an_expression_added_at_the_bottom_of_the_timeline_comes_into_view() { assert_eq!(expression(&h, layer, opacity).as_deref(), Some("25")); } +fn right_click(h: &mut Harness<'_, EffectcraftApp>, p: Pos2) { + h.input_mut().events.push(Event::PointerMoved(p)); + h.step(); + h.input_mut().events.push(Event::PointerButton { pos: p, button: PointerButton::Secondary, pressed: true, modifiers: Modifiers::NONE }); + h.step(); + h.input_mut().events.push(Event::PointerButton { pos: p, button: PointerButton::Secondary, pressed: false, modifiers: Modifiers::NONE }); + h.run_steps(3); +} + +/// Right-click Opacity's `target` (its stopwatch or name) in the Timeline, pick Add Expression +/// and type `text` without another click: the new expression's editor has the keyboard. +fn add_expression_from_menu(target: &str, text: &str) { + let (mut h, _) = harness(); + let (layer, opacity, _) = open_transform(&mut h, "Plate"); + let at = center(&h, &format!("timeline.prop.{opacity}.{target}")); + right_click(&mut h, at); + let item = format!("timeline.prop.{opacity}.menu.addExpression"); + assert!(h.state().auto.find(&item).is_some(), "the {target}'s menu offers Add Expression"); + let animated = + h.state().session.active_comp().unwrap().layer(effectcraft_engine::project::LayerId(layer)).unwrap().props.find(opacity).unwrap().is_animated(); + assert!(!animated, "the right-click didn't toggle the stopwatch"); + let at = center(&h, &item); + click(&mut h, at, 1); + assert!(expression(&h, layer, opacity).is_some(), "an expression was added from the {target}'s menu"); + type_text(&mut h, text); + commit_expression(&mut h); + assert_eq!(expression(&h, layer, opacity).as_deref(), Some(text), "typing replaced the expression added from the {target}'s menu"); +} + +/// #3192: Linux window managers take Alt+click to move the window, so the stopwatch has a +/// right-click menu whose Add Expression does what the Alt-click does: the expression is added +/// and its editor takes the keyboard (the stopwatch itself isn't toggled). +#[test] +fn add_expression_from_the_stopwatch_menu_focuses_the_new_expression() { + add_expression_from_menu("stopwatch", "50"); +} + +/// #3192: Add Expression from the property name's menu opens the editor with the keyboard too, +/// as the Alt-click does (it took another click). +#[test] +fn add_expression_from_the_name_menu_focuses_the_new_expression() { + add_expression_from_menu("name", "90"); +} + /// A primary-button drag from `from` to `to` with `modifiers` held from the press to the release. fn drag_with(h: &mut Harness<'_, EffectcraftApp>, from: Pos2, to: Pos2, modifiers: Modifiers) { h.input_mut().events.push(Event::PointerMoved(from)); From c54b542274076139f0ad52902f341bf8d485748c Mon Sep 17 00:00:00 2001 From: vangel76 Date: Sat, 7 Nov 2026 16:02:19 +0100 Subject: [PATCH 17/74] Tests: Match System resolves to English in ui-egui test builds Fixes #3244. The ui-egui tests look up English labels, but Session::default() keeps the language pref "system", so on a German (or any non-English) system they read translated labels and fail. Match System now resolves to English under cfg(test) (lib tests) and under a new test-only `test-support` feature, which the crate's own dev-dependency on itself turns on for tests/it, examples and workspace test builds. Product builds are unchanged; tests about a translation still set their language explicitly. The Match System test now asserts the English pin explicitly. --- Cargo.lock | 1 + crates/ui-egui/Cargo.toml | 10 ++++++++++ crates/ui-egui/src/i18n.rs | 30 +++++++++++++++++++++++------- 3 files changed, 34 insertions(+), 7 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 5b426c8f8..c7c81888f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1988,6 +1988,7 @@ dependencies = [ "effectcraft-segment", "effectcraft-text", "effectcraft-time", + "effectcraft-ui-egui", "eframe", "egui", "egui_extras", diff --git a/crates/ui-egui/Cargo.toml b/crates/ui-egui/Cargo.toml index 1576907cb..3c5862594 100644 --- a/crates/ui-egui/Cargo.toml +++ b/crates/ui-egui/Cargo.toml @@ -45,7 +45,17 @@ craft-update = { workspace = true } pollster = { workspace = true } sys-locale = { workspace = true } +[features] +# Tests only, never enable in a product: Settings ▸ General ▸ Language ▸ Match System resolves to +# English, so English label lookups pass on any system language (#3244). The self dev-dependency +# below turns it on wherever dev-dependencies are built: this crate's integration tests, its +# examples (`snapshot` shots are deterministic English unless a step sets a language) and +# workspace test builds of the apps (`cargo test --workspace`). +test-support = [] + [dev-dependencies] +# This crate's own integration tests build it with `test-support` (see `[features]`). +effectcraft-ui-egui = { workspace = true, features = ["test-support"] } # Fonts for kittest: the workspace `egui` features are what test roots resolve, but stating them # here documents the requirement (kittest's own dev-dependencies do not propagate to consumers). egui = { workspace = true, features = ["default_fonts", "monochrome_emoji_fonts"] } diff --git a/crates/ui-egui/src/i18n.rs b/crates/ui-egui/src/i18n.rs index 68e58f842..8ab99ead7 100644 --- a/crates/ui-egui/src/i18n.rs +++ b/crates/ui-egui/src/i18n.rs @@ -84,7 +84,14 @@ pub(crate) fn swedish(app: &EffectcraftApp) -> bool { /// Settings ▸ General ▸ Language ▸ Match System: the operating system's interface language /// where EffectCraft has it, else English (#229), as After Effects installs in the system's /// language. The browser build follows the locales its page passes in ([`set_system_locales`]). +/// +/// Test builds (this crate's unit tests, and its integration tests through the `test-support` +/// feature) resolve Match System to English, so tests that look up English labels pass on any +/// system language (#3244); tests about a translation choose their language explicitly. fn system_language() -> &'static str { + if cfg!(any(test, feature = "test-support")) { + return "en"; + } #[cfg(not(target_arch = "wasm32"))] { static LANGUAGE: std::sync::OnceLock<&'static str> = std::sync::OnceLock::new(); @@ -9169,13 +9176,22 @@ mod tests { } let mut app = EffectcraftApp::new(effectcraft_engine::Session::default()); assert_eq!(app.session.prefs.general.language, "system", "the default"); - assert_eq!(japanese(&app), system_language() == "ja"); - assert_eq!(simplified(&app), system_language() == "zh-hans"); - assert_eq!(traditional(&app), system_language() == "zh-hant"); - assert_eq!(spanish(&app), system_language() == "es"); - assert_eq!(czech(&app), system_language() == "cs"); - assert_eq!(polish(&app), system_language() == "pl"); - assert_eq!(swedish(&app), system_language() == "sv"); + assert_eq!(system_language(), "en", "test builds pin Match System to English (#3244)"); + assert_eq!(language(&app), "en"); + for (flag, name) in [ + (japanese(&app), "ja"), + (simplified(&app), "zh-hans"), + (traditional(&app), "zh-hant"), + (ukrainian(&app), "uk"), + (spanish(&app), "es"), + (brazilian_portuguese(&app), "pt-br"), + (czech(&app), "cs"), + (polish(&app), "pl"), + (georgian(&app), "ka"), + (swedish(&app), "sv"), + ] { + assert!(!flag, "Match System shows {name} in a test build"); + } app.session.execute("prefs.set", json!({"key":"general.language", "value":"ja"})).unwrap(); assert!(japanese(&app)); app.session.execute("prefs.set", json!({"key":"general.language", "value":"zh-hans"})).unwrap(); From 7dabd6e6f16265218fc07248c5c0ef217bf5610e Mon Sep 17 00:00:00 2001 From: vangel76 Date: Sat, 7 Nov 2026 16:24:57 +0100 Subject: [PATCH 18/74] Effect Controls: drop a replaced project's lock before answering a lock request (#3193) A ui.set {effectControlsLocked: true} that ran after the project was replaced but before the next frame reported the old project's lock as success; the next frame then cleared it. set_lock now scopes the lock first. --- .../src/panels/effect_controls/common.rs | 3 +++ crates/ui-egui/tests/it/ui_effect_controls.rs | 26 +++++++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/crates/ui-egui/src/panels/effect_controls/common.rs b/crates/ui-egui/src/panels/effect_controls/common.rs index 49bb514dd..1e7d7cba1 100644 --- a/crates/ui-egui/src/panels/effect_controls/common.rs +++ b/crates/ui-egui/src/panels/effect_controls/common.rs @@ -49,6 +49,9 @@ pub(super) fn shown_layer(app: &EffectcraftApp) -> Option { /// does nothing; locking while already locked keeps the layer it is locked to. Returns whether /// the panel is locked afterwards. The tab's lock icon and `ui.set {effectControlsLocked}`. pub fn set_lock(app: &mut EffectcraftApp, on: bool) -> bool { + // A lock left from a replaced project is gone before this one is answered (a control + // request can run between the replacement and the next frame). + scope_lock(app); if !on { app.ui.effect_controls_lock = None; return false; diff --git a/crates/ui-egui/tests/it/ui_effect_controls.rs b/crates/ui-egui/tests/it/ui_effect_controls.rs index d72febf01..ce51a2b2f 100644 --- a/crates/ui-egui/tests/it/ui_effect_controls.rs +++ b/crates/ui-egui/tests/it/ui_effect_controls.rs @@ -1013,6 +1013,32 @@ fn replacing_the_project_unlocks_effect_controls() { assert_eq!(h.state().ui.effect_controls_lock, None, "the lock belongs to the replaced project"); } +/// #3193: a lock request right after the project is replaced, before the next frame, is not +/// answered with the old project's lock: the reply matches the lock the panel keeps. +#[test] +fn a_lock_request_after_a_project_replacement_does_not_report_the_old_lock() { + use effectcraft_ui_egui::control::{self, ControlRequest, Outcome}; + fn send(h: &mut Harness<'_, EffectcraftApp>) -> serde_json::Value { + let ctx = h.ctx.clone(); + let (req, _rx) = ControlRequest::new("ui.set", json!({"effectControlsLocked": true})); + let now = h.state().now(); + match control::handle(h.state_mut(), &ctx, &req, now) { + Outcome::Done(v) => v, + _ => panic!("ui.set: no immediate reply"), + } + } + let (app, a, _, _, _) = two_layers(); + let mut h = Harness::builder().with_size(egui::vec2(1600.0, 1000.0)).build_eframe(|_| app); + h.run_steps(4); + assert_eq!(send(&mut h)["ok"], true); + assert_eq!(h.state().ui.effect_controls_lock.map(|l| l.layer), Some(a)); + let project = (*h.state().session.project).clone(); + h.state_mut().session.replace_project(project, None); + let reply = send(&mut h); + h.run_steps(3); + assert_eq!(reply["ok"] == true, h.state().ui.effect_controls_lock.is_some(), "{reply}"); +} + /// #3193: an effect dropped on the locked panel goes to the locked layer and leaves the selection /// alone; one applied to another layer says why the panel doesn't show it. #[test] From b2d6690fd202c6a4a053839f946fc2e0fdb939c0 Mon Sep 17 00:00:00 2001 From: vangel76 Date: Sat, 7 Nov 2026 16:27:36 +0100 Subject: [PATCH 19/74] Number fields: Mask Feather stays non-negative on the other selected layers (#3195) A typed *-1 (the edited layer clamped at 0 by the panel) or a change by the same amount could take another selected layer's Mask Feather below 0. Clamp it at 0 there, as the panels do for the edited layer. --- crates/engine/src/cmd/prop.rs | 23 +++++++++++++++++++++++ crates/engine/src/tests/timeline.rs | 24 ++++++++++++++++++++++++ 2 files changed, 47 insertions(+) diff --git a/crates/engine/src/cmd/prop.rs b/crates/engine/src/cmd/prop.rs index dc043d78f..ae37ea1b7 100644 --- a/crates/engine/src/cmd/prop.rs +++ b/crates/engine/src/cmd/prop.rs @@ -436,6 +436,9 @@ fn set_selected(s: &mut Session, p: &Value) -> Result { let mut q = twin_params(p, olid, ouid); shifted(s, &mut q, olid, ouid, k); typed(s, &mut q, olid, ouid); + if is_mask_feather(s, cid, olid, ouid) { + non_negative(&mut q["value"]); + } // (a layer that can't take it, e.g. a clamped or static property, keeps its value) let _ = set(s, &q); } @@ -444,6 +447,26 @@ fn set_selected(s: &mut Session, p: &Value) -> Result { if merge_p(p).is_some() { run(s) } else { super::app_more::grouped(s, "Change Property", run) } } +/// Whether property `uid` of `lid` is a mask's Mask Feather, which is never negative (After +/// Effects; the panels clamp a typed or scrubbed value at 0). +fn is_mask_feather(s: &Session, cid: ItemId, lid: LayerId, uid: Uid) -> bool { + let Some(l) = s.project.comp(cid).and_then(|c| c.layer(lid)) else { return false }; + l.props.find(uid).is_some_and(|q| q.match_id == "feather") && l.props.parent_of(uid).is_some_and(|g| matches!(g.kind, GroupKind::Mask { .. })) +} + +/// A JSON number, or each number of an array, raised to at least 0. +fn non_negative(v: &mut Value) { + match v { + Value::Number(n) => { + if let Some(x) = n.as_f64().filter(|x| *x < 0.0) { + *v = json!(x.max(0.0)); + } + } + Value::Array(a) => a.iter_mut().for_each(non_negative), + _ => {} + } +} + /// `prop.toggleAnimation`; with `selected`, the other selected layers' same property follows. fn toggle_anim_selected(s: &mut Session, p: &Value) -> Result { let twins = selected_twins(s, p, "prop.toggleAnimation")?; diff --git a/crates/engine/src/tests/timeline.rs b/crates/engine/src/tests/timeline.rs index 6ea4d1336..43eb922e2 100644 --- a/crates/engine/src/tests/timeline.rs +++ b/crates/engine/src/tests/timeline.rs @@ -1554,6 +1554,30 @@ fn a_typed_factor_scales_each_selected_layers_own_value() { assert_eq!(prop(&s, b, "transform/position").value.components()[..2], [50.0, 50.0], "B halves its own X, keeps its Y"); } +/// #3195: Mask Feather is never negative on the other selected layers either, when a typed `*-1` +/// (the edited layer clamped at 0 by the panel) or a change by the same amount takes theirs +/// below 0. +#[test] +fn a_selected_mask_feather_stays_non_negative() { + let (mut s, a) = setup(); + let b = s.execute("layer.newSolid", json!({"color": "#ff0000", "width": 50, "height": 50})).unwrap()["layer"].as_u64().unwrap(); + for (l, feather) in [(a, [10.0, 20.0]), (b, [4.0, 30.0])] { + s.execute("mask.new", json!({"layer": l, "vertices": [[0, 0], [20, 0], [20, 20]]})).unwrap(); + s.execute("prop.set", json!({"layer": l, "path": "masks/#1/feather", "value": feather})).unwrap(); + } + s.execute("layer.select", json!({"layers": [a, b]})).unwrap(); + let feather = |s: &Session, l: u64| prop(s, l, "masks/#1/feather").value.components(); + let uid = prop(&s, a, "masks/#1/feather").uid; + // `*-1` into the linked pair: the panel clamps the edited layer at [0, 0]. + s.execute_checked("prop.set", json!({"layer": a, "prop": uid, "value": [0.0, 0.0], "selected": true, "dims": [0, 1], "factor": -1.0})).unwrap(); + assert_eq!(feather(&s, a), [0.0, 0.0]); + assert_eq!(feather(&s, b), [0.0, 0.0], "B is clamped at 0, not [-4, -30]"); + // A change by the same amount (a scrub, or a typed `+`): A by [-10, -15] from [10, 20]. + s.execute("edit.undo", json!({})).unwrap(); + s.execute_checked("prop.set", json!({"layer": a, "prop": uid, "value": [0.0, 5.0], "selected": true, "offset": true})).unwrap(); + assert_eq!(feather(&s, b), [0.0, 15.0], "B's X is clamped at 0, its Y moves by -15"); +} + /// #2485: linked Audio Levels typed with several layers selected: each takes the typed Left, and /// its Right moves by its own change, so every layer keeps its own stereo balance. #[test] From 998448f94be3474e164d4feb0fcc2cdd97a4a2d1 Mon Sep 17 00:00:00 2001 From: vangel76 Date: Sat, 7 Nov 2026 16:41:19 +0100 Subject: [PATCH 20/74] Audio: a time-reversed layer plays its audio backwards (#3239) A layer with a negative time stretch was skipped by the mix. It now plays its audio reversed at its stretched speed, through the same linear resampler as forward stretch: a reversed read starts at the layer time of the run's end and is played last to first, so comp sample k plays the source just before layer_time(k) (at -100 % with the start at the out point, the exact mirror Backwards gives a forward layer). Backwards on a reversed layer plays it forwards. Resampled runs start at the exact source position (from the layer time in ticks) instead of the grid sample a block starts on, so mixing in frame blocks and in export's one-second blocks gives the same samples, and they read one more source sample so a block's last sample blends instead of holding. Audio effects run on comp time since the layer's start, which joins up across blocks at any stretch and equals the layer time at 100 %. Reads are made in pieces of at most MAX_READ source frames, so memory stays bounded for fast layers and nested fast precomps (the work for nested 1 % precomps is not bounded; follow-up). A stretch faster than 1 % is silent as a last-resort guard, with a tolerance so exactly 1 % plays. A zero or non-finite stretch is skipped. Fixes #3239 --- crates/effects/src/audio_fx.rs | 3 +- crates/render/src/audio.rs | 131 ++++++++++++++--- crates/render/src/tests/audio_fx.rs | 218 +++++++++++++++++++++++++++- 3 files changed, 329 insertions(+), 23 deletions(-) diff --git a/crates/effects/src/audio_fx.rs b/crates/effects/src/audio_fx.rs index c4cb50de2..d8c0a98f3 100644 --- a/crates/effects/src/audio_fx.rs +++ b/crates/effects/src/audio_fx.rs @@ -244,7 +244,8 @@ pub fn preroll(id: &str, params: &Params) -> f64 { } /// Run audio effect `id` over interleaved stereo `buf` (L R L R …) at `rate` Hz, from a cleared -/// state. `start` is the layer time (seconds) of the first sample (oscillator phase). +/// state. `start` is the time (seconds) of the first sample since the layer's start, counted at +/// comp speed whatever the layer's time stretch (oscillator phase). pub fn process(id: &str, params: &Params, buf: &mut [f32], rate: u32, start: f64) { if rate == 0 || buf.len() < 2 { return; diff --git a/crates/render/src/audio.rs b/crates/render/src/audio.rs index e2861340c..b41307b4e 100644 --- a/crates/render/src/audio.rs +++ b/crates/render/src/audio.rs @@ -12,8 +12,11 @@ //! pre-roll of earlier audio and only the block's tail is kept — so preview playback and export //! produce identical samples. //! -//! Time-stretched layers are resampled nearest-sample; reversed (negative stretch) layers are -//! silent for now. +//! Time-stretched layers are resampled linearly, so pitch follows the speed. A reversed layer +//! (negative stretch, Layer > Time > Time-Reverse Layer) plays its audio backwards at the same +//! speed: comp sample `k` plays the source just before layer time `layer_time(k)`, so at -100 % +//! with the start at the out point (After Effects' layout) it is the exact mirror of the +//! layer's span. Backwards on a reversed layer plays it forwards again. use effectcraft_effects::Params; use effectcraft_effects::audio_fx; @@ -24,6 +27,18 @@ use crate::{EvalCtx, ExprHost, FootageSource}; const MAX_DEPTH: usize = 16; +/// Fastest a layer's audio plays (source samples per comp sample): 1 % stretch, the smallest +/// Time Stretch accepts. A faster layer (a stretch below 1 %, only from a damaged or foreign +/// file) is silent instead of reading unbounded source: a last-resort guard, imports clamp +/// such stretches. +const MAX_SPEED: f64 = 100.0; + +/// Most source sample frames one read asks a footage item or precomp for; a longer read is made +/// in pieces, so memory stays bounded however fast a layer plays and however deep stretched +/// precomps nest. The work is not bounded: each 1 % precomp level still reads 100 times its +/// span. +pub(crate) const MAX_READ: usize = 1 << 18; + /// `frames` stereo sample frames of `comp`'s mix from comp time `start` at `rate` Hz, /// interleaved (L R L R …). pub fn mix_comp(project: &Project, footage: &dyn FootageSource, expr: Option<&dyn ExprHost>, comp: ItemId, start: Tick, frames: usize, rate: u32) -> Vec { @@ -102,6 +117,13 @@ fn animated(g: &effectcraft_project::PropGroup) -> bool { }) } +/// The clock (seconds) a layer's audio effects run on at comp time `t` (oscillator phase): comp +/// time since the layer's start. It advances one sample per sample at any stretch, forwards or +/// reversed, so blocks join up; at 100 % it is the layer time. +fn fx_clock(l: &Layer, t: Tick) -> f64 { + Tick(t.0.saturating_sub(l.start_time.0)).seconds() +} + fn sample_tick(i: i64, rate: u32) -> Tick { // Keep the requested sample when the rate does not divide the tick clock. let t = Tick::from_units(i, i64::from(rate)); @@ -127,7 +149,7 @@ impl MixEnv<'_> { /// The layer's own (pre-effect) audio for comp sample indices `i0 .. i0 + n`, silent /// outside its [in, out) span. Comp sample index `k` maps to `in + out − 1 − k` when - /// `reversed`. + /// `reversed` (the Backwards effect). fn layer_raw(&self, l: &Layer, i0: i64, n: usize, reversed: bool) -> Vec { let rate = self.rate; let mut out = vec![0.0f32; n * 2]; @@ -154,40 +176,84 @@ impl MixEnv<'_> { if a >= b { return out; } - let m = b - a; let speed = 100.0 / l.stretch; - let src_n = ((m as f64 * speed).ceil() as usize).max(1); - let lt = l.layer_time(sample_tick(i0 + a as i64, rate)); + let aspeed = speed.abs(); + // A zero or non-finite stretch never gets here (see `mix_into`); NaN is checked anyway. + // The tolerance keeps exactly 1 % audible whatever the float noise in the stretch. + if aspeed.is_nan() || aspeed > MAX_SPEED * (1.0 + 1e-9) { + return out; + } + let backwards = speed < 0.0; + // Comp samples per piece, so that one piece reads at most MAX_READ source frames (its + // span, the fraction it starts past a source sample, and the sample after its end). + let piece = (((MAX_READ - 4) as f64 / aspeed) as usize).clamp(1, b - a); + let mut p0 = a; + while p0 < b { + let p1 = p0.saturating_add(piece).min(b); + // Forwards a piece reads the source from the layer time of its first sample. Backwards + // it reads from the layer time of its end and plays what it read last to first, so + // comp sample `k` plays the source just before `layer_time(k)` — at -100 % the exact + // mirror Backwards gives a forward layer. + let origin = sample_tick(i0.saturating_add((if backwards { p1 } else { p0 }) as i64), rate); + let Some(run) = self.source_run(l, l.layer_time(origin), p1 - p0, aspeed) else { return out }; + for (i, f) in run.as_chunks::<2>().0.iter().take(p1 - p0).enumerate() { + let k = if backwards { (p1 - 1).saturating_sub(i) } else { p0 + i }; + if let Some(o) = out.get_mut(k * 2..k * 2 + 2) { + o.copy_from_slice(f); + } + } + p0 = p1; + } + out + } + + /// `m` sample frames of the layer's source audio from layer time `lt` on, played at `speed` + /// (≥ 0) source samples per comp sample: blended linearly between source samples, or copied + /// as they are at speed 1. `None` when the layer has no source audio. + /// + /// Sources deliver samples on their sample grid (from the sample at or before the time asked + /// for), so a resampled run reads from that grid sample and starts `x` at `lt`'s exact + /// position past it: a run then continues where the previous block or piece left off. + fn source_run(&self, l: &Layer, lt: Tick, m: usize, speed: f64) -> Option> { + let rate = self.rate; + let same = (speed - 1.0).abs() < 1e-9; + let s0 = lt.to_units_floor(i64::from(rate)); + let from = sample_tick(s0, rate); + // `lt`'s position past sample s0, in samples (0 ≤ frac < 1); copies ignore it. + let tps = TICKS_PER_SECOND as i128; + let frac = if same { 0.0 } else { (i128::from(lt.0) * i128::from(rate)).rem_euclid(tps) as f64 / tps as f64 }; + // Resampled, one more source sample than the run covers: the last sample blends with it. + let src_n = ((frac + m as f64 * speed).ceil() as usize).saturating_add(usize::from(!same)).max(1); let buf = match &l.source { LayerSource::Footage { item } => match self.project.item(*item).map(|i| &i.kind) { - Some(ItemKind::Footage(f)) if f.has_audio => self.footage.audio(*item, f, lt, src_n, rate).unwrap_or_else(|| vec![0.0; src_n * 2]), - _ => return out, + Some(ItemKind::Footage(f)) if f.has_audio => self.footage.audio(*item, f, from, src_n, rate).unwrap_or_else(|| vec![0.0; src_n * 2]), + _ => return None, }, LayerSource::Comp { item } => { if self.project.comp_contains(*item, self.comp_id) { - return out; + return None; } let mut sub = vec![0.0f32; src_n * 2]; - mix_into(&AudioSources { project: self.project, footage: self.footage, expr: self.expr, rate }, *item, lt, &mut sub, self.depth + 1); + mix_into(&AudioSources { project: self.project, footage: self.footage, expr: self.expr, rate }, *item, from, &mut sub, self.depth + 1); sub } - _ => return out, + _ => return None, }; - let same = (speed - 1.0).abs() < 1e-9; + let mut out = vec![0.0f32; m * 2]; for i in 0..m { // The source sample at fractional index `x`, blended linearly with the next one unless the // layer plays at its own speed (then the samples are copied as they are). - let x = i as f64 * speed; + let x = frac + i as f64 * speed; let j = if same { i } else { (x as usize).min(src_n - 1) }; if j * 2 + 1 >= buf.len() { break; } let k = if same || (j + 1) * 2 + 1 >= buf.len() { j } else { j + 1 }; let f = if same { 0.0 } else { ((x - j as f64) as f32).clamp(0.0, 1.0) }; - out[(a + i) * 2] = buf[j * 2] + (buf[k * 2] - buf[j * 2]) * f; - out[(a + i) * 2 + 1] = buf[j * 2 + 1] + (buf[k * 2 + 1] - buf[j * 2 + 1]) * f; + out[i * 2] = buf[j * 2] + (buf[k * 2] - buf[j * 2]) * f; + out[i * 2 + 1] = buf[j * 2 + 1] + (buf[k * 2 + 1] - buf[j * 2 + 1]) * f; } - out + Some(out) } /// Effect parameters at comp time `t`. @@ -215,7 +281,7 @@ impl MixEnv<'_> { let pre = (pre_s * self.rate as f64).ceil() as usize; let b0 = i0 - pre as i64; let mut buf = self.layer_raw(l, b0, n + pre, reversed); - let start = l.layer_time(sample_tick(b0, self.rate)).seconds(); + let start = fx_clock(l, sample_tick(b0, self.rate)); for (id, g, p) in &fx { if audio_fx::per_sample(id) && animated(g) { self.process_stepped(l, id, g, &mut buf, b0); @@ -239,7 +305,7 @@ impl MixEnv<'_> { let end = usize::try_from(next.saturating_sub(b0)).unwrap_or(frames).clamp(k + 1, frames); let t = sample_tick(i, self.rate); if let Some(piece) = buf.get_mut(k * 2..end * 2) { - audio_fx::process(id, &self.params(l, g, t), piece, self.rate, l.layer_time(t).seconds()); + audio_fx::process(id, &self.params(l, g, t), piece, self.rate, fx_clock(l, t)); } k = end; } @@ -291,7 +357,8 @@ fn mix_into(sources: &AudioSources<'_>, comp_id: ItemId, start: Tick, out: &mut let any_solo = comp.layers.iter().any(|l| l.switches.solo && audible(project, l)); let s0 = start.to_units_floor(rate as i64); for l in &comp.layers { - if !audible(project, l) || (any_solo && !l.switches.solo) || l.stretch <= 0.0 { + // A zero or non-finite stretch has no layer time to play; a negative one plays backwards. + if !audible(project, l) || (any_solo && !l.switches.solo) || l.stretch == 0.0 || !l.stretch.is_finite() { continue; } // Sample range of the block that falls inside the layer's [in, out). @@ -482,6 +549,11 @@ mod tests { let base = Comp::new(64, 64, FrameRate::new(25, 1), Tick::from_seconds_f64(2.0)); let mut tone = build::layer(&mut p, &base, "tone", LayerSource::Footage { item: fid }, (0, 0), None); tone.stretch = stretch; + // A reversed layer as After Effects lays it out: over [0, 2) s, starting at its out point. + let end = if stretch < 0.0 { 2.0 } else { 0.0 }; + if stretch < 0.0 { + (tone.in_point, tone.out_point, tone.start_time) = (Tick::ZERO, Tick::from_seconds_f64(2.0), Tick::from_seconds_f64(2.0)); + } let mut comp = base.clone(); comp.layers = vec![tone].into(); let cid = p.add_item("C", effectcraft_color::Label::Sandstone, None, ItemKind::Comp(comp.into())); @@ -491,8 +563,11 @@ mod tests { let speed = 100.0 / stretch; let (mut worst, mut sq) = (0.0f64, 0.0f64); for i in 0..n { - // The source plays `speed` times as fast as the comp: source time = speed * comp time. - let ideal = (std::f64::consts::TAU * 440.0 * speed * (t.seconds() + i as f64 / rate as f64)).sin() * 0.5; + // The source plays `speed` times as fast as the comp: source time = speed * (comp time − + // start). Reversed, a comp sample plays the source sample just before that time. + let back = if stretch < 0.0 { speed / rate as f64 } else { 0.0 }; + let src = speed * (t.seconds() + i as f64 / rate as f64 - end) + back; + let ideal = (std::f64::consts::TAU * 440.0 * src).sin() * 0.5; let e = (m[i * 2] as f64 - ideal).abs(); worst = worst.max(e); sq += e * e; @@ -511,4 +586,18 @@ mod tests { assert!(worst < 1e-2, "stretch {stretch}%: error {worst} at 8 kHz"); } } + + /// A reversed layer plays its tone backwards with the same resampling quality as forwards + /// (it was silent, #3239); at -100 % the samples are copied exactly. + #[test] + fn reversed_layers_resample_a_tone_with_small_error() { + for stretch in [-70.0, -33.0] { + let (worst, _) = stretch_error(stretch, 48_000); + assert!(worst < 1e-3, "stretch {stretch}%: error {worst} at 48 kHz"); + let (worst, _) = stretch_error(stretch, 8_000); + assert!(worst < 1e-2, "stretch {stretch}%: error {worst} at 8 kHz"); + } + let (worst, _) = stretch_error(-100.0, 48_000); + assert!(worst < 1e-5, "stretch -100%: error {worst}"); + } } diff --git a/crates/render/src/tests/audio_fx.rs b/crates/render/src/tests/audio_fx.rs index a6c85f2de..5ef2949e5 100644 --- a/crates/render/src/tests/audio_fx.rs +++ b/crates/render/src/tests/audio_fx.rs @@ -1,13 +1,14 @@ //! Audio effects through the mixdown (the path preview playback and export share). use std::sync::Arc; +use std::sync::atomic::{AtomicUsize, Ordering}; use effectcraft_keyframe::{Keyframe, Value}; use effectcraft_project::build::{self, Ids}; use effectcraft_project::{Comp, Footage, FootageKind, ItemId, ItemKind, LayerSource, Project, Solid}; use effectcraft_time::{FrameRate, Tick}; -use crate::audio::{comp_has_audio, footage_peaks, mix_comp, peak_summary}; +use crate::audio::{MAX_READ, comp_has_audio, footage_peaks, mix_comp, peak_summary}; use crate::{FootageSource, Image}; const SR: u32 = 8000; @@ -109,6 +110,221 @@ fn backwards_reverses_the_layer_span() { assert!((m[4000 * 2] - 0.5).abs() < 1e-3); } +/// `comp_with`, its layer reversed at `stretch` (< 0) the way After Effects lays it out: in 0, +/// out `out` s, starting at the out point. +fn reversed(id: Option<&str>, vals: &[(&str, Value)], solid: bool, stretch: f64, out: f64) -> (Project, ItemId) { + let (mut p, cid) = comp_with(id, vals, solid); + let l = &mut p.comp_mut(cid).unwrap().layers[0]; + l.stretch = stretch; + (l.in_point, l.out_point, l.start_time) = (Tick::ZERO, Tick::from_seconds_f64(out), Tick::from_seconds_f64(out)); + (p, cid) +} + +/// A time-reversed layer plays its audio backwards instead of being silent (#3239): at -100 % +/// sample for sample what Backwards plays on the forward layer, in any block size. +#[test] +fn a_reversed_layer_plays_its_audio_backwards() { + let n = SR as usize; + let (p, cid) = reversed(None, &[], false, -100.0, 1.0); + let m = mix(&p, &Signal(ramp), cid, 0.0, n); + // Comp sample k plays source sample n − 1 − k. + for k in [0usize, 1, 2000, 4000, 7000] { + let want = (n - 1 - k) as f32 / SR as f32; + assert!((m[k * 2] - want).abs() < 1e-6 && (m[k * 2 + 1] + want).abs() < 1e-6, "k={k}: {} {}, want {want}", m[k * 2], m[k * 2 + 1]); + } + let (bp, bcid) = comp_with(Some("ec.audio.backwards"), &[], false); + assert_eq!(m, mix(&bp, &Signal(ramp), bcid, 0.0, n), "not the mirror Backwards plays"); + let mut blocks = vec![]; + for k in 0..n / 320 { + blocks.extend(mix(&p, &Signal(ramp), cid, (k * 320) as f64 / SR as f64, 320)); + } + let worst = m.iter().zip(&blocks).map(|(a, b)| (a - b).abs()).fold(0.0f32, f32::max); + assert!(worst < 1e-6, "block size changes the result by {worst}"); +} + +/// The speed of a reversed layer follows its stretch as a forward one's does: -50 % plays the +/// source twice as fast, -200 % at half speed, both backwards. +#[test] +fn a_reversed_layer_plays_at_its_stretch() { + // -50 % over 0.5 s: comp sample k plays source sample 2 (n − 1 − k), n = 4000. + let (p, cid) = reversed(None, &[], false, -50.0, 0.5); + let m = mix(&p, &Signal(ramp), cid, 0.0, 4000); + for k in [0usize, 1000, 3000] { + let want = 2.0 * (3999 - k) as f32 / SR as f32; + assert!((m[k * 2] - want).abs() < 1e-6, "-50 %, k={k}: {}, want {want}", m[k * 2]); + } + // -200 % over 2 s: comp sample k plays source sample (n − 1 − k) / 2, n = 16000, blended. + let (p, cid) = reversed(None, &[], false, -200.0, 2.0); + let m = mix(&p, &Signal(ramp), cid, 0.0, 16_000); + for k in [0usize, 1, 5001, 12_000] { + let want = (15_999 - k) as f32 / 2.0 / SR as f32; + assert!((m[k * 2] - want).abs() < 1e-5, "-200 %, k={k}: {}, want {want}", m[k * 2]); + } +} + +/// Backwards on a reversed layer plays it forwards: the two reversals cancel. +#[test] +fn backwards_on_a_reversed_layer_plays_forwards() { + let (p, cid) = reversed(Some("ec.audio.backwards"), &[], false, -100.0, 1.0); + let (fp, fcid) = comp_with(None, &[], false); + for (from, frames) in [(0.0, SR as usize), (0.25, 2000)] { + let m = mix(&p, &Signal(ramp), cid, from, frames); + assert!(m.iter().any(|v| v.abs() > 0.1), "silent from {from} s"); + assert_eq!(m, mix(&fp, &Signal(ramp), fcid, from, frames), "from {from} s"); + } +} + +/// Effects on a reversed layer run on a clock that moves forwards with the samples at comp speed, +/// so a Tone joins up across blocks (preview's frame blocks and export's one-second blocks +/// agree), at -50 % too. On the layer-time clock, block k at -100 % would start 0.08 k s off: +/// 35.2 k cycles at 440 Hz, a phase jump. +#[test] +fn tone_on_a_reversed_layer_joins_up_across_blocks() { + let mut vals: Vec<(&str, Value)> = vec![("frequency1", Value::Scalar(440.0))]; + for k in ["frequency2", "frequency3", "frequency4", "frequency5"] { + vals.push((k, Value::Scalar(0.0))); + } + for stretch in [-100.0, -50.0] { + let (p, cid) = reversed(Some("ec.audio.tone"), &vals, true, stretch, 1.0); + let long = mix(&p, &Signal(ramp), cid, 0.2, 1600); + assert!(rms(&long) > 0.05, "{stretch} %: silent: {}", rms(&long)); + let mut blocks = vec![]; + for k in 0..5 { + blocks.extend(mix(&p, &Signal(ramp), cid, 0.2 + k as f64 * 0.04, 320)); + } + let worst = long.iter().zip(&blocks).map(|(a, b)| (a - b).abs()).fold(0.0f32, f32::max); + assert!(worst < 1e-3, "{stretch} %: block size changes the tone by {worst}"); + } +} + +/// Footage audio on its sample grid, as the media pool decodes it: a read from any time starts +/// at the sample at or before it. Sample n is `f(n / rate)` over [0, 2) s. +struct Grid(fn(f64) -> (f32, f32)); +impl FootageSource for Grid { + fn frame(&self, _: ItemId, _: &Footage, _: Tick) -> Option> { + None + } + fn audio(&self, _: ItemId, _: &Footage, start: Tick, frames: usize, rate: u32) -> Option> { + let s0 = start.to_units_floor(i64::from(rate)); + Some( + (0..frames as i64) + .flat_map(|i| { + let t = (s0 + i) as f64 / rate as f64; + let (l, r) = if (0.0..2.0).contains(&t) { (self.0)(t) } else { (0.0, 0.0) }; + [l, r] + }) + .collect(), + ) + } +} + +/// A stretched layer, forwards or reversed, gives the same samples mixed in one block as in +/// blocks of any size (preview's video-frame blocks, export's seconds): each block resamples +/// from the exact source position, not from the source sample a block happens to start on. +#[test] +fn stretched_layers_join_up_across_blocks() { + // (stretch, out point, start): the reversed layers start at their out point, the -50 % one + // off the sample grid (a third of a sample late), so its source positions are fractional. + let third = Tick(Tick::from_seconds_f64(1.0 / SR as f64).0 / 3); + for (stretch, out, start) in [ + (200.0, 2.0, Tick::ZERO), + (-70.0, 0.7, Tick::from_seconds_f64(0.7)), + (-50.0, 0.5, Tick(Tick::from_seconds_f64(0.5).0 + third.0)), + (-200.0, 2.0, Tick::from_seconds_f64(2.0)), + ] { + let (mut p, cid) = comp_with(None, &[], false); + let l = &mut p.comp_mut(cid).unwrap().layers[0]; + l.stretch = stretch; + (l.in_point, l.out_point, l.start_time) = (Tick::ZERO, Tick::from_seconds_f64(out), start); + let n = 3210; + let long = mix(&p, &Grid(ramp), cid, 0.1, n); + assert!(long.iter().any(|v| v.abs() > 0.05), "{stretch} %: silent"); + for block in [321, 107] { + let mut blocks = vec![]; + for k in 0..n / block { + blocks.extend(mix(&p, &Grid(ramp), cid, 0.1 + (k * block) as f64 / SR as f64, block)); + } + let worst = long.iter().zip(&blocks).map(|(a, b)| (a - b).abs()).fold(0.0f32, f32::max); + assert!(worst < 1e-6, "{stretch} %, blocks of {block}: off by {worst}"); + } + } +} + +/// At 200 % the last sample of a block blends with the source sample after it, as inside the +/// block, instead of holding the one before. +#[test] +fn a_stretched_block_ends_on_a_blended_sample() { + let (mut p, cid) = comp_with(None, &[], false); + let l = &mut p.comp_mut(cid).unwrap().layers[0]; + l.stretch = 200.0; + (l.in_point, l.out_point, l.start_time) = (Tick::ZERO, Tick::from_seconds_f64(2.0), Tick::ZERO); + let m = mix(&p, &Grid(ramp), cid, 0.0, 320); + // Comp sample 319 plays source position 159.5. + let want = 159.5 / SR as f32; + assert!((m[319 * 2] - want).abs() < 1e-6, "{}, want {want}", m[319 * 2]); +} + +/// Records the longest audio read and answers with a short run, so an unbounded request fails +/// the assert below instead of allocating. +struct Longest(AtomicUsize); +impl FootageSource for Longest { + fn frame(&self, _: ItemId, _: &Footage, _: Tick) -> Option> { + None + } + fn audio(&self, _: ItemId, _: &Footage, _: Tick, frames: usize, _: u32) -> Option> { + self.0.fetch_max(frames, Ordering::Relaxed); + Some(vec![0.25; frames.min(4096) * 2]) + } +} + +/// Tiny, huge, zero and non-finite stretches, and fast precomps nested in fast precomps, read +/// at most MAX_READ source frames at a time and mix to finite samples (a tiny positive stretch +/// asked for 10⁸ times the block, and its precomp buffer could abort the process). +#[test] +fn extreme_stretches_read_bounded_source() { + let stretches = [1e-6, -1e-6, 0.5, -0.5, 1.0, -1.0, 1e12, -1e12, f64::MIN_POSITIVE, 0.0, f64::NAN, f64::INFINITY, f64::NEG_INFINITY]; + for stretch in stretches { + let (mut p, cid) = comp_with(None, &[], false); + p.comp_mut(cid).unwrap().layers[0].stretch = stretch; + let src = Longest(AtomicUsize::new(0)); + let m = mix(&p, &src, cid, 0.0, 800); + let longest = src.0.load(Ordering::Relaxed); + assert!(longest <= MAX_READ, "stretch {stretch}: read {longest} frames at once"); + assert!(m.iter().all(|v| v.is_finite()), "stretch {stretch}"); + } + // Exactly 1 %, give or take float noise, stays audible. + let (mut p, cid) = comp_with(None, &[], false); + p.comp_mut(cid).unwrap().layers[0].stretch = 1.0 - 1e-12; + let m = mix(&p, &Longest(AtomicUsize::new(0)), cid, 0.0, 800); + assert!(m.iter().any(|v| *v != 0.0), "1 % is silent"); + // A 1 % footage layer in a 1 % precomp: 10⁴ source samples per comp sample. The precomp layer + // lasts 0.02 s, which plays the inner layer's 2 s once (backwards from its out point at -1 %). + for sign in [1.0, -1.0] { + let mut p = Project::default(); + let base = Comp::new(64, 64, FrameRate::new(25, 1), Tick::from_seconds_f64(2.0)); + let fid = p.add_item("x.wav", effectcraft_color::Label::SeaFoam, None, ItemKind::Footage(audio_footage())); + let mut inner_l = build::layer(&mut p, &base, "a", LayerSource::Footage { item: fid }, (64, 64), None); + inner_l.stretch = sign; + (inner_l.in_point, inner_l.out_point, inner_l.start_time) = + (Tick::ZERO, Tick::from_seconds_f64(2.0), if sign < 0.0 { Tick::from_seconds_f64(2.0) } else { Tick::ZERO }); + let mut inner = base.clone(); + inner.layers = vec![inner_l].into(); + let inner_id = p.add_item("Inner", effectcraft_color::Label::Sandstone, None, ItemKind::Comp(inner.into())); + let mut pre = build::layer(&mut p, &base, "pre", LayerSource::Comp { item: inner_id }, (64, 64), None); + pre.stretch = sign; + let pre_out = Tick::from_seconds_f64(0.02); + (pre.in_point, pre.out_point, pre.start_time) = (Tick::ZERO, pre_out, if sign < 0.0 { pre_out } else { Tick::ZERO }); + let mut outer = base.clone(); + outer.layers = vec![pre].into(); + let outer_id = p.add_item("Outer", effectcraft_color::Label::Sandstone, None, ItemKind::Comp(outer.into())); + let src = Longest(AtomicUsize::new(0)); + let m = mix(&p, &src, outer_id, 0.0, 800); + let longest = src.0.load(Ordering::Relaxed); + assert!(longest > 0 && longest <= MAX_READ, "nested {sign}%: read {longest} frames at once"); + assert!(m.iter().all(|v| v.is_finite())); + } +} + #[test] fn high_low_pass_attenuates() { fn hi(t: f64) -> (f32, f32) { From f715f30cccffa9ed29f4d081d0b032803f6f0c47 Mon Sep 17 00:00:00 2001 From: vangel76 Date: Sat, 7 Nov 2026 17:31:58 +0100 Subject: [PATCH 21/74] Cryptomatte: the Selection tool picks IDs while the effect is selected As in After Effects, with the Cryptomatte effect selected in Effect Controls and the Selection tool active, a click on its layer in the Composition viewer picks the ID under the cursor without arming the eyedropper: plain replaces, Shift adds, Alt or Ctrl removes, one undo step each, with the same hover name and highlight. A drag there moves nothing. The viewer keeps its clicks off the layer, on its own handles there (bounding box, gizmo, mask vertices, motion-path keys, guides, region of interest), while a region of interest or Free Transform box is drawn, with another effect or a property row selected, and with other tools. A press in the pass the pointer leaves the layer is passed on to the viewer as a click. Reuses the eyedropper's code path (effect.pickCryptomatte); automation id viewer.cryptoSelect. Fixes #2839 --- crates/ui-egui/src/panels/crypto_pick.rs | 155 ++++++++-- .../src/panels/effect_controls/viewer.rs | 22 +- crates/ui-egui/src/panels/viewer.rs | 79 ++++- crates/ui-egui/src/panels/viewer/shapes.rs | 6 + crates/ui-egui/tests/it/main.rs | 1 + crates/ui-egui/tests/it/ui_crypto_pick.rs | 3 + .../ui-egui/tests/it/ui_crypto_pick_undo.rs | 2 + crates/ui-egui/tests/it/ui_crypto_select.rs | 288 ++++++++++++++++++ docs/control-protocol.md | 4 +- docs/effects-parity.md | 2 +- 10 files changed, 531 insertions(+), 31 deletions(-) create mode 100644 crates/ui-egui/tests/it/ui_crypto_select.rs diff --git a/crates/ui-egui/src/panels/crypto_pick.rs b/crates/ui-egui/src/panels/crypto_pick.rs index 2310ba93b..e5e6dd88f 100644 --- a/crates/ui-egui/src/panels/crypto_pick.rs +++ b/crates/ui-egui/src/panels/crypto_pick.rs @@ -3,8 +3,17 @@ //! the viewer names the ID under the cursor, and a click picks it into the Selection: a plain //! click replaces it, Shift adds, Alt or Ctrl (Option or Cmd on a Mac) removes. It stays armed //! for the next click until Esc or the button again, and highlights the hovered ID's coverage. -//! Arming it whenever the effect is selected (After Effects' behaviour, #1811) would only need -//! to set the same pick. +//! +//! As in After Effects, the Selection tool picks the same way without arming anything while the +//! Cryptomatte effect is selected in Effect Controls (#2839, [`implicit_pick`]): over the +//! effect's layer (selected, so the viewer's press acts on it) a click picks and a drag moves nothing; +//! elsewhere the viewer keeps its clicks (selecting and moving layers). Esc has nothing to end. +//! "Over the layer" is its bounding quad, as the viewer's own hit test (not its alpha), less the +//! viewer's handles on it ([`super::viewer::grabs_at`]: bounding-box handles, mask vertices, +//! motion-path keys, guides), which keep scaling, reshaping and moving. A press in the very pass +//! the pointer leaves the layer is passed on to the viewer as a click. While the effect is +//! selected (or the eyedropper armed), a double-click on the layer picks (one step: the second +//! click changes nothing) instead of opening its precomp or editing its text. //! //! The hover needs the frame's Cryptomatte probe ([`effectcraft_engine::Session::crypto_probe_job`]): //! it is built off the UI thread, one at a time, not while playing or scrubbing; the chip says so @@ -21,7 +30,7 @@ use std::sync::atomic::{AtomicBool, Ordering}; use effectcraft_engine::commands::CryptoProbeJob; use effectcraft_engine::geom::Mat3; -use effectcraft_engine::project::Layer; +use effectcraft_engine::project::{GroupKind, Layer}; use effectcraft_engine::render::EvalCtx; use egui::{Color32, Rect, Sense, vec2}; use serde_json::{Value, json}; @@ -35,6 +44,9 @@ use crate::theme::Tokens; /// [`FxPick::kind`] of the Cryptomatte eyedropper. pub const KIND: &str = "crypto"; +/// The Cryptomatte effect's match name. +const EFFECT: &str = "ec.3d.cryptomatte"; + /// How long the time must stay put before a probe is built for it (scrubbing). const SETTLE: f64 = 0.1; @@ -111,6 +123,15 @@ struct State { seen: Option<((u64, u64), u64)>, /// The hovered ID's highlight: its hash and probe query, coverage texture, size and uv. mask: Option, + /// Picking with the Selection tool while the effect is selected ([`implicit_pick`]), not with + /// the armed eyedropper. + implicit: bool, + /// The last egui pass the overlay took clicks in (egui hands a press to what sensed it the + /// pass before). + sensed: Option, + /// The query of the last click queued: the second click of a double-click on the same pixel + /// with the same keys is not queued again. + last_click: Option, } /// Clears the busy flag and repaints when a build ends, a panic included. @@ -185,9 +206,20 @@ pub fn upkeep(app: &mut EffectcraftApp, ctx: &egui::Context) { } ctx.data_mut(|d| d.insert_temp(eid, epoch)); } - if app.ui.fx_pick.as_ref().is_none_or(|p| p.kind != KIND) && ctx.data(|d| d.get_temp::(sid)).is_some() { + // Picking with the Selection tool (the effect selected) keeps its state while the viewer runs + // it, every pass. + let old = ctx.data(|d| d.get_temp::(sid)); + let implicit = old.as_ref().is_some_and(|s| s.implicit && s.seen.is_some_and(|(_, n)| n == ctx.cumulative_pass_nr())); + if app.ui.fx_pick.as_ref().is_none_or(|p| p.kind != KIND) + && !implicit + && let Some(mut old) = old + { ctx.data_mut(|d| d.remove::(sid)); app.session.drop_crypto_probe(); + // The effect deselected (or the tool changed) while clicks waited: say they're not made. + if old.implicit && !old.pending.is_empty() { + drop_waiting(app, ctx, &mut old); + } } // The clicks waiting in the viewer, after everything else this pass did (its keys, edits and // frame steps included). @@ -273,10 +305,16 @@ fn viewer_pan_and_secondary(app: &mut EffectcraftApp, ctx: &egui::Context, resp: } } -/// Whether Space / Hand / Zoom (`tools`) or middle/right buttons (`nav`) own the pointer. -struct PointerNav { +/// Whether Space / Hand / Zoom (`tools`) or middle/right buttons (`nav`) own the pointer, and +/// whether a press at a point is the overlay's (`owned`: the Selection tool picking only over the +/// effect's layer, and only where the overlay took the press from the viewer). +struct PointerNav<'a> { tools: bool, nav: bool, + owned: &'a dyn Fn(egui::Pos2) -> bool, + /// The overlay took the pass's presses from the viewer (it sensed clicks the pass before): + /// one it does not own is passed on to the viewer ([`super::viewer::FORWARDED_PRIMARY`]). + forward: bool, } /// Queues Cryptomatte picks from primary presses and releases on the armed eyedropper overlay. @@ -290,7 +328,7 @@ fn viewer_click_to_pick( map: &super::viewer::ViewerMap, ectx: &EvalCtx, st: &mut State, - pointer: PointerNav, + pointer: PointerNav<'_>, at: &dyn Fn(egui::Pos2) -> Option, ) { let ctx = ui.ctx().clone(); @@ -320,7 +358,14 @@ fn viewer_click_to_pick( continue; }; if pressed { - let ours = map.area.contains(pos) && ctx.layer_id_at(pos) == Some(ui.layer_id()); + let in_viewer = map.area.contains(pos) && ctx.layer_id_at(pos) == Some(ui.layer_id()); + let ours = in_viewer && (pointer.owned)(pos); + // Taken from the viewer but not ours (the pointer left the layer in this very pass): + // the viewer clicks there next pass, so it isn't swallowed. + if pointer.forward && in_viewer && !ours && !pointer.nav { + let pass = ctx.cumulative_pass_nr(); + ctx.data_mut(|d| d.insert_temp(egui::Id::unique(super::viewer::FORWARDED_PRIMARY), (pos, keys, pass))); + } st.press = (!pointer.nav && ours).then(|| Press { pos, keys, @@ -348,6 +393,11 @@ fn viewer_click_to_pick( "replace" }); let same_frame = q["time"].as_f64() == Some(ectx.time.seconds()); + let double = ctx.input(|i| i.pointer.button_double_clicked(egui::PointerButton::Primary)); + if double && st.last_click.as_ref() == Some(&q) { + // The second click of a double-click, same pixel and keys: it would change nothing. + continue; + } if pos.distance(from) > SLOP { app.say(&ctx, tr("Drag cancelled the pick").to_string()); } else if app.playback.playing && !(same_frame && app.session.crypto_probe_ready(&q).unwrap_or(false)) { @@ -361,6 +411,7 @@ fn viewer_click_to_pick( st.pending_rev = rev; } st.after_typing |= typing || ctx.text_edit_focused(); + st.last_click = Some(q.clone()); st.pending.push(q); } } @@ -522,7 +573,8 @@ fn handle_escape(app: &mut EffectcraftApp, ui: &mut egui::Ui, st: &mut State) { } } -/// The armed eyedropper's part of the viewer. `m` maps the layer's effect space to the comp. +/// The armed eyedropper's part of the viewer, or (`implicit`) the Selection tool's while the +/// effect is selected ([`implicit_pick`]). `m` maps the layer's effect space to the comp. #[allow(clippy::too_many_arguments)] pub fn viewer_hook( app: &mut EffectcraftApp, @@ -533,6 +585,7 @@ pub fn viewer_hook( layer: &Layer, m: &Mat3, pick: &FxPick, + implicit: bool, ) { let ctx = ui.ctx().clone(); let sid = pick_id(); @@ -540,7 +593,9 @@ pub fn viewer_hook( reset_stale_state(app, &ctx, &mut st, pick); // The Cryptomatte effect that owns the Selection (gone: the pick ends). let Some(fx) = layer.effects().and_then(|f| f.groups().find(|g| g.find(pick.prop).is_some())).map(|g| g.uid) else { - app.ui.fx_pick = None; + if !implicit { + app.ui.fx_pick = None; + } return; }; let start = track_scrub_settle(app, &ctx, &mut st, ectx.time.seconds()); @@ -565,21 +620,83 @@ pub fn viewer_hook( // The Hand and Zoom tools (Space held too) and other buttons keep their own clicks and drags. let tools = super::space_hand(&ctx) || matches!(app.ui.tool, Tool::Hand | Tool::Zoom); let nav = tools || ctx.input(|i| i.pointer.middle_down() || i.pointer.secondary_down()); - // Drags too: a click that drifts must not move the layer under it. (egui hands a press to - // what sensed it the pass before, so middle and right presses land here: passed on below.) - let resp = ui.interact(map.area, egui::Id::unique("viewer-fx-pick"), if tools { Sense::hover() } else { Sense::click_and_drag() }); - viewer_pan_and_secondary(app, &ctx, &resp); - app.auto.add("viewer.fxPick", map.area, &pick.name); - let mut reading = hover_read(app, &ctx, map, &mut st, &resp, start, &at); - viewer_click_to_pick(app, ui, map, ectx, &mut st, PointerNav { tools, nav }, &at); + let oid = egui::Id::unique("viewer-fx-pick"); + // The Selection tool picks only over the effect's layer (#2839): the viewer's press there acts + // on it, a selected layer, even where layers in front cover it ([`super::viewer::pick`]). + // Elsewhere, and on the viewer's own handles there, the viewer keeps its clicks. The armed + // eyedropper picks anywhere. + let ui_layer = ui.layer_id(); + let on_layer = |p: egui::Pos2| { + !implicit + || (map.area.contains(p) + && ctx.layer_id_at(p) == Some(ui_layer) + && super::viewer::layer_hit(ectx, layer.id, map.to_comp(p)) + && !super::viewer::grabs_at(&ctx, p)) + }; + // A press the overlay holds (or its middle-button pan) stays its own wherever the pointer goes. + let panning = ctx.is_being_dragged(oid) && ctx.input(|i| i.pointer.middle_down()); + let over = st.press.is_some() || panning || ctx.pointer_hover_pos().is_some_and(on_layer); + let pass = ctx.cumulative_pass_nr(); + // A press is the overlay's only where it sensed clicks the pass before (egui handed it the + // press, not the viewer underneath, which would select or move the layer as well). + let owned_before = !implicit || st.sensed.is_some_and(|n| n.saturating_add(1) == pass); + st.implicit = implicit; + let mut reading = false; + if !implicit || over { + // Drags too: a click that drifts must not move the layer under it. (egui hands a press to + // what sensed it the pass before, so middle and right presses land here: passed on below.) + let resp = ui.interact(map.area, oid, if tools { Sense::hover() } else { Sense::click_and_drag() }); + if !tools { + st.sensed = Some(pass); + } + viewer_pan_and_secondary(app, &ctx, &resp); + app.auto.add(if implicit { "viewer.cryptoSelect" } else { "viewer.fxPick" }, map.area, &pick.name); + reading = hover_read(app, &ctx, map, &mut st, &resp, start, &at); + } else { + (st.asked, st.reply) = (None, None); + } + let owned = |p: egui::Pos2| owned_before && on_layer(p); + viewer_click_to_pick(app, ui, map, ectx, &mut st, PointerNav { tools, nav, owned: &owned, forward: implicit && owned_before }, &at); // Clicks waiting for their probe ([`make_waiting`], at the end of the pass). reading |= !st.pending.is_empty(); draw_coverage_highlight(app, &ctx, painter, map, m, &mut st); - draw_status_chip(app, &ctx, painter, map, &st, reading); - handle_escape(app, ui, &mut st); + if !implicit || over || reading { + draw_status_chip(app, &ctx, painter, map, &st, reading); + } + // (Selected, the effect has nothing for Esc to end.) + if !implicit { + handle_escape(app, ui, &mut st); + } ctx.data_mut(|d| d.insert_temp(sid, st)); } +/// The Cryptomatte effect selected in Effect Controls with the Selection tool active (After +/// Effects: clicks in the Composition panel pick its IDs then, no eyedropper armed; #2839): the +/// pick for its Selection. The last selected item on a selected layer counts, and only the +/// effect itself (its header in Effect Controls or the Timeline): a press on one of its rows (the +/// eyedropper's button, the Selection field) selects that property instead. An armed eyedropper +/// or crosshair (of any kind) comes first. +pub fn implicit_pick(app: &EffectcraftApp, ctx: &egui::Context) -> Option { + if app.ui.tool != Tool::Selection || app.ui.fx_pick.is_some() || app.ui.text_color_pick.is_some() || crate::color_picker::eyedropper_armed(ctx) { + return None; + } + // Drawing a region of interest or a Free Transform box: the viewer keeps every press. + if app.ui.viewer.roi_draw || super::viewer_overlays::free_transform_target(ctx).is_some() { + return None; + } + let comp = app.session.active_comp()?; + let time = app.session.time(); + let st = &app.session.state; + let (lid, uid) = st.selected_props.iter().rev().find(|(l, _)| st.selected_layers.contains(l))?; + let layer = comp.layer(*lid).filter(|l| l.is_active_at(time))?; + let fx = layer.effects()?.groups().find(|g| g.uid == *uid)?; + if !fx.enabled || !matches!(&fx.kind, GroupKind::Effect { effect } if effect == EFFECT) { + return None; + } + let prop = fx.get("selection")?; + Some(FxPick { kind: KIND.into(), layer: lid.0, prop: prop.uid, name: prop.name.clone() }) +} + #[cfg(test)] mod tests { use super::*; diff --git a/crates/ui-egui/src/panels/effect_controls/viewer.rs b/crates/ui-egui/src/panels/effect_controls/viewer.rs index 6d4eb7ffd..a2da5aa9b 100644 --- a/crates/ui-egui/src/panels/effect_controls/viewer.rs +++ b/crates/ui-egui/src/panels/effect_controls/viewer.rs @@ -221,12 +221,24 @@ pub fn viewer_hook( let ctx = ui.ctx().clone(); crate::color_picker::note_viewer(&ctx, map.area); let mut actions: Actions = vec![]; - let point_preview = if let Some(pick) = app.ui.fx_pick.clone().filter(|p| p.kind == crate::panels::crypto_pick::KIND) { + // The armed Cryptomatte eyedropper, or the Selection tool while the effect is selected (#2839). + let crypto = app.ui.fx_pick.clone().filter(|p| p.kind == crate::panels::crypto_pick::KIND).map(|p| (p, false)); + let crypto = crypto.or_else(|| crate::panels::crypto_pick::implicit_pick(app, &ctx).map(|p| (p, true))); + let point_preview = if let Some((pick, implicit)) = crypto { match ectx.comp.layer(effectcraft_engine::project::LayerId(pick.layer)) { - Some(l) => { - crate::panels::crypto_pick::viewer_hook(app, ui, painter, map, ectx, l, &crate::panels::viewer::from_effect_space(ectx, l, l2c(ectx, l)), &pick) - } - None => app.ui.fx_pick = None, + Some(l) => crate::panels::crypto_pick::viewer_hook( + app, + ui, + painter, + map, + ectx, + l, + &crate::panels::viewer::from_effect_space(ectx, l, l2c(ectx, l)), + &pick, + implicit, + ), + None if !implicit => app.ui.fx_pick = None, + None => {} } None } else { diff --git a/crates/ui-egui/src/panels/viewer.rs b/crates/ui-egui/src/panels/viewer.rs index b1899e1f8..d653dc662 100644 --- a/crates/ui-egui/src/panels/viewer.rs +++ b/crates/ui-egui/src/panels/viewer.rs @@ -67,6 +67,45 @@ fn map_id() -> egui::Id { /// the viewer opens its context menu there on the next pass. pub(crate) const FORWARDED_SECONDARY: &str = "viewer-forwarded-secondary"; +/// The egui data key (`(egui::Pos2, egui::Modifiers, u64)`: point, keys, egui pass) of a primary +/// press an overlay sensed but does not want (the Selection tool's Cryptomatte picking, #2839, when +/// the pointer left the layer in the pass of the press): the viewer clicks there on the next pass. +pub(crate) const FORWARDED_PRIMARY: &str = "viewer-forwarded-primary"; + +/// What the viewer's own handles reach this pass, for overlays drawn on top of it. +#[derive(Clone, Default)] +struct Grabs { + handles: Vec, + vertices: Vec, + keys: Vec, + axes: Vec<(LayerId, [Pos2; 2], [f64; 3], [f64; 3])>, + /// Region-of-interest handles. + roi: Vec, + /// Visible, unlocked guides on screen: (x or y, vertical). + guides: Vec<(f32, bool)>, +} + +fn grabs_id() -> egui::Id { + egui::Id::unique("viewer-grabs") +} + +/// Whether a Selection-tool press at `pos` grabs one of the viewer's own controls (a layer's +/// bounding-box handle or gizmo arrow, a mask vertex or tangent, a motion-path key, a guide, a +/// region-of-interest handle) instead of the layer under it, with the reach the viewer gives +/// them. An overlay that takes presses on a layer leaves these to the viewer. +pub(crate) fn grabs_at(ctx: &egui::Context, pos: Pos2) -> bool { + ctx.data(|d| { + let Some(g) = d.get_temp::(grabs_id()) else { return false }; + let reach = vertex_grab_radius(pos, g.handles.iter().copied()); + g.handles.iter().any(|h| h.distance(pos) < HANDLE + 2.0) + || g.vertices.iter().any(|v| v.distance(pos) < reach) + || g.keys.iter().any(|k| k.distance(pos) < 6.0) + || axis_at(&g.axes, pos).is_some() + || g.roi.iter().any(|h| h.distance(pos) < 6.0) + || g.guides.iter().any(|&(at, vertical)| (if vertical { pos.x } else { pos.y } - at).abs() < 4.0) + }) +} + pub fn comp_to_screen(ctx: &egui::Context, p: [f32; 2]) -> Option { let m: ViewerMap = ctx.data(|d| d.get_temp(map_id()))?; Some(m.to_screen([p[0] as f64, p[1] as f64])) @@ -1759,10 +1798,14 @@ fn phase_clicked(app: &mut EffectcraftApp, ectx: &EvalCtx, resp: &egui::Response layers_at(ectx, cpt).any(|h| h.id == l.id) && super::puppet_tool::takes_pin(app, ectx, l, cpt) }) }; - if resp.clicked() - && !text_owns - && let Some(pos) = resp.interact_pointer_pos() - { + // A press an overlay passed on in the pass before (FORWARDED_PRIMARY) is a click here. + let pass = ctx.cumulative_pass_nr(); + let forwarded = ctx + .data_mut(|d| d.remove_temp::<(Pos2, egui::Modifiers, u64)>(egui::Id::unique(FORWARDED_PRIMARY))) + .filter(|f| f.2.saturating_add(1) == pass) + .map(|f| (f.0, f.1)); + let clicked = if resp.clicked() && !text_owns { resp.interact_pointer_pos().map(|p| (p, mods)) } else { None }; + if let Some((pos, mods)) = clicked.or(forwarded) { let cpt = map.to_comp(pos); match app.ui.tool { Tool::Zoom => { @@ -1937,6 +1980,32 @@ fn phase_tail(app: &mut EffectcraftApp, ui: &mut egui::Ui, ectx: &EvalCtx, v: &m let ctx = v.ctx.clone(); let painter = v.painter.clone(); let comp = v.comp; + // The viewer's own controls, which overlays on top leave to it (#2839). + let roi = app.session.state.region_of_interest.map(|[rx, ry, rw, rh]| { + let r = Rect::from_min_size(v.comp_rect.min + vec2(rx as f32 * zoom, ry as f32 * zoom), vec2(rw as f32 * zoom, rh as f32 * zoom)); + roi_handles(r).to_vec() + }); + let guides = if app.ui.viewer.guides && !app.ui.viewer.lock_guides { + comp.guides + .iter() + .map(|g| { + let px = g.px(comp.width, comp.height); + let s = map.to_screen([px, px]); + (if g.vertical { s.x } else { s.y }, g.vertical) + }) + .collect() + } else { + vec![] + }; + let grabs = Grabs { + handles: v.handle_hits.iter().map(|h| h.2).collect(), + vertices: v.vertex_hits.iter().map(|h| h.pos).collect(), + keys: v.key_hits.iter().map(|h| h.pos).collect(), + axes: v.axis_hits.clone(), + roi: roi.unwrap_or_default(), + guides, + }; + ctx.data_mut(|d| d.insert_temp(grabs_id(), grabs)); // 3D Camera Tracker points, targets and their menu (on top of the viewer's gestures while // the effect is selected), and its analysis banner. super::camera_tracker_ui::viewer_hook(app, ui, &painter, &map, ectx, &|c, l| l2c(c, l).0); @@ -2001,7 +2070,7 @@ use geometry::{box_corners, project_clipped}; pub(crate) use geometry::{from_effect_space, fx2c, l2c, layer_outline, layer_quad}; use gesture::{Gesture, HANDLE, ROTO_HINT, pen_id, roi_drag, roi_handles, vertex_grab_radius}; use shapes::{create_shape, draw_shape_ghost, shape_drag}; -pub(crate) use shapes::{layer_at, layer_at_including_locked, layers_at, pick, selectable_layers}; +pub(crate) use shapes::{layer_at, layer_at_including_locked, layer_hit, layers_at, pick, selectable_layers}; #[cfg(test)] use tools::constrain_45; use tools::{add_motion_key, axis_at, axis_drag, drag_axes, draw_rigs, parent_world_inverse, pen_cursor, pen_cursor_icon, pen_press, select_motion_key}; diff --git a/crates/ui-egui/src/panels/viewer/shapes.rs b/crates/ui-egui/src/panels/viewer/shapes.rs index 0cd579883..f38b16e31 100644 --- a/crates/ui-egui/src/panels/viewer/shapes.rs +++ b/crates/ui-egui/src/panels/viewer/shapes.rs @@ -22,6 +22,12 @@ pub(crate) fn layers_at<'a, 'b>(ectx: &'b EvalCtx<'a>, cpt: [f64; 2]) -> impl It selectable_layers(ectx.comp, ectx.time).filter(move |l| layer_quad(ectx, l).is_some_and(|(_, q, _)| point_in_quad(cpt, &q))) } +/// Whether `layer` is among the selectable layers under a comp point ([`layers_at`] for that one +/// layer: its bounding quad, not its alpha). +pub(crate) fn layer_hit(ectx: &EvalCtx, layer: LayerId, cpt: [f64; 2]) -> bool { + selectable_layers(ectx.comp, ectx.time).find(|l| l.id == layer).is_some_and(|l| layer_quad(ectx, l).is_some_and(|(_, q, _)| point_in_quad(cpt, &q))) +} + /// Topmost selectable layer under a comp point. pub(crate) fn layer_at<'a>(ectx: &EvalCtx<'a>, cpt: [f64; 2]) -> Option<&'a Layer> { layers_at(ectx, cpt).next() diff --git a/crates/ui-egui/tests/it/main.rs b/crates/ui-egui/tests/it/main.rs index 422f0223e..ace9f7222 100644 --- a/crates/ui-egui/tests/it/main.rs +++ b/crates/ui-egui/tests/it/main.rs @@ -26,6 +26,7 @@ mod ui_contrast; mod ui_control_modifiers; mod ui_crypto_pick; mod ui_crypto_pick_undo; +mod ui_crypto_select; mod ui_dialog_preview; mod ui_dock_resize; mod ui_drag_drop; diff --git a/crates/ui-egui/tests/it/ui_crypto_pick.rs b/crates/ui-egui/tests/it/ui_crypto_pick.rs index 0d1767c7e..893e5d562 100644 --- a/crates/ui-egui/tests/it/ui_crypto_pick.rs +++ b/crates/ui-egui/tests/it/ui_crypto_pick.rs @@ -76,6 +76,9 @@ fn crypto_session(tag: &str, w: usize, h: usize, blur: bool) -> (Session, Ids) { s.execute_checked("prop.set", json!({"path": "effects/#1/blurriness", "value": 40.0})).unwrap(); } s.execute_checked("effect.apply", json!({"effect": "Cryptomatte"})).unwrap(); + // Not selected: these test the armed eyedropper alone (the Selection tool picks too while + // the effect is selected, #2839: `ui_crypto_select`). + s.state.selected_props.clear(); let fx = s.active_comp().unwrap().layers[0].effects().unwrap().groups().last().unwrap(); let sel = fx.get("selection").unwrap().uid; (s, Ids { layer, sel, footage, dir }) diff --git a/crates/ui-egui/tests/it/ui_crypto_pick_undo.rs b/crates/ui-egui/tests/it/ui_crypto_pick_undo.rs index 22aaeb9a5..a35743ac0 100644 --- a/crates/ui-egui/tests/it/ui_crypto_pick_undo.rs +++ b/crates/ui-egui/tests/it/ui_crypto_pick_undo.rs @@ -538,6 +538,8 @@ fn run_step(h: &mut Harness<'_, EffectcraftApp>, f: &Fix, m: &mut Model, size: [ } Step::ApplyCrypto(layer) => { h.state_mut().session.execute_checked("effect.apply", json!({"comp": f.comp, "layers": [layer], "effect": "Cryptomatte"})).unwrap(); + // Not left selected: the model covers the armed eyedropper alone (#2839). + h.state_mut().session.state.selected_props.clear(); let uid = sel_uid(&h.state().session, f.comp, *layer).unwrap(); layer_mut(&mut o, *layer).effects.push((CRYPTO.into(), Some((uid, String::new())))); (format!("#{n} apply Cryptomatte to {layer}"), true) diff --git a/crates/ui-egui/tests/it/ui_crypto_select.rs b/crates/ui-egui/tests/it/ui_crypto_select.rs new file mode 100644 index 000000000..f4b693556 --- /dev/null +++ b/crates/ui-egui/tests/it/ui_crypto_select.rs @@ -0,0 +1,288 @@ +//! Picking Cryptomatte IDs with the Selection tool (#2839): as in After Effects, while the +//! Cryptomatte effect is selected in Effect Controls a click on its layer in the Composition +//! viewer picks the ID under the cursor (plain replaces, Shift adds, Alt or Ctrl removes), one +//! undo step each, without arming the eyedropper, and moves or selects nothing. With another +//! effect selected, off the layer, or with another tool, the viewer behaves as before. + +use crate::ui_crypto_pick::{chip, crypto_exr, settle, wait_probe}; +use effectcraft_engine::Session; +use effectcraft_engine::keyframe::Value as KV; +use effectcraft_engine::project::LayerId; +use effectcraft_ui_egui::EffectcraftApp; +use effectcraft_ui_egui::dock::{self, PanelKind, SplitSize}; +use effectcraft_ui_egui::panels::viewer::comp_to_screen; +use effectcraft_ui_egui::state::Tool; +use egui::{Event, Modifiers, PointerButton, Pos2, vec2}; +use egui_kittest::Harness; +use serde_json::json; + +struct Ids { + plate: u64, + side: u64, + blur: u64, + crypto: u64, + sel: u64, + dir: std::path::PathBuf, +} + +/// A 480 × 180 comp: the 320 × 180 Cryptomatte plate in the middle (comp x 80 to 400) with a +/// Gaussian Blur and a Cryptomatte effect, and a 64 × 64 solid beside it (comp x 408 to 472). +/// The plate is selected, and its Cryptomatte effect in Effect Controls. +fn session(tag: &str) -> (Session, Ids) { + let dir = std::env::temp_dir().join(format!("ec-ui-crysel-{tag}-{}", std::process::id())); + std::fs::create_dir_all(&dir).unwrap(); + let path = crypto_exr(&dir, "plate.exr", 320, 180, 1); + let mut s = effectcraft_host::session(); + let r = s.execute("file.import", json!({"paths": [path]})).unwrap(); + assert_eq!(r["errors"], json!([]), "{r}"); + let footage = r["items"][0].as_u64().unwrap(); + s.execute("comp.new", json!({"name": "Crypto", "width": 480, "height": 180, "frameRate": 24, "duration": 2})).unwrap(); + let plate = s.execute("layer.addItem", json!({"item": footage})).unwrap()["layer"].as_u64().unwrap(); + s.state.selected_layers = vec![LayerId(plate)]; + s.execute_checked("effect.apply", json!({"effect": "Gaussian Blur"})).unwrap(); + s.execute_checked("effect.apply", json!({"effect": "Cryptomatte"})).unwrap(); + let side = s.execute("layer.newSolid", json!({"name": "Side", "width": 64, "height": 64})).unwrap()["layer"].as_u64().unwrap(); + s.execute_checked("prop.set", json!({"layer": side, "path": "transform/position", "value": [440.0, 40.0]})).unwrap(); + let fx = s.active_comp().unwrap().layer(LayerId(plate)).unwrap().effects().unwrap(); + let (blur, crypto) = (fx.groups().next().unwrap(), fx.groups().nth(1).unwrap()); + assert_eq!(crypto.match_id, "ec.3d.cryptomatte"); + let (blur, crypto, sel) = (blur.uid, crypto.uid, crypto.get("selection").unwrap().uid); + s.state.selected_layers = vec![LayerId(plate)]; + s.state.selected_props = vec![(LayerId(plate), crypto)]; + s.history = Default::default(); + (s, Ids { plate, side, blur, crypto, sel, dir }) +} + +/// The Composition viewer and Effect Controls side by side, the Timeline below. +fn harness(s: Session) -> Harness<'static, EffectcraftApp> { + let mut app = EffectcraftApp::new(s); + app.ui.viewer.show_layer_controls = false; + app.ui.tool = Tool::Selection; + app.ui.dock = dock::docked(dock::vsplit( + SplitSize::FixedSecond(160.0), + dock::hsplit(SplitSize::FixedSecond(420.0), dock::tabs(&[PanelKind::Composition], 0), dock::tabs(&[PanelKind::EffectControls], 0)), + dock::tabs(&[PanelKind::Timeline], 0), + )); + let mut h = Harness::builder().with_size(vec2(1400.0, 800.0)).with_pixels_per_point(1.0).with_step_dt(0.25).build_eframe(|_| app); + settle(&mut h); + h +} + +/// The screen point of the plate's pixel (x, y). +fn on_plate(h: &Harness<'_, EffectcraftApp>, x: f64, y: f64) -> Pos2 { + comp_to_screen(&h.ctx, [(80.0 + x) as f32, y as f32]).unwrap() +} + +fn hover(h: &mut Harness<'_, EffectcraftApp>, pos: Pos2) -> Option { + h.event(Event::PointerMoved(pos)); + // The overlay comes up over the layer in this pass and is hovered from the next one on. + h.run_steps(2); + wait_probe(h); + h.step(); + chip(h) +} + +/// Hover (a pass for the viewer to see the pointer), then press and release with `modifiers`. +fn click(h: &mut Harness<'_, EffectcraftApp>, pos: Pos2, modifiers: Modifiers) { + h.event(Event::PointerMoved(pos)); + h.event(Event::ModifiersChanged(modifiers)); + h.step(); + for pressed in [true, false] { + h.event(Event::PointerButton { pos, button: PointerButton::Primary, pressed, modifiers }); + h.step(); + } + h.event(Event::ModifiersChanged(Modifiers::NONE)); + h.step(); + wait_probe(h); + h.run_steps(2); +} + +fn drag(h: &mut Harness<'_, EffectcraftApp>, from: Pos2, by: egui::Vec2) { + h.event(Event::PointerMoved(from)); + h.step(); + h.event(Event::PointerButton { pos: from, button: PointerButton::Primary, pressed: true, modifiers: Modifiers::NONE }); + h.step(); + for k in 1..=6 { + h.event(Event::PointerMoved(from + by * k as f32 / 6.0)); + h.step(); + } + h.event(Event::PointerButton { pos: from + by, button: PointerButton::Primary, pressed: false, modifiers: Modifiers::NONE }); + h.run_steps(3); +} + +fn selection(h: &Harness<'_, EffectcraftApp>, ids: &Ids) -> String { + let s = &h.state().session; + match &s.active_comp().unwrap().layer(LayerId(ids.plate)).unwrap().props.find(ids.sel).unwrap().value { + KV::Str(t) => t.clone(), + v => panic!("{v:?}"), + } +} + +fn position(h: &Harness<'_, EffectcraftApp>, layer: u64) -> String { + let l = h.state().session.active_comp().unwrap().layer(LayerId(layer)).unwrap().clone(); + format!("{:?}", l.transform().and_then(|t| t.get("position")).map(|p| p.value.clone())) +} + +fn undo_steps(h: &Harness<'_, EffectcraftApp>) -> usize { + h.state().session.history.undo.len() +} + +fn selected_layers(h: &Harness<'_, EffectcraftApp>) -> Vec { + h.state().session.state.selected_layers.iter().map(|l| l.0).collect() +} + +/// Without the change, the viewer took these clicks itself: the Selection stayed empty. +#[test] +fn the_selection_tool_picks_while_the_cryptomatte_effect_is_selected() { + let (s, ids) = session("picks"); + let mut h = harness(s); + let (steps, before) = (undo_steps(&h), position(&h, ids.plate)); + let (a, b) = (on_plate(&h, 50.0, 40.0), on_plate(&h, 120.0, 100.0)); + // The viewer names the ID under the cursor, as the armed eyedropper does. + assert_eq!(hover(&mut h, a).as_deref(), Some("c1_1")); + assert!(h.state().auto.find("viewer.cryptoSelect").is_some(), "the Selection tool picks over the layer"); + assert_eq!(hover(&mut h, b).as_deref(), Some("c3_3")); + click(&mut h, a, Modifiers::NONE); + assert_eq!(selection(&h, &ids), "c1_1", "a click replaces"); + click(&mut h, b, Modifiers::SHIFT); + assert_eq!(selection(&h, &ids), "c1_1, c3_3", "Shift adds"); + click(&mut h, a, Modifiers::ALT); + assert_eq!(selection(&h, &ids), "c3_3", "Alt removes"); + click(&mut h, b, Modifiers::COMMAND); + assert_eq!(selection(&h, &ids), "", "Ctrl removes"); + click(&mut h, b, Modifiers::NONE); + assert_eq!(selection(&h, &ids), "c3_3"); + click(&mut h, b, Modifiers::CTRL); + assert_eq!(selection(&h, &ids), "", "Ctrl (not Cmd) removes too"); + click(&mut h, b, Modifiers::NONE); + assert_eq!(selection(&h, &ids), "c3_3"); + assert_eq!(undo_steps(&h), steps + 7, "one undo step per pick"); + assert!(h.state().ui.fx_pick.is_none(), "nothing armed"); + // Nothing else changed: the layer stays put and stays the only one selected. + assert_eq!(position(&h, ids.plate), before); + assert_eq!(selected_layers(&h), vec![ids.plate]); + // A drag on the layer moves nothing and picks nothing. + drag(&mut h, a, vec2(120.0, 40.0)); + assert_eq!((position(&h, ids.plate), undo_steps(&h), selection(&h, &ids)), (before, steps + 7, "c3_3".to_string())); + // Esc has nothing to end: the next click still picks. + h.key_press(egui::Key::Escape); + h.run_steps(2); + click(&mut h, a, Modifiers::SHIFT); + assert_eq!(selection(&h, &ids), "c3_3, c1_1", "added at the end"); + // Undo takes the picks back one at a time. + h.state_mut().session.execute_checked("edit.undo", json!({})).unwrap(); + assert_eq!(selection(&h, &ids), "c3_3"); + let _ = std::fs::remove_dir_all(&ids.dir); +} + +/// With another effect selected, beside the layer, or with another tool, the viewer keeps its +/// clicks: the Selection tool moves and selects layers as before, and nothing is picked. +#[test] +fn the_viewer_keeps_its_clicks_with_another_effect_beside_the_layer_or_another_tool() { + let (s, ids) = session("keeps"); + let mut h = harness(s); + let a = on_plate(&h, 50.0, 40.0); + // Gaussian Blur selected instead: a drag moves the layer. + h.state_mut().session.state.selected_props = vec![(LayerId(ids.plate), ids.blur)]; + h.run_steps(2); + let before = position(&h, ids.plate); + assert_eq!(hover(&mut h, a), None, "no ID named"); + assert!(h.state().auto.find("viewer.cryptoSelect").is_none()); + drag(&mut h, a, vec2(60.0, 0.0)); + assert_ne!(position(&h, ids.plate), before, "the drag moved the layer"); + assert_eq!(selection(&h, &ids), ""); + // The Cryptomatte effect selected again: a click on the other layer beside it selects that one. + h.state_mut().session.execute_checked("edit.undo", json!({})).unwrap(); + h.state_mut().session.state.selected_layers = vec![LayerId(ids.plate)]; + h.state_mut().session.state.selected_props = vec![(LayerId(ids.plate), ids.crypto)]; + h.run_steps(2); + let side = comp_to_screen(&h.ctx, [440.0, 40.0]).unwrap(); + let a = on_plate(&h, 50.0, 40.0); + assert_eq!(hover(&mut h, a).as_deref(), Some("c1_1")); + click(&mut h, side, Modifiers::NONE); + assert_eq!(selected_layers(&h), vec![ids.side], "the click beside the layer selected the other layer"); + assert_eq!(selection(&h, &ids), ""); + // The Hand tool with the effect selected: no pick. + h.state_mut().session.state.selected_layers = vec![LayerId(ids.plate)]; + h.state_mut().session.state.selected_props = vec![(LayerId(ids.plate), ids.crypto)]; + h.state_mut().ui.tool = Tool::Hand; + h.run_steps(2); + let a = on_plate(&h, 50.0, 40.0); + click(&mut h, a, Modifiers::NONE); + assert_eq!(selection(&h, &ids), "", "the Hand tool picks nothing"); + // Back to the Selection tool: it picks. + h.state_mut().ui.tool = Tool::Selection; + h.run_steps(2); + let a = on_plate(&h, 50.0, 40.0); + assert_eq!(hover(&mut h, a).as_deref(), Some("c1_1")); + click(&mut h, a, Modifiers::NONE); + assert_eq!(selection(&h, &ids), "c1_1"); + let _ = std::fs::remove_dir_all(&ids.dir); +} + +/// A press in the very pass the pointer leaves the layer for another one (the overlay sensed it +/// the pass before, so egui hands it the press): passed on to the viewer, which selects that layer. +#[test] +fn a_press_as_the_pointer_leaves_the_layer_still_selects_the_other_layer() { + let (s, ids) = session("leave"); + let mut h = harness(s); + let a = on_plate(&h, 50.0, 40.0); + assert_eq!(hover(&mut h, a).as_deref(), Some("c1_1")); + let side = comp_to_screen(&h.ctx, [440.0, 40.0]).unwrap(); + let ev = &mut h.input_mut().events; + ev.push(Event::PointerMoved(side)); + ev.push(Event::PointerButton { pos: side, button: PointerButton::Primary, pressed: true, modifiers: Modifiers::NONE }); + h.step(); + h.event(Event::PointerButton { pos: side, button: PointerButton::Primary, pressed: false, modifiers: Modifiers::NONE }); + h.run_steps(3); + assert_eq!(selected_layers(&h), vec![ids.side], "the press wasn't swallowed"); + assert_eq!(selection(&h, &ids), ""); + let _ = std::fs::remove_dir_all(&ids.dir); +} + +/// The viewer's own handles on the layer keep working with the effect selected: a drag on a +/// corner of the bounding box scales the layer and picks nothing. +#[test] +fn a_corner_handle_still_scales_with_the_effect_selected() { + let (s, ids) = session("handle"); + let mut h = harness(s); + h.state_mut().ui.viewer.show_layer_controls = true; + h.run_steps(2); + let a = on_plate(&h, 50.0, 40.0); + assert_eq!(hover(&mut h, a).as_deref(), Some("c1_1"), "inside the layer it picks"); + let scale = |h: &Harness<'_, EffectcraftApp>| { + let l = h.state().session.active_comp().unwrap().layer(LayerId(ids.plate)).unwrap().clone(); + format!("{:?}", l.transform().and_then(|t| t.get("scale")).map(|p| p.value.clone())) + }; + let before = scale(&h); + // The bottom-right corner of the plate (comp 400, 180). + let corner = comp_to_screen(&h.ctx, [400.0, 180.0]).unwrap(); + drag(&mut h, corner, vec2(-40.0, -20.0)); + assert_ne!(scale(&h), before, "the handle scaled the layer"); + assert_eq!(selection(&h, &ids), ""); + let _ = std::fs::remove_dir_all(&ids.dir); +} + +/// From the effect selected to the eyedropper and back: its button selects the Selection +/// property (the effect itself no longer), so after Esc a click on the layer picks nothing and +/// no Cryptomatte IDs are kept read. +#[test] +fn after_the_eyedropper_and_esc_the_selection_tool_does_not_pick() { + let (s, ids) = session("handoff"); + let mut h = harness(s); + let a = on_plate(&h, 50.0, 40.0); + assert_eq!(hover(&mut h, a).as_deref(), Some("c1_1"), "the effect selected: it picks"); + let eye = crate::ui_crypto_pick::at(&h, &format!("effectControls.prop.{}.eyedropper", ids.sel)); + click(&mut h, eye, Modifiers::NONE); + assert!(h.state().ui.fx_pick.as_ref().is_some_and(|p| p.kind == "crypto" && p.prop == ids.sel), "armed"); + assert_eq!(h.state().session.state.selected_props, vec![(LayerId(ids.plate), ids.sel)], "the row is selected"); + h.key_press(egui::Key::Escape); + h.run_steps(2); + assert!(h.state().ui.fx_pick.is_none(), "Esc ends the eyedropper"); + click(&mut h, a, Modifiers::NONE); + h.run_steps(2); + assert_eq!(selection(&h, &ids), "", "no pick"); + assert!(h.state().auto.find("viewer.cryptoSelect").is_none()); + assert!(!h.state().session.crypto_probe_stats().0, "no IDs kept read"); + let _ = std::fs::remove_dir_all(&ids.dir); +} diff --git a/docs/control-protocol.md b/docs/control-protocol.md index 9f3d69ea5..c28d009ce 100644 --- a/docs/control-protocol.md +++ b/docs/control-protocol.md @@ -152,7 +152,9 @@ revolutions are `timeline.prop..revolutions`, `effectControls.prop..re `viewer.motionPath..[.in|.out]`, `viewer.puppetPin.[.rotate|.scale]`, `viewer.freeTransform.handle.`, `viewer.cryptoPick` (the Cryptomatte eyedropper's hover chip; its label is the chip's text), `viewer.cryptoPick.error` (the chip when it shows an error; label `whole`, or -`cut` when the message is cut off), `viewer.cryptoPick.highlight` (the hovered ID's highlighted area) and +`cut` when the message is cut off), `viewer.cryptoPick.highlight` (the hovered ID's highlighted area), +`viewer.cryptoSelect` (the Selection tool picking Cryptomatte IDs over the layer while its Cryptomatte +effect is selected in Effect Controls, no eyedropper armed; label: the Selection's name) and `viewer.regionOfInterest`; the Graph Editor adds `timeline.graph.transformBox[.]`, `timeline.graph.snap` and `timeline.graph.reference`. Use `ui.elements` to see what is on screen. diff --git a/docs/effects-parity.md b/docs/effects-parity.md index c7f850834..2f51c4cc8 100644 --- a/docs/effects-parity.md +++ b/docs/effects-parity.md @@ -26,7 +26,7 @@ corpus compares renders, each effect is assumed to differ somewhere, and users k | Category | Effects | On the GPU | Known differences or bugs | |---|---:|---:|---| -| 3D Channel | 8 | 8 | Cryptomatte's GPU path is slow (#473; the Selection is picked in the viewer with an eyedropper, quoted names with commas fixed); multi-layer EXR: one decode per frame for the picture and the channels, cache softly bounded at 1 GB (#474; holding channels as f16 is still to do) | +| 3D Channel | 8 | 8 | Cryptomatte's GPU path is slow (#473; the Selection is picked in the viewer with an eyedropper, or with the Selection tool while the effect is selected as in After Effects (#2839), quoted names with commas fixed); multi-layer EXR: one decode per frame for the picture and the channels, cache softly bounded at 1 GB (#474; holding channels as f16 is still to do) | | Audio | 13 | 0 | CPU by design (applied in the mixdown) | | Blur & Sharpen | 16 | 15 | Radial Blur keeps the layer edges as After Effects 2026 (713 now pixel-identical); Gaussian Blur interaction and output to be measured first ([fidelity/ae-2026-acceptance.md](fidelity/ae-2026-acceptance.md)) | | Channel | 13 | 13 | — | From 16dccbb8fa8bb0a9e37a99f17109e63d23c99da6 Mon Sep 17 00:00:00 2001 From: vangel76 Date: Sat, 7 Nov 2026 17:37:39 +0100 Subject: [PATCH 22/74] Effect Controls: a lock request with no layer shown is an error (#3193) With the lock kept for a layer of another comp and nothing selected in the active one, ui.set {effectControlsLocked: true} reported success. It now returns the documented error and leaves the kept lock alone. --- crates/ui-egui/src/control.rs | 14 +++++---- crates/ui-egui/tests/it/ui_effect_controls.rs | 30 +++++++++++++++++++ 2 files changed, 39 insertions(+), 5 deletions(-) diff --git a/crates/ui-egui/src/control.rs b/crates/ui-egui/src/control.rs index f6c255d69..b9ff46c4b 100644 --- a/crates/ui-egui/src/control.rs +++ b/crates/ui-egui/src/control.rs @@ -281,11 +281,15 @@ fn set_ui(app: &mut EffectcraftApp, ctx: &egui::Context, p: &Value) -> Outcome { { return err(e); } - // Effect Controls' tab lock: on locks it to the layer it shows. - if let Some(on) = p.get("effectControlsLocked").and_then(Value::as_bool) - && crate::panels::effect_controls::set_lock(app, on) != on - { - return err("effectControlsLocked: Effect Controls shows no layer to lock"); + // Effect Controls' tab lock: on locks it to the layer it shows. With no layer shown (the + // lock kept for a layer of another comp, nothing selected here) a lock request is an error + // and the kept lock stays. + if let Some(on) = p.get("effectControlsLocked").and_then(Value::as_bool) { + crate::panels::effect_controls::scope_lock(app); + let shown = crate::panels::effect_controls::shown_layer_id(app).is_some(); + if (on && !shown) || crate::panels::effect_controls::set_lock(app, on) != on { + return err("effectControlsLocked: Effect Controls shows no layer to lock"); + } } set_ui_layout(app, p) } diff --git a/crates/ui-egui/tests/it/ui_effect_controls.rs b/crates/ui-egui/tests/it/ui_effect_controls.rs index ce51a2b2f..ffafe867d 100644 --- a/crates/ui-egui/tests/it/ui_effect_controls.rs +++ b/crates/ui-egui/tests/it/ui_effect_controls.rs @@ -1013,6 +1013,36 @@ fn replacing_the_project_unlocks_effect_controls() { assert_eq!(h.state().ui.effect_controls_lock, None, "the lock belongs to the replaced project"); } +/// #3193: with the lock kept for a layer of another comp and no layer selected in the active one, +/// Effect Controls shows no layer: a lock request is an error and the kept lock stays. +#[test] +fn a_lock_request_with_no_layer_shown_fails_and_keeps_the_other_comps_lock() { + use effectcraft_ui_egui::control::{self, ControlRequest, Outcome}; + fn send(h: &mut Harness<'_, EffectcraftApp>) -> serde_json::Value { + let ctx = h.ctx.clone(); + let (req, _rx) = ControlRequest::new("ui.set", json!({"effectControlsLocked": true})); + let now = h.state().now(); + match control::handle(h.state_mut(), &ctx, &req, now) { + Outcome::Done(v) => v, + _ => panic!("ui.set: no immediate reply"), + } + } + let (app, a, _, _, _) = two_layers(); + let mut h = Harness::builder().with_size(egui::vec2(1600.0, 1000.0)).build_eframe(|_| app); + h.run_steps(4); + assert_eq!(send(&mut h)["ok"], true); + let kept = h.state().ui.effect_controls_lock; + assert_eq!(kept.map(|l| l.layer), Some(a)); + // Another comp becomes active, nothing selected in it. + h.state_mut().session.execute("comp.new", json!({"name": "Other", "width": 320, "height": 180, "frameRate": 30, "duration": 5})).unwrap(); + h.state_mut().session.state.selected_layers.clear(); + h.run_steps(3); + assert_ne!(h.state().session.active_comp_id().map(|c| c.0), kept.map(|l| l.comp), "another comp is active"); + let reply = send(&mut h); + assert_eq!(reply["ok"], false, "{reply}"); + assert_eq!(h.state().ui.effect_controls_lock, kept, "the other comp's lock stays"); +} + /// #3193: a lock request right after the project is replaced, before the next frame, is not /// answered with the old project's lock: the reply matches the lock the panel keeps. #[test] From 78acc6e1b1f02a19c18c7ecb7fe9d9afc368fd4b Mon Sep 17 00:00:00 2001 From: vangel76 Date: Sat, 7 Nov 2026 17:41:58 +0100 Subject: [PATCH 23/74] Number fields: a typed +n adds n to each selected layer, even where the edited one is clamped (#3195) A typed +10 on several selected layers went through the same-change offset path, so when the edited layer's range cut its change short (Opacity 95 + 10 stops at 100) the others moved by 5. Send the typed operand as prop.set addend: each other layer takes its own value plus it, within its own range; scrubs keep the offset path. --- crates/engine/src/cmd/prop.rs | 26 ++++++++++++++--------- crates/engine/src/tests/timeline.rs | 11 ++++++++++ crates/ui-egui/src/widgets.rs | 11 +++++----- crates/ui-egui/tests/it/ui_inline_edit.rs | 10 +++++++++ 4 files changed, 43 insertions(+), 15 deletions(-) diff --git a/crates/engine/src/cmd/prop.rs b/crates/engine/src/cmd/prop.rs index ae37ea1b7..028fdb0dd 100644 --- a/crates/engine/src/cmd/prop.rs +++ b/crates/engine/src/cmd/prop.rs @@ -371,24 +371,30 @@ fn set_selected(s: &mut Session, p: &Value) -> Result { }; let follow: Vec = p.get("follow").and_then(Value::as_array).into_iter().flatten().filter_map(Value::as_u64).filter_map(|d| usize::try_from(d).ok()).collect(); - // A typed `*2` or `/2` (#3195): each layer's own value times it, not the edited layer's result. + // A typed `*2`, `/2` (`factor`) or `+10` (`addend`) (#3195): each layer's own value times it + // or plus it, not the edited layer's result (which its range may have clamped). let factor = p.get("factor").and_then(Value::as_f64).filter(|k| k.is_finite() && delta.is_none()); + let addend = p.get("addend").and_then(Value::as_f64).filter(|n| n.is_finite() && delta.is_none()); + let own_step = |v: f64| -> Option { + let r = match (factor, addend) { + (Some(k), _) => v * k, + (None, Some(n)) => v + n, + (None, None) => return None, + }; + r.is_finite().then_some(r) + }; // Each layer's own value where `set` writes it (a `time` other than the CTI keys there). let typed = |s: &Session, q: &mut Value, olid: LayerId, ouid: Uid| { let at = snapped_key_time(s, cid, olid, q, "prop.set").ok().flatten(); - if let (true, Some(k)) = (scalar, factor) - && let Some(own) = components_at(s, cid, olid, ouid, at).and_then(|c| c.first().copied()) - && (own * k).is_finite() - { - q["value"] = json!(own * k); + if scalar && let Some(v) = components_at(s, cid, olid, ouid, at).and_then(|c| c.first().copied()).and_then(own_step) { + q["value"] = json!(v); } if let (Some(dims), Some(n)) = (&dims, &new) && let Some(mut c) = components_at(s, cid, olid, ouid, at) { // The typed components' new values on this layer. - let typed_value = |c: &[f64], d: usize| match factor { - Some(k) => c.get(d).map(|v| v * k).filter(|v| v.is_finite()), - None => n.get(d).copied(), + let typed_value = |c: &[f64], d: usize| { + if factor.is_some() || addend.is_some() { c.get(d).copied().and_then(own_step) } else { n.get(d).copied() } }; let change = dims.first().and_then(|&d| Some(typed_value(&c, d)? - c.get(d)?)).unwrap_or(0.0); let values: Vec> = dims.iter().map(|&d| typed_value(&c, d)).collect(); @@ -1459,7 +1465,7 @@ pub fn specs() -> Vec { "Set Property Value", [], None, - "{layer?, path|prop, value, time? (layer s by default), timeBase?: layer|comp, merge?, selected? (also the other selected layers), offset? (them by the same change), proportional? (with offset: Proportional Scrubbing, each layer by its share in selection order, the first none, the last all), dims? (with selected, a typed value: the components edited, e.g. [2] for Z; the other layers keep the rest of their own value), follow? (with dims: components that move on each other layer by the change its first typed component takes), factor? (with selected, a typed `*` or `/`: the other layers take their own value times it, in the dims components of a vector)}", + "{layer?, path|prop, value, time? (layer s by default), timeBase?: layer|comp, merge?, selected? (also the other selected layers), offset? (them by the same change), proportional? (with offset: Proportional Scrubbing, each layer by its share in selection order, the first none, the last all), dims? (with selected, a typed value: the components edited, e.g. [2] for Z; the other layers keep the rest of their own value), follow? (with dims: components that move on each other layer by the change its first typed component takes), factor? (with selected, a typed `*` or `/`: the other layers take their own value times it, in the dims components of a vector), addend? (the same for a typed `+`: their own value plus it)}", has_layers, set_selected ), diff --git a/crates/engine/src/tests/timeline.rs b/crates/engine/src/tests/timeline.rs index 43eb922e2..f291de627 100644 --- a/crates/engine/src/tests/timeline.rs +++ b/crates/engine/src/tests/timeline.rs @@ -1552,6 +1552,17 @@ fn a_typed_factor_scales_each_selected_layers_own_value() { s.execute_checked("prop.set", json!({"layer": a, "prop": position, "value": [5.0, 20.0], "selected": true, "dims": [0], "factor": 0.5})).unwrap(); assert_eq!(prop(&s, a, "transform/position").value.components()[..2], [5.0, 20.0]); assert_eq!(prop(&s, b, "transform/position").value.components()[..2], [50.0, 50.0], "B halves its own X, keeps its Y"); + // A typed `+10` (`addend`): each layer's own value plus 10, within its own range, even where + // the edited layer's range cut its change short (95 + 10 stops at 100). + s.execute("prop.set", json!({"layer": a, "path": "transform/opacity", "value": 95.0})).unwrap(); + s.execute("prop.set", json!({"layer": b, "path": "transform/opacity", "value": 30.0})).unwrap(); + s.execute_checked("prop.set", json!({"layer": a, "prop": opacity, "value": 100.0, "selected": true, "addend": 10.0})).unwrap(); + assert_eq!(prop(&s, b, "transform/opacity").value.as_f64(), 40.0, "B adds 10, not the 5 A moved"); + s.execute("prop.set", json!({"layer": b, "path": "transform/opacity", "value": 95.0})).unwrap(); + s.execute_checked("prop.set", json!({"layer": a, "prop": opacity, "value": 100.0, "selected": true, "addend": 10.0})).unwrap(); + assert_eq!(prop(&s, b, "transform/opacity").value.as_f64(), 100.0, "B's own range still applies"); + s.execute_checked("prop.set", json!({"layer": a, "prop": position, "value": [15.0, 20.0], "selected": true, "dims": [0], "addend": 10.0})).unwrap(); + assert_eq!(prop(&s, b, "transform/position").value.components()[..2], [60.0, 50.0], "B adds 10 to its own X"); } /// #3195: Mask Feather is never negative on the other selected layers either, when a typed `*-1` diff --git a/crates/ui-egui/src/widgets.rs b/crates/ui-egui/src/widgets.rs index c5b7223da..87a6d217e 100644 --- a/crates/ui-egui/src/widgets.rs +++ b/crates/ui-egui/src/widgets.rs @@ -164,14 +164,15 @@ pub fn hot_number_step_at( /// Mark a queued `prop.set` (with `selected`) as the typed `step` it came from, so every other /// selected layer takes that step from its own value rather than the edited layer's result -/// (#3195): `+n` by the same change (`offset`), `*n` and `/n` by the same `factor`. +/// (#3195): `+n` adds `n` to each (`addend`; not the edited layer's change, which its range may +/// have clamped), `*n` and `/n` multiply each by the same `factor`. pub fn mark_step(action: Option<&mut (String, serde_json::Value)>, step: Option) { let (Some((_, a)), Some(step)) = (action, step) else { return }; - match step.factor() { - Some(k) => a["factor"] = serde_json::json!(k), - None if matches!(step, crate::num_math::Step::Add(_)) => a["offset"] = serde_json::json!(true), + match (step, step.factor()) { + (crate::num_math::Step::Add(n), _) => a["addend"] = serde_json::json!(n), + (_, Some(k)) => a["factor"] = serde_json::json!(k), // (A factor too large to send: the other layers take the typed result.) - None => {} + (_, None) => {} } } diff --git a/crates/ui-egui/tests/it/ui_inline_edit.rs b/crates/ui-egui/tests/it/ui_inline_edit.rs index c845863e4..7d51f0015 100644 --- a/crates/ui-egui/tests/it/ui_inline_edit.rs +++ b/crates/ui-egui/tests/it/ui_inline_edit.rs @@ -480,6 +480,16 @@ fn a_typed_step_applies_to_each_selected_layer() { key(&mut h, Key::Enter); assert_eq!((opacity_of(&h, a), opacity_of(&h, b)), expected, "{text}"); } + // `+10` where the edited layer stops at 100: the others still add 10, not the 5 it moved. + for (l, v) in [(a, 95.0), (b, 30.0)] { + h.state_mut().session.execute("prop.set", json!({"layer": l, "path": "transform/opacity", "value": v})).unwrap(); + } + h.run_steps(5); + let p = center(&h, &format!("timeline.prop.{opacity}.value")); + click(&mut h, p, 1); + type_text(&mut h, "+10"); + key(&mut h, Key::Enter); + assert_eq!((opacity_of(&h, a), opacity_of(&h, b)), (100.0, 40.0)); } /// A click at `p` with `modifiers` held. From d1bdb701b74b5e7b6b764d66607b31af0f1ba109 Mon Sep 17 00:00:00 2001 From: vangel76 Date: Sat, 7 Nov 2026 18:22:03 +0100 Subject: [PATCH 24/74] Effect Controls: a lock request while locked in another comp is an error (#3193) Locked to a layer of another comp, the panel shows the active comp's selection and its tab lock reads locked (a click unlocks). ui.set {effectControlsLocked: true} reported success there without locking the shown layer; it now returns an error and keeps the other comp's lock. --- crates/ui-egui/src/control.rs | 5 +++ crates/ui-egui/tests/it/ui_effect_controls.rs | 31 +++++++++++++++++++ docs/control-protocol.md | 2 +- 3 files changed, 37 insertions(+), 1 deletion(-) diff --git a/crates/ui-egui/src/control.rs b/crates/ui-egui/src/control.rs index b9ff46c4b..15a10380d 100644 --- a/crates/ui-egui/src/control.rs +++ b/crates/ui-egui/src/control.rs @@ -290,6 +290,11 @@ fn set_ui(app: &mut EffectcraftApp, ctx: &egui::Context, p: &Value) -> Outcome { if (on && !shown) || crate::panels::effect_controls::set_lock(app, on) != on { return err("effectControlsLocked: Effect Controls shows no layer to lock"); } + // Locked to a layer of another comp, the panel shows this comp's selection: the tab's + // lock reads locked and a click unlocks, so a lock request does not lock what is shown. + if on && app.ui.effect_controls_lock.is_some_and(|k| app.session.active_comp_id().is_none_or(|c| c.0 != k.comp)) { + return err("effectControlsLocked: Effect Controls is locked to a layer of another composition; unlock it first"); + } } set_ui_layout(app, p) } diff --git a/crates/ui-egui/tests/it/ui_effect_controls.rs b/crates/ui-egui/tests/it/ui_effect_controls.rs index ffafe867d..ec7d5683d 100644 --- a/crates/ui-egui/tests/it/ui_effect_controls.rs +++ b/crates/ui-egui/tests/it/ui_effect_controls.rs @@ -1043,6 +1043,37 @@ fn a_lock_request_with_no_layer_shown_fails_and_keeps_the_other_comps_lock() { assert_eq!(h.state().ui.effect_controls_lock, kept, "the other comp's lock stays"); } +/// #3193: locked to a layer of another comp, the panel shows this comp's selection; a lock request +/// does not claim to lock that layer: it is an error and the other comp's lock stays. +#[test] +fn a_lock_request_while_locked_in_another_comp_fails() { + use effectcraft_ui_egui::control::{self, ControlRequest, Outcome}; + fn send(h: &mut Harness<'_, EffectcraftApp>) -> serde_json::Value { + let ctx = h.ctx.clone(); + let (req, _rx) = ControlRequest::new("ui.set", json!({"effectControlsLocked": true})); + let now = h.state().now(); + match control::handle(h.state_mut(), &ctx, &req, now) { + Outcome::Done(v) => v, + _ => panic!("ui.set: no immediate reply"), + } + } + let (app, a, _, _, _) = two_layers(); + let mut h = Harness::builder().with_size(egui::vec2(1600.0, 1000.0)).build_eframe(|_| app); + h.run_steps(4); + assert_eq!(send(&mut h)["ok"], true); + let kept = h.state().ui.effect_controls_lock; + assert_eq!(kept.map(|l| l.layer), Some(a)); + // Another comp with a selected layer becomes active: the panel shows that layer. + h.state_mut().session.execute("comp.new", json!({"name": "Other", "width": 320, "height": 180, "frameRate": 30, "duration": 5})).unwrap(); + let c = h.state_mut().session.execute("layer.newSolid", json!({"name": "C", "color": "#00ff00"})).unwrap()["layer"].as_u64().unwrap(); + h.state_mut().session.state.selected_layers = vec![LayerId(c)]; + h.run_steps(3); + assert_ne!(h.state().session.active_comp_id().map(|x| x.0), kept.map(|l| l.comp), "another comp is active"); + let reply = send(&mut h); + assert_eq!(reply["ok"], false, "{reply}"); + assert_eq!(h.state().ui.effect_controls_lock, kept, "the other comp's lock stays"); +} + /// #3193: a lock request right after the project is replaced, before the next frame, is not /// answered with the old project's lock: the reply matches the lock the panel keeps. #[test] diff --git a/docs/control-protocol.md b/docs/control-protocol.md index 8af641a3a..126b8fd02 100644 --- a/docs/control-protocol.md +++ b/docs/control-protocol.md @@ -66,7 +66,7 @@ panel registers an element id; both are enforced by `crates/ui-egui/tests/it/ui_ |---|---|---| | `ui.inspect` | `{}` | Window size, pixels per point, fps, serialized `UiState`, playback, time, active comp, selection, open dialog, last render ms, `keyboardFocus`, `windowFocused`, `workspaceStorageError`. | | `ui.elements` | `{prefix?}` | `[{id, label, rect: [x, y, w, h]}]`: the widgets drawn in the last frame, in points. | -| `ui.set` | `{tool?, workspace?, theme?, focused?, viewer?, effectControlsLocked?, timeline?, menuBar?, home?}` | `viewer: {zoom (null = fit), res: Full\|Half\|Third\|Quarter\|Auto, pan: [x,y], grid, rulers, safeMargins, transparencyGrid}`; `effectControlsLocked: true` locks Effect Controls on the layer it shows (an error when it shows none), `false` unlocks it (the tab's lock icon, `panel.tab.EffectControls.lock`; `ui.inspect` reports it as `effect_controls_lock: {comp, layer}`); `timeline: {pps (null = fit), start, graphEditor, showModes, openLayers: [ids], openGroups: [uids]}`. | +| `ui.set` | `{tool?, workspace?, theme?, focused?, viewer?, effectControlsLocked?, timeline?, menuBar?, home?}` | `viewer: {zoom (null = fit), res: Full\|Half\|Third\|Quarter\|Auto, pan: [x,y], grid, rulers, safeMargins, transparencyGrid}`; `effectControlsLocked: true` locks Effect Controls on the layer it shows (an error when it shows none, or while it is locked to a layer of another composition), `false` unlocks it (the tab's lock icon, `panel.tab.EffectControls.lock`; `ui.inspect` reports it as `effect_controls_lock: {comp, layer}`); `timeline: {pps (null = fit), start, graphEditor, showModes, openLayers: [ids], openGroups: [uids]}`. | | `ui.panel.show` / `ui.panel.close` | `{panel}` | Panels: `Project`, `Composition`, `Timeline`, `EffectControls`, `EffectsPresets`, `Info`, `Preview`, `Character`, `Paragraph`, `Align` and others. | | `ui.playback` | `{action: play\|stop\|toggle\|status}` | `{playing, time, audio, levelsDb, peaksDb}` (audio: an audio preview is running; L/R meter levels and held peaks in dBFS) | | `ui.resize` | `{width, height}` | Resizes the window (points). | From df01e1d75c369b5ec52bbe60873b9ed12a0c07f5 Mon Sep 17 00:00:00 2001 From: vangel76 Date: Sat, 7 Nov 2026 18:26:04 +0100 Subject: [PATCH 25/74] Cryptomatte: a touch that lands on the layer and presses in one pass picks A touch, or a pen without hover, arrives on the layer and presses in the same pass. The overlay wasn't there the pass before, so egui handed the press to the viewer and nothing was picked. The overlay now takes such a press when it is on the layer and off the viewer's handles, and claims it from the viewer (CLAIMED_PRIMARY), which then neither clicks, double-clicks nor drags with it: Shift no longer toggles the layer off, and a drag moves nothing. The claim ends with the button up. --- crates/ui-egui/src/panels/crypto_pick.rs | 22 ++++++++--- crates/ui-egui/src/panels/viewer.rs | 24 +++++++++++- crates/ui-egui/tests/it/ui_crypto_select.rs | 41 +++++++++++++++++++++ 3 files changed, 79 insertions(+), 8 deletions(-) diff --git a/crates/ui-egui/src/panels/crypto_pick.rs b/crates/ui-egui/src/panels/crypto_pick.rs index e5e6dd88f..543fa6bdf 100644 --- a/crates/ui-egui/src/panels/crypto_pick.rs +++ b/crates/ui-egui/src/panels/crypto_pick.rs @@ -11,7 +11,8 @@ //! "Over the layer" is its bounding quad, as the viewer's own hit test (not its alpha), less the //! viewer's handles on it ([`super::viewer::grabs_at`]: bounding-box handles, mask vertices, //! motion-path keys, guides), which keep scaling, reshaping and moving. A press in the very pass -//! the pointer leaves the layer is passed on to the viewer as a click. While the effect is +//! the pointer leaves the layer is passed on to the viewer as a click; one in the pass it arrives +//! on the layer (a touch, a pen without hover) is claimed from the viewer and picks. While the effect is //! selected (or the eyedropper armed), a double-click on the layer picks (one step: the second //! click changes nothing) instead of opening its precomp or editing its text. //! @@ -307,7 +308,7 @@ fn viewer_pan_and_secondary(app: &mut EffectcraftApp, ctx: &egui::Context, resp: /// Whether Space / Hand / Zoom (`tools`) or middle/right buttons (`nav`) own the pointer, and /// whether a press at a point is the overlay's (`owned`: the Selection tool picking only over the -/// effect's layer, and only where the overlay took the press from the viewer). +/// effect's layer, off the viewer's own handles there). struct PointerNav<'a> { tools: bool, nav: bool, @@ -315,6 +316,10 @@ struct PointerNav<'a> { /// The overlay took the pass's presses from the viewer (it sensed clicks the pass before): /// one it does not own is passed on to the viewer ([`super::viewer::FORWARDED_PRIMARY`]). forward: bool, + /// The overlay did not sense clicks the pass before, so egui hands the pass's presses to the + /// viewer: one it owns (a touch, or a pen without hover, arriving on the layer and pressing in + /// one pass) is claimed from the viewer ([`super::viewer::CLAIMED_PRIMARY`]). + claim: bool, } /// Queues Cryptomatte picks from primary presses and releases on the armed eyedropper overlay. @@ -366,6 +371,10 @@ fn viewer_click_to_pick( let pass = ctx.cumulative_pass_nr(); ctx.data_mut(|d| d.insert_temp(egui::Id::unique(super::viewer::FORWARDED_PRIMARY), (pos, keys, pass))); } + // Handed to the viewer but ours: the viewer leaves it alone, so it only picks. + if pointer.claim && ours && !pointer.nav { + ctx.data_mut(|d| d.insert_temp(egui::Id::unique(super::viewer::CLAIMED_PRIMARY), ())); + } st.press = (!pointer.nav && ours).then(|| Press { pos, keys, @@ -637,8 +646,9 @@ pub fn viewer_hook( let panning = ctx.is_being_dragged(oid) && ctx.input(|i| i.pointer.middle_down()); let over = st.press.is_some() || panning || ctx.pointer_hover_pos().is_some_and(on_layer); let pass = ctx.cumulative_pass_nr(); - // A press is the overlay's only where it sensed clicks the pass before (egui handed it the - // press, not the viewer underneath, which would select or move the layer as well). + // Whether the overlay sensed clicks the pass before: egui handed it the pass's presses, not the + // viewer underneath (which would select or move the layer as well). If it didn't, a press of + // ours is claimed from the viewer; if it did, one not ours is passed on to the viewer. let owned_before = !implicit || st.sensed.is_some_and(|n| n.saturating_add(1) == pass); st.implicit = implicit; let mut reading = false; @@ -655,8 +665,8 @@ pub fn viewer_hook( } else { (st.asked, st.reply) = (None, None); } - let owned = |p: egui::Pos2| owned_before && on_layer(p); - viewer_click_to_pick(app, ui, map, ectx, &mut st, PointerNav { tools, nav, owned: &owned, forward: implicit && owned_before }, &at); + let pointer = PointerNav { tools, nav, owned: &on_layer, forward: implicit && owned_before, claim: implicit && !owned_before }; + viewer_click_to_pick(app, ui, map, ectx, &mut st, pointer, &at); // Clicks waiting for their probe ([`make_waiting`], at the end of the pass). reading |= !st.pending.is_empty(); draw_coverage_highlight(app, &ctx, painter, map, m, &mut st); diff --git a/crates/ui-egui/src/panels/viewer.rs b/crates/ui-egui/src/panels/viewer.rs index d653dc662..5886f722e 100644 --- a/crates/ui-egui/src/panels/viewer.rs +++ b/crates/ui-egui/src/panels/viewer.rs @@ -72,6 +72,17 @@ pub(crate) const FORWARDED_SECONDARY: &str = "viewer-forwarded-secondary"; /// the pointer left the layer in the pass of the press): the viewer clicks there on the next pass. pub(crate) const FORWARDED_PRIMARY: &str = "viewer-forwarded-primary"; +/// The egui data key (`()`) of a primary press an overlay claimed although egui handed it to the +/// viewer (the Selection tool's Cryptomatte picking, #2839, when the pointer arrived on the layer +/// and pressed in one pass, as a touch or a pen without hover does): the viewer neither clicks +/// nor drags with it, and lets go of the claim once the button is up. +pub(crate) const CLAIMED_PRIMARY: &str = "viewer-claimed-primary"; + +/// Whether an overlay claimed the primary press in progress ([`CLAIMED_PRIMARY`]). +fn press_claimed(ctx: &egui::Context) -> bool { + ctx.data(|d| d.get_temp::<()>(egui::Id::unique(CLAIMED_PRIMARY))).is_some() +} + /// What the viewer's own handles reach this pass, for overlays drawn on top of it. #[derive(Clone, Default)] struct Grabs { @@ -1203,6 +1214,7 @@ fn phase_dragstart(app: &mut EffectcraftApp, ui: &mut egui::Ui, ectx: &EvalCtx, }; if resp.drag_started() && !text_owns + && !press_claimed(ui.ctx()) && let Some(pos) = resp.interact_pointer_pos() { let cpt = map.to_comp(pos); @@ -1804,7 +1816,9 @@ fn phase_clicked(app: &mut EffectcraftApp, ectx: &EvalCtx, resp: &egui::Response .data_mut(|d| d.remove_temp::<(Pos2, egui::Modifiers, u64)>(egui::Id::unique(FORWARDED_PRIMARY))) .filter(|f| f.2.saturating_add(1) == pass) .map(|f| (f.0, f.1)); - let clicked = if resp.clicked() && !text_owns { resp.interact_pointer_pos().map(|p| (p, mods)) } else { None }; + // A press an overlay claimed (CLAIMED_PRIMARY) is not a click here. + let claimed = press_claimed(&ctx); + let clicked = if resp.clicked() && !text_owns && !claimed { resp.interact_pointer_pos().map(|p| (p, mods)) } else { None }; if let Some((pos, mods)) = clicked.or(forwarded) { let cpt = map.to_comp(pos); match app.ui.tool { @@ -1914,8 +1928,14 @@ fn phase_clicked(app: &mut EffectcraftApp, ectx: &EvalCtx, resp: &egui::Response fn phase_dblclick(app: &mut EffectcraftApp, ectx: &EvalCtx, resp: &egui::Response, v: &mut Vw<'_>) { let map = v.map; - let text_owns = v.text_owns; let ctx = v.ctx.clone(); + // A press an overlay claimed (CLAIMED_PRIMARY) is no double-click here either; the claim + // ends with the button up (after this pass's click and double-click). + let claimed = press_claimed(&ctx); + if claimed && !ctx.input(|i| i.pointer.primary_down()) { + ctx.data_mut(|d| d.remove::<()>(egui::Id::unique(CLAIMED_PRIMARY))); + } + let text_owns = v.text_owns || claimed; let handle_hits = std::mem::take(&mut v.handle_hits); let vertex_hits = std::mem::take(&mut v.vertex_hits); let paths = std::mem::take(&mut v.paths); diff --git a/crates/ui-egui/tests/it/ui_crypto_select.rs b/crates/ui-egui/tests/it/ui_crypto_select.rs index f4b693556..b1b716e24 100644 --- a/crates/ui-egui/tests/it/ui_crypto_select.rs +++ b/crates/ui-egui/tests/it/ui_crypto_select.rs @@ -286,3 +286,44 @@ fn after_the_eyedropper_and_esc_the_selection_tool_does_not_pick() { assert!(!h.state().session.crypto_probe_stats().0, "no IDs kept read"); let _ = std::fs::remove_dir_all(&ids.dir); } + +/// A touch (or a pen without hover) arrives on the layer and presses in one pass: the overlay +/// wasn't there the pass before, so egui hands the press to the viewer. It is still a pick (and +/// Shift still adds), and the viewer neither selects, toggles nor moves the layer with it. +#[test] +fn a_touch_that_arrives_and_presses_in_one_pass_picks() { + let (s, ids) = session("touch"); + let mut h = harness(s); + let (steps, before) = (undo_steps(&h), position(&h, ids.plate)); + let (a, b) = (on_plate(&h, 50.0, 40.0), on_plate(&h, 120.0, 100.0)); + let touch = |h: &mut Harness<'_, EffectcraftApp>, pos: Pos2, modifiers: Modifiers, by: egui::Vec2| { + h.event(Event::PointerGone); + h.run_steps(2); + let ev = &mut h.input_mut().events; + ev.push(Event::ModifiersChanged(modifiers)); + ev.push(Event::PointerMoved(pos)); + ev.push(Event::PointerButton { pos, button: PointerButton::Primary, pressed: true, modifiers }); + h.step(); + for k in 1..=4 { + h.event(Event::PointerMoved(pos + by * k as f32 / 4.0)); + h.step(); + } + h.event(Event::PointerButton { pos: pos + by, button: PointerButton::Primary, pressed: false, modifiers }); + h.step(); + h.event(Event::ModifiersChanged(Modifiers::NONE)); + h.event(Event::PointerGone); + h.step(); + wait_probe(h); + h.run_steps(2); + }; + touch(&mut h, a, Modifiers::NONE, vec2(0.0, 0.0)); + assert_eq!(selection(&h, &ids), "c1_1", "the touch picked"); + touch(&mut h, b, Modifiers::SHIFT, vec2(0.0, 0.0)); + assert_eq!(selection(&h, &ids), "c1_1, c3_3", "Shift added"); + assert_eq!(selected_layers(&h), vec![ids.plate], "the viewer didn't toggle the layer"); + // A touch that drags moves nothing (and picks nothing). + touch(&mut h, a, Modifiers::NONE, vec2(120.0, 40.0)); + assert_eq!((position(&h, ids.plate), undo_steps(&h)), (before, steps + 2), "the layer stays put"); + assert_eq!(selection(&h, &ids), "c1_1, c3_3"); + let _ = std::fs::remove_dir_all(&ids.dir); +} From 691befb201660bdacf672d9a8ee3dbfa1cf6b02b Mon Sep 17 00:00:00 2001 From: vangel76 Date: Sat, 7 Nov 2026 19:45:59 +0100 Subject: [PATCH 26/74] Cryptomatte selection: doc comments for the functions this change touches --- crates/ui-egui/src/panels/viewer.rs | 9 +++++++++ crates/ui-egui/tests/it/ui_crypto_pick_undo.rs | 2 ++ crates/ui-egui/tests/it/ui_crypto_select.rs | 6 ++++++ 3 files changed, 17 insertions(+) diff --git a/crates/ui-egui/src/panels/viewer.rs b/crates/ui-egui/src/panels/viewer.rs index 5886f722e..05e9b23d7 100644 --- a/crates/ui-egui/src/panels/viewer.rs +++ b/crates/ui-egui/src/panels/viewer.rs @@ -96,6 +96,7 @@ struct Grabs { guides: Vec<(f32, bool)>, } +/// The egui data key of the viewer's [`Grabs`] for the current pass. fn grabs_id() -> egui::Id { egui::Id::unique("viewer-grabs") } @@ -1180,6 +1181,8 @@ fn led_by_a_moved_ancestor(comp: &Comp, layers: &[MoveLayer]) -> Vec { layers.iter().map(|l| led(l.0)).collect() } +/// Starts the gesture a new primary drag begins (pan, move, scale, a handle, a vertex, a marquee), +/// unless an overlay claimed the press. fn phase_dragstart(app: &mut EffectcraftApp, ui: &mut egui::Ui, ectx: &EvalCtx, resp: &egui::Response, v: &mut Vw<'_>) { let comp_rect = v.comp_rect; let zoom = v.zoom; @@ -1780,6 +1783,8 @@ fn phase_stopped(app: &mut EffectcraftApp, ui: &mut egui::Ui, ectx: &EvalCtx, re v.pin_hits = pin_hits; } +/// Acts on a click in the viewer for the active tool (select, zoom, add or delete vertices), +/// including a click an overlay passed on, and not one an overlay claimed. fn phase_clicked(app: &mut EffectcraftApp, ectx: &EvalCtx, resp: &egui::Response, v: &mut Vw<'_>) { let area = v.area; let zoom = v.zoom; @@ -1926,6 +1931,8 @@ fn phase_clicked(app: &mut EffectcraftApp, ectx: &EvalCtx, resp: &egui::Response v.puppet_layers = puppet_layers; } +/// Acts on a double-click: a path's points into a Free Transform box, or a precomp opened; lets +/// go of an overlay's claim on the press once the button is up. fn phase_dblclick(app: &mut EffectcraftApp, ectx: &EvalCtx, resp: &egui::Response, v: &mut Vw<'_>) { let map = v.map; let ctx = v.ctx.clone(); @@ -1986,6 +1993,8 @@ fn phase_dblclick(app: &mut EffectcraftApp, ectx: &EvalCtx, resp: &egui::Respons v.paths = paths; } +/// Runs the overlays drawn on top of the viewer (effect, gradient and text handles), after +/// recording the viewer's own controls for them, and draws the status chips. fn phase_tail(app: &mut EffectcraftApp, ui: &mut egui::Ui, ectx: &EvalCtx, v: &mut Vw<'_>) { let t = v.t; let cid = v.cid; diff --git a/crates/ui-egui/tests/it/ui_crypto_pick_undo.rs b/crates/ui-egui/tests/it/ui_crypto_pick_undo.rs index a35743ac0..7780dac0c 100644 --- a/crates/ui-egui/tests/it/ui_crypto_pick_undo.rs +++ b/crates/ui-egui/tests/it/ui_crypto_pick_undo.rs @@ -452,6 +452,8 @@ enum Step { Blur(u64, f64), } +/// Performs one model step (a pick, an edit, undo or redo, applying or removing the effect) and +/// records the state the model expects after it. fn run_step(h: &mut Harness<'_, EffectcraftApp>, f: &Fix, m: &mut Model, size: [f64; 2], step: &Step, n: usize) { let mut o = m.state().clone(); let before = h.state().session.history.undo.len(); diff --git a/crates/ui-egui/tests/it/ui_crypto_select.rs b/crates/ui-egui/tests/it/ui_crypto_select.rs index b1b716e24..9331913d4 100644 --- a/crates/ui-egui/tests/it/ui_crypto_select.rs +++ b/crates/ui-egui/tests/it/ui_crypto_select.rs @@ -73,6 +73,7 @@ fn on_plate(h: &Harness<'_, EffectcraftApp>, x: f64, y: f64) -> Pos2 { comp_to_screen(&h.ctx, [(80.0 + x) as f32, y as f32]).unwrap() } +/// Moves the pointer to `pos`, waits until the Cryptomatte IDs are read, and returns the chip's text. fn hover(h: &mut Harness<'_, EffectcraftApp>, pos: Pos2) -> Option { h.event(Event::PointerMoved(pos)); // The overlay comes up over the layer in this pass and is hovered from the next one on. @@ -97,6 +98,7 @@ fn click(h: &mut Harness<'_, EffectcraftApp>, pos: Pos2, modifiers: Modifiers) { h.run_steps(2); } +/// Presses at `from`, drags by `by` over a few passes and releases. fn drag(h: &mut Harness<'_, EffectcraftApp>, from: Pos2, by: egui::Vec2) { h.event(Event::PointerMoved(from)); h.step(); @@ -110,6 +112,7 @@ fn drag(h: &mut Harness<'_, EffectcraftApp>, from: Pos2, by: egui::Vec2) { h.run_steps(3); } +/// The plate's Cryptomatte Selection text. fn selection(h: &Harness<'_, EffectcraftApp>, ids: &Ids) -> String { let s = &h.state().session; match &s.active_comp().unwrap().layer(LayerId(ids.plate)).unwrap().props.find(ids.sel).unwrap().value { @@ -118,15 +121,18 @@ fn selection(h: &Harness<'_, EffectcraftApp>, ids: &Ids) -> String { } } +/// The layer's Position value, formatted for comparison. fn position(h: &Harness<'_, EffectcraftApp>, layer: u64) -> String { let l = h.state().session.active_comp().unwrap().layer(LayerId(layer)).unwrap().clone(); format!("{:?}", l.transform().and_then(|t| t.get("position")).map(|p| p.value.clone())) } +/// How many steps the undo history holds. fn undo_steps(h: &Harness<'_, EffectcraftApp>) -> usize { h.state().session.history.undo.len() } +/// The ids of the selected layers. fn selected_layers(h: &Harness<'_, EffectcraftApp>) -> Vec { h.state().session.state.selected_layers.iter().map(|l| l.0).collect() } From 12264b4e6380bb9104f03bd3a9105c03911f1316 Mon Sep 17 00:00:00 2001 From: vangel76 Date: Sat, 7 Nov 2026 19:47:55 +0100 Subject: [PATCH 27/74] Doc comments for the functions this PR touches --- crates/ui-egui/src/panels/effect_controls/common.rs | 1 + crates/ui-egui/tests/it/ui_effect_controls.rs | 5 +++++ 2 files changed, 6 insertions(+) diff --git a/crates/ui-egui/src/panels/effect_controls/common.rs b/crates/ui-egui/src/panels/effect_controls/common.rs index 1e7d7cba1..31e884b98 100644 --- a/crates/ui-egui/src/panels/effect_controls/common.rs +++ b/crates/ui-egui/src/panels/effect_controls/common.rs @@ -40,6 +40,7 @@ pub(crate) fn shown_layer_id(app: &EffectcraftApp) -> Option Option { let id = shown_layer_id(app)?; app.session.active_comp()?.layer(id).cloned() diff --git a/crates/ui-egui/tests/it/ui_effect_controls.rs b/crates/ui-egui/tests/it/ui_effect_controls.rs index ec7d5683d..b79aade0d 100644 --- a/crates/ui-egui/tests/it/ui_effect_controls.rs +++ b/crates/ui-egui/tests/it/ui_effect_controls.rs @@ -936,6 +936,7 @@ fn shows_effect(h: &Harness<'_, EffectcraftApp>, fx: u64) -> bool { h.state().auto.elements.iter().any(|e| e.id == id) } +/// Selects `layer` through the `layer.select` command and runs a few frames. fn select_layer(h: &mut Harness<'_, EffectcraftApp>, layer: u64) { h.state_mut().session.execute("layer.select", json!({"layers": [layer]})).unwrap(); h.run_steps(3); @@ -1018,6 +1019,7 @@ fn replacing_the_project_unlocks_effect_controls() { #[test] fn a_lock_request_with_no_layer_shown_fails_and_keeps_the_other_comps_lock() { use effectcraft_ui_egui::control::{self, ControlRequest, Outcome}; + /// Sends a `ui.set` request that locks Effect Controls and returns the immediate reply. fn send(h: &mut Harness<'_, EffectcraftApp>) -> serde_json::Value { let ctx = h.ctx.clone(); let (req, _rx) = ControlRequest::new("ui.set", json!({"effectControlsLocked": true})); @@ -1048,6 +1050,7 @@ fn a_lock_request_with_no_layer_shown_fails_and_keeps_the_other_comps_lock() { #[test] fn a_lock_request_while_locked_in_another_comp_fails() { use effectcraft_ui_egui::control::{self, ControlRequest, Outcome}; + /// Sends a `ui.set` request that locks Effect Controls and returns the immediate reply. fn send(h: &mut Harness<'_, EffectcraftApp>) -> serde_json::Value { let ctx = h.ctx.clone(); let (req, _rx) = ControlRequest::new("ui.set", json!({"effectControlsLocked": true})); @@ -1079,6 +1082,7 @@ fn a_lock_request_while_locked_in_another_comp_fails() { #[test] fn a_lock_request_after_a_project_replacement_does_not_report_the_old_lock() { use effectcraft_ui_egui::control::{self, ControlRequest, Outcome}; + /// Sends a `ui.set` request that locks Effect Controls and returns the immediate reply. fn send(h: &mut Harness<'_, EffectcraftApp>) -> serde_json::Value { let ctx = h.ctx.clone(); let (req, _rx) = ControlRequest::new("ui.set", json!({"effectControlsLocked": true})); @@ -1129,6 +1133,7 @@ fn applying_an_effect_while_locked_keeps_the_selection() { #[test] fn effect_controls_lock_over_the_control_channel() { use effectcraft_ui_egui::control::{self, ControlRequest, Outcome}; + /// Sends a `ui.set` request with `params` and returns the immediate reply. fn send(h: &mut Harness<'_, EffectcraftApp>, params: serde_json::Value) -> serde_json::Value { let ctx = h.ctx.clone(); let (req, _rx) = ControlRequest::new("ui.set", params); From 6739afd3d6456eb25c18681b6def37fba5290bbd Mon Sep 17 00:00:00 2001 From: vangel76 Date: Sat, 7 Nov 2026 19:48:27 +0100 Subject: [PATCH 28/74] Doc comments for the functions this PR touches --- crates/engine/src/tests/track.rs | 1 + crates/script/src/model.rs | 1 + 2 files changed, 2 insertions(+) diff --git a/crates/engine/src/tests/track.rs b/crates/engine/src/tests/track.rs index bd1adaeca..52c8a2bc0 100644 --- a/crates/engine/src/tests/track.rs +++ b/crates/engine/src/tests/track.rs @@ -440,6 +440,7 @@ fn analyze_1080p_single_point_speed() { struct SampleFootage; impl crate::render::ExprHost for SampleFootage { + /// Reads the red channel of the layer's footage pixel at (0, 0) through the context's footage source, scaled to 0-100. fn eval( &self, ctx: &EvalCtx, diff --git a/crates/script/src/model.rs b/crates/script/src/model.rs index 694d82d39..a7476f370 100644 --- a/crates/script/src/model.rs +++ b/crates/script/src/model.rs @@ -510,6 +510,7 @@ fn text_json(d: &TextDoc) -> J { doc } +/// Returns the property's value at the requested time, evaluating its expression unless `pre` is set. fn value(s: &Session, a: &J) -> R { let (cid, c, l) = layer_of(s, a)?; let uid = u(a, "uid").ok_or("missing uid")?; From 3ab35d0ff656530cd873c5cf293514ddfaa8b4b3 Mon Sep 17 00:00:00 2001 From: vangel76 Date: Sat, 7 Nov 2026 19:49:24 +0100 Subject: [PATCH 29/74] Doc comments for the functions this PR touches --- crates/engine/src/cmd/prop.rs | 1 + crates/ui-egui/src/num_math.rs | 7 +++++++ crates/ui-egui/src/panels/effect_controls/rows.rs | 4 ++++ 3 files changed, 12 insertions(+) diff --git a/crates/engine/src/cmd/prop.rs b/crates/engine/src/cmd/prop.rs index 028fdb0dd..dec791b88 100644 --- a/crates/engine/src/cmd/prop.rs +++ b/crates/engine/src/cmd/prop.rs @@ -1457,6 +1457,7 @@ fn convert_one_expr(s: &mut Session, cid: ItemId, lid: LayerId, uid: Uid) -> Res }) } +/// Returns the command specs for the property commands (get, set, keyframes, expressions and the rest). pub fn specs() -> Vec { vec![ query!("prop.get", "Get Property", "{layer?, path|prop, time? (comp s)}", get), diff --git a/crates/ui-egui/src/num_math.rs b/crates/ui-egui/src/num_math.rs index 3e329c9e4..68225511c 100644 --- a/crates/ui-egui/src/num_math.rs +++ b/crates/ui-egui/src/num_math.rs @@ -126,6 +126,8 @@ struct Parser<'a> { } impl Parser<'_> { + /// Reads a sum or difference of terms, left to right. + /// Returns `None` on a syntax error or a non-finite result. fn expr(&mut self) -> Option { let mut v = self.term()?; while let Some(&op) = self.chars.peek() { @@ -144,6 +146,8 @@ impl Parser<'_> { Some(v) } + /// Reads a product or quotient of factors, left to right. + /// Returns `None` on a syntax error, a division by zero or a non-finite result. fn term(&mut self) -> Option { let mut v = self.factor()?; while let Some(&op) = self.chars.peek() { @@ -162,6 +166,8 @@ impl Parser<'_> { Some(v) } + /// Reads a signed factor, a bracketed expression or a number. + /// Nesting deeper than `MAX_DEPTH` gives `None`. fn factor(&mut self) -> Option { let c = *self.chars.peek()?; match c { @@ -239,6 +245,7 @@ impl Parser<'_> { mod tests { use super::calc as eval; + /// Evaluates `text` with no current value, so a leading operator has nothing to apply to. fn abs(text: &str) -> Option { eval(text, None) } diff --git a/crates/ui-egui/src/panels/effect_controls/rows.rs b/crates/ui-egui/src/panels/effect_controls/rows.rs index e2634d6b8..b21021bb3 100644 --- a/crates/ui-egui/src/panels/effect_controls/rows.rs +++ b/crates/ui-egui/src/panels/effect_controls/rows.rs @@ -260,6 +260,7 @@ fn value_mask(app: &mut EffectcraftApp, ui: &mut egui::Ui, cx: &PropCx, value: & } } +/// Draws an angle property's value cell as revolutions plus degrees and queues a set action when it is edited. fn value_angle(app: &mut EffectcraftApp, ui: &mut egui::Ui, cx: &PropCx, value: &Value, actions: &mut Actions) { let Value::Scalar(v) = value else { return; @@ -275,6 +276,7 @@ fn value_angle(app: &mut EffectcraftApp, ui: &mut egui::Ui, cx: &PropCx, value: cx.twirl_open(app, ui); } +/// Draws a single-number property's value cell with its range, unit suffix and scrub speed, and queues a set action when it is edited. fn value_scalar(app: &mut EffectcraftApp, ui: &mut egui::Ui, cx: &PropCx, value: &Value, actions: &mut Actions) { let Value::Scalar(v) = value else { return; @@ -308,6 +310,8 @@ fn value_scalar(app: &mut EffectcraftApp, ui: &mut egui::Ui, cx: &PropCx, value: } } +/// Draws one numeric cell per component of a point or vector property, with the pick crosshair for points. +/// Returns the cell rectangles. fn value_vector(app: &mut EffectcraftApp, ui: &mut egui::Ui, cx: &PropCx, value: &Value, actions: &mut Actions) -> Vec { let mut dims = vec![]; let PropCx { t, uid, cy, vx, layer, prop, .. } = *cx; From 2a00e26be5f6ea39fcfc007ad420e0d7f1e61780 Mon Sep 17 00:00:00 2001 From: vangel76 Date: Sat, 7 Nov 2026 19:50:27 +0100 Subject: [PATCH 30/74] Doc comments for the functions this PR touches --- crates/render/src/cache/dependencies.rs | 2 ++ crates/render/src/pipeline.rs | 2 ++ crates/render/src/tests/frame_blend.rs | 6 ++++++ 3 files changed, 10 insertions(+) diff --git a/crates/render/src/cache/dependencies.rs b/crates/render/src/cache/dependencies.rs index c8c483f99..c1ff20565 100644 --- a/crates/render/src/cache/dependencies.rs +++ b/crates/render/src/cache/dependencies.rs @@ -122,6 +122,8 @@ impl Dependencies<'_> { Some(()) } + /// Folds what the layer contributes at this time into the dependency cache key, including its source, effects and frame blend state. + /// Returns `None` when the layer cannot be keyed: past the depth limit, in a reference cycle, or with expressions that read other times. fn layer(&mut self, ctx: &EvalCtx, layer: &Layer) -> Option<()> { let id = (ctx.comp_id, layer.id); if self.path.len() > crate::MAX_FX_DEPTH || self.path.contains(&id) { diff --git a/crates/render/src/pipeline.rs b/crates/render/src/pipeline.rs index 6012a7404..89ce84f7f 100644 --- a/crates/render/src/pipeline.rs +++ b/crates/render/src/pipeline.rs @@ -861,6 +861,8 @@ impl<'a> Renderer<'a> { if per_pixel { Some(Arc::new(interpret_pixels(&img, f, self.pipe.linear_footage_space(f)))) } else { Some(img) } } + /// The footage frame at source time `t` after frame blending: the average of the source frames one comp frame spans, else a blend of the two neighbouring frames, else the plain frame. + /// Log scans are converted first, and blended frames are cached. fn footage_frame_raw(&self, ctx: &EvalCtx, layer: &Layer, item: ItemId, f: &Footage, t: Tick, size: Option<(u32, u32)>) -> Option> { let mode = frame_blend_mode(ctx, layer); let float = self.pipe.float_curve(f); diff --git a/crates/render/src/tests/frame_blend.rs b/crates/render/src/tests/frame_blend.rs index e80731d35..ecee73b6d 100644 --- a/crates/render/src/tests/frame_blend.rs +++ b/crates/render/src/tests/frame_blend.rs @@ -58,6 +58,7 @@ impl FootageSource for Frames { } } +/// [`setup_rated`] with 24 fps footage and no recorded native rate. fn setup(mode: FrameBlend, size: (u32, u32)) -> (Project, ItemId) { setup_rated(mode, size, FrameRate::FPS_24, None) } @@ -174,6 +175,7 @@ fn time_stretched_precomp_frame_mixes() { assert!(img.get(12, 10)[3] < 1e-3 && (img.get(17, 10)[3] - 1.0).abs() < 1e-3 && (img.get(22, 10)[3] - 1.0).abs() < 1e-3 && img.get(27, 10)[3] < 1e-3); } +/// Fast-conformed footage (30 fps interpreted as 300 fps) averages every source frame a comp frame spans, for Frame Mix and Pixel Motion. #[test] fn frame_mix_averages_every_frame_of_fast_conformed_footage() { // #3180: 30 fps footage interpreted as 300 fps in a 30 fps comp. Every comp frame lands @@ -195,6 +197,7 @@ fn frame_mix_averages_every_frame_of_fast_conformed_footage() { assert!(frame(&p, cid, 0, false).get(4, 4)[0].abs() < 1e-6); } +/// Footage at the comp's rate, or only mildly faster, renders as before. #[test] fn frame_mix_of_common_footage_is_unchanged() { // Footage at the comp's rate: each comp frame is exactly one source frame, blended or not. @@ -226,6 +229,7 @@ fn remap(p: &mut Project, cid: ItemId, keys: Vec) { p.comp_mut(cid).unwrap().layers[0].props.children.push(effectcraft_project::Node::Prop(tr)); } +/// Time remap holds and cuts do not smear: a still frame stays a single source frame. #[test] fn time_remap_jumps_stay_crisp() { let fast = FrameRate::new(300, 1); @@ -253,6 +257,7 @@ fn time_remap_jumps_stay_crisp() { assert!((v - 0.45).abs() < 1e-5, "steady remap: {v}"); } +/// A frame-blended dependency changes its owner's cache key when its source goes from holding to running at speed. #[test] fn a_frame_blended_dependency_keys_how_far_its_source_runs() { // A footage layer selected by another layer's effect: at comp frames 0 and 1 its source time @@ -276,6 +281,7 @@ fn a_frame_blended_dependency_keys_how_far_its_source_runs() { assert_ne!(key(0), key(1)); } +/// The mix window is the source span of one comp frame, and falls back to the two-frame blend below four source frames per comp frame. #[test] fn mix_window_covers_the_source_span_of_one_comp_frame() { let comp_frame = |r: FrameRate| r.frame_duration(); From 7dc345c4c2343f4e17b8cd0ea2a6b616e3c19a87 Mon Sep 17 00:00:00 2001 From: vangel76 Date: Sat, 7 Nov 2026 19:50:40 +0100 Subject: [PATCH 31/74] Doc comments for the functions this PR touches --- crates/script/src/model.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/crates/script/src/model.rs b/crates/script/src/model.rs index 1140820c6..3cf25ed81 100644 --- a/crates/script/src/model.rs +++ b/crates/script/src/model.rs @@ -32,6 +32,7 @@ fn layer_of<'a>(s: &'a Session, a: &J) -> Result<(ItemId, &'a Comp, &'a Layer), Ok((cid, c, l)) } +/// Answers a script model query of the given `kind` against the session, dispatching to the matching reader, and errors on an unknown kind. pub fn query(s: &mut Session, kind: &str, a: &J) -> R { match kind { "project" => Ok(project(s)), From 25a6a76c47e39e588a3f2655d9ad434bcc56ea1c Mon Sep 17 00:00:00 2001 From: vangel76 Date: Sat, 7 Nov 2026 19:50:42 +0100 Subject: [PATCH 32/74] Doc comments for the functions this PR touches --- crates/engine/src/tests/viewer.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/crates/engine/src/tests/viewer.rs b/crates/engine/src/tests/viewer.rs index d2c2180c1..431b9e735 100644 --- a/crates/engine/src/tests/viewer.rs +++ b/crates/engine/src/tests/viewer.rs @@ -194,6 +194,7 @@ fn circles_snap_point_to_point(bezier: bool) { assert_ne!(a, b, "two shape layers"); if bezier { for l in [a, b] { + /// Returns the uid of the first ellipse group in the tree, searching depth first. fn ellipse(g: &effectcraft_project::PropGroup) -> Option { if g.match_id == "ellipse" { return Some(g.uid); From 7694c1117056d52fce675fab477531822559d807 Mon Sep 17 00:00:00 2001 From: vangel76 Date: Sun, 8 Nov 2026 03:10:16 +0100 Subject: [PATCH 33/74] aep: a layer's markers and Source Text are read where they are stored, not searched for (#3276) A layer's marker list is a direct child of its root tdgp, and its Source Text record btds a direct child of the ADBE Text Properties group. Markers walked the whole layer subtree twice per layer (most layers have no marker list, so the walk always ran to the end), and the three text-document accessors walked it once each. --- crates/aep/src/container/textdoc.rs | 75 ++++++++++++++++++++++++++--- crates/aep/src/import/markers.rs | 2 +- 2 files changed, 68 insertions(+), 9 deletions(-) diff --git a/crates/aep/src/container/textdoc.rs b/crates/aep/src/container/textdoc.rs index 566a48534..51b15d7df 100644 --- a/crates/aep/src/container/textdoc.rs +++ b/crates/aep/src/container/textdoc.rs @@ -881,23 +881,31 @@ impl TextDocument { } impl<'f, 'a> Layer<'f, 'a> { - /// The text document of a text layer (`LIST btdk` below `ADBE Text Properties`). + /// The Source Text record `LIST btds` (`ADBE Text Document`). It is a direct child of the + /// `ADBE Text Properties` group, which is a direct child of the layer's root `tdgp` + /// (`Layr/tdgp/tdgp/btds`, in every sample), so only that level is searched rather than the + /// whole layer. + fn source_text_record(&self) -> Option<&'f Chunk<'a>> { + let root = self.chunk.child_list(b"tdgp")?; + root.children().iter().filter(|g| g.is_list(b"tdgp")).find_map(|g| g.child_list(b"btds")) + } + + /// The text document of a text layer (`LIST btdk` in the Source Text record `btds`). pub fn text_document(&self) -> Option { - TextDocument::parse(self.chunk.find_list(b"btdk")?.bytes()?) + TextDocument::parse(self.source_text_record()?.child_list(b"btdk")?.bytes()?) } /// The Source Text property record: the `tdbs` in `btds[ADBE Text Document]` (a placeholder /// in every sample: no keys, no expression). Source Text keyframes and expressions were not /// sampled; a `list` (keys) or an expression in it marks them. pub fn source_text_property(&self) -> Option> { - let btds = self.chunk.descendants().map(|(_, c)| c).find(|c| c.is_list(b"btds"))?; - Property::from_chunk(btds.child_list(b"tdbs")?) + Property::from_chunk(self.source_text_record()?.child_list(b"tdbs")?) } - /// The number of text documents (`LIST btdk`) of the layer: 1 in every sample; more would be - /// one per Source Text keyframe (not sampled). + /// The number of text documents (`LIST btdk`) in the Source Text record: 1 in every sample; + /// more would be one per Source Text keyframe (not sampled). pub fn text_document_count(&self) -> usize { - self.chunk.descendants().filter(|(_, c)| c.is_list(b"btdk")).count() + self.source_text_record().map_or(0, |btds| btds.descendants().filter(|(_, c)| c.is_list(b"btdk")).count()) } } @@ -942,10 +950,23 @@ pub enum CueType { Other(u32), } +impl<'a> Chunk<'a> { + /// The marker list `LIST mrst` of a marker holder: a layer (`LIST Layr`) or a composition's + /// `SecL` list. A layer's `mrst` is a direct child of its root `tdgp` (`ADBE Marker`), so only + /// that place is looked at; most layers have none, and searching their whole property tree + /// for it was a large part of an import. Any other holder is searched depth-first. + pub(crate) fn marker_list(&self) -> Option<&Chunk<'a>> { + if self.is_list(b"Layr") { + return self.child_list(b"tdgp")?.child_list(b"mrst"); + } + self.find_list(b"mrst") + } +} + /// Reads the markers below `holder` (a layer or the composition's `SecL` list): the `mrst` list has a /// property whose 16-byte records give the times and one `Nmrd` list per marker with the strings. fn markers_in(holder: &Chunk<'_>) -> Vec { - let Some(mrst) = holder.find_list(b"mrst") else { return Vec::new() }; + let Some(mrst) = holder.marker_list() else { return Vec::new() }; let Some(prop) = mrst.child_list(b"tdbs").and_then(Property::from_chunk) else { return Vec::new() }; let Some(keys) = prop.keys() else { return Vec::new() }; let scale = prop.time_scale().unwrap_or(0); @@ -1006,6 +1027,44 @@ impl<'f, 'a> Comp<'f, 'a> { mod tests { use super::*; + fn list(t: &[u8; 4], kids: Vec>) -> Chunk<'static> { + Chunk::list(*t, kids) + } + + #[test] + fn a_layers_marker_list_is_the_one_in_its_root_group() { + let marked = |tag: u8| list(b"mrst", vec![Chunk::data(*b"test", vec![tag])]); + // The stored place: `Layr/tdgp/mrst`. + let layer = list(b"Layr", vec![list(b"tdgp", vec![list(b"tdgp", vec![]), marked(1)])]); + assert_eq!(layer.marker_list().and_then(|m| m.child(b"test")).and_then(Chunk::bytes), Some(&[1u8][..])); + // A list deeper in the layer is not the layer's marker list (the old depth-first search + // found it); a layer without a marker list in its root group has no markers. + let deep = list(b"Layr", vec![list(b"tdgp", vec![list(b"tdgp", vec![marked(2)])])]); + assert!(deep.marker_list().is_none()); + assert!(Layer { chunk: &deep }.markers().is_empty()); + // Any other holder (a composition's `SecL`) is still searched depth-first. + let secl = list(b"SecL", vec![list(b"tdgp", vec![list(b"tdgp", vec![marked(3)])])]); + assert_eq!(secl.marker_list().and_then(|m| m.child(b"test")).and_then(Chunk::bytes), Some(&[3u8][..])); + } + + #[test] + fn the_source_text_record_is_found_in_the_text_group() { + let doc = b"/1 << /1 [ << /0 << /0 (\xfe\xff\0H\0\r) /6 << /0 [ << /0 << /0 << /6 << /0 0 /1 50 /53 << /0 << /1 [ 1 1 0 0 ] >> >> >> >> >> /1 2 >> ] >> >> >> ] >>"; + // `btdk` is an opaque list (its body is not a chunk sequence), as the reader makes it. + let btdk = || Chunk { id: *b"LIST", body: crate::container::Body::OpaqueList { list_type: *b"btdk", data: doc.to_vec().into() }, pad: None }; + let btds_real = |n: usize| list(b"btds", std::iter::once(list(b"tdbs", vec![])).chain((0..n).map(|_| btdk())).collect()); + let layer = list(b"Layr", vec![list(b"tdgp", vec![list(b"tdgp", vec![]), list(b"tdgp", vec![btds_real(2)])])]); + let view = Layer { chunk: &layer }; + assert_eq!(view.text_document().map(|d| d.text), Some("H".to_string())); + assert!(view.source_text_property().is_some()); + assert_eq!(view.text_document_count(), 2); + // A record anywhere else in the layer is not its Source Text. + let elsewhere = list(b"Layr", vec![list(b"tdgp", vec![list(b"tdgp", vec![list(b"tdgp", vec![btds_real(1)])])])]); + let view = Layer { chunk: &elsewhere }; + assert!(view.text_document().is_none() && view.source_text_property().is_none()); + assert_eq!(view.text_document_count(), 0); + } + #[test] fn non_finite_numbers_are_not_numbers() { let TextValue::Dict(pairs) = parse_text_dict(b"/1 inf /2 nan /3 1e999 /4 -inf /5 2.5 /6 7") else { panic!("a dict") }; diff --git a/crates/aep/src/import/markers.rs b/crates/aep/src/import/markers.rs index b65840c69..93faea90a 100644 --- a/crates/aep/src/import/markers.rs +++ b/crates/aep/src/import/markers.rs @@ -24,7 +24,7 @@ use crate::import::time::TimeMap; /// The time scale of the marker list below `holder` (the layer, or the composition's `SecL`). fn marker_scale(holder: &Chunk<'_>) -> Option { - holder.find_list(b"mrst")?.child_list(b"tdbs").and_then(AepProp::from_chunk)?.time_scale().filter(|s| *s != 0) + holder.marker_list()?.child_list(b"tdbs").and_then(AepProp::from_chunk)?.time_scale().filter(|s| *s != 0) } /// The markers of a layer (`holder` = the layer chunk) or a composition (`holder` = its `SecL`). From 8bf184a2d4d175417858b38113d5bc23002fb25f Mon Sep 17 00:00:00 2001 From: vangel76 Date: Sun, 8 Nov 2026 03:15:24 +0100 Subject: [PATCH 34/74] aep export: build the prototype template once, sort names with cached keys (#3277) The template every export clones from is immutable; it is now built by the first export of the process and kept (about 0.93 MB). The name sort builds each natural key once instead of on every comparison. Root solids move into the Solids folder instead of being copied. The two key-list rewrites share one emit-local helper and match names compare as bytes instead of a decoded String. Output is byte-identical. --- crates/aep/src/export/emit.rs | 416 ++++++++++++++++++++++++------ crates/aep/src/export/template.rs | 17 ++ 2 files changed, 355 insertions(+), 78 deletions(-) diff --git a/crates/aep/src/export/emit.rs b/crates/aep/src/export/emit.rs index e7423efe9..0f49f995f 100644 --- a/crates/aep/src/export/emit.rs +++ b/crates/aep/src/export/emit.rs @@ -7,7 +7,7 @@ use crate::{AepFile, Chunk}; use effectcraft_project::{ItemId, LayerId}; use crate::export::plan::{CompPlan, Entry, ItemPlan, KeyPlan, LayerPlan, LayerSrc, ProjectPlan, PropKind, PropPlan, SolidPlan, natural_key}; -use crate::export::template::Template; +use crate::export::template::{self, Template}; use crate::export::tree::{ R, child_data, data_mut, kids_mut, list_mut, put, put_bits, put_cstr, put_f32, put_f64, put_i32, put_u8, put_u16, put_u32, set_utf8, tpl, }; @@ -58,10 +58,14 @@ struct Cx<'t> { type Sorted = (String, u64, Vec>); pub(crate) fn emit(plan: &ProjectPlan, extra: &[u8]) -> R> { - let t = Template::load()?; + emit_with(template::shared()?, plan, extra) +} + +/// [`emit`] with the prototypes of `t`. +fn emit_with(t: &Template, plan: &ProjectPlan, extra: &[u8]) -> R> { let special = plan.root.iter().find(|e| e.name == "Solids" && matches!(e.plan, ItemPlan::Folder(_))).map(|e| e.ec); let mut cx = Cx { - t: &t, + t, ids: Ids { next: FIRST_ID }, solid_ids: HashMap::new(), comp_ids: HashMap::new(), @@ -77,16 +81,14 @@ pub(crate) fn emit(plan: &ProjectPlan, extra: &[u8]) -> R> { let mut root: Vec = Vec::new(); build_level(&plan.root, &mut cx, &ids_by_entry, &mut |name, key, chunks| root.push((name, key, chunks)))?; - // The Solids folder: root-level solids and the hidden solids of null layers. - let mut solids_children: Vec = Vec::new(); - for (name, key, chunks) in root.iter().filter(|(_, k, _)| *k & ROOT_SOLID != 0) { - solids_children.push((name.clone(), *key, chunks.clone())); - } - root.retain(|(_, k, c)| *k & ROOT_SOLID == 0 && !c.is_empty()); - for (i, (name, chunk)) in cx.extra_solids.iter().enumerate() { - let mut v = vec![chunk.clone()]; + // The Solids folder: root-level solids and the hidden solids of null layers (moved, not + // copied: the root keeps neither). + let (mut solids_children, mut root): (Vec, Vec) = root.into_iter().partition(|(_, k, _)| *k & ROOT_SOLID != 0); + root.retain(|(_, _, c)| !c.is_empty()); + for (i, (name, chunk)) in std::mem::take(&mut cx.extra_solids).into_iter().enumerate() { + let mut v = vec![chunk]; v.extend(cx.t.fset.iter().cloned()); - solids_children.push((name.clone(), i as u64, v)); + solids_children.push((name, i as u64, v)); } let mut folder = match cx.special_slot.take() { Some((item, inner)) => { @@ -125,24 +127,39 @@ pub(crate) fn emit(plan: &ProjectPlan, extra: &[u8]) -> R> { /// Marks a root entry that goes into the `Solids` folder. const ROOT_SOLID: u64 = 1 << 63; +/// Sorts by name in natural order, then by the tie-break key. Each name's [`natural_key`] is built +/// once, not on every comparison; the sort is stable, as `sort_by` is. fn sort_named(v: &mut [Sorted]) { - v.sort_by(|a, b| natural_cmp(&a.0, &b.0).then(a.1.cmp(&b.1))); + v.sort_by_cached_key(|(name, key, _)| (Natural(natural_key(name)), *key)); } -fn natural_cmp(a: &str, b: &str) -> std::cmp::Ordering { - let (ka, kb) = (natural_key(a), natural_key(b)); - for (x, y) in ka.iter().zip(kb.iter()) { - let c = match (x.0, y.0) { - (true, true) => x.2.cmp(&y.2).then_with(|| x.1.cmp(&y.1)), - (false, false) => x.1.cmp(&y.1), - (true, false) => std::cmp::Ordering::Less, - (false, true) => std::cmp::Ordering::Greater, - }; - if c != std::cmp::Ordering::Equal { - return c; +/// A [`natural_key`] ordered segment by segment: a number before text, numbers by value and then +/// by their digits (`1` before `01`), text by its lowercase string; a key that is a prefix of the +/// other first. Two keys compare equal only when they are equal. +#[derive(PartialEq, Eq)] +struct Natural(Vec<(bool, String, u128)>); + +impl Ord for Natural { + fn cmp(&self, other: &Self) -> std::cmp::Ordering { + for (x, y) in self.0.iter().zip(other.0.iter()) { + let c = match (x.0, y.0) { + (true, true) => x.2.cmp(&y.2).then_with(|| x.1.cmp(&y.1)), + (false, false) => x.1.cmp(&y.1), + (true, false) => std::cmp::Ordering::Less, + (false, true) => std::cmp::Ordering::Greater, + }; + if c != std::cmp::Ordering::Equal { + return c; + } } + self.0.len().cmp(&other.0.len()) + } +} + +impl PartialOrd for Natural { + fn partial_cmp(&self, other: &Self) -> Option { + Some(self.cmp(other)) } - ka.len().cmp(&kb.len()) } fn assign_ids(entries: &[Entry], cx: &mut Cx<'_>, map: &mut HashMap) -> R<()> { @@ -404,8 +421,7 @@ fn put_used_comps(cx: &Cx<'_>, item: &mut Chunk<'static>, cp: &CompPlan) -> R<() fn put_time_remap_max(layr: &mut Chunk<'static>, seconds: f64) -> R<()> { let root = list_mut(layr, b"tdgp")?; let kids = kids_mut(root)?; - let at = - kids.iter().position(|c| c.is(b"tdmn") && tdmn_name(c).as_deref() == Some("ADBE Time Remapping")).ok_or_else(|| tpl("layer without Time Remapping"))?; + let at = kids.iter().position(|c| c.is(b"tdmn") && tdmn_is(c, "ADBE Time Remapping")).ok_or_else(|| tpl("layer without Time Remapping"))?; let tdbs = kids.get_mut(at + 1).ok_or_else(|| tpl("Time Remapping without a record"))?; put_f64(child_data(tdbs, b"tduM")?, 0, seconds) } @@ -420,8 +436,7 @@ fn put_time_remap_max(layr: &mut Chunk<'static>, seconds: f64) -> R<()> { fn put_time_remap(cx: &Cx<'_>, layr: &mut Chunk<'static>, remap: &PropPlan) -> R<()> { let root = list_mut(layr, b"tdgp")?; let kids = kids_mut(root)?; - let at = - kids.iter().position(|c| c.is(b"tdmn") && tdmn_name(c).as_deref() == Some("ADBE Time Remapping")).ok_or_else(|| tpl("layer without Time Remapping"))?; + let at = kids.iter().position(|c| c.is(b"tdmn") && tdmn_is(c, "ADBE Time Remapping")).ok_or_else(|| tpl("layer without Time Remapping"))?; let tdbs = kids.get_mut(at + 1).ok_or_else(|| tpl("Time Remapping without a record"))?; put_u32(child_data(tdbs, b"tdsb")?, 0, 1)?; { @@ -454,21 +469,12 @@ fn put_time_remap(cx: &Cx<'_>, layr: &mut Chunk<'static>, remap: &PropPlan) -> R .find(|k| matches!(&k.body, crate::Body::List { list_type, .. } if list_type == b"list")) .cloned() .ok_or_else(|| tpl("keyed prototype without a list"))?; - let n = u32::try_from(remap.keys.len()).map_err(|_| tpl("too many keys"))?; let size = 48usize; - { - let lhd3 = child_data(&mut list, b"lhd3")?; - crate::export::synth::put_key_counts(lhd3, n, 48)?; - } let mut ldat = Vec::with_capacity(size * remap.keys.len()); for k in &remap.keys { ldat.extend(plain_record(1, k, size)?); } - let ld = kids_mut(&mut list)?; - match ld.iter_mut().find(|c| c.is(b"ldat")) { - Some(c) => *c = Chunk::data(*b"ldat", ldat), - None => ld.push(Chunk::data(*b"ldat", ldat)), - } + set_key_list(&mut list, ldat, remap.keys.len(), size)?; if let Some(slot) = rk.get_mut(cdat_at) { *slot = list; } @@ -616,10 +622,7 @@ fn put_masks(cx: &Cx<'_>, layr: &mut Chunk<'static>, lp: &LayerPlan) -> R<()> { let parade = crate::export::masks::build_parade(&cx.t.masks, &lp.masks, lp.source_size)?; let root = list_mut(layr, b"tdgp")?; let kids = kids_mut(root)?; - let at = kids - .iter() - .position(|c| c.is(b"tdmn") && tdmn_name(c).as_deref() == Some("ADBE Transform Group")) - .ok_or_else(|| tpl("layer without a Transform group"))?; + let at = kids.iter().position(|c| c.is(b"tdmn") && tdmn_is(c, "ADBE Transform Group")).ok_or_else(|| tpl("layer without a Transform group"))?; kids.splice(at..at, parade); Ok(()) } @@ -633,10 +636,7 @@ fn put_effects(cx: &mut Cx<'_>, layr: &mut Chunk<'static>, lp: &LayerPlan, layer let parade = crate::export::effects::build_parade(&cx.t.effects, &lp.fx, layer_id, layer_ids, lp.source_size, &mut cx.fx_use)?; let root = list_mut(layr, b"tdgp")?; let kids = kids_mut(root)?; - let at = kids - .iter() - .position(|c| c.is(b"tdmn") && tdmn_name(c).as_deref() == Some("ADBE Transform Group")) - .ok_or_else(|| tpl("layer without a Transform group"))?; + let at = kids.iter().position(|c| c.is(b"tdmn") && tdmn_is(c, "ADBE Transform Group")).ok_or_else(|| tpl("layer without a Transform group"))?; kids.splice(at..at, parade); Ok(()) } @@ -659,9 +659,17 @@ const GROUP_ORDER: [&str; 13] = [ "ADBE Group End", ]; -fn tdmn_name(c: &Chunk<'_>) -> Option { - let d = c.bytes()?; - Some(d.iter().take_while(|b| **b != 0).map(|b| char::from(*b)).collect()) +/// The match name of a `tdmn` chunk: its bytes up to the first NUL, not decoded (empty for a chunk +/// without data). Every name the exporter looks for is ASCII, so comparing bytes is the same as +/// comparing the Latin-1 text the other exporter modules decode. +fn tdmn_bytes<'c>(c: &'c Chunk<'_>) -> &'c [u8] { + let d = c.bytes().unwrap_or_default(); + d.split(|b| *b == 0).next().unwrap_or_default() +} + +/// True when `c` has data and names `name`. +fn tdmn_is(c: &Chunk<'_>, name: &str) -> bool { + c.bytes().is_some() && tdmn_bytes(c) == name.as_bytes() } /// Inserts the properties of `lp` into the layer's Transform group, in the order the file uses. @@ -671,20 +679,17 @@ fn put_properties(cx: &Cx<'_>, layr: &mut Chunk<'static>, lp: &LayerPlan, cp: &C } let root = list_mut(layr, b"tdgp")?; let kids = kids_mut(root)?; - let at = kids - .iter() - .position(|c| c.is(b"tdmn") && tdmn_name(c).as_deref() == Some("ADBE Transform Group")) - .ok_or_else(|| tpl("layer without a Transform group"))?; + let at = kids.iter().position(|c| c.is(b"tdmn") && tdmn_is(c, "ADBE Transform Group")).ok_or_else(|| tpl("layer without a Transform group"))?; let group = kids.get_mut(at + 1).ok_or_else(|| tpl("Transform group without a list"))?; let gk = kids_mut(group)?; // Split the group into its header (up to the first match name) and (tdmn, node) pairs. let first = gk.iter().position(|c| c.is(b"tdmn")).ok_or_else(|| tpl("empty Transform group"))?; let header: Vec> = gk.drain(..first).collect(); - let mut pairs: Vec<(String, Vec>)> = Vec::new(); + let mut pairs: Vec<(Vec, Vec>)> = Vec::new(); let mut it = std::mem::take(gk).into_iter(); while let Some(m) = it.next() { - let name = tdmn_name(&m).unwrap_or_default(); - if name == "ADBE Group End" { + let name = tdmn_bytes(&m).to_vec(); + if name == b"ADBE Group End" { pairs.push((name, vec![m])); continue; } @@ -694,22 +699,19 @@ fn put_properties(cx: &Cx<'_>, layr: &mut Chunk<'static>, lp: &LayerPlan, cp: &C for p in &lp.props { let name = p.kind.match_name(); let built = build_prop(cx, p, lp, cp)?; - let tdmn = pairs.iter().find(|(n, _)| n == name).and_then(|(_, v)| v.first().cloned()).unwrap_or_else(|| { - cx.t.stat - .iter() - .chain(cx.t.keyd.iter()) - .find(|c| c.is(b"tdmn") && tdmn_name(c).as_deref() == Some(name)) - .cloned() - .unwrap_or_else(|| Chunk::data(*b"tdmn", Vec::new())) + let tdmn = pairs.iter().find(|(n, _)| n == name.as_bytes()).and_then(|(_, v)| v.first().cloned()).unwrap_or_else(|| { + cx.t.stat.iter().chain(cx.t.keyd.iter()).find(|c| c.is(b"tdmn") && tdmn_is(c, name)).cloned().unwrap_or_else(|| Chunk::data(*b"tdmn", Vec::new())) }); let entry = vec![tdmn, built]; - if let Some(slot) = pairs.iter_mut().find(|(n, _)| n == name) { + if let Some(slot) = pairs.iter_mut().find(|(n, _)| n == name.as_bytes()) { slot.1 = entry; } else { - pairs.push((name.to_string(), entry)); + pairs.push((name.as_bytes().to_vec(), entry)); } } - let rank = |n: &str| if n == "ADBE Group End" { usize::MAX } else { GROUP_ORDER.iter().position(|g| *g == n).unwrap_or(GROUP_ORDER.len()) }; + let rank = |n: &[u8]| { + if n == b"ADBE Group End" { usize::MAX } else { GROUP_ORDER.iter().position(|g| g.as_bytes() == n).unwrap_or(GROUP_ORDER.len()) } + }; pairs.sort_by_key(|(n, _)| rank(n)); gk.extend(header); gk.extend(pairs.into_iter().flat_map(|(_, v)| v)); @@ -743,22 +745,12 @@ fn build_prop(cx: &Cx<'_>, p: &PropPlan, lp: &LayerPlan, cp: &CompPlan) -> R *c = Chunk::data(*b"ldat", ldat), - None => ld.push(Chunk::data(*b"ldat", ldat)), - } + set_key_list(list_mut(&mut tdbs, b"list")?, ldat, p.keys.len(), size)?; } if let Some((text, _)) = &p.expr { let kids = kids_mut(&mut tdbs)?; @@ -768,6 +760,25 @@ fn build_prop(cx: &Cx<'_>, p: &PropPlan, lp: &LayerPlan, cp: &CompPlan) -> R, records: Vec, count: usize, size: usize) -> R<()> { + let n = u32::try_from(count).map_err(|_| tpl("too many keys"))?; + { + let lhd3 = child_data(list, b"lhd3")?; + crate::export::synth::put_key_counts(lhd3, n, u32::try_from(size).map_err(|_| tpl("record size"))?)?; + } + let ld = kids_mut(list)?; + match ld.iter_mut().find(|c| c.is(b"ldat")) { + Some(c) => *c = Chunk::data(*b"ldat", records), + None => ld.push(Chunk::data(*b"ldat", records)), + } + Ok(()) +} + /// One keyframe record (`docs/aep-format.md`, 3.5). fn key_record(kind: PropKind, k: &KeyPlan, size: usize) -> R> { let mut r = vec![0u8; size]; @@ -818,3 +829,252 @@ pub(crate) fn plain_record(n: usize, k: &KeyPlan, size: usize) -> R> { } Ok(r) } + +#[cfg(test)] +mod tests { + use super::*; + use std::sync::Arc; + + use effectcraft_color::Label; + use effectcraft_keyframe::{Keyframe, Value}; + use effectcraft_project::{Comp, Expression, ItemKind, LayerSource, Project, Property, Solid, build}; + use effectcraft_time::{FrameRate, TICKS_PER_SECOND, Tick}; + + fn secs(s: f64) -> Tick { + Tick((s * TICKS_PER_SECOND as f64).round() as i64) + } + + fn add_layer(p: &mut Project, comp: ItemId, name: &str, source: LayerSource, edit: impl FnOnce(&mut effectcraft_project::Layer)) { + let mut c = p.comp(comp).cloned().expect("comp"); + let mut l = build::layer(p, &c, name, source, (640, 360), None); + l.name = name.to_string(); + edit(&mut l); + c.layers.insert(0, l); + if let Some(i) = p.item_mut(comp) { + i.kind = ItemKind::Comp(Arc::new(c)); + } + } + + fn two_keys(prop: Option<&mut Property>) { + let prop = prop.expect("property"); + let v = prop.value.clone(); + prop.keys = vec![Keyframe::new(secs(0.0), v.clone()), Keyframe::new(secs(1.5), v)]; + } + + /// A project that reaches every part of `emit`: folders (one named `Solids`), root and nested + /// solids with names that sort naturally, a precomposition, null layers (hidden solids), keyed + /// Transform properties, an expression and a keyed time remap. + fn varied(tag: &str) -> Project { + let mut p = Project::default(); + let solid = |w| ItemKind::Solid(Solid { color: [0.2, 0.4, 0.6], width: w, height: 360, pixel_aspect: 1.0 }); + let solids = p.add_item("Solids", Label::None, None, ItemKind::Folder); + let nested = p.add_item("Folder 10", Label::None, None, ItemKind::Folder); + p.add_item("folder 9", Label::None, Some(nested), ItemKind::Folder); + let mut ids = Vec::new(); + for name in ["Solid 10", "Solid 2", "solid 2", "Solid 02", "Solid", "Ölung 1", ""] { + ids.push(p.add_item(&format!("{name}{tag}"), Label::Red, None, solid(640))); + } + p.add_item("In Solids 3", Label::None, Some(solids), solid(320)); + p.add_item("In folder", Label::None, Some(nested), solid(100)); + let pre = p.add_item("Pre 1", Label::None, Some(nested), ItemKind::Comp(Arc::new(Comp::new(640, 360, FrameRate::new(25, 1), secs(3.0))))); + add_layer(&mut p, pre, "S", LayerSource::Solid { item: ids[0] }, |_| {}); + let main = p.add_item("Main", Label::None, None, ItemKind::Comp(Arc::new(Comp::new(1920, 1080, FrameRate::new(30, 1), secs(5.0))))); + add_layer(&mut p, main, "Null A", LayerSource::Null, |_| {}); + add_layer(&mut p, main, "Null B", LayerSource::Null, |l| two_keys(l.props.prop_mut("transform/position"))); + add_layer(&mut p, main, "Solid layer", LayerSource::Solid { item: ids[1] }, |l| { + two_keys(l.props.prop_mut("transform/opacity")); + two_keys(l.props.prop_mut("transform/anchor")); + if let Some(r) = l.props.prop_mut("transform/rotation") { + r.expr = Some(Expression { text: "time * 10".into(), enabled: true }); + } + }); + add_layer(&mut p, main, "Precomp", LayerSource::Comp { item: pre }, |l| { + let mut r = Property::new(9_000, "timeRemap", "Time Remap", Value::Scalar(0.0)); + r.keys = vec![Keyframe::new(secs(0.0), Value::Scalar(3.0)), Keyframe::new(secs(3.0), Value::Scalar(0.0))]; + l.props.children.insert(0, r.into()); + }); + p + } + + fn plan_of(p: &Project) -> ProjectPlan { + let mut col = crate::export::report::Collector::default(); + crate::export::plan::plan(p, &mut col) + } + + /// The template is built once and every later export uses the same one. + #[test] + fn the_template_is_built_once() { + let a = template::shared().expect("template"); + let b = template::shared().expect("template"); + assert!(std::ptr::eq(a, b)); + } + + /// Exports with the shared template give the bytes a freshly built template gives, also after + /// other exports have used it (nothing an export does reaches the template). + #[test] + fn the_shared_template_exports_what_a_fresh_one_does() { + let (a, b) = (plan_of(&varied("")), plan_of(&varied(" x"))); + let fresh = |plan: &ProjectPlan| emit_with(&Template::load().expect("template"), plan, b"trailer").expect("emit"); + let (want_a, want_b) = (fresh(&a), fresh(&b)); + assert!(want_a != want_b); + // The project exports without losses and the remap is written. + let r = crate::export::export(&varied(""), &crate::export::ExportOptions { allow_losses: false }).expect("lossless"); + assert_eq!(r.report.exported.time_remaps, 1); + // The null layers' hidden solids and the root solids went into the `Solids` folder. + let file = crate::parse(&want_a).expect("parse"); + let folder = file.walk().map(|(_, c)| c).filter(|c| c.is_list(b"Sfdr")).map(|c| c.children().iter().filter(|k| k.is_list(b"Item")).count()).max(); + assert_eq!(folder, Some(10)); + for _ in 0..3 { + assert!(emit(&a, b"trailer").expect("emit") == want_a); + assert!(emit(&b, b"trailer").expect("emit") == want_b); + } + } + + /// Exports running at the same time on several threads, the first of them possibly building + /// the shared template, all give the single-threaded bytes. + #[test] + fn concurrent_exports_give_the_same_bytes() { + let plans: Vec = ["", " x", " y", " z"].iter().map(|t| plan_of(&varied(t))).collect(); + let want: Vec> = plans.iter().map(|plan| emit_with(&Template::load().expect("template"), plan, &[]).expect("emit")).collect(); + let got: Vec>> = std::thread::scope(|s| { + let handles: Vec<_> = (0..8) + .map(|i| { + let plans = &plans; + s.spawn(move || (0..4).map(|k| emit(&plans[(i + k) % plans.len()], &[]).expect("emit")).collect::>()) + }) + .collect(); + handles.into_iter().map(|h| h.join().expect("thread")).collect() + }); + for (i, bytes) in got.iter().enumerate() { + for (k, b) in bytes.iter().enumerate() { + assert!(b == &want[(i + k) % want.len()], "thread {i}, export {k}"); + } + } + } + + /// The comparator `sort_named` used before its keys were cached: `natural_key` built on every + /// comparison. + fn old_sort_named(v: &mut [Sorted]) { + fn natural_cmp(a: &str, b: &str) -> std::cmp::Ordering { + let (ka, kb) = (natural_key(a), natural_key(b)); + for (x, y) in ka.iter().zip(kb.iter()) { + let c = match (x.0, y.0) { + (true, true) => x.2.cmp(&y.2).then_with(|| x.1.cmp(&y.1)), + (false, false) => x.1.cmp(&y.1), + (true, false) => std::cmp::Ordering::Less, + (false, true) => std::cmp::Ordering::Greater, + }; + if c != std::cmp::Ordering::Equal { + return c; + } + } + ka.len().cmp(&kb.len()) + } + v.sort_by(|a, b| natural_cmp(&a.0, &b.0).then(a.1.cmp(&b.1))); + } + + /// The cached-key sort orders names exactly as the comparator it replaced, and keeps entries + /// with the same name and key in their order (each carries its position as a marker chunk). + #[test] + fn sort_named_orders_as_the_old_comparator() { + let parts = [ + "", + "0", + "00", + "1", + "01", + "2", + "10", + "a", + "A", + "b", + " ", + "-", + ".", + "é", + "É", + "ß", + "İ", + "\u{0}", + "Solid ", + "solid ", + "x9", + "9x", + "340282366920938463463374607431768211455", + "340282366920938463463374607431768211456", + "999999999999999999999999999999999999999999", + ]; + // A small deterministic generator (splitmix64). + let mut state = 0x2545_f491_4f6c_dd1d_u64; + let mut next = move || { + state = state.wrapping_add(0x9e37_79b9_7f4a_7c15); + let mut z = state; + z = (z ^ (z >> 30)).wrapping_mul(0xbf58_476d_1ce4_e5b9); + z = (z ^ (z >> 27)).wrapping_mul(0x94d0_49bb_1331_11eb); + (z ^ (z >> 31)) as usize + }; + for round in 0..200 { + let n = 1 + next() % 40; + let v: Vec = (0..n) + .map(|i| { + let name: String = (0..next() % 4).map(|_| parts[next() % parts.len()]).collect(); + let key = (next() % 3) as u64 | if next() % 2 == 0 { ROOT_SOLID } else { 0 }; + (name, key, vec![Chunk::data(*b"mark", (i as u32).to_le_bytes().to_vec())]) + }) + .collect(); + let (mut old, mut new) = (v.clone(), v); + old_sort_named(&mut old); + sort_named(&mut new); + assert_eq!(old, new, "round {round}"); + } + } + + /// `tdmn_is` and `tdmn_bytes` agree with the Latin-1 decoding they replaced for every name the + /// emitter looks up, which holds because those names are ASCII. + #[test] + fn match_names_compare_as_the_latin1_decoding_did() { + fn old_tdmn_name(c: &Chunk<'_>) -> Option { + let d = c.bytes()?; + Some(d.iter().take_while(|b| **b != 0).map(|b| char::from(*b)).collect()) + } + let mut wanted: Vec<&str> = GROUP_ORDER.to_vec(); + wanted.extend(PropKind::ALL.iter().map(|k| k.match_name())); + wanted.extend(["ADBE Time Remapping", "ADBE Transform Group", "ADBE Group End"]); + assert!(wanted.iter().all(|n| n.is_ascii())); + let mut chunks: Vec> = wanted.iter().map(|n| Chunk::data(*b"tdmn", format!("{n}\0\0junk").into_bytes())).collect(); + chunks.extend([ + Chunk::data(*b"tdmn", Vec::new()), + Chunk::data(*b"tdmn", b"ADBE Position".to_vec()), + Chunk::data(*b"tdmn", b"ADBE Position_0\0".to_vec()), + Chunk::data(*b"tdmn", b"ADBE Opacit\xe9\0".to_vec()), + Chunk::data(*b"tdmn", b"\xc3\x84DBE Scale\0".to_vec()), + Chunk::data(*b"tdmn", b"\0ADBE Scale".to_vec()), + Chunk::list(*b"tdmn", Vec::new()), + ]); + for c in &chunks { + for n in &wanted { + assert_eq!(tdmn_is(c, n), old_tdmn_name(c).as_deref() == Some(*n), "{c:?} {n}"); + } + let old = old_tdmn_name(c).unwrap_or_default(); + if old.is_ascii() { + assert_eq!(tdmn_bytes(c), old.as_bytes()); + } + } + } + + /// `set_key_list` writes the counts and replaces the records, or adds them when the list has + /// none. + #[test] + fn set_key_list_writes_counts_and_records() { + let lhd3 = || Chunk::data(*b"lhd3", vec![0; 52]); + let mut want_lhd3 = vec![0; 52]; + crate::export::synth::put_key_counts(&mut want_lhd3, 3, 48).expect("counts"); + for start in [vec![lhd3(), Chunk::data(*b"ldat", vec![9; 4])], vec![lhd3()]] { + let mut list = Chunk::list(*b"list", start); + set_key_list(&mut list, vec![7; 144], 3, 48).expect("set"); + assert_eq!(list.children(), &[Chunk::data(*b"lhd3", want_lhd3.clone()), Chunk::data(*b"ldat", vec![7; 144])]); + } + let mut no_header = Chunk::list(*b"list", Vec::new()); + assert!(set_key_list(&mut no_header, Vec::new(), 0, 48).is_err()); + } +} diff --git a/crates/aep/src/export/template.rs b/crates/aep/src/export/template.rs index 91daba636..608fa75e1 100644 --- a/crates/aep/src/export/template.rs +++ b/crates/aep/src/export/template.rs @@ -81,6 +81,23 @@ fn transform_records(keyed: bool) -> Vec> { .collect() } +/// The template every export clones from: built by the first export of the process and kept, as +/// [`Template::load`] always builds the same prototypes and nothing changes them afterwards (the +/// exporter only reads them through `&Template` and clones what it edits). +/// +/// Concurrent exports are safe: the `static` requires `Template: Send + Sync`, which the compiler +/// checks. Exports that race on the first call each build a template; one is kept and the others +/// are dropped, and all of them are equal. A failed build is not kept, so the next export tries +/// again and reports the same error. +pub(crate) fn shared() -> R<&'static Template> { + static SHARED: std::sync::OnceLock