mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-11-29 09:46:19 +01:00
866 lines
47 KiB
Rust
866 lines
47 KiB
Rust
//! The canvas context menu: right-click selects the object under the pointer and lists what
|
||
//! applies to the selection.
|
||
|
||
use egui::{Event, PointerButton, Pos2, Rect, pos2, vec2};
|
||
use serde_json::json;
|
||
use vectorcraft_doc::{NodeId, NodeKind};
|
||
use vectorcraft_engine::Session;
|
||
use vectorcraft_geom::Point;
|
||
|
||
use crate::canvas::Xf;
|
||
use crate::menus::{self, Item};
|
||
use crate::{VectorcraftApp, canvas};
|
||
|
||
fn app() -> VectorcraftApp {
|
||
let mut app = VectorcraftApp::new(Session::new(), Default::default());
|
||
app.run("file.new", json!({"width": 400, "height": 300})).unwrap();
|
||
app
|
||
}
|
||
|
||
fn rect(app: &mut VectorcraftApp, x: f64, y: f64) -> NodeId {
|
||
NodeId(app.run("shape.rectangle", json!({"x": x, "y": y, "width": 100, "height": 100})).unwrap()["id"].as_u64().unwrap())
|
||
}
|
||
|
||
/// One headless frame of the canvas on an 800 × 600 window → the text painted, with its rect.
|
||
fn frame(app: &mut VectorcraftApp, ctx: &egui::Context, events: Vec<Event>) -> Vec<(String, Rect)> {
|
||
let raw = egui::RawInput { screen_rect: Some(Rect::from_min_size(Pos2::ZERO, vec2(800.0, 600.0))), events, ..Default::default() };
|
||
let mut out = ctx.run_ui(raw, |ui| canvas::show(app, ui));
|
||
out.textures_delta.clear();
|
||
fn walk(s: &egui::Shape, v: &mut Vec<(String, Rect)>) {
|
||
match s {
|
||
egui::Shape::Text(t) => v.push((t.galley.text().to_string(), Rect::from_min_size(t.pos, t.galley.size()))),
|
||
egui::Shape::Vec(s) => s.iter().for_each(|s| walk(s, v)),
|
||
_ => {}
|
||
}
|
||
}
|
||
let mut v = vec![];
|
||
out.shapes.iter().for_each(|c| walk(&c.shape, &mut v));
|
||
v
|
||
}
|
||
|
||
fn press(pos: Pos2, button: PointerButton, pressed: bool) -> Event {
|
||
Event::PointerButton { pos, button, pressed, modifiers: Default::default() }
|
||
}
|
||
|
||
/// Click `button` at `pos` (move, press, release, then a frame to show the result).
|
||
fn click(app: &mut VectorcraftApp, ctx: &egui::Context, pos: Pos2, button: PointerButton) -> Vec<(String, Rect)> {
|
||
frame(app, ctx, vec![Event::PointerMoved(pos)]);
|
||
frame(app, ctx, vec![press(pos, button, true)]);
|
||
frame(app, ctx, vec![press(pos, button, false)]);
|
||
frame(app, ctx, vec![])
|
||
}
|
||
|
||
/// Where document point `p` is on screen (after a first frame laid the canvas out).
|
||
fn screen(app: &VectorcraftApp, x: f64, y: f64) -> Pos2 {
|
||
Xf::new(app.canvas_rect.unwrap(), app.view().unwrap()).to_screen(Point::new(x, y))
|
||
}
|
||
|
||
fn has(texts: &[(String, Rect)], label: &str) -> bool {
|
||
texts.iter().any(|(t, _)| t.trim_start_matches(['✓', ' ']) == label)
|
||
}
|
||
|
||
fn at(texts: &[(String, Rect)], label: &str) -> Pos2 {
|
||
texts.iter().find(|(t, _)| t == label).map(|(_, r)| r.center()).unwrap_or_else(|| panic!("no `{label}` in {texts:?}"))
|
||
}
|
||
|
||
#[test]
|
||
fn right_click_selects_the_object_and_lists_its_commands() {
|
||
let mut app = app();
|
||
let id = rect(&mut app, 50.0, 50.0);
|
||
app.run("select.none", json!({})).unwrap();
|
||
let ctx = egui::Context::default();
|
||
assert!(!has(&frame(&mut app, &ctx, vec![]), "Arrange"));
|
||
let p = screen(&app, 100.0, 100.0);
|
||
let texts = click(&mut app, &ctx, p, PointerButton::Secondary);
|
||
assert_eq!(app.session.active().unwrap().selection.objects, [id], "the object right-clicked is selected");
|
||
for label in ["Undo Rectangle", "Cut", "Copy", "Make Guides", "Transform", "Arrange", "Select", "Export Selection…"] {
|
||
assert!(has(&texts, label), "{label} in {texts:?}");
|
||
}
|
||
// Nothing that needs several objects, a group or nothing selected.
|
||
for label in ["Group", "Ungroup", "Make Clipping Mask", "Release Compound Path", "Select All", "Zoom In"] {
|
||
assert!(!has(&texts, label), "{label} in {texts:?}");
|
||
}
|
||
// Choosing an item runs its command and closes the menu.
|
||
let texts = click(&mut app, &ctx, at(&texts, "Make Guides"), PointerButton::Primary);
|
||
let n = app.session.active().unwrap().doc.node(id).cloned().unwrap();
|
||
assert!(matches!(n.kind, NodeKind::Path { guide: true, .. }));
|
||
assert!(!has(&texts, "Arrange"));
|
||
}
|
||
|
||
#[test]
|
||
fn right_click_on_empty_canvas_offers_the_view_and_a_dismissing_click_does_nothing() {
|
||
let mut app = app();
|
||
app.select_tool("rectangle");
|
||
let ctx = egui::Context::default();
|
||
frame(&mut app, &ctx, vec![]);
|
||
let p = screen(&app, 300.0, 250.0);
|
||
let texts = click(&mut app, &ctx, p, PointerButton::Secondary);
|
||
for label in ["Zoom In", "Zoom Out", "Fit Artboard in Window", "Show Rulers", "Select All"] {
|
||
assert!(has(&texts, label), "{label} in {texts:?}");
|
||
}
|
||
assert!(!has(&texts, "Cut") && !has(&texts, "Arrange"));
|
||
// A click on the canvas away from the menu only closes it: the Rectangle tool draws nothing.
|
||
let away = pos2(100.0, 500.0);
|
||
frame(&mut app, &ctx, vec![Event::PointerMoved(away)]);
|
||
frame(&mut app, &ctx, vec![press(away, PointerButton::Primary, true)]);
|
||
frame(&mut app, &ctx, vec![Event::PointerMoved(away + vec2(40.0, 30.0))]);
|
||
frame(&mut app, &ctx, vec![press(away + vec2(40.0, 30.0), PointerButton::Primary, false)]);
|
||
let texts = frame(&mut app, &ctx, vec![]);
|
||
assert!(!has(&texts, "Zoom In"));
|
||
assert_eq!(app.session.active().unwrap().doc.art_bounds(), None);
|
||
}
|
||
|
||
/// A right-click over a locked object adds an Unlock submenu that lists the object by its name.
|
||
/// Choosing the name unlocks the object in one undo step; once the menu is closed, the menu lists
|
||
/// no Unlock items.
|
||
#[test]
|
||
fn right_click_on_a_locked_object_offers_unlock_by_its_name() {
|
||
let mut app = app();
|
||
let id = rect(&mut app, 50.0, 50.0);
|
||
app.run("layer.setProps", json!({"ids": [id.0], "name": "Sky", "locked": true})).unwrap();
|
||
let steps = app.session.active().unwrap().history.undo.len();
|
||
let ctx = egui::Context::default();
|
||
frame(&mut app, &ctx, vec![]);
|
||
let p = screen(&app, 100.0, 100.0);
|
||
let texts = click(&mut app, &ctx, p, PointerButton::Secondary);
|
||
assert!(has(&texts, "Unlock"), "{texts:?}");
|
||
// Agents see the item under the Unlock path, by the object's name.
|
||
let entries = menus::context_entries(&app);
|
||
let item = entries.iter().find(|e| e.path == ["Unlock"]).unwrap();
|
||
assert_eq!((item.label.as_str(), item.command.as_deref()), ("Sky", Some("layer.setProps")));
|
||
assert_eq!(item.params, json!({"ids": [id.0], "locked": false}));
|
||
// Hovering Unlock opens the submenu.
|
||
frame(&mut app, &ctx, vec![Event::PointerMoved(at(&texts, "Unlock"))]);
|
||
let texts = frame(&mut app, &ctx, vec![]);
|
||
let texts = click(&mut app, &ctx, at(&texts, "Sky"), PointerButton::Primary);
|
||
let st = app.session.active().unwrap();
|
||
assert!(!st.doc.node(id).unwrap().locked, "the object is unlocked");
|
||
assert_eq!(st.history.undo.len(), steps + 1, "in one undo step");
|
||
assert!(!has(&texts, "Unlock"), "the menu closed: {texts:?}");
|
||
// Locked again, it is not listed until the next right-click over it.
|
||
app.run("layer.setProps", json!({"ids": [id.0], "locked": true})).unwrap();
|
||
assert!(menus::context_entries(&app).iter().all(|e| e.path.first().map(String::as_str) != Some("Unlock")));
|
||
}
|
||
|
||
/// #528: right-clicking a ruler offers the document units, the current one checked, and choosing one
|
||
/// swaps them — the same effect as Preferences ▸ Units ▸ General (`document.setUnits`).
|
||
#[test]
|
||
fn right_clicking_a_ruler_swaps_the_document_units() {
|
||
use vectorcraft_doc::Unit;
|
||
let mut app = app();
|
||
app.ui.view.rulers = true;
|
||
let ctx = egui::Context::default();
|
||
frame(&mut app, &ctx, vec![]);
|
||
assert_eq!(app.session.general_unit(), Unit::Points, "a new document starts in the prefs unit");
|
||
// Right-click on the top ruler (the band along the top, past the origin box).
|
||
let texts = click(&mut app, &ctx, pos2(400.0, 8.0), PointerButton::Secondary);
|
||
for label in ["Points", "Picas", "Inches", "Millimeters", "Feet & Inches", "Pixels"] {
|
||
assert!(has(&texts, label), "{label} in {texts:?}");
|
||
}
|
||
assert!(texts.iter().any(|(t, _)| t == "✓ Points"), "the current unit is checked: {texts:?}");
|
||
// Choosing a unit runs document.setUnits and closes the menu.
|
||
let mm = texts.iter().find(|(t, _)| t.trim_start_matches(['✓', ' ']) == "Millimeters").map(|(_, r)| r.center()).expect("Millimeters");
|
||
let texts = click(&mut app, &ctx, mm, PointerButton::Primary);
|
||
assert_eq!(app.session.general_unit(), Unit::Millimeters, "the ruler's unit changed");
|
||
assert!(!has(&texts, "Inches"), "the menu closed");
|
||
// The left ruler offers the same menu, now checking the new unit.
|
||
let texts = click(&mut app, &ctx, pos2(8.0, 300.0), PointerButton::Secondary);
|
||
assert!(texts.iter().any(|(t, _)| t == "✓ Millimeters"), "the left ruler reflects the current unit: {texts:?}");
|
||
}
|
||
|
||
/// Cmd+Alt+Shift+U is Switch Units by default (Ctrl+Alt+Shift+U on Windows and Linux,
|
||
/// Option+Shift+Command+U on macOS): pressed, the document goes to the unit after its own in the
|
||
/// ruler's menu, and Smart Guides (Cmd+U) stay as they are. No other default has the key.
|
||
#[test]
|
||
fn cmd_alt_shift_u_switches_to_the_next_unit() {
|
||
use crate::shortcut_editor as ed;
|
||
use vectorcraft_doc::Unit;
|
||
assert_eq!(ed::default_command_shortcut("document.switchUnits"), Some("Cmd+Alt+Shift+U"));
|
||
assert_eq!(ed::command_for_key("Cmd+Alt+Shift+U"), Some("document.switchUnits"));
|
||
let shown = menus::pretty_shortcut("Cmd+Alt+Shift+U");
|
||
assert_eq!(shown, if cfg!(target_os = "macos") { "⌥⇧⌘U" } else { "Ctrl+Alt+Shift+U" });
|
||
let mut app = app();
|
||
let listed = app.run("shortcuts.list", json!({"query": "document.switchUnits"})).unwrap();
|
||
let rows: Vec<_> = listed.as_array().expect("rows").iter().map(|r| (r["label"].clone(), r["group"].clone(), r["default"].clone())).collect();
|
||
assert_eq!(rows, [(json!("Switch Units"), json!("Document"), json!("Cmd+Alt+Shift+U"))], "Keyboard Shortcuts lists it under Document");
|
||
assert_eq!(app.run("shortcuts.conflicts", json!({})).unwrap(), json!([]), "no other default has the key");
|
||
fn press(app: &mut VectorcraftApp) {
|
||
let ctx = egui::Context::default();
|
||
let modifiers = egui::Modifiers::COMMAND | egui::Modifiers::ALT | egui::Modifiers::SHIFT;
|
||
let ev = Event::Key { key: egui::Key::U, physical_key: None, pressed: true, repeat: false, modifiers };
|
||
let mut out = ctx.run_ui(egui::RawInput { events: vec![ev], ..Default::default() }, |ui| crate::shortcuts::handle(app, ui.ctx()));
|
||
out.textures_delta.clear();
|
||
}
|
||
let guides = app.ui.view.smart_guides;
|
||
press(&mut app);
|
||
assert_eq!(app.session.general_unit(), Unit::Picas, "Points → Picas");
|
||
assert_eq!(app.ui.view.smart_guides, guides, "Smart Guides stay as they are");
|
||
app.run("document.setUnits", json!({"units": "Feet"})).unwrap();
|
||
press(&mut app);
|
||
assert_eq!(app.session.general_unit(), Unit::Points, "Feet → Points");
|
||
}
|
||
|
||
fn labels(items: &[Item]) -> Vec<&'static str> {
|
||
items
|
||
.iter()
|
||
.flat_map(|it| match it {
|
||
// A spelling suggestion shows the word it puts in, not its label.
|
||
Item::Cmd(_, "text.editRange", _) => vec![],
|
||
Item::Cmd(l, ..) => vec![*l],
|
||
Item::Header(l) => vec![*l],
|
||
Item::Sub(l, ch) => std::iter::once(*l).chain(labels(ch)).collect(),
|
||
_ => vec![],
|
||
})
|
||
.collect()
|
||
}
|
||
|
||
#[test]
|
||
fn the_menu_follows_the_selection() {
|
||
let mut app = app();
|
||
let (a, b) = (rect(&mut app, 0.0, 0.0), rect(&mut app, 50.0, 50.0));
|
||
app.run("select.set", json!({"ids": [a.0, b.0]})).unwrap();
|
||
let l = labels(&menus::context_items(&app));
|
||
for label in ["Group", "Join", "Make Clipping Mask", "Make Compound Path", "Objects on Path", "Bring to Front", "Move…"] {
|
||
assert!(l.contains(&label), "{label} in {l:?}");
|
||
}
|
||
assert!(!l.contains(&"Ungroup") && !l.contains(&"Isolate Selected Group"));
|
||
// Objects on Path chooses its tool, which attaches the selection to the path clicked next.
|
||
menus::invoke(&mut app, "object.objectsOnPath.attach", json!({}));
|
||
assert_eq!(app.session.tool_id(), "objectsOnPath");
|
||
app.select_tool("selection");
|
||
app.run("object.group", json!({})).unwrap();
|
||
let l = labels(&menus::context_items(&app));
|
||
assert!(l.contains(&"Ungroup") && l.contains(&"Isolate Selected Group"));
|
||
assert!(!l.contains(&"Group") && !l.contains(&"Join"));
|
||
// Isolated: a way out.
|
||
menus::invoke(&mut app, "object.isolate", json!({}));
|
||
assert!(labels(&menus::context_items(&app)).contains(&"Exit Isolation Mode"));
|
||
// Agents see the same menu, with submenus as paths.
|
||
let entries = menus::context_entries(&app);
|
||
assert!(entries.iter().any(|e| e.command.as_deref() == Some("object.exitIsolation") && e.enabled));
|
||
app.run("select.set", json!({"ids": [a.0]})).unwrap();
|
||
let entries = menus::context_entries(&app);
|
||
let front = entries.iter().find(|e| e.command.as_deref() == Some("object.arrange.bringToFront")).unwrap();
|
||
assert_eq!(front.path, ["Arrange"]);
|
||
// No separator leads, trails or doubles up.
|
||
let items = menus::context_items(&app);
|
||
assert!(!matches!(items.first(), Some(Item::Sep)) && !matches!(items.last(), Some(Item::Sep)));
|
||
assert!(items.windows(2).all(|w| !matches!(w, [Item::Sep, Item::Sep])));
|
||
}
|
||
|
||
/// The labels of the context menu's isolation items.
|
||
fn isolate_labels(app: &VectorcraftApp) -> Vec<&'static str> {
|
||
labels(&menus::context_items(app)).into_iter().filter(|l| l.starts_with("Isolate Selected")).collect()
|
||
}
|
||
|
||
/// One selected path has Isolate Selected Path, which isolates that path.
|
||
#[test]
|
||
fn single_path_context_item_enters_isolation() {
|
||
let mut app = app();
|
||
let a = rect(&mut app, 0.0, 0.0);
|
||
app.run("select.set", json!({"ids": [a.0]})).unwrap();
|
||
assert_eq!(isolate_labels(&app), ["Isolate Selected Path"]);
|
||
let entries = menus::context_entries(&app);
|
||
assert!(entries.iter().any(|e| e.command.as_deref() == Some("object.isolate") && e.enabled), "agents see the item");
|
||
menus::invoke(&mut app, "object.isolate", json!({}));
|
||
assert_eq!(app.session.active().unwrap().isolation, Some(a), "the path is isolated");
|
||
assert!(labels(&menus::context_items(&app)).contains(&"Exit Isolation Mode"));
|
||
}
|
||
|
||
/// The isolation item names the kind of the one selected object: a group or a compound path is a
|
||
/// group, a path (a group's member too) is a path, and no object or two objects have neither.
|
||
#[test]
|
||
fn isolation_context_item_follows_selected_kind() {
|
||
let mut app = app();
|
||
let (a, b) = (rect(&mut app, 0.0, 0.0), rect(&mut app, 50.0, 50.0));
|
||
app.run("select.none", json!({})).unwrap();
|
||
assert!(isolate_labels(&app).is_empty(), "nothing selected");
|
||
app.run("select.set", json!({"ids": [a.0, b.0]})).unwrap();
|
||
assert!(isolate_labels(&app).is_empty(), "two paths");
|
||
app.run("object.compoundPath.make", json!({})).unwrap();
|
||
assert_eq!(isolate_labels(&app), ["Isolate Selected Group"], "a compound path selected whole");
|
||
let compound = app.session.active().unwrap().selection.objects.clone();
|
||
app.run("select.set", json!({"ids": [a.0]})).unwrap();
|
||
assert_eq!(app.session.active().unwrap().selection.objects, [a], "a compound path's member selected on its own");
|
||
assert_eq!(isolate_labels(&app), ["Isolate Selected Path"], "a path in a compound path");
|
||
app.run("select.set", json!({"ids": [compound[0].0]})).unwrap();
|
||
app.run("object.compoundPath.release", json!({})).unwrap();
|
||
app.run("select.set", json!({"ids": [a.0, b.0]})).unwrap();
|
||
app.run("object.group", json!({})).unwrap();
|
||
assert_eq!(isolate_labels(&app), ["Isolate Selected Group"], "a group");
|
||
app.run("select.set", json!({"ids": [a.0]})).unwrap();
|
||
assert_eq!(app.session.active().unwrap().selection.objects, [a], "a member selected on its own");
|
||
assert_eq!(isolate_labels(&app), ["Isolate Selected Path"], "a path in a group");
|
||
menus::invoke(&mut app, "object.isolate", json!({}));
|
||
assert_eq!(app.session.active().unwrap().isolation, Some(a), "the member is isolated");
|
||
}
|
||
|
||
/// Neither isolation item shows for an object `object.isolate` refuses, which a selection set by a
|
||
/// command can still hold: a hidden or locked object, a path in a locked group, and a guide while
|
||
/// guides are locked.
|
||
#[test]
|
||
fn isolation_context_item_skips_objects_that_cannot_be_isolated() {
|
||
let mut app = app();
|
||
let (a, b) = (rect(&mut app, 0.0, 0.0), rect(&mut app, 50.0, 50.0));
|
||
let set = |app: &mut VectorcraftApp, id: NodeId, f: fn(&mut vectorcraft_doc::Node)| {
|
||
let st = app.session.doc_mut().unwrap();
|
||
f(std::sync::Arc::make_mut(&mut st.doc).node_mut(id).unwrap());
|
||
st.selection.set([id]);
|
||
};
|
||
set(&mut app, a, |n| n.locked = true);
|
||
assert!(isolate_labels(&app).is_empty(), "a locked path");
|
||
set(&mut app, a, |n| {
|
||
n.locked = false;
|
||
n.visible = false;
|
||
});
|
||
assert!(isolate_labels(&app).is_empty(), "a hidden path");
|
||
set(&mut app, a, |n| n.visible = true);
|
||
assert_eq!(isolate_labels(&app), ["Isolate Selected Path"]);
|
||
app.run("select.set", json!({"ids": [a.0, b.0]})).unwrap();
|
||
app.run("object.group", json!({})).unwrap();
|
||
let g = app.session.active().unwrap().selection.objects[0];
|
||
set(&mut app, g, |n| n.locked = true);
|
||
assert!(isolate_labels(&app).is_empty(), "a locked group");
|
||
app.session.doc_mut().unwrap().selection.set([a]);
|
||
assert!(isolate_labels(&app).is_empty(), "a path in a locked group");
|
||
set(&mut app, g, |n| n.locked = false);
|
||
app.run("object.ungroup", json!({})).unwrap();
|
||
app.run("select.set", json!({"ids": [a.0]})).unwrap();
|
||
app.run("view.guides.make", json!({})).unwrap();
|
||
app.run("view.guides.lock", json!({})).unwrap();
|
||
assert!(app.session.guides_locked());
|
||
app.session.doc_mut().unwrap().selection.set([a]);
|
||
assert!(isolate_labels(&app).is_empty(), "a guide while guides are locked");
|
||
}
|
||
|
||
/// While a symbol is edited, a path in the symbol's art has Isolate Selected Path, and a path
|
||
/// outside it, which a selection set by a command can still hold, has no isolation item.
|
||
#[test]
|
||
fn isolation_context_item_skips_objects_outside_the_edited_symbol() {
|
||
let mut app = app();
|
||
let outside = rect(&mut app, 0.0, 0.0);
|
||
rect(&mut app, 200.0, 0.0);
|
||
app.run("symbol.new", json!({"name": "Box"})).unwrap();
|
||
app.run("symbol.edit", json!({"name": "Box"})).unwrap();
|
||
assert!(app.session.active().unwrap().doc.symbol_edit.is_some(), "symbol editing mode");
|
||
let inside = rect(&mut app, 100.0, 100.0);
|
||
app.session.doc_mut().unwrap().selection.set([inside]);
|
||
assert_eq!(isolate_labels(&app), ["Isolate Selected Path"], "a path in the symbol's art");
|
||
app.session.doc_mut().unwrap().selection.set([outside]);
|
||
assert!(isolate_labels(&app).is_empty(), "a path outside the symbol's art");
|
||
assert!(app.run("object.isolate", json!({"id": outside.0})).is_err(), "object.isolate refuses it");
|
||
}
|
||
|
||
/// A closed path of 60 jagged anchors around (150, 150), filled so that a right-click inside it
|
||
/// hits it, with nothing selected.
|
||
fn jagged(app: &mut VectorcraftApp) -> NodeId {
|
||
let anchors: Vec<_> = (0..60)
|
||
.map(|i| {
|
||
let a = f64::from(i) * std::f64::consts::TAU / 60.0;
|
||
let r = 60.0 + f64::from(i % 2) * 4.0;
|
||
json!({"x": 150.0 + r * a.cos(), "y": 150.0 + r * a.sin()})
|
||
})
|
||
.collect();
|
||
let id = NodeId(app.run("path.create", json!({"anchors": anchors, "closed": true})).unwrap()["id"].as_u64().unwrap());
|
||
app.run("paint.setFill", json!({"color": "#ff8010"})).unwrap();
|
||
app.run("select.none", json!({})).unwrap();
|
||
id
|
||
}
|
||
|
||
/// The anchor points of every path in the document.
|
||
fn anchor_total(app: &VectorcraftApp) -> usize {
|
||
let mut n = 0;
|
||
app.session.active().unwrap().doc.walk(|node| {
|
||
if let NodeKind::Path { path, .. } = &node.kind {
|
||
n += path.anchor_count();
|
||
}
|
||
});
|
||
n
|
||
}
|
||
|
||
/// A right-click on a path lists Object › Path › Smooth… after Join and Average…. Smooth… opens
|
||
/// the Smooth slider at Auto-Smooth's level and adds no undo step while it is open; OK keeps the
|
||
/// smoothed path as one undo step.
|
||
#[test]
|
||
fn right_click_on_a_path_opens_the_smooth_slider() {
|
||
let mut app = app();
|
||
let id = jagged(&mut app);
|
||
let ctx = egui::Context::default();
|
||
frame(&mut app, &ctx, vec![]);
|
||
let p = screen(&app, 150.0, 150.0);
|
||
let texts = click(&mut app, &ctx, p, PointerButton::Secondary);
|
||
assert_eq!(app.session.active().unwrap().selection.objects, [id], "the path right-clicked is selected");
|
||
let y = |label| at(&texts, label).y;
|
||
assert!(y("Join") < y("Average…") && y("Average…") < y("Smooth…"), "{texts:?}");
|
||
let (undo, before) = (app.session.active().unwrap().history.undo.len(), anchor_total(&app));
|
||
let after = click(&mut app, &ctx, at(&texts, "Smooth…"), PointerButton::Primary);
|
||
assert!(!has(&after, "Arrange"), "choosing Smooth… closes the menu");
|
||
let open = app.ui.dialog.as_ref().map(|d| (d.kind.clone(), d.f64("level", -1.0)));
|
||
assert_eq!(open, Some((crate::dialogs::smooth_slider::KIND.to_string(), vectorcraft_engine::SMOOTH_AUTO_LEVEL)));
|
||
assert_eq!(app.session.active().unwrap().history.undo.len(), undo, "no undo step while the slider is open");
|
||
crate::dialogs::confirm(&mut app).unwrap();
|
||
assert!(app.ui.dialog.is_none() && !app.session.in_interaction());
|
||
let st = app.session.active().unwrap();
|
||
assert_eq!(st.history.undo.len(), undo + 1, "OK keeps the result as one undo step");
|
||
assert!(anchor_total(&app) < before, "the path was smoothed: {} anchor points from {before}", anchor_total(&app));
|
||
}
|
||
|
||
/// Smooth… is listed while the selection holds a path: a path, a group of paths, a compound path,
|
||
/// a path with type, and anchors selected with Direct Selection (whose Remove Anchor Points
|
||
/// stays). It is left out with nothing, only type or a group of type selected.
|
||
#[test]
|
||
fn smooth_follows_the_selections_paths() {
|
||
let mut app = app();
|
||
let (a, b) = (rect(&mut app, 0.0, 0.0), rect(&mut app, 150.0, 0.0));
|
||
let t = NodeId(app.run("text.create", json!({"x": 10, "y": 250, "text": "Type"})).unwrap()["id"].as_u64().unwrap());
|
||
let smooth = |app: &VectorcraftApp| labels(&menus::context_items(app)).contains(&"Smooth…");
|
||
app.run("select.none", json!({})).unwrap();
|
||
assert!(!smooth(&app), "nothing selected");
|
||
app.run("select.set", json!({"ids": [t.0]})).unwrap();
|
||
assert!(!smooth(&app), "only type selected");
|
||
let u = NodeId(app.run("text.create", json!({"x": 10, "y": 300, "text": "More"})).unwrap()["id"].as_u64().unwrap());
|
||
app.run("select.set", json!({"ids": [t.0, u.0]})).unwrap();
|
||
app.run("object.group", json!({})).unwrap();
|
||
assert!(!smooth(&app), "a group of type only");
|
||
app.run("object.ungroup", json!({})).unwrap();
|
||
app.run("select.set", json!({"ids": [a.0]})).unwrap();
|
||
assert!(smooth(&app), "a path");
|
||
app.run("select.set", json!({"ids": [a.0, t.0]})).unwrap();
|
||
assert!(smooth(&app), "a path and type");
|
||
app.run("select.set", json!({"ids": [a.0, b.0]})).unwrap();
|
||
app.run("object.group", json!({})).unwrap();
|
||
assert!(smooth(&app), "a group of paths");
|
||
app.run("object.ungroup", json!({})).unwrap();
|
||
app.run("select.set", json!({"ids": [a.0, b.0]})).unwrap();
|
||
app.run("object.compoundPath.make", json!({})).unwrap();
|
||
assert!(smooth(&app), "a compound path");
|
||
app.run("object.compoundPath.release", json!({})).unwrap();
|
||
app.select_tool("directSelection");
|
||
app.run("select.anchors", json!({"id": a.0, "anchors": [[0, 1]]})).unwrap();
|
||
assert!(smooth(&app), "an anchor point direct-selected");
|
||
assert!(labels(&menus::context_items(&app)).contains(&"Remove Anchor Points"), "the anchor entries stay");
|
||
}
|
||
|
||
/// Selected type has Bullets and Numbering › Apply Bullets in the context menu and the Type menu,
|
||
/// which makes its paragraphs a bulleted list; other art has no such item.
|
||
#[test]
|
||
fn selected_type_has_apply_bullets() {
|
||
let mut app = app();
|
||
let r = rect(&mut app, 0.0, 0.0);
|
||
app.run("select.set", json!({"ids": [r.0]})).unwrap();
|
||
let bullets = |app: &VectorcraftApp| menus::context_entries(app).into_iter().find(|e| e.command.as_deref() == Some("type.applyBullets"));
|
||
assert!(bullets(&app).is_none(), "a rectangle has no paragraphs");
|
||
let t = NodeId(app.run("text.create", json!({"x": 10, "y": 200, "text": "Item"})).unwrap()["id"].as_u64().unwrap());
|
||
app.run("select.set", json!({"ids": [t.0]})).unwrap();
|
||
let e = bullets(&app).expect("Apply Bullets");
|
||
assert_eq!((e.path.as_slice(), e.label.as_str(), e.enabled), (&["Bullets and Numbering".to_string()][..], "Apply Bullets", true));
|
||
let typed = menus::menu_entries(&app).into_iter().find(|e| e.command.as_deref() == Some("type.applyBullets")).expect("in the Type menu");
|
||
assert_eq!(typed.path, ["Type", "Bullets and Numbering"]);
|
||
menus::invoke(&mut app, "type.applyBullets", json!({}));
|
||
let Some(NodeKind::Text(text)) = app.session.active().unwrap().doc.node(t).map(|n| &n.kind) else { panic!("text") };
|
||
assert!(text.para_at(0).list.is_some());
|
||
}
|
||
|
||
/// Selected type has Bullets and Numbering › Apply Numbers in the context menu and the Type menu,
|
||
/// after Apply Bullets, which makes its paragraphs a numbered list; other art has no such item.
|
||
#[test]
|
||
fn selected_type_has_apply_numbers() {
|
||
let mut app = app();
|
||
let r = rect(&mut app, 0.0, 0.0);
|
||
app.run("select.set", json!({"ids": [r.0]})).unwrap();
|
||
let numbers = |app: &VectorcraftApp| menus::context_entries(app).into_iter().position(|e| e.command.as_deref() == Some("type.applyNumbers"));
|
||
assert!(numbers(&app).is_none(), "a rectangle has no paragraphs");
|
||
let t = NodeId(app.run("text.create", json!({"x": 10, "y": 200, "text": "Item"})).unwrap()["id"].as_u64().unwrap());
|
||
app.run("select.set", json!({"ids": [t.0]})).unwrap();
|
||
let entries = menus::context_entries(&app);
|
||
let at = numbers(&app).expect("Apply Numbers");
|
||
let e = &entries[at];
|
||
assert_eq!((e.path.as_slice(), e.label.as_str(), e.enabled), (&["Bullets and Numbering".to_string()][..], "Apply Numbers", true));
|
||
assert_eq!(entries.get(at.wrapping_sub(1)).and_then(|e| e.command.as_deref()), Some("type.applyBullets"));
|
||
let typed = menus::menu_entries(&app).into_iter().find(|e| e.command.as_deref() == Some("type.applyNumbers")).expect("in the Type menu");
|
||
assert_eq!(typed.path, ["Type", "Bullets and Numbering"]);
|
||
menus::invoke(&mut app, "type.applyNumbers", json!({}));
|
||
let Some(NodeKind::Text(text)) = app.session.active().unwrap().doc.node(t).map(|n| &n.kind) else { panic!("text") };
|
||
assert_eq!(text.para_at(0).list.and_then(|l| l.number), Some(vectorcraft_doc::text::NumberStyle::Arabic));
|
||
}
|
||
|
||
/// A right-click on type has Spelling › Auto Spell Check, which turns it on or off as Edit ›
|
||
/// Spelling does (helpx: Check and correct spelling, "Turn on Auto Spell Check"). The context menu
|
||
/// draws no check marks, but this submenu shows whether it is on, with Check Spelling… after it;
|
||
/// other submenus stay without the room for one. Other art has no Spelling submenu.
|
||
#[test]
|
||
fn right_click_on_type_turns_auto_spell_check_on_and_off() {
|
||
let mut app = app();
|
||
let r = rect(&mut app, 200.0, 150.0);
|
||
let t = NodeId(app.run("text.create", json!({"x": 20, "y": 60, "text": "Hello", "size": 24})).unwrap()["id"].as_u64().unwrap());
|
||
app.run("select.none", json!({})).unwrap();
|
||
let ctx = egui::Context::default();
|
||
frame(&mut app, &ctx, vec![]);
|
||
let p = screen(&app, 250.0, 200.0);
|
||
assert!(!has(&click(&mut app, &ctx, p, PointerButton::Secondary), "Spelling"), "a rectangle has no Spelling submenu");
|
||
assert_eq!(app.session.active().unwrap().selection.objects, [r]);
|
||
let away = screen(&app, 390.0, 290.0);
|
||
click(&mut app, &ctx, away, PointerButton::Primary);
|
||
// Right-click the type, open Spelling: Auto Spell Check, not checked.
|
||
let open_spelling = |app: &mut VectorcraftApp| {
|
||
let p = word_at(app, t, 0, 5);
|
||
let texts = click(app, &ctx, p, PointerButton::Secondary);
|
||
assert_eq!(app.session.active().unwrap().selection.objects, [t], "the type right-clicked is selected");
|
||
frame(app, &ctx, vec![Event::PointerMoved(at(&texts, "Spelling"))]);
|
||
frame(app, &ctx, vec![])
|
||
};
|
||
let texts = open_spelling(&mut app);
|
||
assert!(texts.iter().any(|(s, _)| s == " Auto Spell Check"), "unchecked, with the room for a check mark: {texts:?}");
|
||
assert!(texts.iter().any(|(s, _)| s == "Transform"), "other items have no gutter: {texts:?}");
|
||
// Agents see it under the Spelling path.
|
||
let e = menus::context_entries(&app).into_iter().find(|e| e.command.as_deref() == Some("edit.autoSpellCheck")).expect("Auto Spell Check");
|
||
assert_eq!((e.path.as_slice(), e.label.as_str()), (&["Spelling".to_string()][..], "Auto Spell Check"));
|
||
let texts = click(&mut app, &ctx, at(&texts, " Auto Spell Check"), PointerButton::Primary);
|
||
assert!(app.ui.view.auto_spell_check, "turned on");
|
||
assert!(!has(&texts, "Spelling"), "the menu closed: {texts:?}");
|
||
let texts = open_spelling(&mut app);
|
||
assert!(texts.iter().any(|(s, _)| s == "✓ Auto Spell Check"), "checked: {texts:?}");
|
||
assert!(texts.iter().any(|(s, _)| s == "Check Spelling…"), "the dialog's item, which has no check mark: {texts:?}");
|
||
click(&mut app, &ctx, at(&texts, "✓ Auto Spell Check"), PointerButton::Primary);
|
||
assert!(!app.ui.view.auto_spell_check, "turned off");
|
||
}
|
||
|
||
/// A right-click on type has Spelling › Check Spelling… after Auto Spell Check, as Edit ›
|
||
/// Spelling has, which opens the Check Spelling dialog on the type right-clicked (helpx: Check and
|
||
/// correct spelling). Other art has no such item.
|
||
#[test]
|
||
fn right_click_on_type_opens_check_spelling() {
|
||
let mut app = app();
|
||
rect(&mut app, 200.0, 150.0);
|
||
let t = NodeId(app.run("text.create", json!({"x": 20, "y": 60, "text": "Hello wrold", "size": 24})).unwrap()["id"].as_u64().unwrap());
|
||
app.run("select.none", json!({})).unwrap();
|
||
let ctx = egui::Context::default();
|
||
frame(&mut app, &ctx, vec![]);
|
||
let p = screen(&app, 250.0, 200.0);
|
||
click(&mut app, &ctx, p, PointerButton::Secondary);
|
||
let dialog_item = |app: &VectorcraftApp| {
|
||
menus::context_entries(app).into_iter().position(|e| e.command.as_deref() == Some(crate::dialogs::check_spelling::COMMAND))
|
||
};
|
||
assert!(dialog_item(&app).is_none(), "a rectangle has no Check Spelling…");
|
||
let away = screen(&app, 390.0, 290.0);
|
||
click(&mut app, &ctx, away, PointerButton::Primary);
|
||
let p = word_at(&app, t, 0, 5);
|
||
let texts = click(&mut app, &ctx, p, PointerButton::Secondary);
|
||
assert_eq!(app.session.active().unwrap().selection.objects, [t], "the type right-clicked is selected");
|
||
// Agents see it under the Spelling path, after Auto Spell Check.
|
||
let entries = menus::context_entries(&app);
|
||
let i = dialog_item(&app).expect("Check Spelling…");
|
||
assert_eq!((entries[i].path.as_slice(), entries[i].label.as_str(), entries[i].enabled), (&["Spelling".to_string()][..], "Check Spelling…", true));
|
||
assert_eq!(entries.get(i.wrapping_sub(1)).and_then(|e| e.command.as_deref()), Some("edit.autoSpellCheck"));
|
||
frame(&mut app, &ctx, vec![Event::PointerMoved(at(&texts, "Spelling"))]);
|
||
let texts = frame(&mut app, &ctx, vec![]);
|
||
let (auto, check) = (at(&texts, " Auto Spell Check"), at(&texts, "Check Spelling…"));
|
||
assert!(auto.y < check.y, "Auto Spell Check first: {texts:?}");
|
||
let texts = click(&mut app, &ctx, check, PointerButton::Primary);
|
||
assert!(!has(&texts, "Spelling"), "the menu closed: {texts:?}");
|
||
let d = app.ui.dialog.as_ref().expect("a dialog opened");
|
||
assert_eq!(d.kind.as_str(), crate::dialogs::check_spelling::KIND);
|
||
assert_eq!(d.fields["ids"], json!([t.0]), "it checks the type right-clicked");
|
||
assert_eq!(d.fields["not_in_dictionary"], json!("wrold"), "and stops at its unknown word");
|
||
}
|
||
|
||
/// A right-click on a word Auto Spell Check underlined lists the suggestions, Ignore All and Add
|
||
/// to Dictionary, and the type's Spelling submenu with Check Spelling… as well.
|
||
#[test]
|
||
fn right_click_on_an_underlined_word_also_offers_check_spelling() {
|
||
let mut app = app();
|
||
let id = spelled(&mut app, "the speling cat");
|
||
let ctx = egui::Context::default();
|
||
frame(&mut app, &ctx, vec![]);
|
||
let p = word_at(&app, id, 4, 11);
|
||
let texts = click(&mut app, &ctx, p, PointerButton::Secondary);
|
||
assert!(has(&texts, "Ignore All") && has(&texts, "Add to Dictionary"), "{texts:?}");
|
||
let e = menus::context_entries(&app).into_iter().find(|e| e.command.as_deref() == Some(crate::dialogs::check_spelling::COMMAND));
|
||
assert_eq!(e.map(|e| e.path), Some(vec!["Spelling".to_string()]), "Spelling › Check Spelling…");
|
||
frame(&mut app, &ctx, vec![Event::PointerMoved(at(&texts, "Spelling"))]);
|
||
let texts = frame(&mut app, &ctx, vec![]);
|
||
assert!(texts.iter().any(|(s, _)| s == "✓ Auto Spell Check"), "checked: {texts:?}");
|
||
click(&mut app, &ctx, at(&texts, "Check Spelling…"), PointerButton::Primary);
|
||
let d = app.ui.dialog.as_ref().expect("a dialog opened");
|
||
assert_eq!(d.kind.as_str(), crate::dialogs::check_spelling::KIND);
|
||
assert_eq!(d.fields["not_in_dictionary"], json!("speling"));
|
||
}
|
||
|
||
/// The members of the overlap area of Intertwine group `g` at (x, y), in the order they are drawn
|
||
/// there, bottom first.
|
||
fn intertwine_order(app: &VectorcraftApp, g: NodeId, x: f64, y: f64) -> Vec<NodeId> {
|
||
use vectorcraft_tools::intertwine::{drawn_order, face_at, faces};
|
||
let st = app.session.active().unwrap();
|
||
let children = st.doc.node(g).unwrap().children().unwrap();
|
||
let face = face_at(&faces(children), Point::new(x, y)).unwrap();
|
||
drawn_order(children, &face).into_iter().map(|i| children[i].id).collect()
|
||
}
|
||
|
||
/// With the Intertwine tool, a right-click where three or more objects of the group overlap lists
|
||
/// Bring to Front, Bring Forward, Send Backward and Send to Back for the object nearest to the
|
||
/// pointer in place of the Arrange submenu, and they move it in that overlap area only. Elsewhere
|
||
/// the menu is the usual one.
|
||
#[test]
|
||
fn right_click_where_three_intertwined_objects_overlap_arranges_the_nearest_one() {
|
||
let mut app = app();
|
||
// Rectangles from x = 50, 90 and 130, 120 wide: all three overlap from x = 130 to 170.
|
||
let wide = |app: &mut VectorcraftApp, x: f64| {
|
||
NodeId(app.run("shape.rectangle", json!({"x": x, "y": 50, "width": 120, "height": 100})).unwrap()["id"].as_u64().unwrap())
|
||
};
|
||
let (a, b, c) = (wide(&mut app, 50.0), wide(&mut app, 90.0), wide(&mut app, 130.0));
|
||
app.run("select.set", json!({"ids": [a.0, b.0, c.0]})).unwrap();
|
||
let g = NodeId(app.run("object.intertwine.make", json!({})).unwrap()["id"].as_u64().unwrap());
|
||
assert_eq!(app.session.tool_id(), "intertwine");
|
||
let ctx = egui::Context::default();
|
||
frame(&mut app, &ctx, vec![]);
|
||
let items = ["Bring to Front", "Bring Forward", "Send Backward", "Send to Back"];
|
||
// By the right edge of A (the first rectangle), where all three overlap.
|
||
let p = screen(&app, 169.0, 100.0);
|
||
let texts = click(&mut app, &ctx, p, PointerButton::Secondary);
|
||
assert_eq!(app.session.active().unwrap().selection.objects, [g], "the selection stays the group");
|
||
for label in items {
|
||
assert!(has(&texts, label), "{label} in {texts:?}");
|
||
}
|
||
assert!(!has(&texts, "Arrange"), "no Arrange submenu: {texts:?}");
|
||
let rows: Vec<f32> = items.iter().map(|l| at(&texts, l).y).collect();
|
||
assert!(rows.windows(2).all(|w| w[0] < w[1]), "in that order: {rows:?}");
|
||
let Some(Item::Cmd(_, id, params)) = menus::context_items(&app).into_iter().find(|i| matches!(i, Item::Cmd("Bring Forward", ..))) else {
|
||
panic!("no Bring Forward item")
|
||
};
|
||
assert_eq!(id, "object.intertwine.arrange");
|
||
assert_eq!((¶ms["id"], ¶ms["order"], ¶ms["member"]), (&json!(g.0), &json!("forward"), &json!(a.0)));
|
||
// Bring Forward moves A one place up in that area only.
|
||
assert_eq!(intertwine_order(&app, g, 150.0, 100.0), [a, b, c]);
|
||
let texts = click(&mut app, &ctx, at(&texts, "Bring Forward"), PointerButton::Primary);
|
||
assert!(!has(&texts, "Bring Forward"), "the menu closes");
|
||
assert_eq!(intertwine_order(&app, g, 150.0, 100.0), [b, a, c]);
|
||
assert_eq!(intertwine_order(&app, g, 110.0, 100.0), [a, b], "where only A and B overlap");
|
||
// Where only two overlap, the menu is the usual one.
|
||
let p = screen(&app, 110.0, 100.0);
|
||
let texts = click(&mut app, &ctx, p, PointerButton::Secondary);
|
||
assert!(has(&texts, "Arrange") && !has(&texts, "Bring Forward"), "{texts:?}");
|
||
}
|
||
|
||
/// Every label the context menu can show is a menu string, so complete catalogs must translate it.
|
||
#[test]
|
||
fn every_context_label_is_a_menu_string() {
|
||
let strings = menus::menu_strings();
|
||
let mut app = app();
|
||
let mut shown = vec![];
|
||
// Nothing selected, one path, two paths, a group, a clipping group, a compound path, isolation.
|
||
shown.extend(labels(&menus::context_items(&app)));
|
||
let (a, b) = (rect(&mut app, 0.0, 0.0), rect(&mut app, 50.0, 50.0));
|
||
shown.extend(labels(&menus::context_items(&app)));
|
||
app.run("select.set", json!({"ids": [a.0, b.0]})).unwrap();
|
||
shown.extend(labels(&menus::context_items(&app)));
|
||
app.run("object.compoundPath.make", json!({})).unwrap();
|
||
shown.extend(labels(&menus::context_items(&app)));
|
||
app.run("object.compoundPath.release", json!({})).unwrap();
|
||
app.run("select.set", json!({"ids": [a.0, b.0]})).unwrap();
|
||
app.run("object.clippingMask.make", json!({})).unwrap();
|
||
shown.extend(labels(&menus::context_items(&app)));
|
||
app.run("object.group", json!({})).unwrap();
|
||
shown.extend(labels(&menus::context_items(&app)));
|
||
menus::invoke(&mut app, "object.isolate", json!({}));
|
||
shown.extend(labels(&menus::context_items(&app)));
|
||
// The Unlock submenu that a right-click over a locked object adds.
|
||
menus::invoke(&mut app, "object.exitIsolation", json!({}));
|
||
app.run("layer.setProps", json!({"ids": [a.0], "locked": true})).unwrap();
|
||
app.ui.context_locked = Some((app.session.active().unwrap().uid, vec![a]));
|
||
shown.extend(labels(&menus::context_items(&app)));
|
||
// A word Auto Spell Check underlined, with no suggestions: Ignore All, Add to Dictionary.
|
||
let text = NodeId(app.run("text.create", json!({"x": 10, "y": 200, "text": "Zqxvbtkw"})).unwrap()["id"].as_u64().unwrap());
|
||
app.ui.context_spelling = Some(crate::state::ContextSpelling {
|
||
uid: app.session.active().unwrap().uid,
|
||
id: text,
|
||
start: 0,
|
||
end: 8,
|
||
word: "Zqxvbtkw".into(),
|
||
suggestions: vec![],
|
||
});
|
||
shown.extend(labels(&menus::context_items(&app)));
|
||
app.ui.context_spelling = None;
|
||
// The Lock Artboards and Unlock Artboards submenus of the Artboard tool.
|
||
app.run("artboard.new", json!({"x": 400, "y": 0, "width": 100, "height": 80})).unwrap();
|
||
app.run("artboard.setProps", json!({"index": 0, "locked": true})).unwrap();
|
||
app.select_tool("artboard");
|
||
shown.extend(labels(&menus::context_items(&app)));
|
||
// Lock Selected with the artboard that isn't locked active.
|
||
crate::panels::artboards::select(&mut app, 1);
|
||
shown.extend(labels(&menus::context_items(&app)));
|
||
let missing: Vec<_> = shown.iter().filter(|l| !strings.contains(**l)).collect();
|
||
assert!(missing.is_empty(), "context menu labels missing from menu_strings: {missing:?}");
|
||
for label in menus::CONTEXT_LABELS {
|
||
assert!(shown.contains(label), "`{label}` is listed but the context menu never shows it");
|
||
}
|
||
}
|
||
|
||
/// Point type holding `t` at (20, 60), with Auto Spell Check on.
|
||
fn spelled(app: &mut VectorcraftApp, t: &str) -> NodeId {
|
||
let id = NodeId(app.run("text.create", json!({"x": 20, "y": 60, "text": t, "size": 24})).unwrap()["id"].as_u64().unwrap());
|
||
app.run("select.none", json!({})).unwrap();
|
||
app.run("edit.autoSpellCheck", json!({})).unwrap();
|
||
id
|
||
}
|
||
|
||
/// Where on screen the middle of bytes `a..b` of type object `id` is.
|
||
fn word_at(app: &VectorcraftApp, id: NodeId, a: usize, b: usize) -> Pos2 {
|
||
let st = app.session.active().unwrap();
|
||
let Some(NodeKind::Text(t)) = st.doc.node(id).map(|n| &n.kind) else { panic!("type") };
|
||
let lay = vectorcraft_text::layout(vectorcraft_text::FontDb::global(), t);
|
||
let q = vectorcraft_text::selection_quads(&lay, a, b)[0].map(|c| t.xf * c);
|
||
let c = Point::new(q.iter().map(|p| p.x).sum::<f64>() / 4.0, q.iter().map(|p| p.y).sum::<f64>() / 4.0);
|
||
screen(app, c.x, c.y)
|
||
}
|
||
|
||
fn plain(app: &VectorcraftApp, id: NodeId) -> String {
|
||
match app.session.active().unwrap().doc.node(id).map(|n| &n.kind) {
|
||
Some(NodeKind::Text(t)) => t.plain_text(),
|
||
_ => panic!("type"),
|
||
}
|
||
}
|
||
|
||
/// With Auto Spell Check on, right-clicking an underlined word lists what the dictionary suggests
|
||
/// instead, then Ignore All and Add to Dictionary; a suggestion replaces the word in one undo step
|
||
/// (helpx: Check and correct spelling, "Correct spelling errors during Auto Spell Check").
|
||
#[test]
|
||
fn right_click_on_an_underlined_word_offers_its_suggestions() {
|
||
let mut app = app();
|
||
let id = spelled(&mut app, "the speling cat");
|
||
let want: Vec<String> = app.session.execute("text.spelling.suggest", &json!({"word": "speling"})).unwrap()["suggestions"]
|
||
.as_array()
|
||
.unwrap()
|
||
.iter()
|
||
.map(|s| s.as_str().unwrap().to_string())
|
||
.collect();
|
||
assert!(!want.is_empty(), "the dictionary has suggestions for `speling`");
|
||
let ctx = egui::Context::default();
|
||
frame(&mut app, &ctx, vec![]);
|
||
let p = word_at(&app, id, 4, 11);
|
||
let texts = click(&mut app, &ctx, p, PointerButton::Secondary);
|
||
for w in &want {
|
||
assert!(has(&texts, w), "`{w}` in {texts:?}");
|
||
}
|
||
assert!(has(&texts, "Ignore All") && has(&texts, "Add to Dictionary"), "{texts:?}");
|
||
// Agents see them at the top of the menu, a suggestion by its word.
|
||
let entries = menus::context_entries(&app);
|
||
assert_eq!((entries[0].label.as_str(), entries[0].command.as_deref()), (want[0].as_str(), Some("text.editRange")));
|
||
let steps = app.session.active().unwrap().history.undo.len();
|
||
let texts = click(&mut app, &ctx, at(&texts, &want[0]), PointerButton::Primary);
|
||
assert_eq!(plain(&app, id), format!("the {} cat", want[0]), "the word is replaced");
|
||
assert_eq!(app.session.active().unwrap().history.undo.len(), steps + 1, "in one undo step");
|
||
assert!(!has(&texts, "Ignore All"), "the menu closed: {texts:?}");
|
||
}
|
||
|
||
/// Ignore All and Add to Dictionary from the menu: the word's line goes, in this document for
|
||
/// Ignore All, in every check for Add. A word with nothing to suggest says so.
|
||
#[test]
|
||
fn ignore_all_and_add_to_dictionary_from_the_context_menu() {
|
||
let mut app = app();
|
||
let id = spelled(&mut app, "Qzvxtrwk and Kwqzvxtr");
|
||
let ctx = egui::Context::default();
|
||
frame(&mut app, &ctx, vec![]);
|
||
let p = word_at(&app, id, 0, 8);
|
||
let texts = click(&mut app, &ctx, p, PointerButton::Secondary);
|
||
assert!(has(&texts, "No Suggestions"), "{texts:?}");
|
||
click(&mut app, &ctx, at(&texts, "Ignore All"), PointerButton::Primary);
|
||
assert!(app.session.active().unwrap().spelling_ignored.contains("qzvxtrwk"));
|
||
let p = word_at(&app, id, 13, 21);
|
||
let texts = click(&mut app, &ctx, p, PointerButton::Secondary);
|
||
click(&mut app, &ctx, at(&texts, "Add to Dictionary"), PointerButton::Primary);
|
||
let added = craft_proof_has(&mut app, "kwqzvxtr");
|
||
app.run("text.spelling.removeWord", json!({"word": "kwqzvxtr"})).unwrap();
|
||
assert!(added, "added to the custom dictionary");
|
||
// Nothing left underlined there: a right-click on it offers no spelling items.
|
||
let p = word_at(&app, id, 0, 8);
|
||
let texts = click(&mut app, &ctx, p, PointerButton::Secondary);
|
||
assert!(!has(&texts, "Ignore All"), "{texts:?}");
|
||
}
|
||
|
||
fn craft_proof_has(app: &mut VectorcraftApp, w: &str) -> bool {
|
||
app.run("text.spelling.dictionary", json!({})).unwrap()["words"].as_array().unwrap().iter().any(|x| x == w)
|
||
}
|
||
|
||
/// With Auto Spell Check off, or on a word that is right, a right-click lists no spelling items.
|
||
#[test]
|
||
fn no_spelling_items_off_a_misspelled_word_or_with_auto_spell_check_off() {
|
||
let mut app = app();
|
||
let id = spelled(&mut app, "the Vqzxtrwk");
|
||
let ctx = egui::Context::default();
|
||
frame(&mut app, &ctx, vec![]);
|
||
let p = word_at(&app, id, 0, 3);
|
||
let texts = click(&mut app, &ctx, p, PointerButton::Secondary);
|
||
assert!(!has(&texts, "Ignore All"), "a right word: {texts:?}");
|
||
app.run("edit.autoSpellCheck", json!({})).unwrap();
|
||
frame(
|
||
&mut app,
|
||
&ctx,
|
||
vec![Event::Key { key: egui::Key::Escape, physical_key: None, pressed: true, repeat: false, modifiers: Default::default() }],
|
||
);
|
||
let p = word_at(&app, id, 4, 12);
|
||
let texts = click(&mut app, &ctx, p, PointerButton::Secondary);
|
||
assert!(!has(&texts, "Ignore All"), "off: {texts:?}");
|
||
}
|
||
|
||
/// A right word in type on top of an underlined word in other type: the right-click is on the
|
||
/// top type, so it lists no spelling items for the word under it.
|
||
#[test]
|
||
fn a_word_under_other_type_is_not_the_one_right_clicked() {
|
||
let mut app = app();
|
||
let under = spelled(&mut app, "Qzvxtrwk");
|
||
let ctx = egui::Context::default();
|
||
frame(&mut app, &ctx, vec![]);
|
||
let p = word_at(&app, under, 0, 8);
|
||
let texts = click(&mut app, &ctx, p, PointerButton::Secondary);
|
||
assert!(has(&texts, "Ignore All"), "alone, the word is offered: {texts:?}");
|
||
frame(
|
||
&mut app,
|
||
&ctx,
|
||
vec![Event::Key { key: egui::Key::Escape, physical_key: None, pressed: true, repeat: false, modifiers: Default::default() }],
|
||
);
|
||
app.run("text.create", json!({"x": 20, "y": 60, "text": "the cat sat on the mat", "size": 24})).unwrap();
|
||
app.run("select.none", json!({})).unwrap();
|
||
let texts = click(&mut app, &ctx, p, PointerButton::Secondary);
|
||
assert!(!has(&texts, "Ignore All"), "under right type: {texts:?}");
|
||
}
|
||
|
||
/// In an Intertwine group, the type drawn on top at the right-click is the one whose word is
|
||
/// offered: a member brought to the front there, though earlier in the stacking order.
|
||
#[test]
|
||
fn in_an_intertwine_group_the_type_drawn_on_top_is_the_one_right_clicked() {
|
||
let mut app = app();
|
||
let under = spelled(&mut app, "Qzvxtrwk");
|
||
let over =
|
||
NodeId(app.run("text.create", json!({"x": 20, "y": 60, "text": "the cat sat on the mat", "size": 24})).unwrap()["id"].as_u64().unwrap());
|
||
app.run("select.set", json!({"ids": [under.0, over.0]})).unwrap();
|
||
let g = NodeId(app.run("object.intertwine.make", json!({})).unwrap()["id"].as_u64().unwrap());
|
||
app.select_tool("selection");
|
||
app.run("select.none", json!({})).unwrap();
|
||
let ctx = egui::Context::default();
|
||
frame(&mut app, &ctx, vec![]);
|
||
// A point on the word where glyphs of both overlap: Intertwine's areas follow the outlines.
|
||
let quad = {
|
||
let st = app.session.active().unwrap();
|
||
let Some(NodeKind::Text(t)) = st.doc.node(under).map(|n| &n.kind) else { panic!("type") };
|
||
let lay = vectorcraft_text::layout(vectorcraft_text::FontDb::global(), t);
|
||
vectorcraft_text::selection_quads(&lay, 0, 8)[0].map(|c| t.xf * c)
|
||
};
|
||
let (x0, x1) = (quad.iter().map(|c| c.x).fold(f64::MAX, f64::min), quad.iter().map(|c| c.x).fold(f64::MIN, f64::max));
|
||
let (y0, y1) = (quad.iter().map(|c| c.y).fold(f64::MAX, f64::min), quad.iter().map(|c| c.y).fold(f64::MIN, f64::max));
|
||
let overlap = (0..40)
|
||
.flat_map(|i| (0..20).map(move |j| Point::new(x0 + (x1 - x0) * (i as f64 + 0.5) / 40.0, y0 + (y1 - y0) * (j as f64 + 0.5) / 20.0)))
|
||
.find(|q| app.run("object.intertwine.arrange", json!({"id": g.0, "at": [q.x, q.y], "order": "back", "member": under.0})).is_ok())
|
||
.expect("glyphs of both overlap somewhere on the word");
|
||
let p = screen(&app, overlap.x, overlap.y);
|
||
let texts = click(&mut app, &ctx, p, PointerButton::Secondary);
|
||
assert!(!has(&texts, "Ignore All"), "under the right type: {texts:?}");
|
||
frame(
|
||
&mut app,
|
||
&ctx,
|
||
vec![Event::Key { key: egui::Key::Escape, physical_key: None, pressed: true, repeat: false, modifiers: Default::default() }],
|
||
);
|
||
app.run("object.intertwine.arrange", json!({"id": g.0, "at": [overlap.x, overlap.y], "order": "front", "member": under.0})).unwrap();
|
||
let texts = click(&mut app, &ctx, p, PointerButton::Secondary);
|
||
assert!(has(&texts, "Ignore All"), "brought to the front there: {texts:?}");
|
||
}
|