Count tool: Marker Size and Label Size fields take effect (#4156) (#4124)

Root cause: the options bar sent markerSize/labelSize as f32 JSON floats
(8.0) and count.setGroup read them with as_u64, which is None for floats,
so the command returned Ok and changed nothing.

Fix: count.setGroup accepts whole numbers sent as floats (UIs, agents and
actions send them), rejects fractions, negatives and non-numbers with
BadParams, and clamps in u64/f64 so huge values no longer wrap through
`as u32`. The options bar now sends integers.

Co-authored-by: Claude Opus 5.5 (1M context) <[email protected]>
This commit is contained in:
David MannandClaude Opus 5.5 authored and GitHub committed 2026-10-12 20:37:29 -04:00
1 parent 991ebe16d5
commit cb3aef5753
3 files changed
+79 -5

No files matched your search

+21 -4
View File
@@ -842,6 +842,23 @@ fn count_delete_group(s: &mut Session, p: &Value) -> Result<Value> {
Ok(count_json(doc(s)?.as_ref()))
}
/// A size parameter (`markerSize`, `labelSize`) clamped to `lo..=hi`: `None` when absent or null.
/// Whole numbers sent as JSON floats (`8.0`, as UIs and recorded actions do) count; fractions,
/// negatives and non-numbers are bad params rather than silently ignored. Clamping happens in
/// `u64`/`f64`, so a huge value can't wrap through `as u32`.
fn whole_size(cmd: &str, p: &Value, key: &str, lo: u32, hi: u32) -> Result<Option<u32>> {
let v = match p.get(key) {
None | Some(Value::Null) => return Ok(None),
Some(v) => v,
};
let n = match (v.as_u64(), v.as_f64()) {
(Some(n), _) => n.clamp(u64::from(lo), u64::from(hi)),
(None, Some(f)) if f.is_finite() && f >= 0.0 && f.fract() == 0.0 => f.clamp(f64::from(lo), f64::from(hi)) as u64,
_ => return Err(bad(cmd, format!("`{key}` = {v} must be a whole number ({lo}..={hi})"))),
};
Ok(Some(u32::try_from(n).unwrap_or(hi)))
}
fn count_set_group(s: &mut Session, p: &Value) -> Result<Value> {
const CMD: &str = "count.setGroup";
let d = doc(s)?;
@@ -853,11 +870,11 @@ fn count_set_group(s: &mut Session, p: &Value) -> Result<Value> {
if let Some(c) = color_of(p, "color") {
grp.color = c;
}
if let Some(v) = p.get("markerSize").and_then(Value::as_u64) {
grp.marker_size = (v as u32).clamp(1, 10);
if let Some(v) = whole_size(CMD, p, "markerSize", 1, 10)? {
grp.marker_size = v;
}
if let Some(v) = p.get("labelSize").and_then(Value::as_u64) {
grp.label_size = (v as u32).clamp(8, 72);
if let Some(v) = whole_size(CMD, p, "labelSize", 8, 72)? {
grp.label_size = v;
}
if let Some(v) = p.get("visible").and_then(Value::as_bool) {
grp.visible = v;
+37
View File
@@ -349,3 +349,40 @@ fn now_iso_shape() {
assert_eq!(t.len(), 20, "{t}");
assert!(t.ends_with('Z') && &t[4..5] == "-");
}
#[test]
fn count_set_group_takes_sizes_sent_as_whole_floats() {
// The options bar sends `{"markerSize": 8.0, "labelSize": 20.0}`; `as_u64` read neither, so
// the fields did nothing.
let mut s = session(100, 100, 8);
s.execute("count.add", json!({"x": 10, "y": 10})).unwrap();
s.execute("count.setGroup", json!({"markerSize": 8.0, "labelSize": 20.0})).unwrap();
let g = &d(&s).measurement.count_groups[0];
assert_eq!((g.marker_size, g.label_size), (8, 20));
// Out-of-range whole numbers clamp, also huge ones that `as u32` would have wrapped.
s.execute("count.setGroup", json!({"markerSize": 4_294_967_297u64, "labelSize": 1e300})).unwrap();
let g = &d(&s).measurement.count_groups[0];
assert_eq!((g.marker_size, g.label_size), (10, 72));
}
#[test]
fn count_set_group_rejects_sizes_that_are_not_whole_numbers() {
let mut s = session(100, 100, 8);
s.execute("count.add", json!({"x": 10, "y": 10})).unwrap();
s.execute("count.setGroup", json!({"markerSize": 3, "labelSize": 12})).unwrap();
let past = s.active().unwrap().history.past_len();
for p in [
json!({"markerSize": 8.5}),
json!({"labelSize": 12.25}),
json!({"markerSize": -2}),
json!({"labelSize": -9.0}),
json!({"markerSize": "8"}),
json!({"labelSize": true}),
] {
let err = s.execute("count.setGroup", p.clone()).unwrap_err();
assert!(matches!(err, EngineError::BadParams { .. }), "{p}: {err}");
}
let g = &d(&s).measurement.count_groups[0];
assert_eq!((g.marker_size, g.label_size), (3, 12), "a rejected call changes nothing");
assert_eq!(s.active().unwrap().history.past_len(), past);
}
+21 -1
View File
@@ -565,7 +565,7 @@ pub fn options_bar(app: &mut PhotocraftApp, ui: &mut egui::Ui, tool: Tool) -> bo
ui.label(RichText::new(tl!("Label Size")).color(t.text_dim).size(11.0));
let b = crate::widgets::value_field(ui, &mut ls, 8.0..=72.0, "", 44.0).changed();
if a || b {
let _ = app.run("count.setGroup", json!({"markerSize": ms.round(), "labelSize": ls.round()}));
let _ = app.run("count.setGroup", count_size_params(ms, ls));
}
}
true
@@ -613,6 +613,13 @@ pub fn options_bar(app: &mut PhotocraftApp, ui: &mut egui::Ui, tool: Tool) -> bo
// ------------------------------------------------------------------ panels and dialogs
/// `count.setGroup` params for the options bar's Marker Size and Label Size fields, sent as JSON
/// integers (the fields edit `f32`s; `8.0` is a float on the wire).
fn count_size_params(marker: f32, label: f32) -> Value {
let whole = |v: f32, lo: f32, hi: f32| if v.is_finite() { v.round().clamp(lo, hi) as u32 } else { lo as u32 };
json!({"markerSize": whole(marker, 1.0, 10.0), "labelSize": whole(label, 8.0, 72.0)})
}
fn float_frame(t: &Tokens) -> egui::Frame {
egui::Frame::NONE
.fill(t.card)
@@ -1223,4 +1230,17 @@ pub(crate) mod tests {
assert!(empty.run("view.colorSamplers.add", json!({"x": 1, "y": 1})).is_err());
assert!(empty.run("view.colorSamplers.list", json!({})).is_err());
}
#[test]
fn count_size_fields_send_whole_numbers_the_engine_applies() {
let p = count_size_params(7.6, 20.0);
assert_eq!(p, json!({"markerSize": 8, "labelSize": 20}));
assert!(p["markerSize"].is_u64() && p["labelSize"].is_u64());
assert_eq!(count_size_params(f32::NAN, 500.0), json!({"markerSize": 1, "labelSize": 72}));
let (mut app, _ctx) = app();
app.run("count.add", json!({"x": 10, "y": 10})).unwrap();
app.run("count.setGroup", count_size_params(8.0, 20.0)).unwrap();
let g = &app.session.active().unwrap().doc.measurement.count_groups[0];
assert_eq!((g.marker_size, g.label_size), (8, 20));
}
}