From 456ecd0bfd809674440f35514d26298f6276d61e Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Thu, 26 Nov 2026 17:41:05 -0800 Subject: [PATCH] Require a drawing for New Swatch in the Color panel --- crates/ui-egui/src/panels/color.rs | 15 +++++++------ crates/ui-egui/src/panels/color/tests.rs | 27 +++++++++++++++++++++++- 2 files changed, 35 insertions(+), 7 deletions(-) diff --git a/crates/ui-egui/src/panels/color.rs b/crates/ui-egui/src/panels/color.rs index 441200b9ee..034ae7933a 100644 --- a/crates/ui-egui/src/panels/color.rs +++ b/crates/ui-egui/src/panels/color.rs @@ -579,7 +579,7 @@ pub fn show(app: &mut VectorcraftApp, ui: &mut Ui) { } /// Carry out what the panel menu asked of the app. -fn carry_out(app: &mut VectorcraftApp, tgt: &Target, action: ColorPanelAction) { +fn carry_out(app: &mut VectorcraftApp, tgt: &Target, action: ColorPanelAction) -> Result<(), String> { match action { ColorPanelAction::Mode(m) => set_mode(app, tgt, m), ColorPanelAction::Copy(text) => { @@ -590,17 +590,21 @@ fn carry_out(app: &mut VectorcraftApp, tgt: &Target, action: ColorPanelAction) { ColorPanelAction::Invert => recolor(app, tgt, true, false), ColorPanelAction::Complement => recolor(app, tgt, false, false), ColorPanelAction::NewSwatch => { + if app.session.active().is_none() { + return Err(tl!("No document").into()); + } if let Some(c) = tgt.color() { // A tint saves a tint swatch ("Name 40%"); a color keeps its model. let p = match tgt.tint() { Some(t) => json!({"swatch": t.swatch, "tint": t.tint * 100.0}), None => json!({"color": color_json(&c), "keepModel": true}), }; - app.run("swatch.new", p).ok(); + app.run("swatch.new", p)?; } } _ => {} } + Ok(()) } /// The panel menu: Hide Options and Show Recent Colors, the five modes, Invert, Complement, Create @@ -615,6 +619,7 @@ pub(crate) fn menu_states(app: &VectorcraftApp, states: &mut CommandStates) { let tgt = target_now(app); let has_color = tgt.color().is_some() || mixed_now(app); for (command, state) in ColorPanelMenu::states(LAYOUT, &app.ui.color_panel, &shown(app, &tgt), has_color) { + let state = if command == ColorPanelCommand::NewSwatch && !crate::menus::enabled(app, "swatch.new") { state.enabled(false) } else { state }; states.set(command, state); } } @@ -624,10 +629,8 @@ pub(crate) fn run(app: &mut VectorcraftApp, id: &str, _params: &Value) -> Option let command = ColorPanelCommand::from_ref(&CommandRef::new(id))?; let tgt = target_now(app); let edited = srgba(&tgt.color().unwrap_or(Color::BLACK)); - if let Some(action) = ColorPanelMenu::run_command(&mut app.ui.color_panel, command, edited) { - carry_out(app, &tgt, action); - } - Some(Ok(Value::Null)) + let outcome = ColorPanelMenu::run_command(&mut app.ui.color_panel, command, edited).map_or(Ok(()), |action| carry_out(app, &tgt, action)); + Some(outcome.map(|()| Value::Null)) } #[cfg(test)] diff --git a/crates/ui-egui/src/panels/color/tests.rs b/crates/ui-egui/src/panels/color/tests.rs index e63fc09e7a..d354702ef2 100644 --- a/crates/ui-egui/src/panels/color/tests.rs +++ b/crates/ui-egui/src/panels/color/tests.rs @@ -1,6 +1,31 @@ use super::*; use vectorcraft_engine::Session; +#[test] +fn create_new_swatch_requires_a_drawing_but_copy_color_remains_available() { + let enabled = |app: &VectorcraftApp, command| { + let mut states = CommandStates::new(); + menu_states(app, &mut states); + states.get(&CommandRef::from(command)).is_some_and(|state| state.is_enabled()) + }; + let mut app = VectorcraftApp::new(Session::new(), Default::default()); + let new_swatch = CommandRef::from(ColorPanelCommand::NewSwatch).id; + for _ in 0..2 { + app.run("file.new", json!({"width": 100, "height": 100})).unwrap(); + app.session.fill_active = true; + app.run("paint.setFill", json!({"color": "#336699"})).unwrap(); + assert!(enabled(&app, ColorPanelCommand::NewSwatch)); + assert!(enabled(&app, ColorPanelCommand::CopyHex)); + let before = app.session.active().unwrap().doc.swatches.len(); + app.run(&new_swatch, json!({})).unwrap(); + assert_eq!(app.session.active().unwrap().doc.swatches.len(), before + 1); + app.session.execute("file.close", &json!({"index": 0})).unwrap(); + assert!(enabled(&app, ColorPanelCommand::CopyHex)); + assert!(!enabled(&app, ColorPanelCommand::NewSwatch)); + assert!(app.run(&new_swatch, json!({})).is_err()); + } +} + fn app() -> VectorcraftApp { let mut app = VectorcraftApp::new(Session::new(), Default::default()); app.run("file.new", json!({"width": 200, "height": 200})).unwrap(); @@ -70,7 +95,7 @@ fn create_new_swatch_keeps_the_shown_colors_model_in_a_cmyk_document() { assert_eq!(shown.model(), model, "{fill}"); let count = app.session.doc().unwrap().doc.swatches.len(); let tgt = target_now(&app); - carry_out(&mut app, &tgt, ColorPanelAction::NewSwatch); + carry_out(&mut app, &tgt, ColorPanelAction::NewSwatch).unwrap(); assert_eq!(app.session.doc().unwrap().doc.swatches.len(), count + 1, "{fill}: one new swatch"); let w = newest(&app); assert_eq!(w.paint.color(), Some(shown), "{fill}: the swatch has the shown color, {}", w.name);