Skip to content

Commit 220f27a

Browse files
committed
Canvas: the resize cursor follows the handle's rotated diagonal (#745)
The resize cursor was picked from a fixed handle-to-cursor mapping: handle 0 always showed NW-SE, handle 2 always NE-SW, and so on. That is correct for unrotated shapes, but the handles sit on the shape's transformed corners - a 45-degree rotation puts the NW corner's drag axis on the other diagonal, and the cursor pointed the wrong way. The cursor is now picked from the handle's resize axis mapped through the shape's transform: for each handle the axis direction (centre toward the handle) is rotated with the shape, and the closest of the four resize cursors is chosen. Unrotated shapes get exactly the same cursors as before; rotated and flipped shapes get the one that matches the drag direction.
1 parent 010941e commit 220f27a

2 files changed

Lines changed: 100 additions & 6 deletions

File tree

‎ROADMAP.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@ app's JSON control channel (`deckcraft --control PORT`).
135135
| 2026-10-17 | Opening a .deckcraft file whose shapes repeat an id gives the repeats fresh ids (groups, layouts and masters too), so the deck validates and edits by id find one shape |
136136
| 2026-10-17 | Edit Points: turning a shape into a freeform is one undo step, so Undo brings the original shape back (it used to stay a freeform) |
137137
| 2026-10-17 | While a placeholder's text is being edited, a click on a shape in front of it selects that shape instead of moving the caret, as in PowerPoint (#931) |
138+
| 2026-10-15 | Canvas: the resize cursor follows the handle's rotated diagonal — rotated shapes showed the wrong diagonal cursor (#745) |
138139
| 2026-10-15 | PPTX trigger sequences keep separate On Click steps, including paragraph builds; synthetic import/edit/save/reopen and headless show regressions |
139140
| 2026-10-16 | Export: PNG with a transparent background — the slide background is left out, only the slide's content is drawn (Export… dialog, File › Export, web download, `file.export`/`file.render` `transparentBackground`, `deckcraft-cli render --transparent`) (#784) |
140141
| 2026-10-16 | Arrange ▸ Align: Align to Slide and Align Selected Objects, as in PowerPoint. One object aligns to the slide and several to each other until the other choice is ticked; the choice holds for that selection and also drives Distribute |

‎crates/ui-egui/src/canvas.rs‎

