mirror of
https://github.com/storytold/photocraft.git
synced 2026-12-03 06:31:42 +01:00
Star tool: Align Edges in the options bar (#16869)
The Star tool's bar now ends with Align Edges, on by default and greyed in Path and Pixels mode, as on the other shape tools. With it off a star keeps the drag's fractional box in Shape mode. Fixes #16869
This commit is contained in:
1 parent
038d035110
commit
36ccb885a5
5 files changed
+103
-13
No files matched your search
@@ -159,10 +159,9 @@ fn bar(app: &mut PhotocraftApp, ui: &mut egui::Ui, tool: Tool, open: &mut BarOpe
|
||||
}
|
||||
}
|
||||
// Every shape tool's one Align Edges, at the bar's right end after the gear (#13346, #13370,
|
||||
// #13326). Not the Star tool's, which the reference application doesn't have (its stars are the
|
||||
// Polygon tool's). Only a shape layer has pixel edges to align: greyed in Path mode (and in a
|
||||
// pixels mode).
|
||||
if is_shape_tool(tool) && tool != Tool::Star {
|
||||
// #13326, #16869 for the Star tool). Only a shape layer has pixel edges to align: greyed in Path
|
||||
// mode (and in a pixels mode).
|
||||
if is_shape_tool(tool) {
|
||||
Divider::vertical().show(ui);
|
||||
super::alignment::shape_tool_option(app, ui);
|
||||
}
|
||||
|
||||
@@ -72,7 +72,6 @@ fn path_and_pixels_mode_keep_snapping_with_the_greyed_checkbox_off() {
|
||||
}
|
||||
app.ui.tool_options.shape_mode = VectorMode::Shape;
|
||||
assert!(!super::shapes::snaps_to_pixels(&app, ShapeKind::Polygon), "Shape mode with Align Edges off keeps fractions");
|
||||
assert!(super::shapes::snaps_to_pixels(&app, ShapeKind::Star), "the Star tool has no Align Edges and still snaps");
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -132,7 +131,4 @@ fn the_bar_ends_with_align_edges_enabled_only_in_shape_mode() {
|
||||
h.run_steps(4);
|
||||
assert_eq!(h.get_by_label("Align Edges").accesskit_node().is_disabled(), greyed, "{mode:?} mode");
|
||||
}
|
||||
|
||||
let h = harness(Tool::Star);
|
||||
assert!(h.query_by_label("Align Edges").is_none(), "the Star tool has no Align Edges");
|
||||
}
|
||||
@@ -136,12 +136,15 @@ fn whole_box(rect: &Rect, whole: bool) -> Option<Rect> {
|
||||
(w >= 1.0 && h >= 1.0).then(|| Rect::new(x0, y0, x0 + w, y0 + h))
|
||||
}
|
||||
|
||||
/// Whether a drag of `kind` lands on whole pixels. The Triangle (#13370), Rectangle (#13346) and
|
||||
/// Polygon (#13326) drags keep their fractions with Align Edges off in Shape mode; the other Shape
|
||||
/// tools' drags (the framework's drag lands them on pixels), and a greyed checkbox, still snap.
|
||||
/// Whether a drag of `kind` lands on whole pixels. The Triangle (#13370), Rectangle (#13346),
|
||||
/// Polygon (#13326) and Star (#16869) drags keep their fractions with Align Edges off in Shape mode;
|
||||
/// the other Shape tools' drags (the framework's drag lands them on pixels), and a greyed checkbox,
|
||||
/// still snap.
|
||||
pub(super) fn snaps_to_pixels(app: &PhotocraftApp, kind: ShapeKind) -> bool {
|
||||
let o = &app.ui.tool_options;
|
||||
!(matches!(kind, ShapeKind::Triangle | ShapeKind::Rectangle | ShapeKind::Polygon) && o.shape_mode == super::VectorMode::Shape && !o.align_edges)
|
||||
!(matches!(kind, ShapeKind::Triangle | ShapeKind::Rectangle | ShapeKind::Polygon | ShapeKind::Star)
|
||||
&& o.shape_mode == super::VectorMode::Shape
|
||||
&& !o.align_edges)
|
||||
}
|
||||
|
||||
/// `geometry` as a drag commits it: boxes on whole pixels, a line aligned to pixel edges
|
||||
@@ -542,3 +545,7 @@ mod triangle_history_ui_tests {
|
||||
assert!(st.doc.layers.iter().any(|l| l.name == "Triangle 1"));
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
#[path = "star_align_edges_tests.rs"]
|
||||
mod star_align_edges_tests;
|
||||
@@ -0,0 +1,88 @@
|
||||
//! #16869: the Star tool's Align Edges checkbox, at the options bar's right end as on the other
|
||||
//! shape tools. On (the default) a star's box snaps to whole pixels; off it keeps the drag's
|
||||
//! fractional coordinates. It is greyed in Path and Pixels mode, where the box still snaps.
|
||||
|
||||
use craft::draw::ui::VectorMode;
|
||||
use craft::draw::ui::shape_drag::ShapeKind;
|
||||
use egui::Modifiers;
|
||||
use egui::accesskit::Toggled;
|
||||
use egui_kittest::kittest::{NodeT, Queryable};
|
||||
use serde_json::json;
|
||||
|
||||
use super::super::test_input::harness;
|
||||
use crate::PhotocraftApp;
|
||||
use crate::canvas::{ToolEvent, tool_event};
|
||||
use crate::state::{Tool, ToolOptions};
|
||||
|
||||
const START: [f64; 2] = [10.3, 10.6];
|
||||
const END: [f64; 2] = [40.7, 30.2];
|
||||
/// The box `[x, y, w, h]` from `START` to `END` as dragged.
|
||||
const FRACTIONAL: [f64; 4] = [10.3, 10.6, 30.4, 19.6];
|
||||
|
||||
/// The live star box a Star drag from `START` to `END` draws with the options `set` gives, `None`
|
||||
/// when it made no layer. The pixel-grid snap of vector tools is off, so only Align Edges rounds.
|
||||
fn star_box(set: impl FnOnce(&mut ToolOptions)) -> Option<[f64; 4]> {
|
||||
let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default());
|
||||
app.run("file.new", json!({"width": 200, "height": 100})).unwrap();
|
||||
app.run("prefs.set", json!({"path": "tools.snapVectorToolsAndTransformsToPixelGrid", "value": false})).unwrap();
|
||||
app.sync_views();
|
||||
app.ui.views[0].zoom = 16.0;
|
||||
app.ui.tool = Tool::Star;
|
||||
set(&mut app.ui.tool_options);
|
||||
let before = app.session.active().unwrap().doc.layers.len();
|
||||
let none = Modifiers::NONE;
|
||||
tool_event(&mut app, ToolEvent::Down { x: START[0], y: START[1], pressure: 1.0 }, none);
|
||||
tool_event(&mut app, ToolEvent::Move { x: (START[0] + END[0]) / 2.0, y: (START[1] + END[1]) / 2.0, pressure: 1.0 }, none);
|
||||
tool_event(&mut app, ToolEvent::Move { x: END[0], y: END[1], pressure: 1.0 }, none);
|
||||
tool_event(&mut app, ToolEvent::Up { x: END[0], y: END[1] }, none);
|
||||
let st = app.session.active().unwrap();
|
||||
if st.doc.layers.len() == before {
|
||||
return None;
|
||||
}
|
||||
let id = st.active_layer.unwrap().0;
|
||||
let info = app.session.execute("shape.info", json!({"layer": id})).unwrap();
|
||||
assert_eq!(info["live"]["kind"].as_str(), Some("star"));
|
||||
serde_json::from_value(info["live"]["rect"].clone()).ok()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn on_by_default_a_star_snaps_and_off_it_keeps_the_drags_fractions() {
|
||||
assert!(ToolOptions::default().align_edges);
|
||||
let on = star_box(|_| {}).unwrap();
|
||||
assert!(on.iter().all(|v| v.fract() == 0.0), "on: {on:?}");
|
||||
let off = star_box(|o| o.align_edges = false).unwrap();
|
||||
assert!(off.iter().zip(FRACTIONAL).all(|(g, w)| (g - w).abs() < 1e-9), "off: {off:?}, want {FRACTIONAL:?}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn path_and_pixels_mode_keep_snapping_with_the_greyed_checkbox_off() {
|
||||
let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default());
|
||||
app.run("file.new", json!({"width": 200, "height": 100})).unwrap();
|
||||
app.ui.tool_options.align_edges = false;
|
||||
for mode in [VectorMode::Path, VectorMode::Pixels] {
|
||||
app.ui.tool_options.shape_mode = mode;
|
||||
assert!(super::snaps_to_pixels(&app, ShapeKind::Star), "{mode:?}");
|
||||
}
|
||||
app.ui.tool_options.shape_mode = VectorMode::Shape;
|
||||
assert!(!super::snaps_to_pixels(&app, ShapeKind::Star), "Shape mode with Align Edges off keeps fractions");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_bar_ends_with_align_edges_on_and_greyed_outside_shape_mode() {
|
||||
let mut h = harness(Tool::Star);
|
||||
let arrange = h.get_by_label("Path arrangement").rect();
|
||||
let node = h.get_by_label("Align Edges");
|
||||
assert!(node.rect().left() > arrange.right(), "{:?} after {arrange:?}", node.rect());
|
||||
assert!((node.rect().center().y - arrange.center().y).abs() < 4.0, "on the bar's row");
|
||||
assert!(!node.accesskit_node().is_disabled(), "enabled in Shape mode");
|
||||
assert_eq!(node.accesskit_node().toggled(), Some(Toggled::True));
|
||||
node.click();
|
||||
h.run_steps(3);
|
||||
assert!(!h.state().ui.tool_options.align_edges, "a click turns it off");
|
||||
|
||||
for (mode, greyed) in [(VectorMode::Path, true), (VectorMode::Pixels, true), (VectorMode::Shape, false)] {
|
||||
h.state_mut().ui.tool_options.shape_mode = mode;
|
||||
h.run_steps(4);
|
||||
assert_eq!(h.get_by_label("Align Edges").accesskit_node().is_disabled(), greyed, "{mode:?} mode");
|
||||
}
|
||||
}
|
||||
@@ -54,7 +54,7 @@ fn align_edges_is_on_by_default_and_saved() {
|
||||
|
||||
#[test]
|
||||
fn other_shape_tools_always_snap() {
|
||||
for tool in [Tool::EllipseShape, Tool::Star] {
|
||||
for tool in [Tool::EllipseShape] {
|
||||
let r = rect(|o| o.align_edges = false, tool, START, END).unwrap();
|
||||
assert!(r.iter().all(|v| v.fract() == 0.0), "{tool:?}: {r:?}");
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user