mirror of
https://github.com/storytold/photocraft.git
synced 2026-12-03 06:31:42 +01:00
Move tool: show W and H beside the pointer while drawing the layer marquee (#18162)
A Move drag that starts on empty canvas (or only on locked layers) with Auto-Select draws the layer marquee, and nothing showed its size. The marquee now asks for the same W and H readout the selection marquee shows, in its words and pixel units, on every drag step. Show Transformation Values hides it as it hides the drag readout. The release and the layers it selects are unchanged.
This commit is contained in:
1 parent
0ec46b1409
commit
fc177b03c7
3 files changed
+94
-1
No files matched your search
@@ -353,7 +353,10 @@ impl CanvasTool for MoveRun<'_> {
|
||||
let at = space.view.to_doc(pointer.pos);
|
||||
match run.kind {
|
||||
Kind::Marquee => {
|
||||
self.scope.with_app(|app| crate::move_box::drag_to(app, [at.x, at.y]));
|
||||
self.scope.with_app(|app| {
|
||||
crate::move_box::drag_to(app, [at.x, at.y]);
|
||||
crate::move_box::readout(app, cx);
|
||||
});
|
||||
}
|
||||
Kind::Refused => {
|
||||
if !std::mem::replace(&mut run.prompted, true) {
|
||||
|
||||
@@ -7,6 +7,8 @@
|
||||
use serde_json::json;
|
||||
|
||||
use craft::behavior::kurbo::{Point, Rect};
|
||||
use craft::imaging::ui::select::SelectWords;
|
||||
use craft::ui::canvas::ToolContext;
|
||||
use craft::ui::canvas_controls::Marquee;
|
||||
use craft::ui::contract::Show as _;
|
||||
|
||||
@@ -85,6 +87,15 @@ pub(crate) fn cancel(app: &mut PhotocraftApp) {
|
||||
app.layer_box = None;
|
||||
}
|
||||
|
||||
/// The marquee's W and H beside the pointer while it is dragged, in the selection marquee's words and units
|
||||
/// (Show Transformation Values hides it, as it hides the drag readout).
|
||||
pub(crate) fn readout(app: &PhotocraftApp, cx: &mut ToolContext<'_>) {
|
||||
let Some(b) = app.layer_box else { return };
|
||||
let rect = Rect::from_points(Point::new(b.start[0], b.start[1]), Point::new(b.end[0], b.end[1]));
|
||||
let (words, units) = (cx.words::<SelectWords>(), cx.units());
|
||||
cx.readout(vec![(words.width.clone(), units.pixels(rect.width())), (words.height.clone(), units.pixels(rect.height()))]);
|
||||
}
|
||||
|
||||
/// The marquee being dragged, over the canvas `canvas`.
|
||||
pub(crate) fn show(app: &PhotocraftApp, ui: &mut egui::Ui, canvas: egui::Rect) {
|
||||
let Some(b) = app.layer_box else { return };
|
||||
@@ -205,3 +216,7 @@ mod tests {
|
||||
assert_eq!(app.session.active().unwrap().selected_layers(), vec![paint]);
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
#[path = "move_marquee_readout_tests.rs"]
|
||||
mod move_marquee_readout_tests;
|
||||
@@ -0,0 +1,75 @@
|
||||
//! #18162: the Move tool's layer marquee shows its W and H beside the pointer while it is dragged, as a layer drag
|
||||
//! shows its distances, and adds no history step.
|
||||
|
||||
use serde_json::json;
|
||||
|
||||
use crate::canvas::{ToolEvent, tool_event, tool_event_response};
|
||||
use crate::state::Tool;
|
||||
use crate::{PhotocraftApp, Services};
|
||||
|
||||
/// A white document whose only layer is the locked Background, with the Move tool and Auto-Select on: a press
|
||||
/// anywhere on the canvas starts the layer marquee.
|
||||
fn app() -> Box<PhotocraftApp> {
|
||||
let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), Services::default());
|
||||
app.run("file.new", json!({"width": 200, "height": 150})).unwrap();
|
||||
app.sync_views();
|
||||
app.ui.tool = Tool::Move;
|
||||
app.ui.tool_options.move_auto_select = true;
|
||||
app
|
||||
}
|
||||
|
||||
/// The readout rows of the pass that moves the pointer to `to`, as label and value text.
|
||||
fn readout(app: &mut PhotocraftApp, to: [f64; 2]) -> Vec<(String, String)> {
|
||||
tool_event_response(app, ToolEvent::Move { x: to[0], y: to[1], pressure: 1.0 }, egui::Modifiers::NONE)
|
||||
.readout
|
||||
.expect("the layer marquee has a readout")
|
||||
.rows
|
||||
.into_iter()
|
||||
.map(|(label, value)| (label.as_str().to_owned(), value))
|
||||
.collect()
|
||||
}
|
||||
|
||||
/// The number a readout value shows ("42 px" reads 42).
|
||||
fn number(value: &str) -> f64 {
|
||||
value.trim_end_matches("px").trim().parse().unwrap_or_else(|_| panic!("a pixel value: {value:?}"))
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_layer_marquee_reads_its_width_and_height_as_it_grows() {
|
||||
let mut app = app();
|
||||
tool_event(&mut app, ToolEvent::Down { x: 20.0, y: 20.0, pressure: 1.0 }, egui::Modifiers::NONE);
|
||||
let first = readout(&mut app, [60.0, 50.0]);
|
||||
assert!(app.layer_box.is_some(), "the press started the layer marquee");
|
||||
let [(w_label, w), (h_label, h)] = first.as_slice() else { panic!("two rows, W and H: {first:?}") };
|
||||
assert!(w_label.starts_with('W') && h_label.starts_with('H'), "{first:?}");
|
||||
assert!(w.ends_with("px") && h.ends_with("px"), "{first:?}");
|
||||
assert!(number(w) > 0.0 && number(h) > 0.0, "{first:?}");
|
||||
|
||||
let wider = readout(&mut app, [120.0, 50.0]);
|
||||
assert!(number(&wider[0].1) > number(w), "a wider drag reads a larger W: {first:?} then {wider:?}");
|
||||
assert_eq!(wider[1].1, *h, "H stays while only the width changes");
|
||||
|
||||
let taller = readout(&mut app, [120.0, 110.0]);
|
||||
assert!(number(&taller[1].1) > number(&wider[1].1), "a taller drag reads a larger H: {wider:?} then {taller:?}");
|
||||
|
||||
// Up and to the left of the press, the box reads its size, not a negative one.
|
||||
let back = readout(&mut app, [5.0, 10.0]);
|
||||
assert!(number(&back[0].1) > 0.0 && number(&back[1].1) > 0.0, "{back:?}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_layer_marquee_adds_no_history_step_and_leaves_the_background() {
|
||||
let mut app = app();
|
||||
let (steps, bg, bounds) = {
|
||||
let st = app.session.active().unwrap();
|
||||
let bg = st.active_layer.unwrap();
|
||||
(st.history.past_len(), bg, st.doc.layer(bg).unwrap().surface().unwrap().content_bounds())
|
||||
};
|
||||
tool_event(&mut app, ToolEvent::Down { x: 20.0, y: 20.0, pressure: 1.0 }, egui::Modifiers::NONE);
|
||||
assert!(!readout(&mut app, [90.0, 70.0]).is_empty());
|
||||
tool_event(&mut app, ToolEvent::Up { x: 90.0, y: 70.0 }, egui::Modifiers::NONE);
|
||||
let st = app.session.active().unwrap();
|
||||
assert_eq!(st.history.past_len(), steps, "no history step");
|
||||
assert_eq!(st.doc.layer(bg).unwrap().surface().unwrap().content_bounds(), bounds, "the Background did not move");
|
||||
assert!(app.layer_box.is_none());
|
||||
}
|
||||
Reference in new issue
Block a user