Skip to content

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

fix(ui): ensure turning View -> Snap off disables all snapping (#5782)

seotarek422bea8beb61
diff --git a/crates/ui-egui/src/snap_ui.rs b/crates/ui-egui/src/snap_ui.rs
index e0b122c37..392363c31 100644
--- a/crates/ui-egui/src/snap_ui.rs
+++ b/crates/ui-egui/src/snap_ui.rs
@@ -254,7 +254,7 @@ fn begin(app: &mut PhotocraftApp, p: [f64; 2], mods: egui::Modifiers) {
// A moving selection never snaps back to where it started.
targets = targets.filtered(|k| k != SnapKind::Selection);
}
- let smart = if matches!(gesture, Gesture::Move { .. }) && smart_on(app) { build(app, &exclude, true) } else { SnapTargets::default() };
+ let smart = if snap_on(app) && matches!(gesture, Gesture::Move { .. }) && smart_on(app) { build(app, &exclude, true) } else { SnapTargets::default() };
app.prefs_rt.snap = Some(ActiveSnap { gesture, start: p, targets, smart, disabled: false });
}
@@ -282,7 +282,7 @@ fn pixel_round(app: &PhotocraftApp, p: [f64; 2]) -> [f64; 2] {
fn apply(app: &mut PhotocraftApp, p: [f64; 2], mods: egui::Modifiers) -> [f64; 2] {
let tol = tolerance(app);
let Some(snap) = app.prefs_rt.snap.as_ref() else { return p };
- if snap.disabled {
+ if !snap_on(app) || snap.disabled {
app.prefs_rt.snap_lines.clear();
return p;
}
@@ -680,6 +680,20 @@ mod tests {
assert!(!override_held(cmd) && override_held(ctrl));
}
+ #[test]
+ fn snap_off_disables_all_snapping_even_with_smart_guides() {
+ let mut app = app_with_box();
+ app.ui.tool = Tool::Move;
+ assert!(app.ui.view.show.smart_guides);
+ app.ui.extras.snap = false;
+ app.ui.tool_options.move_auto_select = false;
+ let m = egui::Modifiers::NONE;
+ crate::canvas::tool_event(&mut app, ToolEvent::Down { x: 60.0, y: 60.0, pressure: 1.0 }, m);
+ crate::canvas::tool_event(&mut app, ToolEvent::Up { x: 323.0, y: 100.0 }, m);
+ assert_eq!(mover_bounds(&app).x0, 303);
+ assert!(app.prefs_rt.snap_lines.is_empty());
+ }
+
#[test]
fn marquee_corner_snaps_to_guides_and_document_edges() {
let mut app = app_with_box();
diff --git a/crates/ui-egui/src/view_cmds.rs b/crates/ui-egui/src/view_cmds.rs
index 1da1f918d..b71ce497e 100644
--- a/crates/ui-egui/src/view_cmds.rs
+++ b/crates/ui-egui/src/view_cmds.rs
@@ -302,6 +302,9 @@ fn remembered(app: &PhotocraftApp) -> Value {
if e.guides != def.guides {
m.insert("guides".into(), Value::Bool(e.guides));
}
+ if e.snap != def.snap {
+ m.insert("snap".into(), Value::Bool(e.snap));
+ }
if let Some(show) = changed_flags(v.show, Show::default(), &SHOW_KEYS, show_slot) {
m.insert("show".into(), show);
}
@@ -342,6 +345,9 @@ pub fn restore(app: &mut PhotocraftApp) {
if let Some(on) = flag("guides") {
app.ui.extras.guides = on;
}
+ if let Some(on) = flag("snap") {
+ app.ui.extras.snap = on;
+ }
for (k, v) in saved.get("show").and_then(Value::as_object).into_iter().flatten() {
if let (Some(slot), Some(on)) = (show_slot(&mut app.ui.view.show, k), v.as_bool()) {
*slot = on;