Skip to content

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

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

David Mann2a690f0f7117
diff --git a/crates/ui-egui/src/canvas.rs b/crates/ui-egui/src/canvas.rs
index 914d395ac..325ed5e31 100644
--- a/crates/ui-egui/src/canvas.rs
+++ b/crates/ui-egui/src/canvas.rs
@@ -6006,10 +6006,12 @@ pub fn commit_polygon(app: &mut PhotocraftApp) {
}
}
-/// Apply the crop tool's rectangle.
+/// Apply the crop tool's rectangle. When `image.crop` refuses it (the error is in the status bar),
+/// the frame, its turn and its box Undo stay for fixing.
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);
let Some(r) = app.ui.crop_rect.take() else { return };
@@ -6041,9 +6043,9 @@ pub fn commit_crop(app: &mut PhotocraftApp) {
}
// W x H x Resolution (#2443): resample to that size and set the resolution.
crate::crop_size::commit_params(app, &mut p);
- if app.run("image.crop", p).is_ok()
- && let Some(i) = app.session.active_index()
- {
+ if app.run("image.crop", p).is_err() {
+ crate::crop_ui::restore(app, kept);
+ } else if let Some(i) = app.session.active_index() {
app.ui.views[i].fit_pending = true;
}
}
diff --git a/crates/ui-egui/src/crop_size.rs b/crates/ui-egui/src/crop_size.rs
index b30cdd8c4..d7ef2954a 100644
--- a/crates/ui-egui/src/crop_size.rs
+++ b/crates/ui-egui/src/crop_size.rs
@@ -525,6 +525,33 @@ 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 older_tool_options_load_without_the_new_fields() {
let o: ToolOptions = serde_json::from_str(r#"{"crop_ratio": "1:1"}"#).unwrap();
diff --git a/crates/ui-egui/src/crop_ui.rs b/crates/ui-egui/src/crop_ui.rs
index f1df63ff1..a4cfd0e94 100644
--- a/crates/ui-egui/src/crop_ui.rs
+++ b/crates/ui-egui/src/crop_ui.rs
@@ -377,10 +377,14 @@ fn keeps_pending(app: &PhotocraftApp) -> bool {
}
/// Commits the drawn frame on leaving the Crop tool as ↵ would with the tool still picked (so the
-/// view goes upright too, `crop_mode::upright`), then gives the new tool back.
+/// view goes upright too, `crop_mode::upright`), then gives the new tool back. A refused crop
+/// keeps its frame: that is cancelled, so the new tool doesn't retry it every frame.
fn commit_on_leave(app: &mut PhotocraftApp) {
let tool = std::mem::replace(&mut app.ui.tool, Tool::Crop);
crate::canvas::commit_crop(app);
+ if app.ui.crop_rect.is_some() {
+ cancel(app);
+ }
app.ui.tool = tool;
}
@@ -422,6 +426,30 @@ fn apply_step(app: &mut PhotocraftApp, s: CropStep) {
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.