diff --git a/crates/ui-egui/src/ribbon/tests.rs b/crates/ui-egui/src/ribbon/tests.rs index d1c75079a6..681f4c856e 100644 --- a/crates/ui-egui/src/ribbon/tests.rs +++ b/crates/ui-egui/src/ribbon/tests.rs @@ -1071,3 +1071,163 @@ fn table_style_options_align_in_two_columns() { let caption = text_rect("Table Style Options"); assert!(banded.bottom() <= caption.top(), "checkboxes collide with group caption: {banded:?} vs {caption:?}"); } + +struct StylesGalleryLayout { + gallery: egui::Rect, + tiles: Vec<(String, egui::Rect, Option)>, + up: egui::Rect, + down: egui::Rect, + more: egui::Rect, +} + +/// Lay out the Home tab in LTR (`rtl == false`) or Arabic RTL (`rtl == true`) using `fixture_lang` +/// so the test never skips when `CRAFT_I18N_DIR` is unset, and collect the Styles gallery's +/// container, visible tiles, and stepper/More buttons. +fn styles_gallery_layout(rtl: bool) -> StylesGalleryLayout { + use egui::accesskit::Role; + + struct ResetLang; + impl Drop for ResetLang { + fn drop(&mut self) { + crate::i18n::apply_language(crate::i18n::Lang::EN); + } + } + let _reset = ResetLang; + + let lang = if rtl { + crate::i18n::fixture_lang("ar", "\tHome\tالرئيسية\n\tStyles\tالأنماط\n\tNormal\tعادي\n") + } else { + crate::i18n::Lang::EN + }; + + let mut app = WordApp::new(Session::new(wordcraft_doc::Document::new()), crate::Services::default()); + app.ui.language = "en".into(); + app.run("ui.tab", json!({"tab": "Home"})).unwrap(); + + let mut h = crate::test_harness::builder().with_size(vec2(2560.0, 900.0)).build_ui_state( + move |ui, app: &mut WordApp| { + let ctx = ui.ctx().clone(); + app.logic(&ctx); + if rtl { + ctx.set_missing_glyph_policy(egui::MissingGlyphPolicy::Tofu); + crate::i18n::apply_language(lang); + ctx.global_style_mut(|s| s.right_to_left = true); + craft::ui::runtime::env::with_env(ui, |o| o.direction = Some(craft::i18n::Direction::Rtl), |ui| app.ui(ui)); + ctx.global_style_mut(|s| s.right_to_left = false); + crate::i18n::apply_language(crate::i18n::Lang::EN); + } else { + app.ui(ui); + } + }, + app, + ); + crate::test_harness::frames(&mut h, 4); + + crate::i18n::apply_language(lang); + let gallery_label = crate::i18n::t("Styles").to_string(); + let names: Vec = h.state().session.doc.styles.gallery().iter().take(6).map(|s| crate::i18n::style_name(s)).collect(); + crate::i18n::apply_language(crate::i18n::Lang::EN); + + let gallery = h + .query_all(By::new().role(Role::RadioGroup).label(gallery_label.as_str())) + .next() + .unwrap_or_else(|| panic!("Styles radio group '{gallery_label}' not found")) + .rect(); + assert!(gallery.is_positive(), "Styles gallery rect must be positive: {gallery:?}"); + + let in_gallery = |rect: egui::Rect| gallery.contains_rect(rect); + let tiles: Vec<_> = names + .iter() + .filter_map(|name| { + h.query_all(By::new().role(Role::RadioButton).label(name.as_str())) + .find(|node| in_gallery(node.rect())) + .map(|node| (name.clone(), node.rect(), node.accesskit_node().toggled())) + }) + .collect(); + assert_eq!(tiles.len(), names.len(), "expected all {} visible Styles tiles inside {gallery:?}: {tiles:?} vs {names:?}", names.len()); + + let button = |label: &str| { + h.query_all(By::new().role(Role::Button).label(label)) + .find(|node| in_gallery(node.rect())) + .unwrap_or_else(|| panic!("gallery button '{label}' not found inside {gallery:?}")) + .rect() + }; + + StylesGalleryLayout { gallery, tiles, up: button("Previous row"), down: button("Next row"), more: button("More") } +} + +/// #5566, #7753: in a right-to-left interface the Styles gallery starts at the right, as the rest +/// of the ribbon does: the first style (Normal) is the rightmost tile, and the scroll arrows and +/// More button sit in a column at the gallery's left end. Left to right is unchanged. +#[test] +fn the_styles_gallery_reads_from_the_right_in_a_right_to_left_interface() { + use egui::accesskit::Toggled; + + let assert_common = |layout: &StylesGalleryLayout, expected_first: &str, dir: &str| { + assert_eq!(layout.tiles.len(), 6, "{dir}: expected 6 visible gallery tiles: {:?}", layout.tiles); + let (first_name, first_rect, first_toggled) = &layout.tiles[0]; + assert_eq!(first_name, expected_first, "{dir}: first tile should be localized Normal"); + assert_eq!(*first_toggled, Some(Toggled::True), "{dir}: active Normal style should be checked"); + assert!( + layout + .tiles + .iter() + .all(|(_, r, _)| r.is_positive() && layout.gallery.contains_rect(*r) && (r.center().y - first_rect.center().y).abs() <= 1.0), + "{dir}: gallery tiles must sit on one horizontal row inside {:?}: {:?}", + layout.gallery, + layout.tiles + ); + + let controls = [("Previous row", layout.up), ("Next row", layout.down), ("More", layout.more)]; + for (name, rect) in controls { + assert!(rect.is_positive(), "{dir}: {name} rect must be positive: {rect:?}"); + assert!(layout.gallery.contains_rect(rect), "{dir}: {name} {rect:?} must sit inside gallery {:?}", layout.gallery); + } + assert!( + (layout.up.center().x - layout.down.center().x).abs() <= 1.0 && (layout.down.center().x - layout.more.center().x).abs() <= 1.0, + "{dir}: up, down, and More buttons must align in one column: up={:?}, down={:?}, more={:?}", + layout.up, + layout.down, + layout.more + ); + assert!( + layout.up.max.y <= layout.down.min.y + 1.0 && layout.down.max.y <= layout.more.min.y + 1.0, + "{dir}: buttons must stack top-to-bottom (up -> down -> More): up={:?}, down={:?}, more={:?}", + layout.up, + layout.down, + layout.more + ); + }; + + // 1. Left-to-right (English) + let ltr = styles_gallery_layout(false); + assert_common(<r, "Normal", "LTR"); + assert!( + ltr.tiles.windows(2).all(|w| w[0].1.center().x < w[1].1.center().x && w[0].1.max.x <= w[1].1.min.x + 1.0), + "LTR: tiles must run left to right: {:?}", + ltr.tiles + ); + for (name, ctrl) in [("Previous row", ltr.up), ("Next row", ltr.down), ("More", ltr.more)] { + assert!( + ltr.tiles.iter().all(|(_, tile, _)| ctrl.min.x >= tile.max.x), + "LTR: {name} ({ctrl:?}) must sit to the right of every tile: {:?}", + ltr.tiles + ); + } + + // 2. Right-to-left (Arabic) + let rtl = styles_gallery_layout(true); + assert_common(&rtl, "عادي", "RTL"); + assert!( + rtl.tiles.windows(2).all(|w| w[0].1.center().x > w[1].1.center().x && w[0].1.min.x + 1.0 >= w[1].1.max.x), + "RTL: tiles must run right to left from the rightmost Normal tile: {:?}", + rtl.tiles + ); + for (name, ctrl) in [("Previous row", rtl.up), ("Next row", rtl.down), ("More", rtl.more)] { + assert!( + rtl.tiles.iter().all(|(_, tile, _)| ctrl.max.x <= tile.min.x), + "RTL: {name} ({ctrl:?}) must sit to the left of every tile: {:?}", + rtl.tiles + ); + } +}