diff --git a/crates/engine/src/cmd/comment.rs b/crates/engine/src/cmd/comment.rs index 388d56d88..35ea38901 100644 --- a/crates/engine/src/cmd/comment.rs +++ b/crates/engine/src/cmd/comment.rs @@ -312,9 +312,12 @@ fn own(cmd: &str, m: &Message, who: &str, what: &str) -> crate::Result<()> { } /// A node to pin to: a node id on `page`, `true` for the innermost frame under the point, else none. -fn anchor_at(doc: &Document, page: NodeId, x: f64, y: f64, v: Option<&Value>) -> Option { +fn anchor_at(doc: &Document, page: NodeId, x: f64, y: f64, v: Option<&Value>, within: Option) -> Option { match v { - Some(Value::Bool(true)) => frame_at(doc, page, x, y), + Some(Value::Bool(true)) => match within.filter(|w| *w != page && doc.page_of(*w) == Some(page) && contains(doc, *w, x, y)) { + Some(w) => frame_in(doc, w, x, y, Some(w)), + None => frame_at(doc, page, x, y), + }, Some(v) => id_of(v).filter(|a| *a != page && doc.page_of(*a) == Some(page)), None => None, } @@ -670,7 +673,7 @@ pub fn specs() -> Vec { spec!( "comment.add", "Add comment", - "{x, y, text, page?, author?, anchor? (node id to pin to; true = innermost frame under the point)}", + "{x, y, text, page?, author?, anchor? (node id to pin to; true = innermost frame under the point), within? (with anchor true: search only inside this frame on the page, which is the anchor when no child holds the point)}", true, |s, p| { let x = req_num("comment.add", p, "x")?; @@ -682,7 +685,7 @@ pub fn specs() -> Vec { if !doc.pages.contains(&page) { return Err(bad("comment.add", format!("{page} is not a page"))); } - let anchor = anchor_at(doc, page, x, y, get(p, "anchor")); + let anchor = anchor_at(doc, page, x, y, get(p, "anchor"), id(p, "within")); let (lx, ly) = local(doc, anchor, x, y); let mut c = Comment { id: s.doc_mut()?.new_comment_id(), @@ -772,7 +775,7 @@ pub fn specs() -> Vec { let (x, y) = (req_num("comment.move", p, "x")?, req_num("comment.move", p, "y")?); let doc = s.doc()?; let page = doc.comment(id).map(|c| c.page).ok_or_else(|| bad("comment.move", "no comment"))?; - let anchor = anchor_at(doc, page, x, y, Some(get(p, "anchor").unwrap_or(&Value::Bool(true)))); + let anchor = anchor_at(doc, page, x, y, Some(get(p, "anchor").unwrap_or(&Value::Bool(true))), None); let (lx, ly) = local(doc, anchor, x, y); if let Some(c) = s.doc_mut()?.comment_mut(id) { (c.x, c.y, c.anchor) = (lx, ly, anchor); @@ -974,9 +977,20 @@ pub fn specs() -> Vec { /// Innermost frame (or component/instance/section) under a page point, for anchoring. fn frame_at(doc: &Document, page: NodeId, x: f64, y: f64) -> Option { + frame_in(doc, page, x, y, None) +} + +/// Whether the page point lies inside node `n`'s box. +fn contains(doc: &Document, n: NodeId, x: f64, y: f64) -> bool { + let Some(node) = doc.node(n) else { return false }; + let l = doc.world_transform(n).inverse() * geom::Point::new(x, y); + l.x >= 0.0 && l.y >= 0.0 && l.x <= node.width && l.y <= node.height +} + +/// The innermost frame-like or section under the point, searching from `root` (with `found` as the fallback). +fn frame_in(doc: &Document, root: NodeId, x: f64, y: f64, mut found: Option) -> Option { let pt = geom::Point::new(x, y); - let mut found = None; - let mut stack = vec![page]; + let mut stack = vec![root]; while let Some(parent) = stack.pop() { let Some(n) = doc.node(parent) else { continue }; for c in n.children.iter().rev() { @@ -1012,6 +1026,25 @@ mod tests { assert_eq!(super::parse_link("figcraft://comment/5/"), Some(5)); } + #[test] + fn comment_add_within_anchors_to_the_presented_frame() { + let mut s = Session::new(); + let mk = |s: &mut Session, params: serde_json::Value| crate::params::id_of(&s.execute("node.create", ¶ms).unwrap()["id"]).unwrap(); + let a = mk(&mut s, json!({"type": "frame", "x": 0, "y": 0, "width": 200, "height": 200})); + let b = mk(&mut s, json!({"type": "frame", "x": 100, "y": 0, "width": 200, "height": 200})); + let anchor = |s: &mut Session, params: serde_json::Value| { + let c = s.execute("comment.add", ¶ms).unwrap(); + let id = c["id"].as_u64().unwrap(); + s.doc().unwrap().comment(id).unwrap().anchor + }; + assert_eq!(anchor(&mut s, json!({"x": 150, "y": 50, "text": "t", "anchor": true})), Some(b)); + assert_eq!(anchor(&mut s, json!({"x": 150, "y": 50, "text": "t", "anchor": true, "within": a})), Some(a)); + let c = mk(&mut s, json!({"type": "frame", "x": 120, "y": 20, "width": 50, "height": 50, "parent": a})); + assert_eq!(anchor(&mut s, json!({"x": 130, "y": 30, "text": "t", "anchor": true, "within": a})), Some(c)); + // A point outside `within` ignores it. + assert_eq!(anchor(&mut s, json!({"x": 250, "y": 50, "text": "t", "anchor": true, "within": a})), Some(b)); + } + #[test] fn comment_thread_lifecycle_and_anchoring() { let mut s = Session::new(); diff --git a/crates/ui-egui/src/canvas/comments.rs b/crates/ui-egui/src/canvas/comments.rs index 1026ea47c..b12dd7907 100644 --- a/crates/ui-egui/src/canvas/comments.rs +++ b/crates/ui-egui/src/canvas/comments.rs @@ -833,7 +833,10 @@ fn escape(app: &mut FigApp, ctx: &egui::Context) -> bool { /// The draft's composer next to its pin at `at` (screen), kept inside `bounds`; posting adds the /// comment (on `page` when given, else the current one) and shows its thread. The editor and the /// presenter both draw it here. -fn draft_composer(app: &mut FigApp, ctx: &egui::Context, at: Pos2, bounds: Rect, page: Option) { +/// +/// `target` is the page and the presented frame when called from the presenter: the comment then +/// anchors within that frame. A comment that fails to post leaves the draft and its text. +fn draft_composer(app: &mut FigApp, ctx: &egui::Context, at: Pos2, bounds: Rect, target: Option<(NodeId, NodeId)>) { let Some((x, y, mut text)) = app.ui.comments.draft.clone() else { return }; let t = Tokens::get(ctx); let people = app.session.doc().map(|d| d.comment_people()).unwrap_or_default(); @@ -849,13 +852,17 @@ fn draft_composer(app: &mut FigApp, ctx: &egui::Context, at: Pos2, bounds: Rect, app.ui.comments.mention = pick; if post { let mut params = json!({"x": x, "y": y, "text": text.trim(), "anchor": true}); - if let (Some(page), Some(o)) = (page, params.as_object_mut()) { + if let (Some((page, frame)), Some(o)) = (target, params.as_object_mut()) { o.insert("page".into(), json!(page)); + o.insert("within".into(), json!(frame)); } - let r = app.run("comment.add", params); - app.ui.comments.draft = None; - if let Some(id) = r.and_then(|v| v.get("id").and_then(Value::as_u64)) { - app.ui.comments.show(id); + if let Some(r) = app.run("comment.add", params) { + app.ui.comments.draft = None; + if let Some(id) = r.get("id").and_then(Value::as_u64) { + app.ui.comments.show(id); + } + } else if let Some(d) = &mut app.ui.comments.draft { + d.2 = text; } } else if let Some(d) = &mut app.ui.comments.draft { d.2 = text; @@ -1262,7 +1269,7 @@ pub fn presenter(app: &mut FigApp, ctx: &egui::Context, frame: NodeId, place: cr ui.painter().circle_filled(rr.center(), 3.0, crate::theme::ACCENT_ON); }, ); - draft_composer(app, ctx, at, ctx.content_rect(), doc.page_of(frame)); + draft_composer(app, ctx, at, ctx.content_rect(), doc.page_of(frame).map(|page| (page, frame))); return; } match app.ui.comments.open.and_then(|o| pins.iter().find(|p| p.id == o)) { diff --git a/crates/ui-egui/src/comments_tests.rs b/crates/ui-egui/src/comments_tests.rs index f8d73f0d7..6cde5a6db 100644 --- a/crates/ui-egui/src/comments_tests.rs +++ b/crates/ui-egui/src/comments_tests.rs @@ -366,6 +366,62 @@ fn typing_c_into_the_presenters_draft_keeps_the_mode_on() { assert!(comments(&u)["draft"].is_array()); } +#[test] +fn a_comment_posted_in_the_overlap_anchors_to_the_presented_frame() { + let mut s = Session::new(); + let a = id(&s.execute("node.create", &json!({"type": "frame", "x": 50, "y": 40, "width": 400, "height": 300})).unwrap()); + // Stacked after `a` and overlapping its right part. + s.execute("node.create", &json!({"type": "frame", "x": 250, "y": 40, "width": 400, "height": 300})).unwrap(); + let mut u = Ui::new(s); + u.run("proto.present", json!({"start": a})); + key(&mut u, Key::C, Modifiers::NONE); + let screen = egui::Rect::from_min_size(pos2(0.0, 80.0), vec2(1440.0, 820.0)); + let frame_min = screen.center() - vec2(200.0, 150.0); + click(&mut u, frame_min + vec2(250.0, 90.0)); + u.frame(vec![Event::Text("overlap".into())]); + key(&mut u, Key::Enter, Modifiers::NONE); + assert_eq!(comment_count(&u), 1); + let c = u.app.session.doc().unwrap().comments[0].clone(); + assert_eq!(c.anchor, Some(a), "the comment is on the presented frame, not the one stacked over it"); + assert_eq!(comments(&u)["open"], c.id, "its pin shows in the presenter"); +} + +#[test] +fn a_draft_that_fails_to_post_keeps_its_text() { + let (mut h, _) = crate::live_tests::Harness::new(); + // A view-only link: the session refuses every edit, a comment too. + crate::live_server_tests::joined(&mut h, figcraft_engine::live::Access::View); + h.frames(2); + start_draft(&mut h.app, 150.0, 150.0); + h.frame(vec![]); + h.frame(vec![Event::Text("hello".into())]); + let k = |pressed| Event::Key { key: Key::Enter, physical_key: None, pressed, repeat: false, modifiers: Modifiers::NONE }; + h.frame(vec![k(true), k(false)]); + h.frames(2); + assert!(h.app.session.doc().unwrap().comments.is_empty(), "the session refused the comment"); + let draft = h.app.ui.comments.draft.clone(); + assert_eq!(draft.as_ref().map(|d| d.2.as_str()), Some("hello"), "the draft and its text stay: {draft:?}"); +} + +#[test] +fn a_press_held_into_the_presenters_comment_mode_does_not_navigate_on_release() { + let mut s = Session::new(); + let f = id(&s.execute("node.create", &json!({"type": "frame", "x": 50, "y": 40, "width": 400, "height": 300})).unwrap()); + let other = id(&s.execute("node.create", &json!({"type": "frame", "x": 700, "y": 0, "width": 400, "height": 300})).unwrap()); + let mut u = Ui::new(s); + u.run("proto.interaction.add", json!({"id": f, "trigger": {"type": "onClick"}, "action": {"type": "navigate", "dest": other}})); + u.run("proto.present", json!({"start": f})); + u.frames(2, Vec::new); + let screen = egui::Rect::from_min_size(pos2(0.0, 80.0), vec2(1440.0, 820.0)); + let at = screen.center() - vec2(200.0, 150.0) + vec2(120.0, 90.0); + u.button(at, true); + key(&mut u, Key::C, Modifiers::NONE); + assert_eq!(u.app.inspect()["presenter"]["comments"], true); + u.button(at, false); + u.frames(30, Vec::new); + assert_eq!(u.app.inspect()["presenter"]["current"], json!(f), "the press began before comment mode, so its release is no click"); +} + #[derive(Clone, Default)] struct Clip(Arc>>); diff --git a/crates/ui-egui/src/present/input.rs b/crates/ui-egui/src/present/input.rs index b8e94a22c..793283d7c 100644 --- a/crates/ui-egui/src/present/input.rs +++ b/crates/ui-egui/src/present/input.rs @@ -34,6 +34,19 @@ fn release_drag_scroll(p: &mut Presenter, l: &Layout, down: bool, velocity: Vec2 } } +/// Without clicks (the comment mode), forget a press in progress and what it began: a drag-scroll and a +/// transition following the pointer. +fn drop_press(p: &mut Presenter, clicks: bool) { + if clicks { + return; + } + p.press = None; + p.drag_scroll = None; + if let Some(a) = p.anim.as_mut() { + a.follow = None; + } +} + /// Scrolls the first of `targets` the wheel can still move, and smooths the wheel's speed over /// ~50 ms whatever the frame rate. fn wheel_scroll(p: &mut Presenter, l: &Layout, base: &Document, targets: Vec, wheel: Vec2, dt: f64) { @@ -194,6 +207,8 @@ pub(super) fn input(p: &mut Presenter, ctx: &egui::Context, l: &Layout, mode: In }); let keys = if keys_on { keys } else { Vec::new() }; let pressed = pressed && clicks; + // A press already in progress when the comment mode starts is dropped, so its release is no click. + drop_press(p, clicks); // Controller buttons pressed while a link notice shows are dropped, as keys are. let pads = std::mem::take(&mut p.pads); let pads = if p.link_notice.is_none() { pads } else { Vec::new() };