diff --git a/crates/text/src/ui/character.rs b/crates/text/src/ui/character.rs index d9d676c04..25b1466af 100644 --- a/crates/text/src/ui/character.rs +++ b/crates/text/src/ui/character.rs @@ -8,12 +8,14 @@ #[cfg(feature = "font-picker")] use craft_font_manager::Catalog; #[cfg(feature = "font-picker")] -use craft_font_manager::picker::{FontFilters, FontPicker, FontPickerSettings}; +use craft_font_manager::axes::AxisSliders; +#[cfg(feature = "font-picker")] +use craft_font_manager::picker::{FontFilters, FontPickerSettings}; use std::ops::RangeInclusive; #[cfg(feature = "font-picker")] use std::sync::Arc; -use crate::ui::length::{DocUnit, shown}; +use crate::ui::length::DocUnit; use craft_behavior::{ColumnWidth, EditEvent, IconId}; use craft_theme::{Rgba, Srgba}; use craft_ui::choice::Swatch; @@ -40,8 +42,8 @@ pub use paragraph_dialog::{ SpacingKind, SpecialIndent, }; use rows::{ - LengthRow, Lengths, PlainRow, Toggle, hidden_side, mark, mixed, number_change, number_presets, picked_event, plain, select_row, string_entries, - toggle_row, + LengthRow, Lengths, PlainRow, Toggle, family_field, hidden_side, leading_menu, leading_row, mark, mixed, number_change, number_presets, + picked_event, plain, select_row, size_menu, string_entries, toggle_row, }; pub use style_options::{ STYLE_COLOR_KEY, StyleAttribute, StyleOptionValues, StyleOptionsChange, TextStyleOptions, TextStyleOptionsOutput, TextStyleOptionsWords, @@ -465,9 +467,8 @@ pub struct CharacterControls<'a> { #[prop] languages: Option<&'a [(String, String)]>, #[cfg(feature = "font-picker")] - picker: Option<(&'a Arc, Bind<'a, FontPickerSettings>, Bind<'a, FontFilters>)>, + picker: Option>, #[cfg(feature = "font-picker")] - #[allow(dead_code)] // P3(panels-w13): tl.R6 axes: Option>, } @@ -489,12 +490,11 @@ impl<'a> CharacterControls<'a> { } } - /// The family field is craft-font-manager's font picker over `catalog`, with the app's stars, recents and - /// filters. + /// The family field is craft-font-manager's font picker over `source`: the catalog, the app's stars, recents and + /// filters, the preview text and the served families. #[cfg(feature = "font-picker")] pub fn font_picker(mut self, source: FontPickerSource<'a>) -> Self { - // P3(panels-w13): tl.R6 (preview_text and served are unread) - self.picker = Some((source.catalog, source.settings, source.filters)); + self.picker = Some(source); self } @@ -512,20 +512,15 @@ impl<'a> CharacterControls<'a> { let (unit, superscript) = (self.unit, self.superscript); #[cfg(feature = "font-picker")] let mut picker = self.picker; + #[cfg(feature = "font-picker")] + let axes = self.axes; let env = Env::of(ui); let words = env.words::(); let units = env.units(); let lengths = Lengths { units: &units, unit }; - let size_presets: Vec> = SIZE_PRESETS - .iter() - .map(|&p| { - let points = f64::from(p); - Entry::new(NumberPick::Number(points), units.text(unit.kind, shown(&units, unit, points), 0..=PRESET_DECIMALS)) - }) - .collect(); - let auto_row = Entry::new(NumberPick::Choice(AUTO_ROW), words.auto.clone()).checked(values.leading == Some(Leading::Auto)); - let leading_presets: Vec> = [auto_row, Entry::separator()].into_iter().chain(size_presets.iter().cloned()).collect(); + let size_presets = size_menu(&units, unit); + let leading_presets = leading_menu(&words, values.leading, &size_presets); let tracking_presets = number_presets(&units, TRACKING_PRESETS.iter().copied()); let kerning_presets: Vec> = [ Entry::new(NumberPick::Choice(METRICS_ROW), words.metrics.clone()).checked(values.kerning == Some(Kerning::Metrics)), @@ -539,11 +534,7 @@ impl<'a> CharacterControls<'a> { let family_entries = string_entries(families.iter().map(|name| (name.clone(), name.clone()))); let style_entries = string_entries(styles.iter().map(|name| (name.clone(), name.clone()))); let language_entries = languages.map(|languages| string_entries(languages.iter().cloned())); - let leading_value = match values.leading { - Some(Leading::Fixed(points)) => Some(points), - Some(Leading::Auto) => values.size.map(|size| size * AUTO_LEADING), - None => None, - }; + let kerning_shown = values.kerning.map(|kerning| match kerning { Kerning::Manual(value) => NamedOr::Number(f64::from(value)), Kerning::Metrics => NamedOr::Named(KerningName::Metrics), @@ -551,39 +542,31 @@ impl<'a> CharacterControls<'a> { }); let mut edits: Vec> = Vec::new(); - #[cfg(feature = "font-picker")] let mut preview_family = None; - #[cfg(not(feature = "font-picker"))] - let preview_family = None; + let mut rested_family = None; let mut requests = HostRequests::new(); let shown = Stack::new().label(self.label).show(ui, |ui, _| { #[cfg(feature = "font-picker")] - let by_picker = if let Some((catalog, settings, filters)) = picker.as_mut() { - let family = mixed(values.family.clone()); - let side = SideLabel::new().kind(LabelKind::Hidden); - let shown = Field::new(words.family.clone()).side(side).show(ui, |ui, _| { - let out = FontPicker::new(words.family.clone(), Bind::given(family.clone()), catalog) - .settings(settings.reborrowed()) - .filters(filters.reborrowed()) - .show(ui); - (out.preview, out.edit.events) - }); - if let Some((preview, events)) = shown.inner { - preview_family = preview; - let kept = events.into_iter().filter(|event| matches!(event, EditEvent::Commit { .. } | EditEvent::Set { .. })); - edits.extend(kept.map(|event| event.map(CharacterChange::Family))); - } - true - } else { - false - }; + let family = family_field(ui, &words.family, mixed(values.family.clone()), &family_entries, picker.as_mut()); #[cfg(not(feature = "font-picker"))] - let by_picker = false; - if !by_picker { - edits.extend(select_row(ui, &words.family, hidden_side(), mixed(values.family.clone()), &family_entries, CharacterChange::Family)); + let family = family_field(ui, &words.family, mixed(values.family.clone()), &family_entries); + preview_family = family.preview; + rested_family = family.rested; + edits.extend(family.edits.into_iter().map(|event| event.map(CharacterChange::Family))); + if let Some(style) = family.picked_style { + edits.push(picked_event(CharacterChange::Style(style), words.style.as_str())); } edits.extend(select_row(ui, &words.style, hidden_side(), mixed(values.style.clone()), &style_entries, CharacterChange::Style)); + #[cfg(feature = "font-picker")] + if let Some(axes) = axes { + let held = values.axes.clone().unwrap_or_default(); + let mut sliders = AxisSliders::new(words.axes.clone(), axes.axes, Bind::given(held)).instances(axes.instances).italic(axes.italic); + if let Some(weight) = axes.weight { + sliders = sliders.weight(weight); + } + edits.extend(sliders.show(ui).edit.into_iter().map(|event| event.map(CharacterChange::Axes))); + } Grid::new(&TWO_COLUMNS).fill_width().show(ui, |ui, _| { let size = LengthRow { @@ -595,18 +578,8 @@ impl<'a> CharacterControls<'a> { }; let (event, _) = lengths.row(ui, size); edits.extend(number_change(event, CharacterChange::Size)); - let leading = LengthRow { - label: words.leading.clone(), - side: mark(GlyphLabel::text("A↕"), Some(words.leading_hint.clone()), FieldLayout::Inside), - value: leading_value, - range: LEADING_RANGE, - presets: &leading_presets, - }; - let (event, picked) = lengths.row(ui, leading); - edits.extend(number_change(event, |points| CharacterChange::Leading(Leading::Fixed(points)))); - if picked == Some(NumberPick::Choice(AUTO_ROW)) { - edits.push(picked_event(CharacterChange::Leading(Leading::Auto), words.leading.as_str())); - } + let side = mark(GlyphLabel::text("A↕"), Some(words.leading_hint.clone()), FieldLayout::Inside); + edits.extend(leading_row(&lengths, ui, &words, side, values.leading, values.size, &leading_presets)); }); Grid::new(&TWO_COLUMNS).fill_width().show(ui, |ui, _| { let event = lengths.kerning( @@ -731,7 +704,7 @@ impl<'a> CharacterControls<'a> { edits.extend(select_row(ui, &words.language, hidden_side(), mixed(values.language.clone()), entries, CharacterChange::Language)); } }); - CharacterControlsOutput { response: shown.output, edits, preview_family, rested_family: None, requests } + CharacterControlsOutput { response: shown.output, edits, preview_family, rested_family, requests } } } diff --git a/crates/text/src/ui/character/paragraph.rs b/crates/text/src/ui/character/paragraph.rs index 08d05a96d..1b50a9474 100644 --- a/crates/text/src/ui/character/paragraph.rs +++ b/crates/text/src/ui/character/paragraph.rs @@ -44,9 +44,9 @@ craft_ui::words! { } /// The range of the indent fields, points. -const INDENT_RANGE: RangeInclusive = -1296.0..=1296.0; +pub(super) const INDENT_RANGE: RangeInclusive = -1296.0..=1296.0; /// The range of the space fields, points. -const SPACE_RANGE: RangeInclusive = 0.0..=1296.0; +pub(super) const SPACE_RANGE: RangeInclusive = 0.0..=1296.0; /// Paragraph alignment, in Photoshop's button order. #[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)] @@ -196,38 +196,16 @@ impl<'a> ParagraphControls<'a> { pub fn show(self, ui: &mut Ui) -> ParagraphControlsOutput { let values = self.values; let point_text = self.point_text; - let _ = self.vertical; // P3(panels-w13): tl.R6 + let vertical = self.vertical; let show_hyphenate = self.hyphenate; let env = Env::of(ui); let words = env.words::(); let units = env.units(); let lengths = Lengths { units: &units, unit: self.unit }; - let aligns: Vec> = [ - (TextAlign::Left, "align_left", &words.left), - (TextAlign::Center, "align_center", &words.center), - (TextAlign::Right, "align_right", &words.right), - (TextAlign::JustifyLeft, "align_justify", &words.justify_left), - (TextAlign::JustifyCenter, "align_justify", &words.justify_center), - (TextAlign::JustifyRight, "align_justify", &words.justify_right), - (TextAlign::JustifyAll, "align_justify", &words.justify_all), - ] - .into_iter() - .map(|(align, icon, name)| { - let toggle = Toggle::new(name, icon, Some(values.align == Some(align)), Box::new(move |_| ParagraphChange::Align(align))); - if point_text && align.needs_box() { toggle.disabled(&words.justify_needs_box) } else { toggle } - }) - .collect(); - let directions: Vec> = [ - (ParagraphDirection::Automatic, "pilcrow", &words.direction_auto), - (ParagraphDirection::LeftToRight, "pilcrow_right", &words.direction_ltr), - (ParagraphDirection::RightToLeft, "pilcrow_left", &words.direction_rtl), - ] - .into_iter() - .map(|(direction, icon, name)| { - Toggle::new(name, icon, Some(values.direction == Some(direction)), Box::new(move |_| ParagraphChange::Direction(direction))) - }) - .collect(); + let aligns: Vec> = + align_toggles(values, &words, vertical).into_iter().chain(justify_toggles(values, &words, point_text)).collect(); + let directions = direction_toggles(values, &words); let mut edits: Vec> = Vec::new(); let shown = Stack::new().label(self.label).show(ui, |ui, _| { @@ -291,3 +269,55 @@ impl<'a> ParagraphControls<'a> { ParagraphControlsOutput { response: shown.output, edits } } } + +/// The Left, Center and Right toggles; with `vertical` they are the Top, Middle and Bottom ones, which set the same +/// [`TextAlign`] values. +pub(crate) fn align_toggles(values: &ParagraphValues, words: &ParagraphWords, vertical: bool) -> Vec> { + let marks: [(TextAlign, &'static str, &Label); 3] = if vertical { + [ + (TextAlign::Left, "text_align_top", &words.top), + (TextAlign::Center, "text_align_middle", &words.middle), + (TextAlign::Right, "text_align_bottom", &words.bottom), + ] + } else { + [ + (TextAlign::Left, "align_left", &words.left), + (TextAlign::Center, "align_center", &words.center), + (TextAlign::Right, "align_right", &words.right), + ] + }; + marks + .into_iter() + .map(|(align, icon, name)| Toggle::new(name, icon, Some(values.align == Some(align)), Box::new(move |_| ParagraphChange::Align(align)))) + .collect() +} + +/// The four justify toggles; in point text, which has no box to fill, they are disabled. +pub(crate) fn justify_toggles(values: &ParagraphValues, words: &ParagraphWords, point_text: bool) -> Vec> { + [ + (TextAlign::JustifyLeft, &words.justify_left), + (TextAlign::JustifyCenter, &words.justify_center), + (TextAlign::JustifyRight, &words.justify_right), + (TextAlign::JustifyAll, &words.justify_all), + ] + .into_iter() + .map(|(align, name)| { + let toggle = Toggle::new(name, "align_justify", Some(values.align == Some(align)), Box::new(move |_| ParagraphChange::Align(align))); + if point_text && align.needs_box() { toggle.disabled(&words.justify_needs_box) } else { toggle } + }) + .collect() +} + +/// The three direction toggles. +pub(crate) fn direction_toggles(values: &ParagraphValues, words: &ParagraphWords) -> Vec> { + [ + (ParagraphDirection::Automatic, "pilcrow", &words.direction_auto), + (ParagraphDirection::LeftToRight, "pilcrow_right", &words.direction_ltr), + (ParagraphDirection::RightToLeft, "pilcrow_left", &words.direction_rtl), + ] + .into_iter() + .map(|(direction, icon, name)| { + Toggle::new(name, icon, Some(values.direction == Some(direction)), Box::new(move |_| ParagraphChange::Direction(direction))) + }) + .collect() +} diff --git a/crates/text/src/ui/character/rows.rs b/crates/text/src/ui/character/rows.rs index a22ba44ce..a3156965f 100644 --- a/crates/text/src/ui/character/rows.rs +++ b/crates/text/src/ui/character/rows.rs @@ -3,10 +3,12 @@ use std::ops::RangeInclusive; use std::sync::Arc; -use crate::ui::length::{DocUnit, length_field}; +use crate::ui::length::{DocUnit, length_field, shown}; use craft_behavior::{EditEvent, EditEvents, IconId, Mixed, NumberCodec}; use craft_ui::buttons::ToggleButton; -use craft_ui::contract::{Bind, Entry, FieldLayout, GlyphLabel, LabelKind, Props as _, Show as _, ShowContainer as _, SideLabel, TooltipSpec}; +use craft_ui::contract::{ + Bind, Entry, FieldLayout, GlyphLabel, LabelKind, Props as _, Show as _, ShowContainer as _, SideLabel, Sizing, TooltipSpec, +}; use craft_ui::form::Field; use craft_ui::layout_components::Row; use craft_ui::pickers::Select; @@ -14,47 +16,54 @@ use craft_ui::runtime::env::{Label, UnitKind, Units}; use craft_ui::text::{NamedOr, NumberField, NumberPick}; use egui::Ui; -use super::{KerningName, PRESET_DECIMALS, TRACKING_RANGE}; +#[cfg(feature = "font-picker")] +use super::FontPickerSource; +use super::{ + AUTO_LEADING, AUTO_ROW, CharacterChange, CharacterWords, KerningName, LEADING_RANGE, Leading, PRESET_DECIMALS, SIZE_PRESETS, TRACKING_RANGE, +}; +#[cfg(feature = "font-picker")] +use craft_font_manager::picker::FontPicker; /// The unit the length fields of a pattern show their numbers in. -pub(super) struct Lengths<'u> { - pub(super) units: &'u Units, - pub(super) unit: DocUnit, +pub(crate) struct Lengths<'u> { + pub(crate) units: &'u Units, + pub(crate) unit: DocUnit, } -/// A length field of a row: its label, side label, the mark's hint, value in points and range in points. -pub(super) struct LengthRow<'p> { - pub(super) label: Label, - pub(super) side: SideLabel, - pub(super) value: Option, - pub(super) range: RangeInclusive, - pub(super) presets: &'p [Entry], +/// A length field of a row: its label, side label, value in points and range in points. +pub(crate) struct LengthRow<'p> { + pub(crate) label: Label, + pub(crate) side: SideLabel, + pub(crate) value: Option, + pub(crate) range: RangeInclusive, + pub(crate) presets: &'p [Entry], } /// A plain number field of a row: a count, a percentage, or a value in thousandths. -pub(super) struct PlainRow<'p> { - pub(super) label: Label, - pub(super) side: SideLabel, - pub(super) value: Mixed>, - pub(super) range: RangeInclusive, - pub(super) percent: bool, - pub(super) presets: &'p [Entry], +pub(crate) struct PlainRow<'p> { + pub(crate) label: Label, + pub(crate) side: SideLabel, + pub(crate) value: Mixed>, + pub(crate) range: RangeInclusive, + pub(crate) percent: bool, + pub(crate) presets: &'p [Entry], } impl Lengths<'_> { - /// A field over a length in points, shown in the unit: its event in points and the preset row picked. - pub(super) fn row(&self, ui: &mut Ui, spec: LengthRow<'_>) -> (EditEvents>, Option) { + /// A field over a length in points, shown in the unit: its event in points and the preset row picked. The field + /// fills its grid cell. + pub(crate) fn row(&self, ui: &mut Ui, spec: LengthRow<'_>) -> (EditEvents>, Option) { let LengthRow { label, side, value, range, presets } = spec; let points = value.map_or(Mixed::Mixed, |value| Mixed::Same(Some(f64::from(value)))); let field = Field::new(label.clone()).side(side).show(ui, |ui, _| { - let out = length_field(self.unit, label.clone(), points, range.clone()).presets(presets).scrub_by_label().show(ui); + let out = length_field(self.unit, label.clone(), points, range.clone()).presets(presets).scrub_by_label().sizing(Sizing::FillWidth).show(ui); (out.edit.events, out.preset) }); field.inner.unwrap_or_default() } /// The kerning field: a number or the name of a presets row (Metrics, Optical). - pub(super) fn kerning( + pub(crate) fn kerning( &self, ui: &mut Ui, label: Label, @@ -72,7 +81,7 @@ impl Lengths<'_> { } /// A field over a number that is not a length: its event and the preset row picked. - pub(super) fn plain(&self, ui: &mut Ui, spec: PlainRow<'_>) -> (EditEvents>, Option) { + pub(crate) fn plain(&self, ui: &mut Ui, spec: PlainRow<'_>) -> (EditEvents>, Option) { let PlainRow { label, side, value, range, percent, presets } = spec; let units = self.units; let shown = Field::new(label.clone()).side(side).show(ui, |ui, _| { @@ -91,7 +100,7 @@ impl Lengths<'_> { /// A label shown as a mark at `layout`, whose drag scrubs the field; `hint`, when given, is its tooltip in place of the /// label. -pub(super) fn mark(glyph: GlyphLabel, hint: Option