diff --git a/crates/midra-layout/src/format_ui.rs b/crates/midra-layout/src/format_ui.rs index 719853e..585cfef 100644 --- a/crates/midra-layout/src/format_ui.rs +++ b/crates/midra-layout/src/format_ui.rs @@ -268,6 +268,17 @@ fn fit_rect(rect: Rect, size: [f64; 2]) -> Rect { let k = (rect.width() / size[0] as f32).min(rect.height() / size[1] as f32); Rect::from_center_size(rect.center(), Vec2::new(size[0] as f32, size[1] as f32) * k) } +/// Full usable input bounds in the same coordinate space as the visible box. +pub(super) fn content_bounds(rect: Rect, source: Source, crop: Crop) -> Option { + let input = source.input().ok()?; + let retained = crop.remaining(input).ok()?; + let k = rect.width() as f64 / retained[0]; + Some(Rect::from_min_size( + rect.min - Vec2::new((crop.left * k) as f32, (crop.top * k) as f32), + Vec2::new((input[0] * k) as f32, (input[1] * k) as f32), + )) +} + /// Reveal only the usable input discarded by the layer crop, behind other layers. pub(super) fn paint_content_overflow( painter: &egui::Painter, @@ -275,17 +286,9 @@ pub(super) fn paint_content_overflow( source: Source, crop: Crop, ) { - let Ok(input) = source.input() else { + let Some(full) = content_bounds(rect, source, crop) else { return; }; - let Ok(retained) = crop.remaining(input) else { - return; - }; - let k = rect.width() as f64 / retained[0]; - let full = Rect::from_min_size( - rect.min - Vec2::new((crop.left * k) as f32, (crop.top * k) as f32), - Vec2::new((input[0] * k) as f32, (input[1] * k) as f32), - ); // Disjoint strips avoid painting the visible box twice or doubling opacity. for clip in [ Rect::from_min_max(full.min, Pos2::new(full.right(), rect.top())), diff --git a/crates/midra-layout/src/gui.rs b/crates/midra-layout/src/gui.rs index d6507b7..c01fbbe 100644 --- a/crates/midra-layout/src/gui.rs +++ b/crates/midra-layout/src/gui.rs @@ -885,6 +885,37 @@ impl Editor { ); } } + fn content_hit(&self, pointer: Pos2, origin: Pos2, scale: f32) -> Option { + if !self.edit_content { + return None; + } + let i = self.layout.selected()?; + let to_rect = |g: &Geometry| { + Rect::from_min_size( + origin + Vec2::new(g.x as f32, g.y as f32) * scale, + Vec2::new(g.width as f32, g.height as f32) * scale, + ) + }; + let rect = to_rect(&self.layout.layers()[i]); + // The faded preview is behind other visible layers, including locked ones. + if !rect.contains(pointer) + && self + .layout + .layers() + .iter() + .enumerate() + .any(|(j, g)| j != i && self.layout.visible(j) && to_rect(g).contains(pointer)) + { + return None; + } + format_ui::content_bounds( + rect, + self.layout.formats().sources[self.layout.formats().source_for[i]], + self.layout.formats().advanced[i], + ) + .filter(|bounds| bounds.contains(pointer)) + .map(|_| i) + } fn resize_handle( &self, pointer: Pos2, @@ -1027,7 +1058,9 @@ impl Editor { origin = area.center() - Vec2::new(sw / 2.0, sh / 2.0) * scale + self.pan * scale; } } - if self.edit_content && response.hovered() { + if response.hovered() + && pointer.is_some_and(|p| self.content_hit(p, origin, scale).is_some()) + { ui.ctx().set_cursor_icon(egui::CursorIcon::Grab); } if !self.edit_content @@ -1059,13 +1092,12 @@ impl Editor { && let Some(p) = ui.input(|i| i.pointer.press_origin()) { let handle = self.resize_handle(p, origin, scale); + let content = self.content_hit(p, origin, scale); let p = (p - origin) / scale; let point = [p.x as f64, p.y as f64]; let shift = ui.input(|i| i.modifiers.shift); - if self.edit_content - && let Some(i) = self.layout.selected() - { - if self.layout.layers()[i].contains(point) { + if self.edit_content { + if let Some(i) = content { self.layout.start_content_drag(i, point); } } else if let Some((i, corner)) = handle.filter(|_| !shift) { @@ -1556,6 +1588,73 @@ mod tests { assert!(!app.edit_content); } #[test] + fn faded_content_can_be_grabbed_outside_box_at_different_camera_scales() { + for zoom in [0.6, 1.0] { + let mut app = Editor { + edit_content: true, + zoom, + pan: Vec2::new(-30.0, 20.0), + ..Default::default() + }; + app.layout + .set_framing( + 1, + midra_layout::formats::Framing { + zoom: 2.0, + offset: [0.0; 2], + }, + ) + .unwrap(); + let ctx = egui::Context::default(); + let before = *app.layout.layers(); + let formats = *app.layout.formats(); + let output = frame(&mut app, &ctx, vec![]); + let rect = layer_rects(&output)[1]; + let start = rect.center_top() - Vec2::new(0.0, 15.0); + assert!(!rect.contains(start)); + frame( + &mut app, + &ctx, + vec![egui::Event::PointerMoved(start), mouse(start, true)], + ); + let end = start + Vec2::new(10.0, 10.0); + frame(&mut app, &ctx, vec![egui::Event::PointerMoved(end)]); + frame(&mut app, &ctx, vec![mouse(end, false)]); + assert_eq!(*app.layout.layers(), before); + assert_ne!(*app.layout.formats(), formats); + assert_eq!(app.layout.selected(), Some(1)); + app.layout.undo(); + assert_eq!(*app.layout.formats(), formats); + } + } + #[test] + fn content_hit_excludes_empty_space_and_preview_covered_by_another_layer() { + let mut app = Editor { + edit_content: true, + ..Default::default() + }; + app.layout + .set_framing( + 1, + midra_layout::formats::Framing { + zoom: 4.0, + offset: [0.0; 2], + }, + ) + .unwrap(); + let origin = Pos2::ZERO; + let covered = Pos2::new(700.0, 300.0); + assert_eq!(app.content_hit(covered, origin, 1.0), None); + app.layout.set_visible(0, false); + assert_eq!(app.content_hit(covered, origin, 1.0), Some(1)); + assert_eq!( + app.content_hit(Pos2::new(1480.0, -500.0), origin, 1.0), + None + ); + app.edit_content = false; + assert_eq!(app.content_hit(covered, origin, 1.0), None); + } + #[test] fn rounded_fields_do_not_quantize_unchanged_values_and_copy_uses_pixels() { let mut app = Editor::default(); let ctx = egui::Context::default(); diff --git a/docs/midra-layout.md b/docs/midra-layout.md index 09014c9..7cc95fb 100644 --- a/docs/midra-layout.md +++ b/docs/midra-layout.md @@ -261,6 +261,8 @@ framing**: While editing, cropped-out content appears at 50% opacity outside the box, behind other layers. This preview stops at the usable input boundary; input cropping remains excluded. The content inside the box stays fully opaque. + Grab the faded area to reframe content too; other visible layers covering it + take precedence. Empty space beyond the usable input is not draggable content. Sources shared by both layers share the input crop. The dialog names affected layers; each keeps its independent layer crop. Shared changes undo atomically,