Skip to content

DesignCraft: An open-source page and layout design editor written by scratch in pure Rust

Merge prefs-language: UI: Cancel in Preferences restores the interface language

aiongg6c716ca84b41
diff --cc crates/ui-egui/src/dialogs.rs
index dee7f2a5c,c66cfa1fc..0dc637912
--- a/crates/ui-egui/src/dialogs.rs
+++ b/crates/ui-egui/src/dialogs.rs
@@@ -1759,110 -1759,20 +1777,124 @@@ pub fn show(app: &mut DesignApp, ctx: &
}
}
+/// Cell Options keys (`table.getCellStroke` names) and whether each belongs to the stroke.
+const CELL_OPTION_KEYS: [(&str, bool); 8] =
+ [("weight", true), ("type", true), ("color", true), ("tint", true), ("gapColor", true), ("gapTint", true), ("fill", false), ("fillTint", false)];
+
+/// Table › Cell Options › Strokes and Fills… for the target cells. The fields start from the
+/// cells' values (`seed`); OK applies the fields that differ from it.
+pub fn open_cell_options(app: &mut DesignApp) -> Result<Value, String> {
+ let edges = crate::panels::cell_stroke::edges_param(&[true; 6]);
+ let cur = app.session.execute("table.getCellStroke", &json!({"edges": edges})).map_err(|e| e.to_string())?;
+ let mut fields = Map::new();
+ for (k, _) in CELL_OPTION_KEYS {
+ fields.insert(k.into(), cur[k].clone());
+ }
+ fields.insert("seed".into(), Value::Object(fields.clone()));
+ fields.insert("edges".into(), edges);
+ app.ui.dialog = Some(Dialog::new("cellOptions", Value::Object(fields)));
+ Ok(Value::Null)
+}
+
+/// Cell Options › Strokes and Fills: the edge proxy and stroke fields, then the cell fill.
+fn cell_options(app: &mut DesignApp, ui: &mut egui::Ui, d: &mut Dialog) {
+ use crate::panels::cell_stroke;
+ let lang = app.ui.language.clone();
+ crate::rtl::label(ui, egui::RichText::new(crate::i18n::tr(&lang, "Cell Stroke")).font(semibold(12.0)));
+ let mut edges = cell_stroke::edges_from(d.fields.get("edges").unwrap_or(&Value::Null));
+ if cell_stroke::edge_proxy(ui, "cell_options", &mut edges, &lang) {
+ cell_options_edges_changed(app, d, &edges);
+ }
+ ui.add_space(6.0);
+ let values = Value::Object(d.fields.clone());
+ let any = edges.iter().any(|on| *on);
+ if let Some((k, v)) = ui.add_enabled_ui(any, |ui| cell_stroke::stroke_fields(app, ui, "cell_options", &values)).inner {
+ d.fields.insert(k.into(), v);
+ }
+ ui.add_space(10.0);
+ crate::rtl::label(ui, egui::RichText::new(crate::i18n::tr(&lang, "Cell Fill")).font(semibold(12.0)));
+ egui::Grid::new("cell_fill").num_columns(2).spacing([8.0, 6.0]).show(ui, |ui| {
+ crate::rtl::label(ui, crate::i18n::tr(&lang, "Color:"));
+ let mut picked = None;
+ crate::panels::swatch_picker(app, ui, "cell_fill_color", values["fill"].as_str().map(str::to_string), |_, n| picked = Some(n));
+ if let Some(n) = picked {
+ d.fields.insert("fill".into(), json!(n));
+ }
+ ui.end_row();
+ crate::rtl::label(ui, crate::i18n::tr(&lang, "Tint:"));
+ let tint = values["fillTint"].as_f64().map(|t| (t * 100.0).round());
+ if let Some(t) = crate::widgets::NumField::number("cell_fill_tint", tint, "%", 0).width(60.0).range(0.0, 100.0).show(ui) {
+ d.fields.insert("fillTint".into(), json!(t / 100.0));
+ }
+ ui.end_row();
+ });
+}
+
+/// Cell Options with a new edge choice: the stroke fields the user hasn't changed show the
+/// chosen edges' values, and every stroke field's seed becomes those values, so OK applies
+/// exactly the fields that differ from what the chosen edges show.
+fn cell_options_edges_changed(app: &mut DesignApp, d: &mut Dialog, edges: &crate::panels::cell_stroke::Edges) {
+ d.fields.insert("edges".into(), crate::panels::cell_stroke::edges_param(edges));
+ let cur = crate::panels::cell_stroke::current(app, edges);
+ let mut seed = d.fields.get("seed").and_then(Value::as_object).cloned().unwrap_or_default();
+ for (k, stroke) in CELL_OPTION_KEYS {
+ if !stroke {
+ continue;
+ }
+ let v = cur.get(k).cloned().unwrap_or(Value::Null);
+ // Null shows mixed values; the user can't type it.
+ let field = d.fields.get(k).filter(|f| !f.is_null());
+ if field.is_none() || field == seed.get(k) {
+ d.fields.insert(k.into(), v.clone());
+ }
+ seed.insert(k.into(), v);
+ }
+ d.fields.insert("seed".into(), Value::Object(seed));
+}
+
+/// Apply the Cell Options fields that differ from the cells' values, as one `table.setCell`.
+fn confirm_cell_options(app: &mut DesignApp, d: &Dialog) -> Result<Value, String> {
+ let seed = d.fields.get("seed").cloned().unwrap_or(Value::Null);
+ let mut stroke = Map::new();
+ let mut params = Map::new();
+ for (k, is_stroke) in CELL_OPTION_KEYS {
+ let Some(v) = d.fields.get(k).filter(|v| !v.is_null() && seed.get(k) != Some(*v)) else { continue };
+ match (is_stroke, k) {
+ (true, _) => {
+ stroke.insert(k.into(), v.clone());
+ }
+ (false, "fillTint") => {
+ params.insert("tint".into(), v.clone());
+ }
+ _ => {
+ params.insert(k.into(), v.clone());
+ }
+ }
+ }
+ if !stroke.is_empty() {
+ stroke.insert("edges".into(), d.fields.get("edges").cloned().unwrap_or(json!("all")));
+ params.insert("stroke".into(), Value::Object(stroke));
+ }
+ if params.is_empty() {
+ return Ok(Value::Null);
+ }
+ app.run("table.setCell", Value::Object(params))
+}
+
+ /// Close the open dialog without applying it. Preferences also puts back the interface language
+ /// it opened with, since choosing one previews it at once.
+ pub fn cancel(app: &mut DesignApp) {
+ let d = app.ui.dialog.take();
+ app.cancel_pdf_import();
+ if let Some(d) = d
+ && d.id == "preferences"
+ && let Some(lang) = d.fields.get("openedLanguage").and_then(Value::as_str)
+ && lang != app.ui.language
+ {
+ let _ = app.run("app.language", json!({"lang": lang}));
+ }
+ }
+
/// Apply the open dialog.
pub fn confirm(app: &mut DesignApp) -> Result<Value, String> {
let Some(d) = app.ui.dialog.take() else { return Err("no dialog open".into()) };
diff --cc crates/ui-egui/src/menus.rs
index dc071bc8f,ec1c7bbaf..ad97245d5
--- a/crates/ui-egui/src/menus.rs
+++ b/crates/ui-egui/src/menus.rs
@@@ -1140,12 -1043,9 +1127,14 @@@ pub fn run_ui(app: &mut DesignApp, id:
f["storyEditorSize"] = json!(app.ui.story_editor_size);
let list = app.session.prefs.autocorrect_list.iter().map(|(a, b)| format!("{a} → {b}")).collect::<Vec<_>>().join("\n");
f["autocorrectText"] = json!(list);
+ // The effective setting; it becomes an explicit preference only when the user changes it,
+ // so an untouched one keeps following the interface language.
+ let cjk = crate::cjk_features(app);
+ f["cjkFeatures"] = json!(cjk);
+ f["cjkFeatures.initial"] = json!(cjk);
f["section"] = json!("general");
+ // The language previews as it changes; Cancel goes back to this one.
+ f["openedLanguage"] = json!(app.ui.language);
app.ui.dialog = Some(crate::dialogs::Dialog::new("preferences", f));
Ok(Value::Null)
}
@@@ -3127,37 -2702,9 +3126,33 @@@ mod tests
assert_eq!(restored.language, "uk");
assert_eq!(crate::i18n::tr(&restored.language, "File"), "Файл");
assert_eq!(crate::i18n::tr(&restored.language, "A custom name"), "A custom name");
- assert!(menu_tree().iter().flat_map(|(_, entries)| entries.iter()).any(|entry| {
- matches!(entry, Item::Sub(label, children) if label == "Interface Language" && children.iter().any(|child| {
- matches!(child, Item::Cmd { label, id, params, .. } if label == "Українська" && id == "app.language" && params["lang"] == "uk")
- }))
- }));
+ assert!(crate::i18n::LANGUAGES.contains(&("uk", "Українська")), "Preferences lists Ukrainian by its own name");
}
+ #[test]
+ fn russian_language_is_selectable_persisted_and_keeps_document_data() {
+ let mut app = crate::DesignApp::new(designcraft_engine::Session::new(), crate::Services::default());
+ app.run("file.new", json!({"pages": 4, "facingPages": true})).unwrap();
+ app.run("frame.create", json!({"rect": [36, 36, 200, 200], "content": "text", "text": "My words · Мои слова"})).unwrap();
+ let before = serde_json::to_value(&app.session.doc().unwrap().doc).unwrap();
+ run_ui(&mut app, "app.language", &json!({"lang": "ru"})).unwrap().unwrap();
+ assert_eq!(checked(&app, "app.language", &json!({"lang": "ru"})), Some(true));
+ assert_eq!(before, serde_json::to_value(&app.session.doc().unwrap().doc).unwrap());
+ for (title, _) in menu_tree() {
+ assert_ne!(crate::i18n::tr("ru", title), title, "{title}");
+ }
+ let bytes = serde_json::to_vec(&app.ui).unwrap();
+ let restored: crate::UiState = serde_json::from_slice(&bytes).unwrap();
+ assert_eq!(restored.language, "ru");
+ assert_eq!(crate::i18n::tr(&restored.language, "File"), "Файл");
+ assert_eq!(crate::i18n::tr(&restored.language, "A custom name"), "A custom name");
+ assert!(menu_tree().iter().flat_map(|(_, entries)| entries.iter()).any(|entry| {
+ matches!(entry, Item::Sub(label, children) if label == "Interface Language" && children.iter().any(|child| {
+ matches!(child, Item::Cmd { label, id, params, .. } if label == "Русский" && id == "app.language" && params["lang"] == "ru")
+ }))
+ }));
+ }
+
#[test]
fn arabic_dialog_and_language_switch_keep_finite_widget_geometry() {
let mut app = crate::DesignApp::new(designcraft_engine::Session::new(), crate::Services::default());