PhotoCraft: An open-source, photo editor written by scratch in pure Rust
Merge PR #6484: Shapes: the Color panel, Swatches and Gradients fill the selected shape (#5568)
aiongga1d23fd2a623
diff --cc crates/engine/src/presets/tests.rsindex 432ef67ca,0b714245d..3906db577--- a/crates/engine/src/presets/tests.rs+++ b/crates/engine/src/presets/tests.rs@@@ -766,54 -749,44 +766,96 @@@ fn clone_source_errors_and_overlay()assert_eq!(v["rotation"], json!(-90.0));}+ fn shape_fill(s: &Session, id: LayerId) -> Option<Fill> {+ match &s.active().unwrap().doc.layer(id).unwrap().content {+ LayerContent::Shape(sh) => sh.fill.clone(),+ _ => panic!("not a shape layer"),+ }+ }++ /// #5568: clicking a gradient with a shape layer selected fills the shape with it in one undo+ /// step; a gradient fill keeps its angle, style, scale and reverse.+ #[test]+ fn gradient_preset_click_fills_the_selected_shape_layer() {+ let mut s = session(8);+ let id = LayerId(s.execute("shape.create", json!({"rect": [8, 8, 40, 30], "fill": "#336699"})).unwrap()["layer"].as_u64().unwrap());+ let before = shape_fill(&s, id);+ let r = s.execute("gradient.presets.select", json!({"preset": "Red 01"})).unwrap();+ assert_eq!(r["layer"], json!(id.0));+ let Some(Fill::Gradient { stops, angle, style, .. }) = shape_fill(&s, id) else { panic!("not a gradient") };+ // Red 01 starts at #7f0000; a new gradient fill is linear at 90°.+ assert_eq!(stops[0].1.to_rgba8(), [0x7f, 0, 0, 0xff]);+ assert_eq!((angle, style), (90.0, photocraft_doc::GradientStyle::Linear));+ s.execute("edit.undo", json!({})).unwrap();+ assert_eq!(shape_fill(&s, id), before);++ s.execute("shape.edit", json!({"layer": id.0, "fill": {"gradient": {"angle": 30, "style": "radial", "scale": 80, "reverse": true}}})).unwrap();+ s.execute("gradient.presets.select", json!({"preset": "Green 01"})).unwrap();+ let Some(Fill::Gradient { angle, style, scale, reverse, .. }) = shape_fill(&s, id) else { panic!("not a gradient") };+ assert_eq!((angle, style, scale, reverse), (30.0, photocraft_doc::GradientStyle::Radial, 0.8, true));+ }++ /// A locked shape refuses the click before anything changes, the Gradient tool's gradient included.+ #[test]+ fn gradient_preset_click_on_a_locked_shape_changes_nothing() {+ let mut s = session(8);+ let id = LayerId(s.execute("shape.create", json!({"rect": [8, 8, 40, 30], "fill": "#336699"})).unwrap()["layer"].as_u64().unwrap());+ let (before, gradient) = (shape_fill(&s, id), s.presets.gradient.name.clone());+ s.execute("layer.lockLayers", json!({"all": true})).unwrap();+ let err = s.execute("gradient.presets.select", json!({"preset": "Red 01"})).unwrap_err().to_string();+ assert!(err.contains("locked"), "{err}");+ assert_eq!(shape_fill(&s, id), before);+ assert_eq!(s.presets.gradient.name, gradient);+ }+++// ------------------------------------------------------------------ persistence++/// #5602: one damaged preset set in the preferences file must not discard the saved New+/// Document presets (or any other set) at the next launch.+#[test]+fn a_damaged_preset_set_keeps_the_other_sets() {+ let mut s = Session::new();+ let settings = json!({"width": 1600, "height": 900, "resolution": 144});+ s.execute("document.presets.save", json!({"name": "Banner", "settings": settings})).unwrap();+ let export = json!({+ "format": "jpg", "quality": 70, "lossless": false, "transparency": false, "scale": 50, "width": 640, "height": 480,+ "lockAspect": true, "sizing": "pixels", "metadata": "exif", "profile": "srgb", "embedProfile": false, "tgaBits": "24"+ });+ s.execute("file.export.presets.save", json!({"name": "Web JPEG", "settings": export})).unwrap();+ let saved = s.presets.to_json(&s);+ for key in ["gradients", "gradient", "styles", "shapes", "pattern_groups", "custom_shapes", "layer_defaults", "swatches", "export_presets"] {+ let mut blob = saved.clone();+ blob[key] = json!("not a preset set");+ let mut next = Session::new();+ next.load_presets_json(blob);+ assert_eq!(next.presets.documents, s.presets.documents, "damaged `{key}`");+ assert_eq!(next.presets.tool_presets.len(), Session::new().presets.tool_presets.len(), "damaged `{key}`");+ if key != "export_presets" {+ assert_eq!(next.presets.export_presets, s.presets.export_presets, "damaged `{key}`");+ }+ }+ // Null sets keep their defaults, as before.+ let mut next = Session::new();+ next.load_presets_json(json!({"tool_presets": null, "gradients": null, "documents": saved["documents"].clone()}));+ assert_eq!(next.presets.tool_presets.len(), Session::new().presets.tool_presets.len());+ assert_eq!(next.presets.gradients.len(), Session::new().presets.gradients.len());+ assert_eq!(next.presets.documents, s.presets.documents);+}++// ------------------------------------------------------------------ export presets++#[test]+fn export_presets_survive_a_presets_json_round_trip() {+ let mut s = Session::new();+ s.execute("file.export.presets.save", json!({"name": "Web", "settings": {"format": "jpg", "quality": 70}})).unwrap();+ assert_eq!(s.presets.export_presets.len(), 1);++ let saved = s.presets.to_json(&s);+ assert_eq!(saved["export_presets"].as_array().map(Vec::len), Some(1));++ let mut restored = Session::new();+ restored.load_presets_json(saved);+ assert_eq!(restored.presets.export_presets.len(), 1);+ assert_eq!(restored.presets.export_presets[0].name, "Web");+ assert_eq!(restored.presets.export_presets[0].settings.format, "jpg");+}diff --cc crates/ui-egui/src/swatches_ui.rsindex f39f1688a,d1fe7ef7c..dae445816--- a/crates/ui-egui/src/swatches_ui.rs+++ b/crates/ui-egui/src/swatches_ui.rs@@@ -2,10 -2,11 +2,11 @@@//! preset browser: groups with disclosure triangles, a colour grid with name tooltips, a context//! menu (Rename, Delete, Move to), and the footer (size · New Group · New Swatch · Delete).//!- //! As in Photoshop, clicking a swatch sets the foreground colour (and recolours selected type);+ //! As in Photoshop, clicking a swatch sets the foreground colour (and recolours selected type or+ //! fills the selected shape layer);//! Alt- or Ctrl/Cmd-clicking sets the background colour; double-clicking renames it. The//! panel menu (the group's "…" button) adds New Swatch, New Group, Import/Replace (`.aco`,-//! `.ase`), Export (`.aco`) and Export for Exchange (`.ase`) and Reset. Every action is a+//! `.act`, `.ase`), Export (`.aco`) and Export for Exchange (`.ase`) and Reset. Every action is a//! `swatches.*` engine command; this module only draws and dispatches. Colours are shown in the//! RGB working space through `photocraft-cms` (cached per library revision and colour settings).@@@ -278,18 -276,31 +281,43 @@@ mod testsuse egui::{Modifiers, vec2};use egui_kittest::Harness;+ /// Bytes without a path (the browser) go by the file name: an `.act` whose first color is+ /// 00 01 xx looks like an `.aco` header by content.+ #[test]+ fn an_act_from_bytes_imports_by_its_file_name() {+ let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default());+ let mut colors = vec![[10u8, 20, 30]; 256];+ colors[0] = [0, 1, 5];+ let bytes = photocraft_psd::act::write(&colors, None, None).unwrap();+ let r = import_bytes(&mut app, "Palette.act", &bytes, true).unwrap();+ assert_eq!((r["format"].as_str(), r["count"].as_u64()), (Some("act"), Some(256)));+ }++ /// #5568: clicking a swatch with a shape layer selected fills the shape with it, one undo+ /// step each, a gradient fill included.+ #[test]+ fn swatch_click_fills_the_selected_shape() {+ use photocraft_doc::{Fill, LayerContent, LayerId};+ let mut app = PhotocraftApp::new(photocraft_engine::Session::new(), crate::Services::default());+ app.session.execute("file.new", json!({"width": 64, "height": 48})).unwrap();+ let id = LayerId(app.run("shape.create", json!({"rect": [8, 8, 40, 30], "fill": {"gradient": {}}})).unwrap()["layer"].as_u64().unwrap());+ let fill = |app: &PhotocraftApp| match &app.session.active().unwrap().doc.layer(id).unwrap().content {+ LayerContent::Shape(sh) => sh.fill.clone(),+ _ => panic!("not a shape layer"),+ };+ let steps = |app: &PhotocraftApp| app.session.active().unwrap().history.entries().len();+ let before = steps(&app);+ let names: Vec<String> = app.session.presets.swatches[0].items.iter().take(2).map(|s| s.name.clone()).collect();+ use_swatch(&mut app, &names[0], false);+ let fg = app.session.tools.foreground;+ assert_eq!(fill(&app), Some(Fill::Solid(photocraft_doc::Color::rgba(fg[0], fg[1], fg[2], 1.0))), "{} filled the shape", names[0]);+ assert_eq!(steps(&app), before + 1);+ use_swatch(&mut app, &names[1], false);+ let fg = app.session.tools.foreground;+ assert_eq!(fill(&app), Some(Fill::Solid(photocraft_doc::Color::rgba(fg[0], fg[1], fg[2], 1.0))), "{} filled the shape", names[1]);+ assert_eq!(steps(&app), before + 2, "each swatch click is its own undo step");+ }+fn click(h: &mut Harness<'_, PhotocraftApp>, p: egui::Pos2, modifiers: Modifiers) {h.hover_at(p);// Held modifiers, as when the user holds Alt while clicking.