From 730dc5f6c36d85fceba2f76fa79a7fb1d13b46fd Mon Sep 17 00:00:00 2001 From: semmelrinde <124386289+semmelrinde@users.noreply.github.com> Date: Thu, 29 Oct 2026 17:55:25 +0000 Subject: [PATCH] Segmented choices: show the options in the user's language (#8030) A segmented control measured its options translated but painted them as the caller passed them, so the many callers that pass English labels (the Coil's Circular | Square and Inside | Center | Outside, Revolve's One Side | Two Sides | Symmetric, Shell's direction, ...) showed English in every language. Only where the row was too narrow and the control fell back to its dropdown, which translates, did they read right. Segments now paint, size, describe and tip their options translated; the handles keep the option as passed. Co-authored-by: Claude Opus 5.5 --- crates/ui-egui/src/ui_kit/choice.rs | 19 +++++++++++-------- crates/ui-egui/src/ui_kit/choice_tests.rs | 19 +++++++++++++++++++ 2 files changed, 30 insertions(+), 8 deletions(-) diff --git a/crates/ui-egui/src/ui_kit/choice.rs b/crates/ui-egui/src/ui_kit/choice.rs index 5cd2d94c9..af451912c 100644 --- a/crates/ui-egui/src/ui_kit/choice.rs +++ b/crates/ui-egui/src/ui_kit/choice.rs @@ -58,7 +58,10 @@ pub(crate) fn segmented(ui: &mut Ui, selected: &mut usize, options: &[&str], con let t = Tokens::get(); let catalog = super::layout::in_catalog(ui); let font_size = if small || catalog { t.font_small } else { t.font_label }; - let natural: Vec<_> = options + // Callers pass English source labels or text they translated; either way a segment shows it + // translated, as the dropdown it falls back to does. Handles keep the option as passed. + let shown: Vec<&str> = options.iter().map(|option| crate::i18n::tr_label(option)).collect(); + let natural: Vec<_> = shown .iter() .enumerate() .map(|(index, option)| { @@ -69,9 +72,9 @@ pub(crate) fn segmented(ui: &mut Ui, selected: &mut usize, options: &[&str], con .collect(); let total = natural.iter().sum::(); let segment_width = dimension(ui.available_width()) / options.len().max(1) as f32; - let widest = options + let widest = shown .iter() - .map(|option| ui.painter().layout_no_wrap(crate::i18n::tr_label(option).to_string(), crate::fonts::semibold(font_size), t.form_ink).size().x) + .map(|option| ui.painter().layout_no_wrap((*option).to_owned(), crate::fonts::semibold(font_size), t.form_ink).size().x) .fold(0.0, f32::max); // Judged on the whole row, which is what a scrollbar narrows, not on one segment. let need = if intrinsic { total } else { (widest + 2.0 * t.space_8) * options.len() as f32 }; @@ -107,7 +110,7 @@ pub(crate) fn segmented(ui: &mut Ui, selected: &mut usize, options: &[&str], con let equal_width = rect.width() / options.len().max(1) as f32; let mut clicked = false; let mut x = rect.left(); - for (index, option) in options.iter().enumerate() { + for (index, (option, text)) in options.iter().zip(&shown).enumerate() { let width = if (intrinsic || proportional) && total > 0.0 { natural.get(index).copied().unwrap_or(0.0) * rect.width() / total } else { equal_width }; let segment = egui::Rect::from_min_size(egui::pos2(x, rect.top()), vec2(width, rect.height())); @@ -149,7 +152,7 @@ pub(crate) fn segmented(ui: &mut Ui, selected: &mut usize, options: &[&str], con ui.painter().vline(segment.left(), segment.y_range(), t.control_border_stroke(t.control_border)); } let font = if on { crate::fonts::semibold(font_size) } else { egui::FontId::proportional(font_size) }; - let galley = ui.painter().layout_no_wrap((*option).to_string(), font.clone(), t.form_ink); + let galley = ui.painter().layout_no_wrap((*text).to_string(), font.clone(), t.form_ink); let icon_width = if icons.get(index).is_some() { t.space_14 + t.space_6 } else { 0.0 }; let text_rect = egui::Rect::from_min_size( egui::pos2(segment.center().x - (galley.size().x + icon_width) / 2.0 + icon_width, segment.top()), @@ -166,7 +169,7 @@ pub(crate) fn segmented(ui: &mut Ui, selected: &mut usize, options: &[&str], con paint_text( ui, text_rect, - option, + text, font, if !segment_enabled { t.form_dim @@ -182,7 +185,7 @@ pub(crate) fn segmented(ui: &mut Ui, selected: &mut usize, options: &[&str], con false, ); t.button_bevel_with_radius(ui, &response, on, rounding); - response.widget_info(|| egui::WidgetInfo::selected(egui::WidgetType::Button, segment_enabled, on, *option)); + response.widget_info(|| egui::WidgetInfo::selected(egui::WidgetType::Button, segment_enabled, on, *text)); scenario::publish_target(&format!("dialog:{}:{option}", label(ui)), segment); if let Some(target) = targets.get(index) { let visible = segment.intersect(ui.clip_rect()); @@ -190,7 +193,7 @@ pub(crate) fn segmented(ui: &mut Ui, selected: &mut usize, options: &[&str], con scenario::publish_target(target, visible); } } - let response = if let Some(reason) = reason { response.on_disabled_hover_text(reason) } else { response.on_hover_text(*option) }; + let response = if let Some(reason) = reason { response.on_disabled_hover_text(reason) } else { response.on_hover_text(*text) }; if response.clicked() && segment_enabled { *selected = index; clicked = true; diff --git a/crates/ui-egui/src/ui_kit/choice_tests.rs b/crates/ui-egui/src/ui_kit/choice_tests.rs index 93d28d4d6..23d9fe57d 100644 --- a/crates/ui-egui/src/ui_kit/choice_tests.rs +++ b/crates/ui-egui/src/ui_kit/choice_tests.rs @@ -101,6 +101,25 @@ fn segmented_click_changes_selection_and_keeps_all_choices_visible() { } } +/// A segment shows its option in the user's language, as the dropdown it falls back to does, and +/// keeps the option as passed in its handle. +#[test] +fn segments_show_their_options_translated() { + let ctx = crate::fonts::test_context(); + let bounds = egui::Rect::from_min_size(egui::Pos2::ZERO, egui::vec2(600.0, 120.0)); + let _japanese = crate::i18n::scope("ja"); + let mut selected = 0; + let mut output = ctx.run_ui(egui::RawInput { screen_rect: Some(bounds), ..Default::default() }, |ui| { + row(ui, "Section", |ui| segmented(ui, &mut selected, &["Circular", "Square"], Default::default())); + }); + output.textures_delta.clear(); + let painted = |text: &str| output.shapes.iter().any(|shape| matches!(&shape.shape, egui::Shape::Text(t) if t.galley.job.text == text)); + for (option, shown) in [("Circular", "円形"), ("Square", "正方形")] { + assert!(painted(shown) && !painted(option), "{option} shows as {shown}"); + assert!(scenario::handle_at(&format!("dialog:Section:{option}")).is_some(), "{option} keeps its handle"); + } +} + #[test] fn optional_field_keeps_empty_default_and_options_use_two_columns() { let ctx = crate::fonts::test_context();