Lines changed: 99 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -758,12 +758,7 @@ fn pointer(app: &mut SlideApp, ui: &Ui, resp: &egui::Response, xf: Xf) {
758758
(ToolKind::Shape { .. }, _) => egui::CursorIcon::Crosshair,
759759
(ToolKind::TextBox, _) => egui::CursorIcon::Text,
760760
(ToolKind::Ink { .. }, _) => egui::CursorIcon::Crosshair,
761-
(_, Some(Hit::Resize { handle, .. })) => match handle {
762-
0 | 4 => egui::CursorIcon::ResizeNwSe,
763-
2 | 6 => egui::CursorIcon::ResizeNeSw,
764-
1 | 5 => egui::CursorIcon::ResizeVertical,
765-
_ => egui::CursorIcon::ResizeHorizontal,
766-
},
761+
(_, Some(Hit::Resize { id, handle })) => resize_cursor(&app.session, *id, *handle),
767762
(_, Some(Hit::Rotate { .. })) => egui::CursorIcon::Alias,
768763
(_, Some(Hit::TableColumn { .. })) => egui::CursorIcon::ResizeColumn,
769764
(_, Some(Hit::TableRow { .. })) => egui::CursorIcon::ResizeRow,
@@ -1347,17 +1342,115 @@ fn grid_fits(span: f32, step: f32) -> bool {
13471342
span.is_finite() && span > 0.0 && step >= 1.0 && span / step <= 800.0
13481343
}
13491344

1345+
/// The resize cursor for `handle` of shape `id`: the handle's resize axis is computed in the
1346+
/// shape's own space and mapped through its transform, so a rotated or flipped shape shows the
1347+
/// diagonal that matches where the drag actually goes (#745). Without a transform the mapping
1348+
/// reduces to the classic NW/NE/E/SE/S/SW/W assignment.
1349+
fn resize_cursor(session: &deckcraft_engine::Session, id: deckcraft_model::ShapeId, handle: u8) -> egui::CursorIcon {
1350+
// The resize axis in the shape's local coordinates, as (dx, dy) from the box centre toward
1351+
// the handle. Corners resize along the diagonal; edge handles along their edge.
1352+
let Some(d) = session.active() else { return egui::CursorIcon::Default };
1353+
let Some(sh) = d.shape(id) else { return egui::CursorIcon::Default };
1354+
let Some(x) = sh.xfrm else { return egui::CursorIcon::Default };
1355+
let (w, h) = (x.w, x.h);
1356+
let (dx, dy): (f64, f64) = match handle {
1357+
0 => (-w, -h),
1358+
1 => (0.0, -h),
1359+
2 => (w, -h),
1360+
3 => (w, 0.0),
1361+
4 => (w, h),
1362+
5 => (0.0, h),
1363+
6 => (-w, h),
1364+
_ => (-w, 0.0),
1365+
};
1366+
// Map through the linear part of the transform (scale, rotation, flips): two points along
1367+
// the axis, the difference is the slide-space direction.
1368+
let centre = Point::new(x.x + w / 2.0, x.y + h / 2.0);
1369+
let a = x.affine();
1370+
let p0 = a * centre;
1371+
let p1 = a * Point::new(centre.x + dx, centre.y + dy);
1372+
let v = p1 - p0;
1373+
if !v.x.is_finite() || !v.y.is_finite() || (v.x.abs() < 1e-12 && v.y.abs() < 1e-12) {
1374+
return egui::CursorIcon::Default;
1375+
}
1376+
// Angle in [0, 180): 0° = right (horizontal), 45° = down-right (NW-SE), 90° = down
1377+
// (vertical), 135° = down-left (NE-SW). Pick the closest of the four.
1378+
let mut deg = v.y.atan2(v.x).to_degrees();
1379+
if deg < 0.0 {
1380+
deg += 180.0;
1381+
}
1382+
if (22.5..67.5).contains(&deg) {
1383+
egui::CursorIcon::ResizeNwSe
1384+
} else if (67.5..112.5).contains(&deg) {
1385+
egui::CursorIcon::ResizeVertical
1386+
} else if (112.5..157.5).contains(&deg) {
1387+
egui::CursorIcon::ResizeNeSw
1388+
} else {
1389+
egui::CursorIcon::ResizeHorizontal
1390+
}
1391+
}
1392+
13501393
#[cfg(test)]
13511394
mod tests {
13521395
#![allow(clippy::unwrap_used, clippy::expect_used)]
13531396

1397+
use std::sync::Arc;
1398+
13541399
use deckcraft_engine::Session;
13551400
use egui::{Event, Modifiers, MouseWheelUnit, Pos2, TouchPhase, pos2, vec2};
13561401
use egui_kittest::Harness;
13571402

13581403
use super::*;
13591404
use crate::{Services, SlideApp};
13601405

1406+
/// #745: the resize cursor follows the handle's on-screen diagonal. A 45°-rotated shape's
1407+
/// NW corner sits at the top-right of the screen, where the drag axis is NE-SW — the old
1408+
/// fixed handle-to-cursor mapping showed NW-SE there.
1409+
#[test]
1410+
fn resize_cursor_tracks_rotation() {
1411+
let mut s = Session::with_new();
1412+
// A square shape: the NW handle's diagonal is at 45° unrotated, so each 45° of shape
1413+
// rotation cleanly moves the cursor to the next of the four resize directions.
1414+
let id = s.execute("shape.insert", &serde_json::json!({"preset": "rect", "rect": [100.0, 100.0, 100.0, 100.0]})).unwrap()["id"]
1415+
.as_u64()
1416+
.unwrap() as u32;
1417+
let sid = deckcraft_model::ShapeId(id);
1418+
// Unrotated: the NW handle (0) drags the NW-SE diagonal.
1419+
assert_eq!(resize_cursor(&s, sid, 0), egui::CursorIcon::ResizeNwSe);
1420+
assert_eq!(resize_cursor(&s, sid, 4), egui::CursorIcon::ResizeNwSe);
1421+
assert_eq!(resize_cursor(&s, sid, 2), egui::CursorIcon::ResizeNeSw);
1422+
assert_eq!(resize_cursor(&s, sid, 6), egui::CursorIcon::ResizeNeSw);
1423+
assert_eq!(resize_cursor(&s, sid, 1), egui::CursorIcon::ResizeVertical);
1424+
assert_eq!(resize_cursor(&s, sid, 3), egui::CursorIcon::ResizeHorizontal);
1425+
// 45° rotation: the NW corner's drag axis rotates 45° with the shape — it is now vertical.
1426+
{
1427+
let doc = s.doc_mut().unwrap();
1428+
let pres = Arc::make_mut(&mut doc.doc);
1429+
let slide = Arc::make_mut(&mut pres.slides[0]);
1430+
if let Some(sh) = slide.shapes.iter_mut().find(|x| x.id == sid)
1431+
&& let Some(x) = sh.xfrm.as_mut()
1432+
{
1433+
x.rot = 45.0;
1434+
}
1435+
}
1436+
assert_eq!(resize_cursor(&s, sid, 0), egui::CursorIcon::ResizeVertical, "45°: NW handle drags vertically");
1437+
assert_eq!(resize_cursor(&s, sid, 2), egui::CursorIcon::ResizeHorizontal, "45°: NE handle drags horizontally");
1438+
// 90° rotation: the NW corner is at the top-right; the drag axis is NE-SW.
1439+
{
1440+
let doc = s.doc_mut().unwrap();
1441+
let pres = Arc::make_mut(&mut doc.doc);
1442+
let slide = Arc::make_mut(&mut pres.slides[0]);
1443+
if let Some(sh) = slide.shapes.iter_mut().find(|x| x.id == sid)
1444+
&& let Some(x) = sh.xfrm.as_mut()
1445+
{
1446+
x.rot = 90.0;
1447+
}
1448+
}
1449+
assert_eq!(resize_cursor(&s, sid, 0), egui::CursorIcon::ResizeNeSw, "90°: the diagonal is NE-SW");
1450+
assert_eq!(resize_cursor(&s, sid, 2), egui::CursorIcon::ResizeNwSe, "90°: NE diagonal is NW-SE");
1451+
assert_eq!(resize_cursor(&s, sid, 1), egui::CursorIcon::ResizeHorizontal, "90°: the top edge is horizontal");
1452+
}
1453+
13611454
fn harness() -> Harness<'static, SlideApp> {
13621455
let mut h = Harness::builder().with_size(egui::vec2(1200.0, 800.0)).build_ui_state(
13631456
|ui, app: &mut SlideApp| {

0 commit comments

Comments
 (0)