Skip to content

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

UI: Cancel in Preferences restores the interface language

aiongg513c28b2eb5f
diff --git a/crates/ui-egui/src/control.rs b/crates/ui-egui/src/control.rs
index f0d34a7ba..62af4df2b 100644
--- a/crates/ui-egui/src/control.rs
+++ b/crates/ui-egui/src/control.rs
@@ -302,8 +302,7 @@ pub fn handle(app: &mut DesignApp, ctx: &egui::Context, req: &ControlRequest) ->
}
"ui.dialog.confirm" => wrap(crate::dialogs::confirm(app)),
"ui.dialog.cancel" => {
- app.cancel_pdf_import();
- app.ui.dialog = None;
+ crate::dialogs::cancel(app);
ok(Value::Null)
}
"ui.resize" => {
diff --git a/crates/ui-egui/src/dialogs.rs b/crates/ui-egui/src/dialogs.rs
index 7d9eb4bb7..c66cfa1fc 100644
--- a/crates/ui-egui/src/dialogs.rs
+++ b/crates/ui-egui/src/dialogs.rs
@@ -1754,14 +1754,25 @@ pub fn show(app: &mut DesignApp, ctx: &egui::Context) {
Some(true) => {
let _ = confirm(app);
}
- Some(false) => {
- app.ui.dialog = None;
- app.cancel_pdf_import();
- }
+ Some(false) => cancel(app),
None => {}
}
}
+/// 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()) };
@@ -3899,6 +3910,20 @@ mod tests {
let labels = dialog_frame(&mut app, &ctx, screen, vec![]);
assert!(labels.iter().any(|(text, _, _)| text == "日本語"));
assert!(labels.iter().any(|(text, _, _)| text == crate::i18n::tr("ja", "Interface Language")));
+
+ // Cancel puts back the language the dialog opened with; OK keeps the choice.
+ let cancel_button = visible_label(&labels, screen, crate::i18n::tr("ja", "Cancel")).center();
+ click_dialog(&mut app, &ctx, screen, cancel_button);
+ assert!(app.ui.dialog.is_none());
+ assert_eq!(app.ui.language, "");
+ app.run("app.preferences", json!({})).unwrap();
+ app.run("app.language", json!({"lang": "it"})).unwrap();
+ confirm(&mut app).unwrap();
+ assert_eq!(app.ui.language, "it");
+ app.run("app.preferences", json!({})).unwrap();
+ app.run("app.language", json!({"lang": "uk"})).unwrap();
+ cancel(&mut app);
+ assert_eq!(app.ui.language, "it");
}
#[test]
diff --git a/crates/ui-egui/src/menus.rs b/crates/ui-egui/src/menus.rs
index 85abacc15..ec1c7bbaf 100644
--- a/crates/ui-egui/src/menus.rs
+++ b/crates/ui-egui/src/menus.rs
@@ -1044,6 +1044,8 @@ pub fn run_ui(app: &mut DesignApp, id: &str, p: &Value) -> Option<Result<Value,
let list = app.session.prefs.autocorrect_list.iter().map(|(a, b)| format!("{a} → {b}")).collect::<Vec<_>>().join("\n");
f["autocorrectText"] = json!(list);
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)
}