A live rectangle stored four corner radii but drew them all with the largest, and the corner widgets always rounded all four corners, so one corner could not be rounded on its own. - geom: rectangle_with_corners draws each corner with its own radius and kind (Round, Inverted Round, Chamfer); a sharp corner is one anchor, a cut one two. Uniform corners keep the exact rounded-rectangle anchors. - doc: LiveShape::Rectangle gains `kinds` (saved only when a corner isn't round; older files read as round), plus helpers mapping anchors to corners. - engine: object.setLiveShape takes `corners` and `kind`; without `corners` it edits the Direct-Selected corners, else all four, and keeps the selected corners selected as their anchors change. - tools: with Direct Selection, only the selected corners show widgets and a drag rounds just those; Alt-click cycles the corner kind; double-click opens the new Corners dialog (also `ui.corners`). - ui: Properties/Transform radius fields show and set the selected corners; CSS export keeps per-corner radii and skips cut-in corners. - docs: mcp.md (Live Corners), control-protocol.md (Corners dialog), ROADMAP. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
200 lines
8.9 KiB
Rust
200 lines
8.9 KiB
Rust
//! Corners: double-clicking a Live Corners widget (or `ui.corners`) sets the corner kind and
|
||
//! radius of a live rectangle's corners: those whose widgets show (the Direct-Selected ones, else
|
||
//! all four). OK runs `object.setLiveShape` for them, one undo step.
|
||
//!
|
||
//! Fields: `id`, `corners` (indices 0–3: top-left, top-right, bottom-right, bottom-left), `kind`
|
||
//! ("round", "invertedRound" or "chamfer") and `radius` (pt). While the corners differ in kind
|
||
//! or radius, that field is absent and OK leaves it as it is.
|
||
|
||
use serde_json::{Value, json};
|
||
use vectorcraft_engine::doc::{NodeId, NodeKind};
|
||
use vectorcraft_geom::shapes::CornerKind;
|
||
|
||
use super::swatch_options::{grid, label};
|
||
use super::{DialogSpec, form, run_and_close};
|
||
use crate::state::Dialog;
|
||
use crate::{VectorcraftApp, widgets};
|
||
|
||
/// The dialog kind of Corners (the corner widgets' double-click).
|
||
pub const KIND: &str = vectorcraft_tools::corners::DIALOG;
|
||
|
||
pub(super) const SPEC: DialogSpec = DialogSpec { heading: |_| tl!("Corners").into(), body, confirm, min_width: 280.0, ..DialogSpec::FORM };
|
||
|
||
/// Open Corners for `{id?, corners?}`: the live rectangle `id` (default: the selected one) and its
|
||
/// `corners` (default: the selected corners), filled in with their kind and radius.
|
||
pub fn open(app: &mut VectorcraftApp, p: &Value) -> Result<Value, String> {
|
||
let st = app.session.active().ok_or("no document")?;
|
||
let id = match p.get("id").and_then(Value::as_u64) {
|
||
Some(id) => NodeId(id),
|
||
None => match st.selection.objects[..] {
|
||
[id] => id,
|
||
_ => return Err("select one live rectangle, or give its `id`".into()),
|
||
},
|
||
};
|
||
let Some(NodeKind::Path { live: Some(live @ vectorcraft_engine::doc::LiveShape::Rectangle { .. }), .. }) = st.doc.node(id).map(|n| &n.kind)
|
||
else {
|
||
return Err("Corners edits a live rectangle".into());
|
||
};
|
||
let corners = match p.get("corners").and_then(Value::as_array) {
|
||
Some(a) => std::array::from_fn(|k| a.iter().any(|v| v.as_u64() == Some(k as u64))),
|
||
None => live.picked_corners(st.selection.partial(id)),
|
||
};
|
||
if !corners.contains(&true) {
|
||
return Err("give corners 0–3".into());
|
||
}
|
||
let (radius, kind) = live.corner_style(corners);
|
||
let mut fields = json!({"id": id.0, "corners": (0..4).filter(|k| corners[*k]).collect::<Vec<_>>()});
|
||
if let Some(k) = kind {
|
||
fields["kind"] = json!(k);
|
||
}
|
||
if let Some(r) = radius {
|
||
fields["radius"] = json!(r);
|
||
}
|
||
app.ui.dialog = Some(Dialog::new(KIND, fields));
|
||
Ok(Value::Null)
|
||
}
|
||
|
||
fn body(app: &mut VectorcraftApp, ui: &mut egui::Ui, d: &mut Dialog) -> bool {
|
||
let unit = app.session.general_unit();
|
||
let current = d.fields.get("kind").and_then(|k| serde_json::from_value::<CornerKind>(k.clone()).ok());
|
||
grid(ui, |ui| {
|
||
label(ui, tl!("Corner:"));
|
||
ui.vertical(|ui| {
|
||
for k in CornerKind::ALL {
|
||
let name = match k {
|
||
CornerKind::Round => tl!("Round"),
|
||
CornerKind::InvertedRound => tl!("Inverted Round"),
|
||
CornerKind::Chamfer => tl!("Chamfer"),
|
||
};
|
||
if widgets::radio(ui, name, Some(k) == current, true) {
|
||
d.fields.insert("kind".into(), json!(k));
|
||
}
|
||
}
|
||
});
|
||
ui.end_row();
|
||
label(ui, tl!("Radius:"));
|
||
form::length(ui, d, "radius", unit, 120.0);
|
||
ui.end_row();
|
||
});
|
||
false
|
||
}
|
||
|
||
fn confirm(app: &mut VectorcraftApp, d: &Dialog) -> Result<Value, String> {
|
||
let mut p = json!({"id": d.fields.get("id"), "corners": d.fields.get("corners")});
|
||
if let Some(k) = d.fields.get("kind") {
|
||
p["kind"] = k.clone();
|
||
}
|
||
if d.fields.contains_key("radius") {
|
||
p["radius"] = json!(d.f64("radius", 0.0));
|
||
}
|
||
run_and_close(app, "object.setLiveShape", p)
|
||
}
|
||
|
||
#[cfg(test)]
|
||
mod tests {
|
||
use super::*;
|
||
use vectorcraft_engine::Session;
|
||
use vectorcraft_engine::doc::LiveShape;
|
||
use vectorcraft_tools::corners::CornerWidgets;
|
||
use vectorcraft_tools::{PointerEvent, PointerKind};
|
||
|
||
/// A selected 100 pt live square at (100, 100).
|
||
fn app() -> (VectorcraftApp, u64) {
|
||
let mut app = VectorcraftApp::new(Session::new(), Default::default());
|
||
app.run("file.new", json!({"width": 400, "height": 400})).unwrap();
|
||
let id = app.run("shape.rectangle", json!({"x": 100, "y": 100, "width": 100, "height": 100})).unwrap()["id"].as_u64().unwrap();
|
||
(app, id)
|
||
}
|
||
|
||
fn live(app: &VectorcraftApp, id: u64) -> LiveShape {
|
||
let Some(NodeKind::Path { live: Some(l), .. }) = app.session.active().unwrap().doc.node(NodeId(id)).map(|n| n.kind.clone()) else {
|
||
panic!("not live")
|
||
};
|
||
l
|
||
}
|
||
|
||
fn corners(app: &VectorcraftApp, id: u64) -> ([f64; 4], [CornerKind; 4]) {
|
||
let LiveShape::Rectangle { radii, kinds, .. } = live(app, id) else { panic!("not a rectangle") };
|
||
(radii, kinds)
|
||
}
|
||
|
||
/// The radius the Properties panel's Corner Radius field shows.
|
||
fn panel_radius(app: &VectorcraftApp, id: u64) -> Option<f64> {
|
||
let n = app.session.active().unwrap().doc.node(NodeId(id)).unwrap().clone();
|
||
crate::panels::corner_radius(app, &n, &live(app, id))
|
||
}
|
||
|
||
fn shown(app: &mut VectorcraftApp) -> Vec<String> {
|
||
fn texts(s: &egui::Shape, out: &mut Vec<String>) {
|
||
match s {
|
||
egui::Shape::Text(t) => out.push(t.galley.text().to_string()),
|
||
egui::Shape::Vec(v) => v.iter().for_each(|s| texts(s, out)),
|
||
_ => {}
|
||
}
|
||
}
|
||
let ctx = egui::Context::default();
|
||
crate::theme::install_fonts(&ctx);
|
||
// A new window lays itself out in its first frame and paints in the next.
|
||
ctx.run_ui(Default::default(), |ui| super::super::show(app, ui.ctx())).textures_delta.clear();
|
||
let mut out = ctx.run_ui(Default::default(), |ui| super::super::show(app, ui.ctx()));
|
||
out.textures_delta.clear();
|
||
let mut v = vec![];
|
||
out.shapes.iter().for_each(|c| texts(&c.shape, &mut v));
|
||
v
|
||
}
|
||
|
||
#[test]
|
||
fn double_clicking_a_direct_selected_corner_edits_it_alone() {
|
||
let (mut app, id) = app();
|
||
// Direct Selection picks the bottom-right anchor: only its widget shows.
|
||
app.run("select.anchors", json!({"id": id, "anchors": [[0, 2]], "mode": "set"})).unwrap();
|
||
app.select_tool("directSelection");
|
||
let view = app.view_info();
|
||
let st = app.session.active().unwrap();
|
||
let at: Vec<_> = CornerWidgets::of(&st.doc, &st.selection, view.zoom).unwrap().visible().collect();
|
||
assert_eq!(at.len(), 1);
|
||
crate::canvas::dispatch(&mut app, &PointerEvent::new(PointerKind::DoubleClick, at[0].x, at[0].y), view);
|
||
let d = app.ui.dialog.clone().expect("Corners opened");
|
||
assert_eq!(
|
||
(d.kind.as_str(), d.fields.get("corners"), d.fields.get("kind"), d.f64("radius", -1.0)),
|
||
(KIND, Some(&json!([2])), Some(&json!("round")), 0.0)
|
||
);
|
||
let texts = shown(&mut app);
|
||
for s in ["Corners", "Corner:", "Round", "Inverted Round", "Chamfer", "Radius:"] {
|
||
assert!(texts.iter().any(|t| t == s), "{s} in {texts:?}");
|
||
}
|
||
let d = app.ui.dialog.as_mut().unwrap();
|
||
d.fields.insert("kind".into(), json!("chamfer"));
|
||
d.fields.insert("radius".into(), json!(12));
|
||
super::super::confirm(&mut app).unwrap();
|
||
assert!(app.ui.dialog.is_none());
|
||
assert_eq!(corners(&app, id), ([0.0, 0.0, 12.0, 0.0], [CornerKind::Round, CornerKind::Round, CornerKind::Chamfer, CornerKind::Round]));
|
||
// The panels' radius field shows the selected corner's radius, blank for the whole
|
||
// shape (its corners differ).
|
||
assert_eq!(panel_radius(&app, id), Some(12.0));
|
||
app.run("select.set", json!({"ids": [id]})).unwrap();
|
||
assert_eq!(panel_radius(&app, id), None);
|
||
}
|
||
|
||
#[test]
|
||
fn ui_corners_opens_on_the_selection_and_leaves_mixed_values_alone() {
|
||
let (mut app, id) = app();
|
||
app.run("object.setLiveShape", json!({"corners": [0], "radius": 5, "kind": "invertedRound"})).unwrap();
|
||
app.run("ui.corners", json!({})).unwrap();
|
||
let d = app.ui.dialog.clone().unwrap();
|
||
assert_eq!(d.fields.get("corners"), Some(&json!([0, 1, 2, 3])));
|
||
assert!(d.fields.get("kind").is_none() && d.fields.get("radius").is_none(), "mixed: {d:?}");
|
||
let before = corners(&app, id);
|
||
super::super::confirm(&mut app).unwrap();
|
||
assert_eq!(corners(&app, id), before);
|
||
// A radius for all four keeps their kinds.
|
||
app.run("ui.corners", json!({"id": id})).unwrap();
|
||
app.ui.dialog.as_mut().unwrap().fields.insert("radius".into(), json!("3 pt"));
|
||
super::super::confirm(&mut app).unwrap();
|
||
assert_eq!(corners(&app, id), ([3.0; 4], before.1));
|
||
// Only live rectangles, and only real corners.
|
||
assert!(app.run("ui.corners", json!({"id": 9999})).is_err());
|
||
assert!(app.run("ui.corners", json!({"corners": [7]})).is_err());
|
||
}
|
||
}
|