diff --git a/crates/midra-layout/src/format_ui.rs b/crates/midra-layout/src/format_ui.rs index 3eebbe9..3aeb6b0 100644 --- a/crates/midra-layout/src/format_ui.rs +++ b/crates/midra-layout/src/format_ui.rs @@ -136,6 +136,12 @@ fn aspect_combo(ui: &mut egui::Ui, id: &str, aspect: &mut Aspect) -> bool { }); old != *aspect } +fn nonnegative_number(text: &str) -> Option { + text.trim() + .parse::() + .ok() + .filter(|value| value.is_finite() && *value >= 0.0) +} fn dimensions(ui: &mut egui::Ui, size: &mut [f64; 2], prefix: &str) { ui.horizontal(|ui| { ui.label(prefix); @@ -302,7 +308,7 @@ impl Editor { }); let old_crop=d.source.input_crop; ui.horizontal_wrapped(|ui| { - for (title,value) in [("L ",&mut d.source.input_crop.left),("R ",&mut d.source.input_crop.right),("T ",&mut d.source.input_crop.top),("B ",&mut d.source.input_crop.bottom)] { ui.add(egui::DragValue::new(value).speed(1.0).custom_formatter(|v,_|number(v)).prefix(title)); } + for (title,value) in [("L ",&mut d.source.input_crop.left),("R ",&mut d.source.input_crop.right),("T ",&mut d.source.input_crop.top),("B ",&mut d.source.input_crop.bottom)] { ui.add(egui::DragValue::new(value).range(0.0..=f64::MAX).custom_parser(nonnegative_number).speed(1.0).custom_formatter(|v,_|number(v)).prefix(title)); } }); if old_crop!=d.source.input_crop && let Ok(size)=d.source.input() { d.input_size=size; } ui.label("Input crop edges: native source pixels. Native-size changes reset the draft input crop before review."); @@ -345,7 +351,7 @@ impl Editor { let before_framing=d.framing; let mut percent=d.framing.zoom*100.0; ui.horizontal(|ui| { - if ui.add(egui::DragValue::new(&mut percent).speed(1.0).custom_formatter(|v,_|number(v)).suffix("% content zoom")).changed() {d.framing.zoom=percent/100.0;} + if ui.add(egui::DragValue::new(&mut percent).range(100.0..=10000.0).custom_parser(nonnegative_number).speed(1.0).custom_formatter(|v,_|number(v)).suffix("% content zoom")).changed() {d.framing.zoom=percent/100.0;} ui.add(egui::DragValue::new(&mut d.framing.offset[0]).speed(1.0).custom_formatter(|v,_|number(v)).prefix("X ")); ui.add(egui::DragValue::new(&mut d.framing.offset[1]).speed(1.0).custom_formatter(|v,_|number(v)).prefix("Y ")); }); @@ -528,6 +534,119 @@ fn paint_pattern_clipped( mod tests { use super::super::tests::{click_text, frame, mouse}; use super::*; + fn enter_value(app: &mut Editor, ctx: &egui::Context, label: &str, text: &str) { + click_text(app, ctx, label); + frame(app, ctx, vec![egui::Event::Paste(text.into())]); + frame( + app, + ctx, + vec![egui::Event::Key { + key: egui::Key::Enter, + physical_key: None, + pressed: true, + repeat: false, + modifiers: egui::Modifiers::NONE, + }], + ); + frame(app, ctx, vec![]); + } + + #[test] + fn input_crop_controls_reject_negative_paste_and_keep_fractional_values() { + let ctx = egui::Context::default(); + let mut app = Editor { + property_tab: PropertyTab::Source, + ..Default::default() + }; + let crop = Crop { + left: 20.0, + right: 20.0, + top: 20.0, + bottom: 20.0, + }; + let source = Source { + input_crop: crop, + ..Source::default() + }; + app.layout + .configure(1, 1, source, app.layout.layers()[1], true) + .unwrap(); + app.sync(); + app.format_draft = Some(FormatDraft::new(&app.layout, 1)); + for label in ["L 20.0", "R 20.0", "T 20.0", "B 20.0"] { + enter_value(&mut app, &ctx, label, "-12.5"); + assert_eq!(app.format_draft.as_ref().unwrap().source.input_crop, crop); + } + enter_value(&mut app, &ctx, "L 20.0", "NaN"); + assert_eq!(app.format_draft.as_ref().unwrap().source.input_crop, crop); + enter_value(&mut app, &ctx, "L 20.0", "12.5"); + assert_eq!( + app.format_draft.as_ref().unwrap().source.input_crop.left, + 12.5 + ); + enter_value(&mut app, &ctx, "L 12.5", "0"); + assert_eq!( + app.format_draft.as_ref().unwrap().source.input_crop.left, + 0.0 + ); + let output = frame(&mut app, &ctx, vec![]); + let start = output + .shapes + .iter() + .find_map(|shape| match &shape.shape { + egui::Shape::Text(t) if t.galley.text() == "R 20.0" => { + Some(t.pos + Vec2::new(12.0, 5.0)) + } + _ => None, + }) + .unwrap(); + let end = start - Vec2::new(100.0, 0.0); + frame( + &mut app, + &ctx, + vec![egui::Event::PointerMoved(start), mouse(start, true)], + ); + frame(&mut app, &ctx, vec![egui::Event::PointerMoved(end)]); + frame(&mut app, &ctx, vec![mouse(end, false)]); + assert_eq!( + app.format_draft.as_ref().unwrap().source.input_crop.right, + 0.0 + ); + } + + #[test] + fn crop_zoom_rejects_negative_entry_but_content_offsets_allow_negative_values() { + let ctx = egui::Context::default(); + let mut app = Editor { + property_tab: PropertyTab::Crop, + ..Default::default() + }; + app.format_draft = Some(FormatDraft::new(&app.layout, 1)); + enter_value(&mut app, &ctx, "100.0% content zoom", "-50"); + assert_eq!(app.format_draft.as_ref().unwrap().framing.zoom, 1.0); + enter_value(&mut app, &ctx, "100.0% content zoom", "50"); + assert_eq!(app.format_draft.as_ref().unwrap().framing.zoom, 1.0); + enter_value(&mut app, &ctx, "100.0% content zoom", "20000"); + assert!((app.format_draft.as_ref().unwrap().framing.zoom - 100.0).abs() < 1e-9); + enter_value(&mut app, &ctx, "10000.0% content zoom", "200"); + enter_value(&mut app, &ctx, "X 0.0", "-10"); + assert!( + app.layout + .formats() + .mapping(1, app.layout.layers()[1]) + .unwrap() + .framing + .offset[0] + < 0.0 + ); + let crop = app.layout.formats().advanced[1]; + assert!( + [crop.left, crop.right, crop.top, crop.bottom] + .iter() + .all(|v| *v >= 0.0) + ); + } + #[test] fn crop_control_drag_applies_without_a_button_and_keeps_pending_source_edits() { let ctx = egui::Context::default(); diff --git a/docs/midra-layout.md b/docs/midra-layout.md index 3896b07..5fd89e7 100644 --- a/docs/midra-layout.md +++ b/docs/midra-layout.md @@ -307,13 +307,16 @@ Select one layer and click its **Layer resolution** card to open **Layer propert 1. Choose a local **Source identity** and its original **Source format** and native dimensions. Input 1/2 are planning identities, not device routing commands. 2. Set **Input resolution** to the usable size. Typing a custom size centers the - input crop; edit L/R/T/B for asymmetric placement. Native-size changes reset + input crop; edit L/R/T/B for asymmetric placement. Crop edges stop at zero + when dragging; negative or non-finite typed/pasted values are not accepted. Native-size changes reset the draft crop to the full source before review. A custom input retains the original Source format, e.g. 16:9 native 1920 × 1080 cropped to 1920 × 1000. 3. Set **Layer resolution** to the final visible box. Use the aspect toggle to enter an independent custom width and height. Apply source/input/layer changes together; the selected screen-corner anchor stays fixed. 4. Use **Layer crop / zoom & position** for zoom (100–10000%) and X/Y offsets in output pixels. + Zoom is bounded to 100–10000%; negative or non-finite entries are rejected. + X/Y offsets remain signed for left/up positioning; they are not crop amounts. These controls operate on the applied formats and update immediately as edited. A continuous zoom/offset drag is one Undo step; **Recenter** preserves zoom; **Reset crop** returns to the minimum centered diff --git a/docs/midralayout/changelog.md b/docs/midralayout/changelog.md index e1ab7e2..fbf49f1 100644 --- a/docs/midralayout/changelog.md +++ b/docs/midralayout/changelog.md @@ -4,6 +4,8 @@ ## Unreleased +- Reject negative/non-finite crop entries; crop-edge dragging stops at zero and zoom stays within 100–10000%. Signed content offsets remain supported. + - Keep the chosen resize mode through selection changes, Escape, Undo/Redo and project loads in the current session; single-layer resizing still works normally. - “Options save automatically” is non-selectable informational text.