diff --git a/crates/ui-egui/src/dialogs/revolve.rs b/crates/ui-egui/src/dialogs/revolve.rs index 7d7b6fe3d1..1349146f59 100644 --- a/crates/ui-egui/src/dialogs/revolve.rs +++ b/crates/ui-egui/src/dialogs/revolve.rs @@ -238,17 +238,15 @@ fn solid_controls(app: &VectorcraftApp, ui: &mut egui::Ui, d: &mut Dialog, id: & } } -/// The Bevel Shape menu of Extrude & Bevel and Extrude & Bevel (Classic) (`effect`): None and the -/// built-in shapes, then the custom bevels of the bevels file, and the effect's own custom bevel -/// when the file does not list it (a document from elsewhere carries its points). Picking a -/// custom bevel stores its name and a copy of its points. In the 3D and Materials panel, Edit -/// Bevels… opens the bevels file (`__editBevels`, which the panel runs). -pub(super) fn bevel_choice(app: &VectorcraftApp, ui: &mut egui::Ui, d: &mut Dialog, effect: &str) { - const KEY: &str = "bevel"; - // (shown label, value, the custom bevel's name and points). The built-in labels are - // translated; custom bevels show their names as they are. - type Choice = (String, Value, Option<(String, Value)>); - let mut options: Vec = super::effect::own_choices(effect, KEY, false) +/// A Bevel Shape menu entry: (shown label, value, the custom bevel's name and points). +pub(crate) type BevelChoice = (String, Value, Option<(String, Value)>); + +/// The entries of [`bevel_choice`]'s menu for form `fields`, and how many are built in. The +/// built-in labels are translated; custom bevels show their names as they are. The effect's own +/// custom bevel, the panel's [`OWN_BEVEL`] else the form's, is listed unless the file lists it by +/// name with the same points. +pub(crate) fn bevel_options(app: &VectorcraftApp, fields: &serde_json::Map, effect: &str) -> (Vec, usize) { + let mut options: Vec = super::effect::own_choices(effect, "bevel", false) .unwrap_or_default() .into_iter() .map(|(label, value)| (tl!(label).to_string(), json!(value), None)) @@ -257,16 +255,39 @@ pub(super) fn bevel_choice(app: &VectorcraftApp, ui: &mut egui::Ui, d: &mut Dial for b in app.session.bevels.bevels() { options.push((b.name.clone(), json!("custom"), Some((b.name.clone(), b.profile_json())))); } + let custom = fields.get("bevel").and_then(Value::as_str) == Some("custom"); + let own = match fields.get(OWN_BEVEL).and_then(Value::as_array).map(Vec::as_slice) { + Some([n, p]) => Some((n.as_str().unwrap_or_default().to_string(), p.clone())), + _ => fields + .get(BEVEL_PROFILE) + .filter(|p| custom && !p.is_null()) + .map(|p| (fields.get(BEVEL_NAME).and_then(Value::as_str).unwrap_or_default().to_string(), p.clone())), + }; + if let Some((n, p)) = own + && !options[builtin..].iter().any(|(_, _, c)| c.as_ref().is_some_and(|(cn, cp)| *cn == n && *cp == p)) + { + let shown = if n.is_empty() { tl!("Custom").to_string() } else { n.clone() }; + options.push((shown, json!("custom"), Some((n, p)))); + } + (options, builtin) +} + +/// The form field where the 3D and Materials panel puts the first selected effect's custom bevel, +/// `[name, points]`, so Bevel Shape lists it while a mixed selection unsets the shown bevel. +pub(crate) const OWN_BEVEL: &str = "__ownBevel"; + +/// The Bevel Shape menu of Extrude & Bevel and Extrude & Bevel (Classic) (`effect`): None and the +/// built-in shapes, then the custom bevels of the bevels file, and the effect's own custom bevel +/// when the file does not list it (a document from elsewhere carries its points). Picking a +/// custom bevel stores its name and a copy of its points. In the 3D and Materials panel, Edit +/// Bevels… opens the bevels file (`__editBevels`, which the panel runs). +pub(super) fn bevel_choice(app: &VectorcraftApp, ui: &mut egui::Ui, d: &mut Dialog, effect: &str) { + const KEY: &str = "bevel"; + let (options, builtin) = bevel_options(app, &d.fields, effect); let name = d.fields.get(BEVEL_NAME).and_then(Value::as_str).unwrap_or_default().to_string(); let points = d.fields.get(BEVEL_PROFILE).filter(|v| !v.is_null()).cloned(); let custom = d.fields.get(KEY).and_then(Value::as_str) == Some("custom"); let is = |c: &Option<(String, Value)>| c.as_ref().is_some_and(|(n, p)| *n == name && Some(p) == points.as_ref()); - // The effect's own custom bevel, unless the file lists it by name with the same points. - let own = custom && !options[builtin..].iter().any(|(_, _, c)| is(c)); - if own && let Some(p) = &points { - let shown = if name.is_empty() { tl!("Custom").to_string() } else { name.clone() }; - options.push((shown, json!("custom"), Some((name.clone(), p.clone())))); - } let default = vectorcraft_effects::default_params(effect).and_then(|p| p.get(KEY).cloned()).unwrap_or(Value::Null); let mixed = d.fields.get(KEY).is_some_and(Value::is_null); let value = d.fields.get(KEY).filter(|v| !v.is_null()).cloned().unwrap_or_else(|| default.clone()); diff --git a/crates/ui-egui/src/panels/tests_three_d.rs b/crates/ui-egui/src/panels/tests_three_d.rs index d272901fce..d2bca6e290 100644 --- a/crates/ui-egui/src/panels/tests_three_d.rs +++ b/crates/ui-egui/src/panels/tests_three_d.rs @@ -273,6 +273,44 @@ fn a_custom_bevel_picked_for_a_mixed_selection_reaches_every_effect() { } } +/// Two custom bevels the bevels file does not list: the mixed selection unsets the shown points, +/// and Bevel Shape still lists the first effect's own bevel, which then reaches both effects. +#[test] +fn a_mixed_selection_still_lists_the_first_effects_own_custom_bevel() { + use crate::dialogs::revolve::{OWN_BEVEL, bevel_options}; + let mut app = app(); + let notch = json!([[0.0, 0.0], [0.2, 0.6], [0.7, 0.4], [1.0, 1.0]]); + let steps = json!([[0.0, 0.0], [0.0, 0.5], [0.5, 0.5], [0.5, 1.0], [1.0, 1.0]]); + let custom = |name: &str, points: &Value| json!({"bevel": "custom", "bevelName": name, "bevelProfile": points}); + app.run("effect.apply", json!({"effect": "threeD.extrude", "item": null, "params": custom("Notch", ¬ch)})).unwrap(); + let a = app.session.active().unwrap().selection.objects[0].0; + let b = app.run("shape.rectangle", json!({"x": 250, "y": 100, "width": 100, "height": 100})).unwrap()["id"].as_u64().unwrap(); + app.run("effect.apply", json!({"effect": "threeD.extrude", "item": null, "params": custom("Steps", &steps)})).unwrap(); + app.run("select.set", json!({"ids": [a, b]})).unwrap(); + let mut fields = target(&app).map(|t| vectorcraft_effects::merged_params(&t.effect, &t.params)).unwrap().as_object().cloned().unwrap(); + let own = super::own_bevel(&fields).unwrap(); + super::mark_mixed(&app, &mut fields); + assert_eq!(fields[vectorcraft_effects::BEVEL_PROFILE], Value::Null); + fields.insert(OWN_BEVEL.into(), own); + let (options, builtin) = bevel_options(&app, &fields, "threeD.extrude"); + assert_eq!(options.len(), builtin + 1); + let (label, value, picked) = options[builtin].clone(); + assert_eq!((label.as_str(), value), ("Notch", json!("custom"))); + let (name, points) = picked.unwrap(); + assert_eq!(points, notch); + // Picking it sends the whole bevel to both effects. + fields.remove(OWN_BEVEL); + let before = Value::Object(fields.clone()); + fields.insert(vectorcraft_effects::BEVEL_NAME.into(), json!(name)); + fields.insert(vectorcraft_effects::BEVEL_PROFILE.into(), points); + send(&mut app, &egui::Context::default(), changed_settings(&before, &Value::Object(fields)), false); + let st = app.session.active().unwrap(); + for id in [a, b] { + let p = &st.doc.node(vectorcraft_doc::NodeId(id)).unwrap().appearance.effects[0].params; + assert_eq!((&p[vectorcraft_effects::BEVEL_NAME], &p[vectorcraft_effects::BEVEL_PROFILE]), (&json!("Notch"), ¬ch), "{id}"); + } +} + #[test] fn the_panel_finds_a_3d_effect_on_a_fill() { let mut app = app(); diff --git a/crates/ui-egui/src/panels/three_d.rs b/crates/ui-egui/src/panels/three_d.rs index c16963bb8b..ddab3a0fb8 100644 --- a/crates/ui-egui/src/panels/three_d.rs +++ b/crates/ui-egui/src/panels/three_d.rs @@ -13,6 +13,7 @@ use vectorcraft_effects::{BEVEL_NAME, BEVEL_PROFILE, CLASSIC_EXTRUDE, CLASSIC_RE use super::{live_run, pstate, set_pstate}; use crate::VectorcraftApp; +use crate::dialogs::revolve::OWN_BEVEL; use crate::dialogs::three_d_materials as materials; use crate::state::Dialog; use crate::theme::Tokens; @@ -235,13 +236,18 @@ pub fn show(app: &mut VectorcraftApp, ui: &mut Ui) { }; // The applied effect's settings in the dialog's form, with the controls' own state. let mut fields = vectorcraft_effects::merged_params(&t.effect, &t.params).as_object().cloned().unwrap_or_default(); + let own_bevel = own_bevel(&fields); mark_mixed(app, &mut fields); fields.extend(pstate::>(&ctx, UI_STATE)); fields.insert("__effect".into(), json!(t.effect)); fields.insert("__panel".into(), json!(true)); + if let Some(own) = own_bevel { + fields.insert(OWN_BEVEL.into(), own); + } let mut d = Dialog { kind: ID.into(), fields }; let before = crate::dialogs::form::params(&d); crate::dialogs::revolve::controls(app, ui, &mut d); + d.fields.remove(OWN_BEVEL); let edit_bevels = d.fields.remove("__editBevels").is_some(); let action = d.fields.remove(materials::ACTION).and_then(|v| materials::Action::from_value(&v)); let state: Map = d @@ -490,6 +496,14 @@ pub(crate) fn mark_mixed(app: &VectorcraftApp, fields: &mut Map) } } +/// The custom bevel of an effect's settings `fields`, `[name, points]`, when its `bevel` is +/// `"custom"` with points (the Bevel Shape menu lists it while a mixed selection unsets them). +pub(crate) fn own_bevel(fields: &Map) -> Option { + let points = fields.get(BEVEL_PROFILE).filter(|v| !v.is_null())?; + let name = fields.get(BEVEL_NAME).and_then(Value::as_str).unwrap_or_default(); + (fields.get("bevel").and_then(Value::as_str) == Some("custom")).then(|| json!([name, points])) +} + /// The settings whose values differ between `before` and `after`, with their values in `after`. /// A change to a custom bevel (`bevel` `"custom"`, its name or its points) sends all three, so an /// effect of a mixed selection that had none takes the whole bevel.