From 746e1c633d9796d68bcb27b7e7f7dc66ea917ce1 Mon Sep 17 00:00:00 2001 From: Colin Faulkingham Date: Thu, 29 Oct 2026 02:16:56 +0000 Subject: [PATCH] Dialogs: wrap status and refusal messages at full width (#8455) (#8460) * claim: #8455 * fix(ui): wrap dialog status and refusal messages (#8455) --- crates/ui-egui/src/dialogs_assembly.rs | 16 ++---- crates/ui-egui/src/dialogs_cam.rs | 4 +- crates/ui-egui/src/dialogs_coupling.rs | 8 ++- crates/ui-egui/src/dialogs_coupling_tests.rs | 49 +++++++++++++++++ crates/ui-egui/src/dialogs_dense.rs | 9 ++-- crates/ui-egui/src/dialogs_trace.rs | 19 +++---- crates/ui-egui/src/i18n.rs | 4 +- crates/ui-egui/src/ui_kit/gallery.rs | 9 ++++ crates/ui-egui/src/ui_kit/layout.rs | 22 ++++++++ crates/ui-egui/src/ui_kit/layout_tests.rs | 56 ++++++++++++++++++++ docs/ui-kit.md | 1 + 11 files changed, 160 insertions(+), 37 deletions(-) diff --git a/crates/ui-egui/src/dialogs_assembly.rs b/crates/ui-egui/src/dialogs_assembly.rs index 0a8f39886..91d9df3cf 100644 --- a/crates/ui-egui/src/dialogs_assembly.rs +++ b/crates/ui-egui/src/dialogs_assembly.rs @@ -559,7 +559,7 @@ pub fn rows(app: &mut SolveApp, ui: &mut egui::Ui, k: &mut Asm, inputs: &mut [Se // Picks that make no joint say why, under them, and offer the way out (#1239). if let Some((why, bodies)) = problem { ui.vertical(|ui| { - ui.add(egui::Label::new(RichText::new(why).color(t.warning)).wrap()); + crate::ui_kit::status_line(ui, &why, crate::ui_kit::StatusTone::Warning); if !bodies.is_empty() && crate::ui_kit::button(ui, crate::i18n::tr("cmd.component_from_bodies"), crate::ui_kit::Button::Normal) .on_hover_text(crate::i18n::tr("joint.from_bodies_hint")) @@ -634,7 +634,7 @@ pub fn rows(app: &mut SolveApp, ui: &mut egui::Ui, k: &mut Asm, inputs: &mut [Se let mut edit = false; ui.vertical(|ui| { let why = crate::i18n::format("drive.rigid", &[("joint", name.as_str())]); - ui.add(egui::Label::new(RichText::new(why).color(t.warning)).wrap()); + crate::ui_kit::status_line(ui, &why, crate::ui_kit::StatusTone::Warning); let r = crate::ui_kit::button(ui, crate::i18n::tr_label("Edit Joint"), crate::ui_kit::Button::Normal); crate::scenario::publish_handle("drive:edit_joint", r.rect.center()); edit = r.clicked(); @@ -742,9 +742,7 @@ pub fn rows(app: &mut SolveApp, ui: &mut egui::Ui, k: &mut Asm, inputs: &mut [Se let s = &app.session; let joints = &s.doc.assembly.joints; if joints.len() < 2 { - form_row(ui, "", |ui| { - ui.label(RichText::new(crate::i18n::tr_label("needs two joints")).color(t.text_dim)); - }); + crate::ui_kit::status_line(ui, "needs two joints", crate::ui_kit::StatusTone::Dim); return false; } let labels: Vec<&str> = joints.iter().map(|j| j.name.as_str()).collect(); @@ -831,9 +829,7 @@ fn interference(app: &mut SolveApp, picked: &[Sel], clearance: &str) -> (Value, fn interference_rows(app: &SolveApp, ui: &mut egui::Ui, r: &Value) { let t = Tokens::get(); if let Some(e) = r.get("error").and_then(Value::as_str) { - form_row(ui, "", |ui| { - ui.label(RichText::new(e).color(t.error)); - }); + crate::ui_kit::status_line(ui, e, crate::ui_kit::StatusTone::Error); return; } let hits = r["overlaps"].as_array().cloned().unwrap_or_default(); @@ -844,9 +840,7 @@ fn interference_rows(app: &SolveApp, ui: &mut egui::Ui, r: &Value) { let near = r["near"].as_array().cloned().unwrap_or_default(); let pair = |h: &Value| format!("{} ยท {}", h["a"].as_str().unwrap_or(""), h["b"].as_str().unwrap_or("")); if hits.is_empty() && unchecked.is_empty() { - form_row(ui, "Result", |ui| { - ui.label(RichText::new(crate::i18n::tr_label("no interference")).color(t.text_dim)); - }); + crate::ui_kit::status_line(ui, "no interference", crate::ui_kit::StatusTone::Dim); } // Pairs closer than the clearance, nearest first. if !near.is_empty() { diff --git a/crates/ui-egui/src/dialogs_cam.rs b/crates/ui-egui/src/dialogs_cam.rs index afbd4189e..64751dbde 100644 --- a/crates/ui-egui/src/dialogs_cam.rs +++ b/crates/ui-egui/src/dialogs_cam.rs @@ -94,9 +94,7 @@ pub fn rows(app: &mut SolveApp, ui: &mut egui::Ui, f: &mut CamForm, inputs: &[Se let s = &app.session; let (cams, followers) = (joints_of(s, &[JointKind::Revolute]), joints_of(s, &[JointKind::Slider, JointKind::Revolute])); if cams.is_empty() || followers.len() < 2 && followers.iter().all(|j| cams.iter().any(|c| c.0 == j.0)) { - form_row(ui, "", |ui| { - ui.label(RichText::new(crate::i18n::tr_label("needs a joint that turns and one that slides or swings")).color(t.text_dim)); - }); + crate::ui_kit::status_line(ui, "needs a joint that turns and one that slides or swings", crate::ui_kit::StatusTone::Dim); return false; } // A profile picked on a part chooses the joint that part turns on, once. diff --git a/crates/ui-egui/src/dialogs_coupling.rs b/crates/ui-egui/src/dialogs_coupling.rs index 21872308a..0f3208222 100644 --- a/crates/ui-egui/src/dialogs_coupling.rs +++ b/crates/ui-egui/src/dialogs_coupling.rs @@ -4,7 +4,7 @@ //! face also picks the joint it turns on. Play turns the driving joint so the coupling shows its //! motion before OK. -use crate::ui_kit::{choice_row, expr_row}; +use crate::ui_kit::{StatusTone, choice_row, expr_row, status_line}; use egui::RichText; use serde_json::{Value, json}; use solidcraft_engine::Sel; @@ -133,10 +133,8 @@ pub fn rows(app: &mut SolveApp, ui: &mut egui::Ui, f: &mut CouplingForm, inputs: let ([role_a, role_b], turn_b) = roles(f.command); let (la, lb) = (joints_that(s, true), joints_that(s, turn_b)); if la.is_empty() || lb.is_empty() { - form_row(ui, "", |ui| { - let need = if turn_b { "needs two joints that turn" } else { "needs a joint that turns and one that slides" }; - ui.label(RichText::new(crate::i18n::tr_label(need)).color(t.text_dim)); - }); + let need = if turn_b { "needs two joints that turn" } else { "needs a joint that turns and one that slides" }; + status_line(ui, need, StatusTone::Dim); return false; } // A face picked on a part chooses the joint that part turns on, once. diff --git a/crates/ui-egui/src/dialogs_coupling_tests.rs b/crates/ui-egui/src/dialogs_coupling_tests.rs index 888c3ccd5..30c808b3e 100644 --- a/crates/ui-egui/src/dialogs_coupling_tests.rs +++ b/crates/ui-egui/src/dialogs_coupling_tests.rs @@ -6,6 +6,55 @@ use super::*; use crate::Services; use crate::dialogs::{Kind, apply_commands, preview_commands}; +#[test] +fn missing_joint_messages_are_drawn_in_full() { + for language in ["en", "de", "vi"] { + let _language = crate::i18n::scope(language); + for (command, message) in [ + ("joint.screw", "needs a joint that turns and one that slides"), + ("joint.gear", "needs two joints that turn"), + ("joint.rack_pinion", "needs a joint that turns and one that slides"), + ] { + let mut app = SolveApp::new(Session::default(), Services::default()); + app.preferences.language = language.to_owned(); + app.start(command); + let ctx = crate::fonts::test_context(); + let mut textures = std::collections::HashMap::new(); + let mut draw = |app: &mut SolveApp| { + let input = + egui::RawInput { screen_rect: Some(egui::Rect::from_min_size(egui::Pos2::ZERO, egui::vec2(800.0, 600.0))), ..Default::default() }; + let mut output = ctx.run_ui(input, |ui| crate::dialogs::show(app, ui.ctx())); + crate::ui_kit::test_textures_update(&mut textures, &output.textures_delta); + output.textures_delta.clear(); + output + }; + for _ in 0..3 { + draw(&mut app); + } + let output = draw(&mut app); + if let Ok(dir) = std::env::var("SOLIDCRAFT_DIALOG_STATUS_EVIDENCE") { + std::fs::create_dir_all(&dir).unwrap(); + crate::ui_kit::test_raster( + &ctx, + output.shapes.clone(), + output.pixels_per_point, + app.viewport.dialog_rect.expect("dialog bounds").expand(4.0), + &textures, + ) + .save(std::path::Path::new(&dir).join(format!("{command}-{language}.png"))) + .unwrap(); + } + let message = crate::i18n::tr_label(message); + let text = crate::test_support::text_shapes(&output).find(|text| text.galley.text() == message).expect("missing-joint message"); + assert!(!text.galley.elided, "{command} ({language}) cuts off: {message}"); + let bounds = text.galley.rect.translate(text.pos.to_vec2()); + let clip = + output.shapes.iter().find(|shape| matches!(&shape.shape, egui::Shape::Text(t) if t.galley.text() == message)).unwrap().clip_rect; + assert!(clip.contains_rect(bounds), "{command} ({language}) clips the message: {bounds:?} outside {clip:?}"); + } + } +} + /// A grounded base with discs G1 (radius 10 at x = 0) and G2 (radius 20 at x = 30) turning on /// revolute joints about +Z, and a nut on a cylindrical joint. fn discs() -> SolveApp { diff --git a/crates/ui-egui/src/dialogs_dense.rs b/crates/ui-egui/src/dialogs_dense.rs index d77d28ef5..ede0ecca9 100644 --- a/crates/ui-egui/src/dialogs_dense.rs +++ b/crates/ui-egui/src/dialogs_dense.rs @@ -742,11 +742,10 @@ fn additional_rows(app: &mut SolveApp, d: &mut Dialog, ui: &mut egui::Ui) -> boo ui_kit::options(ui, "feature_options", &mut [("Flip", flip)]); } Kind::BoundaryFill { operation, cells, .. } => { - form_row(ui, "Cells", |ui| { - if cells.is_empty() { - ui.label(RichText::new(tr("Pick the tool bodies")).color(Tokens::get().text_dim)); - } - }); + form_row(ui, "Cells", |_| {}); + if cells.is_empty() { + ui_kit::status_line(ui, "Pick the tool bodies", ui_kit::StatusTone::Dim); + } for (i, (_, label, on)) in cells.iter_mut().enumerate() { form_row(ui, &format!("Cell {}", i + 1), |ui| { ui_kit::checkbox(ui, label.as_str(), on); diff --git a/crates/ui-egui/src/dialogs_trace.rs b/crates/ui-egui/src/dialogs_trace.rs index 9656fce7c..2ab4f9683 100644 --- a/crates/ui-egui/src/dialogs_trace.rs +++ b/crates/ui-egui/src/dialogs_trace.rs @@ -3,7 +3,7 @@ //! and drawn in the viewport; OK keeps it as a 3D sketch spline (`motion.trace.keep`). use crate::ui_kit::{choice_row, expr_row, text_row}; -use egui::{Pos2, RichText, Stroke}; +use egui::{Pos2, Stroke}; use serde_json::{Value, json}; use solidcraft_engine::doc::{IDENTITY, Mat, apply_point, mat_inverse, mat_mul}; use solidcraft_engine::geom::Vec3; @@ -135,7 +135,6 @@ pub(crate) fn stop_text(v: &Value) -> Option { /// The dialog's rows; true when Enter was pressed in a value. pub fn rows(app: &mut crate::SolveApp, ui: &mut egui::Ui, f: &mut TraceForm, inputs: &[SelInput]) -> bool { - let t = Tokens::get(); let s = &app.session; let mut enter = false; let studies: Vec<&str> = s.doc.assembly.studies.iter().map(|m| m.name.as_str()).collect(); @@ -152,9 +151,7 @@ pub fn rows(app: &mut crate::SolveApp, ui: &mut egui::Ui, f: &mut TraceForm, inp } else { let list = movable(s); if list.is_empty() { - form_row(ui, "", |ui| { - ui.label(RichText::new(crate::i18n::tr_label("no joint that moves yet")).color(t.text_dim)); - }); + crate::ui_kit::status_line(ui, "no joint that moves yet", crate::ui_kit::StatusTone::Dim); return false; } let labels: Vec<&str> = list.iter().filter_map(|i| s.doc.assembly.joints.get(*i)).map(|j| j.name.as_str()).collect(); @@ -191,16 +188,14 @@ pub fn rows(app: &mut crate::SolveApp, ui: &mut egui::Ui, f: &mut TraceForm, inp f.view = placement(s, inputs).and_then(|(_, _, shown, placed)| Some((shown, mat_inverse(&placed)?))); } if let Some((_, r)) = &f.shown { - let (text, colour) = match r { + let (text, tone) = match r { Ok(v) => match stop_text(v) { - Some(why) => (why, t.warning), - None => (crate::i18n::format("trace.points", &[("count", &v["samples"].to_string())]), t.text_dim), + Some(why) => (why, crate::ui_kit::StatusTone::Warning), + None => (crate::i18n::format("trace.points", &[("count", &v["samples"].to_string())]), crate::ui_kit::StatusTone::Dim), }, - Err(e) => (crate::i18n::tr_label(e).to_string(), t.text_dim), + Err(e) => (crate::i18n::tr_label(e).to_string(), crate::ui_kit::StatusTone::Dim), }; - form_row(ui, "", |ui| { - ui.add(egui::Label::new(RichText::new(text).color(colour)).wrap()); - }); + crate::ui_kit::status_line(ui, &text, tone); } enter } diff --git a/crates/ui-egui/src/i18n.rs b/crates/ui-egui/src/i18n.rs index ef410152f..af867f5ac 100644 --- a/crates/ui-egui/src/i18n.rs +++ b/crates/ui-egui/src/i18n.rs @@ -1262,6 +1262,7 @@ mod tests { const KIT_CALLS: &[KitCall] = &[ // layout.rs: `sized_row` draws `tr_label(text)`, and every row builder reaches it. KitCall { name: "row", args: &[1], id: false, trim: &[1] }, + KitCall { name: "status_line", args: &[1], id: false, trim: &[] }, KitCall { name: "expr_row", args: &[1], id: false, trim: &[1] }, KitCall { name: "hint_row", args: &[1], id: false, trim: &[1] }, KitCall { name: "text_row", args: &[1], id: false, trim: &[1] }, @@ -1946,7 +1947,7 @@ mod tests { /// The names the hand-written kit probes import, exactly as a UI file would. const KIT_IMPORTS: &str = concat!( - "use crate::ui_kit::{row, expr_row, hint_row, text_row, choice_row, section, section_count, ", + "use crate::ui_kit::{row, status_line, expr_row, hint_row, text_row, choice_row, section, section_count, ", "multiline_text_field, dropdown, cell_editor, bounded_cell_editor, search_field, placement_field, ", "library_cell, icon_button_at, icon_button_colored, footer, button, primary_button, ", "wide_primary_button, action_menu, compact_button, icon_label_button, command_menu_button, ", @@ -1970,6 +1971,7 @@ mod tests { fn every_kit_call_is_read_at_its_real_signature() { let cases: &[(&str, &[&str])] = &[ ("row(ui, \"row\", |ui| {})", &["row"]), + ("status_line(ui, \"status_line\", StatusTone::Dim)", &["status_line"]), ("expr_row(ui, \"expr_row\", value)", &["expr_row"]), ("hint_row(ui, \"hint_row\", value, hint)", &["hint_row"]), ("text_row(ui, \"text_row\", value, hint)", &["text_row"]), diff --git a/crates/ui-egui/src/ui_kit/gallery.rs b/crates/ui-egui/src/ui_kit/gallery.rs index 353561119..fda35fd88 100644 --- a/crates/ui-egui/src/ui_kit/gallery.rs +++ b/crates/ui-egui/src/ui_kit/gallery.rs @@ -1402,6 +1402,15 @@ const ELEMENTS: &[Element] = &[ element!("caption", "dialog:caption:Gallery", None, |ui, _| { caption(ui, "Gallery", "Construction"); }), + element!("status_line", "dialog:status:needs a joint that turns and one that slides", None, |ui, _| { + status_line(ui, "needs a joint that turns and one that slides", StatusTone::Dim); + }), + element!("status_line_warning", |ui, _| { + status_line(ui, "Pick two different components before creating a joint.", StatusTone::Warning); + }), + element!("status_line_error", |ui, _| { + status_line(ui, "The interference check could not finish. Check the selected bodies.", StatusTone::Error); + }), element!("figure_rows", "dialog:figure:gallery", Some("dialog:Gallery"), |ui, s| { let Sample { value, second, .. } = s; figure_rows(ui, "gallery", &mut [("Gallery", value), ("Second", second)], |painter, rect, slots| { diff --git a/crates/ui-egui/src/ui_kit/layout.rs b/crates/ui-egui/src/ui_kit/layout.rs index ebdd89cfa..6401d8d23 100644 --- a/crates/ui-egui/src/ui_kit/layout.rs +++ b/crates/ui-egui/src/ui_kit/layout.rs @@ -171,6 +171,28 @@ pub(crate) fn row(ui: &mut Ui, text: &str, control: impl FnOnce(&mut Ui) -> R sized_row(ui, text, row_height(), control) } +/// Semantic ink for a form's explanatory message. +#[derive(Clone, Copy)] +pub(crate) enum StatusTone { + Dim, + Warning, + Error, +} + +/// Full-width, wrapping status or refusal text. Place directly in the form, outside a `row`: +/// rows clip their controls to one line. Tokens: `font_label, text_dim, warning, error`. +pub(crate) fn status_line(ui: &mut Ui, text: &str, tone: StatusTone) -> Response { + let t = Tokens::get(); + let ink = match tone { + StatusTone::Dim => t.text_dim, + StatusTone::Warning => t.warning, + StatusTone::Error => t.error, + }; + let response = ui.add(egui::Label::new(RichText::new(crate::i18n::tr_label(text)).size(t.font_label).color(ink)).wrap()); + scenario::publish_rect(&format!("dialog:status:{text}"), response.rect); + response +} + /// Declarative expression row; retains the field's completion and accepted Enter. Tokens: `row, field`. pub(crate) fn expr_row(ui: &mut Ui, label: &str, value: &mut String) -> bool { row(ui, label, |ui| super::field(ui, value)) diff --git a/crates/ui-egui/src/ui_kit/layout_tests.rs b/crates/ui-egui/src/ui_kit/layout_tests.rs index 8c7af6480..eb0c99948 100644 --- a/crates/ui-egui/src/ui_kit/layout_tests.rs +++ b/crates/ui-egui/src/ui_kit/layout_tests.rs @@ -4,6 +4,62 @@ use crate::ui_kit::*; use crate::{scenario, theme::Tokens}; use egui::Ui; +#[test] +fn status_lines_wrap_at_form_width_and_leave_room_for_the_next_row() { + let previous_dark = crate::theme::is_dark(); + for dark in [false, true] { + crate::theme::set_dark(dark); + let base = Tokens::get(); + for width in [160.0, 280.0] { + for font_label in [base.font_label, base.font_label * 1.8] { + let _theme = crate::theme::scoped_tokens(Tokens { font_label, ..base }); + for (tone, ink) in [(StatusTone::Dim, base.text_dim), (StatusTone::Warning, base.warning), (StatusTone::Error, base.error)] { + let ctx = crate::fonts::test_context(); + let message = "needs a joint that turns and one that slides"; + let mut message_rect = egui::Rect::NOTHING; + let output = crate::test_support::frame( + &ctx, + egui::RawInput { + screen_rect: Some(egui::Rect::from_min_size(egui::Pos2::ZERO, egui::vec2(400.0, 600.0))), + ..Default::default() + }, + |ui| { + ui.set_width(width); + scope(ui, |ui| { + message_rect = status_line(ui, message, tone).rect; + assert_eq!(ui.style().wrap_mode, Some(egui::TextWrapMode::Truncate), "ordinary controls keep their style"); + row(ui, "Radius", |ui| { + ui.label("10 mm"); + }); + }); + }, + ); + let text = crate::test_support::text_shapes(&output).find(|text| text.galley.text() == message).unwrap(); + assert!(!text.galley.elided, "width={width}, font={font_label}"); + if width == 160.0 { + assert!(text.galley.rows.len() > 1, "a narrow form must wrap"); + } + assert!(text.galley.job.sections.iter().all(|section| section.format.color == ink)); + assert!(message_rect.width() <= width + 0.5); + let next = scenario::rect_of("dialog:row:Radius").unwrap(); + assert!((message_rect.left() - next.left()).abs() < 0.5, "use both form columns"); + assert!(next.top() >= message_rect.bottom(), "the next row must follow all lines"); + let painted = text.galley.rect.translate(text.pos.to_vec2()); + assert!(message_rect.expand(0.5).contains_rect(painted), "reserve the painted height"); + let clip = output + .shapes + .iter() + .find(|shape| matches!(&shape.shape, egui::Shape::Text(t) if t.galley.text() == message)) + .unwrap() + .clip_rect; + assert!(clip.contains_rect(painted), "every line is visible"); + } + } + } + } + crate::theme::set_dark(previous_dark); +} + #[test] fn theme_rail_cues_follow_hidden_content() { use crate::ui_kit::preferences::theme_rail_cues; diff --git a/docs/ui-kit.md b/docs/ui-kit.md index c442755d3..a3ce8ece5 100644 --- a/docs/ui-kit.md +++ b/docs/ui-kit.md @@ -135,6 +135,7 @@ table. Panel/Window/Confirm compositions also have dedicated geometry and intera | status: `completion_text` | Compact strip readout with an optional green completion tick and caller-supplied result identity | `font_small`, `control_height`, `form_ink/success/dim`, `space_4/14` | | metrics: `row_height`, `table_row_height`, `header_height` | Shared control/header heights | `control_height`, `space_8`, `form_header_height` | | layout: `scope`, `row` | Form styling and aligned label/control columns | `form_label_width`, `form_gap_y`, `control_*`, `font_label`, `dialog_label`, `space_4/8` | +| `status_line`, `StatusTone` | Full-width, wrapping status or refusal text in dim, warning or error ink. Translates source labels; already translated messages also work. Place directly in the form, outside `row`, so its height grows with the whole message. Rect: `dialog:status:TEXT`. | `font_label`, `text_dim`, `warning`, `error` | | `caption` | Dim caption aligned beneath a control | `font_small`, `text_dim`, label-column tokens | | `figure_rows`, `FigureSlot` | A caller-painted figure in the label column beside a stack of captioned expression fields (the Hole dialog's section); the caller gets each field's rect and hover/focus state to draw leaders and light the part it sizes. Fields keep their `dialog: