diff --git a/crates/ui-egui/src/panels.rs b/crates/ui-egui/src/panels.rs index ccff0fdcd..8feb8c8c5 100644 --- a/crates/ui-egui/src/panels.rs +++ b/crates/ui-egui/src/panels.rs @@ -9,6 +9,7 @@ pub mod crypto_pick; pub mod delete_items; pub mod dialogs; pub mod dialogs_3d; +pub(crate) mod drag_preview; pub mod effect_controls; pub mod effects_presets; pub mod essential; diff --git a/crates/ui-egui/src/panels/drag_preview.rs b/crates/ui-egui/src/panels/drag_preview.rs new file mode 100644 index 000000000..e0f53fb12 --- /dev/null +++ b/crates/ui-egui/src/panels/drag_preview.rs @@ -0,0 +1,54 @@ +//! Fast Previews ▸ Adaptive Resolution for drags outside the viewer: as in After Effects, a drag +//! that changes the frame on screen (scrubbing the time, moving or trimming layers, slipping, +//! dragging keys, scrubbing values) draws a draft until the button is released. + +use std::sync::Arc; + +use effectcraft_engine::project::{ItemId, Project}; + +use crate::EffectcraftApp; + +/// The project, the active comp and its current frame index when a panel starts its pass. +/// [`DragWatch::end`] compares them with the comp and frame after the panel ran its gestures, so +/// every drag the panel handles is covered without a flag per gesture. +pub(crate) struct DragWatch { + before: Option<(ItemId, Arc, i64)>, +} + +impl DragWatch { + pub(crate) fn begin(app: &EffectcraftApp) -> Self { + let project = app.session.project.clone(); + let before = app.session.active_comp_id().and_then(|id| { + let frame = project.comp_arc(id)?.frame_rate.frame_at(app.session.time()); + Some((id, project, frame)) + }); + Self { before } + } + + /// While the primary button drags (moved past a click, or held past one), a change of the + /// current frame or of the active comp's pixels since [`Self::begin`] sets + /// `property_interacting`, which stays set until the button is up (`App::frame`). Pixels are + /// compared as the frame cache does: markers count when an expression reads them, a layer's + /// audio when something draws it, before or after the edit. A click, or a drag that draws + /// the same pixels (scrolling, zooming, the work area, selecting) keeps Full. + pub(crate) fn end(self, app: &mut EffectcraftApp, ctx: &egui::Context) { + let Some((id, project, frame)) = self.before else { return }; + if app.ui.viewer.property_interacting || !ctx.input(|i| i.pointer.primary_down() && i.pointer.is_decidedly_dragging()) { + return; + } + if app.session.active_comp_id() != Some(id) { + return; + } + let (Some(was), Some(now)) = (project.comp_arc(id), app.session.project.comp_arc(id)) else { return }; + let moved = was.frame_rate.frame_at(app.session.time()) != frame; + let edited = || { + let current = &app.session.project; + let heard = current.heard_layers().union(&project.heard_layers()); + !now.same_pixels_with(&was, current.reads_markers(), &|l| heard.contains(id, l)) + }; + if moved || (!Arc::ptr_eq(&was, &now) && edited()) { + app.ui.viewer.property_interacting = true; + ctx.request_repaint(); + } + } +} diff --git a/crates/ui-egui/src/panels/properties.rs b/crates/ui-egui/src/panels/properties.rs index 454d075ec..b8e5e49de 100644 --- a/crates/ui-egui/src/panels/properties.rs +++ b/crates/ui-egui/src/panels/properties.rs @@ -67,6 +67,7 @@ pub fn show(app: &mut EffectcraftApp, ui: &mut egui::Ui, rect: Rect) { p.text(rect.center(), Align2::CENTER_CENTER, crate::i18n::tr("Select a layer to see its properties"), Tokens::ui(12.0), t.text_faint); return; }; + let watch = super::drag_preview::DragWatch::begin(app); let project = app.session.project.clone(); let expr = app.session.expr.clone(); let now = app.session.time(); @@ -190,6 +191,7 @@ pub fn show(app: &mut EffectcraftApp, ui: &mut egui::Ui, rect: Rect) { app.ui.status = e; } } + watch.end(app, &ctx); } fn section_header(app: &mut EffectcraftApp, p: &egui::Painter, x0: f32, w: f32, y: f32, title: &str, auto: &str) { diff --git a/crates/ui-egui/src/panels/timeline.rs b/crates/ui-egui/src/panels/timeline.rs index dd92ef941..26be3d29b 100644 --- a/crates/ui-egui/src/panels/timeline.rs +++ b/crates/ui-egui/src/panels/timeline.rs @@ -1981,6 +1981,7 @@ pub fn show(app: &mut EffectcraftApp, ui: &mut egui::Ui, rect: Rect) { return; }; let Some(comp) = app.session.project.comp_arc(cid) else { return }; + let watch = super::drag_preview::DragWatch::begin(app); let mut tl = layout(app, ui, rect, cid, &comp); let g = &mut tl; if !ui.input(|i| i.pointer.primary_down()) { @@ -2040,7 +2041,8 @@ pub fn show(app: &mut EffectcraftApp, ui: &mut egui::Ui, rect: Rect) { drop_targets(app, ui, g); let Rw { tl, ui_actions, .. } = rw; apply_ui_acts(app, &tl.comp, ui_actions); - apply_actions(app, ui, &tl.ctx, tl.actions); + apply_actions(app, &tl.ctx, tl.actions); + watch.end(app, &tl.ctx); } /// Applies the UI-only changes the frame collected (twirls, time, ending an undo merge). @@ -2099,11 +2101,7 @@ fn apply_ui_acts(app: &mut EffectcraftApp, comp: &Comp, ui_actions: Vec) } /// Runs the commands the frame collected through [`crate::menus::invoke`]. -fn apply_actions(app: &mut EffectcraftApp, ui: &mut egui::Ui, ctx: &egui::Context, actions: Vec<(String, serde_json::Value)>) { - if ui.input(|i| i.pointer.primary_down()) && actions.iter().any(|(id, _)| id == "prop.set") { - app.ui.viewer.property_interacting = true; - ctx.request_repaint(); - } +fn apply_actions(app: &mut EffectcraftApp, ctx: &egui::Context, actions: Vec<(String, serde_json::Value)>) { for (id, params) in actions { if id == "__endMerge" { app.session.history.merge_key = None; diff --git a/crates/ui-egui/src/state.rs b/crates/ui-egui/src/state.rs index ff1475dcb..0f7d97bc0 100644 --- a/crates/ui-egui/src/state.rs +++ b/crates/ui-egui/src/state.rs @@ -265,6 +265,9 @@ pub struct ViewerState { /// The pointer is dragging in the viewer this frame (Adaptive Resolution). #[serde(skip)] pub interacting: bool, + /// A drag outside the viewer changed the frame on screen: a property scrub in Effect Controls, + /// or any Timeline or Properties drag ([`crate::panels::drag_preview`]). Adaptive Resolution + /// draws a draft until the primary button is up, which clears it. #[serde(skip)] pub property_interacting: bool, /// Extended Viewer: the comp-space region the viewer renders this frame (comp frame plus diff --git a/crates/ui-egui/tests/it/main.rs b/crates/ui-egui/tests/it/main.rs index 6073e1c36..8b156cb51 100644 --- a/crates/ui-egui/tests/it/main.rs +++ b/crates/ui-egui/tests/it/main.rs @@ -101,6 +101,7 @@ mod ui_time_zoom_controls; mod ui_timeline_audio_levels; mod ui_timeline_effect_label; mod ui_timeline_group_select; +mod ui_timeline_interacting; mod ui_timeline_marquee; mod ui_timeline_pan; mod ui_timeline_shapes; diff --git a/crates/ui-egui/tests/it/ui_timeline_interacting.rs b/crates/ui-egui/tests/it/ui_timeline_interacting.rs new file mode 100644 index 000000000..5cf11ad0e --- /dev/null +++ b/crates/ui-egui/tests/it/ui_timeline_interacting.rs @@ -0,0 +1,229 @@ +//! Fast Previews ▸ Adaptive Resolution while dragging outside the viewer (After Effects): any +//! Timeline drag that changes the frame on screen (the time, or the comp's pixels) draws a draft +//! until release, and the first frame after the release is full resolution again. A plain click, +//! or a drag that changes nothing on screen, keeps Full. +use effectcraft_engine::Session; +use effectcraft_ui_egui::EffectcraftApp; +use egui::{Event, Pos2, pos2, vec2}; +use egui_kittest::Harness; +use serde_json::json; + +/// The Timeline of a 10 s comp at 30 fps with a solid (opacity keys at 0 s and 1 s, revealed) +/// and a 20 s precomp trimmed to 3–7 s (its source runs past both ends, so it can slip), in +/// Adaptive Resolution. +struct Fixture { + h: Harness<'static, EffectcraftApp>, + solid: u64, + pre: u64, + opacity: u64, +} + +fn fixture(mode: &str) -> Fixture { + let mut s = Session::default(); + s.execute("comp.new", json!({"name": "Inner", "width": 320, "height": 180, "frameRate": 30, "duration": 20})).unwrap(); + s.execute("comp.new", json!({"name": "Main", "width": 320, "height": 180, "frameRate": 30, "duration": 10})).unwrap(); + let solid = s.execute("layer.newSolid", json!({"name": "Box", "color": "#e04020", "width": 80, "height": 80})).unwrap()["layer"].as_u64().unwrap(); + for (time, value) in [(0.0, 0.0), (1.0, 100.0)] { + s.execute("prop.addKey", json!({"layer": solid, "path": "transform/opacity", "time": time, "value": value})).unwrap(); + } + let pre = s.execute("layer.addItem", json!({"item": "Inner"})).unwrap()["layer"].as_u64().unwrap(); + s.execute("layer.timing", json!({"layers": [pre], "in": 3.0, "out": 7.0})).unwrap(); + let comp = s.active_comp().unwrap(); + let opacity = comp.layers.iter().find(|l| l.id.0 == solid).unwrap().props.prop("transform/opacity").unwrap().uid; + s.execute("layer.select", json!({"layers": [solid]})).unwrap(); + s.execute("view.fastPreviewMode", json!({"mode": mode})).unwrap(); + let mut h = Harness::builder().with_size(vec2(1600.0, 1000.0)).build_eframe(|_| EffectcraftApp::new(s)); + h.run_steps(3); + let ctx = h.ctx.clone(); + effectcraft_ui_egui::menus::invoke(h.state_mut(), &ctx, "timeline.reveal.opacity", json!({})).unwrap(); + h.run_steps(4); + Fixture { h, solid, pre, opacity } +} + +fn rect(h: &Harness<'_, EffectcraftApp>, id: &str) -> egui::Rect { + let e = h.state().auto.find(id).unwrap_or_else(|| panic!("no {id}")); + egui::Rect::from_min_size(pos2(e.rect[0], e.rect[1]), vec2(e.rect[2], e.rect[3])) +} + +/// What a frame-changing drag must change: the project or the current time. +fn on_screen(h: &Harness<'_, EffectcraftApp>) -> (u64, effectcraft_time::Tick) { + (h.state().session.revision, h.state().session.time()) +} + +/// Presses at `from` and moves to `to` in ten steps, leaving the button down. +fn hold(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: egui::PointerButton::Primary, pressed: true, modifiers: Default::default() }); + h.step(); + for step in 1..=10 { + h.input_mut().events.push(Event::PointerMoved(from + (to - from) * (step as f32 / 10.0))); + h.step(); + } +} + +/// Releases at `at` and runs exactly one frame: that frame must already be sharp. +fn release(h: &mut Harness<'_, EffectcraftApp>, at: Pos2) { + h.input_mut().events.push(Event::PointerButton { pos: at, button: egui::PointerButton::Primary, pressed: false, modifiers: Default::default() }); + h.step(); +} + +fn draft(h: &Harness<'_, EffectcraftApp>) -> bool { + h.state().viewer_scale(1.0, 1.0) < 1.0 +} + +/// Drags from `from` by `by`: a draft while held, Full on the first frame after release. +fn assert_draft_while_dragging(f: &mut Fixture, from: Pos2, by: egui::Vec2) { + let before = on_screen(&f.h); + hold(&mut f.h, from, from + by); + assert_ne!(on_screen(&f.h), before, "the drag changed nothing on screen"); + assert!(f.h.state().ui.viewer.property_interacting, "not interacting while dragging"); + assert!(draft(&f.h), "full resolution while dragging"); + release(&mut f.h, from + by); + assert!(!f.h.state().ui.viewer.property_interacting, "still interacting after release"); + assert!(!draft(&f.h), "the first frame after release is a draft"); +} + +#[test] +fn playhead_drag_draws_a_draft() { + let mut f = fixture("adaptive"); + let ruler = rect(&f.h, "timeline.ruler"); + assert_draft_while_dragging(&mut f, pos2(ruler.min.x + 40.0, ruler.max.y - 3.0), vec2(200.0, 0.0)); +} + +#[test] +fn timecode_drag_draws_a_draft() { + let mut f = fixture("adaptive"); + let from = rect(&f.h, "timeline.timecode").center(); + assert_draft_while_dragging(&mut f, from, vec2(60.0, 0.0)); +} + +#[test] +fn layer_bar_drag_draws_a_draft() { + let mut f = fixture("adaptive"); + let from = rect(&f.h, &format!("timeline.layer.{}.bar", f.solid)).center(); + assert_draft_while_dragging(&mut f, from, vec2(60.0, 0.0)); +} + +#[test] +fn in_point_trim_draws_a_draft() { + let mut f = fixture("adaptive"); + let from = rect(&f.h, &format!("timeline.layer.{}.bar.in", f.pre)).center(); + assert_draft_while_dragging(&mut f, from, vec2(60.0, 0.0)); +} + +#[test] +fn source_slip_draws_a_draft() { + let mut f = fixture("adaptive"); + let from = rect(&f.h, &format!("timeline.layer.{}.sourcel", f.pre)).center(); + assert_draft_while_dragging(&mut f, from, vec2(60.0, 0.0)); +} + +#[test] +fn key_drag_draws_a_draft() { + let mut f = fixture("adaptive"); + let from = rect(&f.h, &format!("timeline.key.{}.0", f.opacity)).center(); + assert_draft_while_dragging(&mut f, from, vec2(60.0, 0.0)); +} + +#[test] +fn in_out_and_duration_cell_drags_draw_a_draft() { + for kind in ["in", "out", "duration"] { + let mut f = fixture("adaptive"); + effectcraft_ui_egui::panels::timeline::set_column(&mut f.h.state_mut().ui.timeline, kind, true).unwrap(); + f.h.run_steps(3); + let from = rect(&f.h, &format!("timeline.layer.{}.{kind}", f.pre)).center(); + assert_draft_while_dragging(&mut f, from, vec2(60.0, 0.0)); + } +} + +#[test] +fn properties_scrub_draws_a_draft() { + let mut f = fixture("adaptive"); + let ctx = f.h.ctx.clone(); + effectcraft_ui_egui::menus::invoke(f.h.state_mut(), &ctx, "window.panel", json!({"panel": "properties"})).unwrap(); + f.h.run_steps(3); + let from = rect(&f.h, &format!("properties.prop.{}.value", f.opacity)).center(); + assert_draft_while_dragging(&mut f, from, vec2(40.0, 0.0)); +} + +#[test] +fn work_area_drag_keeps_full_resolution() { + let mut f = fixture("adaptive"); + let from = rect(&f.h, "timeline.workArea.end").center(); + let before = f.h.state().session.active_comp().unwrap().work_area; + hold(&mut f.h, from, from + vec2(-80.0, 0.0)); + assert_ne!(f.h.state().session.active_comp().unwrap().work_area, before, "the work area moved"); + assert!(!draft(&f.h), "a work-area drag changes no pixels"); + release(&mut f.h, from); +} + +/// Scrubs the precomp's Audio Levels in the Timeline; `reads_audio` adds an expression that +/// reads audio. Returns whether the viewer drew a draft while held. +fn audio_levels_scrub(reads_audio: bool) -> bool { + let mut f = fixture("adaptive"); + if reads_audio { + let rotation = json!({"layer": f.solid, "path": "transform/rotation", "expression": "thisComp.layer(1).audioLevels; value"}); + f.h.state_mut().session.execute("prop.setExpression", rotation).unwrap(); + } + let uid = f.h.state().session.active_comp().unwrap().layers.iter().find(|l| l.id.0 == f.pre).unwrap().props.prop("audio/levels").unwrap().uid; + let tl = &mut f.h.state_mut().ui.timeline; + tl.open_layers.insert(f.pre); + tl.layer_reveal.insert(f.pre, vec!["props".into()]); + tl.reveal_props.insert(uid); + f.h.run_steps(3); + let from = rect(&f.h, &format!("timeline.prop.{uid}.value.0")).center(); + let before = on_screen(&f.h); + hold(&mut f.h, from, from + vec2(40.0, 0.0)); + assert_ne!(on_screen(&f.h), before, "the scrub changed the levels"); + let draft = draft(&f.h); + release(&mut f.h, from); + draft +} + +/// A layer's audio is pixels only when something draws it (an expression reading audio here). +#[test] +fn audio_levels_scrub_draws_a_draft_only_when_audio_is_drawn() { + assert!(audio_levels_scrub(true), "an expression reads the audio"); + assert!(!audio_levels_scrub(false), "nothing draws the audio"); +} + +/// Markers are pixels only to the expressions that read them (#1083): moving one keeps Full. +#[test] +fn marker_drag_keeps_full_resolution() { + let mut f = fixture("adaptive"); + f.h.state_mut().session.execute("comp.addMarker", json!({"time": 2.0})).unwrap(); + f.h.run_steps(3); + let from = rect(&f.h, "timeline.marker.0").center(); + let before = f.h.state().session.active_comp().unwrap().markers[0].time; + hold(&mut f.h, from, from + vec2(80.0, 0.0)); + assert_ne!(f.h.state().session.active_comp().unwrap().markers[0].time, before, "the marker moved"); + assert!(!draft(&f.h), "a marker no expression reads changes no pixels"); + release(&mut f.h, from); +} + +#[test] +fn timeline_click_keeps_full_resolution() { + let mut f = fixture("adaptive"); + let point = rect(&f.h, "timeline.ruler").center(); + let before = f.h.state().session.time(); + f.h.input_mut().events.push(Event::PointerMoved(point)); + f.h.step(); + f.h.input_mut().events.push(Event::PointerButton { pos: point, button: egui::PointerButton::Primary, pressed: true, modifiers: Default::default() }); + f.h.run_steps(3); + assert_ne!(f.h.state().session.time(), before, "the click moved the playhead"); + assert!(!draft(&f.h)); + release(&mut f.h, point); +} + +/// The drag is still noted, but Fast Previews Off never lowers the resolution. +#[test] +fn fast_previews_off_keeps_full_resolution() { + let mut f = fixture("off"); + let ruler = rect(&f.h, "timeline.ruler"); + let from = pos2(ruler.min.x + 40.0, ruler.max.y - 3.0); + hold(&mut f.h, from, from + vec2(200.0, 0.0)); + assert!(f.h.state().ui.viewer.property_interacting); + assert!(!draft(&f.h)); + release(&mut f.h, from); +}