2bc41aeb8602
tests_constrain.rs
//! The W/H link (Constrain Width and Height Proportions) in the Transform panel, the Properties
//! panel's Transform section and the Control bar: one preference, passed on as `proportional`.
//! Alt+Enter in their fields applies the value to a copy of the selection, passed on as `copy`, and
//! Ctrl/Cmd+Enter in W or H keeps the proportions whether the link is on or off.
use egui::{Event, Key, Modifiers, PointerButton};
use serde_json::json;
use super::tests_appearance::{frame_raw, run};
use super::*;
/// What draws a set of size fields.
type Draw = fn(&mut VectorcraftApp, &mut Ui);
/// A selected 100 × 50 pt rectangle.
fn rect_app() -> VectorcraftApp {
let mut app = VectorcraftApp::new(vectorcraft_engine::Session::new(), Default::default());
run(&mut app, "file.new", json!({"width": 400, "height": 400}));
let id = run(&mut app, "shape.rectangle", json!({"x": 10, "y": 10, "width": 100, "height": 50}))["id"].clone();
run(&mut app, "select.set", json!({ "ids": [id] }));
app
}
fn size(app: &VectorcraftApp) -> (f64, f64) {
let st = app.session.active().unwrap();
let b = st.doc.bounds_of(&st.selection.objects, false).unwrap();
(b.width().round(), b.height().round())
}
/// How a field's editing ends: Enter, Enter in the frame the text is typed in, or a press away from
/// the field, with these modifiers held.
#[derive(Clone, Copy)]
enum End {
Enter(Modifiers),
TypedEnter(Modifiers),
ClickAway(Modifiers),
}
/// Type `value` into the field of what `draw` shows that shows `field` (the first one drawn), and
/// end its editing with `end`. Returns the context the frames ran in.
fn set_field(app: &mut VectorcraftApp, draw: Draw, field: &str, value: &str, end: End) -> egui::Context {
set_nth_field(app, draw, (field, 0), value, end)
}
/// [`set_field`] for the field drawn `nth` (from 0) among those that show `field`.
fn set_nth_field(app: &mut VectorcraftApp, draw: Draw, (field, nth): (&str, usize), value: &str, end: End) -> egui::Context {
let ctx = egui::Context::default();
crate::theme::install_fonts(&ctx);
let mut time = 0.0;
let mut frame = |app: &mut VectorcraftApp, events: Vec<Event>| {
time += 1.0;
frame_raw(&ctx, app, egui::RawInput { events, time: Some(time), ..Default::default() }, draw)
};
// A window shows nothing in its first frame, which only measures it.
frame(app, vec![]);
let shown = frame(app, vec![]);
let at = shown.iter().filter(|(s, _)| s == field).nth(nth).unwrap_or_else(|| panic!("no {field} field {nth} in {shown:?}")).1.center();
let button = |pos, pressed, modifiers| Event::PointerButton { pos, button: PointerButton::Primary, pressed, modifiers };
frame(app, vec![Event::PointerMoved(at), button(at, true, Modifiers::NONE), button(at, false, Modifiers::NONE)]);
// Focus selected the whole field: typing replaces it.
frame(app, vec![]);
let text = Event::Text(value.into());
if !matches!(end, End::TypedEnter(_)) {
frame(app, vec![text.clone()]);
}
// The modifiers go down first, as a keyboard reports them.
match end {
End::Enter(modifiers) => {
let enter = Event::Key { key: Key::Enter, physical_key: None, pressed: true, repeat: false, modifiers };
frame(app, vec![Event::ModifiersChanged(modifiers), enter]);
}
End::TypedEnter(modifiers) => {
let enter = Event::Key { key: Key::Enter, physical_key: None, pressed: true, repeat: false, modifiers };
frame(app, vec![text, Event::ModifiersChanged(modifiers), enter]);
}
End::ClickAway(modifiers) => {
let away = at + egui::vec2(0.0, 300.0);
let press = [button(away, true, modifiers), button(away, false, modifiers)];
frame(app, [Event::ModifiersChanged(modifiers), Event::PointerMoved(away)].into_iter().chain(press).collect());
}
}
ctx
}
/// Type `value` into the W field (the one showing "100 pt") of what `draw` shows, and press Enter.
fn set_width(app: &mut VectorcraftApp, draw: Draw, value: &str) {
set_field(app, draw, "100 pt", value, End::Enter(Modifiers::NONE));
}
/// The paths of the document, bottom to top: their size and angle (rounded), and whether each is
/// selected.
fn paths(app: &VectorcraftApp) -> Vec<((f64, f64), f64, bool)> {
let st = app.session.active().unwrap();
let mut out = vec![];
st.doc.walk(|n| {
if matches!(n.kind, vectorcraft_doc::NodeKind::Path { .. }) {
let b = st.doc.oriented_bounds(&[n.id], false).unwrap();
out.push(((b.rect.width().round(), b.rect.height().round()), b.angle.round(), st.selection.objects.contains(&n.id)));
}
});
out
}
#[test]
fn the_link_keeps_proportions_in_every_size_field() {
let surfaces: [(&str, Draw); 3] =
[("Transform panel", transform::show), ("Properties", properties::transform_section), ("Control bar", crate::chrome::control_bar)];
for (name, draw) in surfaces {
// Off: W alone.
let mut app = rect_app();
set_width(&mut app, draw, "200");
assert_eq!(size(&app), (200.0, 50.0), "{name}, unlinked");
// On: H follows.
let mut app = rect_app();
run(&mut app, "prefs.set", json!({"key": "constrainProportions", "value": true}));
set_width(&mut app, draw, "200");
assert_eq!(size(&app), (200.0, 100.0), "{name}, linked");
}
}
/// Alt+Enter (Option+Return) applies a field's value to a copy of the selection and selects the
/// copy, in one undo step; a press elsewhere with Alt held applies it to the selection.
#[test]
fn alt_enter_applies_the_value_to_a_copy_in_every_size_field() {
let surfaces: [(&str, Draw); 3] =
[("Transform panel", transform::show), ("Properties", properties::transform_section), ("Control bar", crate::chrome::control_bar)];
for (name, draw) in surfaces {
let mut app = rect_app();
set_field(&mut app, draw, "100 pt", "200", End::Enter(Modifiers::ALT));
assert_eq!(paths(&app), [((100.0, 50.0), 0.0, false), ((200.0, 50.0), 0.0, true)], "{name}");
run(&mut app, "edit.undo", json!({}));
assert_eq!(paths(&app), [((100.0, 50.0), 0.0, true)], "{name}, undone");
let mut app = rect_app();
set_field(&mut app, draw, "100 pt", "200", End::ClickAway(Modifiers::ALT));
assert_eq!(paths(&app), [((200.0, 50.0), 0.0, true)], "{name}, a press away");
}
}
/// Alt+Enter in Rotate turns a copy to the angle and leaves the original, in the Transform panel
/// and the Properties panel; in the Transform panel, Alt+Enter in Shear shears a copy.
#[test]
fn alt_enter_in_rotate_and_shear_transforms_a_copy() {
for (name, draw) in [("Transform panel", transform::show as Draw), ("Properties", properties::transform_section)] {
let mut app = rect_app();
// Rotate is the first field showing 0° (the Transform panel's Shear, after it, rests at 0° too).
set_field(&mut app, draw, "0°", "45", End::Enter(Modifiers::ALT));
assert_eq!(paths(&app), [((100.0, 50.0), 0.0, false), ((100.0, 50.0), 45.0, true)], "{name}");
}
let mut app = rect_app();
// The second field showing 0° is Shear.
set_nth_field(&mut app, transform::show, ("0°", 1), "30", End::Enter(Modifiers::ALT));
assert_eq!(paths(&app), [((100.0, 50.0), 0.0, false), ((129.0, 50.0), 0.0, true)]);
}
/// With anchors selected with Direct Selection, the fields show those anchors' box, and Alt+Enter
/// applies the value to them as Enter does.
#[test]
fn alt_enter_moves_selected_anchors_without_a_copy() {
let mut app = rect_app();
let id = app.session.active().unwrap().selection.objects[0].0;
run(&mut app, "select.anchors", json!({"id": id, "anchors": [[0, 1]]}));
let before = paths(&app).len();
// The anchor's X (the top-right corner of the rectangle at 10, 10).
set_field(&mut app, transform::show, "110 pt", "150", End::Enter(Modifiers::ALT));
assert_eq!(paths(&app).len(), before);
let st = app.session.active().unwrap();
assert_eq!(st.doc.bounds_of(&st.selection.objects, false).unwrap().width().round(), 140.0);
}
/// Ctrl/Cmd+Enter (Command+Return) in W or H applies the value and keeps the proportions with the
/// link off, and the field's editing ends as with Enter.
#[test]
fn ctrl_cmd_enter_in_a_size_field_scales_proportionally() {
let surfaces: [(&str, Draw); 3] =
[("Transform panel", transform::show), ("Properties", properties::transform_section), ("Control bar", crate::chrome::control_bar)];
for (name, draw) in surfaces {
for (field, value) in [("100 pt", "200"), ("50 pt", "100")] {
let mut app = rect_app();
let ctx = set_field(&mut app, draw, field, value, End::Enter(Modifiers::COMMAND));
assert_eq!(size(&app), (200.0, 100.0), "{name}, {field}");
assert_eq!(ctx.memory(|m| m.focused()), None, "{name}, {field}: the field's editing ended");
}
// The text typed in the frame of the Ctrl/Cmd+Enter is taken too.
let mut app = rect_app();
set_field(&mut app, draw, "100 pt", "300", End::TypedEnter(Modifiers::COMMAND));
assert_eq!(size(&app), (300.0, 150.0), "{name}, typed with the Ctrl/Cmd+Enter");
}
}
/// Ctrl/Cmd+Enter in X or Y does nothing: the field keeps the focus and the selection stays.
#[test]
fn ctrl_cmd_enter_in_a_position_field_keeps_editing() {
for (name, draw) in [("Transform panel", transform::show as Draw), ("Properties", properties::transform_section)] {
let mut app = rect_app();
// X of the rectangle's center.
let ctx = set_field(&mut app, draw, "60 pt", "200", End::Enter(Modifiers::COMMAND));
assert_eq!(paths(&app), [((100.0, 50.0), 0.0, true)], "{name}");
let st = app.session.active().unwrap();
assert_eq!(st.doc.bounds_of(&st.selection.objects, false).unwrap().x0, 10.0, "{name}");
assert!(ctx.memory(|m| m.focused().is_some()), "{name}: the field keeps the focus");
}
}
/// With the Artboard tool, Ctrl/Cmd+Enter in W keeps the active artboard's proportions.
#[test]
fn ctrl_cmd_enter_scales_the_artboard_proportionally() {
let board = |end| {
let mut app = VectorcraftApp::new(vectorcraft_engine::Session::new(), Default::default());
run(&mut app, "file.new", json!({"width": 200, "height": 150}));
app.run("tool.select", json!({"tool": "artboard"})).unwrap();
set_field(&mut app, transform::show, "200 pt", "400", end);
let r = app.session.active().unwrap().doc.artboards[0].rect;
(r.width().round(), r.height().round())
};
assert_eq!(board(End::Enter(Modifiers::NONE)), (400.0, 150.0));
assert_eq!(board(End::Enter(Modifiers::COMMAND)), (400.0, 300.0));
}
/// A percentage typed alone in W or H (`25%`) is a share of the size the field shows, as in
/// Illustrator: with the link off only that side changes, with it on the other side follows, and
/// Ctrl/Cmd+Enter keeps the proportions as with a length.
#[test]
fn a_percentage_in_a_size_field_scales_the_size_it_shows() {
let surfaces: [(&str, Draw); 3] =
[("Transform panel", transform::show), ("Properties", properties::transform_section), ("Control bar", crate::chrome::control_bar)];
for (name, draw) in surfaces {
let mut app = rect_app();
set_field(&mut app, draw, "100 pt", "25%", End::Enter(Modifiers::NONE));
assert_eq!(size(&app), (25.0, 50.0), "{name}, W");
let mut app = rect_app();
set_field(&mut app, draw, "50 pt", "150 %", End::Enter(Modifiers::NONE));
assert_eq!(size(&app), (100.0, 75.0), "{name}, H");
let mut app = rect_app();
set_field(&mut app, draw, "100 pt", "200%", End::Enter(Modifiers::COMMAND));
assert_eq!(size(&app), (200.0, 100.0), "{name}, Ctrl/Cmd+Enter");
let mut app = rect_app();
run(&mut app, "prefs.set", json!({"key": "constrainProportions", "value": true}));
set_field(&mut app, draw, "50 pt", "300%", End::Enter(Modifiers::NONE));
assert_eq!(size(&app), (300.0, 150.0), "{name}, linked");
}
}
/// A percentage with a thousands comma in W or H (`1,000%`) is refused and leaves the size as it
/// was, rather than read as `1.000%`; a decimal comma (`250,5%`) still reads as a share.
#[test]
fn a_thousands_comma_in_a_percentage_is_refused_in_a_size_field() {
let surfaces: [(&str, Draw); 3] =
[("Transform panel", transform::show), ("Properties", properties::transform_section), ("Control bar", crate::chrome::control_bar)];
for (name, draw) in surfaces {
let mut app = rect_app();
set_field(&mut app, draw, "100 pt", "1,000%", End::Enter(Modifiers::NONE));
assert_eq!(size(&app), (100.0, 50.0), "{name}, 1,000%");
let mut app = rect_app();
set_field(&mut app, draw, "100 pt", "250,5%", End::Enter(Modifiers::NONE));
assert_eq!(size(&app), (251.0, 50.0), "{name}, 250,5%");
}
}
/// Arithmetic with a decimal comma works in W (#5684), also after a value shown with a point; a
/// number with both separators is refused and leaves the size as it was.
#[test]
fn arithmetic_with_a_decimal_comma_works_in_a_size_field() {
let surfaces: [(&str, Draw); 3] =
[("Transform panel", transform::show), ("Properties", properties::transform_section), ("Control bar", crate::chrome::control_bar)];
for (name, draw) in surfaces {
for (text, want) in [("100,5-20,5", 80.0), ("90-0,5*20", 80.0), ("10,5*2", 21.0), ("100.5 pt-20,5", 80.0), ("1.234,5", 100.0)] {
let mut app = rect_app();
set_field(&mut app, draw, "100 pt", text, End::Enter(Modifiers::NONE));
assert_eq!(size(&app), (want, 50.0), "{name}, {text}");
}
}
}
/// A length field of the New Document dialog keeps the focus on Ctrl/Cmd+Enter, and the dialog
/// creates no document.
#[test]
fn ctrl_cmd_enter_in_a_dialog_field_keeps_editing() {
let mut app = VectorcraftApp::new(vectorcraft_engine::Session::new(), Default::default());
app.run("file.newDialog", json!({})).unwrap();
let d = app.ui.dialog.as_ref().unwrap();
let unit = d.fields.get("units").and_then(|u| u.as_str()).and_then(vectorcraft_doc::Unit::named).unwrap_or_default();
let shown = unit.format(d.fields["width"].as_f64().unwrap());
let dialogs: Draw = |app, ui| crate::dialogs::show(app, ui.ctx());
let ctx = set_field(&mut app, dialogs, &shown, "300", End::Enter(Modifiers::COMMAND));
assert!(app.ui.dialog.is_some() && app.session.documents().is_empty());
assert!(ctx.memory(|m| m.focused().is_some()), "the field keeps the focus");
}
#[test]
fn the_link_is_a_preference_agents_can_read_and_set() {
let mut app = rect_app();
assert_eq!(run(&mut app, "prefs.get", json!({"key": "constrainProportions"})), json!(false));
run(&mut app, "prefs.set", json!({"key": "constrainProportions", "value": true}));
assert!(app.session.prefs.constrain_proportions);
// What the fields send with the link on.
run(&mut app, "object.setBounds", json!({"height": 100, "reference": 4, "proportional": true}));
assert_eq!(size(&app), (200.0, 100.0));
}
/// One Tab moves from a field to the next field: a field's label (its scrub zone), spinner and
/// preset chevron are not tab stops of their own, and the W/H link between W and H, which is
/// still focusable, comes after the fields. In the Transform panel and the Properties panel the
/// fields run X, W, Y, H (a grid, two to a row); in the Control bar X, Y, W, H.
#[test]
fn one_tab_moves_from_a_field_to_the_next() {
// From X (the selection's centre, 60 pt) the next field is W in the panels and Y in the bar.
for (name, draw, next) in [
("Transform panel", transform::show as Draw, "width"),
("Properties", properties::transform_section as Draw, "width"),
("Control bar", crate::chrome::control_bar as Draw, "y"),
] {
let mut app = rect_app();
tab_then_type(&mut app, draw, "60 pt", "200");
let st = app.session.active().unwrap();
let b = st.doc.bounds_of(&st.selection.objects, false).unwrap();
match next {
"width" => assert_eq!(size(&app), (200.0, 50.0), "{name}: Tab went from X to W"),
_ => assert_eq!((b.width().round(), b.y0.round()), (100.0, 175.0), "{name}: Tab went from X to Y"),
}
}
// In the Control bar the W/H link sits between W and H: Tab goes from W straight to H.
let mut app = rect_app();
tab_then_type(&mut app, crate::chrome::control_bar, "100 pt", "200");
assert_eq!(size(&app), (100.0, 200.0), "Control bar: Tab went from W to H");
// The Artboard tool's Control bar puts the link after its fields too: Tab goes from W to H.
let mut app = rect_app();
app.select_tool("artboard");
tab_then_type(&mut app, crate::chrome::control_bar, "400 pt", "200");
let r = app.session.doc().unwrap().doc.artboards[0].rect;
assert_eq!((r.width().round(), r.height().round()), (400.0, 200.0), "Artboard Control bar: Tab went from W to H");
// The Transform panel's Rotate and Shear fields each have a spinner and presets: Tab goes from
// Rotate straight to Shear, past Rotate's preset chevron and Shear's spinner.
let mut app = rect_app();
tab_then_type(&mut app, transform::show, "0°", "30");
let shear = app.session.active().map(|st| st.doc.bounds_of(&st.selection.objects, false).map(|b| (b.width().round(), b.height().round())));
assert_eq!(shear, Some(Some((129.0, 50.0))), "Transform panel: Tab went from Rotate to Shear");
// Shift+Tab goes back one field, one press too: from W to X in the panels.
for (name, draw) in [("Transform panel", transform::show as Draw), ("Properties", properties::transform_section as Draw)] {
let mut app = rect_app();
shift_tab_then_type(&mut app, draw, "100 pt", "200");
let st = app.session.active().unwrap();
let b = st.doc.bounds_of(&st.selection.objects, false).unwrap();
assert_eq!((b.width().round(), b.x0.round()), (100.0, 150.0), "{name}: Shift+Tab went from W to X");
}
}
/// A click on the Control bar's W/H link toggles Constrain Width and Height Proportions, and the
/// link comes after the fields in the tab order, so Tab still moves from W to H.
#[test]
fn the_control_bar_link_toggles_by_click() {
let mut app = rect_app();
assert!(!app.session.prefs.constrain_proportions);
let ctx = egui::Context::default();
crate::theme::install_fonts(&ctx);
let mut time = 0.0;
let mut frame = |app: &mut VectorcraftApp, events: Vec<Event>| {
time += 1.0;
frame_raw(&ctx, app, egui::RawInput { events, time: Some(time), ..Default::default() }, crate::chrome::control_bar)
};
frame(&mut app, vec![]);
let shown = frame(&mut app, vec![]);
let at = |text: &str| shown.iter().find(|(t, _)| t == text).map(|(_, r)| *r).unwrap_or_else(|| panic!("no `{text}` in {shown:?}"));
let (w, h) = (at("100 pt"), at("50 pt"));
// The link's button fills the gap between the W and H fields.
let link = egui::pos2((w.right() + h.left()) / 2.0, w.center().y);
let button = |pressed| Event::PointerButton { pos: link, button: PointerButton::Primary, pressed, modifiers: Modifiers::NONE };
for events in [vec![Event::PointerMoved(link), button(true), button(false)], vec![]] {
frame(&mut app, events);
}
assert!(app.session.prefs.constrain_proportions, "the click toggled the link");
// Tab from H finds the link, and Space or Enter toggles it, as a dialog's button takes them.
let h = at("50 pt").center();
let button = |pos, pressed| Event::PointerButton { pos, button: PointerButton::Primary, pressed, modifiers: Modifiers::NONE };
let tab = Event::Key { key: Key::Tab, physical_key: None, pressed: true, repeat: false, modifiers: Modifiers::NONE };
let enter = Event::Key { key: Key::Enter, physical_key: None, pressed: true, repeat: false, modifiers: Modifiers::NONE };
for events in [vec![Event::PointerMoved(h), button(h, true), button(h, false)], vec![], vec![tab], vec![], vec![enter], vec![]] {
frame(&mut app, events);
}
assert!(!app.session.prefs.constrain_proportions, "Enter on the focused link toggled it");
assert_eq!(size(&app), (100.0, 50.0), "and the fields kept their values");
}
/// Type `value` into the field of what `draw` shows that shows `field` after one Tab, and press
/// Enter. The field the Tab moved to takes the value; the one pressed keeps its own.
fn tab_then_type(app: &mut VectorcraftApp, draw: Draw, field: &str, value: &str) {
tab_then_type_with(app, draw, field, value, Modifiers::NONE);
}
/// [`tab_then_type`] with Shift+Tab, which goes back one field.
fn shift_tab_then_type(app: &mut VectorcraftApp, draw: Draw, field: &str, value: &str) {
tab_then_type_with(app, draw, field, value, Modifiers::SHIFT);
}
/// [`tab_then_type`], the Tab key held with `modifiers`.
fn tab_then_type_with(app: &mut VectorcraftApp, draw: Draw, field: &str, value: &str, modifiers: Modifiers) {
let ctx = egui::Context::default();
crate::theme::install_fonts(&ctx);
let mut time = 0.0;
let mut frame = |app: &mut VectorcraftApp, events: Vec<Event>| {
time += 1.0;
frame_raw(&ctx, app, egui::RawInput { events, time: Some(time), ..Default::default() }, draw)
};
frame(app, vec![]);
let shown = frame(app, vec![]);
let at = shown.iter().filter(|(s, _)| s == field).map(|(_, r)| r.center()).next().unwrap_or_else(|| panic!("no {field} field in {shown:?}"));
let button = |pos, pressed| Event::PointerButton { pos, button: PointerButton::Primary, pressed, modifiers: Modifiers::NONE };
let tab = Event::Key { key: Key::Tab, physical_key: None, pressed: true, repeat: false, modifiers };
for events in [vec![Event::PointerMoved(at), button(at, true), button(at, false)], vec![], vec![tab], vec![]] {
frame(app, events);
}
let enter = Event::Key { key: Key::Enter, physical_key: None, pressed: true, repeat: false, modifiers: Modifiers::NONE };
for events in [vec![Event::Text(value.into())], vec![enter], vec![]] {
frame(app, events);
}
}
No file is open. Choose one in the tree, or press Ctrl+O (⌘O on a Mac) to find one by name.
Syntax highlighting could not load. The unmodified read-only text remains available above.