Skip to content

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

Zoom tool: double-click zooms to 100% (#2508) (#2527)

Valentin Klein28edd59339dd
diff --git a/contributors/people.toml b/contributors/people.toml
index 140a9acb6..6207ffa32 100644
--- a/contributors/people.toml
+++ b/contributors/people.toml
@@ -74,3 +74,6 @@ sync_github_name = true
real_name = "Ahmed Moussa"
display_name = "Ahmed Moussa"
sync_github_name = false
+
+[people.vangel76]
+sync_github_name = true
diff --git a/crates/ui-egui/src/panels.rs b/crates/ui-egui/src/panels.rs
index 3a1d36456..7152f15e8 100644
--- a/crates/ui-egui/src/panels.rs
+++ b/crates/ui-egui/src/panels.rs
@@ -149,6 +149,12 @@ pub fn toolbar(app: &mut PhotocraftApp, ui: &mut egui::Ui) {
// With no document open there is nothing to fit.
let _ = app.run("view.fitOnScreen", json!({}));
}
+ // Double-clicking the Zoom tool zooms to 100% (Photoshop).
+ if resp.double_clicked() && tool == Tool::Zoom {
+ app.ui.tool = tool;
+ // With no document open there is nothing to zoom.
+ let _ = app.run("view.actualPixels", json!({}));
+ }
// Right-click or long-press opens the flyout (Photoshop).
let held_for =
resp.is_pointer_button_down_on().then(|| ui.input(|i| i.pointer.press_start_time().map(|t0| i.time - t0))).flatten();
@@ -3898,16 +3904,20 @@ mod toolbar_tests {
}
}
- /// The Hand tool's toolbar button: tool buttons have no label, so find it by slot order.
- fn hand_button(h: &egui_kittest::Harness<'_, PhotocraftApp>) -> egui::Pos2 {
+ /// A tool's toolbar button: tool buttons have no label, so find it by slot order.
+ fn tool_button(h: &egui_kittest::Harness<'_, PhotocraftApp>, tool: Tool) -> egui::Pos2 {
let size = egui::Vec2::splat(if Tokens::get(&h.ctx).pro { 30.0 } else { 36.0 });
let buttons: Vec<Rect> = h.ctx.viewport(|v| {
v.prev_pass.widgets.layers().flat_map(|(_, w)| w.iter()).filter(|w| w.rect.size() == size && w.sense.senses_click()).map(|w| w.rect).collect()
});
- let index = TOOL_SECTIONS.iter().flat_map(|section| section.iter()).position(|slot| slot.contains(&Tool::Hand)).unwrap();
+ let index = TOOL_SECTIONS.iter().flat_map(|section| section.iter()).position(|slot| slot.contains(&tool)).unwrap();
buttons[index].center()
}
+ fn hand_button(h: &egui_kittest::Harness<'_, PhotocraftApp>) -> egui::Pos2 {
+ tool_button(h, Tool::Hand)
+ }
+
fn toolbar_harness(app: PhotocraftApp) -> egui_kittest::Harness<'static, PhotocraftApp> {
// 60 fps steps, so two clicks a few frames apart are a double-click.
let mut h = egui_kittest::Harness::builder().with_size(vec2(800.0, 1400.0)).with_step_dt(1.0 / 60.0).build_ui_state(
@@ -3956,6 +3966,41 @@ mod toolbar_tests {
assert_eq!(h.state().ui.tool, Tool::Hand);
assert!(h.state().session.active().is_none());
}
+
+ /// Double-clicking the Zoom tool zooms to 100% (Photoshop); a single click only picks the tool.
+ #[test]
+ fn double_clicking_the_zoom_tool_zooms_to_actual_pixels() {
+ let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default());
+ app.run("file.new", json!({"width": 64, "height": 64})).unwrap();
+ app.ui.tool = Tool::Move;
+ app.ui.views[0].fit_pending = true;
+ app.ui.views[0].zoom = 0.25;
+ let mut h = toolbar_harness(app);
+ let p = tool_button(&h, Tool::Zoom);
+ click(&mut h, p);
+ assert_eq!(h.state().ui.tool, Tool::Zoom);
+ assert_eq!(h.state().ui.views[0].zoom, 0.25, "a single click doesn't zoom");
+ // Past the double-click window, so the next two clicks are a fresh double-click.
+ h.run_steps(40);
+ click(&mut h, p);
+ click(&mut h, p);
+ assert_eq!(h.state().ui.tool, Tool::Zoom);
+ assert_eq!(h.state().ui.views[0].zoom, 1.0, "a double-click zooms to 100%");
+ assert!(!h.state().ui.views[0].fit_pending, "100% replaces a pending fit");
+ }
+
+ /// With no document open the double-click still picks the tool and doesn't panic.
+ #[test]
+ fn double_clicking_the_zoom_tool_without_a_document_is_harmless() {
+ let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default());
+ app.ui.tool = Tool::Move;
+ let mut h = toolbar_harness(app);
+ let p = tool_button(&h, Tool::Zoom);
+ click(&mut h, p);
+ click(&mut h, p);
+ assert_eq!(h.state().ui.tool, Tool::Zoom);
+ assert!(h.state().session.active().is_none());
+ }
}
#[cfg(test)]