Levels fields size from the number field's own value width

This commit is contained in:
aiongg
2026-11-21 08:02:56 +00:00
parent 3a8c030cc4
commit 7fb78caf54
6 changed files with 53 additions and 6 deletions
@@ -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),
}
+32 -2
View File
@@ -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);
}
}
}
}
@@ -140,7 +140,7 @@ impl<C: Component> 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
@@ -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;
+5
View File
@@ -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)
+7
View File
@@ -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