PhotoCraft: An open-source, photo editor written by scratch in pure Rust
Rulers: Alt-drag makes a guide of the other orientation (#5698) (#5885)
Michael Higginsbe4cadd83ddb
diff --git a/crates/ui-egui/src/rulers.rs b/crates/ui-egui/src/rulers.rsindex 7f9086006..972d7c034 100644--- a/crates/ui-egui/src/rulers.rs+++ b/crates/ui-egui/src/rulers.rs@@ -431,6 +431,9 @@ pub fn draw_rulers(app: &mut PhotocraftApp, ui: &mut egui::Ui, full: Rect, xf: &ui.ctx().set_cursor_icon(if vertical { egui::CursorIcon::ResizeHorizontal } else { egui::CursorIcon::ResizeVertical });}if let Some(pos) = resp.interact_pointer_pos().filter(|_| salt != "ruler-corner" && (resp.dragged() || resp.drag_started())) {+ // ⌥/Alt turns the guide the other way, as in Photoshop: a vertical guide out of the top+ // ruler, a horizontal one out of the left (#5698), switching as Alt goes up and down.+ let vertical = vertical != ui.input(|i| i.modifiers.alt);let d = xf.to_doc(pos);let along = if vertical { d[0] } else { d[1] };let pos = if ui.input(|i| i.modifiers.shift) { snap_to_tick(app, xf, vertical, along) } else { crate::snap_ui::snap_guide(app, vertical, along) };@@ -951,6 +954,35 @@ mod tests {}}+ /// #5698: as in Photoshop, an Alt-drag out of the top ruler makes a vertical guide and one out+ /// of the left ruler a horizontal guide; without Alt each ruler makes its own kind.+ #[test]+ fn alt_drags_a_guide_of_the_other_orientation_out_of_a_ruler() {+ let full = Rect::from_min_size(Pos2::ZERO, vec2(400.0, 300.0));+ let rect = Rect::from_min_max(full.min + vec2(RULER, RULER), full.max);+ let xf = ViewXform { rect, zoom: 1.0, center: [128.0, 64.0], flip: false, rotation: 0.0, aspect: 1.0 };+ let drag = |from: Pos2, modifiers: egui::Modifiers| {+ let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default());+ app.run("file.new", json!({"width": 256, "height": 128})).unwrap();+ let mut h = egui_kittest::Harness::builder().with_size(vec2(400.0, 300.0)).build_ui_state(move |ui, app| draw_rulers(app, ui, full, &xf), app);+ let to = xf.to_screen(37.0, 90.0);+ h.event_modifiers(egui::Event::PointerMoved(from), modifiers);+ h.event_modifiers(egui::Event::PointerButton { pos: from, button: egui::PointerButton::Primary, pressed: true, modifiers }, modifiers);+ h.run_steps(1);+ h.event_modifiers(egui::Event::PointerMoved(to), modifiers);+ h.run_steps(1);+ h.event_modifiers(egui::Event::PointerButton { pos: to, button: egui::PointerButton::Primary, pressed: false, modifiers }, modifiers);+ h.run_steps(2);+ let g = &h.state().session.active().unwrap().doc.guides;+ (g.vertical.clone(), g.horizontal.clone())+ };+ let (top, left) = (pos2(200.0, RULER / 2.0), pos2(RULER / 2.0, 150.0));+ assert_eq!(drag(top, egui::Modifiers::NONE), (vec![], vec![90.0]), "top ruler: horizontal");+ assert_eq!(drag(left, egui::Modifiers::NONE), (vec![37.0], vec![]), "left ruler: vertical");+ assert_eq!(drag(top, egui::Modifiers::ALT), (vec![37.0], vec![]), "Alt from the top ruler: vertical");+ assert_eq!(drag(left, egui::Modifiers::ALT), (vec![], vec![90.0]), "Alt from the left ruler: horizontal");+ }+/// #3770: a guide Shift-dragged out of a ruler lands on the nearest tick the ruler draws/// (tenths of the labelled step, in the ruler unit); without Shift it lands where it's dropped.#[test]@@ -978,6 +1010,8 @@ mod tests {h.state().session.active().unwrap().doc.guides.vertical.clone()};assert_eq!(drag_guide(egui::Modifiers::NONE), vec![37.0]);+ // #5698: Alt turns it into a horizontal guide (at the drop's y, the document's centre).+ assert_eq!(drag_guide(egui::Modifiers::ALT), Vec::<f64>::new(), "no vertical guide with Alt");// Pixels at 100%: labels every 100 px, ticks every 10 px.assert_eq!(drag_guide(egui::Modifiers::SHIFT), vec![40.0]);