diff --git a/crates/ui-egui/locales/cs.json b/crates/ui-egui/locales/cs.json index ea10219217..a1f2fc2699 100644 --- a/crates/ui-egui/locales/cs.json +++ b/crates/ui-egui/locales/cs.json @@ -806,6 +806,7 @@ "Golden": "Zlaté", "Golden Glow": "Zlatá záře", "Golden Hour": "Zlatá hodina", + "Golden Ratio": "Zlatý řez", "Grab bag": "Různorodé", "Grain": "Zrnitost", "Graphic": "Grafika", diff --git a/crates/ui-egui/locales/de.json b/crates/ui-egui/locales/de.json index bc67606eb6..600aaf6865 100644 --- a/crates/ui-egui/locales/de.json +++ b/crates/ui-egui/locales/de.json @@ -626,6 +626,7 @@ "Golden": "Goldfarben", "Golden Glow": "Goldener Glanz", "Golden Hour": "Goldene Stunde", + "Golden Ratio": "Goldener Schnitt", "Grab bag": "Namensliste", "Grain": "Körnung", "Graphic": "Grafisch", diff --git a/crates/ui-egui/locales/es.json b/crates/ui-egui/locales/es.json index c34057bdfe..43422791b6 100644 --- a/crates/ui-egui/locales/es.json +++ b/crates/ui-egui/locales/es.json @@ -632,6 +632,7 @@ "Golden": "Dorado", "Golden Glow": "Resplandor dorado", "Golden Hour": "Hora dorada", + "Golden Ratio": "Proporción áurea", "Grab bag": "Miscelánea", "Grain": "Grano", "Graphic": "Gráfico", diff --git a/crates/ui-egui/locales/fi.json b/crates/ui-egui/locales/fi.json index 59cc611cd6..23e0446d33 100644 --- a/crates/ui-egui/locales/fi.json +++ b/crates/ui-egui/locales/fi.json @@ -796,6 +796,7 @@ "Golden": "Kultainen", "Golden Glow": "Kultainen hehku", "Golden Hour": "Kultainen hetki", + "Golden Ratio": "Kultainen leikkaus", "Grab bag": "Sekalaista", "Grain": "Rakeisuus", "Graphic": "Grafiikka", diff --git a/crates/ui-egui/locales/fr.json b/crates/ui-egui/locales/fr.json index 31e6814b6d..a1b2ac6e66 100644 --- a/crates/ui-egui/locales/fr.json +++ b/crates/ui-egui/locales/fr.json @@ -591,6 +591,7 @@ "Golden": "Doré", "Golden Glow": "Lueur dorée", "Golden Hour": "Heure dorée", + "Golden Ratio": "Nombre d’or", "Grab bag": "Liste de noms", "Grain": "Grain", "Graphic": "Graphique", diff --git a/crates/ui-egui/locales/id.json b/crates/ui-egui/locales/id.json index 52705328cc..a7040ac4fe 100644 --- a/crates/ui-egui/locales/id.json +++ b/crates/ui-egui/locales/id.json @@ -803,6 +803,7 @@ "Golden": "Rasio emas", "Golden Glow": "Hangat Keemasan", "Golden Hour": "Golden Hour", + "Golden Ratio": "Rasio emas", "Grab bag": "Daftar nama", "Grain": "Butiran", "Graphic": "Grafis", diff --git a/crates/ui-egui/locales/it.json b/crates/ui-egui/locales/it.json index 5b0ccc0cee..8a5f5926d2 100644 --- a/crates/ui-egui/locales/it.json +++ b/crates/ui-egui/locales/it.json @@ -592,6 +592,7 @@ "Golden": "Dorato", "Golden Glow": "Bagliore dorato", "Golden Hour": "Ora d'oro", + "Golden Ratio": "Sezione aurea", "Grab bag": "Varie", "Grain": "Grana", "Graphic": "Grafica", diff --git a/crates/ui-egui/locales/ja.json b/crates/ui-egui/locales/ja.json index 97c15dd019..0620ddb394 100644 --- a/crates/ui-egui/locales/ja.json +++ b/crates/ui-egui/locales/ja.json @@ -788,6 +788,7 @@ "Global Saturation": "全体の彩度", "Glossy": "光沢紙", "Golden": "黄金比", + "Golden Ratio": "黄金比", "Grain": "粒子", "Graphic": "画像", "Green": "グリーン", diff --git a/crates/ui-egui/locales/ka.json b/crates/ui-egui/locales/ka.json index 30769b60fa..a9484392b1 100644 --- a/crates/ui-egui/locales/ka.json +++ b/crates/ui-egui/locales/ka.json @@ -874,6 +874,7 @@ "Golden": "ოქროსფერი", "Golden Glow": "ოქროსფერი ნათება", "Golden Hour": "ოქროს საათი", + "Golden Ratio": "ოქროს კვეთა", "Grab bag": "სხვადასხვა", "Grain": "მარცვლოვნება", "Graphic": "გრაფიკა", diff --git a/crates/ui-egui/locales/pl.json b/crates/ui-egui/locales/pl.json index dbc29a1941..b5739663f9 100644 --- a/crates/ui-egui/locales/pl.json +++ b/crates/ui-egui/locales/pl.json @@ -576,6 +576,7 @@ "Golden": "Złocisty", "Golden Glow": "Złota poświata", "Golden Hour": "Złota godzina", + "Golden Ratio": "Złoty podział", "Grab bag": "Zbiór różnych elementów", "Grain": "Ziarno", "Graphic": "Graficzny", diff --git a/crates/ui-egui/locales/pt-br.json b/crates/ui-egui/locales/pt-br.json index 3aedd82906..c0d6b5f316 100644 --- a/crates/ui-egui/locales/pt-br.json +++ b/crates/ui-egui/locales/pt-br.json @@ -600,6 +600,7 @@ "Golden": "Dourado", "Golden Glow": "Brilho dourado", "Golden Hour": "Hora dourada", + "Golden Ratio": "Proporção áurea", "Grab bag": "Lista de nomes", "Grain": "Granulado", "Graphic": "Gráfico", diff --git a/crates/ui-egui/locales/ru.json b/crates/ui-egui/locales/ru.json index b3df02470e..2207776a6d 100644 --- a/crates/ui-egui/locales/ru.json +++ b/crates/ui-egui/locales/ru.json @@ -690,6 +690,7 @@ "Golden": "Золотой", "Golden Glow": "Золотое сияние", "Golden Hour": "Золотой час", + "Golden Ratio": "Золотое сечение", "Grain": "Зерно", "Graphic": "Графичный", "Green": "Зелёный", diff --git a/crates/ui-egui/locales/sv.json b/crates/ui-egui/locales/sv.json index 94b9e30333..b9be6f6f93 100644 --- a/crates/ui-egui/locales/sv.json +++ b/crates/ui-egui/locales/sv.json @@ -850,6 +850,7 @@ "Golden": "Gyllene", "Golden Glow": "Gyllene glöd", "Golden Hour": "Gyllene timmen", + "Golden Ratio": "Gyllene snittet", "GPS Track Log": "GPS-spårlogg", "GPU Rendering": "GPU-rendering", "GPU rendering": "GPU-rendering", diff --git a/crates/ui-egui/locales/uk.json b/crates/ui-egui/locales/uk.json index 09b78fab1e..b94bcab9eb 100644 --- a/crates/ui-egui/locales/uk.json +++ b/crates/ui-egui/locales/uk.json @@ -983,6 +983,7 @@ "Global Luminance": "Загальна яскравість", "Global Saturation": "Загальна насиченість", "Golden": "Золотисте", + "Golden Ratio": "Золотий переріз", "Green Amount": "Кількість зеленого", "Green Hue": "Тон зеленого", "Green Hue High": "Верхня межа тону зеленого", diff --git a/crates/ui-egui/locales/vi.json b/crates/ui-egui/locales/vi.json index af27e9aa0a..e698e74571 100644 --- a/crates/ui-egui/locales/vi.json +++ b/crates/ui-egui/locales/vi.json @@ -735,6 +735,7 @@ "Golden": "Vàng", "Golden Glow": "Ánh vàng", "Golden Hour": "Giờ vàng", + "Golden Ratio": "Tỷ lệ vàng", "Grab bag": "Tạp hợp", "Grain": "Hạt", "Graphic": "Đồ họa", diff --git a/crates/ui-egui/locales/zh-hans.json b/crates/ui-egui/locales/zh-hans.json index ca13d782a5..44d0bc78a0 100644 --- a/crates/ui-egui/locales/zh-hans.json +++ b/crates/ui-egui/locales/zh-hans.json @@ -740,6 +740,7 @@ "Golden": "黄金比例", "Golden Glow": "金色光晕", "Golden Hour": "黄金时刻", + "Golden Ratio": "黄金比例", "Grain": "颗粒", "Graphic": "图形", "Green": "绿色", diff --git a/crates/ui-egui/locales/zh-hant.json b/crates/ui-egui/locales/zh-hant.json index f3dfc422ec..189b66778f 100644 --- a/crates/ui-egui/locales/zh-hant.json +++ b/crates/ui-egui/locales/zh-hant.json @@ -741,6 +741,7 @@ "Golden": "黃金比例", "Golden Glow": "金色光暈", "Golden Hour": "黃金時刻", + "Golden Ratio": "黃金比例", "Grain": "顆粒", "Graphic": "圖像", "Green": "綠色", diff --git a/crates/ui-egui/src/panels/detail/retouch.rs b/crates/ui-egui/src/panels/detail/retouch.rs index d74ead9ba9..4ad80d3e7e 100644 --- a/crates/ui-egui/src/panels/detail/retouch.rs +++ b/crates/ui-egui/src/panels/detail/retouch.rs @@ -63,10 +63,12 @@ pub(super) fn remove_overlay(app: &mut LightcraftApp, ui: &mut egui::Ui, resp: & .copied() }; let hover = resp.hover_pos().and_then(hit); + // with no brush chosen (the Remove panel's Mode is None) the photo takes no new spots + let brush = matches!(app.ui.tool.as_str(), "remove" | "fill" | "heal" | "clone"); if let Some(h) = resp.hover_pos() { if hover.is_some() { ui.ctx().set_cursor_icon(egui::CursorIcon::Grab); - } else { + } else if brush { ui.ctx().set_cursor_icon(egui::CursorIcon::None); p.circle_stroke(h, r, Stroke::new(1.0, Color32::WHITE)); p.circle_stroke(h, r * (1.0 - app.ui.remove_feather / 100.0).max(0.05), Stroke::new(1.0, Color32::from_white_alpha(110))); @@ -104,7 +106,8 @@ pub(super) fn remove_overlay(app: &mut LightcraftApp, ui: &mut egui::Ui, resp: & let _ = app.run("spot.select", json!({"index": spot})); return; } - if let Some(q) = resp.interact_pointer_pos() + if brush + && let Some(q) = resp.interact_pointer_pos() && (resp.drag_started() || resp.dragged() || resp.clicked()) { let n = map.norm(q); diff --git a/crates/ui-egui/src/panels/right/crop.rs b/crates/ui-egui/src/panels/right/crop.rs index 09e1ecc29b..afdcf141a6 100644 --- a/crates/ui-egui/src/panels/right/crop.rs +++ b/crates/ui-egui/src/panels/right/crop.rs @@ -80,7 +80,9 @@ pub(super) fn crop(app: &mut LightcraftApp, ui: &mut egui::Ui, id: PhotoId) { ui.label(crate::i18n::tr("Aspect Ratio")); let cur = aspect_label(app.crop_tool_aspect(), original); let t = Tokens::get(ui.ctx()); - let r = crate::widgets::dropdown(ui, "cropAspect", crate::i18n::tr(&cur), t.font(12.5), t.text); + // as wide as the longest preset; a custom ratio (`1.37 : 1`) is shorter than that + let choices = ASPECT_PRESETS.map(|(label, _)| crate::i18n::tr(label)); + let r = crate::widgets::dropdown_fitting(ui, "cropAspect", crate::i18n::tr(&cur), &choices, t.font(12.5), t.text); egui::Popup::menu(&r).show(|ui| { for (label, a) in ASPECT_PRESETS { @@ -184,27 +186,25 @@ pub(super) fn crop(app: &mut LightcraftApp, ui: &mut egui::Ui, id: PhotoId) { }); }); padded(ui, |ui| { + use crate::state::CropOverlay as O; + let opts = [ + (O::Thirds, "Thirds", "thirds"), + (O::Grid, "Grid", "grid"), + (O::Golden, "Golden Ratio", "golden"), + (O::Diagonal, "Diagonal", "diagonal"), + (O::Triangle, "Triangle", "triangle"), + (O::Spiral, "Spiral", "spiral"), + (O::None, "None", "none"), + ]; + let items: Vec<(&str, &str)> = opts.iter().map(|(_, l, k)| (*l, *k)).collect(); + let active = opts.iter().position(|(o, _, _)| *o == app.ui.crop_overlay); + if let Some(i) = crate::widgets::labeled_dropdown(ui, "cropOverlay", "Overlay", &items, active) { + app.ui.crop_overlay = opts[i].0; + } + if matches!(app.ui.crop_overlay, O::Triangle | O::Spiral) + && text_button(ui, "cropOverlayOrient", crate::i18n::tr("Flip Overlay (⇧O)"), false).clicked() { - use crate::state::CropOverlay as O; - let opts = [ - (O::Thirds, "Thirds", "thirds"), - (O::Grid, "Grid", "grid"), - (O::Golden, "Golden Ratio", "golden"), - (O::Diagonal, "Diagonal", "diagonal"), - (O::Triangle, "Triangle", "triangle"), - (O::Spiral, "Spiral", "spiral"), - (O::None, "None", "none"), - ]; - let items: Vec<(&str, &str)> = opts.iter().map(|(_, l, k)| (*l, *k)).collect(); - let active = opts.iter().position(|(o, _, _)| *o == app.ui.crop_overlay); - if let Some(i) = crate::widgets::labeled_dropdown(ui, "cropOverlay", "Overlay", &items, active) { - app.ui.crop_overlay = opts[i].0; - } - if matches!(app.ui.crop_overlay, O::Triangle | O::Spiral) - && text_button(ui, "cropOverlayOrient", crate::i18n::tr("Flip Overlay (⇧O)"), false).clicked() - { - let _ = app.run("view.cropOverlayOrientation", json!({})); - } + let _ = app.run("view.cropOverlayOrientation", json!({})); } }); divider(ui); @@ -274,11 +274,12 @@ pub(super) fn remove(app: &mut LightcraftApp, ui: &mut egui::Ui, id: PhotoId) { let d = app.session.develop_of(id).unwrap_or_default(); header(ui, "Remove"); padded(ui, |ui| { - // painting with no mode chosen adds a Remove spot (`retouch.rs`), so the dropdown names it then - let modes = [("Remove", "remove"), ("Content-Aware", "fill"), ("Heal", "heal"), ("Clone", "clone")]; + // None, the first item, is what shows while no brush is chosen: the photo then takes no new + // spots (`retouch.rs`), and existing ones can still be selected and moved + let modes = [("None", "none"), ("Remove", "remove"), ("Content-Aware", "fill"), ("Heal", "heal"), ("Clone", "clone")]; let active = modes.iter().position(|(_, tool)| app.ui.tool == *tool); - if let Some(i) = crate::widgets::labeled_dropdown(ui, "removeMode", "Mode", &modes, active) { - app.ui.tool = modes[i].1.into(); + if let Some(&(_, tool)) = crate::widgets::labeled_dropdown(ui, "removeMode", "Mode", &modes, active).and_then(|i| modes.get(i)) { + app.ui.tool = if tool == "none" { String::new() } else { tool.into() }; } ui.add_space(8.0); ui.label(crate::i18n::tr_format!("{} spot(s) on this photo", d.spots.len())); @@ -401,7 +402,8 @@ pub(crate) fn tool_overlay_options(app: &mut LightcraftApp, ui: &mut egui::Ui) { ui.horizontal(|ui| { ui.label(egui::RichText::new(crate::i18n::tr("Tool Overlay")).color(t.text_dim)); let current = app.ui.tool_overlay; - let r = crate::widgets::dropdown(ui, "toolOverlay", crate::i18n::tr(current.label()), t.font(12.5), t.text_label) + let choices = ToolOverlay::ALL.map(|m| crate::i18n::tr(m.label())); + let r = crate::widgets::dropdown_fitting(ui, "toolOverlay", crate::i18n::tr(current.label()), &choices, t.font(12.5), t.text_label) .on_hover_text(crate::i18n::tr("When pins show on the photo (Shift+H cycles)")); egui::Popup::menu(&r).show(|ui| { for m in ToolOverlay::ALL { diff --git a/crates/ui-egui/src/tests/mode_dropdowns.rs b/crates/ui-egui/src/tests/mode_dropdowns.rs index cc7c86749c..fa5aa3aa69 100644 --- a/crates/ui-egui/src/tests/mode_dropdowns.rs +++ b/crates/ui-egui/src/tests/mode_dropdowns.rs @@ -6,11 +6,13 @@ use std::time::Duration; use serde_json::json; use crate::headless::Headless; +use crate::i18n::Locale; use crate::state::{CropOverlay, RightPanel}; use crate::{LightcraftApp, Services}; const T: Duration = Duration::from_secs(20); +/// The demo library in Detail with `panel` (a `panel.*` command) open. fn detail(panel: &str) -> Headless { let services = Services { png: None, ..Default::default() }; let app = LightcraftApp::new(lightcraft_engine::Session::with_demo(), services); @@ -24,19 +26,37 @@ fn detail(panel: &str) -> Headless { h } +/// Click the widget registered as `id`. +fn click(h: &mut Headless, id: &str) { + let r = h.request("ui.clickWidget", json!({"id": id}), T); + assert_eq!(r["ok"], true, "{id}: {r}"); +} + /// Open the dropdown `id` and choose its item `key`. fn choose(h: &mut Headless, id: &str, key: &str) { - let r = h.request("ui.clickWidget", json!({"id": format!("dropdown:{id}")}), T); - assert_eq!(r["ok"], true, "dropdown:{id}: {r}"); - let r = h.request("ui.clickWidget", json!({"id": format!("{id}:{key}")}), T); - assert_eq!(r["ok"], true, "{id}:{key}: {r}"); + click(h, &format!("dropdown:{id}")); + click(h, &format!("{id}:{key}")); h.step(); } +/// Whether a widget whose id starts with `prefix` was drawn in the last frame. fn has_widget(h: &Headless, prefix: &str) -> bool { h.app.widgets.iter().any(|(id, _)| id.starts_with(prefix)) } +/// Where the widget `id` was drawn in the last frame. +fn rect(h: &Headless, id: &str) -> egui::Rect { + h.app.widgets.iter().find(|(w, _)| w == id).map(|(_, r)| *r).unwrap_or_else(|| panic!("no widget {id}")) +} + +/// Whether `text` was painted inside the widget `id` in the last frame. +fn shows(h: &Headless, id: &str, text: &str) -> bool { + let r = rect(h, id); + h.view.painted_shapes().iter().any(|s| matches!(s, egui::Shape::Text(t) if t.galley.job.text == text && r.contains(t.pos))) +} + +/// Overlay and Upright are dropdowns that set the overlay, the Upright mode and (for Guided) the +/// guide tool, as their buttons did. #[test] fn crop_overlay_and_upright_are_dropdowns() { let mut h = detail("panel.crop"); @@ -56,14 +76,89 @@ fn crop_overlay_and_upright_are_dropdowns() { assert!(h.app.ui.tool.is_empty()); } +/// The overlay names are translated in the open menu and in the closed dropdown. #[test] -fn remove_mode_is_a_dropdown() { +fn crop_overlay_dropdown_is_translated() { + let mut h = detail("panel.crop"); + let r = h.request("ui.set", json!({"language": Locale::De.code()}), T); + assert_eq!(r["ok"], true, "{r}"); + h.step(); + let golden = Locale::De.tr("Golden Ratio"); + assert_ne!(golden, "Golden Ratio", "German has its own name for the overlay"); + click(&mut h, "dropdown:cropOverlay"); + h.step(); + assert!(shows(&h, "cropOverlay:golden", golden), "menu item {golden}"); + click(&mut h, "cropOverlay:golden"); + h.step(); + assert_eq!(h.app.ui.crop_overlay, CropOverlay::Golden); + assert!(!has_widget(&h, "cropOverlay:golden"), "the menu closed"); + assert!(shows(&h, "dropdown:cropOverlay", golden), "closed dropdown says {golden}"); + crate::i18n::set_language(Locale::En); +} + +/// The dropdown keeps one width whichever item is chosen, so the row does not jump. +#[test] +fn a_mode_dropdown_keeps_its_width() { + let mut h = detail("panel.crop"); + let first = rect(&h, "dropdown:upright").width(); + for key in ["vertical", "auto", "full"] { + choose(&mut h, "upright", key); + assert_eq!(rect(&h, "dropdown:upright").width(), first, "{key}"); + } + let first = rect(&h, "dropdown:cropAspect").width(); + for aspect in ["1x1", "4x5", "free", "16x10"] { + let r = h.request("engine.execute", json!({"command": "crop.aspect", "params": {"aspect": aspect}}), T); + assert_eq!(r["ok"], true, "{r}"); + h.step(); + assert_eq!(rect(&h, "dropdown:cropAspect").width(), first, "aspect {aspect}"); + } + let mut h = detail("panel.remove"); + let first = rect(&h, "dropdown:removeMode").width(); + for key in ["none", "fill", "heal"] { + choose(&mut h, "removeMode", key); + assert_eq!(rect(&h, "dropdown:removeMode").width(), first, "{key}"); + } + let first = rect(&h, "dropdown:toolOverlay").width(); + for mode in ["always", "selected", "never", "auto"] { + let r = h.request("engine.execute", json!({"command": "view.toolOverlay", "params": {"mode": mode}}), T); + assert_eq!(r["ok"], true, "{r}"); + h.step(); + assert_eq!(rect(&h, "dropdown:toolOverlay").width(), first, "tool overlay {mode}"); + } +} + +/// The number of spots on the active photo. +fn spots(h: &Headless) -> usize { + let id = h.app.session.active().expect("active photo"); + h.app.session.develop_of(id).unwrap_or_default().spots.len() +} + +/// A click on the photo at (`x`, `y`), normalized image coordinates. +fn click_photo(h: &mut Headless, x: f64, y: f64) { + let r = h.request("ui.pointer", json!({"events": [{"kind": "down", "x": x, "y": y}, {"kind": "up", "x": x, "y": y}]}), T); + assert_eq!(r["ok"], true, "{r}"); + h.step(); +} + +/// The Remove mode is a dropdown. None says no brush is chosen, and then a click on the photo adds +/// no spot; the panel opens with Remove, which does. +#[test] +fn remove_mode_is_a_dropdown_with_none() { let mut h = detail("panel.remove"); assert_eq!(h.app.ui.right, RightPanel::Remove); assert!(has_widget(&h, "dropdown:removeMode")); assert!(!has_widget(&h, "button:removeMode-"), "no row of mode buttons"); - for (key, tool) in [("heal", "heal"), ("fill", "fill"), ("clone", "clone"), ("remove", "remove")] { + assert_eq!(h.app.ui.tool, "remove", "the panel opens ready to paint"); + assert!(shows(&h, "dropdown:removeMode", "Remove")); + for (key, tool) in [("heal", "heal"), ("fill", "fill"), ("clone", "clone"), ("none", ""), ("remove", "remove")] { choose(&mut h, "removeMode", key); - assert_eq!(h.app.ui.tool, tool); + assert_eq!(h.app.ui.tool, tool, "{key}"); } + click_photo(&mut h, 0.3, 0.3); + assert_eq!(spots(&h), 1, "Remove paints a spot"); + choose(&mut h, "removeMode", "none"); + assert!(h.app.ui.tool.is_empty()); + assert!(shows(&h, "dropdown:removeMode", "None"), "the dropdown says no brush is chosen"); + click_photo(&mut h, 0.7, 0.7); + assert_eq!(spots(&h), 1, "None paints nothing"); } diff --git a/crates/ui-egui/src/widgets.rs b/crates/ui-egui/src/widgets.rs index c2d4fdd8dc..17fe6894fb 100644 --- a/crates/ui-egui/src/widgets.rs +++ b/crates/ui-egui/src/widgets.rs @@ -1051,10 +1051,17 @@ pub fn dropdown(ui: &mut Ui, id: &str, text: &str, font: egui::FontId, color: Co /// [`dropdown`] at most `max` wide, chevron included: a longer `text` is cut short with …, and /// shown in full on hover. pub fn dropdown_within(ui: &mut Ui, id: &str, text: &str, font: egui::FontId, color: Color32, max: f32) -> Response { + dropdown_sized(ui, id, text, font, color, 0.0, max) +} + +/// [`dropdown_within`] at least `min` wide for its text, so a dropdown sized for its longest +/// choice keeps one width whichever is chosen. +fn dropdown_sized(ui: &mut Ui, id: &str, text: &str, font: egui::FontId, color: Color32, min: f32, max: f32) -> Response { let t = Tokens::get(ui.ctx()); let galley = one_line(ui.painter(), text, font, color, max - 20.0); let elided = galley.elided; - let size = vec2(galley.size().x + 20.0, galley.size().y.max(20.0)); + let text_w = galley.size().x.max(min).min((max - 20.0).max(0.0)); + let size = vec2(text_w + 20.0, galley.size().y.max(20.0)); let (r, resp) = ui.allocate_exact_size(size, Sense::click()); resp.widget_info(|| egui::WidgetInfo::labeled(egui::Role::ComboBox, true, text)); register(ui, format!("dropdown:{id}"), r); @@ -1064,18 +1071,37 @@ pub fn dropdown_within(ui: &mut Ui, id: &str, text: &str, font: egui::FontId, co if elided { resp.on_hover_text(text) } else { resp } } +/// A [`dropdown`] showing `text`, as wide as the longest of `choices` (the labels its menu offers, +/// as shown), so choosing another does not change its width or move what follows it. A `text` +/// longer than every choice widens it, and nothing is ever wider than the space left in the row. +pub fn dropdown_fitting(ui: &mut Ui, id: &str, text: &str, choices: &[&str], font: egui::FontId, color: Color32) -> Response { + let widest = choices.iter().map(|c| one_line(ui.painter(), c, font.clone(), color, f32::INFINITY).size().x).fold(0.0, f32::max); + dropdown_sized(ui, id, text, font, color, widest, ui.available_width()) +} + /// A row of `label` and a [`dropdown`] beside it, as White Balance has, choosing one of `items` /// (menu label, key): the panel way to pick one of several modes, where a row of buttons for /// each would take more room than the choice is worth. The closed dropdown names the `active` -/// item, or the first when none is. The dropdown registers as `dropdown:{id}` and each menu item -/// as `{id}:{key}`. Returns the index of the item chosen this frame. +/// item; an `active` that names none (out of range, or `None`) means the first item, in the +/// closed dropdown and the menu's check alike, so callers pass `None` only where the first item is +/// what happens with nothing chosen. It keeps the width of its longest item, so choosing another +/// does not move what follows it. Screen readers hear `label` as its name and the chosen item as +/// its value. The dropdown registers as `dropdown:{id}` and each menu item as `{id}:{key}`. +/// Returns the index of the item chosen this frame. pub fn labeled_dropdown(ui: &mut Ui, id: &str, label: &str, items: &[(&str, &str)], active: Option) -> Option { let t = Tokens::get(ui.ctx()); + let active = active.filter(|&i| i < items.len()).or((!items.is_empty()).then_some(0)); let mut chosen = None; ui.horizontal(|ui| { - ui.label(egui::RichText::new(crate::i18n::tr(label)).color(t.text_dim)); - let current = active.and_then(|i| items.get(i)).or(items.first()).map_or("", |(l, _)| *l); - let r = dropdown(ui, id, crate::i18n::tr(current), t.font(12.5), t.text_label); + let name = crate::i18n::tr(label); + ui.label(egui::RichText::new(name).color(t.text_dim)); + let current = active.and_then(|i| items.get(i)).map_or("", |(l, _)| crate::i18n::tr(l)); + let choices: Vec<&str> = items.iter().map(|(l, _)| crate::i18n::tr(l)).collect(); + let r = dropdown_fitting(ui, id, current, &choices, t.font(12.5), t.text_label); + ui.ctx().accesskit_node_builder(r.id, |node| { + node.set_label(name.to_string()); + node.set_value(current.to_string()); + }); egui::Popup::menu(&r).show(|ui| { for (i, (l, key)) in items.iter().enumerate() { let item = ui.selectable_label(active == Some(i), crate::i18n::tr(l));