diff --git a/crates/ui-egui/src/board_line_tests.rs b/crates/ui-egui/src/board_line_tests.rs index 4303e7bd8..9b420b6ba 100644 --- a/crates/ui-egui/src/board_line_tests.rs +++ b/crates/ui-egui/src/board_line_tests.rs @@ -85,6 +85,28 @@ fn the_connector_bar_sets_its_ends_dashes_and_weight() { assert_eq!(stroke(&u).weight, 4.0, "the weight is its own step"); } +/// A connector drawn from right to left (#1744): its end point's menu comes first, so the menu on +/// the left sets the end on the left. +#[test] +fn the_left_menu_sets_the_left_end_of_a_connector_drawn_right_to_left() { + let mut s = Session::new(); + s.execute("file.newBoard", &json!({})).unwrap(); + let a = s.execute("board.sticky", &json!({"x": 400, "y": 300})).unwrap()["id"].clone(); + let b = s.execute("board.sticky", &json!({"x": 0, "y": 0})).unwrap()["id"].clone(); + let c = id(&s.execute("board.connector", &json!({"from": a, "to": b, "style": "elbow"})).unwrap()); + let mut u = Ui::new(s); + u.frame(vec![]); + u.frame(vec![]); + let stroke = |u: &Ui| u.app.session.doc().unwrap().node(c).unwrap().stroke.clone(); + // The two menus' places on the bar, named for a connector drawn left to right. + let (left, right) = (START, END); + pick(&mut u, left, "Circle"); + assert_eq!(stroke(&u).cap_end, StrokeCap::CircleFilled, "the left menu is the end's"); + assert_ne!(stroke(&u).cap_start, StrokeCap::CircleFilled); + pick(&mut u, right, "Diamond"); + assert_eq!(stroke(&u).cap_start, StrokeCap::DiamondFilled, "the right menu is the start's"); +} + #[test] fn the_colour_wheel_opens_the_picker_for_connectors_only() { let (mut u, c, b) = connector(); diff --git a/crates/ui-egui/src/canvas/board_bar.rs b/crates/ui-egui/src/canvas/board_bar.rs index 84ebede97..022f3f84c 100644 --- a/crates/ui-egui/src/canvas/board_bar.rs +++ b/crates/ui-egui/src/canvas/board_bar.rs @@ -135,6 +135,9 @@ struct Line { dashed: bool, /// Every selected connector is a mind map's branch: no line types. branch: bool, + /// The first connector's start lies to the right of its end on the canvas, so the bar shows its + /// end point's menu first (#1744). + start_right: bool, ids: Vec, } @@ -171,6 +174,7 @@ fn has(app: &FigApp) -> Option { weight: s.weight, dashed: !s.dashes.is_empty(), branch: true, + start_right: start_right(&ds.doc, c), ids: Vec::new(), }); line.branch &= c.branch; @@ -494,17 +498,26 @@ fn line_part(app: &mut FigApp, ui: &mut Ui, line: &Line, at: Pos2, t: &Tokens) { } x = divider(ui, t, x - 2.0, at.y); } - for (start, cap) in [(true, line.start), (false, line.end)] { - cap_menu(app, ui, t, pos2(x, at.y), start, cap); + // Each end's menu sits on the side where that end lies on the canvas, so the left menu sets + // the left end whichever way the connector was drawn (#1744). + let ends = if line.start_right { [(false, line.end), (true, line.start)] } else { [(true, line.start), (false, line.end)] }; + for (i, (start, cap)) in ends.into_iter().enumerate() { + cap_menu(app, ui, t, pos2(x, at.y), start, cap, i == 0); x += CAP_W + 2.0; } x = divider(ui, t, x - 2.0, at.y); style_menu(app, ui, t, pos2(x, at.y), line); } +/// Whether connector `c`'s start lies to the right of its end on the canvas. +fn start_right(doc: &figcraft_doc::Document, c: &figcraft_doc::Connector) -> bool { + let points = figcraft_engine::board::route(doc, c).unwrap_or_default(); + matches!((points.first(), points.last()), (Some(a), Some(b)) if a.x > b.x) +} + /// The menu of a connector's start (`start`) or end point at `at` (its left edge and middle), -/// showing `cap`. -fn cap_menu(app: &mut FigApp, ui: &mut Ui, t: &Tokens, at: Pos2, start: bool, cap: StrokeCap) { +/// showing `cap`, its pictures pointing left when it is the left one of the two (`left`). +fn cap_menu(app: &mut FigApp, ui: &mut Ui, t: &Tokens, at: Pos2, start: bool, cap: StrokeCap, left: bool) { let r = Rect::from_min_size(pos2(at.x, at.y - CELL / 2.0), vec2(CAP_W, CELL)); let label = if start { tr("Start point") } else { tr("End point") }; let resp = ui.interact(r, ui.scope_id().with(("bb_cap", start)), Sense::click()).on_hover_text(label); @@ -514,12 +527,12 @@ fn cap_menu(app: &mut FigApp, ui: &mut Ui, t: &Tokens, at: Pos2, start: bool, ca } // The picture ends 14 before the rectangle it is given: 4 in from this cell's left edge. let art = Rect::from_min_max(pos2(r.min.x, r.min.y), pos2(r.min.x + 62.0, r.max.y)); - crate::right::cap_art(ui.painter(), art, t.icon, cap, start); + crate::right::cap_art(ui.painter(), art, t.icon, cap, left); icons::paint(ui.painter(), Rect::from_center_size(pos2(r.max.x - 7.0, at.y), vec2(12.0, 12.0)), Icon::Chevron, t.icon_secondary); let key = if start { "startCap" } else { "endCap" }; crate::right::menu_popup(&resp).show(|ui| { for c in StrokeCap::ALL { - let art = |p: &egui::Painter, row: Rect, ink: Color32| crate::right::cap_art(p, row, ink, c, start); + let art = |p: &egui::Painter, row: Rect, ink: Color32| crate::right::cap_art(p, row, ink, c, left); if crate::right::menu_item_with(ui, tr(c.label()), "", crate::right::CAP_ART_W, art) { app.run("board.setStyle", json!({key: c})); ui.close();