EffectCraft: An open-source motion graphics and compositing tool written by scratch in pure Rust
Merge pull request #916 from pravdomil/claude/composition-maximize-zoom-fit-86627e
Brandon Thomas2ea7220e4904
diff --cc crates/ui-egui/src/menus.rsindex 951f827d0,71f888fc5..25b2fc23a--- a/crates/ui-egui/src/menus.rs+++ b/crates/ui-egui/src/menus.rs@@@ -553,14 -525,9 +553,15 @@@ pub fn invoke(app: &mut EffectcraftAppapp.stop();return Ok(Value::Null);}+ // The focused Layer panel has its own magnification; otherwise the Composition viewer.+ "view.fit" if app.ui.focused == PanelKind::Layer => {+ app.ui.layer_zoom = None;+ app.ui.layer_pan = [0.0, 0.0];+ return Ok(Value::Null);+ }"view.fit" => {app.ui.viewer.zoom = None;+ app.ui.viewer.fit_up_to_100 = false;app.ui.viewer.pan = [0.0, 0.0];return Ok(Value::Null);}diff --cc crates/ui-egui/src/panels/layer_panel.rsindex e8cae2cd1,a5952d602..c0ebf0133--- a/crates/ui-egui/src/panels/layer_panel.rs+++ b/crates/ui-egui/src/panels/layer_panel.rs@@@ -104,32 -83,10 +104,33 @@@ pub fn show(app: &mut EffectcraftApp, up.rect_filled(top, 0.0, t.panel_bg);p.text(pos2(top.min.x + 10.0, top.center().y), Align2::LEFT_CENTER, tr_args("Layer: {}", &[&layer.name]), Tokens::semibold(12.0), t.text);app.auto.add("layerPanel.title", top, &layer.name);- // Bottom bar: View menu.+ // Canvas geometry: Fit, or the panel's own magnification and pan.let bar = Rect::from_min_max(pos2(rect.min.x, rect.max.y - 30.0), rect.max);+ let canvas_bottom = if roto.is_some() { bar.min.y - 18.0 } else { bar.min.y };+ let area = Rect::from_min_max(pos2(rect.min.x, top.max.y), pos2(rect.max.x, canvas_bottom));+ // The layer's effect bounds, in effect space (where paint and Roto Brush strokes are).+ let [lw, lh] = effectcraft_engine::render::effect_bounds(&app.session.project, &comp, &layer).0;+ let fit = ((area.width() - 40.0) as f64 / lw).min((area.height() - 40.0) as f64 / lh).max(0.01) as f32;+ ctx.data_mut(|d| d.insert_temp(fit_id(), fit));+ let zoom = app.ui.layer_zoom.filter(|z| z.is_finite() && *z > 0.0).unwrap_or(fit) as f64;+ let pan = match app.ui.layer_pan {+ [x, y] if x.is_finite() && y.is_finite() && app.ui.layer_zoom.is_some() => vec2(x, y),+ _ => vec2(0.0, 0.0),+ };+ let origin = area.center() + pan - vec2((lw * zoom / 2.0) as f32, (lh * zoom / 2.0) as f32);++ // Bottom bar: magnification and View menus.p.rect_filled(bar, 0.0, t.panel_bg);- p.text(pos2(bar.min.x + 10.0, bar.center().y), Align2::LEFT_CENTER, tr("View:"), Tokens::ui(12.0), t.text_dim);+ let mr = Rect::from_min_size(pos2(bar.min.x + 8.0, bar.center().y - 10.0), vec2(80.0, 20.0));- if let Some(z) =- super::viewer_tools::magnification_menu(app, ui, mr, "layer-mag", "layerPanel.magnification", "layerMagnification", zoom as f32, app.ui.layer_zoom, fit)++ if let Some((z, up)) =++ super::viewer_tools::magnification_menu(app, ui, mr, "layer-mag", "layerPanel.magnification", "layerMagnification", zoom as f32, app.ui.layer_zoom, false)+ {- app.ui.layer_zoom = z;++ // The Layer panel keeps no fit mode: Fit up to 100% is the fit, at most 100 %.++ app.ui.layer_zoom = if up { Some(fit.min(1.0 / ui.ctx().pixels_per_point())) } else { z };+ app.ui.layer_pan = [0.0, 0.0];+ }+ let vx = mr.max.x + 10.0;+ p.text(pos2(vx, bar.center().y), Align2::LEFT_CENTER, tr("View:"), Tokens::ui(12.0), t.text_dim);let mut names = vec!["Masks".to_string()];if let Some(fx) = layer.effects() {names.extend(fx.groups().map(|g| g.name.clone()));diff --cc crates/ui-egui/src/panels/viewer_tools.rsindex 2f7862e2d,3759c875c..fa1985056--- a/crates/ui-egui/src/panels/viewer_tools.rs+++ b/crates/ui-egui/src/panels/viewer_tools.rs@@@ -510,50 -509,6 +510,53 @@@ fn toggle_popup(ui: &egui::Ui, id: &strui.data_mut(|d| d.insert_temp(pid, !open));}+/// A magnification ratio dropdown (the Composition and Layer panels' footers): `zoom` is the+/// shown magnification and `current` the stored one (None = fit), both in points per pixel.+/// Returns the chosen magnification (`Some(None)` = Fit).+#[allow(clippy::too_many_arguments)]+pub(crate) fn magnification_menu(+ app: &mut EffectcraftApp,+ ui: &mut egui::Ui,+ r: Rect,+ id: &str,+ auto_id: &str,+ auto_items: &str,+ zoom: f32,+ current: Option<f32>,- fit: f32,- ) -> Option<Option<f32>> {++ fit_up: bool,++) -> Option<(Option<f32>, bool)> {+ let t = app.tokens;+ let ppp = ui.ctx().pixels_per_point();+ let pop = format!("{id}-pop");+ let response =+ widgets::dropdown(ui, r, &magnification_label(zoom * ppp * 100.0), &t, egui::Id::new(id)).on_hover_text(crate::i18n::tr("Magnification ratio popup"));+ if response.clicked() {+ toggle_popup(ui, &pop);+ }+ app.auto.add_response(auto_id, &response, "Magnification");+ const MAGS: [&str; 15] = ["Fit", "Fit up to 100%", "-", "1.5%", "3.1%", "6.25%", "12.5%", "25%", "33.3%", "50%", "100%", "200%", "400%", "800%", "1600%"];+ let cur = (zoom * ppp * 100.0 * 10.0).round() / 10.0;+ let items: Vec<(String, bool)> = MAGS+ .iter()+ .map(|m| {+ let on = match *m {- "Fit" => current.is_none(),++ "Fit" => current.is_none() && !fit_up,++ "Fit up to 100%" => current.is_none() && fit_up,+ s => s.trim_end_matches('%').parse::<f32>().is_ok_and(|v| current.is_some() && (v - cur).abs() < 0.06),+ };+ (m.to_string(), on)+ })+ .collect();+ let i = popup(app, ui, &pop, r, &items, auto_items)?;++ // `(zoom, fit up to 100 %)`: Fit and Fit up to 100% leave the zoom unset (it follows the++ // panel size).+ match *MAGS.get(i)? {- "Fit" => Some(None),- "Fit up to 100%" => Some(Some(fit.min(1.0 / ppp))),- s => s.trim_end_matches('%').parse::<f32>().ok().map(|v| Some(v / 100.0 / ppp)),++ "Fit" => Some((None, false)),++ "Fit up to 100%" => Some((None, true)),++ s => s.trim_end_matches('%').parse::<f32>().ok().map(|v| (Some(v / 100.0 / ppp), false)),+ }+}+/// Magnification as After Effects shows it: "136.1 %", "100 %".pub(crate) fn magnification_label(pct: f32) -> String {if (pct - pct.round()).abs() < 0.05 { format!("{:.0} %", pct.round()) } else { format!("{pct:.1} %") }@@@ -621,9 -576,32 +624,11 @@@ fn bottom_bar_contents(app: &mut Effect// Magnification.let r = Rect::from_min_size(pos2(x, cy - 10.0), vec2(80.0, 20.0));- if let Some(z) = magnification_menu(app, ui, r, "vw-mag", "viewer.magnification", "magnification", zoom, app.ui.viewer.zoom, fit) {- let response = widgets::dropdown(ui, r, &magnification_label(zoom * ppp * 100.0), &t, egui::Id::new("vw-mag"))- .on_hover_text(crate::i18n::tr("Magnification ratio popup"));- if response.clicked() {- toggle_popup(ui, "vw-mag-pop");- }- app.auto.add_response("viewer.magnification", &response, "Magnification");- const MAGS: [&str; 15] = ["Fit", "Fit up to 100%", "-", "1.5%", "3.1%", "6.25%", "12.5%", "25%", "33.3%", "50%", "100%", "200%", "400%", "800%", "1600%"];- let cur = (zoom * ppp * 100.0 * 10.0).round() / 10.0;- let items: Vec<(String, bool)> = MAGS- .iter()- .map(|m| {- let on = match *m {- "Fit" => app.ui.viewer.zoom.is_none() && !app.ui.viewer.fit_up_to_100,- "Fit up to 100%" => app.ui.viewer.zoom.is_none() && app.ui.viewer.fit_up_to_100,- s => s.trim_end_matches('%').parse::<f32>().is_ok_and(|v| app.ui.viewer.zoom.is_some() && (v - cur).abs() < 0.06),- };- (m.to_string(), on)- })- .collect();- if let Some(i) = popup(app, ui, "vw-mag-pop", r, &items, "magnification") {++ let fit_up = app.ui.viewer.fit_up_to_100;++ if let Some((z, up)) = magnification_menu(app, ui, r, "vw-mag", "viewer.magnification", "magnification", zoom, app.ui.viewer.zoom, fit_up) {app.ui.viewer.pan = [0.0, 0.0];- app.ui.viewer.fit_up_to_100 = MAGS[i] == "Fit up to 100%";- app.ui.viewer.zoom = match MAGS[i] {- "Fit" | "Fit up to 100%" => None,- s => s.trim_end_matches('%').parse::<f32>().ok().map(|v| v / 100.0 / ppp),- };++ app.ui.viewer.fit_up_to_100 = up;+ app.ui.viewer.zoom = z;}x = r.max.x + 4.0;diff --cc crates/ui-egui/src/state.rsindex 31f94b83e,04fe1e32f..a9ac16398--- a/crates/ui-egui/src/state.rs+++ b/crates/ui-egui/src/state.rs@@@ -233,8 -273,11 +233,10 @@@ pub use effectcraft_engine::project::Repub struct ViewerState {/// Magnification (1 = 100%); None = fit.pub zoom: Option<f32>,+ /// Fit up to 100%: while fitting (`zoom` is None), the comp is never magnified past 100%.+ pub fit_up_to_100: bool,/// Pan offset in points from the centred position.pub pan: [f32; 2],- pub res: Resolution,pub transparency_grid: bool,pub show_masks: bool,pub safe_margins: bool,@@@ -272,7 -315,9 +274,8 @@@ impl Default for ViewerStatefn default() -> Self {ViewerState {zoom: None,+ fit_up_to_100: false,pan: [0.0, 0.0],- res: Resolution::Auto,transparency_grid: false,show_masks: true,safe_margins: false,diff --cc crates/ui-egui/tests/ui_viewer.rsindex c5289ded3,a447d350b..07036ca81--- a/crates/ui-egui/tests/ui_viewer.rs+++ b/crates/ui-egui/tests/ui_viewer.rs@@@ -2025,24 -1949,44 +2025,66 @@@ fn vertex_tools_edit_motion_path_keyfraassert_eq!(keys(&h).len(), 3);}+/// #882: at 100 % (and 200 %) on a scaled display the frame's corner sits on a physical pixel, so+/// every comp pixel covers whole screen pixels instead of being blended with its neighbour.+#[test]+fn frame_sits_on_the_physical_pixel_grid() {+ for ppp in [1.0f32, 1.25, 1.5] {+ let mut s = Session::default();+ s.execute("comp.new", json!({"name": "Grid", "width": 301, "height": 201, "duration": 1})).unwrap();+ let mut h = Harness::builder().with_size(egui::vec2(1600.0, 1000.0)).with_pixels_per_point(ppp).build_eframe(move |_| EffectcraftApp::new(s));+ h.run_steps(3);+ for (mag, pan) in [(1.0f32, [0.3f32, 0.37]), (2.0, [-0.45, 0.2])] {+ h.state_mut().ui.viewer.zoom = Some(mag / ppp);+ h.state_mut().ui.viewer.pan = pan;+ h.run_steps(2);+ let c = rect(&h, "viewer.comp");+ for v in [c.min.x * ppp, c.min.y * ppp] {+ assert!((v - v.round()).abs() < 1e-3, "ppp {ppp}, {mag}×: corner at {v} physical px");+ }+ assert!((c.width() * ppp - 301.0 * mag).abs() < 1e-2, "ppp {ppp}, {mag}×: {} physical px wide", c.width() * ppp);+ }+ }+}+++ /// Magnification ▸ Fit up to 100% keeps fitting as the panel changes size (Maximize Panel), like+ /// Fit, and never magnifies past 100%.+ #[test]+ fn fit_up_to_100_follows_the_maximized_panel() {+ use effectcraft_ui_egui::dock::PanelKind;+ let pick = |h: &mut Harness<'_, EffectcraftApp>| {+ let at = rect(h, "viewer.magnification").center();+ click(h, at);+ let at = rect(h, "viewer.magnification.1").center();+ click(h, at);+ assert_eq!(h.state().ui.viewer.zoom, None, "a fit mode, not a fixed magnification");+ assert!(h.state().ui.viewer.fit_up_to_100);+ h.run_steps(2);+ };+ // A comp larger than the panel: it fills the docked panel, then the maximized one.+ let mut h = Harness::builder().with_size(egui::vec2(1600.0, 1000.0)).build_eframe(|_| {+ let mut s = Session::default();+ s.execute("comp.new", json!({"name": "Big", "width": 3840, "height": 2160, "duration": 4})).unwrap();+ EffectcraftApp::new(s)+ });+ h.run_steps(3);+ pick(&mut h);+ let docked = rect(&h, "viewer.comp").width();+ h.state_mut().toggle_maximize(PanelKind::Composition);+ h.run_steps(3);+ let area = rect(&h, "viewer.area");+ let maximized = rect(&h, "viewer.comp");+ assert!(maximized.width() > docked + 100.0, "{docked} -> {}", maximized.width());+ assert!(area.contains_rect(maximized), "{maximized:?} in {area:?}");+ h.state_mut().toggle_maximize(PanelKind::Composition);+ h.run_steps(3);+ assert_eq!(rect(&h, "viewer.comp").width(), docked, "and back when the panel is restored");+ // A comp smaller than the panel stops at 100%.+ let mut h = harness();+ pick(&mut h);+ let ppp = h.ctx.pixels_per_point();+ assert!(rect(&h, "viewer.comp").width() <= 640.0 / ppp + 0.01);+ h.state_mut().toggle_maximize(PanelKind::Composition);+ h.run_steps(3);+ assert!((rect(&h, "viewer.comp").width() - 640.0 / ppp).abs() < 0.01);+ }