From 552da163a288b1d4ab346b8ca5e2e6f6db6c01c7 Mon Sep 17 00:00:00 2001 From: aiongg Date: Sat, 14 Nov 2026 16:43:14 +0000 Subject: [PATCH] panels-w13: review fixes (review-1) --- crates/text/src/ui/character.rs | 38 ++++-- crates/text/src/ui/character/paragraph.rs | 15 ++- crates/text/src/ui/character/rows.rs | 108 ++++++++---------- crates/text/src/ui/character/style_options.rs | 55 ++++++--- crates/text/tests/it/main.rs | 2 +- crates/text/tests/it/style_options.rs | 2 +- 6 files changed, 123 insertions(+), 97 deletions(-) diff --git a/crates/text/src/ui/character.rs b/crates/text/src/ui/character.rs index 25b1466af..86eb9d9d7 100644 --- a/crates/text/src/ui/character.rs +++ b/crates/text/src/ui/character.rs @@ -42,8 +42,8 @@ pub use paragraph_dialog::{ SpacingKind, SpecialIndent, }; use rows::{ - 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, + LengthRow, Lengths, PlainRow, Toggle, agree, family_field, hidden_side, leading_menu, leading_row, mark, mixed, number_change, number_presets, + plain, select_row, size_menu, string_entries, toggle_row, }; pub use style_options::{ STYLE_COLOR_KEY, StyleAttribute, StyleOptionValues, StyleOptionsChange, TextStyleOptions, TextStyleOptionsOutput, TextStyleOptionsWords, @@ -345,10 +345,28 @@ pub struct CharacterValues { impl CharacterValues { /// The values of two selections together: each field stays where both are equal and becomes `None` where they /// differ. - // P3(panels-w13): tools-w24 decision, `CharacterValues::mixed_with` pub fn mixed_with(&self, other: &Self) -> Self { - let _ = other; - todo!("panels-w13") + CharacterValues { + family: agree(&self.family, &other.family), + style: agree(&self.style, &other.style), + size: agree(&self.size, &other.size), + leading: agree(&self.leading, &other.leading), + kerning: agree(&self.kerning, &other.kerning), + tracking: agree(&self.tracking, &other.tracking), + vertical_scale: agree(&self.vertical_scale, &other.vertical_scale), + horizontal_scale: agree(&self.horizontal_scale, &other.horizontal_scale), + baseline_shift: agree(&self.baseline_shift, &other.baseline_shift), + color: agree(&self.color, &other.color), + faux_bold: agree(&self.faux_bold, &other.faux_bold), + faux_italic: agree(&self.faux_italic, &other.faux_italic), + caps: agree(&self.caps, &other.caps), + position: agree(&self.position, &other.position), + underline: agree(&self.underline, &other.underline), + strikethrough: agree(&self.strikethrough, &other.strikethrough), + opentype: self.opentype.iter().copied().filter(|tag| other.opentype.contains(tag)).collect(), + language: agree(&self.language, &other.language), + axes: agree(&self.axes, &other.axes), + } } } @@ -531,7 +549,6 @@ impl<'a> CharacterControls<'a> { .chain(KERNING_PRESETS.iter().map(|&p| Entry::new(NumberPick::Number(f64::from(p)), units.number(f64::from(p), 0..=PRESET_DECIMALS)))) .collect(); - 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())); @@ -548,15 +565,12 @@ impl<'a> CharacterControls<'a> { let shown = Stack::new().label(self.label).show(ui, |ui, _| { #[cfg(feature = "font-picker")] - let family = family_field(ui, &words.family, mixed(values.family.clone()), &family_entries, picker.as_mut()); + let family = family_field(ui, &words, values, families, picker.as_mut()); #[cfg(not(feature = "font-picker"))] - let family = family_field(ui, &words.family, mixed(values.family.clone()), &family_entries); + let family = family_field(ui, &words, values, families); 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(family.edits); 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 { diff --git a/crates/text/src/ui/character/paragraph.rs b/crates/text/src/ui/character/paragraph.rs index 1b50a9474..364fd7e34 100644 --- a/crates/text/src/ui/character/paragraph.rs +++ b/crates/text/src/ui/character/paragraph.rs @@ -11,7 +11,7 @@ use craft_ui::runtime::env::{Env, Label}; use egui::{Response, Ui}; use super::TWO_COLUMNS; -use super::rows::{LengthRow, Lengths, Toggle, mark, mixed, number_change, toggle_row}; +use super::rows::{LengthRow, Lengths, Toggle, agree, mark, mixed, number_change, toggle_row}; craft_ui::words! { /// The Paragraph controls' words, English by default (PhotoCraft's tooltips). @@ -114,10 +114,17 @@ pub struct ParagraphValues { impl ParagraphValues { /// The values of two selections together: each field stays where both are equal and becomes `None` where they /// differ. - // P3(panels-w13): tools-w24 decision, `ParagraphValues::mixed_with` pub fn mixed_with(&self, other: &Self) -> Self { - let _ = other; - todo!("panels-w13") + ParagraphValues { + align: agree(&self.align, &other.align), + direction: agree(&self.direction, &other.direction), + start_indent: agree(&self.start_indent, &other.start_indent), + end_indent: agree(&self.end_indent, &other.end_indent), + first_line_indent: agree(&self.first_line_indent, &other.first_line_indent), + space_before: agree(&self.space_before, &other.space_before), + space_after: agree(&self.space_after, &other.space_after), + hyphenate: agree(&self.hyphenate, &other.hyphenate), + } } } diff --git a/crates/text/src/ui/character/rows.rs b/crates/text/src/ui/character/rows.rs index caef11ff6..748aefb25 100644 --- a/crates/text/src/ui/character/rows.rs +++ b/crates/text/src/ui/character/rows.rs @@ -19,7 +19,8 @@ use egui::Ui; #[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, + AUTO_LEADING, AUTO_ROW, CharacterChange, CharacterValues, CharacterWords, KerningName, LEADING_RANGE, Leading, PRESET_DECIMALS, SIZE_PRESETS, + TRACKING_RANGE, }; #[cfg(feature = "font-picker")] use craft_font_manager::picker::FontPicker; @@ -211,55 +212,6 @@ pub(crate) fn select_row( shown.inner.unwrap_or_default().map(&change) } -/// What the family field did this pass. -#[derive(Default)] -pub(crate) struct FamilyOutput { - /// The family edits: a pick commits; a hovered row is not one. - pub(crate) edits: Vec>, - /// The style picked in an expanded family's rows of the font picker. - pub(crate) picked_style: Option, - /// The family under the pointer or keyboard row in the open list. - pub(crate) preview: Option, - /// The family the pointer or keyboard rested on in the open list. - pub(crate) rested: Option, -} - -/// The family as a select over `entries`. -fn family_select(ui: &mut Ui, label: &Label, value: Mixed, entries: &[Entry]) -> FamilyOutput { - FamilyOutput { edits: select_row(ui, label, hidden_side(), value, entries, |name| name).into_iter().collect(), ..FamilyOutput::default() } -} - -/// The family field: the font picker over `picker`, or a select over `entries` when there is none. -#[cfg(feature = "font-picker")] -pub(crate) fn family_field( - ui: &mut Ui, - label: &Label, - value: Mixed, - entries: &[Entry], - picker: Option<&mut FontPickerSource<'_>>, -) -> FamilyOutput { - let Some(source) = picker else { return family_select(ui, label, value, entries) }; - let shown = Field::new(label.clone()).side(hidden_side()).show(ui, |ui, _| { - let mut field = FontPicker::new(label.clone(), Bind::given(value.clone()), source.catalog) - .settings(source.settings.reborrowed()) - .filters(source.filters.reborrowed()) - .served(source.served); - if let Some(text) = source.preview_text { - field = field.preview_text(text); - } - field.show(ui) - }); - let Some(out) = shown.inner else { return FamilyOutput::default() }; - let edits = out.edit.events.into_iter().filter(|event| matches!(event, EditEvent::Commit { .. } | EditEvent::Set { .. })).collect(); - FamilyOutput { edits, picked_style: out.picked_style, preview: out.preview, rested: out.rested } -} - -/// The family field: a select over `entries`. -#[cfg(not(feature = "font-picker"))] -pub(crate) fn family_field(ui: &mut Ui, label: &Label, value: Mixed, entries: &[Entry]) -> FamilyOutput { - family_select(ui, label, value, entries) -} - /// 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, @@ -305,27 +257,57 @@ pub(crate) fn toggle_row(ui: &mut Ui, label: Option<&Label>, toggles: &[Toggl } /// What [`family_field`] reports: the family edits, and the family the picker previews and rests on. -// P3(panels-w13): tools-w5 decision, `FamilyField` -#[allow(dead_code)] // P3(panels-w13): FamilyField +#[derive(Default)] pub(crate) struct FamilyField { - pub edits: Vec>, + /// The family edit, and the style edit of a style picked in an expanded family's rows; a hovered row is not one. + pub edits: Vec>, + /// The family under the pointer or keyboard row in the open list. pub preview: Option, + /// The family the pointer or keyboard rested on in the open list. pub rested: Option, } -/// The family field of the character controls and the type bar: the font picker in a hidden-label field when a -/// `picker` source is given, else a select over `families`. -// P3(panels-w13): tools-w5 decision, `family_field` -#[allow(dead_code)] // P3(panels-w13): family_field +/// 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`. pub(crate) fn family_field( ui: &mut Ui, - words: &super::CharacterWords, - values: &super::CharacterValues, + words: &CharacterWords, + values: &CharacterValues, families: &[String], - #[cfg(feature = "font-picker")] picker: Option<&mut super::FontPickerSource<'_>>, + #[cfg(feature = "font-picker")] picker: Option<&mut FontPickerSource<'_>>, ) -> FamilyField { - let _ = (ui, words, values, families); + let value = mixed(values.family.clone()); #[cfg(feature = "font-picker")] - let _ = picker; - todo!("panels-w13") + if let Some(source) = picker { + let shown = Field::new(words.family.clone()).side(hidden_side()).show(ui, |ui, _| { + let mut field = FontPicker::new(words.family.clone(), Bind::given(value.clone()), source.catalog) + .settings(source.settings.reborrowed()) + .filters(source.filters.reborrowed()) + .served(source.served); + if let Some(text) = source.preview_text { + field = field.preview_text(text); + } + field.show(ui) + }); + let Some(out) = shown.inner else { return FamilyField::default() }; + let mut edits: Vec> = out + .edit + .events + .into_iter() + .filter(|event| matches!(event, EditEvent::Commit { .. } | EditEvent::Set { .. })) + .map(|event| event.map(CharacterChange::Family)) + .collect(); + if let Some(style) = out.picked_style { + edits.push(picked_event(CharacterChange::Style(style), words.style.as_str())); + } + return FamilyField { edits, preview: out.preview, rested: out.rested }; + } + let entries = string_entries(families.iter().map(|name| (name.clone(), name.clone()))); + let edits = select_row(ui, &words.family, hidden_side(), value, &entries, CharacterChange::Family).into_iter().collect(); + FamilyField { edits, ..FamilyField::default() } +} + +/// Where two selections' optional values agree: that value, else `None` (mixed). +pub(crate) fn agree(a: &Option, b: &Option) -> Option { + if a == b { a.clone() } else { None } } diff --git a/crates/text/src/ui/character/style_options.rs b/crates/text/src/ui/character/style_options.rs index 8d8ed314f..7f837e25b 100644 --- a/crates/text/src/ui/character/style_options.rs +++ b/crates/text/src/ui/character/style_options.rs @@ -12,14 +12,14 @@ use craft_ui::display::Text; use craft_ui::form::Field; use craft_ui::layout_components::{Grid, Row, Stack}; use craft_ui::patterns::form_dialogs::HostRequests; -use craft_ui::runtime::env::{Env, Label}; +use craft_ui::runtime::env::{Env, Label, WordTemplate}; use craft_ui::text::{NumberPick, TextField}; use egui::{Response, Ui}; use super::paragraph::{INDENT_RANGE, SPACE_RANGE}; use super::rows::{ 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, + plain, select_row, size_menu, string_entries, toggle_row, }; use super::{ CharacterChange, CharacterValues, CharacterWords, ParagraphChange, ParagraphValues, ParagraphWords, SHIFT_RANGE, SIZE_RANGE, TRACKING_PRESETS, @@ -34,16 +34,16 @@ use super::FontPickerSource; mod stories; craft_ui::words! { - /// The words of the style options editor, English by default: the three short alignment names its rows show, the - /// name field, the include checks and the close button. The other labels come from the Character and Paragraph words. + /// The words of the style options editor, English by default: the three short alignment names its rows show. The + /// name field, the include checks and the close button are words the contract row does not list but the editor + /// needs. The other labels come from the Character and Paragraph words. #[non_exhaustive] pub struct TextStyleOptionsWords { left: "styleOptions.left" = "Left"; center: "styleOptions.center" = "Center"; right: "styleOptions.right" = "Right"; - // PLAN: the sheet lists these three words (note 3); the contract row names only the alignment names name: "styleOptions.name" = "Style name"; - include: "styleOptions.include" = "Include"; + include: template "styleOptions.include" = "Include {attribute}"; close: "styleOptions.close" = "Close"; } } @@ -262,7 +262,7 @@ impl<'a> TextStyleOptions<'a> { let size_presets = size_menu(&units, unit); let leading_presets = leading_menu(&cw, values.character.leading, &size_presets); let tracking_presets = number_presets(&units, TRACKING_PRESETS.iter().copied()); - 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 align_entries: Vec> = ALIGNS .iter() @@ -305,13 +305,10 @@ 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.family, mixed(c.family.clone()), &family_entries, picker.as_mut()); + let family = family_field(ui, &cw, c, families, picker.as_mut()); #[cfg(not(feature = "font-picker"))] - let family = family_field(ui, &cw.family, mixed(c.family.clone()), &family_entries); - let mut found = character(family.edits.into_iter().map(|event| event.map(CharacterChange::Family))); - if let Some(style) = family.picked_style { - found.extend(character([picked_event(CharacterChange::Style(style), cw.style.as_str())])); - } + let family = family_field(ui, &cw, c, families); + let mut found = character(family.edits); found.extend(character(select_row( ui, &cw.style, @@ -456,15 +453,41 @@ fn text_side(label: &Label) -> SideLabel { mark(GlyphLabel::text(label.clone()), None, FieldLayout::Beside) } -/// One row: the include check, then the row's `field`. A row that is not included shows the inherited value and takes -/// no input; its check still does. +/// The name of the row of `attribute`, which the include check's accessible name carries. +fn attribute_name(attribute: StyleAttribute, cw: &CharacterWords, pw: &ParagraphWords) -> Label { + match attribute { + StyleAttribute::Font => cw.family.clone(), + StyleAttribute::Size => cw.size.clone(), + StyleAttribute::Leading => cw.leading.clone(), + StyleAttribute::Tracking => cw.tracking.clone(), + StyleAttribute::BaselineShift => cw.baseline_shift.clone(), + StyleAttribute::Color => cw.text_color.clone(), + StyleAttribute::FauxBold => cw.faux_bold.clone(), + StyleAttribute::FauxItalic => cw.faux_italic.clone(), + StyleAttribute::Underline => cw.underline.clone(), + StyleAttribute::Strikethrough => cw.strikethrough.clone(), + StyleAttribute::Align => pw.alignment.clone(), + StyleAttribute::FirstLineIndent => pw.first_line_indent.clone(), + StyleAttribute::StartIndent => pw.start_indent.clone(), + StyleAttribute::EndIndent => pw.end_indent.clone(), + StyleAttribute::SpaceBefore => pw.space_before.clone(), + StyleAttribute::SpaceAfter => pw.space_after.clone(), + StyleAttribute::Hyphenate => pw.hyphenate.clone(), + } +} + +/// One row: the include check, named by the `include` template filled with the row's name, then the row's `field`. A +/// row that is not included shows the inherited value and takes no input; its check still does. fn include_row( ui: &mut Ui, - include: &Label, + include: &WordTemplate, attribute: StyleAttribute, on: bool, field: impl FnOnce(&mut Ui) -> Vec>, ) -> Vec> { + let env = Env::of(ui); + let name = attribute_name(attribute, &env.words::(), &env.words::()); + let include = Label::from(include.fill(&[("attribute", name.as_str())])); let mut found = Vec::new(); Grid::new(&TWO_COLUMNS).fill_width().show(ui, |ui, _| { let check = Field::new(include.clone()).side(hidden_side()).show(ui, |ui, _| Checkbox::new(Bind::given(Mixed::Same(on))).show(ui).events); diff --git a/crates/text/tests/it/main.rs b/crates/text/tests/it/main.rs index efb41a8c7..d66bfd240 100644 --- a/crates/text/tests/it/main.rs +++ b/crates/text/tests/it/main.rs @@ -4,7 +4,7 @@ mod editor; mod notes; -// PLAN: `mod path_text;` belongs here once tools-w4's `tests/it/path_text.rs` exists. It is not in this tree, so the declaration fails to compile. + mod ruler; mod style_options; mod style_panel; diff --git a/crates/text/tests/it/style_options.rs b/crates/text/tests/it/style_options.rs index 05326d6b1..06db29585 100644 --- a/crates/text/tests/it/style_options.rs +++ b/crates/text/tests/it/style_options.rs @@ -33,7 +33,7 @@ fn a_row_left_out_of_the_style_takes_no_input_and_its_check_includes_it() { fixture.idle(4); assert!(changes(&edits).is_empty(), "a row that is not included takes no input: {:?}", changes(&edits)); - let include = fixture.by_label("Include").expect("the include check is shown"); + let include = fixture.by_label("Include Font size").expect("the include check is shown"); fixture.click(&include).idle(4); assert_eq!( changes(&edits),