Skip to content

Commit bc12031

Browse files
committed
Canvas: the resize cursor follows the handle's diagonal, whatever the shape (#745)
Two gaps in the resize cursor, both found by a review of the fix: - The handle's direction was taken from the box centre to the handle, so a wide rectangle's corner pointed almost along an edge: a 400x80 shape's NW corner showed a horizontal cursor. A corner drags along the diagonal whatever the shape's proportions, so the direction is now normalised - the diagonal at 45 degrees - and only the shape's rotation and flips turn it. - The cursor read the shape's own box, but the handles are drawn from the effective one, which a placeholder inherits from its layout. Such a placeholder showed the default arrow instead of a resize cursor. The tests cover both: a wide shape keeps its corner cursors diagonal, and a placeholder that inherits its box has a resize cursor. Red-proof: with the aspect ratio back in the direction, the wide shape's NW corner shows a horizontal cursor; with the shape's own box, the placeholder shows the default arrow.
1 parent f34f412 commit bc12031

2 files changed

Lines changed: 141 additions & 6 deletions

File tree

‎ROADMAP.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ app's JSON control channel (`deckcraft --control PORT`).
137137
| 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) |
138138
| 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) |
139139
| 2026-10-15 | Native saves keep an imported chart's embedded workbook, drawings and relationships; headless PPTX → edit → native save/reopen → PPTX tests verify the payloads and stale-workbook rejection |
140+
| 2026-10-15 | Canvas: the resize cursor follows the handle's rotated diagonal — rotated shapes showed the wrong diagonal cursor (#745) |
140141
| 2026-10-15 | PPTX trigger sequences keep separate On Click steps, including paragraph builds; synthetic import/edit/save/reopen and headless show regressions |
141142
| 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) |
142143
| 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: 140 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,156 @@ 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 handles are drawn from the shape's effective box (a placeholder inherits it
1351+
// from its layout), so the cursor follows the same one.
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 x = deckcraft_engine::cmd::xfrm_of(&d.doc, &d.selection, sh);
1355+
// The resize axis in the shape's own coordinates, as (dx, dy) from the box centre toward
1356+
// the handle. Corners resize along the diagonal, edge handles along their edge. The
1357+
// direction is normalised - the diagonal at 45 degrees whatever the shape's proportions -
1358+
// so a wide rectangle still shows a diagonal cursor on its corners.
1359+
let (dx, dy): (f64, f64) = match handle {
1360+
0 => (-1.0, -1.0),
1361+
1 => (0.0, -1.0),
1362+
2 => (1.0, -1.0),
1363+
3 => (1.0, 0.0),
1364+
4 => (1.0, 1.0),
1365+
5 => (0.0, 1.0),
1366+
6 => (-1.0, 1.0),
1367+
_ => (-1.0, 0.0),
1368+
};
1369+
// Map through the linear part of the transform (rotation, flips): two points along the
1370+
// axis, the difference is the slide-space direction.
1371+
let centre = Point::new(x.x + x.w / 2.0, x.y + x.h / 2.0);
1372+
let a = x.affine();
1373+
let p0 = a * centre;
1374+
let p1 = a * Point::new(centre.x + dx, centre.y + dy);
1375+
let v = p1 - p0;
1376+
if !v.x.is_finite() || !v.y.is_finite() || (v.x.abs() < 1e-12 && v.y.abs() < 1e-12) {
1377+
return egui::CursorIcon::Default;
1378+
}
1379+
// Angle in [0, 180): 0° = right (horizontal), 45° = down-right (NW-SE), 90° = down
1380+
// (vertical), 135° = down-left (NE-SW). Pick the closest of the four.
1381+
let mut deg = v.y.atan2(v.x).to_degrees();
1382+
if deg < 0.0 {
1383+
deg += 180.0;
1384+
}
1385+
if (22.5..67.5).contains(&deg) {
1386+
egui::CursorIcon::ResizeNwSe
1387+
} else if (67.5..112.5).contains(&deg) {
1388+
egui::CursorIcon::ResizeVertical
1389+
} else if (112.5..157.5).contains(&deg) {
1390+
egui::CursorIcon::ResizeNeSw
1391+
} else {
1392+
egui::CursorIcon::ResizeHorizontal
1393+
}
1394+
}
1395+
13501396
#[cfg(test)]
13511397
mod tests {
13521398
#![allow(clippy::unwrap_used, clippy::expect_used)]
13531399

1400+
use std::sync::Arc;
1401+
13541402
use deckcraft_engine::Session;
13551403
use egui::{Event, Modifiers, MouseWheelUnit, Pos2, TouchPhase, pos2, vec2};
13561404
use egui_kittest::Harness;
13571405

13581406
use super::*;
13591407
use crate::{Services, SlideApp};
13601408

1409+
/// #745: the resize cursor follows the handle's on-screen diagonal. A 45°-rotated shape's
1410+
/// NW corner sits at the top-right of the screen, where the drag axis is NE-SW — the old
1411+
/// fixed handle-to-cursor mapping showed NW-SE there.
1412+
#[test]
1413+
fn resize_cursor_tracks_rotation() {
1414+
let mut s = Session::with_new();
1415+
// A square shape: the NW handle's diagonal is at 45° unrotated, so each 45° of shape
1416+
// rotation cleanly moves the cursor to the next of the four resize directions.
1417+
let id = s.execute("shape.insert", &serde_json::json!({"preset": "rect", "rect": [100.0, 100.0, 100.0, 100.0]})).unwrap()["id"]
1418+
.as_u64()
1419+
.unwrap() as u32;
1420+
let sid = deckcraft_model::ShapeId(id);
1421+
// Unrotated: the NW handle (0) drags the NW-SE diagonal.
1422+
assert_eq!(resize_cursor(&s, sid, 0), egui::CursorIcon::ResizeNwSe);
1423+
assert_eq!(resize_cursor(&s, sid, 4), egui::CursorIcon::ResizeNwSe);
1424+
assert_eq!(resize_cursor(&s, sid, 2), egui::CursorIcon::ResizeNeSw);
1425+
assert_eq!(resize_cursor(&s, sid, 6), egui::CursorIcon::ResizeNeSw);
1426+
assert_eq!(resize_cursor(&s, sid, 1), egui::CursorIcon::ResizeVertical);
1427+
assert_eq!(resize_cursor(&s, sid, 3), egui::CursorIcon::ResizeHorizontal);
1428+
// 45° rotation: the NW corner's drag axis rotates 45° with the shape — it is now vertical.
1429+
{
1430+
let doc = s.doc_mut().unwrap();
1431+
let pres = Arc::make_mut(&mut doc.doc);
1432+
let slide = Arc::make_mut(&mut pres.slides[0]);
1433+
if let Some(sh) = slide.shapes.iter_mut().find(|x| x.id == sid)
1434+
&& let Some(x) = sh.xfrm.as_mut()
1435+
{
1436+
x.rot = 45.0;
1437+
}
1438+
}
1439+
assert_eq!(resize_cursor(&s, sid, 0), egui::CursorIcon::ResizeVertical, "45°: NW handle drags vertically");
1440+
assert_eq!(resize_cursor(&s, sid, 2), egui::CursorIcon::ResizeHorizontal, "45°: NE handle drags horizontally");
1441+
// 90° rotation: the NW corner is at the top-right; the drag axis is NE-SW.
1442+
{
1443+
let doc = s.doc_mut().unwrap();
1444+
let pres = Arc::make_mut(&mut doc.doc);
1445+
let slide = Arc::make_mut(&mut pres.slides[0]);
1446+
if let Some(sh) = slide.shapes.iter_mut().find(|x| x.id == sid)
1447+
&& let Some(x) = sh.xfrm.as_mut()
1448+
{
1449+
x.rot = 90.0;
1450+
}
1451+
}
1452+
assert_eq!(resize_cursor(&s, sid, 0), egui::CursorIcon::ResizeNeSw, "90°: the diagonal is NE-SW");
1453+
assert_eq!(resize_cursor(&s, sid, 2), egui::CursorIcon::ResizeNwSe, "90°: NE diagonal is NW-SE");
1454+
assert_eq!(resize_cursor(&s, sid, 1), egui::CursorIcon::ResizeHorizontal, "90°: the top edge is horizontal");
1455+
}
1456+
1457+
/// #745: a corner handle shows a diagonal cursor whatever the shape's proportions. The
1458+
/// corner's direction is the diagonal, not the line to the box's centre, so a wide
1459+
/// rectangle's NW corner still drags along the NW-SE diagonal.
1460+
#[test]
1461+
fn a_wide_shape_keeps_its_corner_cursors_diagonal() {
1462+
let mut s = Session::with_new();
1463+
let id = s.execute("shape.insert", &serde_json::json!({"preset": "rect", "rect": [100.0, 100.0, 400.0, 80.0]})).unwrap()["id"]
1464+
.as_u64()
1465+
.unwrap() as u32;
1466+
let sid = deckcraft_model::ShapeId(id);
1467+
assert_eq!(resize_cursor(&s, sid, 0), egui::CursorIcon::ResizeNwSe, "the NW corner drags NW-SE");
1468+
assert_eq!(resize_cursor(&s, sid, 4), egui::CursorIcon::ResizeNwSe, "the SE corner drags NW-SE");
1469+
assert_eq!(resize_cursor(&s, sid, 2), egui::CursorIcon::ResizeNeSw, "the NE corner drags NE-SW");
1470+
assert_eq!(resize_cursor(&s, sid, 6), egui::CursorIcon::ResizeNeSw, "the SW corner drags NE-SW");
1471+
// The edges keep their own cursors.
1472+
assert_eq!(resize_cursor(&s, sid, 1), egui::CursorIcon::ResizeVertical, "the top edge drags vertically");
1473+
assert_eq!(resize_cursor(&s, sid, 3), egui::CursorIcon::ResizeHorizontal, "the right edge drags horizontally");
1474+
}
1475+
1476+
/// #745: a placeholder that inherits its box from the layout still shows a resize cursor.
1477+
/// Its handles are drawn from the inherited box, so the cursor follows that one.
1478+
#[test]
1479+
fn a_placeholder_that_inherits_its_box_has_a_resize_cursor() {
1480+
let s = Session::with_new();
1481+
let st = s.doc().unwrap();
1482+
let ph = st
1483+
.current_slide()
1484+
.unwrap()
1485+
.shapes
1486+
.iter()
1487+
.find(|sh| sh.xfrm.is_none() && sh.ph.is_some())
1488+
.expect("the new slide has a placeholder without a box of its own")
1489+
.id;
1490+
assert_eq!(resize_cursor(&s, ph, 0), egui::CursorIcon::ResizeNwSe, "the NW corner drags NW-SE");
1491+
assert_eq!(resize_cursor(&s, ph, 2), egui::CursorIcon::ResizeNeSw, "the NE corner drags NE-SW");
1492+
assert_eq!(resize_cursor(&s, ph, 1), egui::CursorIcon::ResizeVertical, "the top edge drags vertically");
1493+
}
1494+
13611495
fn harness() -> Harness<'static, SlideApp> {
13621496
let mut h = Harness::builder().with_size(egui::vec2(1200.0, 800.0)).build_ui_state(
13631497
|ui, app: &mut SlideApp| {

0 commit comments

Comments
 (0)