diff --git a/crates/text/src/ui/character.rs b/crates/text/src/ui/character.rs index 2e9c28889..3caf14d2f 100644 --- a/crates/text/src/ui/character.rs +++ b/crates/text/src/ui/character.rs @@ -975,14 +975,22 @@ impl<'a> CharacterControls<'a> { let columns = if ui.available_width() < pair_width { &TWO_COLUMNS[..1] } else { &TWO_COLUMNS }; let shown = Stack::new().label(self.label).show(ui, |ui, _| { #[cfg(feature = "font-picker")] - let family = family_field_keyed(ui, &words, values, families, Some(keys::FAMILY), None, picker.as_mut()); + let family = family_field_keyed(ui, &words, values, families, Some(keys::FAMILY), None, Some(Sizing::FillWidth), picker.as_mut()); #[cfg(not(feature = "font-picker"))] - let family = family_field_keyed(ui, &words, values, families, Some(keys::FAMILY), None); + let family = family_field_keyed(ui, &words, values, families, Some(keys::FAMILY), None, Some(Sizing::FillWidth)); preview_family = family.preview; rested_family = family.rested; picker_id = family.picker; edits.extend(family.edits); - edits.extend(select_row(ui, &words.style, hidden_side(), mixed(values.style.clone()), &style_entries, CharacterChange::Style)); + edits.extend(select_row( + ui, + &words.style, + hidden_side(), + mixed(values.style.clone()), + &style_entries, + Some(Sizing::FillWidth), + CharacterChange::Style, + )); #[cfg(feature = "font-picker")] if let Some(axes) = axes { let held = values.axes.clone().unwrap_or_default(); @@ -1061,6 +1069,7 @@ impl<'a> CharacterControls<'a> { hidden_side(), mixed(values.font_height), &entries, + Some(Sizing::FillWidth), CharacterChange::FontHeight, )); } @@ -1070,7 +1079,7 @@ impl<'a> CharacterControls<'a> { let event = lengths.kerning( ui, words.kerning.clone(), - mark(GlyphLabel::icon(IconId::new("text_cursor")), Some(words.kerning_hint.clone()), FieldLayout::Beside), + mark(GlyphLabel::icon(IconId::new("text_cursor")), Some(words.kerning_hint.clone()), FieldLayout::Inside), kerning_shown, &kerning_presets, Some(keys::KERNING), @@ -1122,6 +1131,7 @@ impl<'a> CharacterControls<'a> { hidden_side(), mixed(values.anti_alias), &entries, + Some(Sizing::FillWidth), CharacterChange::AntiAlias, )); } @@ -1211,7 +1221,15 @@ impl<'a> CharacterControls<'a> { } if let Some(entries) = &language_entries { - edits.extend(select_row(ui, &words.language, hidden_side(), mixed(values.language.clone()), entries, CharacterChange::Language)); + edits.extend(select_row( + ui, + &words.language, + hidden_side(), + mixed(values.language.clone()), + entries, + Some(Sizing::FillWidth), + CharacterChange::Language, + )); } }); CharacterControlsOutput { response: shown.output, edits, preview_family, rested_family, requests, picker: picker_id } diff --git a/crates/text/src/ui/character/format_text.rs b/crates/text/src/ui/character/format_text.rs index 5a5de1ef6..caafa3c2f 100644 --- a/crates/text/src/ui/character/format_text.rs +++ b/crates/text/src/ui/character/format_text.rs @@ -338,9 +338,9 @@ impl<'a> FormatText<'a> { let panel = Stack::new().label(self.label).show(ui, |ui, _| { // The font, across the panel. #[cfg(feature = "font-picker")] - let family = family_field(ui, &cw, character, families, reason(FormatControl::Font), picker.as_mut()); + let family = family_field(ui, &cw, character, families, reason(FormatControl::Font), Some(Sizing::FillWidth), picker.as_mut()); #[cfg(not(feature = "font-picker"))] - let family = family_field(ui, &cw, character, families, reason(FormatControl::Font)); + let family = family_field(ui, &cw, character, families, reason(FormatControl::Font), Some(Sizing::FillWidth)); edits.extend(character_edits(family.edits)); if let Some(key) = family.host_picked { edits.push(picked_event(FormatTextChange::HostFont(key), cw.family.as_str())); diff --git a/crates/text/src/ui/character/rows.rs b/crates/text/src/ui/character/rows.rs index cd192092a..42a3d2cc6 100644 --- a/crates/text/src/ui/character/rows.rs +++ b/crates/text/src/ui/character/rows.rs @@ -106,7 +106,7 @@ impl Lengths<'_> { field.inner.unwrap_or_default() } - /// The kerning field: a number or the name of a presets row (Metrics, Optical). + /// The kerning field: a number or the name of a presets row (Metrics, Optical). The field fills its grid cell. pub(super) fn kerning( &self, ui: &mut Ui, @@ -118,7 +118,11 @@ impl Lengths<'_> { ) -> EditEvents> { let units = self.units; let shown = Field::new(label.clone()).side(side).show(ui, |ui, _| { - let field = NumberField::named(label.clone(), Bind::given(mixed(value))).range(TRACKING_RANGE).presets(presets).scrub_by_label(); + let field = NumberField::named(label.clone(), Bind::given(mixed(value))) + .range(TRACKING_RANGE) + .presets(presets) + .scrub_by_label() + .sizing(Sizing::FillWidth); let field = field.codec(NumberCodec::new(units.reader(None), 0)); match key { Some(key) => field.doc_key(key), @@ -131,7 +135,7 @@ impl Lengths<'_> { shown.inner.unwrap_or_default() } - /// A field over a number that is not a length: its event and the preset row picked. + /// A field over a number that is not a length: its event and the preset row picked. The field fills its grid cell. pub(crate) fn plain(&self, ui: &mut Ui, spec: PlainRow<'_>) -> (EditEvents>, Option) { self.plain_keyed(ui, spec, None) } @@ -141,7 +145,8 @@ impl Lengths<'_> { let PlainRow { label, side, value, range, percent, presets, disabled } = spec; let units = self.units; let shown = Field::new(label.clone()).side(side).show(ui, |ui, _| { - let field = NumberField::new(label.clone(), Bind::given(value)).range(range.clone()).presets(presets).scrub_by_label(); + let field = + NumberField::new(label.clone(), Bind::given(value)).range(range.clone()).presets(presets).scrub_by_label().sizing(Sizing::FillWidth); let field = if percent { field.unit_kind(UnitKind::Percent).step(UnitKind::Percent.step()) } else { @@ -275,21 +280,30 @@ pub(crate) fn string_entries(pairs: impl Iterator) -> V pairs.map(|(value, text)| Entry::new(value, text)).collect() } -/// A row with a select over `entries`; its pick as a change. +/// A row with a select over `entries`, sized by `sizing` (`None` keeps the select's own width); its pick as a change. pub(crate) fn select_row( ui: &mut Ui, label: &Label, side: SideLabel, value: Mixed, entries: &[Entry], + sizing: Option, change: impl Fn(V) -> C, ) -> EditEvents { let shown = Field::new(label.clone()) .side(side) - .show(ui, |ui, _| Select::new(label.clone(), Bind::given(value.clone()), entries).placeholder("—").show(ui).events); + .show(ui, |ui, _| sized(Select::new(label.clone(), Bind::given(value.clone()), entries).placeholder("—"), sizing).show(ui).events); shown.inner.unwrap_or_default().map(&change) } +/// `component` sized by `sizing`; `None` keeps its own sizing. +fn sized(component: P, sizing: Option) -> P { + match sizing { + Some(sizing) => component.sizing(sizing), + None => component, + } +} + /// One toggle button of a row: its name, icon, state (`None` while the selection mixes) and the change it makes. pub(crate) struct Toggle { pub(crate) name: Label, @@ -347,27 +361,29 @@ pub(crate) struct FamilyField { } /// The family field of the character controls, the style options and the type bar: the font picker in a -/// hidden-label field when a `picker` source is given, else a select over `families`. A `disabled` reason disables the -/// field and shows in its tooltip. +/// hidden-label field when a `picker` source is given, else a select over `families`, sized by `sizing` (`None` keeps +/// the control's own width). A `disabled` reason disables the field and shows in its tooltip. pub(crate) fn family_field( ui: &mut Ui, words: &CharacterWords, values: &CharacterValues, families: &[String], disabled: Option<&Label>, + sizing: Option, #[cfg(feature = "font-picker")] picker: Option<&mut FontPickerSource<'_>>, ) -> FamilyField { #[cfg(feature = "font-picker")] { - family_field_keyed(ui, words, values, families, None, disabled, picker) + family_field_keyed(ui, words, values, families, None, disabled, sizing, picker) } #[cfg(not(feature = "font-picker"))] { - family_field_keyed(ui, words, values, families, None, disabled) + family_field_keyed(ui, words, values, families, None, disabled, sizing) } } /// As [`family_field`], the picker carrying the document `key` a host focuses it by (the plain select has none). +#[allow(clippy::too_many_arguments)] pub(crate) fn family_field_keyed( ui: &mut Ui, words: &CharacterWords, @@ -376,6 +392,7 @@ pub(crate) fn family_field_keyed( #[cfg(feature = "font-picker")] key: Option, #[cfg(not(feature = "font-picker"))] _key: Option, disabled: Option<&Label>, + sizing: Option, #[cfg(feature = "font-picker")] picker: Option<&mut FontPickerSource<'_>>, ) -> FamilyField { let value = mixed(values.family.clone()); @@ -403,7 +420,7 @@ pub(crate) fn family_field_keyed( if let Some(key) = key { field = field.doc_key(key); } - disabled_for(field, &words.family, disabled).show(ui) + disabled_for(sized(field, sizing), &words.family, disabled).show(ui) }); let Some(out) = shown.inner else { return FamilyField::default() }; let id = craft_ui::contract::Outcome::response(&out).id; @@ -422,7 +439,7 @@ pub(crate) fn family_field_keyed( let entries = string_entries(families.iter().map(|name| (name.clone(), name.clone()))); let shown = Field::new(words.family.clone()).side(hidden_side()).show(ui, |ui, _| { let select = Select::new(words.family.clone(), Bind::given(value.clone()), &entries).placeholder("—"); - disabled_for(select, &words.family, disabled).show(ui).events + disabled_for(sized(select, sizing), &words.family, disabled).show(ui).events }); let edits = shown.inner.unwrap_or_default().map(CharacterChange::Family).into_iter().collect(); FamilyField { edits, ..FamilyField::default() } diff --git a/crates/text/src/ui/character/style_options.rs b/crates/text/src/ui/character/style_options.rs index f6b985614..b7ad6ed8e 100644 --- a/crates/text/src/ui/character/style_options.rs +++ b/crates/text/src/ui/character/style_options.rs @@ -308,9 +308,9 @@ impl<'a> TextStyleOptions<'a> { let found = match attribute { StyleAttribute::Font => include_row(ui, &words.include, attribute, on, |ui| { #[cfg(feature = "font-picker")] - let family = family_field(ui, &cw, c, families, None, picker.as_mut()); + let family = family_field(ui, &cw, c, families, None, Some(Sizing::FillWidth), picker.as_mut()); #[cfg(not(feature = "font-picker"))] - let family = family_field(ui, &cw, c, families, None); + let family = family_field(ui, &cw, c, families, None, Some(Sizing::FillWidth)); let mut found = character(family.edits); found.extend(character(select_row( ui, @@ -318,6 +318,7 @@ impl<'a> TextStyleOptions<'a> { hidden_side(), mixed(c.style.clone()), &style_entries, + Some(Sizing::FillWidth), CharacterChange::Style, ))); found @@ -390,7 +391,7 @@ impl<'a> TextStyleOptions<'a> { let align_of = |key: String| { ParagraphChange::Align(ALIGNS.iter().find(|(_, k)| *k == key).map_or_else(TextAlign::default, |(align, _)| *align)) }; - paragraph_changes(select_row(ui, &pw.alignment, hidden_side(), mixed(key), &align_entries, align_of)) + paragraph_changes(select_row(ui, &pw.alignment, hidden_side(), mixed(key), &align_entries, Some(Sizing::FillWidth), align_of)) }), StyleAttribute::FirstLineIndent => include_row(ui, &words.include, attribute, on, |ui| { let row = LengthRow { diff --git a/crates/text/src/ui/type_bar.rs b/crates/text/src/ui/type_bar.rs index b81e31b8a..bcf3d03aa 100644 --- a/crates/text/src/ui/type_bar.rs +++ b/crates/text/src/ui/type_bar.rs @@ -178,14 +178,14 @@ impl<'a> TypeBar<'a> { .extend(toggle.events.map(|on| TypeBarChange::Orientation(if on { TextOrientation::Vertical } else { TextOrientation::Horizontal }))); #[cfg(feature = "font-picker")] - let family = family_field(ui, &character_words, character, families, None, picker.as_mut()); + let family = family_field(ui, &character_words, character, families, None, None, picker.as_mut()); #[cfg(not(feature = "font-picker"))] - let family = family_field(ui, &character_words, character, families, None); + let family = family_field(ui, &character_words, character, families, None, None); preview_family = family.preview; rested_family = family.rested; edits.extend(family.edits.into_iter().map(|event| event.map(TypeBarChange::Character))); - let style = select_row(ui, &bar_words.style, hidden_side(), mixed(character.style.clone()), &style_entries, CharacterChange::Style); + let style = select_row(ui, &bar_words.style, hidden_side(), mixed(character.style.clone()), &style_entries, None, CharacterChange::Style); edits.extend(style.into_iter().map(|event| event.map(TypeBarChange::Character))); // The size glyph the Character panel's size row shows. diff --git a/crates/text/tests/it/character.rs b/crates/text/tests/it/character.rs index f7c97491f..8671198e2 100644 --- a/crates/text/tests/it/character.rs +++ b/crates/text/tests/it/character.rs @@ -1,6 +1,7 @@ use craft_text::ui::character::{CharacterControls, CharacterRows, CharacterValues, Kerning, Leading}; +use craft_theme::TextSize; use craft_ui::testing::{Fixture, trial}; -use egui::vec2; +use egui::{Rect, vec2}; #[trial] fn character_fields_are_readable_when_the_panel_is_narrowed() { @@ -44,3 +45,66 @@ fn the_properties_rows_leave_out_baseline_shift_and_the_style_toggles() { } } } + +/// Edges closer than this are the same edge. +const EDGE: f32 = 0.5; + +#[trial] +fn character_fields_line_up_in_their_columns_at_every_width_and_text_size() { + let families = vec!["Inter".to_owned()]; + let styles = vec!["Regular".to_owned()]; + let mut fixture = Fixture::new(move |ui| { + let values = CharacterValues { + family: Some("Inter".to_owned()), + style: Some("Regular".to_owned()), + size: Some(12.0), + leading: Some(Leading::Auto), + kerning: Some(Kerning::Metrics), + ..CharacterValues::default() + }; + CharacterControls::new("Character", &values, &families, &styles).show(ui); + }); + let left = ["Font size", "Kerning", "Vertical scale", "Baseline shift"]; + let right = ["Leading", "Tracking", "Horizontal scale"]; + let across = ["Font family", "Style"]; + + for text_size in [TextSize::Small, TextSize::Large] { + fixture.text_size(text_size); + for width in [222.0, 300.0, 340.0, 402.0, 520.0] { + fixture.room(vec2(width, 900.0)).settle(); + let at = format!("at {width} pt, {text_size:?} text"); + let rect = |label: &str| fixture.by_label(label).unwrap_or_else(|| panic!("{label} is shown {at}")).rect; + let same = |a: f32, b: f32| (a - b).abs() <= EDGE; + let size = rect("Font size"); + let leading = rect("Leading"); + let lined_up = |labels: &[&str], column: Rect| { + for label in labels { + let field = rect(label); + assert!( + same(field.left(), column.left()) && same(field.right(), column.right()), + "{label} {field:?} spans its column {column:?} {at}" + ); + } + }; + let full = Rect::from_x_y_ranges(size.left()..=leading.right(), size.y_range()); + if size.bottom() <= leading.top() { + // One column: every field spans it. + lined_up(&left, size); + lined_up(&right, size); + lined_up(&across, size); + assert!(same(rect("Text color").right(), size.right()), "the colour well ends at the column's end {at}"); + } else { + assert!(size.right() <= leading.left(), "the columns do not overlap {at}"); + lined_up(&left, size); + lined_up(&right, leading); + lined_up(&across, full); + assert!(same(rect("Text color").right(), leading.right()), "the colour well ends at the right column's end {at}"); + } + assert!(full.right() <= size.left() + width + EDGE, "no field runs past the panel {at}"); + for value in ["12", "Metrics"] { + let text = fixture.texts().iter().find(|run| run.text == value).unwrap_or_else(|| panic!("{value} is painted {at}")).clone(); + assert!(!text.elided && text.clip.expand(EDGE).contains_rect(text.rect), "{value} fits inside its field {at}: {text:?}"); + } + } + } +}