Preserve cap, join and alignment in the Stroke editor

This commit is contained in:
dotCooCoo committed 2026-11-26 19:31:48 -08:00
1 parent 0aea8fed95
commit d2cd1d0e76
2 files changed
+114 -33

No files matched your search

+19 -33
View File
@@ -24,11 +24,9 @@ use craft::ui::style::DataColor;
use craft::ui::text::{LinkMode, LinkedFields};
use craft::ui::text::{NumberField, NumberOutput, NumberPick, StepArrows};
use egui::{Rect, Ui, pos2};
use serde::Serialize;
use serde::de::DeserializeOwned;
use serde_json::{Value, json};
use vectorcraft_color::{Color, Paint};
use vectorcraft_doc::{Appearance, ArrowAlign, Arrowhead, Dash, Document, Node, StrokeLayer, Unit, WidthProfile};
use vectorcraft_doc::{Appearance, ArrowAlign, Arrowhead, Dash, Document, LineCap, LineJoin, Node, StrokeAlign, StrokeLayer, Unit, WidthProfile};
use vectorcraft_engine::inspect::StrokeMixed;
use super::{Live, character, current_stroke, live_run, stroke_mixed};
@@ -171,11 +169,6 @@ pub(crate) fn shown_weight(app: &VectorcraftApp, st: Option<&StrokeLayer>, mixed
(!mixed.weight).then(|| st.map_or(app.session.paint.stroke_width, |s| s.width))
}
/// A document enum as the editor's enum of the same name: both serialise as the same camelCase word.
fn convert<A: Serialize, B: DeserializeOwned + Default>(a: A) -> B {
serde_json::to_value(a).ok().and_then(|v| serde_json::from_value(v).ok()).unwrap_or_default()
}
/// The style the stroke editor shows for `st` (the defaults without a stroke). Dashes count document units, so the
/// pattern is the stroke's own numbers.
pub(crate) fn style_of(st: Option<&StrokeLayer>) -> draw::StrokeStyle {
@@ -185,9 +178,21 @@ pub(crate) fn style_of(st: Option<&StrokeLayer>) -> draw::StrokeStyle {
s.dash.as_ref().map_or((vec![], 0.0, false), |d| (d.pattern.iter().map(|v| *v as f32).collect(), d.offset, d.align_corners));
draw::StrokeStyle {
width: s.width as f32,
align: convert(s.align),
cap: convert(s.cap),
join: convert(s.join),
align: match s.align {
StrokeAlign::Center => draw::StrokeAlign::Center,
StrokeAlign::Inside => draw::StrokeAlign::Inside,
StrokeAlign::Outside => draw::StrokeAlign::Outside,
},
cap: match s.cap {
LineCap::Butt => draw::LineCap::Butt,
LineCap::Round => draw::LineCap::Round,
LineCap::Square => draw::LineCap::Square,
},
join: match s.join {
LineJoin::Miter => draw::LineJoin::Miter,
LineJoin::Round => draw::LineJoin::Round,
LineJoin::Bevel => draw::LineJoin::Bevel,
},
miter_limit: s.miter_limit as f32,
dashes,
dash_offset: offset as f32,
@@ -212,13 +217,12 @@ fn num(v: f32) -> f64 {
/// The `stroke.set` params of an edit of `field` that turned `old` into `new`: only what the field changes, so
/// each selected stroke keeps its other options (its dash offset and alignment included).
pub(crate) fn params_of(old: &draw::StrokeStyle, new: &draw::StrokeStyle, field: StrokeField) -> Value {
let word = |v: &dyn erased::Word| v.word();
let dash = if new.dashes.is_empty() { Value::Null } else { json!(new.dashes.iter().map(|v| num(*v)).collect::<Vec<_>>()) };
let corners = new.dash_align == draw::DashAlign::Corners;
match field {
StrokeField::Align => json!({"align": word(&new.align)}),
StrokeField::Cap => json!({"cap": word(&new.cap)}),
StrokeField::Join => json!({"join": word(&new.join)}),
StrokeField::Align => json!({"align": new.align}),
StrokeField::Cap => json!({"cap": new.cap}),
StrokeField::Join => json!({"join": new.join}),
StrokeField::MiterLimit => json!({"miterLimit": num(new.miter_limit)}),
StrokeField::Dashed => json!({"dash": dash, "alignDashes": corners}),
StrokeField::Dash(_) => json!({"dash": dash}),
@@ -231,24 +235,6 @@ pub(crate) fn params_of(old: &draw::StrokeStyle, new: &draw::StrokeStyle, field:
}
}
/// The word `stroke.set` reads for an editor enum.
mod erased {
use serde::Serialize;
use serde_json::Value;
/// An enum that serialises as its camelCase word.
pub trait Word {
/// The word.
fn word(&self) -> Value;
}
impl<T: Serialize> Word for T {
fn word(&self) -> Value {
serde_json::to_value(self).unwrap_or(Value::Null)
}
}
}
/// Sends an editor edit of `field` (`old` to `new`) to the strokes. Turning dashes off remembers the pattern; turning
/// them on brings back the remembered one, or 12 pt dashes fitted to corners.
fn apply_style(app: &mut VectorcraftApp, old: &draw::StrokeStyle, mut new: draw::StrokeStyle, field: StrokeField) {
@@ -13,6 +13,101 @@ use crate::tests::panel_harness::{Panel, ROOM};
type Show = fn(&mut VectorcraftApp, &mut Ui);
#[test]
fn stroke_style_choices_cross_the_editor_bridge_in_both_directions() {
use craft::draw::ui::stroke::{self as draw, StrokeField};
use vectorcraft_doc::{LineCap, LineJoin, StrokeAlign};
let mut app = app_with_rect();
run(&mut app, "paint.setStroke", json!({"color": "#222222"}));
let mut document = current_stroke(&app).unwrap();
let rows = [
(LineCap::Butt, draw::LineCap::Butt, "butt"),
(LineCap::Round, draw::LineCap::Round, "round"),
(LineCap::Square, draw::LineCap::Square, "square"),
];
for (choice, shown, keyword) in rows {
document.cap = choice;
let style = stroke::style_of(Some(&document));
assert_eq!(style.cap, shown);
let params = stroke::params_of(&draw::StrokeStyle::default(), &style, StrokeField::Cap);
run(&mut app, "stroke.set", params.clone());
assert_eq!(params, json!({"cap": keyword}));
assert_eq!(current_stroke(&app).unwrap().cap, choice);
}
let rows = [
(LineJoin::Miter, draw::LineJoin::Miter, "miter"),
(LineJoin::Round, draw::LineJoin::Round, "round"),
(LineJoin::Bevel, draw::LineJoin::Bevel, "bevel"),
];
for (choice, shown, keyword) in rows {
document.join = choice;
let style = stroke::style_of(Some(&document));
assert_eq!(style.join, shown);
let params = stroke::params_of(&draw::StrokeStyle::default(), &style, StrokeField::Join);
run(&mut app, "stroke.set", params.clone());
assert_eq!(params, json!({"join": keyword}));
assert_eq!(current_stroke(&app).unwrap().join, choice);
}
let rows = [
(StrokeAlign::Center, draw::StrokeAlign::Center, "center"),
(StrokeAlign::Inside, draw::StrokeAlign::Inside, "inside"),
(StrokeAlign::Outside, draw::StrokeAlign::Outside, "outside"),
];
for (choice, shown, keyword) in rows {
document.align = choice;
let style = stroke::style_of(Some(&document));
assert_eq!(style.align, shown);
let params = stroke::params_of(&draw::StrokeStyle::default(), &style, StrokeField::Align);
run(&mut app, "stroke.set", params.clone());
assert_eq!(params, json!({"align": keyword}));
assert_eq!(current_stroke(&app).unwrap().align, choice);
}
}
#[test]
fn compact_stroke_miter_limit_only_applies_to_miter_joins() {
use craft::ui::contract::Role;
use egui::{Key, Pos2, Rect};
use vectorcraft_doc::LineJoin;
for (join, width) in [("round", crate::panel_docking::PANEL_MIN_WIDTH), ("bevel", 320.0)] {
let mut app = app_with_rect();
app.session.prefs.interface_language = "en".into();
run(&mut app, "paint.setStroke", json!({"color": "#222222"}));
run(&mut app, "stroke.set", json!({"join": join, "miterLimit": 4}));
let original = current_stroke(&app).unwrap();
let room = egui::vec2(width, 500.0);
let mut panel = Panel::new(app, stroke::show);
panel.room(room).frame();
let baseline = panel.app().session.active().unwrap().history.undo.len();
let edit = |panel: &mut Panel| {
let field = panel.rect_role("Miter limit", Role::SPIN_BUTTON);
assert!(Rect::from_min_size(Pos2::ZERO, room).contains_rect(field));
let value = panel.app().host.i18n().unwrap().format().number(8.5, 1);
panel.click_at(field.center());
panel.key(Key::A, Modifiers::COMMAND);
panel.type_text(&value);
panel.key(Key::Enter, Modifiers::NONE);
};
edit(&mut panel);
assert_eq!(current_stroke(&panel.app()).unwrap(), original);
assert_eq!(panel.app().session.active().unwrap().history.undo.len(), baseline);
panel.click("Miter");
let miter = current_stroke(&panel.app()).unwrap();
assert_eq!(miter.join, LineJoin::Miter);
edit(&mut panel);
let mut expected = miter.clone();
expected.miter_limit = 8.5;
assert_eq!(current_stroke(&panel.app()).unwrap(), expected);
assert_eq!(panel.app().session.active().unwrap().history.undo.len(), baseline + 2);
panel.app().run("edit.undo", json!({})).unwrap();
assert_eq!(current_stroke(&panel.app()).unwrap(), miter);
panel.app().run("edit.undo", json!({})).unwrap();
assert_eq!(current_stroke(&panel.app()).unwrap(), original);
}
}
#[test]
fn weights_and_dashes_show_in_the_stroke_unit_everywhere() {
let mut app = app_with_rect();