mirror of
https://github.com/storytold/photocraft.git
synced 2026-12-02 06:20:48 +01:00
Merge branch 'bb-move-tool-mx4onk76' of github.com:storytold/photocraft into bb-move-tool-mx4onk76--18160-move-drag-cursors
This commit is contained in:
commit
b5bf348b05
9 files changed
+465
-10
No files matched your search
@@ -208,3 +208,6 @@ mod move_leave_board_top_tests;
|
||||
|
||||
#[cfg(test)]
|
||||
mod move_nest_locked_target_tests;
|
||||
|
||||
#[cfg(test)]
|
||||
mod move_board_takes_layers_tests;
|
||||
@@ -0,0 +1,139 @@
|
||||
//! #18154: a board moved with the Move tool (a drag or an Alt-drag copy) takes in each loose layer
|
||||
//! that now has more than a tenth of its area on it, on top of its stack, in the same "Move" step.
|
||||
//! A lock against auto-nesting on the board or on the layer keeps the layer loose, and layers of
|
||||
//! other boards are never taken.
|
||||
|
||||
use super::*;
|
||||
|
||||
fn doc(s: &Session) -> &Document {
|
||||
&s.active().unwrap().doc
|
||||
}
|
||||
|
||||
fn add_rect(s: &mut Session, r: Rect, name: &str) -> LayerId {
|
||||
s.edit("rect", |doc, active| {
|
||||
let fmt = doc.pixel_format();
|
||||
let mut l = Layer::raster(name.to_string(), fmt);
|
||||
l.surface_mut().unwrap().fill_rect(r, &photocraft_raster::from_rgba(&fmt, [0.5, 0.5, 0.5, 1.0]));
|
||||
let id = doc.insert_above(None, l);
|
||||
*active = Some(id);
|
||||
Ok(id)
|
||||
})
|
||||
.unwrap()
|
||||
}
|
||||
|
||||
fn board(s: &mut Session, rect: [i32; 4], name: &str) -> LayerId {
|
||||
LayerId(s.execute("layer.new.artboard", json!({"rect": rect, "name": name})).unwrap()["layer"].as_u64().unwrap())
|
||||
}
|
||||
|
||||
fn into(s: &mut Session, id: LayerId, parent: LayerId) {
|
||||
s.execute("layer.moveTo", json!({"layer": id.0, "target": parent.0, "position": "into"})).unwrap();
|
||||
}
|
||||
|
||||
fn select(s: &mut Session, id: LayerId) {
|
||||
s.execute("layer.select", json!({"layer": id.0, "mode": "replace"})).unwrap();
|
||||
}
|
||||
|
||||
struct Doc {
|
||||
s: Session,
|
||||
b1: LayerId,
|
||||
b2: LayerId,
|
||||
c: LayerId,
|
||||
d: LayerId,
|
||||
}
|
||||
|
||||
/// The issue's test document: Board1 (50,50-450,450) holds A, B and group G with G1; Board2
|
||||
/// (550,50-950,450) holds C; D (100,550-200,630) is a loose top-level layer.
|
||||
fn setup() -> Doc {
|
||||
let mut s = crate::session();
|
||||
s.execute("file.new", json!({"width": 1000, "height": 700})).unwrap();
|
||||
let b1 = board(&mut s, [50, 50, 400, 400], "Board1");
|
||||
let b2 = board(&mut s, [550, 50, 400, 400], "Board2");
|
||||
let a = add_rect(&mut s, Rect::new(100, 100, 200, 200), "A");
|
||||
into(&mut s, a, b1);
|
||||
let b = add_rect(&mut s, Rect::new(250, 250, 350, 350), "B");
|
||||
into(&mut s, b, b1);
|
||||
let g = s
|
||||
.edit("group", |doc, _| {
|
||||
let fmt = doc.pixel_format();
|
||||
let mut g1 = Layer::raster("G1".to_string(), fmt);
|
||||
g1.surface_mut().unwrap().fill_rect(Rect::new(300, 100, 400, 150), &photocraft_raster::from_rgba(&fmt, [0.5, 0.5, 0.5, 1.0]));
|
||||
Ok(doc.insert_above(None, Layer::group("G", vec![g1])))
|
||||
})
|
||||
.unwrap();
|
||||
into(&mut s, g, b1);
|
||||
let c = add_rect(&mut s, Rect::new(600, 100, 700, 200), "C");
|
||||
into(&mut s, c, b2);
|
||||
let d = add_rect(&mut s, Rect::new(100, 550, 200, 630), "D");
|
||||
Doc { s, b1, b2, c, d }
|
||||
}
|
||||
|
||||
/// Drags the selected layers by `dx`,`dy` with the Move tool, auto-nesting on.
|
||||
fn drag(s: &mut Session, dx: i32, dy: i32) {
|
||||
s.execute("layer.translate", json!({"dx": dx, "dy": dy, "nest": true})).unwrap();
|
||||
}
|
||||
|
||||
fn children(s: &Session, board: LayerId) -> Vec<LayerId> {
|
||||
doc(s).layer(board).unwrap().children().unwrap().iter().map(|l| l.id).collect()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_board_dragged_over_a_loose_layer_takes_it_in_on_top() {
|
||||
let Doc { mut s, b1, d, .. } = setup();
|
||||
select(&mut s, b1);
|
||||
let steps = s.active().unwrap().history.past_len();
|
||||
drag(&mut s, 0, 150);
|
||||
assert_eq!(doc(&s).artboard_of(d), Some(b1), "Board1 covering 50 of D's 80 rows takes D in");
|
||||
assert_eq!(children(&s, b1).last(), Some(&d), "D lands on top of Board1's stack");
|
||||
assert_eq!(s.active().unwrap().history.past_len(), steps + 1, "the move and the nesting are one step");
|
||||
|
||||
s.undo();
|
||||
assert_eq!(doc(&s).artboard_of(d), None, "one Undo leaves D loose again");
|
||||
assert!(doc(&s).layers.iter().any(|l| l.id == d));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn more_than_a_tenth_of_the_layer_on_the_board_decides() {
|
||||
// Board1's bottom edge at 450 + dy: 4 of D's 80 rows (5 %) at dy 104, 12 rows (15 %) at dy 112.
|
||||
for (dy, taken) in [(104, false), (112, true)] {
|
||||
let Doc { mut s, b1, d, .. } = setup();
|
||||
select(&mut s, b1);
|
||||
drag(&mut s, 0, dy);
|
||||
assert_eq!(doc(&s).artboard_of(d) == Some(b1), taken, "Board1 moved by {dy}");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn an_alt_drag_copy_takes_the_layer_into_the_copy() {
|
||||
let Doc { mut s, b1, d, .. } = setup();
|
||||
let kept = children(&s, b1);
|
||||
select(&mut s, b1);
|
||||
// The Move tool's Alt-drag: an in-place copy, then the move of the copy.
|
||||
s.execute("layer.duplicate", json!({"inPlace": true, "withLinks": true})).unwrap();
|
||||
let copy = s.active().unwrap().active_layer.unwrap();
|
||||
assert_ne!(copy, b1);
|
||||
drag(&mut s, 0, 150);
|
||||
assert_eq!(doc(&s).artboard_of(d), Some(copy), "D joins the copy");
|
||||
assert_eq!(children(&s, copy).last(), Some(&d));
|
||||
assert_eq!(children(&s, b1), kept, "the original board keeps just its own children");
|
||||
assert_eq!(doc(&s).walk().iter().filter(|(_, _, l)| l.name == "D").count(), 1, "D is not copied");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_lock_against_auto_nesting_keeps_the_layer_loose() {
|
||||
for locked in ["board", "layer"] {
|
||||
let Doc { mut s, b1, d, .. } = setup();
|
||||
select(&mut s, if locked == "board" { b1 } else { d });
|
||||
s.execute("layer.lockLayers", json!({"artboard": true})).unwrap();
|
||||
select(&mut s, b1);
|
||||
drag(&mut s, 0, 150);
|
||||
assert_eq!(doc(&s).artboard_of(d), None, "lock on the {locked}: D stays loose");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn layers_of_other_boards_are_never_taken() {
|
||||
let Doc { mut s, b1, b2, c, .. } = setup();
|
||||
select(&mut s, b1);
|
||||
drag(&mut s, 500, 0);
|
||||
assert_eq!(doc(&s).artboard_of(c), Some(b2), "C stays in Board2 under the moved Board1");
|
||||
}
|
||||
@@ -261,6 +261,10 @@ type Nest = ((usize, usize), LayerId, Option<LayerId>);
|
||||
/// with Lock › Prevent Auto-Nesting on the layer or a group around it. A board with that lock (or
|
||||
/// Lock All) takes no layer in: one moved onto it stays in its own board while it still overlaps
|
||||
/// it, else it goes to the top level (#18156).
|
||||
///
|
||||
/// A moved board takes in the loose (top-level) layers that now have more than a tenth of their
|
||||
/// area on it, on top of its stack (#18154), with the same locks, clip-group and depth rules. Layers
|
||||
/// of other boards are never taken.
|
||||
/// Returns the layers that changed board, bottom to top.
|
||||
fn auto_nest(doc: &mut Document, ids: &[LayerId]) -> Vec<LayerId> {
|
||||
let boards: Vec<(LayerId, photocraft_geom::Rect)> = doc.artboards().into_iter().rev().map(|(id, _, board)| (id, board.rect)).collect();
|
||||
@@ -273,8 +277,15 @@ fn auto_nest(doc: &mut Document, ids: &[LayerId]) -> Vec<LayerId> {
|
||||
let mut plan: Vec<Nest> = Vec::new();
|
||||
// The paths of the layers planned so far: a layer inside one of them goes with it.
|
||||
let mut carried: Vec<Vec<usize>> = Vec::new();
|
||||
// Moved boards take in the loose layers they now cover (#18154), unless locked against it.
|
||||
let takers: Vec<(LayerId, photocraft_geom::Rect)> = boards.iter().filter(|(id, _)| ids.contains(id) && !locked_board(doc, *id)).copied().collect();
|
||||
for (order, (path, _, l)) in doc.walk().into_iter().enumerate() {
|
||||
if !ids.contains(&l.id) || l.artboard().is_some() || doc.is_background(l.id) || carried.iter().any(|c| path.starts_with(c)) {
|
||||
if l.artboard().is_some() || doc.is_background(l.id) || carried.iter().any(|c| path.starts_with(c)) {
|
||||
continue;
|
||||
}
|
||||
// Not moved itself: only a loose (top-level) layer, and only when a board moved.
|
||||
let moved = ids.contains(&l.id);
|
||||
if !moved && (path.len() != 1 || takers.is_empty()) {
|
||||
continue;
|
||||
}
|
||||
if l.clipped && crate::clip_group::base_of(doc, l.id).is_some() {
|
||||
@@ -283,12 +294,19 @@ fn auto_nest(doc: &mut Document, ids: &[LayerId]) -> Vec<LayerId> {
|
||||
let bounds = crate::layer_multi_cmds::layer_bounds(l);
|
||||
let Some(bounds) = bounds.filter(|b| !b.is_empty()) else { continue };
|
||||
let from = doc.artboard_of(l.id);
|
||||
let mut to = board_after_move(&boards, from, bounds);
|
||||
// A board locked against auto-nesting takes nothing in (#18156): the layer stays in its own
|
||||
// board while it still overlaps it, else it leaves for the top level.
|
||||
if to.is_some_and(|b| locked_board(doc, b)) {
|
||||
to = from.filter(|f| boards.iter().any(|(id, r)| id == f && !r.intersect(&bounds).is_empty()));
|
||||
}
|
||||
let to = if moved {
|
||||
let to = board_after_move(&boards, from, bounds);
|
||||
// A board locked against auto-nesting takes nothing in (#18156): the layer stays in its own
|
||||
// board while it still overlaps it, else it leaves for the top level.
|
||||
if to.is_some_and(|b| locked_board(doc, b)) {
|
||||
from.filter(|f| boards.iter().any(|(id, r)| id == f && !r.intersect(&bounds).is_empty()))
|
||||
} else {
|
||||
to
|
||||
}
|
||||
} else {
|
||||
// A loose layer joins a moved board holding more than a tenth of its area.
|
||||
board_by_area(&takers, bounds)
|
||||
};
|
||||
let clipped = if l.clipped { Vec::new() } else { crate::clip_group::clipped_above(doc, l.id) };
|
||||
let group: Vec<&Layer> = std::iter::once(l).chain(clipped.iter().filter_map(|id| doc.layer(*id))).collect();
|
||||
if from == to || group.iter().any(|m| doc.effective_locks(m.id).artboard) {
|
||||
|
||||
@@ -269,6 +269,10 @@ mod layer_edges_tests;
|
||||
#[path = "canvas/move_readout_arrows_tests.rs"]
|
||||
mod move_readout_arrows_tests;
|
||||
|
||||
#[cfg(test)]
|
||||
#[path = "canvas/move_reference_point_drag_tests.rs"]
|
||||
mod move_reference_point_drag_tests;
|
||||
|
||||
#[cfg(test)]
|
||||
mod live_frame_tests;
|
||||
|
||||
|
||||
@@ -0,0 +1,100 @@
|
||||
//! Move › Show Transform Controls: a drag from the box's centre moves the reference point in a Free Transform session
|
||||
//! instead of the layer (#18161).
|
||||
|
||||
use egui::Modifiers;
|
||||
use photocraft_geom::Rect;
|
||||
use serde_json::json;
|
||||
|
||||
use crate::PhotocraftApp;
|
||||
use crate::canvas::testing::{key, move_to, press, release};
|
||||
use crate::state::Tool;
|
||||
|
||||
/// A 400 × 300 document at 100 % with one layer filled at (100..300, 100..220), so the Move box's centre is (200, 160),
|
||||
/// the Move tool active with Show Transform Controls `controls`.
|
||||
fn fixture(controls: bool) -> Box<PhotocraftApp> {
|
||||
let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default());
|
||||
app.run("file.new", json!({"width": 400, "height": 300})).unwrap();
|
||||
app.sync_views();
|
||||
app.ui.views[0].zoom = 1.0;
|
||||
app.ui.views[0].clear_pending();
|
||||
app.run("layer.new.layer", json!({})).unwrap();
|
||||
app.run("select.rect", json!({"x": 100, "y": 100, "width": 200, "height": 120})).unwrap();
|
||||
app.run("edit.fill", json!({"color": "#0000ff"})).unwrap();
|
||||
app.run("select.deselect", json!({})).unwrap();
|
||||
app.ui.extras.snap = false;
|
||||
app.ui.view.show.smart_guides = false;
|
||||
app.ui.tool_options.move_auto_select = false;
|
||||
app.ui.tool = Tool::Move;
|
||||
app.ui.tool_options.move_show_transform = controls;
|
||||
app
|
||||
}
|
||||
|
||||
fn layer_bounds(app: &PhotocraftApp) -> Rect {
|
||||
let st = app.session.active().unwrap();
|
||||
photocraft_engine::transform_cmds::transform_bounds(&st.doc, st.doc.layer(st.active_layer.unwrap()).unwrap())
|
||||
}
|
||||
|
||||
fn steps(app: &PhotocraftApp) -> usize {
|
||||
app.session.active().unwrap().history.past_len()
|
||||
}
|
||||
|
||||
fn near(a: [f64; 2], b: [f64; 2], within: f64) -> bool {
|
||||
(a[0] - b[0]).abs() <= within && (a[1] - b[1]).abs() <= within
|
||||
}
|
||||
|
||||
/// Drags from `from` to `to` and checks the drag moved the reference point of a Free Transform session on `rect`, with
|
||||
/// the box, the layer and the history as they were; Escape then closes the session with nothing changed.
|
||||
fn assert_reference_point_drag(app: &mut PhotocraftApp, rect: [f64; 4], from: [f64; 2], to: [f64; 2]) {
|
||||
let before = app.session.active().unwrap().doc.clone();
|
||||
let (bounds, history) = (layer_bounds(app), steps(app));
|
||||
press(app, from, Modifiers::NONE);
|
||||
move_to(app, [(from[0] + to[0]) / 2.0, (from[1] + to[1]) / 2.0], Modifiers::NONE);
|
||||
move_to(app, to, Modifiers::NONE);
|
||||
let session = &app.ui.transform.as_ref().expect("a centre drag opens Free Transform").session;
|
||||
let r = session.rect();
|
||||
assert_eq!([r.x0, r.y0, r.x1, r.y1], rect, "the session frames what the Move box framed");
|
||||
let pivot = [session.pivot().x, session.pivot().y];
|
||||
assert!(near(pivot, to, 1.0), "the reference point follows the pointer: {pivot:?}");
|
||||
let quad = session.quad().map(|p| [p.x, p.y]);
|
||||
assert_eq!(quad, [[rect[0], rect[1]], [rect[2], rect[1]], [rect[2], rect[3]], [rect[0], rect[3]]], "the box stays put");
|
||||
release(app, to, Modifiers::NONE);
|
||||
assert!(app.ui.transform.is_some(), "the release ends the drag, not the session");
|
||||
assert_eq!(layer_bounds(app), bounds, "the layer does not move");
|
||||
key(app, egui::Key::Escape, Modifiers::NONE);
|
||||
assert!(app.ui.transform.is_none(), "Escape cancels Free Transform");
|
||||
assert_eq!(app.session.active().unwrap().doc, before, "nothing changed");
|
||||
assert_eq!(steps(app), history, "no history step");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_drag_from_the_box_centre_moves_the_reference_point_not_the_layer() {
|
||||
let mut app = fixture(true);
|
||||
// A few pixels off the exact centre still grabs the point.
|
||||
assert_reference_point_drag(&mut app, [100.0, 100.0, 300.0, 220.0], [202.0, 161.0], [250.0, 190.0]);
|
||||
}
|
||||
|
||||
/// With a selection the box is the selection's bounds, and its centre is the point to drag.
|
||||
#[test]
|
||||
fn a_drag_from_the_selection_box_centre_moves_the_reference_point() {
|
||||
let mut app = fixture(true);
|
||||
app.run("select.rect", json!({"x": 60, "y": 80, "width": 100, "height": 120})).unwrap();
|
||||
assert_reference_point_drag(&mut app, [60.0, 80.0, 160.0, 200.0], [110.0, 140.0], [150.0, 120.0]);
|
||||
}
|
||||
|
||||
/// A drag that starts inside the box away from its centre, or anywhere with the controls off, moves the layer.
|
||||
#[test]
|
||||
fn off_centre_or_without_controls_the_drag_still_moves_the_layer() {
|
||||
for (controls, from) in [(true, [130.0, 120.0]), (false, [200.0, 160.0])] {
|
||||
let mut app = fixture(controls);
|
||||
let (bounds, history) = (layer_bounds(&app), steps(&app));
|
||||
let to = [from[0] + 20.0, from[1] + 10.0];
|
||||
press(&mut app, from, Modifiers::NONE);
|
||||
move_to(&mut app, [from[0] + 10.0, from[1] + 5.0], Modifiers::NONE);
|
||||
move_to(&mut app, to, Modifiers::NONE);
|
||||
release(&mut app, to, Modifiers::NONE);
|
||||
let what = format!("controls {controls}, press {from:?}");
|
||||
assert!(app.ui.transform.is_none(), "{what}: no Free Transform");
|
||||
assert_eq!(layer_bounds(&app), bounds.translate(20, 10), "{what}: the layer moves with the drag");
|
||||
assert_eq!(steps(&app), history + 1, "{what}: one Move step");
|
||||
}
|
||||
}
|
||||
@@ -2,7 +2,7 @@ use super::PhotocraftApp;
|
||||
use super::tools::selection::floating_outline;
|
||||
use crate::state::Tool;
|
||||
use craft::behavior::EditEvent;
|
||||
use craft::behavior::geometry::quad::{BoxRules, BoxState, TransformMode};
|
||||
use craft::behavior::geometry::quad::{BoxPart, BoxRules, BoxState, TransformMode};
|
||||
use craft::behavior::kurbo::{self, BezPath};
|
||||
use craft::draw::ui::transform::{RotateZone, TransformBox, TransformEdit};
|
||||
use craft::ui::contract::{Bind, Props as _, Show as _};
|
||||
@@ -137,7 +137,8 @@ pub(crate) fn begin_controls(app: &mut PhotocraftApp, ctx: &egui::Context) -> Co
|
||||
const CONTROLS_LABEL: &str = "Transform controls";
|
||||
|
||||
/// Move › Show Transform Controls over the canvas body `rect`: a `TransformBox` on [`frame`] whose inside and outside
|
||||
/// (past the corner rotate bands) and arrow keys stay the Move tool's. Its first `Begin` runs [`begin_controls`]; once
|
||||
/// (past the corner rotate bands) and arrow keys stay the Move tool's. Its centre is the reference point: a drag there
|
||||
/// moves the point in a Free Transform session, not the layer (measured, #18161). Its first `Begin` runs [`begin_controls`]; once
|
||||
/// a session starts, this pass's box events go to the session, and from the next pass the session's
|
||||
/// `TransformOverlay`, salted with the same [`BOX_ID_SALT`](crate::transform_tool::BOX_ID_SALT), carries the drag on.
|
||||
pub(crate) fn show(app: &mut PhotocraftApp, ui: &mut egui::Ui, rect: egui::Rect, session_box_shown: bool) {
|
||||
@@ -152,7 +153,7 @@ pub(crate) fn show(app: &mut PhotocraftApp, ui: &mut egui::Ui, rect: egui::Rect,
|
||||
let out = TransformBox::new(CONTROLS_LABEL, Bind::given(BoxState::from_rect(start)), start, 0.0)
|
||||
.rules(rules)
|
||||
.rotate_zone(RotateZone::Band)
|
||||
.reference_point(false)
|
||||
.reference_point(true)
|
||||
.inside_moves(false)
|
||||
.session_arrows(false)
|
||||
.id_salt(crate::transform_tool::BOX_ID_SALT)
|
||||
@@ -165,6 +166,11 @@ pub(crate) fn show(app: &mut PhotocraftApp, ui: &mut egui::Ui, rect: egui::Rect,
|
||||
}
|
||||
let now = ui.input(|i| i.time);
|
||||
if let Some(t) = app.ui.transform.as_mut() {
|
||||
// The session's box hides its reference point unless the setting shows it, and a hidden point takes no part
|
||||
// of the press: show it, so the session's box carries a drag that began on the centre (#18161).
|
||||
if out.held == Some(BoxPart::Pivot) {
|
||||
t.session.apply(TransformEdit::ReferencePoint(true), now);
|
||||
}
|
||||
for event in out.edit.events.iter().cloned() {
|
||||
t.session.apply(TransformEdit::Box(event), now);
|
||||
}
|
||||
|
||||
@@ -13,6 +13,8 @@ pub const MESSAGE: &str = "Could not use the move tool because the layer is lock
|
||||
pub const LINKED_MESSAGE: &str = "Could not use the move tool because linked layers are locked.";
|
||||
/// The alert of a move refused only because a moving group holds a locked layer (#18138), in our own words.
|
||||
pub const GROUP_MESSAGE: &str = "Could not use the move tool because the group contains a locked layer.";
|
||||
/// The alert of an arrow-key nudge refused because the layer is locked (#18130), in our own words.
|
||||
pub const NUDGE_MESSAGE: &str = "Could not nudge the layer because it is locked.";
|
||||
|
||||
/// The selected pixels can't float: only a fresh cut can be refused, so a floating piece is never
|
||||
/// locked. The selection family's ⌘-drag of pixels asks this.
|
||||
@@ -67,6 +69,21 @@ pub fn refused_for_group_child(app: &PhotocraftApp) -> bool {
|
||||
!own_locked && !targets_locked(app) && group_child_locked(app)
|
||||
}
|
||||
|
||||
/// An arrow-key nudge of one layer with no selection that its lock refuses: Lock all or Lock position, its own or a
|
||||
/// group's, which the Background has too (#18130). Several layers and a selection are not measured and keep the
|
||||
/// status-bar message.
|
||||
pub fn nudge_locked(app: &PhotocraftApp) -> bool {
|
||||
let Some(st) = app.session.active() else { return false };
|
||||
let [id] = st.selected_layers() else { return false };
|
||||
let locks = st.doc.effective_locks(*id);
|
||||
st.doc.selection.is_none() && (locks.position || locks.all)
|
||||
}
|
||||
|
||||
/// Opens [`NUDGE_MESSAGE`] as an alert; a press while it is open adds no second one (`alerts::show`).
|
||||
pub fn prompt_nudge(app: &mut PhotocraftApp) {
|
||||
crate::dialogs::alerts::alert(app, tl!(NUDGE_MESSAGE));
|
||||
}
|
||||
|
||||
/// Would a press with the Move tool in effect be a move that fails because of a lock?
|
||||
/// `standing_in` is true when the Move tool stands in for another tool's ⌘ press.
|
||||
pub fn blocked(app: &PhotocraftApp, standing_in: bool) -> bool {
|
||||
@@ -114,6 +131,10 @@ pub fn prompt(app: &mut PhotocraftApp) {
|
||||
crate::dialogs::alerts::alert(app, message);
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
#[path = "move_locked_nudge_alert_tests.rs"]
|
||||
mod move_locked_nudge_alert_tests;
|
||||
|
||||
#[cfg(test)]
|
||||
#[path = "move_lock_linked_text_tests.rs"]
|
||||
mod move_lock_linked_text_tests;
|
||||
|
||||
@@ -0,0 +1,159 @@
|
||||
//! #18130: an arrow-key nudge with the Move tool on a layer locked in place (Lock all, Lock position, the
|
||||
//! Background) opens one alert of its own that says the nudge could not be done, instead of only a status-bar line.
|
||||
//! Layers with only a pixel lock still nudge; several layers and a selection keep today's behaviour.
|
||||
|
||||
use craft::ui::dialog::StackDialog;
|
||||
use egui::{Key, Modifiers};
|
||||
use egui_kittest::Harness;
|
||||
use egui_kittest::kittest::Queryable;
|
||||
use photocraft_doc::{LayerId, Locks};
|
||||
use serde_json::json;
|
||||
|
||||
use super::{MESSAGE, NUDGE_MESSAGE};
|
||||
use crate::PhotocraftApp;
|
||||
use crate::state::Tool;
|
||||
|
||||
/// A name and the locks it sets.
|
||||
type LockCase = (&'static str, fn(&mut Locks));
|
||||
|
||||
/// A document of a Background and one red-filled layer, the Move tool picked and the layer selected.
|
||||
fn harness() -> Harness<'static, Box<PhotocraftApp>> {
|
||||
let mut h = Harness::builder().with_size(egui::vec2(1400.0, 900.0)).with_max_steps(64).build_eframe(|cc| {
|
||||
PhotocraftApp::setup_context(&cc.egui_ctx, Default::default());
|
||||
let mut app = PhotocraftApp::with_context(cc.egui_ctx.clone(), photocraft_engine::Session::new(), crate::Services::default());
|
||||
app.run("file.new", json!({"width": 200, "height": 200})).unwrap();
|
||||
app.run("layer.new.layer", json!({})).unwrap();
|
||||
app.run("select.rect", json!({"x": 40, "y": 40, "width": 20, "height": 20})).unwrap();
|
||||
app.run("edit.fill", json!({"color": "#ff0000"})).unwrap();
|
||||
app.run("select.deselect", json!({})).unwrap();
|
||||
app.ui.tool = Tool::Move;
|
||||
app.ui.tool_options.move_auto_select = false;
|
||||
app
|
||||
});
|
||||
h.run_steps(6);
|
||||
h
|
||||
}
|
||||
|
||||
fn layer(h: &Harness<'_, Box<PhotocraftApp>>) -> LayerId {
|
||||
h.state().session.active().unwrap().active_layer.unwrap()
|
||||
}
|
||||
|
||||
fn background(h: &Harness<'_, Box<PhotocraftApp>>) -> LayerId {
|
||||
h.state().session.active().unwrap().doc.background().unwrap().id
|
||||
}
|
||||
|
||||
fn set_locks(h: &mut Harness<'_, Box<PhotocraftApp>>, id: LayerId, f: impl Fn(&mut Locks)) {
|
||||
h.state_mut()
|
||||
.session
|
||||
.edit("lock", |doc, _| {
|
||||
f(&mut doc.layer_mut(id).unwrap().locks);
|
||||
Ok(())
|
||||
})
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
fn select(h: &mut Harness<'_, Box<PhotocraftApp>>, id: LayerId) {
|
||||
h.state_mut().run("layer.select", json!({"layer": id})).unwrap();
|
||||
h.run_steps(1);
|
||||
}
|
||||
|
||||
/// The messages of the open alerts.
|
||||
fn alerts(h: &Harness<'_, Box<PhotocraftApp>>) -> Vec<String> {
|
||||
let dialogs = &h.state().framework.dialogs;
|
||||
let message = |id| dialogs.get_by_id::<craft::ui::dialog::HostedAlert>(id)?.json_fields()?.get("message")?.as_str().map(String::from);
|
||||
dialogs.stack().ids().filter_map(message).collect()
|
||||
}
|
||||
|
||||
fn history(h: &Harness<'_, Box<PhotocraftApp>>) -> Vec<String> {
|
||||
h.state().session.active().unwrap().history.entries().to_vec()
|
||||
}
|
||||
|
||||
fn offset(h: &Harness<'_, Box<PhotocraftApp>>, id: LayerId) -> (i32, i32) {
|
||||
let st = h.state().session.active().unwrap();
|
||||
let b = st.doc.layer(id).unwrap().surface().unwrap().content_bounds();
|
||||
(b.x0, b.y0)
|
||||
}
|
||||
|
||||
fn press(h: &mut Harness<'_, Box<PhotocraftApp>>, mods: Modifiers, key: Key) {
|
||||
h.key_press_modifiers(mods, key);
|
||||
h.run_steps(2);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_nudge_of_a_layer_locked_in_place_opens_one_alert_of_its_own() {
|
||||
let cases: [LockCase; 3] = [("Lock all", |l| l.all = true), ("Lock position", |l| l.position = true), ("Background", |_| {})];
|
||||
for (name, lock) in cases {
|
||||
for mods in [Modifiers::NONE, Modifiers::SHIFT] {
|
||||
let mut h = harness();
|
||||
let id = if name == "Background" { background(&h) } else { layer(&h) };
|
||||
set_locks(&mut h, id, lock);
|
||||
select(&mut h, id);
|
||||
let (before, at) = (history(&h), offset(&h, id));
|
||||
press(&mut h, mods, Key::ArrowRight);
|
||||
assert_eq!(alerts(&h), [NUDGE_MESSAGE], "{name}, {mods:?}");
|
||||
// The 2nd and 3rd press go to the alert: still one, nothing moves.
|
||||
press(&mut h, mods, Key::ArrowRight);
|
||||
press(&mut h, mods, Key::ArrowDown);
|
||||
assert_eq!(alerts(&h), [NUDGE_MESSAGE], "{name}: one alert for three presses");
|
||||
assert_eq!((history(&h), offset(&h, id)), (before.clone(), at), "{name}: nothing moved, no state");
|
||||
}
|
||||
}
|
||||
assert_ne!(NUDGE_MESSAGE, MESSAGE, "a text other than the drag alert's");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn enter_space_escape_and_ok_close_the_alert_and_no_press_is_replayed() {
|
||||
for close in ["Enter", "Space", "Escape", "OK"] {
|
||||
let mut h = harness();
|
||||
let id = layer(&h);
|
||||
set_locks(&mut h, id, |l| l.position = true);
|
||||
let (before, at) = (history(&h), offset(&h, id));
|
||||
press(&mut h, Modifiers::NONE, Key::ArrowLeft);
|
||||
press(&mut h, Modifiers::NONE, Key::ArrowLeft);
|
||||
assert_eq!(alerts(&h).len(), 1, "{close}");
|
||||
match close {
|
||||
"Enter" => press(&mut h, Modifiers::NONE, Key::Enter),
|
||||
"Space" => press(&mut h, Modifiers::NONE, Key::Space),
|
||||
"Escape" => press(&mut h, Modifiers::NONE, Key::Escape),
|
||||
_ => {
|
||||
h.get_by_label("OK").click();
|
||||
h.run_steps(2);
|
||||
}
|
||||
}
|
||||
h.run_steps(4);
|
||||
assert!(alerts(&h).is_empty(), "{close} closed the alert and no other followed");
|
||||
assert_eq!((history(&h), offset(&h, id)), (before, at), "{close}: nothing moved");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn layers_with_only_a_pixel_lock_still_nudge() {
|
||||
let cases: [LockCase; 3] = [("no lock", |_| {}), ("Lock image pixels", |l| l.pixels = true), ("Lock transparent pixels", |l| l.transparency = true)];
|
||||
for (name, lock) in cases {
|
||||
let mut h = harness();
|
||||
let id = layer(&h);
|
||||
set_locks(&mut h, id, lock);
|
||||
let at = offset(&h, id);
|
||||
press(&mut h, Modifiers::NONE, Key::ArrowRight);
|
||||
assert!(alerts(&h).is_empty(), "{name}");
|
||||
assert!(offset(&h, id).0 > at.0, "{name}: the layer moved right");
|
||||
assert_eq!(history(&h).last().map(String::as_str), Some("Nudge"), "{name}");
|
||||
}
|
||||
}
|
||||
|
||||
/// Not measured, so they keep today's behaviour: no nudge alert with several layers selected or with a selection.
|
||||
#[test]
|
||||
fn several_layers_or_a_selection_open_no_nudge_alert() {
|
||||
let mut h = harness();
|
||||
let bg = background(&h);
|
||||
h.state_mut().run("layer.select", json!({"layer": bg, "mode": "add"})).unwrap();
|
||||
press(&mut h, Modifiers::NONE, Key::ArrowRight);
|
||||
assert!(!alerts(&h).iter().any(|m| m == NUDGE_MESSAGE), "several layers");
|
||||
|
||||
let mut h = harness();
|
||||
let id = layer(&h);
|
||||
set_locks(&mut h, id, |l| l.position = true);
|
||||
h.state_mut().run("select.rect", json!({"x": 40, "y": 40, "width": 20, "height": 20})).unwrap();
|
||||
press(&mut h, Modifiers::NONE, Key::ArrowRight);
|
||||
assert!(!alerts(&h).iter().any(|m| m == NUDGE_MESSAGE), "a selection");
|
||||
}
|
||||
@@ -191,6 +191,11 @@ pub fn nudge(app: &mut PhotocraftApp, dx: f64, dy: f64, duplicate_first: bool) {
|
||||
crate::move_lock::prompt(app);
|
||||
return;
|
||||
}
|
||||
// One locked layer and no selection: nothing moves, the nudge's own alert says why (#18130).
|
||||
if !duplicate_first && crate::move_lock::nudge_locked(app) {
|
||||
crate::move_lock::prompt_nudge(app);
|
||||
return;
|
||||
}
|
||||
let from = if duplicate_first { duplicate(app) } else { None };
|
||||
let revision = app.session.active().map(|st| st.revision);
|
||||
let moved = if duplicate_first {
|
||||
|
||||
Reference in new issue
Block a user