Skip to content

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

fix(zoom): Alt-click with the Zoom tool on desktops that take Alt+click (#3137) (#3164)

Shehan Kavishkad67e28c11804
diff --git a/crates/ui-egui/src/alt_click_tests.rs b/crates/ui-egui/src/alt_click_tests.rs
index d4a20b04a..5e0e9d683 100644
--- a/crates/ui-egui/src/alt_click_tests.rs
+++ b/crates/ui-egui/src/alt_click_tests.rs
@@ -1,5 +1,5 @@
//! ⌥-click through the real canvas: the Brush's temporary Eyedropper (#1659) and the Clone
-//! Stamp's source point and painting (#1658).
+//! Stamp's source point and painting (#1658), the Zoom tool's Zoom Out (#3137).
use egui::{Modifiers, PointerButton, Pos2, vec2};
use egui_kittest::Harness;
@@ -179,7 +179,7 @@ fn grabbed_alt_click(h: &mut Harness<'static, PhotocraftApp>, x: f32, y: f32, re
#[test]
fn an_alt_click_the_window_manager_took_says_so() {
- for tool in [Tool::Brush, Tool::Pencil, Tool::CloneStamp, Tool::Healing] {
+ for tool in [Tool::Brush, Tool::Pencil, Tool::CloneStamp, Tool::Healing, Tool::Zoom] {
let mut h = harness(two_colours());
h.state_mut().ui.tool = tool;
grabbed_alt_click(&mut h, 50.0, 50.0, true);
@@ -229,3 +229,19 @@ fn a_latched_alt_samples_and_sets_the_clone_source_without_the_key() {
click(&mut h, 150.0, 50.0, Modifiers::NONE);
assert!(h.state().session.presets.clone.active().source.is_some());
}
+
+#[test]
+fn alt_click_with_the_zoom_tool_zooms_out_held_or_latched() {
+ for (sticky, scrubby, m) in [(false, true, Modifiers::ALT), (true, true, Modifiers::NONE), (true, false, Modifiers::NONE)] {
+ let mut h = harness(two_colours());
+ h.state_mut().ui.tool = Tool::Zoom;
+ h.state_mut().ui.tool_options.zoom_scrubby = scrubby;
+ h.state_mut().ui.shell.sticky_alt = sticky;
+ click(&mut h, 50.0, 50.0, m);
+ assert_eq!(h.state().ui.views[0].zoom, 2.0 / 3.0, "sticky={sticky} scrubby={scrubby}");
+ // Without ⌥ the click zooms in again.
+ h.state_mut().ui.shell.sticky_alt = false;
+ click(&mut h, 50.0, 50.0, Modifiers::NONE);
+ assert_eq!(h.state().ui.views[0].zoom, 1.0, "sticky={sticky} scrubby={scrubby}");
+ }
+}
diff --git a/crates/ui-egui/src/alt_grab.rs b/crates/ui-egui/src/alt_grab.rs
index 0ba09bad7..fc0f3f1c8 100644
--- a/crates/ui-egui/src/alt_grab.rs
+++ b/crates/ui-egui/src/alt_grab.rs
@@ -2,8 +2,8 @@
//!
//! Many Linux desktops move a window with Alt+drag: Cinnamon (Linux Mint's default), MATE, Xfce,
//! and KDE before Plasma 6. Their window manager grabs every Alt+left press, so the click never
-//! reaches PhotoCraft: the Brush's ⌥ Eyedropper samples nothing and the Clone Stamp gets no source
-//! point, while the ⌥ cursor still shows (the keyboard is not grabbed until the press). Nothing in
+//! reaches PhotoCraft: the Brush's ⌥ Eyedropper samples nothing, the Clone Stamp gets no source
+//! point and the Zoom tool does not zoom out (#3137), while the ⌥ cursor still shows (the keyboard is not grabbed until the press). Nothing in
//! the app can win that grab back, but its signature is clear: the pointer "leaves" the window from
//! well inside the canvas while ⌥ is held with a tool whose ⌥-click matters (X11 and Wayland report
//! a grab as the pointer leaving), and comes back to a window that still has the keyboard focus
@@ -27,9 +27,9 @@ struct Watch {
pending: bool,
}
-/// Tools whose ⌥-click does something: the ⌥ Eyedropper and the clone source point.
+/// Tools whose ⌥-click does something: the ⌥ Eyedropper, the clone source point and Zoom Out.
pub(crate) fn alt_click_tool(tool: Tool, mods: egui::Modifiers) -> bool {
- crate::canvas::alt_samples(tool, mods) || (mods.alt && !mods.ctrl && matches!(tool, Tool::CloneStamp | Tool::Healing))
+ crate::canvas::alt_samples(tool, mods) || (mods.alt && !mods.ctrl && matches!(tool, Tool::CloneStamp | Tool::Healing | Tool::Zoom))
}
/// The status-bar message for an ⌥-click the desktop took.
diff --git a/crates/ui-egui/src/canvas.rs b/crates/ui-egui/src/canvas.rs
index d18939433..211eb7ebc 100644
--- a/crates/ui-egui/src/canvas.rs
+++ b/crates/ui-egui/src/canvas.rs
@@ -2770,11 +2770,13 @@ pub fn canvas_view(app: &mut PhotocraftApp, ui: &mut egui::Ui, idx: usize, rect:
// ⌘ held: this frame's gestures go to the Move tool (`PhotocraftApp::active_tool`). The Hand
// and Zoom never reach `tool_event`, so only the Move needs the state machine to know.
app.tool_override = (temporary == Some(crate::hold_keys::Temporary::Move)).then_some(Tool::Move);
- // Zoom direction: the temporary zoom key decides, else ⌥ (Zoom tool).
+ // Zoom direction: the temporary zoom key decides, else ⌥ (Zoom tool), held or latched in
+ // Window › Modifier Keys (the way around a desktop that takes Alt+click, `alt_grab`).
+ let sticky_alt = app.ui.shell.sticky_alt;
let zoom_out = |alt: bool| match temporary {
Some(crate::hold_keys::Temporary::ZoomOut) => true,
Some(crate::hold_keys::Temporary::ZoomIn) => false,
- _ => alt,
+ _ => alt || sticky_alt,
};
if under_dialog {
diff --git a/crates/ui-egui/src/zoom_tool.rs b/crates/ui-egui/src/zoom_tool.rs
index b525dd1ea..e28e7e9c0 100644
--- a/crates/ui-egui/src/zoom_tool.rs
+++ b/crates/ui-egui/src/zoom_tool.rs
@@ -70,7 +70,8 @@ pub fn drag(app: &PhotocraftApp, ctx: &egui::Context, view: &mut View, xf: &View
if b.stopped {
ctx.data_mut(|d| d.remove::<ZoomDrag>(id()));
if let Some(to) = z.rect_to {
- zoom_to_rect(view, xf, Rect::from_two_pos(z.anchor, to), z.anchor, ctx.input(|i| i.modifiers.alt), app.ppp);
+ let out = crate::workspace_ui::sticky_mods(app, ctx.input(|i| i.modifiers)).alt;
+ zoom_to_rect(view, xf, Rect::from_two_pos(z.anchor, to), z.anchor, out, app.ppp);
}
}
true