Stroke: built-in width profiles stay protected from Delete Profile

Delete Profile removes saved profiles only, as on main: the engine
refuses a built-in name again, Reset Profiles clears the saved ones, and
the hidden_width_profiles preference is gone. Hiding a built-in also
made stroke.set {profile: "lens"} fail with "unknown profile lens".

The command's enable check goes back to has_saved_profiles. The check
added here passed whenever a built-in other than Uniform was listed, so
a key press ran Delete Profile with nothing to delete and
shortcut_editor::tests::listed_commands_run_from_a_key_without_a_missing_param
failed.

In the drop-down, the footer's Delete Profile, the row menu's Delete
Profile and the trash drop target act on saved profiles only.
This commit is contained in:
dotCooCoo committed 2026-12-05 07:35:17 -08:00
1 parent 5e0e9e52b9
commit 8a7ed03908
6 files changed
+33 -80

No files matched your search

+13 -36
View File
@@ -68,8 +68,8 @@ pub fn specs() -> Vec<CommandSpec> {
"Delete Profile",
[],
None,
"{name?} remove a built-in or saved profile from the Profile list (default: the selected stroke's); \"uniform\" can't be deleted, and strokes keep their widths → {deleted}",
has_deletable_profiles,
"{name?} remove a saved profile from the Profile list (default: the selected stroke's); built-in profiles can't be deleted, and strokes keep their widths → {deleted}",
has_saved_profiles,
profile_delete
),
cmd!(
@@ -77,7 +77,7 @@ pub fn specs() -> Vec<CommandSpec> {
"Reset Profiles",
[],
None,
"{} remove every saved profile and restore the default built-in profiles and order → {removed: count}",
"{} remove every saved profile and restore the default order → {removed: count}",
can_reset_profiles,
profile_reset
),
@@ -734,7 +734,6 @@ impl Session {
let uniform = ProfileEntry { id: uniform.id, label: uniform.label, points: uniform.points, built_in: true };
let candidates: Vec<ProfileEntry<'_>> = WidthProfile::PRESETS[1..]
.iter()
.filter(|p| !self.prefs.hidden_width_profiles.iter().any(|h| h == p.id))
.map(|p| ProfileEntry { id: p.id, label: p.label, points: p.points, built_in: true })
.chain(self.prefs.width_profiles.iter().map(|p| ProfileEntry { id: &p.name, label: &p.name, points: &p.profile.points, built_in: false }))
.collect();
@@ -779,10 +778,9 @@ impl Session {
(1..).map(|n| format!("Width Profile {n}")).find(|n| !self.profile_name_taken(n)).unwrap_or_default()
}
/// Does a listed or built-in profile have `name` as its id or label (ignoring case)?
/// Does a listed profile have `name` as its id or label (ignoring case)?
fn profile_name_taken(&self, name: &str) -> bool {
self.profile_entries().any(|e| e.id.eq_ignore_ascii_case(name) || e.label.eq_ignore_ascii_case(name))
|| WidthProfile::PRESETS.iter().any(|b| b.id.eq_ignore_ascii_case(name) || b.label.eq_ignore_ascii_case(name))
}
/// The variable width Add to Profiles saves: the shown stroke's profile, when it isn't listed.
@@ -804,27 +802,18 @@ fn has_saved_profiles(s: &Session) -> std::result::Result<(), String> {
if s.prefs.width_profiles.is_empty() { Err("no saved profiles".into()) } else { Ok(()) }
}
fn has_deletable_profiles(s: &Session) -> std::result::Result<(), String> {
if s.prefs.width_profiles.is_empty() && !s.profile_entries().any(|e| e.id != WidthProfile::PRESETS[0].id) {
Err("no deletable profiles".into())
} else {
Ok(())
}
}
fn can_reset_profiles(s: &Session) -> std::result::Result<(), String> {
if s.prefs.width_profiles.is_empty() && s.prefs.width_profile_order.is_empty() && s.prefs.hidden_width_profiles.is_empty() {
if s.prefs.width_profiles.is_empty() && s.prefs.width_profile_order.is_empty() {
Err("no saved profiles".into())
} else {
Ok(())
}
}
/// The default order of reorderable profiles (visible built-ins after Uniform, then saved profiles).
/// The default order of reorderable profiles (the built-ins after Uniform, then saved profiles).
fn default_profile_order(s: &Session) -> Vec<String> {
WidthProfile::PRESETS[1..]
.iter()
.filter(|p| !s.prefs.hidden_width_profiles.iter().any(|h| h == p.id))
.map(|p| p.id.to_string())
.chain(s.prefs.width_profiles.iter().map(|sp| sp.name.clone()))
.collect()
@@ -853,36 +842,24 @@ fn profile_delete(s: &mut Session, p: &Value) -> Result<Value> {
None => {
let shown = s.shown_stroke().and_then(|st| st.profile);
let entry = shown.as_ref().and_then(|p| s.profile_entry(Some(p)));
entry.map(|e| e.id.to_string()).ok_or_else(|| bad(C, "give a name, or select a stroke with a deletable profile"))?
entry.map(|e| e.id.to_string()).ok_or_else(|| bad(C, "give a name, or select a stroke with a saved profile"))?
}
};
let uniform = WidthProfile::PRESETS[0];
if name == uniform.id || name.eq_ignore_ascii_case(uniform.label) {
return Err(bad(C, "Uniform can't be deleted"));
if WidthProfile::PRESETS.iter().any(|b| b.id == name || b.label == name) {
return Err(bad(C, format!("{name} is built in and can't be deleted")));
}
let deleted = if let Some(preset) = WidthProfile::PRESETS[1..]
.iter()
.find(|b| (b.id == name || b.label.eq_ignore_ascii_case(&name)) && !s.prefs.hidden_width_profiles.iter().any(|h| h == b.id))
{
s.prefs.hidden_width_profiles.push(preset.id.to_string());
preset.id.to_string()
} else if let Some(i) = s.prefs.width_profiles.iter().position(|sp| sp.name == name) {
s.prefs.width_profiles.remove(i);
name
} else {
return Err(bad(C, format!("no profile called {name}")));
};
s.prefs.width_profile_order.retain(|id| id != &deleted);
let i = s.prefs.width_profiles.iter().position(|sp| sp.name == name).ok_or_else(|| bad(C, format!("no saved profile called {name}")))?;
s.prefs.width_profiles.remove(i);
s.prefs.width_profile_order.retain(|id| id != &name);
if s.prefs.width_profile_order == default_profile_order(s) {
s.prefs.width_profile_order.clear();
}
Ok(json!({ "deleted": deleted }))
Ok(json!({ "deleted": name }))
}
fn profile_reset(s: &mut Session, _: &Value) -> Result<Value> {
let removed = std::mem::take(&mut s.prefs.width_profiles).len();
s.prefs.width_profile_order.clear();
s.prefs.hidden_width_profiles.clear();
Ok(json!({ "removed": removed }))
}
-5
View File
@@ -378,10 +378,6 @@ pub struct Prefs {
/// Preferences dialog field, so resetting preferences keeps it.
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub width_profile_order: Vec<String>,
/// Built-in non-uniform width profile ids deleted/hidden by the user; Reset Profiles clears
/// this list to restore all built-in presets.
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub hidden_width_profiles: Vec<String>,
/// General → Use Japanese Crop Marks: the style of Create Trim Marks and Effect → Crop Marks.
pub japanese_crop_marks: bool,
/// Appearance panel → New Art Has Basic Appearance (on): new art takes one fill and stroke;
@@ -742,7 +738,6 @@ impl Default for Prefs {
gradient_presets: vec![],
width_profiles: vec![],
width_profile_order: vec![],
hidden_width_profiles: vec![],
japanese_crop_marks: false,
new_art_basic: true,
color_themes: vec![],
+6 -20
View File
@@ -84,7 +84,7 @@ fn add_list_apply_delete_reset() {
}
#[test]
fn uniform_cant_be_deleted_and_built_ins_delete_and_reset() {
fn built_ins_cant_be_deleted_and_names_stay_unique() {
let (mut s, id) = session_with_line();
set_profile(&mut s, id, custom());
for taken in ["lens", "Taper Start", "UNIFORM"] {
@@ -92,30 +92,16 @@ fn uniform_cant_be_deleted_and_built_ins_delete_and_reset() {
}
assert!(s.execute("stroke.widthProfile.add", &json!({"name": " "})).is_err());
s.execute("stroke.widthProfile.add", &json!({"name": "Mine"})).unwrap();
// Uniform cannot be deleted by id or label, nor when selected on the stroke.
s.execute("stroke.set", &json!({"profile": "uniform"})).unwrap();
assert!(s.execute("stroke.widthProfile.delete", &json!({})).is_err());
for u in ["uniform", "Uniform", "UNIFORM"] {
let e = s.execute("stroke.widthProfile.delete", &json!({"name": u})).unwrap_err().to_string();
assert!(e.contains("Uniform"), "{u}: {e}");
for b in ["lens", "Lens", "uniform", "wave"] {
let e = s.execute("stroke.widthProfile.delete", &json!({"name": b})).unwrap_err().to_string();
assert!(e.contains("built in"), "{b}: {e}");
}
// Non-uniform built-in profiles CAN be deleted (by name/id or active selection) and restored on Reset.
assert_eq!(s.execute("stroke.widthProfile.delete", &json!({"name": "Lens"})).unwrap()["deleted"], "lens");
assert!(!s.profile_entries().any(|e| e.id == "lens"));
// With a built-in profile selected, Delete has nothing to delete.
s.execute("stroke.set", &json!({"profile": "pinch"})).unwrap();
assert_eq!(s.execute("stroke.widthProfile.delete", &json!({})).unwrap()["deleted"], "pinch");
assert!(!s.profile_entries().any(|e| e.id == "pinch"));
assert!(s.execute("stroke.widthProfile.delete", &json!({"name": "Lens"})).is_err(), "already deleted");
assert!(s.execute("stroke.widthProfile.delete", &json!({})).is_err());
assert!(s.execute("stroke.widthProfile.delete", &json!({"name": "Other"})).is_err());
assert_eq!(s.next_profile_name(), "Width Profile 1");
assert_eq!(s.execute("stroke.widthProfile.delete", &json!({"name": "Mine"})).unwrap()["deleted"], "Mine");
// Reset restores all deleted built-ins as well.
assert!(s.execute("stroke.widthProfile.reset", &json!({})).is_ok());
assert_eq!(ids(&s.execute("stroke.widthProfile.list", &json!({})).unwrap()), WidthProfile::PRESETS.iter().map(|p| p.id).collect::<Vec<_>>());
}
#[test]
+4 -4
View File
@@ -917,7 +917,7 @@ pub(crate) fn profile_select(app: &mut VectorcraftApp, ui: &mut Ui) {
};
let actions = ProfileActions {
can_add: cur.is_some() && brushed != Some(true) && crate::menus::enabled(app, "stroke.widthProfile.add"),
can_delete: cur.as_ref().and_then(|p| app.session.profile_entry(Some(p))).is_some_and(|e| e.id != "uniform")
can_delete: cur.as_ref().and_then(|p| app.session.profile_entry(Some(p))).is_some_and(|e| !e.built_in)
&& brushed != Some(true)
&& crate::menus::enabled(app, "stroke.widthProfile.delete"),
can_reset: crate::menus::enabled(app, "stroke.widthProfile.reset"),
@@ -990,10 +990,10 @@ pub(crate) fn menu_states(app: &VectorcraftApp, states: &mut CommandStates) {
states.set(CommandRef::new("ui.stroke.options"), CommandState::new().title(options));
let enabled = |id: &str| crate::menus::enabled(app, id);
states.set(add_profile(app), CommandState::new().title(tl!("Add to Profiles…")).enabled(enabled("stroke.widthProfile.add")));
// Deletes the selected stroke's non-Uniform profile.
// Deletes the selected stroke's saved profile.
let shown = current_stroke(app).and_then(|s| s.profile);
let deletable = shown.as_ref().and_then(|p| app.session.profile_entry(Some(p))).is_some_and(|e| e.id != "uniform");
states.set(CommandRef::new("stroke.widthProfile.delete"), CommandState::new().title(tl!("Delete Profile")).enabled(deletable));
let saved = shown.as_ref().and_then(|p| app.session.profile_entry(Some(p))).is_some_and(|e| !e.built_in);
states.set(CommandRef::new("stroke.widthProfile.delete"), CommandState::new().title(tl!("Delete Profile")).enabled(saved));
states.set(
CommandRef::new("stroke.widthProfile.reset"),
CommandState::new().title(tl!("Reset Profiles")).enabled(enabled("stroke.widthProfile.reset")),
@@ -563,7 +563,7 @@ fn show_popup_body(
Stack::new()
.fill_width()
.footer(|ui, _| {
footer_action = show_footer(ui, view.list_id, actions);
footer_action = show_footer(ui, view.list_id, actions, view.saved);
})
.show(ui, |ui, _| {
let saved_for_rename = view.saved.clone();
@@ -625,7 +625,10 @@ fn show_popup_body(
(events, footer_action, menu_action)
}
fn show_footer(ui: &mut egui::Ui, list_id: egui::Id, actions: ProfileActions) -> Option<ProfileAction> {
fn show_footer(ui: &mut egui::Ui, list_id: egui::Id, actions: ProfileActions, saved: &HashSet<String>) -> Option<ProfileAction> {
// Only saved profiles can be deleted; the built-in ones stay in the list.
let droppable = saved.clone();
let is_saved = move |key: &ProfileRowKey| key.profile().is_some_and(|id| droppable.contains(id));
// End-edge placement lays out actions from the right edge inward, so index 0 (`Reset Profiles`)
// sits rightmost, with `Delete Profile` and `Add to Profiles` to its left.
let bar = FooterBar::actions([
@@ -634,7 +637,7 @@ fn show_footer(ui: &mut egui::Ui, list_id: egui::Id, actions: ProfileActions) ->
.enabled(actions.can_delete)
.takes_drops_disabled(true)
.drop_hint(tl!("Delete Profile"))
.accepts_drops::<ListDrag<ProfileRowKey>>(move |drag| drag.list == list_id && drag.keys.iter().any(ProfileRowKey::is_reorderable)),
.accepts_drops::<ListDrag<ProfileRowKey>>(move |drag| drag.list == list_id && drag.keys.iter().any(&is_saved)),
FooterAction::new(IconId::new("dc-new-item"), tl!("Add to Profiles")).enabled(actions.can_add),
])
.show_footer(ui);
@@ -642,7 +645,7 @@ fn show_footer(ui: &mut egui::Ui, list_id: egui::Id, actions: ProfileActions) ->
if let Some(drop) = &bar.dropped
&& drop.index == 1
&& let Some(id) = drop.payload::<ListDrag<ProfileRowKey>>().and_then(|d| d.keys.iter().find_map(ProfileRowKey::profile))
&& id != UNIFORM_ID
&& saved.contains(id)
{
return Some(ProfileAction::Delete(Some(id.to_owned())));
}
@@ -696,7 +699,7 @@ fn show_row_menu(
if is_saved && MenuItem::new(tl!("Rename…")).show(ui).ran {
start_rename = true;
}
if MenuItem::new(tl!("Delete Profile")).show(ui).ran {
if is_saved && MenuItem::new(tl!("Delete Profile")).show(ui).ran {
action = Some(ProfileAction::Delete(Some(target.clone())));
}
});
+2 -10
View File
@@ -342,7 +342,7 @@ fn dropdown_footer_manages_profiles_and_confirms_reset() {
assert!(panel.shows("Add to Profiles"), "dropdown shows Add to Profiles footer button");
assert!(panel.shows("Delete Profile"), "dropdown shows Delete Profile footer button");
assert!(panel.shows("Reset Profiles"), "dropdown shows Reset Profiles footer button");
assert!(panel.enabled("Delete Profile"), "non-Uniform built-in profile can be deleted");
assert!(!panel.enabled("Delete Profile"), "a built-in profile can't be deleted");
assert!(
panel.rect("Reset Profiles").left() >= panel.rect("Delete Profile").right() - 0.5
&& panel.rect("Delete Profile").left() >= panel.rect("Add to Profiles").right() - 0.5,
@@ -355,13 +355,6 @@ fn dropdown_footer_manages_profiles_and_confirms_reset() {
panel.click_role("Profile", Role::COMBO_BOX);
assert!(!panel.enabled("Delete Profile"), "Uniform profile cannot be deleted");
// Non-Uniform built-in ("Taper Start") can be deleted directly from the dropdown footer.
panel.click("Taper Start");
panel.click_role("Profile", Role::COMBO_BOX);
assert!(panel.enabled("Delete Profile"));
panel.click("Delete Profile");
assert!(!panel.app().session.profile_entries().any(|e| e.id == "taperStart"), "built-in preset was deleted");
// Custom profile: clicking Add to Profiles saves immediately without opening a name dialog,
// and user-added rows hide their text captions in the dropdown.
panel.app().session.execute("stroke.widthPoint.set", &serde_json::json!({"id": id, "t": 0.35, "left": 12.0, "right": 3.0})).unwrap();
@@ -412,7 +405,7 @@ fn dropdown_footer_manages_profiles_and_confirms_reset() {
assert!(app.session.profile_entries().any(|e| !e.built_in), "cancelling reset preserves profiles");
}
// Confirming OK executes the reset and restores deleted built-ins.
// Confirming OK executes the reset.
panel.click_role("Profile", Role::COMBO_BOX);
panel.click("Reset Profiles");
let mut app = panel.app();
@@ -423,7 +416,6 @@ fn dropdown_footer_manages_profiles_and_confirms_reset() {
});
crate::dialogs::confirm::pump(&mut app, &ctx);
assert!(app.session.profile_entries().all(|e| e.built_in), "Reset Profiles clears saved presets");
assert!(app.session.profile_entries().any(|e| e.id == "taperStart"), "Reset Profiles restores deleted built-ins");
}
#[test]