Skip to content

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

Merge PR #7134: Crop: a refused crop keeps the frame for fixing (#7133)

aiongga0078c3e1f13
diff --cc crates/ui-egui/src/canvas.rs
index b66e5a704,325ed5e31..865040e96
--- a/crates/ui-egui/src/canvas.rs
+++ b/crates/ui-egui/src/canvas.rs
@@@ -6191,10 -6011,9 +6192,11 @@@ pub fn commit_polygon(app: &mut Photocr
pub fn commit_crop(app: &mut PhotocraftApp) {
// Never another document's frame (#1918).
crate::crop_ui::cancel_stale(app);
+ let kept = crate::crop_ui::keep(app);
// Committing ends the box's own Undo/Redo: from here Edit › Undo reverts the document.
crate::crop_ui::clear_history(app);
+ // ↵ during a drag ends it: the release must not bring back the old document's frame.
+ app.crop.drag = None;
let Some(r) = app.ui.crop_rect.take() else { return };
// The crop's default mode leaves the view upright (`crop_mode`).
crate::crop_mode::upright(app);
diff --cc crates/ui-egui/src/crop_size.rs
index 8e1b3703d,d7ef2954a..95b9be221
--- a/crates/ui-egui/src/crop_size.rs
+++ b/crates/ui-egui/src/crop_size.rs
@@@ -525,24 -525,33 +525,51 @@@ mod tests
assert_eq!(app.session.active().unwrap().doc.size, Size::new(100, 50));
}
+ /// A commit `image.crop` refuses (here a resolution past its 10 000 ppi) keeps the frame, its
+ /// turn and its box Undo; leaving the tool then drops the frame instead of retrying.
+ #[test]
+ fn a_refused_commit_keeps_the_frame_and_its_history() {
+ use crate::canvas::{ToolEvent, tool_event};
+ let mut app = app(SampleType::U8);
+ app.ui.tool_options = whr("4 in", "2 in", "20000", PX_PER_IN);
+ app.ui.tool_options.crop_shield.classic_mode = true;
+ crate::crop_ui::ensure_frame(&mut app);
+ tool_event(&mut app, ToolEvent::Down { x: 10.0, y: 10.0, pressure: 1.0 }, egui::Modifiers::NONE);
+ tool_event(&mut app, ToolEvent::Move { x: 110.0, y: 60.0, pressure: 1.0 }, egui::Modifiers::NONE);
+ tool_event(&mut app, ToolEvent::Up { x: 110.0, y: 60.0 }, egui::Modifiers::NONE);
+ app.ui.crop_angle = 12.0;
+ let frame = (app.ui.crop_rect, app.ui.crop_angle);
+ assert!(frame.0.is_some() && !app.crop.default_frame);
+ crate::canvas::commit_crop(&mut app);
+ assert_eq!(app.session.active().unwrap().doc.size, Size::new(400, 300), "refused");
+ assert_eq!((app.ui.crop_rect, app.ui.crop_angle), frame);
+ assert!(!app.crop.default_frame);
+ assert_eq!(crate::crop_ui::is_enabled(&app, "edit.undo"), Some(true), "the box Undo stays");
+ app.ui.tool = Tool::Move;
+ crate::crop_ui::ensure_frame(&mut app);
+ assert_eq!(app.ui.crop_rect, None);
+ crate::crop_ui::ensure_frame(&mut app);
+ assert_eq!(app.session.active().unwrap().doc.size, Size::new(400, 300));
+ }
+
+ #[test]
+ fn points_and_picas_follow_the_point_size_preference() {
+ let mut app = app(SampleType::U8);
+ app.ui.tool_options = whr("72 pt", "1 in", "300", PX_PER_IN);
+ let target_size = |app: &PhotocraftApp| {
+ let mut p = json!({});
+ commit_params(app, &mut p);
+ (p["targetWidth"].as_u64(), p["targetHeight"].as_u64())
+ };
+ assert_eq!(target_size(&app), (Some(300), Some(300)), "PostScript: 72 pt is an inch");
+ app.session.edit_prefs(|p| p.units_and_rulers.point_size = photocraft_engine::prefs::PointSize::Traditional);
+ assert_eq!(target_size(&app), (Some(299), Some(300)), "Traditional: 72.27 pt per inch");
+ let (w, h, _) = target(&app.ui.tool_options, 72.0, 72.27).unwrap();
+ assert!((w - 300.0 * 72.0 / 72.27).abs() < 1e-9 && (h - 300.0).abs() < 1e-9, "{w} {h}");
+ app.ui.tool_options = whr("6 pc", "1 in", "300", PX_PER_IN);
+ assert_eq!(target_size(&app), (Some(299), Some(300)), "a pica is 12 points");
+ }
+
#[test]
fn older_tool_options_load_without_the_new_fields() {
let o: ToolOptions = serde_json::from_str(r#"{"crop_ratio": "1:1"}"#).unwrap();
diff --cc crates/ui-egui/src/crop_ui.rs
index 877c24f32,a4cfd0e94..888ddd5cf
--- a/crates/ui-egui/src/crop_ui.rs
+++ b/crates/ui-egui/src/crop_ui.rs
@@@ -424,12 -426,35 +428,36 @@@ fn apply_step(app: &mut PhotocraftApp,
app.crop.default_frame = s.default_frame;
}
+ /// The pending frame, its box Undo/Redo and the view's turn, kept while `image.crop` runs so a
+ /// refused commit gives them back ([`restore`]).
+ pub(crate) struct Kept {
+ step: CropStep,
+ undo: Vec<CropStep>,
+ redo: Vec<CropStep>,
+ view: Option<(f32, [f32; 2])>,
+ }
+
+ pub(crate) fn keep(app: &PhotocraftApp) -> Kept {
+ let view = app.session.active_index().and_then(|i| app.ui.views.get(i)).map(|v| (v.rotation, v.center));
+ Kept { step: step_of(app), undo: app.crop.undo.clone(), redo: app.crop.redo.clone(), view }
+ }
+
+ pub(crate) fn restore(app: &mut PhotocraftApp, k: Kept) {
+ apply_step(app, k.step);
+ (app.crop.undo, app.crop.redo) = (k.undo, k.redo);
+ if let Some((rotation, center)) = k.view
+ && let Some(v) = app.session.active_index().and_then(|i| app.ui.views.get_mut(i))
+ {
+ (v.rotation, v.center) = (rotation, center);
+ }
+ }
+
/// While the Crop tool has box history, Undo/Redo step the *box*, not the document (Photoshop keeps
/// Edit › Undo live during a crop, #1918/#3145). `Some(enabled)` for those commands, else `None` so
-/// the caller falls back to document history.
+/// the caller falls back to document history. While either box stack has a step, `intercept` takes
+/// both commands, so one with nothing left to step greys out rather than offering the document's.
pub fn is_enabled(app: &PhotocraftApp, id: &str) -> Option<bool> {
- if app.ui.tool != Tool::Crop {
+ if app.ui.tool != Tool::Crop || (app.crop.undo.is_empty() && app.crop.redo.is_empty()) {
return None;
}
match id {