diff --git a/crates/ui-egui/src/tools/frame_tool.rs b/crates/ui-egui/src/tools/frame_tool.rs index a3b43bc664..bc6f843322 100644 --- a/crates/ui-egui/src/tools/frame_tool.rs +++ b/crates/ui-egui/src/tools/frame_tool.rs @@ -23,8 +23,9 @@ use craft::draw::ui::{ use craft::keymap::Mods; use craft::ui::canvas::{CanvasTool, ToolAction, ToolClaims, ToolContext, ToolHostResponse, ToolKey, ToolPath, ToolPointer}; use craft::ui::choice::Segmented; -use craft::ui::contract::{Bind, Entry, Show}; +use craft::ui::contract::{Bind, Entry, GlyphLabel, LabelKind, Props as _, Show, ShowContainer as _, SideLabel, Sizing}; use craft::ui::display::Divider; +use craft::ui::form::Field; use craft::ui::primitives::Draw; use craft::ui::text::NumberField; use egui::{Pos2, Stroke, StrokeKind}; @@ -801,11 +802,11 @@ pub fn options_bar(app: &mut PhotocraftApp, ui: &mut egui::Ui, tool: Tool) -> bo } // An icon entry's label is its tooltip and accessible name. let entries: Vec> = [ - ("square", "rectangle", tl!("Create a new rectangular frame")), - ("circle", "ellipse", tl!("Create a new elliptical frame")), - ("triangle", "triangle", tl!("Create a new triangular frame")), - ("pentagon", "polygon", tl!("Create a new polygonal frame")), - ("shapes", "custom", tl!("Create a new custom frame")), + ("square", "rectangle", tl!("Use the mouse to create a new rectangular Frame")), + ("circle", "ellipse", tl!("Use the mouse to create a new elliptical Frame")), + ("triangle", "triangle", tl!("Use the mouse to create a new triangular Frame")), + ("pentagon", "polygon", tl!("Use the mouse to create a new polygonal Frame")), + ("shapes", "custom", tl!("Use the mouse to create a new custom Frame")), ] .into_iter() .map(|(icon, shape, label)| Entry::new(shape.to_string(), label).icon(IconId::new(icon))) @@ -819,7 +820,11 @@ pub fn options_bar(app: &mut PhotocraftApp, ui: &mut egui::Ui, tool: Tool) -> bo } if app.ui.frame_tool.shape == "polygon" { let mut sides = Some(f64::from(app.ui.frame_tool.sides.clamp(3, 100))); - NumberField::new("Sides", Bind::bound(&mut sides)).range(3.0..=100.0).show(ui); + // The bar shows the "#" caption; "Sides" stays the field's accessible name. + let side = SideLabel::new().kind(LabelKind::Glyph(GlyphLabel::text("#"))); + Field::new("Sides").side(side).sizing(Sizing::Content).show(ui, |ui, _| { + NumberField::new("Sides", Bind::bound(&mut sides)).range(3.0..=100.0).show(ui); + }); if let Some(n) = sides { app.ui.frame_tool.sides = (n.round() as u32).clamp(3, 100); } @@ -844,6 +849,8 @@ mod cursor_tests; #[cfg(test)] mod escape_commit_tests; #[cfg(test)] +mod labels_tests; +#[cfg(test)] mod move_history_tests; #[cfg(test)] mod shift_square_tests; diff --git a/crates/ui-egui/src/tools/frame_tool/labels_tests.rs b/crates/ui-egui/src/tools/frame_tool/labels_tests.rs new file mode 100644 index 0000000000..23d55c27e3 --- /dev/null +++ b/crates/ui-egui/src/tools/frame_tool/labels_tests.rs @@ -0,0 +1,97 @@ +//! #13877: the Frame shape buttons use the measured tooltips, and Polygon shows a `#` caption. + +use egui::accesskit::Role; +use egui_kittest::Harness; +use egui_kittest::kittest::Queryable; +use serde_json::json; + +use crate::PhotocraftApp; +use crate::i18n::Lang; +use crate::state::Tool; + +const SHAPES: [(&str, &str); 5] = [ + ("rectangle", "Use the mouse to create a new rectangular Frame"), + ("ellipse", "Use the mouse to create a new elliptical Frame"), + ("triangle", "Use the mouse to create a new triangular Frame"), + ("polygon", "Use the mouse to create a new polygonal Frame"), + ("custom", "Use the mouse to create a new custom Frame"), +]; + +fn bar() -> Harness<'static, Box> { + let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), Default::default()); + app.run("file.new", json!({"width": 80, "height": 60})).unwrap(); + app.ui.tool = Tool::Frame; + let mut h = + Harness::builder().with_size(egui::vec2(900.0, 180.0)).with_context_setup(|ctx| PhotocraftApp::setup_context(ctx, Default::default())).build_ui_state( + |ui, app| { + let _language = crate::i18n::language_scope(Lang::EN); + ui.horizontal(|ui| super::options_bar(app, ui, Tool::Frame)); + }, + app, + ); + h.run_steps(4); + h +} + +fn drawn(h: &Harness<'_, Box>) -> Vec { + fn collect(shape: &egui::Shape, text: &mut Vec) { + match shape { + egui::Shape::Text(shape) => text.push(shape.galley.job.text.clone()), + egui::Shape::Vec(shapes) => shapes.iter().for_each(|shape| collect(shape, text)), + _ => {} + } + } + let mut text = Vec::new(); + for shape in &h.output().shapes { + collect(&shape.shape, &mut text); + } + text +} + +#[test] +fn shape_buttons_show_the_exact_tooltips_in_order_and_still_select_their_shapes() { + let mut h = bar(); + let lefts = SHAPES.map(|(_, tip)| h.get_by_role_and_label(Role::RadioButton, tip).rect().left()); + assert!(lefts.windows(2).all(|w| w[0] < w[1]), "shape buttons remain in reference order"); + for (shape, tip) in SHAPES { + h.get_by_role_and_label(Role::RadioButton, tip).hover(); + h.run_steps(60); + assert!(drawn(&h).iter().any(|text| text == tip), "hover must show {tip:?}: {:?}", drawn(&h)); + h.get_by_role_and_label(Role::RadioButton, tip).click(); + h.run_steps(4); + assert_eq!(h.state().ui.frame_tool.shape, shape); + assert_eq!(h.state().ui.frame_tool.ellipse, shape == "ellipse"); + } +} + +#[test] +fn polygon_shows_the_hash_caption_and_keeps_the_default_and_accessible_name() { + let mut h = bar(); + h.state_mut().ui.frame_tool.shape = "polygon".into(); + h.run_steps(4); + let text = drawn(&h); + // The bar paints "#" as the caption, and the field keeps "Sides" as its accessible name. + assert!(text.iter().any(|text| text == "#"), "polygon caption: {text:?}"); + assert!(!text.iter().any(|text| text.starts_with("Sides")), "old caption: {text:?}"); + assert!(h.query_by_role_and_label(Role::SpinButton, "Sides").is_some(), "accessible name"); + assert!(h.query_by_role_and_label(Role::SpinButton, "#").is_none(), "\"#\" alone names nothing"); + assert_eq!(h.state().ui.frame_tool.sides, 5); + assert!(text.iter().any(|text| text == "5"), "the field displays the default: {text:?}"); +} + +#[test] +fn polygon_sides_field_keeps_the_three_to_one_hundred_range() { + let mut h = bar(); + h.state_mut().ui.frame_tool.shape = "polygon".into(); + h.run_steps(4); + for (typed, expected) in [("1", 3), ("3", 3), ("100", 100), ("101", 100)] { + h.get_by_role_and_label(Role::SpinButton, "Sides").click(); + h.run_steps(2); + h.key_press_modifiers(egui::Modifiers::COMMAND, egui::Key::A); + h.event(egui::Event::Text(typed.into())); + h.run_steps(2); + h.key_press(egui::Key::Enter); + h.run_steps(4); + assert_eq!(h.state().ui.frame_tool.sides, expected, "typed {typed}"); + } +}