mirror of
https://github.com/storytold/photocraft.git
synced 2026-12-04 04:52:46 -05:00
Merge pull request #20057
Co-authored-by: autocraft-bot[bot] <354215646+autocraft-bot[bot]@users.noreply.github.com>
This commit is contained in:
commit
6d00b2d530
2 files changed
+201
-3
No files matched your search
@@ -15,12 +15,18 @@
|
||||
use craft::behavior::Axis;
|
||||
use craft::behavior::kurbo::Rect;
|
||||
use craft::draw::ui::measure::box_distances;
|
||||
use craft::ui::canvas::{CanvasSpace, tool_pass};
|
||||
use craft::ui::canvas_controls::SmartGuides;
|
||||
use craft::ui::contract::Show as _;
|
||||
use craft::ui::primitives::{RectSide, Rule};
|
||||
use craft::ui::runtime::env::Env;
|
||||
use craft::ui::style::{RuleLook, RuleWidth, rgba_color};
|
||||
use egui::emath::GuiRounding as _;
|
||||
use photocraft_doc::{DocId, LayerId};
|
||||
use photocraft_engine::snap::union;
|
||||
|
||||
use crate::PhotocraftApp;
|
||||
use crate::canvas::document_display::CanvasHost;
|
||||
use crate::state::Tool;
|
||||
|
||||
/// The last answer: the boxes measured with the pointer at `at` on that revision and selection.
|
||||
@@ -30,6 +36,7 @@ pub(crate) struct Cache {
|
||||
at: [i32; 2],
|
||||
group: bool,
|
||||
selected: Vec<LayerId>,
|
||||
selected_bounds: Option<[f64; 4]>,
|
||||
boxes: Option<([f64; 4], [f64; 4])>,
|
||||
}
|
||||
|
||||
@@ -55,7 +62,7 @@ fn boxes(app: &mut PhotocraftApp, at: [f64; 2]) -> Option<([f64; 4], [f64; 4])>
|
||||
// The selection itself (or a layer in a selected group) isn't another layer: nothing to measure.
|
||||
let on_selection = hovered.is_some_and(|id| selected.iter().any(|s| *s == id || d.path_of(id).zip(d.path_of(*s)).is_some_and(|(p, q)| p.starts_with(&q))));
|
||||
let boxes = a.filter(|_| !on_selection).map(|a| (a, hovered.and_then(|id| crate::snap_ui::tree_rect(d, id)).unwrap_or(canvas)));
|
||||
app.move_distance = Some(Cache { doc, revision, at, group, selected: selected.to_vec(), boxes });
|
||||
app.move_distance = Some(Cache { doc, revision, at, group, selected: selected.to_vec(), selected_bounds: a, boxes });
|
||||
boxes
|
||||
}
|
||||
|
||||
@@ -90,6 +97,45 @@ fn duplicate_drag(app: &PhotocraftApp) -> Option<(Rect, Rect)> {
|
||||
Some((rect(r), rect([r[0] + d[0], r[1] + d[1], r[2] + d[0], r[3] + d[1]])))
|
||||
}
|
||||
|
||||
/// Bounds of the single selected layer at actual size, independent of the hover measurement's target.
|
||||
fn selected_outline_bounds(app: &PhotocraftApp, space: CanvasSpace, canvas: egui::Id, hover: egui::Pos2) -> Option<egui::Rect> {
|
||||
if app.ui.tool != Tool::Move
|
||||
|| !app.ui.view.shows(app.ui.view.show.smart_guides)
|
||||
|| app.ui.transform.is_some()
|
||||
|| duplicate_drag(app).is_some()
|
||||
|| !space.view.rect.contains(hover)
|
||||
{
|
||||
return None;
|
||||
}
|
||||
let idx = app.session.active_index()?;
|
||||
if canvas != CanvasHost::primary(app, idx).canvas_id() {
|
||||
return None;
|
||||
}
|
||||
let st = app.session.active()?;
|
||||
let [layer] = st.selected_layers() else { return None };
|
||||
if st.doc.is_background(*layer) {
|
||||
return None;
|
||||
}
|
||||
|
||||
let ppp = space.pixels_per_point;
|
||||
let actual_size = |zoom: f64| (zoom * f64::from(ppp) - 1.0).abs() <= 1e-8;
|
||||
// Keep the measured 100% upright case, on displays with any interface scale.
|
||||
if !(ppp.is_finite() && ppp > 0.0 && actual_size(space.view.zoom) && actual_size(space.view.zoom_x()) && space.view.rotation.abs() < 1e-8) {
|
||||
return None;
|
||||
}
|
||||
let cache = app.move_distance.as_ref().filter(|cache| cache.doc == st.doc.id && cache.revision == st.revision && cache.selected == st.selected_layers())?;
|
||||
let bounds = cache.selected_bounds?;
|
||||
let bounds = egui::Rect::from_points(&space.view.quad(rect(bounds))).round_to_pixels(ppp);
|
||||
bounds.is_positive().then_some(bounds)
|
||||
}
|
||||
|
||||
fn outline_shapes(bounds: egui::Rect, ppp: f32, color: egui::Color32) -> impl Iterator<Item = egui::Shape> {
|
||||
// Rule::edge puts the hairline just inside this expanded rect, on the pixel immediately outside the layer.
|
||||
let outside = bounds.expand(1.0 / ppp);
|
||||
let rule = RuleLook { color, width: RuleWidth::Hairline };
|
||||
[RectSide::Top, RectSide::Bottom, RectSide::Left, RectSide::Right].into_iter().flat_map(move |side| Rule::edge(&rule, outside, side, ppp))
|
||||
}
|
||||
|
||||
/// Shows the measurements over the canvas `rect`, in the Smart Guides colour. `pointer` is the
|
||||
/// screen hover position.
|
||||
pub(crate) fn show(app: &mut PhotocraftApp, ui: &mut egui::Ui, canvas: egui::Rect, pointer: Option<egui::Pos2>) {
|
||||
@@ -98,7 +144,27 @@ pub(crate) fn show(app: &mut PhotocraftApp, ui: &mut egui::Ui, canvas: egui::Rec
|
||||
let measure_hover = modifiers.cmd && !modifiers.alt;
|
||||
let pressed = crate::canvas::tools::in_gesture(app);
|
||||
let at = pointer.and_then(|p| crate::canvas::document_display::doc_point_at(app, p)).map(|p| [p.x, p.y]);
|
||||
let Some((a, b)) = current(app, at, measure_hover, pressed) else { return };
|
||||
let measurement = current(app, at, measure_hover, pressed);
|
||||
let outline = if measure_hover && !pressed {
|
||||
let env = Env::of(ui);
|
||||
let hover = pointer.filter(|pos| ui.ctx().layer_id_at(*pos).is_none_or(|layer| layer == ui.layer_id()));
|
||||
tool_pass(&env.view()).and_then(|pass| {
|
||||
let space = pass.space?;
|
||||
let bounds = selected_outline_bounds(app, space, pass.id, hover?)?;
|
||||
Some((bounds, space.pixels_per_point, env.tokens.color.data.magenta))
|
||||
})
|
||||
} else {
|
||||
None
|
||||
};
|
||||
if outline.is_none() && measurement.is_none() {
|
||||
return;
|
||||
}
|
||||
let color = crate::snap_ui::smart_guide_color(app);
|
||||
if let Some((bounds, ppp, fallback)) = outline {
|
||||
let ink = color.map_or_else(|| rgba_color(fallback), |color| color.0);
|
||||
ui.painter().with_clip_rect(canvas).extend(outline_shapes(bounds, ppp, ink));
|
||||
}
|
||||
let Some((a, b)) = measurement else { return };
|
||||
let Some(st) = app.session.active() else { return };
|
||||
let (dpi, size) = (f64::from(st.doc.resolution_dpi), st.doc.size);
|
||||
let ur = &app.session.prefs().units_and_rulers;
|
||||
@@ -110,7 +176,7 @@ pub(crate) fn show(app: &mut PhotocraftApp, ui: &mut egui::Ui, canvas: egui::Rec
|
||||
format!("{} {}", ur.format(len, dpi, extent), ur.rulers.suffix())
|
||||
});
|
||||
let guides = SmartGuides::new(&lines, &measures);
|
||||
let guides = match crate::snap_ui::smart_guide_color(app) {
|
||||
let guides = match color {
|
||||
Some(color) => guides.color(color),
|
||||
None => guides,
|
||||
};
|
||||
@@ -205,6 +271,10 @@ mod tests {
|
||||
#[path = "move_distance_self_tests.rs"]
|
||||
mod move_distance_self_tests;
|
||||
|
||||
#[cfg(test)]
|
||||
#[path = "move_distance_selected_outline_tests.rs"]
|
||||
mod move_distance_selected_outline_tests;
|
||||
|
||||
#[cfg(test)]
|
||||
#[path = "move_distance_ctrl_alt_tests.rs"]
|
||||
mod move_distance_ctrl_alt_tests;
|
||||
@@ -0,0 +1,128 @@
|
||||
use craft::behavior::CanvasView;
|
||||
use craft::behavior::kurbo::Point;
|
||||
use egui::emath::GuiRounding as _;
|
||||
use egui::{Color32, Modifiers, Pos2, Shape, Stroke, pos2, vec2};
|
||||
use serde_json::json;
|
||||
|
||||
use super::{CanvasHost, CanvasSpace, PhotocraftApp, Rect, Tool, current, outline_shapes, selected_outline_bounds};
|
||||
use crate::canvas::document_display::active_view;
|
||||
use crate::input::shortcut_dispatch::tests::app_harness;
|
||||
|
||||
const B: Rect = Rect::new(600.0, 400.0, 750.0, 520.0);
|
||||
const COLOR: Color32 = Color32::from_rgb(0x0b, 0xd4, 0x31);
|
||||
|
||||
fn document() -> Box<PhotocraftApp> {
|
||||
let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default());
|
||||
app.run("file.new", json!({"width": 1000, "height": 800, "background": "white"})).unwrap();
|
||||
for (name, x, y, width, height) in [("A", 300, 225, 150, 100), ("B", 600, 400, 150, 120)] {
|
||||
app.run("layer.new.layer", json!({"name": name})).unwrap();
|
||||
app.run("select.rect", json!({"x": x, "y": y, "width": width, "height": height})).unwrap();
|
||||
app.run("edit.fill", json!({"color": "#ff0000"})).unwrap();
|
||||
}
|
||||
app.run("select.deselect", json!({})).unwrap();
|
||||
app.session.prefs.edit(|prefs| prefs.guides_grid_and_slices.smart_guide_color = "#0bd431".into());
|
||||
app.ui.tool = Tool::Move;
|
||||
app.ui.view.extras = true;
|
||||
app.ui.view.show.smart_guides = true;
|
||||
app.sync_views();
|
||||
app
|
||||
}
|
||||
|
||||
fn space(ppp: f32) -> CanvasSpace {
|
||||
let screen = egui::Rect::from_min_size(Pos2::ZERO, vec2(1000.0, 800.0) / ppp);
|
||||
CanvasSpace::new(CanvasView::new(screen, Point::new(500.0, 400.0), CanvasView::points_per_unit(1.0, ppp)), ppp)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn selected_outline_uses_the_pixels_immediately_outside_the_layer_at_one_and_two_ppp() {
|
||||
let mut app = document();
|
||||
assert!(current(&mut app, Some([675.0, 460.0]), true, false).is_none(), "self-hover caches the selected bounds without a measurement");
|
||||
let id = CanvasHost::primary(&app, 0).canvas_id();
|
||||
for ppp in [1.0, 2.0] {
|
||||
let space = space(ppp);
|
||||
let bounds = selected_outline_bounds(&app, space, id, space.view.to_screen(B.center())).unwrap();
|
||||
let shapes = outline_shapes(bounds, ppp, COLOR).collect::<Vec<_>>();
|
||||
let expected = [
|
||||
[pos2(599.0, 399.5), pos2(751.0, 399.5)],
|
||||
[pos2(599.0, 520.5), pos2(751.0, 520.5)],
|
||||
[pos2(599.5, 399.0), pos2(599.5, 521.0)],
|
||||
[pos2(750.5, 399.0), pos2(750.5, 521.0)],
|
||||
];
|
||||
assert_eq!(shapes.len(), expected.len());
|
||||
for (shape, expected) in shapes.iter().zip(expected) {
|
||||
let Shape::LineSegment { points, stroke } = shape else { panic!("expected a hairline rule") };
|
||||
assert_eq!(points.map(|point| pos2(point.x * ppp, point.y * ppp)), expected);
|
||||
assert_eq!(*stroke, Stroke::new(1.0 / ppp, COLOR));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn selected_outline_requires_primary_canvas_upright_actual_size_and_one_non_background_layer() {
|
||||
let mut app = document();
|
||||
assert!(current(&mut app, Some([675.0, 460.0]), true, false).is_none());
|
||||
let space = space(1.0);
|
||||
let id = CanvasHost::primary(&app, 0).canvas_id();
|
||||
let at = space.view.to_screen(B.center());
|
||||
assert_eq!(selected_outline_bounds(&app, space, id, at), Some(egui::Rect::from_min_max(pos2(600.0, 400.0), pos2(750.0, 520.0))));
|
||||
assert!(selected_outline_bounds(&app, space, egui::Id::unique("another canvas"), at).is_none());
|
||||
assert!(selected_outline_bounds(&app, space, id, pos2(-1.0, -1.0)).is_none());
|
||||
let turned = CanvasSpace::new(space.view.rotation(15.0), 1.0);
|
||||
assert!(selected_outline_bounds(&app, turned, id, at).is_none());
|
||||
let zoomed = CanvasSpace::new(CanvasView::new(space.view.rect, space.view.center, 2.0), 1.0);
|
||||
assert!(selected_outline_bounds(&app, zoomed, id, at).is_none());
|
||||
app.run("layer.select", json!({"name": "A", "mode": "add"})).unwrap();
|
||||
assert!(selected_outline_bounds(&app, space, id, at).is_none());
|
||||
let background = app.session.active().unwrap().doc.walk().first().unwrap().2.id;
|
||||
app.run("layer.select", json!({"layer": background.0})).unwrap();
|
||||
assert!(selected_outline_bounds(&app, space, id, at).is_none());
|
||||
}
|
||||
|
||||
fn collect_guide_rules(shape: &Shape, out: &mut Vec<([Pos2; 2], Stroke)>) {
|
||||
match shape {
|
||||
Shape::Vec(shapes) => shapes.iter().for_each(|shape| collect_guide_rules(shape, out)),
|
||||
Shape::PaintChunk(shapes) => shapes.iter().for_each(|shape| collect_guide_rules(&shape.shape, out)),
|
||||
Shape::LineSegment { points, stroke } if stroke.color == COLOR => out.push((*points, *stroke)),
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn selected_outline_is_drawn_through_the_canvas_body_even_on_self_hover_and_hides_with_alt() {
|
||||
let mut h = app_harness(document());
|
||||
h.state_mut().run("view.actualPixels", json!({})).unwrap();
|
||||
h.run_steps(24);
|
||||
let view = active_view(h.state()).unwrap();
|
||||
let ppp = h.ctx.pixels_per_point();
|
||||
let bounds = egui::Rect::from_points(&view.quad(B)).round_to_pixels(ppp);
|
||||
assert!(view.rect.contains_rect(bounds.expand(1.0 / ppp)), "test layer and its outline are visible at actual size");
|
||||
let mut expected = Vec::new();
|
||||
for shape in outline_shapes(bounds, ppp, COLOR) {
|
||||
collect_guide_rules(&shape, &mut expected);
|
||||
}
|
||||
assert_eq!(expected.len(), 4);
|
||||
for hover in [Point::new(375.0, 270.0), Point::new(500.0, 350.0), B.center()] {
|
||||
h.event(egui::Event::ModifiersChanged(Modifiers::COMMAND));
|
||||
let screen = view.to_screen(hover);
|
||||
assert!(view.rect.contains(screen));
|
||||
h.hover_at(screen);
|
||||
h.run_steps(2);
|
||||
let mut actual = Vec::new();
|
||||
for shape in &h.output().shapes {
|
||||
collect_guide_rules(&shape.shape, &mut actual);
|
||||
}
|
||||
assert!(expected.iter().all(|rule| actual.contains(rule)), "the body paints all four edges at {hover:?}: {actual:?}");
|
||||
if hover == B.center() {
|
||||
assert_eq!(actual, expected, "self-hover draws the outline but no measurements");
|
||||
} else {
|
||||
assert!(current(h.state_mut(), Some([hover.x, hover.y]), true, false).is_some(), "another layer and empty canvas still measure");
|
||||
}
|
||||
}
|
||||
h.event(egui::Event::ModifiersChanged(Modifiers::COMMAND | Modifiers::ALT));
|
||||
h.run_steps(2);
|
||||
let mut actual = Vec::new();
|
||||
for shape in &h.output().shapes {
|
||||
collect_guide_rules(&shape.shape, &mut actual);
|
||||
}
|
||||
assert!(actual.is_empty(), "Alt suppresses the selection outline along with the measurements");
|
||||
}
|
||||
Reference in new issue
Block a user