New Document: a custom background colour, from Custom… or the swatch beside the menu (#4789) (#4801)

* New Document: a custom background colour, from Custom… or the swatch beside the menu (#4789)

* New Document: test the swatch's colour for each Background Contents choice (#4789)

Co-authored-by: persi-man <[email protected]>

---------

Co-authored-by: persi-man <[email protected]>
This commit is contained in:
F4LCONandpersi-man authored and GitHub committed 2026-10-13 12:24:16 -04:00
1 parent f4b3766b91
commit 62bf7391ce
21 files changed
+131 -6

No files matched your search

+1
View File
@@ -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ří
Can't render this file because it contains an unexpected character in line 6 and column 47.
+1
View File
@@ -865,6 +865,7 @@
Background Hintergrund
Background Color Hintergrundfarbe
Background Contents Hintergrundinhalte
Custom background color Benutzerdefinierte Hintergrundfarbe
Balance Balance
Baseline Grundlinie
Baseline shift Grundlinienverschiebung
Can't render this file because it contains an unexpected character in line 6 and column 47.
+1
View File
@@ -2085,6 +2085,7 @@
Avoid Widows/Orphans Αποφυγή μονών γραμμών στην αρχή ή στο τέλος σελίδας
BLANK DOCUMENT PRESETS ({n}) ΠΡΟΕΠΙΛΟΓΕΣ ΚΕΝΟΥ ΕΓΓΡΑΦΟΥ ({n})
Background Contents Περιεχόμενα φόντου
Custom background color Προσαρμοσμένο χρώμα φόντου
Background from Layer Φόντο από επίπεδο
Baseline Γραμμή βάσης
Baseline shift Μετατόπιση γραμμής βάσης
Can't render this file because it contains an unexpected character in line 1029 and column 18.
+1
View File
@@ -874,6 +874,7 @@
Background Fondo
Background Color Color de fondo
Background Contents Contenido del fondo
Custom background color Color de fondo personalizado
Balance Balance
Baseline Línea de base
Baseline shift Desplazamiento de línea de base
Can't render this file because it contains an unexpected character in line 4 and column 47.
+1
View File
@@ -860,6 +860,7 @@
Background Arrière-plan
Background Color Couleur d'arrière-plan
Background Contents Contenu de l'arrière-plan
Custom background color Couleur d'arrière-plan personnalisée
Balance Balance
Baseline Ligne de base
Baseline shift Décalage de la ligne de base
Can't render this file because it contains an unexpected character in line 6 and column 47.
+1
View File
@@ -2644,6 +2644,7 @@ cameraRaw Yellow Sárga
Autosave minutes Automatikus mentés gyakorisága (perc)
Background Color Háttérszín
Background Contents Háttér tartalma
Custom background color Egyéni háttérszín
Batch Kötegelt feldolgozás
Binary assets added Hozzáadott bináris elemek
Binary assets removed Eltávolított bináris elemek
Can't render this file because it contains an unexpected character in line 6 and column 47.
+1
View File
@@ -854,6 +854,7 @@
Background Background
Background Color Warna Background
Background Contents Isi Background
Custom background color Warna background kustom
Balance Keseimbangan
Baseline Baseline
Baseline shift Geser baseline
Can't render this file because it contains an unexpected character in line 4 and column 47.
+1
View File
@@ -869,6 +869,7 @@
Background Sfondo
Background Color Colore sfondo
Background Contents Contenuto sfondo
Custom background color Colore sfondo personalizzato
Balance Bilanciamento
Baseline Linea base
Baseline shift Scostamento linea base
1 # Italian (it) catalog. Columns, separated by a TAB: context <TAB> source <TAB> translation
869
870
871
872
873
874
875
+1
View File
@@ -873,6 +873,7 @@
Background 背景
Background Color 背景色
Background Contents 背景の内容
Custom background color カスタム背景色
Balance バランス
Baseline ベースライン
Baseline shift ベースラインシフト
Can't render this file because it contains an unexpected character in line 6 and column 47.
+1
View File
@@ -848,6 +848,7 @@
Background 배경
Background Color 배경색
Background Contents 배경 내용
Custom background color 사용자 정의 배경색
Balance 균형
Baseline 기준선
Baseline shift 기준선 이동
1 # Korean (ko) catalog. UTF-8: context<TAB>English source<TAB>translation.
848
849
850
851
852
853
854
+1
View File
@@ -852,6 +852,7 @@
Background Achtergrond
Background Color Achtergrondkleur
Background Contents Achtergrondinhoud
Custom background color Aangepaste achtergrondkleur
Balance Balans
Baseline Basislijn
Baseline shift Basislijnverschuiving
Can't render this file because it contains an unexpected character in line 8 and column 34.
+1
View File
@@ -871,6 +871,7 @@
Background Tło
Background Color Kolor tła
Background Contents Zawartość tła
Custom background color Niestandardowy kolor tła
Balance Balans
Baseline Linia bazowa
Baseline shift Przesunięcie linii bazowej
Can't render this file because it contains an unexpected character in line 6 and column 47.
+1
View File
@@ -854,6 +854,7 @@
Background Plano de fundo
Background Color Cor de fundo
Background Contents Conteúdo do plano de fundo
Custom background color Cor de fundo personalizada
Balance Equilíbrio
Baseline Linha de base
Baseline shift Deslocamento da linha de base
Can't render this file because it contains an unexpected character in line 6 and column 47.
+1
View File
@@ -1527,6 +1527,7 @@
Preset Name: Имя набора:
Width and Height Ширина и высота
Background Contents Содержимое фона
Custom background color Свой цвет фона
White Белый
Background Color Цвет фона
Transparent Прозрачный
Can't render this file because it contains an unexpected character in line 6 and column 47.
+1
View File
@@ -850,6 +850,7 @@
Background Pozadina
Background Color Boja pozadine
Background Contents Sadržaj pozadine
Custom background color Prilagođena boja pozadine
Balance Balans
Baseline Osnovna linija
Baseline shift Pomak osnovne linije
Can't render this file because it contains an unexpected character in line 6 and column 47.
+1
View File
@@ -841,6 +841,7 @@
Background Arka plan
Background Color Arka Plan Rengi
Background Contents Arka Plan İçeriği
Custom background color Özel Arka Plan Rengi
Balance Bakiye
Baseline Temel
Baseline shift Taban çizgisi kayması
Can't render this file because it contains an unexpected character in line 8 and column 34.
+1
View File
@@ -1508,6 +1508,7 @@
Preset Name: Назва набору:
Width and Height Ширина та висота
Background Contents Вміст фону
Custom background color Довільний колір фону
White Білий
Background Color Колір фону
Transparent Прозорий
Can't render this file because it contains an unexpected character in line 1384 and column 18.
+1
View File
@@ -1731,6 +1731,7 @@ cameraRaw Size Kích thước
Resolution: Độ phân giải:
Color Mode Chế độ màu
Background Contents Nội dung nền
Custom background color Màu nền tùy chỉnh
Pixels/Inch Pixel/inch
Pixels/Centimeter Pixel/centimet
White Trắng
Can't render this file because it contains an unexpected character in line 6 and column 47.
+1
View File
@@ -877,6 +877,7 @@
Background 背景
Background Color 背景颜色
Background Contents 背景内容
Custom background color 自定背景颜色
Balance 平衡
Baseline 基线
Baseline shift 基线偏移
1 # Simplified Chinese (zh-hans) catalog. UTF-8, TAB-separated columns:
877
878
879
880
881
882
883
+1
View File
@@ -880,6 +880,7 @@
Background 背景
Background Color 背景色
Background Contents 背景內容
Custom background color 自訂背景色
Balance 平衡
Baseline 基線
Baseline shift 基線位移
Can't render this file because it contains an unexpected character in line 6 and column 47.
+111 -6
View File
@@ -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(&current).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);