DesignCraft: An open-source page and layout design editor written by scratch in pure Rust
Merge remote-tracking branch 'origin/main' into direct-select-anchor-points
moppeeer00a64e594323
diff --cc crates/tools/src/select.rsindex 3f36fea59,033e1a97c..2cc3f0078--- a/crates/tools/src/select.rs+++ b/crates/tools/src/select.rs@@@ -751,370 -683,336 +751,379 @@@ fn positive_pad(v: f64) -> f64if v.is_finite() && v > 0.0 { v } else { 0.0 }}- impl Tool for SelectionTool {- fn id(&self) -> &'static str {- if self.direct { "directSelection" } else { "selection" }- }-- fn pointer(&mut self, cx: &ToolContext, ev: &PointerEvent) -> Vec<Action> {+ impl SelectionTool {+ fn pointer_down(&mut self, cx: &ToolContext, ev: &PointerEvent) -> Vec<Action> {let p = ev.pos;- // Selecting something else ends live-corner editing.- if let Some(id) = self.live- && cx.selection.items != [id]- {- self.live = None;- }- if !self.direct- && let Some(actions) = self.thread_pointer(cx, ev)- {- return actions;- }- // The second press of a double click on a selection handle: Fit Frame to Content, along- // that handle's side (a corner fits both).- if !self.direct- && matches!(ev.kind, PointerKind::Down | PointerKind::DoubleClick)- && ev.click_count() == 2- && let Some(h) = handle_at(cx, p)- {- self.drag = Drag::None;- return vec![Action::Exec("object.fit".into(), json!({"mode": "fitFrameToContent", "axis": fitting_axis(cx, h)}))];- }- // The second press of a double click on a text frame: edit its text.- if matches!(ev.kind, PointerKind::Down | PointerKind::DoubleClick)- && ev.click_count() == 2- && let Some(actions) = edit_text_at(cx, p)++ self.shift_release = None;+ self.key_release = None;+ // Type on a path: a bracket moves where the text starts or ends.+ if !ev.mods.shift+ && let Some((id, end)) = self.bracket_at(cx, p){- self.drag = Drag::None;- return actions;+ self.drag = Drag::Bracket { id, end };+ return vec![Action::Begin("Move Bracket".into())];}- // The second press of a double click on a frame with content: toggle between the frame and- // its content.- if !self.direct- && matches!(ev.kind, PointerKind::Down | PointerKind::DoubleClick)- && ev.click_count() == 2- && let Some(id) = cx.hit(p).map(|(_, id)| cx.doc.top_level_of(id).unwrap_or(id))- && cx.doc.item(id).is_some_and(holds_content)+ if self.direct+ && let Some((id, si, ai, handle)) = anchor_at(cx, p){- self.drag = Drag::None;- let content = !(cx.selection.content && cx.selection.contains(id));- return vec![Action::Exec("selection.set".into(), json!({"ids": [id.0], "content": content}))];- }- match ev.kind {- PointerKind::Move => {- self.hover_handle = handle_at(cx, p);- vec![]- }- PointerKind::Down => {- self.shift_release = None;- self.key_release = None;- // Type on a path: a bracket moves where the text starts or ends.- if !ev.mods.shift- && let Some((id, end)) = self.bracket_at(cx, p)- {- self.drag = Drag::Bracket { id, end };- return vec![Action::Begin("Move Bracket".into())];- }- if self.direct- && let Some((id, si, ai, handle)) = anchor_at(cx, p)- {- let at = anchor_spread_point(cx, id, si, ai);+ let at = anchor_spread_point(cx, id, si, ai);- self.drag = Drag::Anchor { id, si, ai, handle, start: p, at };- return vec![Action::Begin(if handle.is_some() { "Move Direction Handle".into() } else { "Move Anchor".into() })];+ let mut out = vec![];+ let mut others = vec![];+ if handle.is_none() {+ // InDesign's anchor point selection: a click selects the point, Shift+click+ // adds it or takes it away, and dragging a selected point moves all of them.+ let me = (ItemId(id), si, ai);+ let mut points = cx.selection.anchors.clone();+ let was = points.contains(&me);+ if ev.mods.shift && was {+ // Kept selected so a Shift-drag can constrain it; a click still takes it+ // away, on release, as for objects.+ self.anchor_release = Some(me);+ } else if ev.mods.shift {+ points.push(me);+ } else if !was {+ points = vec![me];+ }+ if points != cx.selection.anchors {+ out.push(Action::Exec("selection.set".into(), anchor_selection(cx, &points)));+ }+ others = points.iter().filter(|a| **a != me).map(|(i, s, a)| (i.0, *s, *a)).collect();+ }+ let label = if handle.is_some() {+ "Move Direction Handle"+ } else if others.is_empty() {+ "Move Anchor"+ } else {+ "Move Anchors"+ };+ self.drag = Drag::Anchor { id, si, ai, handle, start: p, at, others };+ out.push(Action::Begin(label.into()));+ return out;+ }+ // As in InDesign, the Direct Selection tool pressed on a corner or anchor of an object+ // that isn't selected selects it and moves just that point (Chris Nielsen's "The+ // InDesign Toolbox", 10:10: click outside, then pull a frame's corner).+ if self.direct+ && !ev.mods.shift+ && let Some((_, hit)) = cx.hit(p)+ && !cx.selection.contains(hit)+ && let Some((id, si, ai, None)) = anchor_at_in(cx, &[hit], p)+ {+ let at = anchor_spread_point(cx, id, si, ai);+ self.drag = Drag::Anchor { id, si, ai, handle: None, start: p, at, others: vec![] };+ return vec![Action::Exec("selection.set".into(), json!({"ids": [id], "content": true})), Action::Begin("Move Anchor".into())];- }- // A selected placed graphic's own handles resize it inside its frame (the- // frame's anchors, under Direct Selection, come first).- if let Some((id, h)) = content_handle_at(cx, p)- && let (Some(loc), Some((_, b))) = (cx.doc.find(id), selected_graphic(cx))- {- let turns = cx.layout.slot(loc.spread).map_or(0, |s| s.rotation as usize);- let handle = (h + 8 - 2 * turns) % 8;- let from = cx.layout.xf(loc.spread).inverse().transform_rect_bbox(b);- self.drag = Drag::ContentResize { id, handle, from, spread: loc.spread };- return vec![Action::Begin("Resize Content".into())];- }- if let Some((frame, corner)) = self.diamond_at(cx, p) {- if ev.mods.alt {- self.drag = Drag::None;- let opts = frame.cycled(corner, ev.mods.shift);- return vec![Action::Exec("object.cornerOptions".into(), corners::params(frame.id, &opts))];+ }+ // A selected placed graphic's own handles resize it inside its frame (the+ // frame's anchors, under Direct Selection, come first).+ if let Some((id, h)) = content_handle_at(cx, p)+ && let (Some(loc), Some((_, b))) = (cx.doc.find(id), selected_graphic(cx))+ {+ let turns = cx.layout.slot(loc.spread).map_or(0, |s| s.rotation as usize);+ let handle = (h + 8 - 2 * turns) % 8;+ let from = cx.layout.xf(loc.spread).inverse().transform_rect_bbox(b);+ self.drag = Drag::ContentResize { id, handle, from, spread: loc.spread };+ return vec![Action::Begin("Resize Content".into())];+ }+ if let Some((frame, corner)) = self.diamond_at(cx, p) {+ if ev.mods.alt {+ self.drag = Drag::None;+ let opts = frame.cycled(corner, ev.mods.shift);+ return vec![Action::Exec("object.cornerOptions".into(), corners::params(frame.id, &opts))];+ }+ self.drag = Drag::Corner { frame, corner, start: p };+ return vec![Action::Begin("Corner Options".into())];+ }+ if let Some(frame) = self.widget_at(cx, p) {+ self.live = Some(frame.id);+ self.drag = Drag::None;+ return vec![Action::Handled];+ }+ if !self.direct+ && handle_at(cx, p).is_none()+ && let Some(center) = rotate_zone(cx, p)+ {+ self.drag = Drag::Rotate { center, start_angle: (p - center).atan2(), start_rotation: crate::snap::reference_rotation(cx) };+ return vec![Action::Begin("Rotate".into())];+ }+ if let Some(h) = handle_at(cx, p).filter(|_| !self.direct)+ && let (Some(b), Some(first)) = (cx.selection_bounds(), cx.selection.items.first())+ && let Some(loc) = cx.doc.find(*first)+ {+ // On a turned spread the canvas handle is another one of the spread's.+ let turns = cx.layout.slot(loc.spread).map_or(0, |s| s.rotation as usize);+ let handle = (h + 8 - 2 * turns) % 8;+ self.drag = Drag::Resize { handle, start: p, from: cx.layout.xf(loc.spread).inverse().transform_rect_bbox(b), spread: loc.spread };+ return vec![Action::Begin("Resize".into())];+ }+ // The content grabber: select the frame's content; a drag moves it.+ if !self.direct+ && !ev.mods.shift+ && let Some(id) = grabber_at(cx, p)+ {+ self.drag = Drag::Pending { start: p, hit: true, content: true };+ if cx.selection.items == [id] && cx.selection.content {+ return vec![];+ }+ return vec![Action::Exec("selection.set".into(), json!({"ids": [id.0], "content": true}))];+ }+ // Cmd+Shift-click a parent item on a page: override it there and select the copy.+ if ev.mods.cmd+ && ev.mods.shift+ && cx.hit(p).is_none()+ && let Some((page, id)) = cx.hit_parent_on_page(p)+ {+ self.drag = Drag::Pending { start: p, hit: true, content: false };+ return vec![Action::Exec("layout.overrideParentItems".into(), json!({"page": page, "ids": [id.0]}))];+ }+ match cx.hit(p) {+ Some((sr, id)) => {+ let id = if self.direct { id } else { cx.doc.top_level_of(id).unwrap_or(id) };+ let mut out = vec![];+ if ev.mods.shift {+ if cx.selection.contains(id) {+ // Keep it selected so a Shift-drag can constrain. A click still toggles, on release.+ self.shift_release = Some(id.0);+ } else {+ out.push(Action::Exec("selection.toggle".into(), json!({"id": id.0})));}- self.drag = Drag::Corner { frame, corner, start: p };- return vec![Action::Begin("Corner Options".into())];+ } else if !cx.selection.contains(id) || (self.direct && !cx.selection.content) {+ out.push(Action::Exec("selection.set".into(), json!({"ids": [id.0], "content": self.direct})));+ } else if !self.direct && cx.selection.items.len() > 1 {+ // InDesign: a click on one of several selected objects makes it the+ // key object, on release so that a drag still moves them all.+ self.key_release = Some(id.0);}- if let Some(frame) = self.widget_at(cx, p) {- self.live = Some(frame.id);- self.drag = Drag::None;- return vec![Action::Handled];- }- if !self.direct- && handle_at(cx, p).is_none()- && let Some(center) = rotate_zone(cx, p)- {- self.drag = Drag::Rotate { center, start_angle: (p - center).atan2(), start_rotation: crate::snap::reference_rotation(cx) };- return vec![Action::Begin("Rotate".into())];+ let content = self.direct || on_selected_content(cx, p);+ self.drag = Drag::Pending { start: p, hit: true, content };+ let _ = sr;+ out+ }+ None => {+ self.live = None;+ self.drag = Drag::Pending { start: p, hit: false, content: false };+ if ev.mods.shift { vec![] } else { vec![Action::Exec("selection.set".into(), json!({"ids": []}))] }+ }+ }+ }++ fn pointer_drag_move(&mut self, cx: &ToolContext, ev: &PointerEvent, drag: Drag) -> Vec<Action> {+ let Drag::Move { start, origin_spread, bounds0, exclude, content } = drag else { return vec![] };+ let p = ev.pos;++ let mut d: Vec2 = p - start;+ let mut screen_x_locked = false;+ let mut screen_y_locked = false;+ if ev.mods.shift {+ if d.x.abs() > d.y.abs() {+ d.y = 0.0;+ screen_y_locked = true;+ } else {+ d.x = 0.0;+ screen_x_locked = true;+ }+ }+ // Snap on the spread under the pointer. Shift uses that spread's view rotation.+ // Content stays in its frame, on the frame's spread.+ let target = if content { origin_spread } else { cx.layout.spread_at(p).map(|(s, _)| s).unwrap_or(origin_spread) };+ // 180 degrees keeps the screen axis. 90 and 270 swap it into the other spread axis.+ let odd_turn = cx.layout.slot(target).is_some_and(|slot| slot.rotation % 2 == 1);+ let spread_x_locked = if odd_turn { screen_y_locked } else { screen_x_locked };+ let spread_y_locked = if odd_turn { screen_x_locked } else { screen_y_locked };+ let x_edges = if spread_x_locked { [false; 3] } else { [true; 3] };+ let y_edges = if spread_y_locked { [false; 3] } else { [true; 3] };+ self.guides.clear();+ // The movement in the origin spread's coordinates (its view may be turned).+ let mut ds = cx.layout.delta_to_spread(origin_spread, d);+ let b0s = bounds0.map(|b| cx.layout.xf(origin_spread).inverse().transform_rect_bbox(b));+ let q0 = b0s.map_or(Point::ORIGIN, |b| Point::new(b.x0, b.y0));+ // Same corner conversion transform.move already uses when the spread changes.+ let mut landed = cx.layout.to_spread(target, cx.layout.to_canvas(origin_spread, q0 + ds));+ // Command suspends snapping. A Shift-locked spread axis is not offered.+ if !ev.mods.cmd+ && cx.snap.any()+ && let Some(b0) = b0s+ {+ let proposed = if target == origin_spread {+ b0 + ds+ } else {+ let shift = landed - q0;+ Rect::new(b0.x0 + shift.x, b0.y0 + shift.y, b0.x1 + shift.x, b0.y1 + shift.y)+ };+ let hit = crate::snap::snap(+ cx,+ SnapRequest {+ spread: target,+ gesture: Gesture::Move,+ rect: proposed,+ x_edges,+ y_edges,+ exclude: &exclude,+ exclude_guides: &[],+ copying: ev.mods.alt && !content,+ lengths: [None, None],+ angle: None,+ radius: 0.0,+ pointer: cx.layout.to_spread(target, p),+ },+ );+ if target == origin_spread {+ if x_edges != [false, false, false] && hit.delta.x.is_finite() {+ ds.x += hit.delta.x;}- if let Some(h) = handle_at(cx, p).filter(|_| !self.direct)- && let (Some(b), Some(first)) = (cx.selection_bounds(), cx.selection.items.first())- && let Some(loc) = cx.doc.find(*first)- {- // On a turned spread the canvas handle is another one of the spread's.- let turns = cx.layout.slot(loc.spread).map_or(0, |s| s.rotation as usize);- let handle = (h + 8 - 2 * turns) % 8;- self.drag =- Drag::Resize { handle, start: p, from: cx.layout.xf(loc.spread).inverse().transform_rect_bbox(b), spread: loc.spread };- return vec![Action::Begin("Resize".into())];+ if y_edges != [false, false, false] && hit.delta.y.is_finite() {+ ds.y += hit.delta.y;}- // The content grabber: select the frame's content; a drag moves it.- if !self.direct- && !ev.mods.shift- && let Some(id) = grabber_at(cx, p)- {- self.drag = Drag::Pending { start: p, hit: true, content: true };- if cx.selection.items == [id] && cx.selection.content {- return vec![];- }- return vec![Action::Exec("selection.set".into(), json!({"ids": [id.0], "content": true}))];+ } else {+ if x_edges != [false, false, false] && hit.delta.x.is_finite() {+ landed.x += hit.delta.x;}- // Cmd+Shift-click a parent item on a page: override it there and select the copy.- if ev.mods.cmd- && ev.mods.shift- && cx.hit(p).is_none()- && let Some((page, id)) = cx.hit_parent_on_page(p)- {- self.drag = Drag::Pending { start: p, hit: true, content: false };- return vec![Action::Exec("layout.overrideParentItems".into(), json!({"page": page, "ids": [id.0]}))];+ if y_edges != [false, false, false] && hit.delta.y.is_finite() {+ landed.y += hit.delta.y;}- match cx.hit(p) {- Some((sr, id)) => {- let id = if self.direct { id } else { cx.doc.top_level_of(id).unwrap_or(id) };- let mut out = vec![];- if ev.mods.shift {- if cx.selection.contains(id) {- // Keep it selected so a Shift-drag can constrain. A click still toggles, on release.- self.shift_release = Some(id.0);- } else {- out.push(Action::Exec("selection.toggle".into(), json!({"id": id.0})));- }- } else if !cx.selection.contains(id) || (self.direct && !cx.selection.content) {- out.push(Action::Exec("selection.set".into(), json!({"ids": [id.0], "content": self.direct})));- } else if !self.direct && cx.selection.items.len() > 1 {- // InDesign: a click on one of several selected objects makes it the- // key object, on release so that a drag still moves them all.- self.key_release = Some(id.0);- }- let content = self.direct || on_selected_content(cx, p);- self.drag = Drag::Pending { start: p, hit: true, content };- let _ = sr;- out- }- None => {- self.live = None;- self.drag = Drag::Pending { start: p, hit: false, content: false };- if ev.mods.shift { vec![] } else { vec![Action::Exec("selection.set".into(), json!({"ids": []}))] }- }+ }+ self.guides = hit.guides;+ }+ // Same spread: dx, dy are the origin spread translation. Crossing: the corner+ // conversion, so the reparented item lands on the snapped target point.+ let (dx, dy) = if target == origin_spread {+ (ds.x, ds.y)+ } else {+ let dt = landed - q0;+ (dt.x, dt.y)+ };+ if content {+ let ids: Vec<u64> = exclude.iter().map(|i| i.0).collect();+ return vec![Action::Preview("transform.move".into(), json!({"dx": dx, "dy": dy, "ids": ids, "content": true}))];+ }+ let mut params = json!({"dx": dx, "dy": dy, "copy": ev.mods.alt});+ if target != origin_spread {+ params = json!({"dx": dx, "dy": dy, "copy": ev.mods.alt, "toSpread": spread_json(target)});+ }+ vec![Action::Preview("transform.move".into(), params)]+ }++ fn pointer_drag(&mut self, cx: &ToolContext, ev: &PointerEvent) -> Vec<Action> {+ let p = ev.pos;+ match self.drag.clone() {+ Drag::Bracket { id, end } => {+ let Some((warp, xf, _, _)) = path_type_of(cx, id) else { return vec![] };+ if xf.determinant().abs() < 1e-12 {+ return vec![];}+ let s = warp.nearest(xf.inverse() * p);+ vec![Action::Preview("type.pathOptions".into(), json!({"id": id.0, if end { "end" } else { "start" }: s}))]}- PointerKind::Drag => match self.drag.clone() {- Drag::Bracket { id, end } => {- let Some((warp, xf, _, _)) = path_type_of(cx, id) else { return vec![] };- if xf.determinant().abs() < 1e-12 {- return vec![];- }- let s = warp.nearest(xf.inverse() * p);- vec![Action::Preview("type.pathOptions".into(), json!({"id": id.0, if end { "end" } else { "start" }: s}))]+ Drag::Pending { start, hit, content } => {+ if (p - start).hypot() < cx.tol(3.0) {+ return vec![];}- Drag::Pending { start, hit, content } => {- if (p - start).hypot() < cx.tol(3.0) {- return vec![];+ self.shift_release = None;+ self.key_release = None;+ if hit && !cx.selection.items.is_empty() {+ // Direct Selection on items without content moves (or duplicates) them whole.+ let content = content && cx.selection.items.iter().any(|i| cx.doc.item(*i).is_some_and(holds_content));+ let sr = cx.selection.items.first().and_then(|i| cx.doc.find(*i)).map(|l| l.spread).unwrap_or(SpreadRef::Doc(0));+ let exclude = cx.selection.items.clone();+ let bounds0 = if content { content_bounds(cx) } else { cx.selection_visible_bounds() };+ self.drag = Drag::Move { start, origin_spread: sr, bounds0, exclude: exclude.clone(), content };+ let mut v = vec![];+ // Dragging a content selection by its frame moves the frame: select it.+ if !content && cx.selection.content {+ let ids: Vec<u64> = exclude.iter().map(|i| i.0).collect();+ v.push(Action::Exec("selection.set".into(), json!({"ids": ids})));}- self.shift_release = None;- self.key_release = None;- if hit && !cx.selection.items.is_empty() {- // Direct Selection on items without content moves (or duplicates) them whole.- let content = content && cx.selection.items.iter().any(|i| cx.doc.item(*i).is_some_and(holds_content));- let sr = cx.selection.items.first().and_then(|i| cx.doc.find(*i)).map(|l| l.spread).unwrap_or(SpreadRef::Doc(0));- let exclude = cx.selection.items.clone();- let bounds0 = if content { content_bounds(cx) } else { cx.selection_visible_bounds() };- self.drag = Drag::Move { start, origin_spread: sr, bounds0, exclude: exclude.clone(), content };- let mut v = vec![];- // Dragging a content selection by its frame moves the frame: select it.- if !content && cx.selection.content {- let ids: Vec<u64> = exclude.iter().map(|i| i.0).collect();- v.push(Action::Exec("selection.set".into(), json!({"ids": ids})));- }- let label = if content {- "Move Content"- } else if ev.mods.alt {- "Duplicate"- } else {- "Move"- };- v.push(Action::Begin(label.into()));- v.extend(self.pointer(cx, ev));- v+ let label = if content {+ "Move Content"+ } else if ev.mods.alt {+ "Duplicate"} else {- self.drag = Drag::Marquee { start, cur: p };- vec![]- }+ "Move"+ };+ v.push(Action::Begin(label.into()));+ v.extend(self.pointer(cx, ev));+ v+ } else {+ self.drag = Drag::Marquee { start, cur: p };+ vec![]}- Drag::Move { start, origin_spread, bounds0, exclude, content } => {- let mut d: Vec2 = p - start;- let mut screen_x_locked = false;- let mut screen_y_locked = false;+ }+ movement @ Drag::Move { .. } => self.pointer_drag_move(cx, ev, movement),+ Drag::Resize { handle, start, from, spread } => {+ let _ = start;+ let pointer = cx.layout.to_spread(spread, p);+ let raw = resize_edges_rect(from, handle, pointer);+ let mut to = resize_rect(from, handle, pointer, ev.mods);+ // Space while dragging: Live Distribute (several objects keep their size).+ let distribute = ev.mods.space && cx.selection.items.len() > 1;+ self.guides.clear();+ // Command still scales content. It does not skip the snap.+ if !distribute && cx.snap.any() {+ let (mut x_edges, mut y_edges) = resize_edge_flags(handle);+ let driver_x = drives_x(handle, from, raw);if ev.mods.shift {- if d.x.abs() > d.y.abs() {- d.y = 0.0;- screen_y_locked = true;- } else {- d.x = 0.0;- screen_x_locked = true;- }- }- // Snap on the spread under the pointer. Shift uses that spread's view rotation.- // Content stays in its frame, on the frame's spread.- let target = if content { origin_spread } else { cx.layout.spread_at(p).map(|(s, _)| s).unwrap_or(origin_spread) };- // 180 degrees keeps the screen axis. 90 and 270 swap it into the other spread axis.- let odd_turn = cx.layout.slot(target).is_some_and(|slot| slot.rotation % 2 == 1);- let spread_x_locked = if odd_turn { screen_y_locked } else { screen_x_locked };- let spread_y_locked = if odd_turn { screen_x_locked } else { screen_y_locked };- let x_edges = if spread_x_locked { [false; 3] } else { [true; 3] };- let y_edges = if spread_y_locked { [false; 3] } else { [true; 3] };- self.guides.clear();- // The movement in the origin spread's coordinates (its view may be turned).- let mut ds = cx.layout.delta_to_spread(origin_spread, d);- let b0s = bounds0.map(|b| cx.layout.xf(origin_spread).inverse().transform_rect_bbox(b));- let q0 = b0s.map_or(Point::ORIGIN, |b| Point::new(b.x0, b.y0));- // Same corner conversion transform.move already uses when the spread changes.- let mut landed = cx.layout.to_spread(target, cx.layout.to_canvas(origin_spread, q0 + ds));- // Command suspends snapping. A Shift-locked spread axis is not offered.- if !ev.mods.cmd- && cx.snap.any()- && let Some(b0) = b0s- {- let proposed = if target == origin_spread {- b0 + ds- } else {- let shift = landed - q0;- Rect::new(b0.x0 + shift.x, b0.y0 + shift.y, b0.x1 + shift.x, b0.y1 + shift.y)- };- let hit = crate::snap::snap(- cx,- SnapRequest {- spread: target,- gesture: Gesture::Move,- rect: proposed,- x_edges,- y_edges,- exclude: &exclude,- exclude_guides: &[],- copying: ev.mods.alt && !content,- lengths: [None, None],- angle: None,- radius: 0.0,- pointer: cx.layout.to_spread(target, p),- },- );- if target == origin_spread {- if x_edges != [false, false, false] && hit.delta.x.is_finite() {- ds.x += hit.delta.x;- }- if y_edges != [false, false, false] && hit.delta.y.is_finite() {- ds.y += hit.delta.y;- }+ if driver_x {+ y_edges = [false, false, false];} else {- if x_edges != [false, false, false] && hit.delta.x.is_finite() {- landed.x += hit.delta.x;- }- if y_edges != [false, false, false] && hit.delta.y.is_finite() {- landed.y += hit.delta.y;- }+ x_edges = [false, false, false];}- self.guides = hit.guides;- }- // Same spread: dx, dy are the origin spread translation. Crossing: the corner- // conversion, so the reparented item lands on the snapped target point.- let (dx, dy) = if target == origin_spread {- (ds.x, ds.y)- } else {- let dt = landed - q0;- (dt.x, dt.y)- };- if content {- let ids: Vec<u64> = exclude.iter().map(|i| i.0).collect();- return vec![Action::Preview("transform.move".into(), json!({"dx": dx, "dy": dy, "ids": ids, "content": true}))];}- let mut params = json!({"dx": dx, "dy": dy, "copy": ev.mods.alt});- if target != origin_spread {- params = json!({"dx": dx, "dy": dy, "copy": ev.mods.alt, "toSpread": spread_json(target)});- }- vec![Action::Preview("transform.move".into(), params)]+ let exclude = cx.selection.items.clone();+ let (pad_x, pad_y) = single_stroke_pad(cx);+ let proposed = to;+ let mut hit = crate::snap::snap(+ cx,+ SnapRequest {+ spread,+ gesture: Gesture::Resize,+ rect: proposed,+ x_edges,+ y_edges,+ exclude: &exclude,+ exclude_guides: &[],+ copying: false,+ lengths: [axis_length(x_edges, proposed.width(), pad_x), axis_length(y_edges, proposed.height(), pad_y)],+ angle: None,+ radius: 0.0,+ pointer,+ },+ );+ to = finish_snapped_resize(from, handle, proposed, ev.mods, driver_x, (x_edges, y_edges), &hit);+ crate::snap::lay_dimension_guides(&mut hit.guides, cx.layout.xf(spread), proposed, to, x_edges, y_edges, hit.length_delta);+ self.guides = hit.guides;}- Drag::Resize { handle, start, from, spread } => {- let _ = start;- let pointer = cx.layout.to_spread(spread, p);- let raw = resize_edges_rect(from, handle, pointer);- let mut to = resize_rect(from, handle, pointer, ev.mods);- // Space while dragging: Live Distribute (several objects keep their size).- let distribute = ev.mods.space && cx.selection.items.len() > 1;- self.guides.clear();- // Command still scales content. It does not skip the snap.- if !distribute && cx.snap.any() {- let (mut x_edges, mut y_edges) = resize_edge_flags(handle);- let driver_x = drives_x(handle, from, raw);- if ev.mods.shift {- if driver_x {- y_edges = [false, false, false];- } else {- x_edges = [false, false, false];- }- }+ vec![Action::Preview(+ "transform.resize".into(),+ json!({"from": rect_json(from), "to": rect_json(to), "content": ev.mods.cmd, "distribute": distribute}),+ )]+ }+ Drag::ContentResize { id, handle, from, spread } => {+ let to = resize_rect(from, handle, cx.layout.to_spread(spread, p), ev.mods);+ vec![Action::Preview("transform.set".into(), json!({"ids": [id.0], "content": true, "bounds": rect_json(to)}))]+ }+ Drag::Marquee { start, .. } => {+ self.drag = Drag::Marquee { start, cur: p };+ vec![]+ }+ Drag::Place { .. } => vec![],+ Drag::Corner { frame, corner, start } => {+ let Some(d) = frame.drag_delta(corner, start, p) else { return vec![] };+ let opts = frame.dragged(corner, frame.size(corner) + d, ev.mods.shift);+ vec![Action::Preview("object.cornerOptions".into(), corners::params(frame.id, &opts))]+ }+ Drag::Rotate { center, start_angle, start_rotation } => {+ let mut a = ((p - center).atan2() - start_angle).to_degrees();+ self.guides.clear();+ if ev.mods.shift {+ // 45 degree steps. The rotation pass does not run while Shift is held.+ a = (a / 45.0).round() * 45.0;+ } else if let Some(start) = start_rotation {+ let raw = -a;+ if let Some(resulting) = crate::snap::folded_rotation(start + raw) {+ let spread = cx.selection.items.first().and_then(|id| cx.doc.find(*id)).map(|loc| loc.spread).unwrap_or(SpreadRef::Doc(0));let exclude = cx.selection.items.clone();- let (pad_x, pad_y) = single_stroke_pad(cx);- let proposed = to;- let mut hit = crate::snap::snap(+ // The rect is unused by the angle pass. It only has to be finite.+ let hit = crate::snap::snap(cx,SnapRequest {spread,@@@ -1125,137 -1023,127 +1134,142 @@@exclude: &exclude,exclude_guides: &[],copying: false,- lengths: [axis_length(x_edges, proposed.width(), pad_x), axis_length(y_edges, proposed.height(), pad_y)],- angle: None,- radius: 0.0,- pointer,+ lengths: [None, None],+ angle: Some(resulting),+ radius: (p - center).hypot(),+ pointer: cx.layout.to_spread(spread, p),},);- to = finish_snapped_resize(from, handle, proposed, ev.mods, driver_x, (x_edges, y_edges), &hit);- crate::snap::lay_dimension_guides(&mut hit.guides, cx.layout.xf(spread), proposed, to, x_edges, y_edges, hit.length_delta);- self.guides = hit.guides;- }- vec![Action::Preview(- "transform.resize".into(),- json!({"from": rect_json(from), "to": rect_json(to), "content": ev.mods.cmd, "distribute": distribute}),- )]- }- Drag::ContentResize { id, handle, from, spread } => {- let to = resize_rect(from, handle, cx.layout.to_spread(spread, p), ev.mods);- vec![Action::Preview("transform.set".into(), json!({"ids": [id.0], "content": true, "bounds": rect_json(to)}))]- }- Drag::Marquee { start, .. } => {- self.drag = Drag::Marquee { start, cur: p };- vec![]- }- Drag::Place { .. } => vec![],- Drag::Corner { frame, corner, start } => {- let Some(d) = frame.drag_delta(corner, start, p) else { return vec![] };- let opts = frame.dragged(corner, frame.size(corner) + d, ev.mods.shift);- vec![Action::Preview("object.cornerOptions".into(), corners::params(frame.id, &opts))]- }- Drag::Rotate { center, start_angle, start_rotation } => {- let mut a = ((p - center).atan2() - start_angle).to_degrees();- self.guides.clear();- if ev.mods.shift {- // 45 degree steps. The rotation pass does not run while Shift is held.- a = (a / 45.0).round() * 45.0;- } else if let Some(start) = start_rotation {- let raw = -a;- if let Some(resulting) = crate::snap::folded_rotation(start + raw) {- let spread =- cx.selection.items.first().and_then(|id| cx.doc.find(*id)).map(|loc| loc.spread).unwrap_or(SpreadRef::Doc(0));- let exclude = cx.selection.items.clone();- // The rect is unused by the angle pass. It only has to be finite.- let hit = crate::snap::snap(- cx,- SnapRequest {- spread,- gesture: Gesture::Rotate,- rect: Rect::new(0.0, 0.0, 1.0, 1.0),- x_edges: [false, false, false],- y_edges: [false, false, false],- exclude: &exclude,- exclude_guides: &[],- copying: false,- lengths: [None, None],- angle: Some(resulting),- radius: (p - center).hypot(),- pointer: cx.layout.to_spread(spread, p),- },- );- if let Some(snapped) = hit.angle- && let Some(delta) = crate::snap::rotation_command_delta(start, snapped, raw)- {- a = -delta;- }- self.guides = hit.guides;+ if let Some(snapped) = hit.angle+ && let Some(delta) = crate::snap::rotation_command_delta(start, snapped, raw)+ {+ a = -delta;}+ self.guides = hit.guides;}- // Screen y points down: a positive screen angle is clockwise.- vec![Action::Preview("transform.rotate".into(), json!({"angle": -a}))]}+ // Screen y points down: a positive screen angle is clockwise.+ vec![Action::Preview("transform.rotate".into(), json!({"angle": -a}))]+ }- Drag::Anchor { id, si, ai, handle, start, at } => {- let d = p - start;+ Drag::Anchor { id, si, ai, handle, start, at, others } => {+ let mut d = p - start;+ if d.hypot() >= cx.tol(3.0) {+ self.anchor_release = None;+ } else if self.anchor_release.is_some() {+ // A Shift-press on a selected point is still a click: nothing moves yet.+ return vec![];+ }+ // Shift: horizontal, vertical or at 45°.+ if ev.mods.shift {+ d = constrain_45(d);+ }- let mut dx = d.x;- let mut dy = d.y;- self.guides.clear();- // Direction handles do not snap. An anchor excludes the path it is on.- if handle.is_none()- && let Some((spread, origin)) = at- {- let proposed = origin + cx.layout.delta_to_spread(spread, d);- let exclude = [ItemId(id)];- let hit = crate::snap::snap(- cx,- SnapRequest {- spread,- gesture: Gesture::Point,- rect: Rect::from_points(proposed, proposed),- x_edges: [true, true, true],- y_edges: [true, true, true],- exclude: &exclude,- exclude_guides: &[],- copying: false,- lengths: [None, None],- angle: None,- radius: 0.0,- pointer: proposed,- },- );- // dx and dy stay in canvas space. Snap.delta is spread space.+ let mut dx = d.x;+ let mut dy = d.y;+ self.guides.clear();+ // Direction handles do not snap. An anchor excludes the path it is on.+ if handle.is_none()+ && let Some((spread, origin)) = at+ {+ let proposed = origin + cx.layout.delta_to_spread(spread, d);+ let exclude = [ItemId(id)];+ let hit = crate::snap::snap(+ cx,+ SnapRequest {+ spread,+ gesture: Gesture::Point,+ rect: Rect::from_points(proposed, proposed),+ x_edges: [true, true, true],+ y_edges: [true, true, true],+ exclude: &exclude,+ exclude_guides: &[],+ copying: false,+ lengths: [None, None],+ angle: None,+ radius: 0.0,+ pointer: proposed,+ },+ );+ // dx and dy stay in canvas space. Snap.delta is spread space.- let corr = spread_delta_to_canvas(cx.layout.xf(spread), hit.delta);- if corr.x.is_finite() {- dx += corr.x;+ let mut corr = spread_delta_to_canvas(cx.layout.xf(spread), hit.delta);+ corr = Vec2::new(if corr.x.is_finite() { corr.x } else { 0.0 }, if corr.y.is_finite() { corr.y } else { 0.0 });+ // Shift: the snap moves the points along the constrained direction only.+ if ev.mods.shift && d.hypot() > 1e-9 {+ let dir = d / d.hypot();+ corr = dir * corr.dot(dir);- }+ }- if corr.y.is_finite() {+ dx += corr.x;dy += corr.y;- self.guides = hit.guides;-- }- let mut params = json!({"id": id, "anchors": [[si, ai]], "dx": dx, "dy": dy});- if let Some(h) = handle {- params["handle"] = json!(h);+ self.guides = hit.guides;+ }+ let mut params = json!({"id": id, "anchors": [[si, ai]], "dx": dx, "dy": dy});+ if let Some(h) = handle {+ params["handle"] = json!(h);+ } else if !others.is_empty() {+ let points: Vec<Value> = std::iter::once((id, si, ai)).chain(others).map(|(i, s, a)| json!([i, s, a])).collect();+ params = json!({"points": points, "dx": dx, "dy": dy});- }- vec![Action::Preview("path.moveAnchors".into(), params)]}- Drag::None => vec![],- },+ vec![Action::Preview("path.moveAnchors".into(), params)]+ }+ Drag::None => vec![],+ }+ }+ }++ impl Tool for SelectionTool {+ fn id(&self) -> &'static str {+ if self.direct { "directSelection" } else { "selection" }+ }++ fn pointer(&mut self, cx: &ToolContext, ev: &PointerEvent) -> Vec<Action> {+ let p = ev.pos;+ // Selecting something else ends live-corner editing.+ if let Some(id) = self.live+ && cx.selection.items != [id]+ {+ self.live = None;+ }+ if !self.direct+ && let Some(actions) = self.thread_pointer(cx, ev)+ {+ return actions;+ }+ // The second press of a double click on a selection handle: Fit Frame to Content, along+ // that handle's side (a corner fits both).+ if !self.direct+ && matches!(ev.kind, PointerKind::Down | PointerKind::DoubleClick)+ && ev.click_count() == 2+ && let Some(h) = handle_at(cx, p)+ {+ self.drag = Drag::None;+ return vec![Action::Exec("object.fit".into(), json!({"mode": "fitFrameToContent", "axis": fitting_axis(cx, h)}))];+ }+ // The second press of a double click on a text frame: edit its text.+ if matches!(ev.kind, PointerKind::Down | PointerKind::DoubleClick)+ && ev.click_count() == 2+ && let Some(actions) = edit_text_at(cx, p)+ {+ self.drag = Drag::None;+ return actions;+ }+ // The second press of a double click on a frame with content: toggle between the frame and+ // its content.+ if !self.direct+ && matches!(ev.kind, PointerKind::Down | PointerKind::DoubleClick)+ && ev.click_count() == 2+ && let Some(id) = cx.hit(p).map(|(_, id)| cx.doc.top_level_of(id).unwrap_or(id))+ && cx.doc.item(id).is_some_and(holds_content)+ {+ self.drag = Drag::None;+ let content = !(cx.selection.content && cx.selection.contains(id));+ return vec![Action::Exec("selection.set".into(), json!({"ids": [id.0], "content": content}))];+ }+ match ev.kind {+ PointerKind::Move => {+ self.hover_handle = handle_at(cx, p);+ vec![]+ }+ PointerKind::Down => self.pointer_down(cx, ev),+ PointerKind::Drag => self.pointer_drag(cx, ev),PointerKind::Up => {self.guides.clear();let release = self.shift_release.take();diff --cc crates/ui-egui/src/canvas/frames.rsindex 000000000,4ff09ab98..8d92ec9damode 000000,100644..100644--- a/crates/ui-egui/src/canvas/frames.rs+++ b/crates/ui-egui/src/canvas/frames.rs@@@ -1,0 -1,378 +1,382 @@@+ use super::page_decor::dashed;+ use super::render::c32;+ use super::rulers::hair;+ use super::text_overlay::draw_text_selection;+ use super::{+ Affine, CanvasLayout, Color32, Content, DRect, DesignApp, Document, Item, Point, Pos2, Rect, Selection, Stroke, StrokeKind, Xf, compose, pos2,+ vec2,+ };++ /// Spread-space → canvas transform for an item at `loc`.+ pub(super) fn item_canvas_xf(doc: &Document, layout: &CanvasLayout, id: designcraft_doc::ItemId) -> Option<(Affine, &'static str)> {+ let loc = doc.find(id)?;+ Some((layout.xf(loc.spread) * doc.parent_xf(&loc), ""))+ }++ pub(super) fn path_screen(it: &Item, xf: &Xf, a: Affine) -> Vec<Vec<Pos2>> {+ let bp = it.path.to_bezpath();+ let full = a * it.xf;+ let mut out = Vec::new();+ let mut cur: Vec<Pos2> = Vec::new();+ designcraft_geom::kurbo::flatten(&bp, 0.25 / xf.zoom.max(0.01), |el| {+ use designcraft_geom::PathEl::*;+ match el {+ MoveTo(p) => {+ if cur.len() > 1 {+ out.push(std::mem::take(&mut cur));+ }+ cur.clear();+ cur.push(xf.to_screen(full * p));+ }+ LineTo(p) => cur.push(xf.to_screen(full * p)),+ ClosePath => {+ if let Some(f) = cur.first().copied() {+ cur.push(f);+ }+ }+ _ => {}+ }+ });+ if cur.len() > 1 {+ out.push(cur);+ }+ out+ }++ pub(super) fn draw_frames(app: &DesignApp, painter: &egui::Painter, xf: &Xf, doc: &Document, layout: &CanvasLayout) {+ if !app.ui.frame_edges {+ return;+ }+ let ported = ported_frames(app, doc);+ for slot in &layout.slots {+ let Some(sp) = doc.spread(slot.spread) else { continue };+ let a = slot.xf;+ // Parent items on document pages, and base parents' items on a parent spread: dotted edges.+ for (pi, page) in sp.pages.iter().enumerate() {+ for (ppi, ppg) in doc.shown_parent_pages(slot.spread, pi) {+ let Some(parent) = doc.parents.get(ppi) else { continue };+ let Some(ppage) = parent.pages.get(ppg) else { continue };+ let dx = page.x - ppage.x;+ for it in &parent.items {+ if (parent.pages.len() > 1 && parent.page_at_x(it.bounds().center().x) != Some(ppg)) || page.overridden.contains(&it.id) {+ continue;+ }+ let col = doc.layer(it.layer).map(|l| c32(l.color)).unwrap_or(Color32::LIGHT_BLUE);+ for poly in path_screen(it, xf, a * Affine::translate((dx, 0.0))) {+ for w in poly.windows(2) {+ dashed(painter, w[0], w[1], Stroke::new(hair(painter), col.gamma_multiply(0.8)), 2.0, 2.0);+ }+ }+ }+ }+ }+ for it in &sp.items {+ draw_item_edges(painter, xf, doc, it, a);+ }+ // Every overset frame, selected or not and inside groups too, shows its red mark at the+ // out port (InDesign shows it only on the selected frame; a report in Preflight is easy+ // to miss while laying out).+ for (it, c) in overset_marks(app, doc, layout, &sp.items, xf.zoom, &ported) {+ let c = xf.to_screen(c);+ let s = designcraft_tools::thread::PORT_SIZE_PX as f32;+ painter.rect_filled(Rect::from_center_size(c, vec2(s, s)), 0.0, Color32::WHITE);+ painter.rect_stroke(Rect::from_center_size(c, vec2(s, s)), 0.0, Stroke::new(hair(painter), layer_color(doc, it)), StrokeKind::Inside);+ let red = Color32::from_rgb(230, 20, 20);+ painter.line_segment([c - vec2(2.5, 0.0), c + vec2(2.5, 0.0)], Stroke::new(1.5, red));+ painter.line_segment([c - vec2(0.0, 2.5), c + vec2(0.0, 2.5)], Stroke::new(1.5, red));+ }+ // Tagged frames in their tag's colour.+ if app.ui.tagged_frames {+ for top in &sp.items {+ top.walk(&mut |it: &Item| {+ if it.xml_tag.is_empty() {+ return;+ }+ let Some(t) = doc.xml.tags.iter().find(|t| t.name == it.xml_tag) else { return };+ let col = Color32::from_rgb(t.color[0], t.color[1], t.color[2]);+ for poly in path_screen(it, xf, a) {+ for w in poly.windows(2) {+ painter.line_segment([w[0], w[1]], Stroke::new(2.5, col));+ }+ }+ });+ }+ }+ }+ }++ fn draw_item_edges(painter: &egui::Painter, xf: &Xf, doc: &Document, it: &Item, a: Affine) {+ if it.hidden || doc.layer(it.layer).is_some_and(|l| !l.visible) {+ return;+ }+ let col = doc.layer(it.layer).map(|l| c32(l.color)).unwrap_or(Color32::LIGHT_BLUE);+ if let Content::Group { items } = &it.content {+ for c in items {+ draw_item_edges(painter, xf, doc, c, a * it.xf);+ }+ return;+ }+ // Frames without a stroke show their edge; shapes with strokes are visible already.+ let show = it.stroke.is_none() || matches!(it.content, Content::Text(_) | Content::Graphic(_));+ if show {+ for poly in path_screen(it, xf, a) {+ painter.add(egui::Shape::line(poly, Stroke::new(hair(painter), col.gamma_multiply(0.75))));+ }+ }+ // Empty graphic frame: the X.+ if it.object_style == designcraft_doc::BASIC_GRAPHICS_FRAME && matches!(it.content, Content::Unassigned) {+ let r = it.inner_bounds();+ let m = a * it.xf;+ let p = |x: f64, y: f64| xf.to_screen(m * Point::new(x, y));+ painter.line_segment([p(r.x0, r.y0), p(r.x1, r.y1)], Stroke::new(hair(painter), col.gamma_multiply(0.75)));+ painter.line_segment([p(r.x1, r.y0), p(r.x0, r.y1)], Stroke::new(hair(painter), col.gamma_multiply(0.75)));+ }+ }++ /// Layer colour of the first selected item: live-corner marks are outlined in it.+ pub(super) fn selection_color(app: &DesignApp) -> Color32 {+ let fallback = Color32::from_rgb(79, 153, 255);+ let Some(st) = app.session.active() else { return fallback };+ st.selection.items.first().and_then(|id| st.doc.item(*id)).map_or(fallback, |it| layer_color(&st.doc, it))+ }++ pub(super) fn layer_color(doc: &Document, it: &Item) -> Color32 {+ doc.layer(it.layer).map(|l| c32(l.color)).unwrap_or(Color32::from_rgb(79, 153, 255))+ }++ /// Draw selection chrome; returns the selection's screen bounds (for the Contextual Task Bar).+ pub(super) fn draw_selection(app: &DesignApp, painter: &egui::Painter, xf: &Xf, doc: &Document, layout: &CanvasLayout) -> Option<Rect> {+ let st = app.session.active()?;+ let sel: &Selection = &st.selection;+ let mut text_rect = None;+ // Text selection / caret.+ if let Some(ts) = sel.text {+ draw_text_selection(app, painter, xf, doc, layout, ts, sel.cells);+ let fid = ts.frame.or_else(|| {+ let cs = app.session.cache.get(doc, ts.story, None);+ compose::caret(&cs, ts.focus).and_then(|c| cs.frames.get(c.0)).map(|f| f.frame)+ });+ if let Some(fid) = fid+ && let (Some(it), Some((a, _))) = (doc.item(fid), item_canvas_xf(doc, layout, fid))+ {+ text_rect = Some(xf.rect(a.transform_rect_bbox(it.bounds())));+ }+ }+ let mut union: Option<Rect> = None;+ let mut color = Color32::from_rgb(79, 153, 255);+ for id in &sel.items {+ let (Some(it), Some((a, _))) = (doc.item(*id), item_canvas_xf(doc, layout, *id)) else { continue };+ color = layer_color(doc, it);+ for poly in path_screen(it, xf, a) {+ painter.add(egui::Shape::line(poly, Stroke::new(1.0, color)));+ }+ if sel.items.len() == 1 && sel.text.is_none() {+ // InDesign draws a 1 pt bounding box around the selection.+ painter.rect_stroke(xf.rect(a.transform_rect_bbox(it.bounds())), 0.0, Stroke::new(1.0, color), StrokeKind::Middle);+ } else if sel.key == Some(*id) {+ // The key object for Align: a thicker bounding box, as in InDesign.+ painter.rect_stroke(xf.rect(a.transform_rect_bbox(it.bounds())), 0.0, Stroke::new(3.0, color), StrokeKind::Middle);+ }+ let b = xf.rect(a.transform_rect_bbox(it.bounds()));+ union = Some(union.map_or(b, |u| u.union(b)));+ // Text frame ports, also of the frames inside a selected group (so an overset frame in a+ // group shows its red mark).+ let mut frames = Vec::new();+ text_frames_in(it, 0, &mut frames);+ for (frame, story) in frames {+ draw_ports(app, painter, xf, doc, layout, frame, story, color);+ }+ if let Content::Graphic(g) = &it.content+ && sel.content+ {+ let gb = xf.rect((a * it.xf).transform_rect_bbox(g.xf.transform_rect_bbox(DRect::new(0.0, 0.0, g.size.0, g.size.1))));+ let brown = Color32::from_rgb(196, 111, 43);+ painter.rect_stroke(gb, 0.0, Stroke::new(1.0, brown), StrokeKind::Middle);+ // Its own handles resize the graphic (InDesign's brown content bounding box).+ if sel.items.len() == 1 {+ for h in designcraft_tools::select::handles(DRect::new(gb.min.x as f64, gb.min.y as f64, gb.max.x as f64, gb.max.y as f64)) {+ let r = Rect::from_center_size(pos2(h.x as f32, h.y as f32), vec2(6.5, 6.5));+ painter.rect_filled(r, 0.0, Color32::WHITE);+ painter.rect_stroke(r, 0.0, Stroke::new(hair(painter), brown), StrokeKind::Inside);+ }+ }+ }+ }+ let direct = matches!(app.session.tool_id(), "directSelection" | "pen" | "addAnchor" | "deleteAnchor" | "convertDirection");+ if direct {++ // Looked up once per anchor drawn: a large marquee selection stays linear.++ let chosen_set: std::collections::HashSet<(designcraft_doc::ItemId, usize, usize)> = sel.anchors.iter().copied().collect();+ for id in &sel.items {+ let (Some(it), Some((a, _))) = (doc.item(*id), item_canvas_xf(doc, layout, *id)) else { continue };+ let m = a * it.xf;+ let col = layer_color(doc, it);- for sp in &it.path.subpaths {- for an in &sp.anchors {++ for (si, sp) in it.path.subpaths.iter().enumerate() {++ for (ai, an) in sp.anchors.iter().enumerate() {+ let p = xf.to_screen(m * an.p);++ // A selected anchor point is solid, the others hollow (InDesign).++ let chosen = chosen_set.contains(&(*id, si, ai));+ for (h, has) in [(an.h_in, an.has_in()), (an.h_out, an.has_out())] {+ if has {+ let hp = xf.to_screen(m * h);+ painter.line_segment([p, hp], Stroke::new(1.0, col));+ painter.circle_filled(hp, 2.5, col);+ }+ }+ let r = Rect::from_center_size(p, vec2(5.0, 5.0));- painter.rect_filled(r, 0.0, Color32::WHITE);++ painter.rect_filled(r, 0.0, if chosen { col } else { Color32::WHITE });+ painter.rect_stroke(r, 0.0, Stroke::new(1.0, col), StrokeKind::Inside);+ }+ }+ }+ }+ if let Some(u) = union+ && sel.text.is_none()+ && !direct+ {+ if sel.items.len() > 1 {+ painter.rect_stroke(u, 0.0, Stroke::new(1.0, color), StrokeKind::Middle);+ }+ let hs = 6.5;+ // Centre point. The Selection tool draws the live-corner widget as an overlay.+ painter.rect_filled(Rect::from_center_size(u.center(), vec2(3.5, 3.5)), 0.0, color);+ // A selected placed graphic has its own handles instead of the frame's.+ let graphic_selected =+ sel.content && sel.items.len() == 1 && doc.item(sel.items[0]).is_some_and(|i| matches!(i.content, Content::Graphic(_)));+ for h in designcraft_tools::select::handles(DRect::new(u.min.x as f64, u.min.y as f64, u.max.x as f64, u.max.y as f64))+ .into_iter()+ .filter(|_| !graphic_selected)+ {+ let r = Rect::from_center_size(pos2(h.x as f32, h.y as f32), vec2(hs, hs));+ painter.rect_filled(r, 0.0, Color32::WHITE);+ painter.rect_stroke(r, 0.0, Stroke::new(hair(painter), color), StrokeKind::Inside);+ }+ }+ // Threads.+ if app.ui.text_threads || sel.items.iter().any(|i| doc.item(*i).is_some_and(|x| x.is_text_frame())) {+ for id in &sel.items {+ let Some(sid) = doc.item(*id).and_then(|i| i.text_frame()).map(|t| t.story) else { continue };+ let Some(story) = doc.story(sid) else { continue };+ for w in story.frames.windows(2) {+ let (Some(a), Some(b)) = (port_pos(doc, layout, w[0], true), port_pos(doc, layout, w[1], false)) else { continue };+ let col = doc.item(w[0]).map(|i| layer_color(doc, i)).unwrap_or(color);+ painter.line_segment([xf.to_screen(a), xf.to_screen(b)], Stroke::new(1.0, col));+ }+ }+ }+ text_rect.or(union)+ }++ /// How deep a selected group is walked for the text frames whose ports are drawn.+ const PORT_GROUP_DEPTH: usize = 32;++ /// The text frames whose ports `draw_selection` draws: the selected text frames and the text+ /// frames inside selected groups.+ pub(super) fn ported_frames(app: &DesignApp, doc: &Document) -> std::collections::HashSet<designcraft_doc::ItemId> {+ let mut frames = Vec::new();+ if let Some(st) = app.session.active() {+ for it in st.selection.items.iter().filter_map(|id| doc.item(*id)) {+ text_frames_in(it, 0, &mut frames);+ }+ }+ frames.into_iter().map(|(f, _)| f.id).collect()+ }++ /// Canvas centres of the always-on overset marks among `items`: the out port of every visible+ /// frame that ends an overset story, except the frames in `ported`, whose out port `draw_ports`+ /// already marks.+ pub(super) fn overset_marks<'a>(+ app: &DesignApp,+ doc: &Document,+ layout: &CanvasLayout,+ items: &'a [std::sync::Arc<Item>],+ zoom: f64,+ ported: &std::collections::HashSet<designcraft_doc::ItemId>,+ ) -> Vec<(&'a Item, Point)> {+ let mut frames = Vec::new();+ overset_frames(app, doc, items, 0, &mut frames);+ frames+ .into_iter()+ .filter(|it| !ported.contains(&it.id))+ .filter_map(|it| Some((it, designcraft_tools::thread::port_center(doc, layout, it.id, true, zoom)?)))+ .collect()+ }++ /// The visible text frames among `items` (groups walked) that end an overset story.+ pub(super) fn overset_frames<'a>(app: &DesignApp, doc: &Document, items: &'a [std::sync::Arc<Item>], depth: usize, out: &mut Vec<&'a Item>) {+ if depth > PORT_GROUP_DEPTH {+ return;+ }+ for it in items {+ if it.hidden || doc.layer(it.layer).is_some_and(|l| !l.visible) {+ continue;+ }+ match &it.content {+ Content::Text(tf) => {+ let Some(story) = doc.story(tf.story) else { continue };+ if story.frames.last() == Some(&it.id) && app.session.cache.get(doc, tf.story, None).is_overset() {+ out.push(it);+ }+ }+ Content::Group { items } => overset_frames(app, doc, items, depth + 1, out),+ _ => {}+ }+ }+ }++ /// `it` when it is a text frame, or every text frame nested in it when it is a group, each with+ /// its story.+ pub(super) fn text_frames_in<'a>(it: &'a Item, depth: usize, out: &mut Vec<(&'a Item, designcraft_doc::StoryId)>) {+ match &it.content {+ Content::Text(tf) => out.push((it, tf.story)),+ Content::Group { items } if depth < PORT_GROUP_DEPTH => {+ for child in items {+ text_frames_in(child, depth + 1, out);+ }+ }+ _ => {}+ }+ }++ /// Port location (canvas): out port at bottom-right, in port at top-left.+ fn port_pos(doc: &Document, layout: &CanvasLayout, id: designcraft_doc::ItemId, out: bool) -> Option<Point> {+ let it = doc.item(id)?;+ let (a, _) = item_canvas_xf(doc, layout, id)?;+ let r = it.inner_bounds();+ let p = if out { Point::new(r.x1 - 10.0, r.y1) } else { Point::new(r.x0 + 10.0, r.y0) };+ Some(a * it.xf * p)+ }++ #[allow(clippy::too_many_arguments)]+ fn draw_ports(+ app: &DesignApp,+ painter: &egui::Painter,+ xf: &Xf,+ doc: &Document,+ layout: &CanvasLayout,+ it: &Item,+ sid: designcraft_doc::StoryId,+ color: Color32,+ ) {+ let Some(story) = doc.story(sid) else { return };+ let pos = story.frames.iter().position(|f| *f == it.id).unwrap_or(0);+ let is_last = pos + 1 == story.frames.len();+ let cs = app.session.cache.get(doc, sid, None);+ let overset = is_last && cs.is_overset();+ let s = designcraft_tools::thread::PORT_SIZE_PX as f32;+ // In port on the left edge below the top-left corner; out port on the right edge above the+ // bottom-right corner. The Selection tool hit-tests the same places.+ let port = |out: bool| designcraft_tools::thread::port_center(doc, layout, it.id, out, xf.zoom).map(|c| xf.to_screen(c));+ let (Some(inp), Some(outp)) = (port(false), port(true)) else { return };+ for (c, has_link, is_out) in [(inp, pos > 0, false), (outp, !is_last, true)] {+ let pr = Rect::from_center_size(c, vec2(s, s));+ painter.rect_filled(pr, 0.0, Color32::WHITE);+ painter.rect_stroke(pr, 0.0, Stroke::new(hair(painter), color), StrokeKind::Inside);+ if is_out && overset {+ let red = Color32::from_rgb(230, 20, 20);+ painter.line_segment([c - vec2(2.5, 0.0), c + vec2(2.5, 0.0)], Stroke::new(1.5, red));+ painter.line_segment([c - vec2(0.0, 2.5), c + vec2(0.0, 2.5)], Stroke::new(1.5, red));+ } else if has_link {+ painter.add(egui::Shape::convex_polygon(vec![c + vec2(-2.0, -2.5), c + vec2(2.5, 0.0), c + vec2(-2.0, 2.5)], color, Stroke::NONE));+ }+ }+ }