From 7fb78caf547c8416a0d57b3f5428201b73afd2ec Mon Sep 17 00:00:00 2001 From: aiongg Date: Sat, 21 Nov 2026 07:51:39 +0000 Subject: [PATCH] Levels fields size from the number field's own value width --- crates/imaging/src/ui/adjust/levels_editor.rs | 6 ++-- crates/imaging/tests/it/adjust_tone.rs | 34 +++++++++++++++++-- crates/ui-contract/src/contract/cx/measure.rs | 2 +- crates/ui-contract/src/runtime/egui/style.rs | 5 +++ crates/ui-contract/src/style/cx.rs | 5 +++ crates/ui-widgets/src/text/mod.rs | 7 ++++ 6 files changed, 53 insertions(+), 6 deletions(-) diff --git a/crates/imaging/src/ui/adjust/levels_editor.rs b/crates/imaging/src/ui/adjust/levels_editor.rs index 5f1bc2ad4..733cb7843 100644 --- a/crates/imaging/src/ui/adjust/levels_editor.rs +++ b/crates/imaging/src/ui/adjust/levels_editor.rs @@ -13,7 +13,7 @@ use craft_ui::contract::{ use craft_ui::display::{Shade, Text}; use craft_ui::runtime::env::Label; use craft_ui::style::{ComponentStyle, DataColor, StyleCx, TypeRole}; -use craft_ui::text::NumberField; +use craft_ui::text::{FieldStyle, NumberField}; use craft_ui::value_drag::{Follow, Order, RowOp, RowRules, Thumb, ThumbOp, ThumbRow, TrackFace}; use egui::{Color32, Rect, Response, Vec2, pos2}; @@ -431,8 +431,8 @@ impl Component for LevelsEditor<'_> { row: control.icon, bar_row: control.icon + cx.tokens.size.ramp.bar, fields: row, - field: cx.text_width(TypeRole::Body, "000") + 2.0 * control.pad_x, - gamma_field: cx.text_width(TypeRole::Body, "0.00") + 2.0 * control.pad_x, + field: FieldStyle::number_width(cx, "000"), + gamma_field: FieldStyle::number_width(cx, "0.00"), width: cx.tokens.size.plot.width, bar: key.channel.bar(cx), } diff --git a/crates/imaging/tests/it/adjust_tone.rs b/crates/imaging/tests/it/adjust_tone.rs index c5eecf4b3..263f05df7 100644 --- a/crates/imaging/tests/it/adjust_tone.rs +++ b/crates/imaging/tests/it/adjust_tone.rs @@ -1,10 +1,15 @@ //! The Levels editor and panel, the Curves panel's point helper and the tone pickers, driven through the public //! widgets as a host would show them. -use craft_imaging::ui::adjust::{CurvesPanel, Levels, ToneChannels, ToneCurves, ToneSpace}; +use std::cell::RefCell; +use std::rc::Rc; + +use craft_imaging::ui::adjust::{CurvesPanel, Levels, LevelsEditor, ToneChannels, ToneCurves, ToneSpace}; use craft_imaging::ui::curve::Curve; use craft_theme::Srgba; -use craft_ui::testing::trial; +use craft_ui::contract::{Bind, Show as _}; +use craft_ui::testing::{Fixture, trial}; +use egui::Vec2; fn grey(v: f32) -> Srgba { Srgba { r: v, g: v, b: v, a: 1.0 } @@ -73,3 +78,28 @@ fn a_curves_point_from_a_sample_goes_on_the_shown_channel_or_on_each_colour() { assert_eq!(xs(&curves, 2), [0.0, 153.0, 255.0]); assert_eq!((xs(&curves, 1).len(), xs(&curves, 3).len()), (2, 2), "the other colours stay as they were"); } + +/// Every value field of the editor holds its value whole: the gamma at both ends of its range and the levels at both +/// ends of theirs lie inside the field, not cut by its edge. +#[trial] +fn the_levels_fields_hold_their_values_whole() { + for (levels, shown) in + [(Levels::new(0.0, 1.0, 255.0, 0.0, 255.0), ["0", "1.00", "255"]), (Levels::new(255.0, 9.99, 255.0, 255.0, 255.0), ["255", "9.99", "255"])] + { + let state = Rc::new(RefCell::new(levels)); + let held = Rc::clone(&state); + let mut fixture = Fixture::new(move |ui| { + let _ = LevelsEditor::new("Levels", Bind::bound(&mut *held.borrow_mut())).show(ui); + }); + fixture.room(Vec2::new(800.0, 600.0)); + fixture.settle(); + for value in shown { + let painted: Vec<_> = fixture.texts().iter().filter(|text| text.text == value).collect(); + assert!(!painted.is_empty(), "{value} is painted"); + for text in painted { + assert!(!text.elided, "{value} is not elided"); + assert!(text.clip.contains_rect(text.rect), "{value} {:?} lies inside its field's clip {:?}", text.rect, text.clip); + } + } + } +} diff --git a/crates/ui-contract/src/contract/cx/measure.rs b/crates/ui-contract/src/contract/cx/measure.rs index f5a3d939e..9a3d63639 100644 --- a/crates/ui-contract/src/contract/cx/measure.rs +++ b/crates/ui-contract/src/contract/cx/measure.rs @@ -140,7 +140,7 @@ impl MeasureCx<'_, C> { /// editor draws past its last letter, so the caret stays inside the control and the text never scrolls. pub fn field_text(&self, run: &TextRun<'_>) -> Vec2 { let text = self.layout(run).size(); - Vec2::new(text.x + self.ui.visuals().text_cursor.stroke.width, text.y) + Vec2::new(text.x + crate::runtime::egui::style::caret_width(self.ui.ctx()), text.y) } /// Lays document `text` out `text.wrap` wide, as `ContentPainterCx::layout` will paint it: the galley a content diff --git a/crates/ui-contract/src/runtime/egui/style.rs b/crates/ui-contract/src/runtime/egui/style.rs index 4285d9b79..ff7b38fd3 100644 --- a/crates/ui-contract/src/runtime/egui/style.rs +++ b/crates/ui-contract/src/runtime/egui/style.rs @@ -10,6 +10,11 @@ pub(crate) fn set_text_cursor(ui: &mut Ui, visible: bool) { } } +/// The width of the caret an editable text draws past its last letter: the one value measuring and styling both read. +pub(crate) fn caret_width(ctx: &Context) -> f32 { + ctx.global_style().visuals.text_cursor.stroke.width +} + /// Paints the selected text of `ui`'s text edits in `ink` on `fill`. pub(crate) fn set_text_selection(ui: &mut Ui, fill: egui::Color32, ink: egui::Color32) { let selection = &mut ui.visuals_mut().selection; diff --git a/crates/ui-contract/src/style/cx.rs b/crates/ui-contract/src/style/cx.rs index d82c9f33b..58c08120a 100644 --- a/crates/ui-contract/src/style/cx.rs +++ b/crates/ui-contract/src/style/cx.rs @@ -396,6 +396,11 @@ impl<'a> StyleCx<'a> { super::text_measure::line_width(self.ctx, self.tokens, stack_facts(self.ctx), self.direction, role, text) } + /// The width of the caret an editable text draws past its last letter. + pub fn caret_width(&self) -> f32 { + crate::runtime::egui::style::caret_width(self.ctx) + } + /// What a surface of `kind` draws. pub fn surface(&self, kind: SurfaceKind) -> SurfaceStyle { surface_style(self.tokens, kind) diff --git a/crates/ui-widgets/src/text/mod.rs b/crates/ui-widgets/src/text/mod.rs index 4b125e7a7..6b7e2108e 100644 --- a/crates/ui-widgets/src/text/mod.rs +++ b/crates/ui-widgets/src/text/mod.rs @@ -100,6 +100,13 @@ impl FieldStyle { f32::from(chars) * digit + 2.0 * cx.control().pad_x } + /// The width of a [`NumberField`] that shows `sample`: the text in the face a number field draws, the caret + /// past its last letter, and the field's padding on both sides. A parent that sizes a field by its sample text + /// (a column of fixed-width number fields) uses it, so the field holds the value whole. + pub fn number_width(cx: &StyleCx<'_>, sample: &str) -> f32 { + cx.text_width(crate::style::TypeRole::Mono, sample) + cx.caret_width() + 2.0 * cx.control().pad_x + } + /// The field's padding, gap and minimum size. pub fn metrics(&self) -> Metrics { self.metrics