diff --git a/crates/engine/src/project_cmds.rs b/crates/engine/src/project_cmds.rs index 33432cda..c5da6d34 100644 --- a/crates/engine/src/project_cmds.rs +++ b/crates/engine/src/project_cmds.rs @@ -9,6 +9,9 @@ use crate::commands::{CommandSpec, always, bad, boolp, strp, u64p}; use crate::materials::Place; use crate::{Result, Session, cmd}; +/// Longest project name, in characters; it goes into export file names. +pub const MAX_PROJECT_NAME_CHARS: usize = 128; + pub fn specs() -> Vec { vec![ cmd!( @@ -22,6 +25,15 @@ pub fn specs() -> Vec { ), cmd!("project.sample", "Open sample project", ["File"], None, "{}", always, sample), cmd!("project.inspect", "Inspect project", [], None, "{}", always, |s, _| Ok(crate::inspect::session(s))), + cmd!( + "project.rename", + "Rename project", + [], + None, + "{name: 1-128 characters without control characters} — the name `$project` stands for in export file names; the project file keeps its own name", + always, + rename + ), cmd!( "material.apply", "Add smart material", @@ -51,6 +63,23 @@ fn new(s: &mut Session, p: &Value) -> Result { Ok(crate::inspect::session(s)) } +fn rename(s: &mut Session, p: &Value) -> Result { + let name = strp(p, "name") + .map(str::trim) + .filter(|name| !name.is_empty() && name.chars().count() <= MAX_PROJECT_NAME_CHARS && !name.chars().any(char::is_control)) + .ok_or_else(|| bad("project.rename", format!("`name` must be 1-{MAX_PROJECT_NAME_CHARS} characters without control characters")))?; + + // The same name is not an undo step and leaves a saved project unmodified + if name != s.doc.project.name { + s.edit("Rename project", |s| { + s.doc.project.name = name.to_string(); + Ok(()) + })?; + } + + Ok(json!({ "name": name })) +} + fn material(s: &mut Session, p: &Value) -> Result { let id = strp(p, "material").ok_or_else(|| bad("material.apply", "`material` is required"))?; let l = crate::materials::apply_at(s, id, Place::from_params(p))?; diff --git a/crates/engine/tests/it/main.rs b/crates/engine/tests/it/main.rs index 11cfaf41..23517f4a 100644 --- a/crates/engine/tests/it/main.rs +++ b/crates/engine/tests/it/main.rs @@ -63,6 +63,7 @@ mod perf; mod polygon_fill; mod project_recovery; mod rebuilt_uv_layout; +mod rename_project; mod resources_used; mod sample; mod solo; diff --git a/crates/engine/tests/it/rename_project.rs b/crates/engine/tests/it/rename_project.rs new file mode 100644 index 00000000..5510163b --- /dev/null +++ b/crates/engine/tests/it/rename_project.rs @@ -0,0 +1,34 @@ +//! `project.rename`: the name `$project` stands for in export file names. + +use serde_json::json; +use texturecraft_engine::{Session, execute}; + +use texturecraft_engine::geom::primitives; + +#[test] +fn a_renamed_project_exports_under_its_new_name_until_undone() { + let mut session = Session::new_project("hero", primitives::cube(2.0), "builtin:cube", None, 128).unwrap(); + let mut preset = execute(&mut session, "export.preset.get", &json!({"name": "Document channels + Normal + AO (No Alpha)"})).unwrap(); + preset["maps"][0]["file"] = json!("$project_$textureSet"); + + execute(&mut session, "project.rename", &json!({"name": " Knight final "})).unwrap(); + + let plan = execute(&mut session, "export.plan", &json!({"preset": preset})).unwrap().to_string(); + assert!(plan.contains("Knight_final_") && !plan.contains("hero_"), "{plan}"); + + execute(&mut session, "edit.undo", &json!({})).unwrap(); + assert_eq!(session.doc.project.name, "hero"); +} + +#[test] +fn an_unusable_or_unchanged_project_name_edits_nothing() { + let mut session = Session::new_project("hero", primitives::cube(2.0), "builtin:cube", None, 128).unwrap(); + for name in [json!(null), json!(7), json!(" "), json!("a\nb"), json!("x".repeat(129))] { + assert!(execute(&mut session, "project.rename", &json!({"name": name.clone()})).is_err(), "{name}"); + } + + execute(&mut session, "project.rename", &json!({"name": "hero"})).unwrap(); + + assert_eq!(session.doc.project.name, "hero"); + assert!(!session.modified, "nothing changed"); +} diff --git a/crates/ui-egui/src/project_config.rs b/crates/ui-egui/src/project_config.rs index a399aa4e..cb887ca1 100644 --- a/crates/ui-egui/src/project_config.rs +++ b/crates/ui-egui/src/project_config.rs @@ -8,6 +8,7 @@ use std::sync::atomic::{AtomicU64, Ordering}; use egui::{Key, Modifiers}; use serde_json::{Value, json}; use texturecraft_engine::core::NormalFormat; +use texturecraft_engine::project_cmds::MAX_PROJECT_NAME_CHARS; use crate::TextureCraftApp; use crate::dock::info_icon; @@ -44,6 +45,7 @@ pub(crate) fn open(app: &mut TextureCraftApp) { mesh: None, reimport: false, preserve: true, + name: None, normal_format: None, color_management: crate::project_form::ColorManagement::Legacy, confirm: false, @@ -211,6 +213,18 @@ pub(crate) fn show(app: &mut TextureCraftApp, ctx: &egui::Context) { ui.add_space(12.0); ui.label(caption(tr("PROJECT SETTINGS"))); ui.add_space(4.0); + ui.horizontal(|ui| { + ui.add_space(ROW_INDENT_POINTS); + let label = ui.label(tr("Project name")).id; + ui.with_layout(egui::Layout::right_to_left(egui::Align::Center), |ui| { + info_icon(ui, tr("The name $project stands for in export file names. The project file keeps its own name.")); + let mut name = config.name.clone().unwrap_or_else(|| project.name.clone()); + let field = egui::TextEdit::singleline(&mut name).char_limit(MAX_PROJECT_NAME_CHARS).desired_width(FIELD_WIDTH_POINTS); + if ui.add(field).labelled_by(label).changed() { + config.name = Some(name); + } + }); + }); let shown_format = config.normal_format.unwrap_or(current_normal_format); row( ui, @@ -328,6 +342,11 @@ pub(crate) fn show(app: &mut TextureCraftApp, ctx: &egui::Context) { } None => true, } + // An untouched name is left alone, even one longer than a typed name may be + && match config.name.as_deref() { + Some(name) => app.run("project.rename", json!({"name": name})).is_some(), + None => true, + } }; if !done && !cancel && open { diff --git a/crates/ui-egui/src/state/project_config.rs b/crates/ui-egui/src/state/project_config.rs index 174b0987..6c7eb95c 100644 --- a/crates/ui-egui/src/state/project_config.rs +++ b/crates/ui-egui/src/state/project_config.rs @@ -7,6 +7,8 @@ pub struct ProjectConfig { pub reimport: bool, /// Whether painted strokes follow the surface of a newly imported mesh pub preserve: bool, + /// The project name as typed, when the field was edited + pub name: Option, /// The selected normal map format when it differs from the project's pub normal_format: Option, /// Color management as chosen in the window; nothing is applied (the project stays Legacy) diff --git a/crates/ui-egui/tests/it/project_config.rs b/crates/ui-egui/tests/it/project_config.rs index a44c4640..700ae461 100644 --- a/crates/ui-egui/tests/it/project_config.rs +++ b/crates/ui-egui/tests/it/project_config.rs @@ -765,3 +765,24 @@ fn project_config_snapshots() { h.run_steps(6); h.render().unwrap().save(output_dir.join("project-config-ocio.png")).unwrap(); } + +#[test] +fn a_typed_project_name_applies_only_with_ok() { + let dir = temp_dir("project-name"); + let mut h = app_on_a_quad(&dir); + let before = h.state().session.doc.project.name.clone(); + + for button in ["Cancel", "OK"] { + open_window(&mut h); + in_window(&h, Role::TextInput, "Project name").focus(); + h.run_steps(2); + h.key_press_modifiers(egui::Modifiers::COMMAND, egui::Key::A); + in_window(&h, Role::TextInput, "Project name").type_text("Knight"); + h.run_steps(2); + click_in_window(&mut h, Role::Button, button); + + let expected = if button == "OK" { "Knight" } else { before.as_str() }; + assert_eq!(h.state().session.doc.project.name, expected, "after {button}"); + assert!(!window_is_open(&h), "{button} closes the window"); + } +}