Spelling right-click: Intertwine members by their tier at the point (part of #4936)

This commit is contained in:
HassanKhan
2026-11-10 10:48:52 +05:00
parent e33906976d
commit 0d7c6dd6e2
2 changed files with 56 additions and 9 deletions
+16 -9
View File
@@ -175,18 +175,25 @@ pub(crate) fn misspelled_word_at(app: &mut VectorcraftApp, p: Point) -> Option<c
// The topmost type whose glyphs `p` is on, and whether it or a layer or group holding it is
// locked: a word under other type isn't the one right-clicked, whether that type's word there
// is right or not.
let mut top: Option<(&Node, bool)> = None;
fn walk<'a>(nodes: &'a [std::sync::Arc<Node>], locked: bool, f: &mut impl FnMut(&'a Node, bool)) {
let mut top: Option<(vectorcraft_doc::NodeId, bool)> = None;
// Back to front as drawn at `p`: an Intertwine group's members by their tier there, those of
// one tier in their stacking order ([`vectorcraft_doc::intertwine`]).
fn walk(nodes: &[std::sync::Arc<Node>], p: Point, locked: bool, f: &mut impl FnMut(&Node, bool)) {
for n in nodes.iter().filter(|n| n.visible) {
let locked = locked || n.locked;
f(n, locked);
if let Some(children) = n.children() {
walk(children, locked, f);
let Some(children) = n.children() else { continue };
if matches!(n.kind, NodeKind::Intertwine { .. }) {
let mut drawn = children.to_vec();
drawn.sort_by_key(|c| vectorcraft_doc::intertwine::tier_at(c, p));
walk(&drawn, p, locked, f);
} else {
walk(children, p, locked, f);
}
}
}
// Paint order: a later hit is on top.
walk(&st.doc.layers, false, &mut |n, locked| {
// A later hit is on top.
walk(&st.doc.layers, p, false, &mut |n, locked| {
let NodeKind::Text(tx) = &n.kind else { return };
if n.geometric_bounds().is_none_or(|b| !b.contains(p)) {
return;
@@ -194,10 +201,11 @@ pub(crate) fn misspelled_word_at(app: &mut VectorcraftApp, p: Point) -> Option<c
let lay = vectorcraft_text::layout(vectorcraft_text::FontDb::global(), tx);
let on = vectorcraft_text::selection_quads(&lay, 0, tx.plain_text().len()).iter().any(|q| in_quad(q.map(|c| tx.xf * c), p));
if on {
top = Some((n, locked));
top = Some((n.id, locked));
}
});
let (n, locked) = top?;
let (id, locked) = top?;
let n = st.doc.node(id)?;
let NodeKind::Text(tx) = &n.kind else { return None };
if locked || !auto_spell_checks(n) {
return None;
@@ -207,7 +215,6 @@ pub(crate) fn misspelled_word_at(app: &mut VectorcraftApp, p: Point) -> Option<c
let w = words
.into_iter()
.find(|w| vectorcraft_text::selection_quads(&lay, w.range.start, w.range.end).iter().any(|q| in_quad(q.map(|c| tx.xf * c), p)))?;
let id = n.id;
let word = tx.plain_text().get(w.range.clone())?.to_string();
let uid = st.uid;
let suggestions = app
+40
View File
@@ -528,3 +528,43 @@ fn a_word_under_other_type_is_not_the_one_right_clicked() {
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:?}");
}