SolidCraft: an open-source parametric 3D CAD application built in pure Rust. Desktop, web, and automation through MCP.
fix(ui): disable bold in Edit Text when font has no bold face (#9940) (#9958)
ashxddaga2e22ff8035b1
diff --git a/assets/locales/pending/sketch-text-no-bold.json b/assets/locales/pending/sketch-text-no-bold.jsonnew file mode 100644index 0000000000..fa3eabdf24--- /dev/null+++ b/assets/locales/pending/sketch-text-no-bold.json@@ -0,0 +1,20 @@+{+ "en": {+ "sketch_text.no_bold": "The built-in font has no bold face: choose an installed font"+ },+ "ar": {+ "sketch_text.no_bold": "الخط المدمج لا يحتوي على نمط غامق: اختر خطاً مثبتاً"+ },+ "he": {+ "sketch_text.no_bold": "לגופן המובנה אין משקל מודגש: בחר גופן מותקן"+ },+ "ja": {+ "sketch_text.no_bold": "組み込みフォントには太字がありません: インストール済みのフォントを選択してください"+ },+ "vi": {+ "sketch_text.no_bold": "Phông chữ tích hợp không có kiểu in đậm: chọn một phông chữ đã cài đặt"+ },+ "zh": {+ "sketch_text.no_bold": "内置字体没有粗体字形:请选择已安装的字体"+ }+}diff --git a/crates/ui-egui/src/sketch_text.rs b/crates/ui-egui/src/sketch_text.rsindex 9089df35e2..42c8174690 100644--- a/crates/ui-egui/src/sketch_text.rs+++ b/crates/ui-egui/src/sketch_text.rs@@ -493,6 +493,13 @@ pub(crate) fn refresh(app: &SolveApp, k: &mut Tx) -> bool {true}+/// Whether the font (or built-in font when empty) has a bold face.+fn font_has_bold_face(name: &str) -> bool {+ let trimmed = name.trim();+ let font = if trimmed.is_empty() { None } else { Some(trimmed) };+ solidcraft_engine::sketch::font_data_styled(font, true, false).is_ok()+}+/// The dialog's rows; true when Enter was pressed in a value field or in the text (Shift+Enter/// starts a new line there). `inputs` and `active`: the dialog's Path input.pub(crate) fn rows(ui: &mut egui::Ui, k: &mut Tx, inputs: &mut [SelInput], active: &mut usize) -> bool {@@ -511,8 +518,12 @@ pub(crate) fn rows(ui: &mut egui::Ui, k: &mut Tx, inputs: &mut [SelInput], activstate.store(ui.ctx(), id);k.focus = false;}+ let font_has_bold = font_has_bold_face(&k.font);+ if !font_has_bold && k.style.bold {+ k.style.bold = false;+ }ui_kit::row(ui, "Font", |ui| font_row(ui, k));- ui_kit::row(ui, "Style", |ui| ui_kit::text_emphasis(ui, &mut k.style.bold, &mut k.style.italic));+ ui_kit::row(ui, "Style", |ui| ui_kit::text_emphasis(ui, &mut k.style.bold, &mut k.style.italic, font_has_bold));ui_kit::row(ui, "Alignment", |ui| {let mut selected = match k.style.align {TextAlign::Left => 0,@@ -692,9 +703,10 @@ fn edit_params(app: &SolveApp, k: &Tx) -> Result<Value, String> {TextAlign::Center => "center",TextAlign::Right => "right",};+ let bold = k.style.bold && font_has_bold_face(&k.font);let mut params = json!({"link": k.link, "at": [sent_x, sent_y], "height": sent_height,- "bold": k.style.bold, "italic": k.style.italic, "align": align,+ "bold": bold, "italic": k.style.italic, "align": align,"letter_spacing": letter_spacing, "line_spacing": line_spacing,"path": k.style.path.clone().unwrap_or_default(), "reverse": k.style.reverse,});@@ -1528,30 +1540,30 @@ mod tests {assert_eq!(tx(&h.app).values[5], "1.25");}+ /// #9940: built-in font has no bold face; Bold toggle is disabled so clicking it+ /// does not set bold or trigger errors, and alignment and other options succeed.#[test]- fn bold_in_a_font_without_a_bold_face_is_refused_with_the_reason() {+ fn bold_in_a_font_without_a_bold_face_is_disabled() {let (mut h, link, _) = setup();let before = (h.app.session.doc.to_json(), h.app.session.undo.len());h.app.start("sketch.edit_text");h.frames(3);+ assert!(!font_has_bold_face(&tx(&h.app).font));+ assert!(!tx(&h.app).style.bold);+ // Clicking disabled bold does not toggle it, preview stays clean without errors.h.step(&json!({"click":{"handle":"dialog:Bold"}})).unwrap();h.frames(2);- assert!(!style_of(&h, &link).bold, "the canvas keeps the last text it could make");- let error = h.app.dialog.as_ref().unwrap().error.clone().unwrap_or_default();- assert!(error.contains("no bold face"), "the preview must explain the unchanged text before OK: {error}");- h.frames(2);- assert_eq!(h.app.dialog.as_ref().unwrap().error.as_deref(), Some(error.as_str()), "the reason stays visible on unchanged frames");- h.step(&json!({"click":{"handle":"dialog:Bold"}})).unwrap();- h.frames(2);- assert!(h.app.dialog.as_ref().unwrap().error.is_none(), "a valid preview clears the refusal");- h.step(&json!({"click":{"handle":"dialog:Bold"}})).unwrap();+ assert!(!tx(&h.app).style.bold);+ assert!(!style_of(&h, &link).bold);+ assert!(h.app.dialog.as_ref().unwrap().error.is_none());+ // Changing alignment succeeds without error.+ h.step(&json!({"click":{"handle":"dialog:Align center"}})).unwrap();h.frames(2);+ assert_eq!(tx(&h.app).style.align, TextAlign::Center);ok(&mut h);- assert!(editing(&h.app));- let error = h.app.dialog.as_ref().unwrap().error.clone().unwrap_or_default();- assert!(error.contains("no bold face"), "{error}");- h.step(&json!({"widget":"Cancel"})).unwrap();- assert_eq!((h.app.session.doc.to_json(), h.app.session.undo.len()), before);+ assert!(!editing(&h.app));+ assert_eq!(style_of(&h, &link).align, TextAlign::Center);+ assert_eq!(h.app.session.undo.len(), before.1 + 1);}#[test]diff --git a/crates/ui-egui/src/ui_kit/choice.rs b/crates/ui-egui/src/ui_kit/choice.rsindex 871eff4dd8..d779bb12da 100644--- a/crates/ui-egui/src/ui_kit/choice.rs+++ b/crates/ui-egui/src/ui_kit/choice.rs@@ -343,14 +343,20 @@ pub(crate) fn checkbox_row(ui: &mut egui::Ui, key: &str, label: &str, on: bool)/// Compact bold/italic toggles for text. Tokens: scope font/control tokens./// The glyphs keep the familiar B/I treatment; accessible names use translated style names.-pub(crate) fn text_emphasis(ui: &mut Ui, bold: &mut bool, italic: &mut bool) {+pub(crate) fn text_emphasis(ui: &mut Ui, bold: &mut bool, italic: &mut bool, bold_enabled: bool) {use crate::fonts::RichTextExt as _;- for (key, text, value, id) in [- ("Bold", egui::RichText::new("B").semibold(), bold, "sketch_text.bold"),- ("Italic", egui::RichText::new("I").italics(), italic, "sketch_text.italic"),+ for (key, text, value, id, enabled) in [+ ("Bold", egui::RichText::new("B").semibold(), bold, "sketch_text.bold", bold_enabled),+ ("Italic", egui::RichText::new("I").italics(), italic, "sketch_text.italic", true),] {- let response = ui.toggle_value(value, text).on_hover_text(crate::i18n::tr(id));- response.widget_info(|| egui::WidgetInfo::selected(egui::WidgetType::Button, ui.is_enabled(), *value, crate::i18n::tr(id)));+ if !enabled {+ *value = false;+ }+ let mut response = ui.add_enabled_ui(enabled, |ui| ui.toggle_value(value, text).on_hover_text(crate::i18n::tr(id))).inner;+ if !enabled {+ response = response.on_disabled_hover_text(crate::i18n::tr("sketch_text.no_bold"));+ }+ response.widget_info(|| egui::WidgetInfo::selected(egui::WidgetType::Button, ui.is_enabled() && enabled, *value, crate::i18n::tr(id)));scenario::publish_target_as(&format!("dialog:{key}"), &format!("dialog:emphasis:{key}"), response.rect);}}diff --git a/crates/ui-egui/src/ui_kit/gallery.rs b/crates/ui-egui/src/ui_kit/gallery.rsindex 749ac2ed72..0c1b7e1d1a 100644--- a/crates/ui-egui/src/ui_kit/gallery.rs+++ b/crates/ui-egui/src/ui_kit/gallery.rs@@ -1289,7 +1289,7 @@ const ELEMENTS: &[Element] = &[}),element!("text_emphasis", "dialog:row:Gallery", Some("dialog:Bold"), |ui, s| {row(ui, "Gallery", |ui| {- text_emphasis(ui, &mut s.checked, &mut false);+ text_emphasis(ui, &mut s.checked, &mut false, true);});}),element!("value_pair_named", "dialog:row:Position", Some("dialog:Distance X"), |ui, s| {@@ -1771,7 +1771,7 @@ fn emphasis_and_compact_modes_keep_independent_actions_in_both_themes() {let mut italic = false;let mut selected = 0;let mut draw = |ui: &mut Ui| {- row(ui, "Style", |ui| text_emphasis(ui, &mut bold, &mut italic));+ row(ui, "Style", |ui| text_emphasis(ui, &mut bold, &mut italic, true));row(ui, "Alignment", |ui| {segmented(ui,