Keep angles typed in degrees exact when tools and fields read and write them

This commit is contained in:
Keavon Chambers committed 2026-11-26 02:37:14 -08:00
1 parent 9fd1011e1e
commit 80f9dbb992
3 files changed
+27 -3

No files matched your search

@@ -1391,7 +1391,7 @@ pub fn angle_widget(parameter_widgets_info: ParameterWidgetsInfo, number_props:
_ => return widgets,
};
let number_props = bounds_in_unit(number_props, |degrees| angle.unit.from_angle(Angle::from_degrees(degrees)));
let number_props = bounds_in_unit(number_props, |degrees| angle.unit.from_degrees(degrees));
widgets.extend_from_slice(&[
Separator::new(SeparatorStyle::Unrelated).widget_instance(),
number_props
+2 -2
View File
@@ -635,7 +635,7 @@ impl TaggedValue {
/// An angle input's value in degrees, whether stored as a typed angle or as a plain number of degrees.
pub fn as_angle_degrees(&self) -> Option<f64> {
match self {
Self::Angle(angle) => Some(angle.to_angle().degrees()),
Self::Angle(angle) => Some(angle.to_degrees()),
Self::Number(degrees) => Some(*degrees),
_ => None,
}
@@ -648,7 +648,7 @@ impl TaggedValue {
_ => AngleUnit::Degree,
};
Self::Angle(TypedAngle {
value: unit.from_angle(Angle::from_degrees(degrees)),
value: unit.from_degrees(degrees),
unit,
})
}
@@ -319,6 +319,14 @@ impl AngleUnit {
Self::Turn => angle.degrees() / 360.,
}
}
/// A number of degrees measured in this unit, which stays exact in degrees rather than round-tripping through radians.
pub fn from_degrees(self, degrees: f64) -> f64 {
match self {
Self::Degree => degrees,
_ => self.from_angle(Angle::from_degrees(degrees)),
}
}
}
/// A length as typed into an input, kept in its unit so a physical size stays physical when the document's resolution changes.
@@ -361,6 +369,14 @@ impl TypedAngle {
pub fn to_angle(self) -> Angle {
self.unit.to_angle(self.value)
}
/// The angle in degrees, exactly as typed when it was typed in degrees.
pub fn to_degrees(self) -> f64 {
match self.unit {
AngleUnit::Degree => self.value,
_ => self.to_angle().degrees(),
}
}
}
// A plain number wired into a length or angle reads in the unit its field shows, pixels or degrees
@@ -462,4 +478,12 @@ mod tests {
assert_eq!(Angle::from_degrees(1.) + Angle::from_degrees(5.), Angle::from_degrees(6.));
assert_eq!(AngleUnit::Turn.to_angle(0.25), Angle::from_degrees(90.));
}
// 15° doesn't survive the trip through radians bit for bit
#[test]
fn degrees_stay_exact_in_degrees() {
assert_eq!(TypedAngle::degrees(15.).to_degrees(), 15.);
assert_eq!(AngleUnit::Degree.from_degrees(15.), 15.);
assert!((AngleUnit::Turn.from_degrees(90.) - 0.25).abs() < 1e-12);
}
}