fix(ui): the Arbitrary angle rounds in f64 and keeps that value (#13067)

A wide field that shows two decimals (Image Rotation > Arbitrary's Angle)
now rounds its stored value while it is still f64, before the widget
narrows it to f32. A 43x27 ruler's 32.1249984 degrees narrowed to exactly
32.125 and the first frame after opening or Alt+Reset drew 32.13 over the
32.12 the dialog kept. The rounded value is also stored as f64, so OK
sends exactly 32.12 rather than the 32.1199989 an f32 widens to.
This commit is contained in:
RickiNano committed 2026-11-11 16:48:00 +01:00
1 parent 58e0a152b9
commit 7350c8239c
2 files changed
+56 -14

No files matched your search

@@ -5,14 +5,27 @@
//! round. The Angle range is -359.99..359.99, wide, so today's wide-range rounding to one
//! decimal decided the dialog's value; only this field now keeps hundredths.
use super::filter_dialog::{body, open, params_of};
use super::filter_dialog::{body, open, params_of, reset_fields};
use crate::PhotocraftApp;
use serde_json::{Map, Value, json};
/// Draw a dialog body once, as a live dialog does every frame: the drawn fields are what
/// the dialog shows and what OK sends.
fn drawn(f: &mut Map<String, Value>) {
egui::Context::default().run_ui(Default::default(), |ui| body(ui, f)).textures_delta.clear();
drawn_texts(f);
}
/// [`drawn`], returning the text the frame painted.
fn drawn_texts(f: &mut Map<String, Value>) -> Vec<String> {
let mut out = egui::Context::default().run_ui(Default::default(), |ui| body(ui, f));
out.textures_delta.clear();
out.shapes
.iter()
.filter_map(|s| match &s.shape {
egui::Shape::Text(t) => Some(t.galley.text().to_string()),
_ => None,
})
.collect()
}
/// A 318 px square document (the size `ok_runs_the_two_decimal_angle` measures on), a ruler
@@ -83,6 +96,29 @@ fn the_ruler_angle_rounds_at_full_precision_before_the_field_narrows() {
assert_eq!(f["direction"], json!("ccw"));
}
/// #13067: the 43x27 ruler's first drawn frame, after opening and again after Alt+Reset,
/// shows the 32.12 the dialog stores (the unrounded pre-fill narrowed to exactly 32.125
/// and drew 32.13 until the next repaint), and the dialog keeps that 32.12 as the exact
/// f64 OK sends, not the 32.1199989 an f32 field value widens to.
#[test]
fn the_ruler_angle_shows_and_sends_the_exact_rounded_value() {
let (mut app, id, mut f) = ruler_dialog([143, 127]);
let check = |f: &mut Map<String, Value>, when: &str| {
let shown = drawn_texts(f);
assert!(shown.iter().any(|t| t == "32.12"), "{when}: the Angle field shows 32.12, drew {shown:?}");
assert!(!shown.iter().any(|t| t == "32.13"), "{when}: the Angle field must not show 32.13, drew {shown:?}");
assert_eq!(f["angle"].as_f64(), Some(32.12), "{when}: the dialog keeps the exact rounded angle");
assert_eq!(params_of(f)["angle"].as_f64(), Some(32.12), "{when}: OK sends the exact rounded angle");
};
// `ruler_dialog` drew one frame already: the first frame draws the fields as opened.
check(&mut app.ui.dialog_mut(id).unwrap().fields.clone(), "after opening");
check(&mut f, "on the next frame");
// Move off the pre-fill first, so only the reset can bring 32.12 back.
f.insert("angle".into(), json!(5.0));
reset_fields(&app, &mut f);
check(&mut f, "after Alt+Reset");
}
/// The fix is per-field: unrelated number fields keep the precision they have today —
/// wide ranges round to one decimal (in another dialog, and under the same key name),
/// small ranges keep their two.
+18 -12
View File
@@ -1158,7 +1158,16 @@ pub fn body(ui: &mut egui::Ui, f: &mut Map<String, Value>) {
}
match p.kind {
Kind::Range { min, max, default } => {
let src = f.get(&p.key).and_then(Value::as_f64);
// The field's shown precision: small ranges keep their two decimals, and so
// does the Arbitrary angle; the other wide fields round to one (#12035).
let scale = field_scale(&cmd, &p.key, min, max);
let wide_decimals = wide_field_decimals(&cmd, &p.key).is_some();
let round64 = |x: f64| (x * f64::from(scale)).round() / f64::from(scale);
// A wide field showing two decimals rounds while the value is still f64, before
// the widget narrows it: a 43x27 ruler is 32.1249984°, whose f32 cast lands
// exactly on 32.125, and the field drew that half-away as 32.13 on the first
// frame after opening or Alt+Reset, over the 32.12 it stored (#13067).
let src = f.get(&p.key).and_then(Value::as_f64).map(|x| if wide_decimals { round64(x) } else { x });
let mut v = src.unwrap_or(default as f64) as f32;
// A widget edit this frame makes the field's f32 value authoritative (#12035).
let mut edited = false;
@@ -1209,18 +1218,15 @@ pub fn body(ui: &mut egui::Ui, f: &mut Map<String, Value>) {
.changed();
}
});
// The field's shown precision: small ranges keep their two decimals, and so
// does the Arbitrary angle; the other wide fields round to one (#12035).
let scale = field_scale(&cmd, &p.key, min, max);
// A field showing two decimals rounds while the value is still f64: a 43x27
// ruler is 32.1249984°, whose f32 cast lands exactly on 32.125, and rounding
// that widened value half-away shows 32.13 where the pre-fill says 32.12.
// Values the user typed or dragged are f32 already, so they round as before.
let w = match (wide_field_decimals(&cmd, &p.key), src) {
(Some(_), Some(raw)) if !edited => ((raw * f64::from(scale)).round() / f64::from(scale)) as f32,
_ => (v * scale).round() / scale,
// Such a field also keeps its rounded value as f64: stored as f32, 32.12 widens to
// 32.1199989 and OK sends that to the command (#13067). A value the user typed or
// dragged this frame is the widget's f32, so it rounds from that.
let w = match (wide_decimals, src) {
(true, Some(rounded)) if !edited => json!(rounded),
(true, _) => json!(round64(f64::from(v))),
(false, _) => json!((v * scale).round() / scale),
};
f.insert(p.key, json!(w));
f.insert(p.key, w);
}
Kind::Choice { options, default } => {
ui.horizontal(|ui| {