Keep Blend Options on its original target kind

This commit is contained in:
dotCooCoo committed 2026-11-26 14:17:42 -08:00
1 parent c646504cec
commit c7dc905fea
2 files changed
+77 -2

No files matched your search

+7
View File
@@ -406,6 +406,13 @@ fn edit_blends(s: &mut Session, label: &str, f: impl Fn(&mut Vec<Arc<Node>>, &mu
ok()
}
impl Session {
/// Whether Blend Options would operate on selected artwork rather than new-blend defaults.
pub fn has_selected_blend(&self) -> bool {
super::common::any_selected_of(self, is_blend, |_| true)
}
}
/// The first selected blend: its id, spec and key ids.
fn selected_spec(s: &Session) -> Option<(NodeId, BlendSpec, Vec<NodeId>)> {
let b = *selected_of(s, is_blend).first()?;
+70 -2
View File
@@ -130,7 +130,10 @@ pub(crate) fn pump(app: &mut VectorcraftApp, _ctx: &egui::Context) {
let state = app
.dialogs()
.and_then(|d| d.with(|host| host.get_by_id::<HostedParamDialog>(dialog_id(KIND)).map(|d| (d.is_previewing(), params(d.fields())))));
if let Some((previewing, p)) = state {
if let Some((previewing, p)) = state
&& (!previewing || app.session.has_selected_blend())
{
// With no blend selected, the command sets application defaults instead of previewing artwork.
// The answer only reports the preview's status, which the preview itself shows.
let _ = preview.run(app, previewing, PREVIEW_LABEL, CMD, &p);
}
@@ -139,8 +142,17 @@ pub(crate) fn pump(app: &mut VectorcraftApp, _ctx: &egui::Context) {
/// OK: keeps the preview as one undo step on a blend; sets the defaults otherwise.
fn ok(app: &mut VectorcraftApp, result: &ParamResult) -> Result<(), String> {
let on_blend = result.fields.text("__target") == Some("blend");
if on_blend != app.session.has_selected_blend() {
let reason = if on_blend {
tl!("Select a blend before applying these options.")
} else {
tl!("Deselect any selected blends before applying the options for new blends.")
};
return Err(reason.to_owned());
}
let p = params(&result.fields);
if result.fields.text("__target") != Some("blend") {
if !on_blend {
return app.run(CMD, p).map(|_| ());
}
let preview = app.object_dialogs.blend.take().unwrap_or_default();
@@ -212,6 +224,62 @@ mod tests {
}
}
fn app_with_smooth_blend() -> (VectorcraftApp, u64) {
let mut app = app();
app.run("file.new", json!({"width": 300, "height": 300})).unwrap();
for x in [0, 200] {
app.run("shape.rectangle", json!({"x": x, "y": 0, "width": 20, "height": 20})).unwrap();
}
app.run("select.all", json!({})).unwrap();
let id = app.run("object.blend.make", json!({"smooth": true})).unwrap()["id"].as_u64().unwrap();
(app, id)
}
#[test]
fn opening_blend_defaults_does_not_edit_a_later_selected_blend() {
let (mut app, id) = app_with_smooth_blend();
app.run("select.none", json!({})).unwrap();
app.run("ui.blendOptions", json!({})).unwrap();
assert_eq!(fields(&app).text("__target"), Some("defaults"));
set(&app, "spacing", json!("steps"));
set(&app, "steps", json!(7));
app.run("select.set", json!({"ids": [id]})).unwrap();
let undo = app.session.active().unwrap().history.undo.len();
confirm(&mut app);
assert_eq!(spec(&app, id).spacing, BlendSpacing::SmoothColor, "a newly selected blend keeps its spacing");
assert_eq!(app.session.active().unwrap().history.undo.len(), undo);
assert!(is_open(&app));
app.run("select.none", json!({})).unwrap();
confirm(&mut app);
assert!(!is_open(&app));
assert_eq!(app.session.prefs.blend_options.unwrap().spacing, BlendSpacing::Steps(7));
}
#[test]
fn opening_blend_options_for_a_selection_does_not_write_defaults() {
let (mut app, id) = app_with_smooth_blend();
let defaults = app.session.prefs.blend_options;
app.run("ui.blendOptions", json!({})).unwrap();
assert_eq!(fields(&app).text("__target"), Some("blend"));
set(&app, "spacing", json!("steps"));
set(&app, "steps", json!(7));
app.run("select.none", json!({})).unwrap();
frame(&mut app);
assert_eq!(app.session.prefs.blend_options, defaults, "the blend preview leaves new-blend defaults intact");
assert!(!app.session.in_interaction());
confirm(&mut app);
assert_eq!(app.session.prefs.blend_options, defaults);
assert!(is_open(&app));
app.run("select.set", json!({"ids": [id]})).unwrap();
confirm(&mut app);
assert!(!is_open(&app));
assert_eq!(spec(&app, id).spacing, BlendSpacing::Steps(7));
}
#[test]
fn opens_on_the_blend_previews_and_keeps_smooth_color() {
let mut app = app();