3D: depth, bevel and offset fields show the document's unit

Extrude & Bevel's Depth and bevel Width and Height, Revolve's Offset,
the classic Extrude & Bevel and Revolve rows, and the Map Art X and Y
rows showed points in a document set to millimeters, inches or another
unit. Each row now goes through Param::length, so its field shows and
reads the document's unit while the effect keeps points. The rows set
no decimals, so a typed 10 mm is kept unrounded and Up steps by the
shown unit's own step.
This commit is contained in:
dotCooCoo committed 2026-11-29 11:53:39 -08:00
1 parent 9bbc3950e1
commit 4f6eb4b36d
3 files changed
+93 -30

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]
+29 -19
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));
@@ -505,7 +515,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 +525,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),
});
}
}