Merge pull request #10122 from dotCooCoo/fix/3d-depth-rows-document-unit

3D: depth, bevel and offset fields show the document's unit
This commit is contained in:
.CooCoo authored and GitHub committed 2026-11-29 18:20:59 -08:00
commit b4f0a7f838
3 files changed
+126 -34

No files matched your search

+4 -6
View File
@@ -14,7 +14,6 @@ use craft::ui::contract::{Bind, CollectionEvent, Gap, Props as _, Show as _, Sho
use craft::ui::layout_components::{Row, Stack};
use craft::ui::patterns::form_dialogs::{CustomContext, DialogFields, FieldValue, HostedParamDialog, Param, ParamKind, ParamRows};
use craft::ui::pickers::Select;
use craft::ui::runtime::env::LengthUnits;
use craft::ui::runtime::env::UnitKind;
use craft::ui::sections::{DialogRole, FooterButton};
use craft::ui::tabs::{Tab, TabKind, TabStrip};
@@ -194,12 +193,11 @@ impl Rows<'_> {
self.range(key, label, range, 1, UnitKind::Percent)
}
/// A length in points, its unit shown as [`length_unit`](super::three_d_classic::length_unit) says.
/// A length in points, shown in the general unit as [`length_units`](super::three_d_classic::length_units) says.
fn length(&self, key: &str, label: &'static str, range: (f64, f64)) -> Param {
// Stored in points; a pixel is a point, as the rulers count it.
let lengths = LengthUnits::new(UnitKind::Points, crate::canvas::rulers::unit_kind(self.unit), 72.0);
let shown = super::three_d_classic::length_unit(self.unit);
self.range(key, label, range, 1, shown).length(crate::fields::bare_points(lengths, self.bare_points))
let lengths = super::three_d_classic::length_units(self.unit, self.bare_points);
let row = Param::new(key, label, ParamKind::range(range.0, range.1, self.default_number(key), SliderScale::Linear)).resettable();
super::three_d_classic::as_length(row, lengths)
}
fn flag(&self, key: &str, label: &'static str) -> Param {
+60 -5
View File
@@ -494,10 +494,10 @@ fn the_bevel_repeat_and_the_revolve_segments_show_no_unit() {
assert_eq!(unit(EXTRUDE, "object", "taper"), Some(Some(UnitKind::Percent)));
}
/// The depth, bevel and offset rows of Extrude & Bevel and Revolve, classic ones included, are lengths in points: their
/// sliders show "pt" in a points document, "px" in a pixels one, and points in a document of any other unit.
/// The depth, bevel and offset rows of Extrude & Bevel and Revolve, classic ones included, hold points and show the
/// document's unit: "pt" in a points document, "px" in a pixels one, "mm" in a millimeters one, "in" in an inches one.
#[test]
fn the_3d_length_rows_show_the_unit_of_the_number_they_hold() {
fn the_3d_length_rows_show_the_document_unit() {
use craft::ui::patterns::form_dialogs::DialogFields;
use craft::ui::runtime::env::UnitKind;
use vectorcraft_doc::Unit;
@@ -510,7 +510,13 @@ fn the_3d_length_rows_show_the_unit_of_the_number_they_hold() {
super::params(&data, &fields, super::Place::Dialog).into_iter().find(|p| p.key == key).map(|p| p.unit)
};
let mut checked = 0;
for (general, shown) in [(Unit::Points, UnitKind::Points), (Unit::Pixels, UnitKind::Pixels), (Unit::Millimeters, UnitKind::Points)] {
let units = [
(Unit::Points, UnitKind::Points),
(Unit::Pixels, UnitKind::Pixels),
(Unit::Millimeters, UnitKind::Millimetres),
(Unit::Inches, UnitKind::Inches),
];
for (general, shown) in units {
for key in ["depth", "bevelWidth", "bevelHeight"] {
assert_eq!(unit(general, EXTRUDE, "object", key), Some(Some(shown)), "{key} in {general:?}");
checked += 1;
@@ -526,7 +532,56 @@ fn the_3d_length_rows_show_the_unit_of_the_number_they_hold() {
checked += 1;
}
}
assert_eq!(checked, 21);
assert_eq!(checked, 28);
}
/// In a millimeters document the 3D dialogs' depth and offset fields show the 72 pt they hold as 25.4 mm, and a
/// number typed there is read in millimeters and kept unrounded; Up steps by a millimeter step.
#[test]
fn a_3d_dialog_in_a_millimeters_document_shows_its_lengths_in_millimeters() {
use craft::ui::contract::Role;
let rows = [
(CLASSIC_EXTRUDE, "object", "depth", "Depth"),
(CLASSIC_REVOLVE, "object", "offset", "Offset"),
(EXTRUDE, "object", "depth", "Depth"),
(REVOLVE, "rotation", "offset", "Offset"),
];
for (effect, page, key, name) in rows {
let mut app = VectorcraftApp::new(Session::new(), Default::default());
app.run("file.new", json!({"width":300,"height":300,"units":"Millimeters"})).unwrap();
app.run("path.create", json!({"d": SQUARE})).unwrap();
let mut h = opened(app, json!({"effect": effect, "item": null}));
h.set("__page", json!(page));
h.set(key, json!(72.0));
let field = |h: &ThreeD| {
let tree = h.fixture.tree();
h.fixture
.by_role(Role::SPIN_BUTTON)
.into_iter()
.find(|t| tree.name_of(t.node) == Some(name))
.unwrap_or_else(|| panic!("{effect}: no {name} field"))
};
let shown = h.fixture.tree().value_of(field(&h).node).map(str::to_owned);
assert!(shown.as_deref().is_some_and(|s| s.contains("25.4") && s.contains("mm")), "{effect} {key}: 72 pt shows as 25.4 mm: {shown:?}");
let target = field(&h);
h.fixture.click(&target);
// Tab commits the field and leaves the dialog open (Enter would press OK).
h.fixture.key(Key::A, Modifiers::COMMAND).type_text("10").key(Key::Tab, Modifiers::NONE);
h.settle();
let stored = h.json(key);
assert!(
stored.as_f64().is_some_and(|points| (points - 10.0 * 72.0 / 25.4).abs() < 1e-3),
"{effect} {key}: 10 mm is kept in points: {stored:?}"
);
// Up moves by the shown unit's own step, at least a tenth of a millimeter.
let target = field(&h);
h.fixture.click(&target);
h.fixture.key(Key::ArrowUp, Modifiers::NONE).key(Key::Tab, Modifiers::NONE);
h.settle();
let moved = h.json(key).as_f64().zip(stored.as_f64()).map(|(after, before)| after - before);
assert!(moved.is_some_and(|by| by >= 0.1 * 72.0 / 25.4 - 1e-6), "{effect} {key}: Up moves at least 0.1 mm: {moved:?}");
h.cancel();
}
}
#[test]
+62 -23
View File
@@ -20,7 +20,7 @@ use craft::ui::list::ThumbAspect;
use craft::ui::patterns::form_dialogs::{CustomContext, DialogFields, FieldValue, HostedParamDialog, Param, ParamKind};
use craft::ui::pickers::Select;
use craft::ui::primitives::ContentPainterCx;
use craft::ui::runtime::env::{Env, UnitKind};
use craft::ui::runtime::env::{Env, LengthUnits, UnitKind};
use craft::ui::sections::{DialogRole, FooterButton};
use craft::ui::style::DataColor;
use craft::ui::text::NumberField;
@@ -50,13 +50,23 @@ const MAP_SYMBOL: &str = "mapSymbol";
/// The Map Art page's rows of the shown surface's map: views of `mapArt`, never effect parameters.
pub(super) const MAP_ROWS: [&str; 6] = [MAP_SYMBOL, MAP_KEYS[0], MAP_KEYS[1], MAP_KEYS[2], MAP_KEYS[3], MAP_KEYS[4]];
/// The unit shown after a 3D length row, whose slider shows the number of points it holds: the general `unit` when
/// that counts the same number (points, or pixels at 72 per inch), and points otherwise.
pub(super) fn length_unit(unit: Unit) -> UnitKind {
match crate::canvas::rulers::unit_kind(unit) {
UnitKind::Pixels => UnitKind::Pixels,
_ => UnitKind::Points,
}
/// The lengths of a 3D length row: stored in points and shown in the general `unit` (a pixel is a point, as the rulers
/// count it), a bare number read as Numbers Without Units Are Points (`bare_points`) says.
pub(super) fn length_units(unit: Unit, bare_points: bool) -> LengthUnits {
crate::fields::bare_points(LengthUnits::new(UnitKind::Points, crate::canvas::rulers::unit_kind(unit), 72.0), bare_points)
}
/// `row`, a range row of points with no decimals set, as a length shown in `lengths.shown`. Without set decimals the
/// field keeps the shown unit's own places and step, and the points are kept unrounded, so a value typed in
/// millimeters or inches reads back as typed.
pub(super) fn as_length(row: Param, lengths: LengthUnits) -> Param {
row.unit(lengths.shown).length(lengths)
}
/// A length row over `range` points with the effect's default, shown as [`as_length`] says.
fn length_row(effect: &str, key: &str, label: &'static str, range: (f64, f64), lengths: LengthUnits) -> Param {
let default = vectorcraft_effects::default_params(effect).and_then(|p| p.get(key).and_then(Value::as_f64)).unwrap_or(range.0);
as_length(Param::new(key, label, ParamKind::range(range.0, range.1, default, SliderScale::Linear)).resettable(), lengths)
}
/// Where the rows show: the 3D and Materials panel marks its fields with `__panel`.
@@ -120,15 +130,15 @@ fn map_art_button(fields: &DialogFields) -> Option<Param> {
/// Extrude & Bevel (Classic)'s Object page: depth and cap, then the bevel: its shape (a custom one included), its
/// height and whether it is added around the shape or carved into it.
pub(crate) fn object_params(data: &Data, fields: &DialogFields, effect: &str) -> Vec<Param> {
let unit = Some(length_unit(data.unit));
let lengths = length_units(data.unit, data.bare_points);
let shaped: Vec<&str> = options_of(effect, "bevel").into_iter().map(|(value, _)| value).filter(|v| *v != "none").collect();
let mut rows = vec![
Param::section(tl!("Object")),
range_row(effect, "depth", tl!("Depth"), (0.0, 2000.0), unit, 1),
length_row(effect, "depth", tl!("Depth"), (0.0, 2000.0), lengths),
cap_row(effect),
Param::section(c3("Bevel")),
bevel_row(data, CLASSIC_EXTRUDE, place_of(fields)),
range_row(effect, "bevelHeight", tl!("Height"), (1.0, 100.0), unit, 1).enabled_when_in("bevel", shaped.clone()),
length_row(effect, "bevelHeight", tl!("Height"), (1.0, 100.0), lengths).enabled_when_in("bevel", shaped.clone()),
toggle_row(effect, "bevelInside", tl!("Bevel Inside")).enabled_when_in("bevel", shaped),
];
rows.extend(map_art_button(fields));
@@ -141,7 +151,7 @@ pub(crate) fn revolve_params(data: &Data, fields: &DialogFields, effect: &str) -
Param::section(tl!("Object")),
range_row(effect, "angle", tl!("Angle"), (0.0, 360.0), Some(UnitKind::Degrees), 1),
cap_row(effect),
range_row(effect, "offset", tl!("Offset"), (0.0, 1000.0), Some(length_unit(data.unit)), 1),
length_row(effect, "offset", tl!("Offset"), (0.0, 1000.0), length_units(data.unit, data.bare_points)),
choice_row(effect, "edge", tl!("Axis side")),
];
rows.extend(map_art_button(fields));
@@ -272,8 +282,9 @@ fn set_map_value(fields: &mut DialogFields, key: &str, value: Value) -> Result<(
let n = value.as_f64().filter(|n| n.is_finite()).ok_or("not a number")?;
let mut m = old.ok_or("the surface has no map")?;
match key {
"mapX" => m.x = round_clamp(n, 2, -GRAPHIC_MAX_OFFSET, GRAPHIC_MAX_OFFSET),
"mapY" => m.y = round_clamp(n, 2, -GRAPHIC_MAX_OFFSET, GRAPHIC_MAX_OFFSET),
// X and Y arrive in points converted from the document's unit: rounding them would move a typed 10 mm.
"mapX" => m.x = n.clamp(-GRAPHIC_MAX_OFFSET, GRAPHIC_MAX_OFFSET),
"mapY" => m.y = n.clamp(-GRAPHIC_MAX_OFFSET, GRAPHIC_MAX_OFFSET),
"mapScaleX" => m.scale_x = round_clamp(n, 2, GRAPHIC_SCALE_RANGE.0, GRAPHIC_SCALE_RANGE.1),
"mapScaleY" => m.scale_y = round_clamp(n, 2, GRAPHIC_SCALE_RANGE.0, GRAPHIC_SCALE_RANGE.1),
"mapRotation" => m.rotation = n.clamp(-360.0, 360.0),
@@ -370,6 +381,12 @@ fn map_of_box(state: &BoxState, rect: Rect, base: &SurfaceMap) -> SurfaceMap {
}
}
/// The map a box edit leaves, or none while the box is still the one `map` drew (`drawn`), so a frame without an edit
/// keeps the map's numbers exactly as typed.
fn map_after_edit(state: &BoxState, drawn: &BoxState, rect: Rect, map: &SurfaceMap) -> Option<SurfaceMap> {
(state != drawn).then(|| map_of_box(state, rect, map)).filter(|moved| moved != map)
}
/// The id of Map Art's Done, in the footer as Reset all is on the other pages.
pub(crate) const MAP_DONE: &str = "mapDone";
@@ -406,6 +423,7 @@ fn flat_view(ui: &mut Ui, fields: &mut DialogFields, flat: &Flat) -> Response {
}
let (Some(m), Some(rect)) = (map.as_ref(), rect) else { return };
let mut state = map_box(m, rect);
let drawn = state;
if m.rotation.abs() < 0.01
&& let (Some(doc), Some((w, h))) = (flat.doc.as_ref(), flat.art)
{
@@ -422,8 +440,7 @@ fn flat_view(ui: &mut Ui, fields: &mut DialogFields, flat: &Flat) -> Response {
.reference_point(false)
.rotate_zone(RotateZone::border(18.0))
.show_at(ui, body.rect);
let moved = map_of_box(&state, rect, m);
if moved != *m {
if let Some(moved) = map_after_edit(&state, &drawn, rect, m) {
edited = Some(moved);
}
});
@@ -505,7 +522,7 @@ pub(crate) fn map_art_params(data: &Data, fields: &DialogFields, effect: &str) -
let flat_row = flat.clone();
rows.push(Param::custom("__mapFlat", "", move |ui, fields, _| flat_view(ui, fields, &flat_row)).full_width());
if let Some(m) = &map {
let unit = Some(length_unit(data.unit));
let lengths = length_units(data.unit, data.bare_points);
let values = [m.x, m.y, m.scale_x, m.scale_y, m.rotation];
let labels = ["X", "Y", tl!("Horizontal Scale"), tl!("Vertical Scale"), tl!("Rotation")];
let bounds = [
@@ -515,14 +532,14 @@ pub(crate) fn map_art_params(data: &Data, fields: &DialogFields, effect: &str) -
GRAPHIC_SCALE_RANGE,
(-360.0, 360.0),
];
let kinds = [unit, unit, Some(UnitKind::Percent), Some(UnitKind::Percent), Some(UnitKind::Degrees)];
// X and Y are lengths in points; the scales are shares and the rotation an angle.
let kinds = [None, None, Some(UnitKind::Percent), Some(UnitKind::Percent), Some(UnitKind::Degrees)];
for i in 0..MAP_KEYS.len() {
let row = Param::new(MAP_KEYS[i], labels[i], ParamKind::range(bounds[i].0, bounds[i].1, values[i], SliderScale::Linear))
.decimals(2)
.on_set(set_map_value);
let row =
Param::new(MAP_KEYS[i], labels[i], ParamKind::range(bounds[i].0, bounds[i].1, values[i], SliderScale::Linear)).on_set(set_map_value);
rows.push(match kinds[i] {
Some(kind) => row.unit(kind),
None => row,
Some(kind) => row.decimals(2).unit(kind),
None => as_length(row, lengths),
});
}
}
@@ -575,4 +592,26 @@ mod tests {
let turned = map(190.0);
assert_eq!(map_of_box(&map_box(&turned, rect), rect, &turned).rotation, -170.0, "a turn wraps to ±180");
}
#[test]
fn map_offsets_keep_the_points_a_document_unit_converts_to() {
// 10 mm, as the X and Y rows hand it over in points.
let ten_mm = 10.0 * 72.0 / 25.4;
let mut fields = DialogFields::new();
store_map(&mut fields, 1, Some(&map(0.0)));
assert_eq!(set_map_value(&mut fields, "mapX", json!(ten_mm)), Ok(()));
assert_eq!(set_map_value(&mut fields, "mapY", json!(-ten_mm)), Ok(()));
let stored = maps_of(&fields).into_iter().find(|m| m.surface == 1).map(|m| (m.x, m.y));
assert_eq!(stored, Some((ten_mm, -ten_mm)), "the typed offset is stored unrounded");
assert_eq!(set_map_value(&mut fields, "mapX", json!(GRAPHIC_MAX_OFFSET * 2.0)), Ok(()));
assert_eq!(maps_of(&fields).first().map(|m| m.x), Some(GRAPHIC_MAX_OFFSET), "the offset still keeps to its bounds");
// A frame without a box edit leaves those numbers as they are.
let rect = art_rect((20.0, 10.0));
let typed = SurfaceMap { x: ten_mm, y: -ten_mm, ..map(0.0) };
let drawn = map_box(&typed, rect);
assert_eq!(map_after_edit(&drawn, &drawn, rect, &typed), None);
let dragged = drawn.moved(Vec2::new(1.0, 0.0));
assert!(map_after_edit(&dragged, &drawn, rect, &typed).is_some(), "a drag still edits the map");
}
}