Skip to content

PhotoCraft: An open-source, photo editor written by scratch in pure Rust

Match Zoom: matched views stay within each document's zoom range (#7036)

David Mannb2f9c45c6a2d
diff --git a/crates/ui-egui/src/view_cmds.rs b/crates/ui-egui/src/view_cmds.rs
index 1da1f918d..56f5071a2 100644
--- a/crates/ui-egui/src/view_cmds.rs
+++ b/crates/ui-egui/src/view_cmds.rs
@@ -861,9 +861,12 @@ fn arrange(app: &mut PhotocraftApp, k: &str, p: &Value) -> Result<Value, String>
let src = app.ui.views.get(i).cloned().ok_or("no document")?;
let (zoom, loc, rot) =
(matches!(k, "matchZoom" | "matchAll"), matches!(k, "matchLocation" | "matchAll"), matches!(k, "matchRotation" | "matchAll"));
- let apply = |v: &mut crate::state::View| {
+ // Each view stays within its own document's zoom range (a view not drawn yet has no
+ // `doc_size`, so the size comes from the document).
+ let sizes: Vec<[u32; 2]> = app.session.documents().iter().map(|d| [d.doc.size.width, d.doc.size.height]).collect();
+ let apply = |v: &mut crate::state::View, doc: usize| {
if zoom {
- v.zoom = src.zoom;
+ v.zoom = crate::zoom_levels::clamp(src.zoom, sizes.get(doc).copied().unwrap_or(v.doc_size));
}
if loc {
v.center = src.center;
@@ -873,8 +876,8 @@ fn arrange(app: &mut PhotocraftApp, k: &str, p: &Value) -> Result<Value, String>
}
v.fit_pending = false;
};
- app.ui.views.iter_mut().for_each(apply);
- app.ui.windows.iter_mut().for_each(|w| apply(&mut w.view));
+ app.ui.views.iter_mut().enumerate().for_each(|(d, v)| apply(v, d));
+ app.ui.windows.iter_mut().for_each(|w| apply(&mut w.view, w.document));
Ok(json!({"zoom": src.zoom, "center": src.center, "rotation": src.rotation}))
}
_ => Err(format!("unknown arrangement {k}")),
diff --git a/crates/ui-egui/src/view_cmds/tests.rs b/crates/ui-egui/src/view_cmds/tests.rs
index 1abe86355..a2d37d6e7 100644
--- a/crates/ui-egui/src/view_cmds/tests.rs
+++ b/crates/ui-egui/src/view_cmds/tests.rs
@@ -183,6 +183,28 @@ fn arrange_layouts_floating_windows_and_matching() {
assert_eq!(app.ui.windows.len(), 1);
}
+/// Match Zoom clamps the matched zoom to each document's own range: a 10 x 10 document can't go
+/// below the zoom at which its shorter side is 2 px, even when the source view is lower.
+#[test]
+fn match_zoom_clamps_to_each_documents_zoom_range() {
+ let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default());
+ app.run("file.new", json!({"width": 10, "height": 10})).unwrap();
+ app.run("file.new", json!({"width": 2000, "height": 2000})).unwrap();
+ app.sync_views();
+ let ctx = egui::Context::default();
+ assert_eq!(app.session.active_index(), Some(1));
+ app.ui.views[1].zoom = 0.05;
+ menu(&mut app, &ctx, "window.arrange.matchZoom", json!({})).unwrap();
+ assert_eq!(app.ui.views[1].zoom, 0.05);
+ assert_eq!(app.ui.views[0].zoom, crate::zoom_levels::min([10, 10]));
+ // Floating windows too, before their first frame (no `doc_size` yet).
+ menu(&mut app, &ctx, "window.arrange.floatAllInWindows", json!({})).unwrap();
+ app.ui.views[1].zoom = 0.05;
+ menu(&mut app, &ctx, "window.arrange.matchZoom", json!({})).unwrap();
+ assert_eq!(app.ui.windows.len(), 2);
+ assert!(app.ui.windows.iter().all(|w| w.view.zoom == if w.document == 0 { crate::zoom_levels::min([10, 10]) } else { 0.05 }));
+}
+
#[test]
fn two_up_and_stacked_cells_keep_their_existing_boundaries() {
let rect = egui::Rect::from_min_max(egui::pos2(40.0, 75.0), egui::pos2(940.0, 675.0));