DesignCraft: An open-source page and layout design editor written by scratch in pure Rust
PR-1141: Swatches panel shows new-shape defaults; translate the spectrum label; test the fill
moppeeer83e789340c82
diff --git a/crates/ui-egui/src/i18n.rs b/crates/ui-egui/src/i18n.rsindex efe089ef9..1bd7074bd 100644--- a/crates/ui-egui/src/i18n.rs+++ b/crates/ui-egui/src/i18n.rs@@ -826,6 +826,7 @@ const TABLE: &[(&str, [&str; 5])] = &[("Choose Image…", ["Choose Image…", "Choisir une image…", "Choose Image…", "Choose Image…", "选择图像…"]),("Class:", ["Class:", "Classe :", "Class:", "Class:", "类别:"]),("Color Picker", ["Color Picker", "Sélecteur de couleurs", "Color Picker", "Color Picker", "拾色器"]),+ ("Color spectrum", ["Farbspektrum", "Spectre de couleurs", "Espectro de color", "カラースペクトル", "色谱"]),("Color Settings", ["Color Settings", "Réglages des couleurs", "Color Settings", "Color Settings", "颜色设置"]),("Color:", ["Color:", "Couleur :", "Color:", "Color:", "颜色:"]),("Columns", ["Columns", "Colonnes", "Columns", "Columns", "栏"]),@@ -5132,4 +5133,12 @@ mod tests {}}}++ #[test]+ fn color_panel_spectrum_label_is_translated_in_every_language() {+ for (code, _) in LANGUAGES.iter().filter(|(c, _)| !c.is_empty()) {+ assert_ne!(tr(code, "Color spectrum"), "Color spectrum", "{code}");+ }+ assert_eq!(tr("", "Color spectrum"), "Color spectrum");+ }}diff --git a/crates/ui-egui/src/i18n/ar.rs b/crates/ui-egui/src/i18n/ar.rsindex b050de38b..d3466915a 100644--- a/crates/ui-egui/src/i18n/ar.rs+++ b/crates/ui-egui/src/i18n/ar.rs@@ -349,6 +349,7 @@ pub(super) const TABLE: &[(&str, &str)] = &[("Object Layer Options", "خيارات طبقات الكائن"),("Colour Picker", "منتقي الألوان"),("Color Picker", "أداة اختيار الألوان"),+ ("Color spectrum", "طيف الألوان"),("Color Settings", "إعدادات الألوان"),("User Dictionary", "قاموس المستخدم"),("Menu Customization", "تخصيص القوائم"),diff --git a/crates/ui-egui/src/i18n/cs.rs b/crates/ui-egui/src/i18n/cs.rsindex e77e72a1c..8ee256913 100644--- a/crates/ui-egui/src/i18n/cs.rs+++ b/crates/ui-egui/src/i18n/cs.rs@@ -154,6 +154,7 @@ pub(super) const TABLE: &[(&str, &str)] = &[("Choose Image…", "Vybrat obrázek…"),("Class:", "Třída:"),("Color Picker", "Výběr barvy"),+ ("Color spectrum", "Barevné spektrum"),("Color Settings", "Nastavení barev"),("Color:", "Barva:"),("Columns", "Sloupce"),diff --git a/crates/ui-egui/src/i18n/de.rs b/crates/ui-egui/src/i18n/de.rsindex efc128858..9747be28e 100644--- a/crates/ui-egui/src/i18n/de.rs+++ b/crates/ui-egui/src/i18n/de.rs@@ -198,6 +198,7 @@ pub(super) const TABLE: &[(&str, &str)] = &[("Choose Image…", "Bild auswählen …"),("Class:", "Klasse:"),("Color Picker", "Farbwähler"),+ ("Color spectrum", "Farbspektrum"),("Color Settings", "Farbeinstellungen"),("Color:", "Farbe:"),("Columns", "Spalten"),diff --git a/crates/ui-egui/src/i18n/it.rs b/crates/ui-egui/src/i18n/it.rsindex a918a9829..9d4fcdd2f 100644--- a/crates/ui-egui/src/i18n/it.rs+++ b/crates/ui-egui/src/i18n/it.rs@@ -265,6 +265,7 @@ pub(super) const TABLE: &[(&str, &str)] = &[("Choose Image…", "Scegli immagine…"),("Class:", "Classe:"),("Color Picker", "Selettore colore"),+ ("Color spectrum", "Spettro dei colori"),("Color Settings", "Impostazioni colore"),("Color:", "Colore:"),("Columns", "Colonne"),diff --git a/crates/ui-egui/src/i18n/ja.rs b/crates/ui-egui/src/i18n/ja.rsindex 5647ca940..c8f33adf3 100644--- a/crates/ui-egui/src/i18n/ja.rs+++ b/crates/ui-egui/src/i18n/ja.rs@@ -383,6 +383,7 @@ pub(super) const TABLE: &[(&str, &str)] = &[("Choose Image…", "画像を選択…"),("Class:", "クラス:"),("Color Picker", "カラーピッカー"),+ ("Color spectrum", "カラースペクトル"),("Color Settings", "カラー設定"),("Color:", "カラー:"),("Columns", "列数"),diff --git a/crates/ui-egui/src/i18n/ka.rs b/crates/ui-egui/src/i18n/ka.rsindex 975da9ca1..7a80d8455 100644--- a/crates/ui-egui/src/i18n/ka.rs+++ b/crates/ui-egui/src/i18n/ka.rs@@ -187,6 +187,7 @@ pub(super) const TABLE: &[(&str, &str)] = &[("Choose Image…", "გამოსახულების არჩევა…"),("Class:", "კლასი:"),("Color Picker", "ფერის ამრჩევი"),+ ("Color spectrum", "ფერთა სპექტრი"),("Color Settings", "ფერის პარამეტრები"),("Color:", "ფერი:"),("Columns", "სვეტები"),diff --git a/crates/ui-egui/src/i18n/pl.rs b/crates/ui-egui/src/i18n/pl.rsindex 68d685d99..c70ba75ad 100644--- a/crates/ui-egui/src/i18n/pl.rs+++ b/crates/ui-egui/src/i18n/pl.rs@@ -154,6 +154,7 @@ pub(super) const TABLE: &[(&str, &str)] = &[("Choose Image…", "Wybierz obraz…"),("Class:", "Klasa:"),("Color Picker", "Próbnik kolorów"),+ ("Color spectrum", "Spektrum kolorów"),("Color Settings", "Ustawienia kolorów"),("Color:", "Kolor:"),("Columns", "Łamy"),diff --git a/crates/ui-egui/src/i18n/pt_br.rs b/crates/ui-egui/src/i18n/pt_br.rsindex 8521de594..8b1a1af7e 100644--- a/crates/ui-egui/src/i18n/pt_br.rs+++ b/crates/ui-egui/src/i18n/pt_br.rs@@ -357,6 +357,7 @@ pub(super) const TABLE: &[(&str, &str)] = &[("Object Layer Options", "Opções de camada do objeto"),("Colour Picker", "Seletor de cores"),("Color Picker", "Seletor de cores"),+ ("Color spectrum", "Espectro de cores"),("Color Settings", "Configurações de cores"),("User Dictionary", "Dicionário do usuário"),("Menu Customization", "Personalização de menus"),diff --git a/crates/ui-egui/src/i18n/ru.rs b/crates/ui-egui/src/i18n/ru.rsindex 251555ca5..a4bc3e7f5 100644--- a/crates/ui-egui/src/i18n/ru.rs+++ b/crates/ui-egui/src/i18n/ru.rs@@ -154,6 +154,7 @@ pub(super) const TABLE: &[(&str, &str)] = &[("Choose Image…", "Выбрать изображение…"),("Class:", "Класс:"),("Color Picker", "Палитра цветов"),+ ("Color spectrum", "Цветовой спектр"),("Color Settings", "Настройка цветов"),("Color:", "Цвет:"),("Columns", "Колонки"),diff --git a/crates/ui-egui/src/i18n/uk.rs b/crates/ui-egui/src/i18n/uk.rsindex dd4a1434d..1fb033cce 100644--- a/crates/ui-egui/src/i18n/uk.rs+++ b/crates/ui-egui/src/i18n/uk.rs@@ -268,6 +268,7 @@ pub(super) const TABLE: &[(&str, &str)] = &[("Choose Image…", "Вибрати зображення…"),("Class:", "Клас:"),("Color Picker", "Палітра кольорів"),+ ("Color spectrum", "Колірний спектр"),("Color Settings", "Налаштування кольору"),("Color:", "Колір:"),("Columns", "Колонки"),diff --git a/crates/ui-egui/src/panels/mod.rs b/crates/ui-egui/src/panels/mod.rsindex 7c40f3b1b..bc3f71795 100644--- a/crates/ui-egui/src/panels/mod.rs+++ b/crates/ui-egui/src/panels/mod.rs@@ -80,6 +80,19 @@ pub fn color_target(app: &mut DesignApp) -> Option<(String, f32, String, f32)> {Some((i.fill, sel_tint(app, false), i.stroke, sel_tint(app, true)))}+/// [`color_target`], or with nothing selected the colours new shapes get (the Color and Swatches+/// panels then edit those, as InDesign's do).+pub fn color_target_or_defaults(app: &mut DesignApp) -> Option<(String, f32, String, f32)> {+ color_target(app).or_else(|| {+ let state = app.session.active()?;+ if !state.selection.items.is_empty() || state.selection.text.is_some() || state.selection.cells.is_some() {+ return None;+ }+ let colors = &state.doc.settings.new_object;+ Some((colors.fill.clone(), colors.fill_tint, colors.stroke.clone(), colors.stroke_tint))+ })+}+/// Apply a swatch (and tint) to the fill or stroke of what colour edits apply to.pub fn apply_swatch(app: &mut DesignApp, stroke: bool, swatch: &str, tint: Option<f32>) -> Result<Value, String> {remember_swatch(app, swatch);diff --git a/crates/ui-egui/src/panels/swatches.rs b/crates/ui-egui/src/panels/swatches.rsindex 9cd71ac82..87984b7a0 100644--- a/crates/ui-egui/src/panels/swatches.rs+++ b/crates/ui-egui/src/panels/swatches.rs@@ -11,7 +11,8 @@ pub fn show(app: &mut DesignApp, ui: &mut egui::Ui) {let t = Tokens::get(ui.ctx());let Some(st) = app.session.active() else { return };let doc = st.doc.clone();- let target = super::color_target(app);+ // With nothing selected, the proxy, Tint field and highlighted row are those of new shapes.+ let target = super::color_target_or_defaults(app);header(app, ui, &doc, target.as_ref());// The row of the colour in front of the proxy is highlighted.let fill_cur = target.map(|(fill, _, stroke, _)| if app.ui.stroke_front { stroke } else { fill });@@ -340,14 +341,7 @@ pub enum ColorMode {/// swatch), the spectrum ramp, Add to Swatches. Edits apply unnamed colours (`object.color`).pub fn color_panel(app: &mut DesignApp, ui: &mut egui::Ui) {let t = Tokens::get(ui.ctx());- let target = super::color_target(app).or_else(|| {- let state = app.session.active()?;- if !state.selection.items.is_empty() || state.selection.text.is_some() || state.selection.cells.is_some() {- return None;- }- let colors = &state.doc.settings.new_object;- Some((colors.fill.clone(), colors.fill_tint, colors.stroke.clone(), colors.stroke_tint))- });+ let target = super::color_target_or_defaults(app);let Some((fill, fill_tint, stroke_sw, stroke_tint)) = target else {ui.label(crate::rtl::widget(ui,@@ -624,10 +618,60 @@ mod color_panel_defaults_tests {h.state_mut().app.run("frame.create", json!({"rect":[240,72,368,200],"content":"unassigned"})).unwrap()["id"].as_u64().unwrap(),);assert_eq!(h.state().app.session.active().unwrap().doc.item(new).unwrap().stroke.swatch, defaults.stroke);+ // Two undo steps: the new frame, then the spectrum click's default stroke.h.state_mut().app.run("edit.undo", json!({})).unwrap();h.state_mut().app.run("edit.undo", json!({})).unwrap();assert_eq!(h.state().app.session.active().unwrap().doc.settings.new_object.stroke, original.swatch);assert_eq!(h.state().app.session.active().unwrap().doc.item(old).unwrap().stroke, original);+ // With the Fill box in front, the ramp sets the default fill instead.+ let original_fill = h.state().app.session.active().unwrap().doc.item(old).unwrap().fill.clone();+ h.state_mut().app.ui.stroke_front = false;+ h.run_steps(2);+ let ramp = h.get_by_label("Color spectrum").rect();+ crate::test_window::click_at(&mut h, egui::pos2(ramp.left() + ramp.width() * 2.0 / 3.0, ramp.center().y));+ let defaults = h.state().app.session.active().unwrap().doc.settings.new_object.clone();+ assert_ne!(defaults.fill, original_fill.swatch);+ assert_eq!(defaults.stroke, original.swatch, "the stroke default is left alone");+ assert_eq!(h.state().app.session.active().unwrap().doc.item(old).unwrap().fill, original_fill);+ }++ /// The Swatches panel shows the same defaults: its Tint field reads the default fill's tint,+ /// and a click on a swatch row sets the default fill, not an existing frame's.+ #[test]+ fn swatches_panel_with_no_selection_shows_and_edits_future_shape_colors() {+ use egui::accesskit::Role;+ let mut app = DesignApp::new(designcraft_engine::Session::new(), crate::Services::default());+ app.run("file.new", json!({})).unwrap();+ let old =+ designcraft_doc::ItemId(app.run("frame.create", json!({"rect":[72,72,200,200],"content":"unassigned"})).unwrap()["id"].as_u64().unwrap());+ let original = app.session.active().unwrap().doc.item(old).unwrap().fill.clone();+ app.run("selection.set", json!({"ids":[]})).unwrap();+ app.run("object.fill", json!({"swatch": "[Black]", "tint": 0.4})).unwrap();+ app.ui.stroke_front = false;+ let mut h = egui_kittest::Harness::builder().with_size(vec2(320.0, 480.0)).build_ui_state(+ |ui, app: &mut DesignApp| {+ // The panel's fonts are installed on the first frame and used from the next.+ let id = egui::Id::new("test_fonts");+ if ui.data(|d| d.get_temp::<bool>(id)).is_none() {+ crate::theme::install_fonts(ui.ctx(), "");+ ui.data_mut(|d| d.insert_temp(id, true));+ return;+ }+ show(app, ui);+ },+ app,+ );+ h.run_steps(3);+ let fields = h.query_all_by(|n| matches!(n.role(), Role::SpinButton | Role::TextInput)).filter_map(|n| n.value()).collect::<Vec<_>>();+ assert!(fields.iter().any(|v| v.starts_with("40")), "the Tint field shows the default fill's 40 %: {fields:?}");+ let row = h.get_by_label("[Paper]").rect().center();+ h.hover_at(row);+ h.drag_at(row);+ h.drop_at(row);+ h.run_steps(3);+ let defaults = h.state().session.active().unwrap().doc.settings.new_object.clone();+ assert_eq!((defaults.fill.as_str(), defaults.fill_tint), ("[Paper]", 1.0));+ assert_eq!(h.state().session.active().unwrap().doc.item(old).unwrap().fill, original);}}