Snap layers to screen center and quarter guides

This commit is contained in:
Dandaedre 2026-11-14 15:33:08 +01:00
parent a5f6391606
commit a0c87608ec
6 changed files with 116 additions and 8 deletions

View File

@ -4744,7 +4744,11 @@ mod tests {
click_text(&mut app, &ctx, "Snapping");
click_text(&mut app, &ctx, "Snap movement & resizing");
assert!(app.snapping.enabled);
for label in ["Pixel edges", "Screen edges", "Visible layer edges"] {
for label in [
"Pixel edges",
"Screen edges, center & quarters",
"Visible layer edges",
] {
click_text(&mut app, &ctx, label);
}
assert!(

View File

@ -83,7 +83,10 @@ impl Editor {
ui.checkbox(&mut self.snapping.enabled, "Snap movement & resizing");
ui.add_enabled_ui(self.snapping.enabled, |ui| {
ui.checkbox(&mut self.snapping.pixels, "Pixel edges");
ui.checkbox(&mut self.snapping.screen_edges, "Screen edges");
ui.checkbox(
&mut self.snapping.screen_edges,
"Screen edges, center & quarters",
);
ui.checkbox(&mut self.snapping.layer_edges, "Visible layer edges")
.on_hover_text("Includes contact between mirrored/opposite resizing layers.");
});

View File

@ -19,6 +19,11 @@ impl Default for Snapping {
}
}
}
// Screen coordinates remain fractional for custom resolutions not divisible by four.
fn screen_guides(size: u32) -> [f64; 5] {
[0.0, 0.25, 0.5, 0.75, 1.0].map(|fraction| size as f64 * fraction)
}
impl Layout {
/// Snap a pivot without editing geometry. Local anchors take precedence over
/// grid/edge options; the caller may bypass them for precise free placement.
@ -61,7 +66,7 @@ impl Layout {
for axis in 0..2 {
let mut edges = Vec::new();
if options.screen_edges {
edges.extend([0.0, self.formats.output[axis] as f64]);
edges.extend(screen_guides(self.formats.output[axis]));
}
if options.layer_edges {
for (i, other) in self.layers.iter().enumerate() {
@ -122,7 +127,7 @@ impl Layout {
for axis in 0..2 {
let mut targets = Vec::new();
if options.screen_edges {
targets.extend([0.0, self.formats.output[axis] as f64]);
targets.extend(screen_guides(self.formats.output[axis]));
}
if options.layer_edges {
for (i, g) in before.layers.iter().enumerate() {
@ -136,8 +141,20 @@ impl Layout {
if !selected[i] {
continue;
}
for moving in edge(*g, axis) {
for (anchor, moving) in edge(*g, axis)
.into_iter()
.chain(std::iter::once(g.center()[axis]))
.enumerate()
{
for target in &targets {
// Centers align to the interior screen guides; retain
// edge-to-edge behavior at screen/layer boundaries.
if anchor == 2
&& (!options.screen_edges
|| !screen_guides(self.formats.output[axis])[1..4].contains(target))
{
continue;
}
let correction = *target - (moving + delta[axis]);
if correction.abs() <= tolerance
&& best.is_none_or(|(d, _)| correction.abs() < d.abs())
@ -269,7 +286,7 @@ impl Layout {
}
let mut targets = Vec::new();
if options.screen_edges {
targets.extend([0.0, self.formats.output[axis] as f64]);
targets.extend(screen_guides(self.formats.output[axis]));
}
if options.layer_edges {
for (i, g) in r.before.layers.iter().enumerate() {

View File

@ -13,7 +13,7 @@ fn screen_snapping_recomputes_from_original_and_is_one_undo_step() {
layout.start_drag([1200.0, 200.0]);
assert_eq!(
layout.drag_to_snapped([1317.0, 201.0], options, 6.0),
[Some(1920.0), None]
[Some(1920.0), Some(540.0)]
);
assert_eq!(layout.layers()[1].x, 1280.0);
layout.drag_to_snapped([1250.0, 200.0], options, 6.0);
@ -550,3 +550,85 @@ fn opposite_vertical_edges_snap_to_meet() {
assert!((guides[1].unwrap() - target).abs() < 1e-8);
assert!((layout.layers()[0].y + layout.layers()[0].height - layout.layers()[1].y).abs() < 1e-8);
}
#[test]
fn screen_quarters_snap_moving_edges_and_centers_and_survive_release() {
let options = Snapping {
enabled: true,
pixels: false,
layer_edges: false,
..Default::default()
};
for fraction in [0.25, 0.5, 0.75] {
for anchor in [0.0, 0.5, 1.0] {
let mut layout = Layout::default();
let g = layout.layers()[1];
let target = [1920.0 * fraction, 1080.0 * fraction];
let point = [
target[0] - g.width * anchor + 2.0,
target[1] - g.height * anchor - 2.0,
];
layout.start_drag([g.x, g.y]);
assert_eq!(
layout.drag_to_snapped(point, options, 6.0),
target.map(Some)
);
let snapped = layout.layers()[1];
assert_eq!(snapped.x + g.width * anchor, target[0]);
assert_eq!(snapped.y + g.height * anchor, target[1]);
layout.finish_drag();
assert_eq!(layout.layers()[1], snapped);
layout.undo();
assert_eq!(layout.layers()[1], g);
}
}
}
#[test]
fn screen_quarters_snap_resizes_and_pivots_and_respect_screen_toggle() {
use midra_layout::ResizeHandle;
let options = Snapping {
enabled: true,
pixels: false,
layer_edges: false,
..Default::default()
};
for fraction in [0.25, 0.5, 0.75] {
for axis in 0..2 {
for enabled in [false, true] {
let mut layout = Layout::default();
layout.select(Some(0));
let g = layout.layers()[0];
let handle = if axis == 0 {
ResizeHandle::Right
} else {
ResizeHandle::Bottom
};
let start = ResizeHandle::positions(g)
.into_iter()
.find(|(h, _)| *h == handle)
.unwrap()
.1;
let target = layout.formats().output[axis] as f64 * fraction;
let mut point = start;
point[axis] = target + 2.0;
let options = Snapping {
screen_edges: enabled,
..options
};
let (pivot, guides) = layout.snap_pivot(0, point, options, 6.0, None, false);
assert_eq!(guides[axis], enabled.then_some(target));
assert_eq!(pivot[axis], if enabled { target } else { point[axis] });
layout.start_handle_resize(0, handle, start, false);
let guides = layout.drag_to_snapped(point, options, 6.0);
assert_eq!(guides[axis], enabled.then_some(target));
let next = layout.layers()[0];
assert_eq!(
[next.x + next.width, next.y + next.height][axis],
if enabled { target } else { point[axis] }
);
assert_eq!([next.x, next.y], [g.x, g.y]);
}
}
}
}

View File

@ -33,7 +33,7 @@ unknown theme names are rejected. There are no numeric coordinates or units.
| `show_toolbar_icon_names` | Boolean; default false |
| `keep_aspect_ratio` | Boolean; default true |
| `show_source_pattern` | Boolean; default false. Shows source patterns on other layers only during crop editing; normal editing always hides patterns. |
| `snapping` | Object with required boolean fields `enabled` (false), `pixels` (true), `screen_edges` (true), `layer_edges` (true); unknown fields rejected |
| `snapping` | Object with required boolean fields `enabled` (false), `pixels` (true), `screen_edges` (true; screen edges plus 25%, 50% and 75% guides on both axes), `layer_edges` (true); unknown fields rejected |
Loaded once at startup before the editor appears. Explicit Options changes save
the current preferences and theme history immediately. The directory is created lazily;

View File

@ -4,6 +4,8 @@
## Unreleased
- Snap moving layers, resize handles and pivots to screen center and quarter guides on both axes. Moving layer centers also snap to these interior guides; the existing screen snapping option controls all screen guides.
- Use a crisp, theme-aware magnet icon for the snapping dropdown; preserve snapping controls and active grid indicators.
- Unify file exchange around shared geometry preparation, byte-oriented codecs and one Export layers window. Add optional native PPTX/FIG import/export with slide/frame selection and embedded artwork, plus local VectorCraft AI conversion. File conversions run off the UI thread. Document and test `--no-default-features --features no_proprietary`; CI covers both configurations and production coverage includes the codecs.