diff --git a/crates/ui-egui/src/panels/misc.rs b/crates/ui-egui/src/panels/misc.rs index a34e0d483..a1cf827ba 100644 --- a/crates/ui-egui/src/panels/misc.rs +++ b/crates/ui-egui/src/panels/misc.rs @@ -415,8 +415,8 @@ fn audio_levels(app: &mut EffectcraftApp, ui: &mut egui::Ui, p: &egui::Painter, // Proportional Scrubbing (Ctrl+Alt / Cmd+Opt): the change spreads over the selected layers by // selection order, as in the Timeline. let proportional = scrub && ui.input(|i| widgets::proportional_scrub(i.modifiers)); - // Linked channels move together; Alt edits one alone (Ctrl+Alt is a proportional scrub). - let linked = app.ui.timeline.is_linked(uid, false) && !ui.input(|i| i.modifiers.alt && !widgets::proportional_scrub(i.modifiers)); + // Linked channels (on by default, #4077) move together; Alt edits one alone (Ctrl+Alt is a proportional scrub). + let linked = app.ui.timeline.is_linked(uid, true) && !ui.input(|i| i.modifiers.alt && !widgets::proportional_scrub(i.modifiers)); let mut actions: Vec = vec![]; // A scrub ended this frame: the edits below still join its undo step, the next scrub starts // another (#3234 review). @@ -446,9 +446,18 @@ fn audio_levels(app: &mut EffectcraftApp, ui: &mut egui::Ui, p: &egui::Painter, // The fields show the value after the expression; `prop.set` writes (and a scrub takes // its change from) the keyframed value under it. So each channel is that value moved // by the change the edit makes to what is shown (every selected layer then moves by - // it), except a typed channel, which is set as typed. - let value: Vec = - value.iter().zip(values.iter().zip(raw)).enumerate().map(|(e, (n, (shown, r)))| if e == d && !scrub { *n } else { r + (n - shown) }).collect(); + // it), except a typed channel, which is set as typed. A linked channel then follows by + // the typed channel's change to that value, as on the other selected layers. + let value: Vec = value + .iter() + .zip(values.iter().zip(raw)) + .enumerate() + .map(|(e, (n, (shown, r)))| match (e == d, scrub) { + (true, false) => *n, + (false, false) if linked => r + (nv - raw[d]), + _ => r + (n - shown), + }) + .collect(); params["value"] = json!(value); actions.push(params); } diff --git a/crates/ui-egui/src/panels/timeline.rs b/crates/ui-egui/src/panels/timeline.rs index 7926a8f5c..feafe55a1 100644 --- a/crates/ui-egui/src/panels/timeline.rs +++ b/crates/ui-egui/src/panels/timeline.rs @@ -4312,7 +4312,7 @@ fn prop_value_cell(app: &mut EffectcraftApp, ui: &mut egui::Ui, g: &mut Rw, rc: let value = ectx.value(layer, prop); let vx = (cw.switches + 4.0).max(name_x + 120.0); - let ve = ValueEdit::new(ui, &lp, layer, prop, pos2(vx, cy)); + let ve = ValueEdit::new(ui, &lp, layer, prop, pos2(vx, cy), ectx.time); let dims = value_editor(app, ui, &ve, &value, &mut *actions); if !dims.is_empty() { dim_hits.push((*uid, dims)); @@ -5926,11 +5926,13 @@ struct ValueEdit<'a> { merge: String, scrub: bool, proportional: bool, + /// The composition time of the value shown. + time: Tick, } impl<'a> ValueEdit<'a> { /// The editor of `prop` on `layer` at `at`; `ui` says whether a scrub is under way. - fn new(ui: &egui::Ui, p: &'a egui::Painter, layer: &'a Layer, prop: &'a Property, at: Pos2) -> Self { + fn new(ui: &egui::Ui, p: &'a egui::Painter, layer: &'a Layer, prop: &'a Property, at: Pos2, time: Tick) -> Self { let uid = prop.uid; // With several layers selected the edit applies to all of them (#491): a scrub by the same // change, a typed value as it is. @@ -5938,7 +5940,7 @@ impl<'a> ValueEdit<'a> { // Proportional Scrubbing (After Effects 26.2): Ctrl+Alt (Cmd+Opt) spreads the change over the // selected layers by selection order, the first unchanged and the last by all of it. let proportional = scrub && ui.input(|i| widgets::proportional_scrub(i.modifiers)); - Self { p, layer, prop, at, uid, merge: format!("scrub-{uid}"), scrub, proportional } + Self { p, layer, prop, at, uid, merge: format!("scrub-{uid}"), scrub, proportional, time } } /// Queues the `prop.set` of one edit; consecutive edits of the property (a scrub) are one undo step. @@ -6036,13 +6038,14 @@ fn vector_editor(app: &mut EffectcraftApp, ui: &mut egui::Ui, ve: &ValueEdit<'_> // Mask Feather: two linked values, never negative (as in After Effects, #203). let feather = prop.match_id == "feather" && layer.props.parent_of(uid).is_some_and(|g| matches!(g.kind, GroupKind::Mask { .. })); let range = if feather { (0.0, 1e9) } else { (-1e9, 1e9) }; - // Audio Levels link their two channels too, off by default, by the same dB change (#2485). + // Audio Levels link their two channels too, on by default as in After Effects, by the same dB + // change (#2485, #4077). let levels = prop.match_id == "levels" && layer.props.parent_of(uid).is_some_and(|g| g.match_id == "audio"); - // Scale and Mask Feather: the chain link (Constrain Proportions, on by default). + // Scale, Mask Feather and Audio Levels: the chain link (Constrain Proportions, on by default). // Rectangle and Ellipse Path Size have it too, off by default (#2489). let size = prop.match_id == "size" && layer.props.parent_of(uid).is_some_and(|g| matches!(g.match_id.as_str(), "rect" | "ellipse")); let linkable = feather || size || levels || (pct && prop.match_id == "scale"); - let linked = linkable && app.ui.timeline.is_linked(uid, !(size || levels)); + let linked = linkable && app.ui.timeline.is_linked(uid, !size); if linkable { let lr = Rect::from_center_size(pos2(x + 7.0, at.y), vec2(14.0, 14.0)); let label = if levels { "Link Channels" } else { "Constrain Proportions" }; @@ -6050,7 +6053,7 @@ fn vector_editor(app: &mut EffectcraftApp, ui: &mut egui::Ui, ve: &ValueEdit<'_> icons::paint(p, lr, Icon::Link, if linked { t.accent } else { t.text_faint }); app.auto.add(&format!("timeline.prop.{uid}.link"), lr, label); if resp.clicked() { - app.ui.timeline.toggle_link(uid, !(size || levels)); + app.ui.timeline.toggle_link(uid, !size); } x += 18.0; } @@ -6077,6 +6080,28 @@ fn vector_editor(app: &mut EffectcraftApp, ui: &mut egui::Ui, ve: &ValueEdit<'_> *slot = v; } } + // The fields show the value after an expression, but `prop.set` writes the keyframed value + // under it. Linked or not, each Audio Levels channel is that value moved by the change the + // edit makes to what is shown, except a typed one, which is set as typed, and its linked + // partner, which follows by the typed channel's change to the keyframed value (as on the + // other selected layers). + if levels { + let raw = prop.value_at(layer.layer_time(ve.time)).components(); + if raw.len() == nc.len() { + let typed = !ve.scrub; + nc = (0..nc.len()) + .map(|e| { + if typed && e == d { + nv + } else if typed && shown > 1 { + raw[e] + (nv - raw[d]) + } else { + raw[e] + (nc[e] - c[e]) + } + }) + .collect(); + } + } ve.set(actions, json!(nc)); // Typed into these components only: other selected layers keep the rest of // their own value (#1434). diff --git a/crates/ui-egui/src/state.rs b/crates/ui-egui/src/state.rs index 9b4dc78e7..3e9e8ae3e 100644 --- a/crates/ui-egui/src/state.rs +++ b/crates/ui-egui/src/state.rs @@ -366,8 +366,8 @@ impl Default for ViewerState { impl TimelineState { /// Whether property `uid`'s chain link (Constrain Proportions) is on. One that starts linked - /// (Scale, Mask Feather) records a click in `unlinked`; one that starts unlinked (shape Size, - /// Audio Levels) records it in `linked`. + /// (Scale, Mask Feather, Audio Levels) records a click in `unlinked`; one that starts unlinked + /// (shape Size) records it in `linked`. A uid in neither set has its default link. pub fn is_linked(&self, uid: u64, linked_by_default: bool) -> bool { if linked_by_default { !self.unlinked.contains(&uid) } else { self.linked.contains(&uid) } } @@ -493,12 +493,12 @@ pub struct TimelineState { /// the others grow with their text, up to 8 lines. #[serde(default)] pub expr_lines: BTreeMap, - /// Scale and Mask Feather properties whose chain link (Constrain Proportions, on by default) - /// was turned off. + /// Scale, Mask Feather and Audio Levels properties whose chain link (Constrain Proportions or + /// Link Channels, on by default) was turned off. #[serde(default)] pub unlinked: BTreeSet, - /// Shape Rectangle and Ellipse Path Size (#2489) and Audio Levels (#2485) properties whose - /// chain link (off by default) was turned on. + /// Shape Rectangle and Ellipse Path Size (#2489) properties whose chain link (off by default) + /// was turned on. #[serde(default)] pub linked: BTreeSet, /// The project (its epoch) `unlinked` and `linked` belong to. diff --git a/crates/ui-egui/tests/it/ui_audio_panel_levels.rs b/crates/ui-egui/tests/it/ui_audio_panel_levels.rs index 518da1794..a73ff1652 100644 --- a/crates/ui-egui/tests/it/ui_audio_panel_levels.rs +++ b/crates/ui-egui/tests/it/ui_audio_panel_levels.rs @@ -1,7 +1,7 @@ //! The Audio panel's level fields (#3227): they show the selected layer's Audio Levels at the //! current time, keyframes and expression included, as the Timeline does; typing into one on a //! keyframed property sets a key at the current time and leaves the static value alone, and with -//! the Timeline's Link Channels on the other channel follows (#2485). +//! the Timeline's Link Channels on (its default, #4077) the other channel follows (#2485). use effectcraft_engine::Session; use effectcraft_engine::project::LayerId; @@ -51,8 +51,9 @@ fn sound_layer(s: &mut Session, levels: [f64; 2]) -> u64 { l } -/// The harness with layer `l` selected and the Audio panel up. -fn harness(mut s: Session, l: u64) -> Harness<'static, EffectcraftApp> { +/// The harness with layer `l` selected and the Audio panel up, its Link Channels as a new project +/// has it (on). +fn linked_harness(mut s: Session, l: u64) -> Harness<'static, EffectcraftApp> { s.execute("layer.select", json!({"layers": [l]})).unwrap(); let mut h = Harness::builder() .with_size(vec2(1600.0, 1000.0)) @@ -63,6 +64,16 @@ fn harness(mut s: Session, l: u64) -> Harness<'static, EffectcraftApp> { h } +/// [`linked_harness`] with the channels unlinked, for tests about the fields themselves: typing +/// or scrubbing one channel leaves the other alone. +fn harness(s: Session, l: u64) -> Harness<'static, EffectcraftApp> { + let mut h = linked_harness(s, l); + let uid = levels(&h, l).uid; + h.state_mut().ui.timeline.toggle_link(uid, true); + h.run_steps(2); + h +} + fn levels(h: &Harness<'_, EffectcraftApp>, l: u64) -> effectcraft_engine::project::Property { h.state().session.active_comp().unwrap().layer(LayerId(l)).unwrap().props.prop("audio/levels").unwrap().clone() } @@ -143,17 +154,15 @@ fn static_levels_take_a_typed_value_without_a_key() { assert!(pr.keys.is_empty(), "no key on a static property"); } -/// With the Timeline's Link Channels on (#2485), typing into one channel moves the other by the -/// same dB, as in the Timeline. +/// With the Timeline's Link Channels on, as it starts (#2485, #4077), typing into one channel +/// moves the other by the same dB, as in the Timeline. #[test] fn linked_levels_move_both_channels_by_the_same_db() { let mut s = Session::default(); let l = sound_layer(&mut s, [-6.0, -12.0]); - let mut h = harness(s, l); + let mut h = linked_harness(s, l); let uid = levels(&h, l).uid; - h.state_mut().ui.timeline.toggle_link(uid, false); - h.run_steps(2); - assert!(h.state().ui.timeline.is_linked(uid, false)); + assert!(h.state().ui.timeline.is_linked(uid, true), "linked by default"); type_value(&mut h, LEFT, "-3"); assert_eq!(levels(&h, l).value.components(), vec![-3.0, -9.0], "both rise 3 dB and keep their 6 dB balance"); h.state_mut().session.execute("edit.undo", json!({})).unwrap(); @@ -295,15 +304,13 @@ fn two_scrubs_are_two_undo_steps() { assert!((left(&h) - first).abs() < 1e-9, "one Undo takes back the second scrub only: {start} / {first} / {second} -> {}", left(&h)); } -/// With Link Channels on, a scrub of Left moves Right by the same dB; Alt scrubs Left alone. +/// With Link Channels on (the default), a scrub of Left moves Right by the same dB; Alt scrubs +/// Left alone. #[test] fn a_linked_scrub_moves_both_channels_and_alt_moves_one() { let mut s = Session::default(); let l = sound_layer(&mut s, [-6.0, -12.0]); - let mut h = harness(s, l); - let uid = levels(&h, l).uid; - h.state_mut().ui.timeline.toggle_link(uid, false); - h.run_steps(2); + let mut h = linked_harness(s, l); scrub_left(&mut h, Modifiers::NONE); let c = levels(&h, l).value.components(); assert!(c[0] > -6.0, "the scrub raised Left: {c:?}"); @@ -332,6 +339,18 @@ fn typing_into_expression_driven_levels_sets_the_value_under_the_expression() { assert_eq!(label(&h, RIGHT), db(-2.0)); } +/// With Link Channels on, a typed channel of expression-driven levels moves the other channel's +/// value under the expression by the same dB as its own, so the stored balance is kept. +#[test] +fn typing_into_linked_expression_driven_levels_keeps_the_balance_under_the_expression() { + let mut s = effectcraft_host::session(); + let l = sound_layer(&mut s, [-6.0, -12.0]); + s.execute("prop.setExpression", json!({"layer": l, "path": "audio/levels", "expression": "value + [10, 10]"})).unwrap(); + let mut h = linked_harness(s, l); + type_value(&mut h, LEFT, "-5"); + assert_eq!(levels(&h, l).value.components(), vec![-5.0, -11.0], "both rise 1 dB under the expression"); +} + /// A narrow Audio panel (200-230 px) still shows the level fields, labelled "L" / "R", and keeps /// them inside the panel. #[test] diff --git a/crates/ui-egui/tests/it/ui_timeline_audio_levels.rs b/crates/ui-egui/tests/it/ui_timeline_audio_levels.rs index 96cf85e97..c407c6cf0 100644 --- a/crates/ui-egui/tests/it/ui_timeline_audio_levels.rs +++ b/crates/ui-egui/tests/it/ui_timeline_audio_levels.rs @@ -1,4 +1,4 @@ -//! Audio Levels' channel link in the Timeline (#2485): off by default; linked, an edit to one +//! Audio Levels' channel link in the Timeline (#2485, #4077): on by default; linked, an edit to one //! channel moves the other by the same dB, so the stereo balance stays, typed or scrubbed, on a //! static or animated property, and one Undo takes it back. @@ -91,12 +91,7 @@ fn linked_audio_levels_move_both_channels_by_the_same_db() { assert_eq!(h.state().auto.find(&link).unwrap().label, "Link Channels"); type_value(&mut h, &left, "-3"); - assert_eq!(levels(&h).value.components(), vec![-3.0, -12.0], "unlinked by default, one channel moves alone"); - set(&mut h, [-6.0, -12.0]); - - click(&mut h, &link); - type_value(&mut h, &left, "-3"); - assert_eq!(levels(&h).value.components(), vec![-3.0, -9.0], "linked, both rise 3 dB and keep their 6 dB balance"); + assert_eq!(levels(&h).value.components(), vec![-3.0, -9.0], "linked by default, both rise 3 dB and keep their 6 dB balance"); undo(&mut h); assert_eq!(levels(&h).value.components(), vec![-6.0, -12.0], "one Undo takes back both channels"); @@ -126,7 +121,26 @@ fn linked_audio_levels_move_both_channels_by_the_same_db() { click(&mut h, &link); type_value(&mut h, &left, "-4"); let keys: Vec> = levels(&h).keys.iter().map(|k| k.value.components()).collect(); - assert_eq!(keys[0], vec![-4.0, -16.0], "unlinked again, the right channel stays"); + assert_eq!(keys[0], vec![-4.0, -16.0], "unlinked by the click, the right channel stays"); +} + +/// An expression that offsets the channels differently shows other values than the stored ones. A +/// linked typed Left sets the stored Left as typed and moves the stored Right by the same change, so +/// the stored balance stays (as in the Audio panel). +#[test] +fn linked_typing_into_expression_driven_levels_keeps_the_stored_balance() { + // The host session has the expression runtime. + let mut s = effectcraft_host::session(); + comps(&mut s); + let (l, uid) = add_sound_layer(&mut s, [-6.0, -12.0]); + s.execute("prop.setExpression", json!({"layer": l, "path": "audio/levels", "expression": "value + [10, 0]"})).unwrap(); + let mut h = Harness::builder() + .with_size(vec2(1600.0, 1000.0)) + .with_context_setup(effectcraft_ui_egui::framework::install_headless) + .build_eframe(|_| EffectcraftApp::new(s)); + reveal(&mut h, l, uid); + type_value(&mut h, &format!("timeline.prop.{uid}.value.0"), "-5"); + assert_eq!(levels_of(&h, l), vec![-5.0, -11.0], "both stored channels rise 1 dB; the shown +10 dB offset on Left is not written into Right"); } /// With several layers selected, a linked typed Left sets each layer's Left and moves its Right @@ -144,15 +158,14 @@ fn linked_typing_keeps_each_selected_layers_own_balance() { .build_eframe(|_| EffectcraftApp::new(s)); reveal(&mut h, a, uid); reveal(&mut h, b, other); - click(&mut h, &format!("timeline.prop.{uid}.link")); type_value(&mut h, &format!("timeline.prop.{uid}.value.0"), "-3"); assert_eq!(levels_of(&h, a), vec![-3.0, -9.0]); assert_eq!(levels_of(&h, b), vec![-3.0, -13.0], "B rises 17 dB on both channels, keeping its 10 dB balance"); } -/// A link turned on does not carry into a new project whose Audio Levels reuse the property uid. +/// A link turned off does not carry into a new project whose Audio Levels reuse the property uid. #[test] -fn a_new_project_starts_with_its_audio_levels_unlinked() { +fn a_new_project_starts_with_its_audio_levels_linked() { let mut s = Session::default(); comps(&mut s); let (l, uid) = add_sound_layer(&mut s, [0.0, 0.0]); @@ -162,14 +175,14 @@ fn a_new_project_starts_with_its_audio_levels_unlinked() { .build_eframe(|_| EffectcraftApp::new(s)); reveal(&mut h, l, uid); click(&mut h, &format!("timeline.prop.{uid}.link")); - assert!(h.state().ui.timeline.is_linked(uid, false)); + assert!(!h.state().ui.timeline.is_linked(uid, true), "the click unlinked them"); h.state_mut().session.execute("file.newProject", json!({})).unwrap(); comps(&mut h.state_mut().session); let (l, again) = add_sound_layer(&mut h.state_mut().session, [0.0, 0.0]); assert_eq!(again, uid, "the new project reuses the property uid"); reveal(&mut h, l, again); - assert!(!h.state().ui.timeline.is_linked(again, false), "the new project's levels start unlinked"); + assert!(h.state().ui.timeline.is_linked(again, true), "the new project's levels start linked"); type_value(&mut h, &format!("timeline.prop.{again}.value.0"), "-3"); - assert_eq!(levels_of(&h, l), vec![-3.0, 0.0]); + assert_eq!(levels_of(&h, l), vec![-3.0, -3.0]); }