Skip to content

PhotoCraft: An open-source, photo editor written by scratch in pure Rust

File menu shows the configured Quick Export format (#1259) (#1269)

Nigel Ihza Firdausya5f9029c2046
diff --git a/crates/ui-egui/src/menus.rs b/crates/ui-egui/src/menus.rs
index c371518e3..0835c45b0 100644
--- a/crates/ui-egui/src/menus.rs
+++ b/crates/ui-egui/src/menus.rs
@@ -614,6 +614,17 @@ fn checked(app: &PhotocraftApp, id: &str) -> Option<bool> {
})
}
+/// Translate the fixed command label and substitute the currently configured export format.
+/// Reuse the existing translated PNG sentence, so dynamic formats work in every UI language.
+fn translated_menu_label(lang: crate::i18n::Lang, item: &MenuItem) -> String {
+ if item.id == "file.export.quickExportAsPng"
+ && let Some(fmt) = item.label.strip_prefix("Quick Export as ")
+ {
+ return crate::i18n::tr_id(lang, &item.id, "Quick Export as PNG").replace("PNG", fmt);
+ }
+ crate::i18n::tr_id(lang, &item.id, &item.label).to_string()
+}
+
/// Menu tree entry for rendering and for `ui.inspect`.
#[derive(Clone, Debug, serde::Serialize)]
pub struct MenuItem {
@@ -701,6 +712,23 @@ pub fn menu_items(app: &PhotocraftApp) -> Vec<MenuItem> {
}
}
crate::plugin_ui::insert_menu_items(app, &mut items);
+ // The File quick-export command uses the format selected in Export Preferences.
+ // On the web it intentionally downloads PNG until the quick-export service supports
+ // the other formats; the Layer quick-export command also always produces PNG.
+ if let Some(item) = items.iter_mut().find(|i| i.id == "file.export.quickExportAsPng") {
+ use photocraft_engine::prefs::QuickExportFormat;
+ let format = if cfg!(target_arch = "wasm32") {
+ "PNG"
+ } else {
+ match app.session.prefs().export.quick_export_format {
+ QuickExportFormat::Png => "PNG",
+ QuickExportFormat::Jpg => "JPG",
+ QuickExportFormat::Gif => "GIF",
+ QuickExportFormat::Webp => "WebP",
+ }
+ };
+ item.label = format!("Quick Export as {format}");
+ }
// File › Open Recent: a dynamic submenu of recently opened files (inserted after "Open As…").
if let Some(after) = items.iter().position(|i| i.id == "file.openAs") {
let rp: Vec<String> = vec!["File".into(), "Open Recent".into()];
@@ -938,7 +966,7 @@ pub fn search_items<'a>(items: &'a [MenuItem], query: &str, lang: crate::i18n::L
let rank = |it: &MenuItem| {
let english = search_rank(&q, &it.label, &it.path);
let path: Vec<String> = it.path.iter().map(|p| crate::i18n::tr(lang, p).to_string()).collect();
- let local = search_rank(&q, crate::i18n::tr_id(lang, &it.id, &it.label), &path);
+ let local = search_rank(&q, &translated_menu_label(lang, it), &path);
english.into_iter().chain(local).min()
};
let mut hits: Vec<(u8, usize, &MenuItem)> =
@@ -978,8 +1006,8 @@ fn help_search(ui: &mut egui::Ui, items: &[MenuItem], clicked: &mut Option<Strin
ui.weak(crate::i18n::tr(lang, "No matching commands"));
}
for it in results {
- let mut trail: Vec<&str> = it.path.iter().map(|p| crate::i18n::tr(lang, p)).collect();
- trail.push(crate::i18n::tr_id(lang, &it.id, &it.label));
+ let mut trail: Vec<String> = it.path.iter().map(|p| crate::i18n::tr(lang, p).to_string()).collect();
+ trail.push(translated_menu_label(lang, it));
let mut b = egui::Button::new(trail.join(" › "));
if let Some(sc) = &it.shortcut {
b = b.shortcut_text(crate::shortcuts::pretty(sc));
@@ -1038,7 +1066,7 @@ fn render_level_rows(ui: &mut egui::Ui, items: &[&MenuItem], depth: usize, click
}
continue;
}
- let mut text = crate::i18n::tr_id(lang, &it.id, &it.label).to_string();
+ let mut text = translated_menu_label(lang, it);
if let Some(c) = it.checked {
text = format!("{} {}", if c { "✔" } else { " " }, text);
}
@@ -1461,3 +1489,26 @@ mod open_recent_tests {
let _ = std::fs::remove_file(&path);
}
}
+
+#[cfg(test)]
+mod quick_export_label_tests {
+ use super::*;
+
+ #[test]
+ fn file_quick_export_menu_reflects_selected_format_without_renaming_layer_export() {
+ let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default());
+ for (value, expected) in [("png", "PNG"), ("jpg", "JPG"), ("gif", "GIF"), ("webp", "WebP")] {
+ app.run("prefs.set", json!({"values": {"export.quickExportFormat": value}})).unwrap();
+ let items = menu_items(&app);
+ let quick: Vec<_> = items.iter().filter(|i| i.id == "file.export.quickExportAsPng").collect();
+ assert_eq!(quick.len(), 1, "the catalogue and UI command must not create duplicate items");
+ let shown_format = if cfg!(target_arch = "wasm32") { "PNG" } else { expected };
+ assert_eq!(quick[0].label, format!("Quick Export as {shown_format}"));
+ let id = crate::i18n::Lang::from_code("id").unwrap();
+ let localized = translated_menu_label(id, quick[0]);
+ assert_eq!(localized, format!("Ekspor Cepat ke {shown_format}"));
+ let layer = items.iter().find(|i| i.id == "layer.quickExportAsPng").unwrap();
+ assert_eq!(layer.label, "Quick Export as PNG", "layer export always creates PNG");
+ }
+ }
+}