Skip to content

DesignCraft: An open-source page and layout design editor written by scratch in pure Rust

Merge PR #1080: Colour Picker dialog rewrite

aiongg01113a8f0046
diff --cc crates/ui-egui/src/dialogs/tests.rs
index 8a80198ea,83527c2ee..fb4c6ea44
--- a/crates/ui-egui/src/dialogs/tests.rs
+++ b/crates/ui-egui/src/dialogs/tests.rs
@@@ -3471,21 -3462,111 +3465,112 @@@ fn guide_counts(app: &DesignApp) -> Vec
(0..doc.page_count()).map(|abs| doc.page(abs).unwrap().guides.len()).collect()
}
+ /// The Color Picker laid out as InDesign's: a field and a slider for the component whose radio
+ /// is on, the new colour over the old one, and the values in every space.
+ #[test]
+
+ fn the_color_picker_keeps_a_typed_cmyk_value_the_colour_shown_does_not_change_with() {
+ use egui_kittest::kittest::Queryable;
+ let mut app = DesignApp::new(designcraft_engine::Session::new(), crate::Services::default());
+ app.run("file.new", json!({})).unwrap();
+ app.ui.dialog = Some(Dialog::new("colorPicker", json!({"target": "fill", "cmyk": [0.0, 0.0, 0.0, 100.0], "text": false})));
+ let mut h = crate::test_window::open(app, egui::vec2(1400.0, 900.0));
+ h.run_steps(3);
+ // A C typed at 100% K is kept, whether or not the colour shown changes with it.
+ let rgb = h.state().app.ui.dialog.as_ref().unwrap().fields.get("rgb").cloned();
+ h.get_by_label("C").focus();
+ h.run_steps(1);
+ h.event(egui::Event::Text("50".into()));
+ h.run_steps(2);
+ let d = h.state().app.ui.dialog.as_ref().unwrap();
+ assert_eq!(d.fields["cmyk"], json!([50.0, 0.0, 0.0, 100.0]), "rgb {:?} (was {rgb:?})", d.fields.get("rgb"));
+ assert!(d.b("edited"));
+ }
+
+ #[test]
+ fn the_color_picker_picks_on_its_field_and_slider_and_adds_a_cmyk_swatch() {
+ use egui_kittest::kittest::Queryable;
+ let mut app = DesignApp::new(designcraft_engine::Session::new(), crate::Services::default());
+ app.run("file.new", json!({})).unwrap();
+ app.run("frame.create", json!({"rect": [72, 72, 200, 200], "shape": "rectangle"})).unwrap();
+ app.run("object.color", json!({"color":designcraft_color::Color::from_hex("#7c1315").unwrap(),"target":"fill"})).unwrap();
+ app.ui.dialog = Some(Dialog::new("colorPicker", json!({"target": "fill", "hex": "#7c1315", "text": false})));
+ let mut h = crate::test_window::open(app, egui::vec2(1400.0, 900.0));
+ h.run_steps(3);
+ // R at first: R up the slider, B across and G up the field.
+ let _ = h.get_by_label("RGB Color Space View");
+ let field = h.get_by_label("Color field").rect();
+ crate::test_window::click_at(&mut h, egui::pos2(field.right() - 1.0, field.top() + 1.0));
+ h.run_steps(2);
+ let p = picked_of(h.state().app.ui.dialog.as_ref().unwrap());
+ assert!((p.rgb[0] - 124.0 / 255.0).abs() < 0.01 && p.rgb[1] > 0.97 && p.rgb[2] > 0.97, "{p:?}: R kept, G and B full");
+ // The slider sets R alone.
+ let slider = h.get_by_label("Color slider").rect();
+ crate::test_window::click_at(&mut h, egui::pos2(slider.center().x, slider.bottom() - 1.0));
+ h.run_steps(2);
+ let p = picked_of(h.state().app.ui.dialog.as_ref().unwrap());
+ assert!(p.rgb[0] < 0.02 && p.rgb[1] > 0.97, "{p:?}");
+ // Hue on the slider: the field is saturation across, brightness up.
+ store_picked(h.state_mut().app.ui.dialog.as_mut().unwrap(), &Picked::from_rgb([124.0 / 255.0, 19.0 / 255.0, 21.0 / 255.0], 0.0));
+ h.state_mut().app.ui.dialog.as_mut().unwrap().fields.insert("component".into(), json!("h"));
+ h.run_steps(2);
+ crate::test_window::click_at(&mut h, egui::pos2(field.left() + 1.0, field.top() + 1.0));
+ h.run_steps(2);
+ let p = picked_of(h.state().app.ui.dialog.as_ref().unwrap());
+ assert!(p.hsb[1] < 0.02 && p.hsb[2] > 0.98 && (p.hsb[0] - 359.0).abs() < 2.0, "{p:?}: no saturation, full brightness, the hue kept");
+ // Lab shows its own view.
+ h.state_mut().app.ui.dialog.as_mut().unwrap().fields.insert("component".into(), json!("l"));
+ h.run_steps(2);
+ let _ = h.get_by_label("Lab Color Space View");
+ // Add CMYK Swatch adds the colour as a CMYK swatch; OK applies it.
+ store_picked(h.state_mut().app.ui.dialog.as_mut().unwrap(), &Picked::from_rgb([124.0 / 255.0, 19.0 / 255.0, 21.0 / 255.0], 0.0));
+ h.run_steps(2);
+ let before = h.state().app.session.active().unwrap().doc.swatches.len();
+ h.get_by_label("Add CMYK Swatch").click();
+ h.run_steps(3);
+ let doc = &h.state().app.session.active().unwrap().doc;
+ assert_eq!(doc.swatches.len(), before + 1);
+ assert!(matches!(
+ doc.swatches.last().unwrap().value,
+ designcraft_color::swatch::SwatchValue::Color { color: designcraft_color::Color::Cmyk { .. }, .. }
+ ));
+ // The percentages the C/M/Y/K fields show, exactly (not the colour converted back).
+ h.state_mut().app.ui.dialog.as_mut().unwrap().fields.insert("cmyk".into(), json!([0.0, 50.0, 100.0, 0.0]));
+ h.run_steps(2);
+ h.get_by_label("Add CMYK Swatch").click();
+ h.run_steps(3);
+ let doc = &h.state().app.session.active().unwrap().doc;
+ let designcraft_color::swatch::SwatchValue::Color { color: designcraft_color::Color::Cmyk { c, m, y, k }, .. } =
+ doc.swatches.last().unwrap().value
+ else {
+ panic!("a CMYK swatch")
+ };
+ assert_eq!([c, m, y, k].map(|v| (v * 100.0).round()), [0.0, 50.0, 100.0, 0.0]);
+ // Its own OK, at the top right, applies it.
+ h.get_by_label("OK").click();
+ h.run_steps(3);
+ assert!(h.state().app.ui.dialog.is_none(), "OK closes it");
+ let st = h.state().app.session.active().unwrap();
+ let id = st.selection.items[0];
+ let fill = st.doc.item(id).unwrap().fill.swatch.clone();
+ assert_eq!(st.doc.resolve_color(&fill, 1.0).unwrap().to_hex(), "#7c1315");
+ }
+
#[test]
-
-fn create_guides_dialog_targets_the_page_in_view() {
+fn create_guides_dialog_covers_the_spread_in_view() {
let mut app = facing_app(6);
- crate::canvas::go_to_page(&mut app, 4);
- crate::menus::activate(&mut app, "layout.createGuides", &Value::Null);
- ctl(&mut app, "ui.dialog.set", json!({"field": "columns", "value": "3"}));
- assert_eq!(ctl(&mut app, "ui.dialog.confirm", json!({}))["ok"], true);
- assert_eq!(guide_counts(&app), vec![0, 0, 0, 0, 4, 0]);
- // "Both pages of the spread" covers the facing pair.
+ // As in InDesign, both pages of the facing spread in view get the guides.
crate::canvas::go_to_page(&mut app, 1);
crate::menus::activate(&mut app, "layout.createGuides", &Value::Null);
+ assert_eq!(app.ui.dialog.as_ref().unwrap().s("target"), "spread");
ctl(&mut app, "ui.dialog.set", json!({"field": "columns", "value": "2"}));
- ctl(&mut app, "ui.dialog.set", json!({"field": "target", "value": "spread"}));
+ assert_eq!(ctl(&mut app, "ui.dialog.confirm", json!({}))["ok"], true);
+ assert_eq!(guide_counts(&app), vec![0, 2, 2, 0, 0, 0]);
+ // "This page" narrows them to the page in view.
+ crate::canvas::go_to_page(&mut app, 4);
+ crate::menus::activate(&mut app, "layout.createGuides", &Value::Null);
+ ctl(&mut app, "ui.dialog.set", json!({"field": "columns", "value": "3"}));
+ ctl(&mut app, "ui.dialog.set", json!({"field": "target", "value": "page"}));
assert_eq!(ctl(&mut app, "ui.dialog.confirm", json!({}))["ok"], true);
assert_eq!(guide_counts(&app), vec![0, 2, 2, 0, 4, 0]);
}