From 579aa6cf5f634de43e2c6a4831c363fb88ac2d28 Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Thu, 26 Nov 2026 17:50:50 -0800 Subject: [PATCH] Restore localized Paragraph alignment and spacing controls --- crates/ui-egui/src/panels/paragraph.rs | 39 ++++++- crates/ui-egui/src/panels/paragraph/tests.rs | 114 +++++++++++++++++++ 2 files changed, 147 insertions(+), 6 deletions(-) diff --git a/crates/ui-egui/src/panels/paragraph.rs b/crates/ui-egui/src/panels/paragraph.rs index 26102b5311..6b93911427 100644 --- a/crates/ui-egui/src/panels/paragraph.rs +++ b/crates/ui-egui/src/panels/paragraph.rs @@ -8,8 +8,9 @@ // PLAN: assumed import paths for the framework items below (behavior::{IconId, Mixed}, keymap::CommandRef, // text::ui::character, ui::{buttons, choice::Select, controls::Checkbox, menu, popover, runtime::access::PopupKind}). use craft::behavior::{IconId, Mixed}; +use craft::i18n::WordSet as _; use craft::keymap::CommandRef; -use craft::text::ui::character::{DirectionRow, ParagraphChange, ParagraphControls, ParagraphDirection, ParagraphValues, TextAlign}; +use craft::text::ui::character::{DirectionRow, ParagraphChange, ParagraphControls, ParagraphDirection, ParagraphValues, ParagraphWords, TextAlign}; use craft::ui::buttons::{IconButton, PopupMark, ToggleButton}; use craft::ui::choice::Checkbox; use craft::ui::contract::{Bind, Entry, MemoProps, Props as _, Show as _, ShowContainer as _, Trigger}; @@ -21,6 +22,7 @@ use craft::ui::popover::Popover; use craft::ui::primitives::{Glyph, IconSpec}; use craft::ui::runtime::access::PopupKind; use craft::ui::runtime::commands::{CommandState, CommandStates}; +use craft::ui::runtime::env::{WordRows, WordsOverride, with_env}; use craft::ui::style::UiTokens; use egui::{Color32, Painter, Rect, Stroke, Ui, vec2}; use serde_json::{Value, json}; @@ -821,6 +823,30 @@ fn kinsoku_row(app: &mut VectorcraftApp, ui: &mut Ui) { } } +/// Maps the toolkit's different English names to the translations already in the app catalogs. +fn localized_control_words() -> WordRows { + [ + ("paragraph.left", ALIGNMENTS[0].2), + ("paragraph.center", ALIGNMENTS[1].2), + ("paragraph.right", ALIGNMENTS[2].2), + ("paragraph.justifyLeft", ALIGNMENTS[3].2), + ("paragraph.justifyCenter", ALIGNMENTS[4].2), + ("paragraph.justifyRight", ALIGNMENTS[5].2), + ("paragraph.justifyAll", ALIGNMENTS[6].2), + ("paragraph.spaceBefore", "Space Before Paragraph"), + ("paragraph.spaceAfter", "Space After Paragraph"), + ] + .into_iter() + .filter_map(|(id, source)| { + let translated = tl!(source); + if translated == source { + return None; + } + ParagraphWords::word(id).map(|word| (word, translated)) + }) + .fold(WordRows::new(), |rows, (word, translated)| rows.row(word, translated)) +} + pub fn show(app: &mut VectorcraftApp, ui: &mut Ui) { let Some((_, para)) = text_style(app) else { super::empty_state(ui, "pilcrow", tl!("No text selected"), tl!("Select a text object to edit its paragraph attributes.")); @@ -831,11 +857,12 @@ pub fn show(app: &mut VectorcraftApp, ui: &mut Ui) { // Paragraph direction, with the Middle Eastern (Indic) options. let direction = if app.session.prefs.show_indic_options { DirectionRow::Explicit } else { DirectionRow::Hidden }; // Indents and paragraph spacing are distances (General); type sizes follow Units ▸ Type. - let out = ParagraphControls::new(tl!("Paragraph"), &shown) - .unit(doc_unit(app.session.general_unit())) - .hyphenate(!hidden) - .direction_row(direction) - .show_with(ui, |ui| lists_row(app, ui)); + let controls = + ParagraphControls::new(tl!("Paragraph"), &shown).unit(doc_unit(app.session.general_unit())).hyphenate(!hidden).direction_row(direction); + let words = localized_control_words(); + let show = |ui: &mut Ui| controls.show_with(ui, |ui| lists_row(app, ui)); + let out = + if words.iter().next().is_some() { with_env(ui, |overrides| overrides.words = Some(WordsOverride::Overlay(words)), show) } else { show(ui) }; for event in &out.edits { edits::apply_event(app, event, &EditPath::Scope, &mut |app, change, _| apply(app, change)); } diff --git a/crates/ui-egui/src/panels/paragraph/tests.rs b/crates/ui-egui/src/panels/paragraph/tests.rs index 0d56471f6d..6bdf50ca0b 100644 --- a/crates/ui-egui/src/panels/paragraph/tests.rs +++ b/crates/ui-egui/src/panels/paragraph/tests.rs @@ -25,6 +25,120 @@ fn editing_text(text: &str) -> (VectorcraftApp, Value) { (app, id) } +/// The minimum-width Paragraph dock keeps translated alignment and spacing controls usable. +#[test] +fn compact_paragraph_dock_keeps_localized_alignment_and_spacing_usable() { + use craft::behavior::dock::{DockEdge, Node, Workspace}; + use egui::accesskit::Role; + use egui::{Event, Key, Modifiers, PointerButton, Pos2, pos2}; + use std::sync::Arc; + + let width = crate::panel_docking::PANEL_MIN_WIDTH; + let screen = Rect::from_min_size(Pos2::ZERO, vec2(1600.0, 1000.0)); + let mut untranslated = Vec::new(); + for lang in ["en", "de", "fr", "pt-br"] { + let mut app = VectorcraftApp::new(Session::new(), Default::default()); + app.session.prefs.interface_language = lang.into(); + app.run("file.new", json!({"width": 400, "height": 300})).unwrap(); + let id = + app.run("text.create", json!({"x": 20, "y": 50, "text": "First\nSecond", "area": {"width": 180, "height": 100}})).unwrap()["id"].clone(); + app.run("select.set", json!({"ids": [id]})).unwrap(); + app.run("window.panel", json!({"panel": "paragraph"})).unwrap(); + app.run("window.panel.dock", json!({"panel": "paragraph", "column": true})).unwrap(); + app.run("window.panel.activate", json!({"panel": "paragraph"})).unwrap(); + + let ws = &app.ui.dock_workspace; + let mut layout = ws.layout().clone(); + let path = layout.dock_path(DockEdge::End).unwrap(); + let mut node = layout.root.as_mut().unwrap(); + for second_child in path { + let Node::Split { first, second, .. } = node else { panic!("end dock path") }; + node = if second_child { second } else { first }; + } + let Node::Dock { columns, .. } = node else { panic!("end dock") }; + let column = columns.iter_mut().find(|column| column.groups.iter().any(|group| group.panels.iter().any(|p| p == "paragraph"))).unwrap(); + assert!(!column.iconic); + column.width = Some(width); + let next = Workspace::from_parts(layout, ws.hidden().to_vec(), ws.floated().to_vec()); + Arc::make_mut(&mut app.ui.dock_workspace).replace(next); + + let ctx = crate::testing::ui_context(); + ctx.enable_accesskit(); + let mut time = 0.0; + let mut frame = |app: &mut VectorcraftApp, mut events: Vec, modifiers| { + time += 0.05; + events.insert(0, Event::ModifiersChanged(modifiers)); + let input = egui::RawInput { time: Some(time), events, screen_rect: Some(screen), ..Default::default() }; + crate::testing::app_frame(app, &ctx, input).platform_output.accesskit_update.map_or_else(Vec::new, |update| update.nodes) + }; + for _ in 0..7 { + frame(&mut app, Vec::new(), Modifiers::NONE); + } + let nodes = frame(&mut app, Vec::new(), Modifiers::NONE); + let rect = + |node: &egui::accesskit::Node| node.bounds().map(|b| Rect::from_min_max(pos2(b.x0 as f32, b.y0 as f32), pos2(b.x1 as f32, b.y1 as f32))); + let title = crate::i18n::t("Paragraph"); + let (_, body_node) = nodes.iter().find(|(_, node)| node.role() == Role::TabPanel && node.label() == Some(title)).unwrap(); + let body = rect(body_node).unwrap(); + assert!((body.width() - width).abs() < 1.0, "{lang}: {body:?}"); + + let mut descendants = Vec::new(); + let mut todo = body_node.children().to_vec(); + while let Some(id) = todo.pop() { + let Some((_, node)) = nodes.iter().find(|(key, _)| *key == id) else { continue }; + todo.extend(node.children()); + if let (Some(label), Some(bounds)) = (node.label(), rect(node)) { + descendants.push((label, node.role(), bounds)); + } + } + let mut control = |names: [&str; 2], role: Option| { + let (label, _, bounds) = descendants + .iter() + .find(|(label, kind, _)| { + role.is_none_or(|expected| *kind == expected) && names.iter().any(|name| *label == *name || *label == crate::i18n::t(name)) + }) + .unwrap_or_else(|| panic!("{lang}: no {names:?} with role {role:?}")); + assert!(bounds.is_positive() && body.expand(0.5).contains_rect(*bounds), "{lang}: {label} {bounds:?} outside {body:?}"); + let expected = crate::i18n::t(names[1]); + if expected != names[1] && *label != expected { + untranslated.push(format!("{lang}: {label:?}, expected {expected:?}")); + } + bounds.center() + }; + for names in [ + ["Left align text", "Align left"], + ["Right align text", "Align right"], + ["Justify last left", "Justify with last line aligned left"], + ["Justify last centered", "Justify with last line aligned center"], + ["Justify last right", "Justify with last line aligned right"], + ["Justify all", "Justify all lines"], + ] { + control(names, None); + } + let center = control(["Center text", "Align center"], None); + let before = control(["Add space before paragraph", "Space Before Paragraph"], Some(Role::SpinButton)); + let after = control(["Add space after paragraph", "Space After Paragraph"], Some(Role::SpinButton)); + let button = |pos, pressed| Event::PointerButton { pos, button: PointerButton::Primary, pressed, modifiers: Modifiers::NONE }; + let key = |key, modifiers, pressed| Event::Key { key, physical_key: None, pressed, repeat: false, modifiers }; + frame(&mut app, vec![Event::PointerMoved(center), button(center, true)], Modifiers::NONE); + frame(&mut app, vec![button(center, false)], Modifiers::NONE); + frame(&mut app, Vec::new(), Modifiers::NONE); + assert_eq!(selected(&app), [Justify::Center], "{lang}: pointer changed alignment"); + for (at, input) in [(before, "12"), (after, "18")] { + frame(&mut app, vec![Event::PointerMoved(at), button(at, true)], Modifiers::NONE); + frame(&mut app, vec![button(at, false)], Modifiers::NONE); + frame(&mut app, vec![key(Key::A, Modifiers::COMMAND, true)], Modifiers::COMMAND); + frame(&mut app, vec![key(Key::A, Modifiers::COMMAND, false), Event::Text(input.to_owned())], Modifiers::NONE); + frame(&mut app, vec![key(Key::Enter, Modifiers::NONE, true)], Modifiers::NONE); + frame(&mut app, vec![key(Key::Enter, Modifiers::NONE, false)], Modifiers::NONE); + } + assert_eq!(shared_para_field(&app, |p| p.space_before), Some(12.0), "{lang}: keyboard updated before"); + assert_eq!(shared_para_field(&app, |p| p.space_after), Some(18.0), "{lang}: keyboard updated after"); + crate::i18n::set_current(crate::i18n::Lang::EN); + } + assert!(untranslated.is_empty(), "lost Paragraph translations: {untranslated:?}"); +} + fn select_range(app: &mut VectorcraftApp, start: usize, end: usize) { app.session.set_tool_option("select", &json!({"start": start, "end": end})); }