PhotoCraft: An open-source, photo editor written by scratch in pure Rust
New Document: a custom background colour, from Custom… or the swatch beside the menu (#4789) (#4801)
F4LCON62bf7391ce2a
diff --git a/crates/ui-egui/src/i18n/cs.tsv b/crates/ui-egui/src/i18n/cs.tsvindex cea55a169..0f0c48b8d 100644--- a/crates/ui-egui/src/i18n/cs.tsv+++ b/crates/ui-egui/src/i18n/cs.tsv@@ -885,6 +885,7 @@Background PozadíBackground Color Barva pozadíBackground Contents Obsah pozadí+ Custom background color Vlastní barva pozadíBalance VyváženíBaseline ÚčaříBaseline shift Posun účařídiff --git a/crates/ui-egui/src/i18n/de.tsv b/crates/ui-egui/src/i18n/de.tsvindex b23510e12..bce9bb32d 100644--- a/crates/ui-egui/src/i18n/de.tsv+++ b/crates/ui-egui/src/i18n/de.tsv@@ -865,6 +865,7 @@Background HintergrundBackground Color HintergrundfarbeBackground Contents Hintergrundinhalte+ Custom background color Benutzerdefinierte HintergrundfarbeBalance BalanceBaseline GrundlinieBaseline shift Grundlinienverschiebungdiff --git a/crates/ui-egui/src/i18n/el.tsv b/crates/ui-egui/src/i18n/el.tsvindex 1dd9ba365..7b72bd7eb 100644--- a/crates/ui-egui/src/i18n/el.tsv+++ b/crates/ui-egui/src/i18n/el.tsv@@ -2085,6 +2085,7 @@Avoid Widows/Orphans Αποφυγή μονών γραμμών στην αρχή ή στο τέλος σελίδαςBLANK DOCUMENT PRESETS ({n}) ΠΡΟΕΠΙΛΟΓΕΣ ΚΕΝΟΥ ΕΓΓΡΑΦΟΥ ({n})Background Contents Περιεχόμενα φόντου+ Custom background color Προσαρμοσμένο χρώμα φόντουBackground from Layer Φόντο από επίπεδοBaseline Γραμμή βάσηςBaseline shift Μετατόπιση γραμμής βάσηςdiff --git a/crates/ui-egui/src/i18n/es.tsv b/crates/ui-egui/src/i18n/es.tsvindex 5d1ff7114..8a68c5821 100644--- a/crates/ui-egui/src/i18n/es.tsv+++ b/crates/ui-egui/src/i18n/es.tsv@@ -874,6 +874,7 @@Background FondoBackground Color Color de fondoBackground Contents Contenido del fondo+ Custom background color Color de fondo personalizadoBalance BalanceBaseline Línea de baseBaseline shift Desplazamiento de línea de basediff --git a/crates/ui-egui/src/i18n/fr.tsv b/crates/ui-egui/src/i18n/fr.tsvindex 697461611..3aca15845 100644--- a/crates/ui-egui/src/i18n/fr.tsv+++ b/crates/ui-egui/src/i18n/fr.tsv@@ -860,6 +860,7 @@Background Arrière-planBackground Color Couleur d'arrière-planBackground Contents Contenu de l'arrière-plan+ Custom background color Couleur d'arrière-plan personnaliséeBalance BalanceBaseline Ligne de baseBaseline shift Décalage de la ligne de basediff --git a/crates/ui-egui/src/i18n/hu.tsv b/crates/ui-egui/src/i18n/hu.tsvindex 9aa29a7cb..f60b798d8 100644--- a/crates/ui-egui/src/i18n/hu.tsv+++ b/crates/ui-egui/src/i18n/hu.tsv@@ -2644,6 +2644,7 @@ cameraRaw Yellow SárgaAutosave minutes Automatikus mentés gyakorisága (perc)Background Color HáttérszínBackground Contents Háttér tartalma+ Custom background color Egyéni háttérszínBatch Kötegelt feldolgozásBinary assets added Hozzáadott bináris elemekBinary assets removed Eltávolított bináris elemekdiff --git a/crates/ui-egui/src/i18n/id.tsv b/crates/ui-egui/src/i18n/id.tsvindex 8182765c4..b4289bc4a 100644--- a/crates/ui-egui/src/i18n/id.tsv+++ b/crates/ui-egui/src/i18n/id.tsv@@ -854,6 +854,7 @@Background BackgroundBackground Color Warna BackgroundBackground Contents Isi Background+ Custom background color Warna background kustomBalance KeseimbanganBaseline BaselineBaseline shift Geser baselinediff --git a/crates/ui-egui/src/i18n/it.tsv b/crates/ui-egui/src/i18n/it.tsvindex 08d8adcc1..2bd87d46c 100644--- a/crates/ui-egui/src/i18n/it.tsv+++ b/crates/ui-egui/src/i18n/it.tsv@@ -869,6 +869,7 @@Background SfondoBackground Color Colore sfondoBackground Contents Contenuto sfondo+ Custom background color Colore sfondo personalizzatoBalance BilanciamentoBaseline Linea baseBaseline shift Scostamento linea basediff --git a/crates/ui-egui/src/i18n/ja.tsv b/crates/ui-egui/src/i18n/ja.tsvindex 8e878c2c2..4e45e1413 100644--- a/crates/ui-egui/src/i18n/ja.tsv+++ b/crates/ui-egui/src/i18n/ja.tsv@@ -873,6 +873,7 @@Background 背景Background Color 背景色Background Contents 背景の内容+ Custom background color カスタム背景色Balance バランスBaseline ベースラインBaseline shift ベースラインシフトdiff --git a/crates/ui-egui/src/i18n/ko.tsv b/crates/ui-egui/src/i18n/ko.tsvindex b7fe66cbb..ce1ded936 100644--- a/crates/ui-egui/src/i18n/ko.tsv+++ b/crates/ui-egui/src/i18n/ko.tsv@@ -848,6 +848,7 @@Background 배경Background Color 배경색Background Contents 배경 내용+ Custom background color 사용자 정의 배경색Balance 균형Baseline 기준선Baseline shift 기준선 이동diff --git a/crates/ui-egui/src/i18n/nl.tsv b/crates/ui-egui/src/i18n/nl.tsvindex 9ffc6e89b..a32df3974 100644--- a/crates/ui-egui/src/i18n/nl.tsv+++ b/crates/ui-egui/src/i18n/nl.tsv@@ -852,6 +852,7 @@Background AchtergrondBackground Color AchtergrondkleurBackground Contents Achtergrondinhoud+ Custom background color Aangepaste achtergrondkleurBalance BalansBaseline BasislijnBaseline shift Basislijnverschuivingdiff --git a/crates/ui-egui/src/i18n/pl.tsv b/crates/ui-egui/src/i18n/pl.tsvindex 4601e9027..2689c3e03 100644--- a/crates/ui-egui/src/i18n/pl.tsv+++ b/crates/ui-egui/src/i18n/pl.tsv@@ -871,6 +871,7 @@Background TłoBackground Color Kolor tłaBackground Contents Zawartość tła+ Custom background color Niestandardowy kolor tłaBalance BalansBaseline Linia bazowaBaseline shift Przesunięcie linii bazowejdiff --git a/crates/ui-egui/src/i18n/pt-br.tsv b/crates/ui-egui/src/i18n/pt-br.tsvindex 21ce21746..2e1a7705f 100644--- a/crates/ui-egui/src/i18n/pt-br.tsv+++ b/crates/ui-egui/src/i18n/pt-br.tsv@@ -854,6 +854,7 @@Background Plano de fundoBackground Color Cor de fundoBackground Contents Conteúdo do plano de fundo+ Custom background color Cor de fundo personalizadaBalance EquilíbrioBaseline Linha de baseBaseline shift Deslocamento da linha de basediff --git a/crates/ui-egui/src/i18n/ru.tsv b/crates/ui-egui/src/i18n/ru.tsvindex cc008ef42..3cf585151 100644--- a/crates/ui-egui/src/i18n/ru.tsv+++ b/crates/ui-egui/src/i18n/ru.tsv@@ -1527,6 +1527,7 @@Preset Name: Имя набора:Width and Height Ширина и высотаBackground Contents Содержимое фона+ Custom background color Свой цвет фонаWhite БелыйBackground Color Цвет фонаTransparent Прозрачныйdiff --git a/crates/ui-egui/src/i18n/sr.tsv b/crates/ui-egui/src/i18n/sr.tsvindex 7a85f8d58..72c6b4bee 100644--- a/crates/ui-egui/src/i18n/sr.tsv+++ b/crates/ui-egui/src/i18n/sr.tsv@@ -850,6 +850,7 @@Background PozadinaBackground Color Boja pozadineBackground Contents Sadržaj pozadine+ Custom background color Prilagođena boja pozadineBalance BalansBaseline Osnovna linijaBaseline shift Pomak osnovne linijediff --git a/crates/ui-egui/src/i18n/tr.tsv b/crates/ui-egui/src/i18n/tr.tsvindex 12383b694..23986a8c5 100644--- a/crates/ui-egui/src/i18n/tr.tsv+++ b/crates/ui-egui/src/i18n/tr.tsv@@ -841,6 +841,7 @@Background Arka planBackground Color Arka Plan RengiBackground Contents Arka Plan İçeriği+ Custom background color Özel Arka Plan RengiBalance BakiyeBaseline TemelBaseline shift Taban çizgisi kaymasıdiff --git a/crates/ui-egui/src/i18n/uk.tsv b/crates/ui-egui/src/i18n/uk.tsvindex a9bae9eb2..2187e4d7f 100644--- a/crates/ui-egui/src/i18n/uk.tsv+++ b/crates/ui-egui/src/i18n/uk.tsv@@ -1508,6 +1508,7 @@Preset Name: Назва набору:Width and Height Ширина та висотаBackground Contents Вміст фону+ Custom background color Довільний колір фонуWhite БілийBackground Color Колір фонуTransparent Прозорийdiff --git a/crates/ui-egui/src/i18n/vi.tsv b/crates/ui-egui/src/i18n/vi.tsvindex fc652eff8..e56eaf305 100644--- a/crates/ui-egui/src/i18n/vi.tsv+++ b/crates/ui-egui/src/i18n/vi.tsv@@ -1731,6 +1731,7 @@ cameraRaw Size Kích thướcResolution: Độ phân giải:Color Mode Chế độ màuBackground Contents Nội dung nền+ Custom background color Màu nền tùy chỉnhPixels/Inch Pixel/inchPixels/Centimeter Pixel/centimetWhite Trắngdiff --git a/crates/ui-egui/src/i18n/zh-hans.tsv b/crates/ui-egui/src/i18n/zh-hans.tsvindex fb6083985..14ee54ff2 100644--- a/crates/ui-egui/src/i18n/zh-hans.tsv+++ b/crates/ui-egui/src/i18n/zh-hans.tsv@@ -877,6 +877,7 @@Background 背景Background Color 背景颜色Background Contents 背景内容+ Custom background color 自定背景颜色Balance 平衡Baseline 基线Baseline shift 基线偏移diff --git a/crates/ui-egui/src/i18n/zh-hant.tsv b/crates/ui-egui/src/i18n/zh-hant.tsvindex 760e39081..7f26404e6 100644--- a/crates/ui-egui/src/i18n/zh-hant.tsv+++ b/crates/ui-egui/src/i18n/zh-hant.tsv@@ -880,6 +880,7 @@Background 背景Background Color 背景色Background Contents 背景內容+ Custom background color 自訂背景色Balance 平衡Baseline 基線Baseline shift 基線位移diff --git a/crates/ui-egui/src/new_doc_ui.rs b/crates/ui-egui/src/new_doc_ui.rsindex 0d2790883..c9bf902c6 100644--- a/crates/ui-egui/src/new_doc_ui.rs+++ b/crates/ui-egui/src/new_doc_ui.rs@@ -285,6 +285,31 @@ fn page_icon(ui: &egui::Ui, r: Rect, w: u32, h: u32, t: &Tokens) {ui.painter().rect_stroke(page, 1.0, Stroke::new(1.2, t.text_dim), StrokeKind::Inside);}+/// The Background Contents menu's entry for a colour of the user's own ([`body`]).+const CUSTOM: &str = "custom";+/// Side of the Background Contents colour swatch.+const SWATCH: f32 = 22.0;++/// The colour the new document's background starts with: white, black, the captured or current+/// toolbox background colour, or a custom `"#rrggbb"`; `None` for Transparent.+fn background_rgb(app: &crate::PhotocraftApp, f: &Map<String, Value>) -> Option<[f32; 3]> {+ let bg = get_s(f, "background", "white");+ match bg.as_str() {+ "transparent" => None,+ "black" => Some([0.0; 3]),+ "backgroundColor" => Some(+ f.get("backgroundColor")+ .and_then(Value::as_array)+ .and_then(|a| Some([a.first()?.as_f64()? as f32, a.get(1)?.as_f64()? as f32, a.get(2)?.as_f64()? as f32]))+ .unwrap_or_else(|| {+ let b = app.session.tools.background;+ [b[0], b[1], b[2]]+ }),+ ),+ hex => Some(crate::color_picker_ui::parse_hex(hex).unwrap_or([1.0; 3])),+ }+}+/// Restore a snapshot without replacing the new document's name or its clipboard offer.pub fn apply_saved_preset(f: &mut Map<String, Value>, preset: &photocraft_engine::document_preset_cmds::DocumentPreset) {apply_settings(f, &preset.settings);@@ -681,19 +706,57 @@ pub fn body(app: &mut crate::PhotocraftApp, ui: &mut egui::Ui, f: &mut Map<Strin});ui.add_space(4.0);small_label(ui, tl!("Background Contents"));- let mut bg = get_s(f, "background", "white");+ // A custom colour is kept as its "#rrggbb", which `file.new` takes as is (#4789).+ let current = get_s(f, "background", "white");+ let mut bg = if crate::color_picker_ui::parse_hex(¤t).is_some() { CUSTOM.to_string() } else { current.clone() };let opts = [("white".to_string(), tl!("White")),("black".to_string(), tl!("Black")),("backgroundColor".to_string(), tl!("Background Color")),("transparent".to_string(), tl!("Transparent")),+ (CUSTOM.to_string(), tl!("Custom…")),];- if widgets::dropdown(ui, "nd-bg", &mut bg, &opts, 240.0) {- f.insert("background".into(), json!(bg));- f.remove("__savedPreset");- // A fresh choice of Background Color uses today's toolbox colour. Selecting a- // saved preset instead restores the colour captured when it was saved.+ let shown = background_rgb(app, f);+ let mut pick = false;+ ui.horizontal(|ui| {+ if widgets::dropdown(ui, "nd-bg", &mut bg, &opts, 240.0 - SWATCH - 6.0) {+ if bg == CUSTOM {+ // Custom… opens the Color Picker on the colour shown so far.+ pick = true;+ } else {+ f.insert("background".into(), json!(bg));+ }+ f.remove("__savedPreset");+ // A fresh choice of Background Color uses today's toolbox colour. Selecting a+ // saved preset instead restores the colour captured when it was saved.+ f.remove("backgroundColor");+ }+ // Photoshop's swatch beside the menu: the colour the canvas starts with; a click+ // opens the Color Picker, as Custom… does.+ let (r, resp) = ui.allocate_exact_size(vec2(SWATCH, SWATCH), Sense::click());+ match shown {+ Some(c) => {+ ui.painter().rect_filled(+ r,+ 2.0,+ egui::Color32::from_rgb((c[0] * 255.0).round() as u8, (c[1] * 255.0).round() as u8, (c[2] * 255.0).round() as u8),+ );+ }+ None => widgets::checker(ui.painter(), r, 4.0),+ }+ ui.painter().rect_stroke(r, 2.0, Stroke::new(1.0, Tokens::get(ui.ctx()).field_border), StrokeKind::Inside);+ let resp = resp.on_hover_text(tl!("Custom background color")).on_hover_cursor(egui::CursorIcon::PointingHand);+ resp.widget_info(|| egui::WidgetInfo::labeled(egui::WidgetType::Button, true, tl!("Custom background color")));+ if resp.clicked() {+ pick = true;+ f.remove("__savedPreset");+ }+ });+ if pick {+ let start = shown.unwrap_or([1.0; 3]);+ f.insert("background".into(), json!(crate::color_picker_ui::hex(start)));f.remove("backgroundColor");+ crate::color_picker_ui::request_field(f, "background", &crate::color_picker_ui::title_for(tl!("Background Contents")));}save_preset(app, ui, f);});@@ -977,6 +1040,48 @@ mod tests {h}+ /// #4789: the swatch beside Background Contents (and Custom…) opens the Color Picker; its+ /// colour becomes the new document's background (kept as its "#rrggbb").+ #[test]+ fn a_custom_background_color_is_picked_and_used() {+ let mut h = harness_with_store(&Arc::new(Mutex::new(None)));+ let nd = h.state().ui.dialogs[0].id;+ assert!(h.query_by_label("Custom background color").is_some());+ h.get_by_label("Custom background color").click();+ h.run_steps(3);+ let picker = h+ .state()+ .ui+ .dialogs+ .iter()+ .find(|d| d.fields.get("__dialog").and_then(serde_json::Value::as_u64) == Some(nd))+ .map(|d| d.id)+ .expect("the Color Picker opens over the dialog");+ h.state_mut().ui.dialog_mut(picker).unwrap().fields.insert("color".into(), serde_json::json!("#336699"));+ crate::dialogs::confirm(h.state_mut(), picker).unwrap();+ h.run_steps(3);+ let bg = h.state().ui.dialogs.iter().find(|d| d.id == nd).unwrap().fields["background"].clone();+ assert_eq!(bg, "#336699");+ crate::dialogs::confirm(h.state_mut(), nd).unwrap();+ let doc = &h.state().session.active().unwrap().doc;+ let px = photocraft_compose::flatten(doc).px[0];+ assert!((px[0] * 255.0 - 51.0).abs() < 1.0 && (px[1] * 255.0 - 102.0).abs() < 1.0 && (px[2] * 255.0 - 153.0).abs() < 1.0, "{px:?}");+ }++ /// #4789: the swatch shows what each Background Contents choice paints, Background Color included.+ #[test]+ fn the_swatch_shows_each_background_choice() {+ let mut h = harness_with_store(&Arc::new(Mutex::new(None)));+ h.state_mut().run("tools.setColors", serde_json::json!({"foreground": "#000000", "background": "#336699"})).unwrap();+ let rgb = |bg: &str| super::super::background_rgb(h.state(), serde_json::json!({ "background": bg }).as_object().unwrap());+ assert_eq!(rgb("white"), Some([1.0; 3]));+ assert_eq!(rgb("black"), Some([0.0; 3]));+ assert_eq!(rgb("transparent"), None);+ assert_eq!(rgb("#ff0000"), Some([1.0, 0.0, 0.0]));+ let c = rgb("backgroundColor").expect("Background Color shows the toolbox colour");+ assert!((c[0] * 255.0 - 51.0).abs() < 1.0 && (c[1] * 255.0 - 102.0).abs() < 1.0 && (c[2] * 255.0 - 153.0).abs() < 1.0, "{c:?}");+ }+fn name_preset(h: &mut Harness<'static, PhotocraftApp>, name: &str) {h.get_by_label("Save Preset…").click();h.run_steps(2);