From 1c3c25035ce6398b31046375485ed0100e8ce247 Mon Sep 17 00:00:00 2001 From: MarekSpodBiedry <73122968+MarekSpodBiedry@users.noreply.github.com> Date: Sat, 24 Oct 2026 07:51:26 +0200 Subject: [PATCH] List binary fractions in the Units precision drop-down for Architectural and Fractional (#1861) (#1952) --- crates/ui-egui/src/dialogs.rs | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/crates/ui-egui/src/dialogs.rs b/crates/ui-egui/src/dialogs.rs index 58e9cf02..2a46d7ed 100644 --- a/crates/ui-egui/src/dialogs.rs +++ b/crates/ui-egui/src/dialogs.rs @@ -235,6 +235,17 @@ fn pick(ui: &mut egui::Ui, salt: &str, value: &mut i64, options: &[(i64, String) }); } +/// Length precision choices for a LUNITS format. Architectural (4) and Fractional (5) step through +/// binary fractions of an inch (LUPREC n is 1/2^n, 0 is whole inches); the other formats show a decimal sample. +fn length_precisions(lunits: i64) -> Vec<(i64, String)> { + use cadcraft_engine::units::format_distance; + if matches!(lunits, 4 | 5) { + (0..=8).map(|p| (p, if p == 0 { "1\"".to_string() } else { format!("1/{}\"", 1i64 << p) })).collect() + } else { + (0..=8).map(|p| (p, format_distance(1.5, lunits, p))).collect() + } +} + /// Drawing Units (UNITS): edits a copy of LUNITS/LUPREC/AUNITS/AUPREC/INSUNITS and applies it /// through the `units` command on OK. fn units(app: &mut CadApp, ctx: &egui::Context, open: &mut bool) { @@ -260,7 +271,7 @@ fn units(app: &mut CadApp, ctx: &egui::Context, open: &mut bool) { pick(ui, "units_lunits", lu, &types(&LENGTH_TYPES)); ui.end_row(); ui.label("Precision"); - pick(ui, "units_luprec", lp, &(0..=8).map(|p| (p, format_distance(1.5, *lu, p))).collect::>()); + pick(ui, "units_luprec", lp, &length_precisions(*lu)); ui.end_row(); ui.strong("Angle"); ui.end_row(); @@ -433,6 +444,15 @@ mod tests { use crate::{CadApp, Services}; + #[test] + fn fractional_length_precisions_list_binary_fractions() { + let labels = ["1\"", "1/2\"", "1/4\"", "1/8\"", "1/16\"", "1/32\"", "1/64\"", "1/128\"", "1/256\""]; + let want: Vec<(i64, String)> = labels.iter().enumerate().map(|(k, l)| (k as i64, l.to_string())).collect(); + assert_eq!(super::length_precisions(4), want); + assert_eq!(super::length_precisions(5), want); + assert_eq!(super::length_precisions(2).len(), 9); + } + #[test] fn picking_a_block_starts_insert_with_its_name() { let mut app = CadApp::new(Session::new(), Services::default());