diff --git a/crates/color-ui/src/ui/picker/compact.rs b/crates/color-ui/src/ui/picker/compact.rs index 716c9c855..fa1953b14 100644 --- a/crates/color-ui/src/ui/picker/compact.rs +++ b/crates/color-ui/src/ui/picker/compact.rs @@ -3,7 +3,7 @@ use craft_behavior::Mixed; use craft_theme::ColorModel; -use craft_ui::contract::{Bind, Entry, LabelScheme, Props as _, Show as _, ShowContainer as _}; +use craft_ui::contract::{Bind, Entry, LabelScheme, Props as _, Show as _, ShowContainer as _, Sizing}; use craft_ui::form::{Field, FieldGroup}; use craft_ui::layout_components::Row; use craft_ui::pickers::Select; @@ -77,7 +77,7 @@ impl<'a> Body<'a> { } } Row::new().show(ui, |ui, _| { - Field::new(words.hex.as_str().to_owned()).side(HexField::hash_label()).show(ui, |ui, _| self.hex_field(ui, pass, &words, got)); + Field::new(words.hex.as_str().to_owned()).side(HexField::hash_label()).show(ui, |ui, _| self.hex_field(ui, pass, &words, None, got)); self.sample_button(ui, &words, got); }); } @@ -119,11 +119,15 @@ impl<'a> Body<'a> { } } - /// The hex field. The Full layout's first show focuses it. - pub(super) fn hex_field(&self, ui: &mut Ui, pass: &Pass<'a>, words: &ColorPickerWords, got: &mut Collected) { - let edit = TextField::hex(words.hex.as_str().to_owned(), Bind::given(pass.shows(pass.shown.color.to_rgba()))) - .empty(pass.shown.color.to_rgba()) - .show(ui); + /// The hex field, at `sizing` when given. The Full layout's first show focuses it. + pub(super) fn hex_field(&self, ui: &mut Ui, pass: &Pass<'a>, words: &ColorPickerWords, sizing: Option, got: &mut Collected) { + let field = + TextField::hex(words.hex.as_str().to_owned(), Bind::given(pass.shows(pass.shown.color.to_rgba()))).empty(pass.shown.color.to_rgba()); + let edit = match sizing { + Some(sizing) => field.sizing(sizing), + None => field, + } + .show(ui); if pass.focus_hex { focus::request(ui.ctx(), FocusTarget::Id(edit.response.id)); } diff --git a/crates/color-ui/src/ui/picker/full.rs b/crates/color-ui/src/ui/picker/full.rs index 3ba4331ac..0769f78e2 100644 --- a/crates/color-ui/src/ui/picker/full.rs +++ b/crates/color-ui/src/ui/picker/full.rs @@ -4,15 +4,15 @@ use craft_behavior::{ColumnWidth, IconId, Metrics, Mixed, NavShape, Sense}; use craft_theme::{ColorModel, HsbModel, OklchModel, RgbModel, Srgba}; use craft_ui::choice::{Checkbox, Radio, RadioGroup, Swatch}; use craft_ui::contract::{ - Bind, Common, Component, Cx, FieldLayout, LabelKind, MeasureCx, Outcome, PaintUi, Props as _, Role, Show as _, ShowContainer as _, SideLabel, - Sizing, + Bind, Common, Component, Cx, FieldLayout, Gap, LabelKind, MeasureCx, Outcome, PaintUi, Props as _, Role, Show as _, ShowContainer as _, + SideLabel, Sizing, }; use craft_ui::display::{Icon, Shade, Text}; use craft_ui::layout_components::{Grid, Row, Stack}; use craft_ui::primitives::IconSpec; -use craft_ui::runtime::env::{Env, Label}; +use craft_ui::runtime::env::{Env, Label, UnitKind}; use craft_ui::style::{ComponentStyle, StyleCx, TypeRole}; -use craft_ui::text::{HexField, NumberField}; +use craft_ui::text::{HexField, NumberField, UnitPlacement}; use egui::{Rect, Response, Ui, Vec2}; use super::channel::{ChannelRule, ChannelUnit, channel_name}; @@ -162,11 +162,8 @@ impl Component for ColorWarningChip<'_> { } } -/// Columns that take the width of their content: the Full layout's two halves, and its channel grid of one to three -/// columns. A group of fields that fills the width would take all of a row's room otherwise. -const HALVES: &[ColumnWidth] = &[ColumnWidth::Content, ColumnWidth::Content]; -const ONE_COLUMN: &[ColumnWidth] = &[ColumnWidth::Content]; -const THREE_COLUMNS: &[ColumnWidth] = &[ColumnWidth::Content, ColumnWidth::Content, ColumnWidth::Content]; +/// The columns of one channel group: radio, letter, and the value cell (field and unit), each as wide as its content. +const THREE_COLUMNS: &[ColumnWidth] = &[ColumnWidth::Content, ColumnWidth::Content, ColumnWidth::Content, ColumnWidth::Content]; /// A grid column of the Full layout: HSB and RGB, then Lab and the host's models, then OKLCH. fn column_of(model: &dyn ColorModel) -> usize { @@ -183,7 +180,7 @@ impl<'a> Body<'a> { pub(super) fn full(&self, ui: &mut Ui, pass: &Pass<'a>, got: &mut Collected) { let words = Env::of(ui).words::(); let visible: Vec<&dyn ColorModel> = self.models.iter().copied().chain([pass.mode.model()]).collect(); - Grid::new(HALVES).show(ui, |ui, _| { + Row::new().wrap(true).gap(Gap::Section).show(ui, |ui, _| { Stack::new().show(ui, |ui, _| { self.area_and_strip(ui, pass, &visible, got); if self.has_web_check { @@ -219,19 +216,21 @@ impl<'a> Body<'a> { /// half sets it. fn compare_swatch(&self, ui: &mut Ui, pass: &Pass<'a>, current: Mixed, words: &ColorPickerWords, got: &mut Collected) { let caption = |text: &str| Text::new(text.to_owned()).role(TypeRole::Caption).shade(Shade::Muted); - Stack::new().show(ui, |ui, _| { + Stack::new().gap(Gap::Tight).show(ui, |ui, _| { caption(words.new_caption.as_str()).show(ui); - Swatch::new(words.new_color.as_str().to_owned(), pass.shows(pass.shown.color)).sizing(Sizing::FillWidth).show(ui); - let restore = words.restore.as_str().to_owned(); - let shown = match current { - Mixed::Same(current) => Mixed::Same(current.color), - Mixed::Mixed => Mixed::Mixed, - }; - if Swatch::new(restore.clone(), shown).tooltip(restore).sizing(Sizing::FillWidth).show(ui).clicked - && let Mixed::Same(current) = current - { - got.clicks.push(current); - } + Stack::new().gap(Gap::None).show(ui, |ui, _| { + Swatch::new(words.new_color.as_str().to_owned(), pass.shows(pass.shown.color)).sizing(Sizing::FillWidth).show(ui); + let restore = words.restore.as_str().to_owned(); + let shown = match current { + Mixed::Same(current) => Mixed::Same(current.color), + Mixed::Mixed => Mixed::Mixed, + }; + if Swatch::new(restore.clone(), shown).tooltip(restore).sizing(Sizing::FillWidth).show(ui).clicked + && let Mixed::Same(current) = current + { + got.clicks.push(current); + } + }); caption(words.current_caption.as_str()).show(ui); }); } @@ -249,15 +248,10 @@ impl<'a> Body<'a> { .filter(|(column, models)| *column == 0 || !models.is_empty()) .map(|(_, models)| models) .collect(); - let widths = match columns.len() { - 1 => ONE_COLUMN, - 2 => &THREE_COLUMNS[..2], - _ => THREE_COLUMNS, - }; - Grid::new(widths).show(ui, |ui, _| { + // The groups sit side by side from the top; each is its own grid of radio, letter and value cells, so the + // fields of a group share one x whether or not their row has a radio. + Row::new().gap(Gap::Section).show(ui, |ui, _| { for (column, models) in columns.iter().enumerate() { - // Each model column is its own grid of radio, letter and field cells, so the fields of a - // column share one x whether or not their row has a radio. Grid::new(THREE_COLUMNS).show(ui, |ui, _| { for model in models { self.model_fields(ui, pass, *model, ModelChoice { visible, offered: &offered, words }, got); @@ -265,7 +259,8 @@ impl<'a> Body<'a> { if column == 0 { Text::new(Label::from("")).show(ui); Text::new(Label::from(HexField::HASH)).shade(Shade::Muted).show(ui); - self.hex_field(ui, pass, words, got); + self.hex_field(ui, pass, words, Some(Sizing::Content), got); + Text::new(Label::from("")).show(ui); } }); } @@ -306,24 +301,32 @@ impl<'a> Body<'a> { } Text::new(Label::from(letter.to_owned())).shade(Shade::Muted).show(ui); let rule = ChannelRule::of(channel); - let unit = ChannelUnit::of(channel.unit); let value = f64::from(values.values[i]); - let field = NumberField::new(name, Bind::given(pass.shows(Some(value)))) + // The unit is the field's kind (typed units, decimals) and sits in its own cell after the field. + let (kind, text) = match ChannelUnit::of(channel.unit) { + ChannelUnit::None => (None, None), + ChannelUnit::Percent => (Some(UnitKind::Percent), Some(Env::of(ui).units().symbol(UnitKind::Percent))), + ChannelUnit::Degrees => (Some(UnitKind::Degrees), Some(Env::of(ui).units().symbol(UnitKind::Degrees))), + ChannelUnit::Text(text) => (None, Some(text.to_owned())), + }; + let field = NumberField::new(name.clone(), Bind::given(pass.shows(Some(value)))) .range(rule.range.clone()) .step(rule.step) .snap_typed(true) - .commit_decimals(usize::from(rule.decimals)); - let field = match unit { - ChannelUnit::None => field, - ChannelUnit::Percent => field.unit_kind(craft_ui::runtime::env::UnitKind::Percent), - ChannelUnit::Degrees => field.unit_kind(craft_ui::runtime::env::UnitKind::Degrees), - ChannelUnit::Text(text) => field.unit(Label::from(text)), + .commit_decimals(usize::from(rule.decimals)) + .fixed_decimals(usize::from(rule.decimals)) + .unit_placement(UnitPlacement::After) + .sizing(Sizing::Content); + let field = match kind { + Some(kind) => field.unit_kind(kind), + None => field, }; got.adopt(field.show(ui).edit, pass.shown, |v, mine| { if let Some(v) = v { *mine = mine.with_channel(model, i, *v, &pass.rules); } }); + Text::new(Label::from(text.unwrap_or_default())).shade(Shade::Muted).show(ui); } } } diff --git a/crates/color-ui/tests/it/picker.rs b/crates/color-ui/tests/it/picker.rs index 170d3cb74..019f1b7d8 100644 --- a/crates/color-ui/tests/it/picker.rs +++ b/crates/color-ui/tests/it/picker.rs @@ -118,8 +118,9 @@ fn a_dialog_reopened_keeps_only_web_colors_and_recalls_the_values_kept_on_ok() { assert_eq!(edited(&dialogs), Some(values), "Reset returns the typed values"); } -/// The Full picker's channel rows show a letter after each radio and no channel words; the radios keep their names, and -/// the number fields of a column, the hex field included, start at one x whether or not their row has a radio. +/// The Full picker lays its channels out as Photoshop's Color Picker does: a letter after each radio and no channel words, +/// the number fields of a group at one x (the hex field, which has no radio, included), a unit after its field and +/// outside it, and the second group (Lab, CMYK) starting on the first row of the first. #[trial] fn the_full_pickers_channel_rows_show_letters_and_align_their_fields() { let mut fixture = Fixture::new(|ui| { @@ -139,7 +140,7 @@ fn the_full_pickers_channel_rows_show_letters_and_align_their_fields() { assert_eq!(fixture.by_role(Role::RADIO_BUTTON).len(), 9, "every channel keeps its radio"); let fields = fixture.by_role(Role::SPIN_BUTTON); assert_eq!(fields.len(), 9); - // The columns are found by x, not by tree order: HSB and RGB fields share the first, Lab's the second. + // The groups are found by x, not by tree order: HSB and RGB fields share the first, Lab's the second. let mut starts: Vec = Vec::new(); for field in &fields { if !starts.iter().any(|start| (start - field.rect.min.x).abs() < 0.5) { @@ -148,7 +149,15 @@ fn the_full_pickers_channel_rows_show_letters_and_align_their_fields() { } starts.sort_by(f32::total_cmp); let rects: Vec<_> = fields.iter().map(|field| field.rect).collect(); - assert_eq!(starts.len(), 2, "the fields start in two columns, one per model column: {rects:?}"); + assert_eq!(starts.len(), 2, "the fields start in two groups: {rects:?}"); let hex = fixture.by_label("Hex color").expect("the hex field"); - assert!((hex.rect.min.x - starts[0]).abs() < 0.5, "the hex field, which has no radio, starts in the first column: {:?}", hex.rect); + assert!((hex.rect.min.x - starts[0]).abs() < 0.5, "the hex field, which has no radio, starts in the first group: {:?}", hex.rect); + let first = fields.iter().filter(|field| (field.rect.min.x - starts[0]).abs() < 0.5).map(|field| field.rect.min.y).fold(f32::MAX, f32::min); + let second = fields.iter().filter(|field| (field.rect.min.x - starts[1]).abs() < 0.5).map(|field| field.rect.min.y).fold(f32::MAX, f32::min); + assert!((first - second).abs() < 0.5, "the second group starts on the first group's first row: {first} against {second}"); + let widest = fields.iter().map(|field| field.rect.width()).fold(0.0_f32, f32::max); + let hue = fields.iter().min_by(|a, b| a.rect.min.y.total_cmp(&b.rect.min.y).then(a.rect.min.x.total_cmp(&b.rect.min.x))).expect("the hue field"); + let degrees = texts.iter().find(|run| run.text == "\u{b0}").expect("the degree unit is shown as text"); + assert!(degrees.rect.min.x >= hue.rect.max.x - 0.5, "the unit sits after the field, outside it: {:?} against {:?}", degrees.rect, hue.rect); + assert!(widest < starts[1] - starts[0], "the fields are short, not the width of the room"); } diff --git a/crates/ui-widgets/src/text/mod.rs b/crates/ui-widgets/src/text/mod.rs index d6f3968bf..245e2d1ca 100644 --- a/crates/ui-widgets/src/text/mod.rs +++ b/crates/ui-widgets/src/text/mod.rs @@ -17,7 +17,7 @@ pub use alt_text::{AltText, AltTextField, AltTextFieldStyle, AltTextWords}; pub use chips::{ChipCodec, ChipField, ChipFieldOutput, ChipFieldStyle, ChipWords, PartialChips}; pub use hex::{HexCodec, HexField}; pub use linked::{LinkMode, LinkedFields, LinkedFieldsStyle, LinkedLayout, LinkedOutput, LinkedWords, follow_equal, follow_ratio, link_toggle}; -pub use number::{NamedOr, NumberField, NumberName, NumberOutput, NumberPick, NumberStepper, NumberStyle, NumberValue, RangeAlert}; +pub use number::{NamedOr, NumberField, NumberName, NumberOutput, NumberPick, NumberStepper, NumberStyle, NumberValue, RangeAlert, UnitPlacement}; pub use selectable_text::{SelectableStyle, SelectableText}; pub use text_field::TextField; pub use words::TextWords; diff --git a/crates/ui-widgets/src/text/number.rs b/crates/ui-widgets/src/text/number.rs index 88eb5ea85..b7a0d27f7 100644 --- a/crates/ui-widgets/src/text/number.rs +++ b/crates/ui-widgets/src/text/number.rs @@ -43,6 +43,18 @@ const DEFAULT_STEP: f64 = 1.0; const VALUES: ColumnKey = ColumnKey::new("number_values"); const UNITS: ColumnKey = ColumnKey::new("number_units"); +/// Where a number field shows its unit. +#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)] +#[non_exhaustive] +pub enum UnitPlacement { + /// Inside the field, after the number. + #[default] + Inside, + /// After the field, outside its box: the field draws no unit and keeps the unit's spellings for typed text and its + /// decimals, and the host draws the unit text beside it (a column of units in a grid of fields). + After, +} + /// The placeholder of a mixed field. const BLANK: &str = "\u{2014}"; @@ -113,6 +125,9 @@ pub struct NumberField<'a, V: NumberValue + 'static = Option> { /// A unit shown after the number. #[prop(into)] unit: Option