Merge #18002 into the merge queue batch

Use fixed coordinate precision in the Info panel
This commit is contained in:
David Mann committed 2026-11-30 00:25:20 +01:00
commit a2beb07f80
2 files changed
+76 -3

No files matched your search

+21 -3
View File
@@ -1,5 +1,6 @@
use craft::color_ui::ui::{InfoColor, InfoMeasure, InfoPanel, InfoReadings, SamplerReadout};
use craft::ui::runtime::env::Label;
use photocraft_engine::prefs::{Unit, UnitsAndRulers};
use super::{PhotocraftApp, Value, json};
use crate::canvas::document_display::CanvasHost;
@@ -48,10 +49,23 @@ fn pointer_cmyk_sample(app: &mut PhotocraftApp, (x, y): (i32, i32)) -> Option<[f
/// The Info panel's W and H for a size of `wh` px (the selection's or the pending crop frame's):
/// both go through the ruler unit's formatter at `dpi`, percent of the document's `extent`.
pub(crate) fn wh_readout(ur: &photocraft_engine::prefs::UnitsAndRulers, dpi: f64, extent: [f64; 2], wh: [f64; 2]) -> [String; 2] {
pub(crate) fn wh_readout(ur: &UnitsAndRulers, dpi: f64, extent: [f64; 2], wh: [f64; 2]) -> [String; 2] {
[ur.format(wh[0], dpi, extent[0]), ur.format(wh[1], dpi, extent[1])]
}
/// A position in the Info panel uses fixed decimals and marks percentages.
fn position_readout(units: &UnitsAndRulers, px: f64, dpi: f64, extent: f64) -> Label {
let unit = units.rulers;
let decimals = if unit == Unit::Millimeters { 1 } else { unit.decimals() };
let value = unit.from_px(px, dpi, extent, units.point_size.per_inch());
// Keep the existing half-away-from-zero rounding, including a half tenth of a percent.
let scale = 10f64.powi(decimals as i32);
let rounded = (value * scale).round() / scale;
let rounded = if rounded == 0.0 { 0.0 } else { rounded };
let suffix = if unit == Unit::Percent { "%" } else { "" };
Label::from(format!("{rounded:.decimals$}{suffix}"))
}
/// The Info panel's ruler cells (#11991), shown while the Ruler tool is active and a ruler is
/// drawn: the angle and length(s), and the point and size the X/Y and W/H cells show instead of
/// the pointer's and the selection's.
@@ -126,7 +140,7 @@ pub(crate) fn readings<'a>(app: &mut PhotocraftApp, samplers: &'a [(SamplerReado
InfoColor::from_parts([byte(c[0]), byte(c[1]), byte(c[2])], cmyk.map(|v| v.clamp(0, 100) as u8))
});
// Measured from the rulers' zero point (#2955).
let at = |axis: usize, px: f64| Label::from(units.format(px - origin[axis], dpi, extent[axis]));
let at = |axis: usize, px: f64| position_readout(&units, px - origin[axis], dpi, extent[axis]);
let position = match (&ruler, guide) {
(Some(r), _) => Some([at(0, r.point[0]), at(1, r.point[1])]),
(None, Some(g)) if g.along == craft::behavior::Axis::Y => Some([at(0, g.position), Label::Empty]),
@@ -171,7 +185,7 @@ pub(crate) fn sampler_rows(app: &mut PhotocraftApp) -> Vec<(SamplerReadout, [Lab
let number = usize::try_from(s["number"].as_i64().unwrap_or(0)).unwrap_or(0);
// The point sits on its pixel's centre (#11725); the row names the pixel, as the pointer row does.
let (x, y) = (at.0.floor(), at.1.floor());
let labels = [Label::from(units.format(x, dpi, f64::from(size.width))), Label::from(units.format(y, dpi, f64::from(size.height)))];
let labels = [position_readout(units, x, dpi, f64::from(size.width)), position_readout(units, y, dpi, f64::from(size.height))];
Some((SamplerReadout::from_values(number, [x as i64, y as i64], rgb, cmyk), labels))
})
.collect()
@@ -291,3 +305,7 @@ mod tests {
#[cfg(test)]
#[path = "info_ruler_tests.rs"]
mod info_ruler_tests;
#[cfg(test)]
#[path = "info_unit_format_tests.rs"]
mod info_unit_format_tests;
@@ -0,0 +1,55 @@
use craft::color_ui::ui::InfoMeasure;
use serde_json::json;
use super::{readings, sampler_rows};
use crate::PhotocraftApp;
use crate::state::Tool;
fn app() -> Box<PhotocraftApp> {
let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default());
app.run("file.new", json!({"width": 400, "height": 300, "resolution": 72})).unwrap();
app
}
fn unit(app: &mut PhotocraftApp, value: &str) {
app.run("prefs.set", json!({"path": "unitsAndRulers.rulers", "value": value})).unwrap();
}
#[test]
fn pointer_coordinates_use_the_info_panels_precision_in_each_ruler_unit() {
let mut app = app();
app.hover_doc = Some([49.5, 51.5]);
let cases = [
("pixels", ["49", "51"]),
("inches", ["0.681", "0.708"]),
("cm", ["1.73", "1.80"]),
("mm", ["17.3", "18.0"]),
("points", ["49.0", "51.0"]),
("picas", ["4.08", "4.25"]),
("percent", ["12.3%", "17.0%"]),
];
let mut actual = Vec::new();
for (name, _) in cases {
unit(&mut app, name);
actual.push(readings(&mut app, &[]).position.unwrap().map(|label| label.as_str().to_owned()));
}
assert_eq!(actual, cases.map(|(_, coordinates)| coordinates));
}
#[test]
fn a_sampler_and_the_ruler_show_coordinates_in_the_same_units_as_the_pointer() {
let mut app = app();
unit(&mut app, "percent");
app.hover_doc = Some([49.5, 51.5]);
let pointer = readings(&mut app, &[]).position.unwrap();
app.run("view.colorSamplers.add", json!({"x": 49, "y": 51})).unwrap();
assert_eq!(sampler_rows(&mut app)[0].1, pointer);
app.ui.tool = Tool::Ruler;
app.run("image.analysis.rulerTool", json!({"start": [49, 51], "end": [69, 51]})).unwrap();
let ruler = readings(&mut app, &[]);
assert_eq!(ruler.position, Some(pointer));
assert_eq!(ruler.extent.unwrap().map(|label| label.as_str().to_owned()), ["5", "0"]);
let Some(InfoMeasure::Line { length, .. }) = ruler.measure else { panic!("the ruler length is displayed") };
assert_eq!(length.as_str(), "5.00");
}