ui: Rename… in a definition's item menu; the selection follows the new name (#290)

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01UhhZMBTviDQxZvwnyP89fo
This commit is contained in:
jyvikoandClaude Opus 5.5 authored and Irakli Kourtis committed 2026-10-15 10:17:28 -04:00
1 parent a0ae8b2111
commit dcf03e3378
5 files changed
+68 -3

No files matched your search

+20
View File
@@ -190,6 +190,26 @@ impl Def {
Def::Partition,
];
/// The kind `model.rename` takes for it; `None` for a partition (its cut faces are named
/// after it).
pub fn rename_kind(self) -> Option<&'static str> {
Some(match self {
Def::Set => "set",
Def::Material => "material",
Def::Section => "section",
Def::Step => "step",
Def::BoundaryCondition => "boundary_condition",
Def::Load => "load",
Def::ReferencePoint => "reference_point",
Def::Coupling => "coupling",
Def::Contact => "contact",
Def::Tie => "tie",
Def::FluidDomain => "fluid_domain",
Def::Datum => "datum",
Def::Partition => return None,
})
}
/// The kind of setup symbols a definition of this kind draws, if any.
pub fn symbols(self) -> Option<fieldcraft_render::glyphs::Definition> {
use fieldcraft_render::glyphs::Definition;
+4 -1
View File
@@ -383,6 +383,7 @@ pub fn handle(app: &mut ModelApp, ctx: &egui::Context, req: &ControlRequest) ->
};
let action = match (s("action"), s("step")) {
(Some("edit"), _) => crate::menu::Action::Edit,
(Some("rename"), _) => crate::menu::Action::Rename,
(Some("hide"), _) => crate::menu::Action::Hide,
(Some("show"), _) => crate::menu::Action::Show,
(Some("isolate"), _) => crate::menu::Action::Isolate,
@@ -391,7 +392,9 @@ pub fn handle(app: &mut ModelApp, ctx: &egui::Context, req: &ControlRequest) ->
(Some(a @ ("activate" | "deactivate")), Some(step)) => {
crate::menu::Action::Active { step: step.to_string(), active: a == "activate" }
}
(a, _) => return err(format!("`action` {a:?}: edit, hide, show, isolate, show_all, delete, or activate/deactivate with `step`")),
(a, _) => {
return err(format!("`action` {a:?}: edit, rename, hide, show, isolate, show_all, delete, or activate/deactivate with `step`"));
}
};
crate::menu::apply(app, target, action);
ok(json!({"status": app.status, "prompt": app.prompt.is_some(), "dialog": app.dialog.as_ref().map(|d| d.command)}))
+10
View File
@@ -601,6 +601,16 @@ impl ModelApp {
e => Failure { message: e.to_string(), refused: None },
});
let changed = self.session.revision() != revision;
// The definition selected in the browser keeps its selection under its new name.
if id == "model.rename"
&& let Ok(v) = &r
&& let Some((def, name)) = &mut self.selected
&& def.rename_kind() == v["kind"].as_str()
&& v["from"].as_str() == Some(name.as_str())
&& let Some(to) = v["name"].as_str()
{
*name = to.to_string();
}
// A view change (hide, show) draws the model again, as an edit does.
if changed || self.session.view != view {
self.model_changed();
+33 -1
View File
@@ -1,7 +1,8 @@
//! The item menu: what a right click on a browser row offers, and on a datum or a partition's cut
//! face in the viewport. Edit… opens the item's edit form, Hide/Show and Isolate change the view
//! (`view.*`), Active in step switches a boundary condition or load on or off per step
//! (`model.step.activate`), and Delete… asks first (a seed's Delete clears it at once: undo brings
//! (`model.step.activate`), Rename… opens `model.rename` (every reference follows the new
//! name), and Delete… asks first (a seed's Delete clears it at once: undo brings
//! it back) ([`crate::files::Prompt::Delete`]): it lists
//! the definitions that use the item, and refuses, or for a datum deletes its partitions with it.
//! Every entry runs the same command as the ribbon, the forms and scripts.
@@ -42,6 +43,8 @@ impl Target {
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum Action {
Edit,
/// Rename a definition (`model.rename`'s form, filled in).
Rename,
Hide,
Show,
Isolate,
@@ -76,6 +79,9 @@ pub fn show(app: &ModelApp, ui: &mut egui::Ui, target: &Target) -> Option<Action
let edit = def.edit_command(&app.session.model, name);
let tip = edit.clone().unwrap_or_else(|| "Not editable: delete it and make it again".to_string());
entry(ui, edit.is_some(), "Edit…", &tip, Action::Edit);
if def.rename_kind().is_some() {
entry(ui, true, "Rename…", "model.rename: every definition that refers to it follows", Action::Rename);
}
}
if let Target::Seed(_) = target {
entry(ui, true, "Edit…", "mesh.seed.edge", Action::Edit);
@@ -142,6 +148,15 @@ pub fn apply(app: &mut ModelApp, target: Target, action: Action) {
}
Ok(Value::Null)
}
Action::Rename => {
if let Target::Def(def, name) = &target
&& let Some(kind) = def.rename_kind()
{
app.select_definition(Some((*def, name.clone())));
app.open_form("model.rename", json!({"kind": kind, "from": name, "to": name}));
}
Ok(Value::Null)
}
Action::Hide => app.run("view.hide", items(&target)),
Action::Show => app.run("view.show", items(&target)),
Action::Isolate => app.run("view.isolate", items(&target)),
@@ -280,6 +295,23 @@ mod tests {
let _ = std::fs::remove_dir_all(dir);
}
/// Rename… opens `model.rename` filled in; running it renames the datum, its partition
/// follows and the browser keeps it selected under the new name (#290).
#[test]
fn rename_from_the_menu_follows_the_selection() {
let (mut app, dir) = plate();
apply(&mut app, Target::Def(Def::Datum, "Mid".into()), Action::Rename);
assert_eq!(app.dialog.as_ref().map(|d| d.command), Some("model.rename"));
let params: Value = serde_json::from_str(&app.dialog.as_ref().unwrap().text).unwrap();
assert_eq!((params["kind"].as_str(), params["from"].as_str()), (Some("datum"), Some("Mid")));
app.dialog = None;
app.run("model.rename", json!({"kind": "datum", "from": "Mid", "to": "Centre"})).unwrap();
assert_eq!(app.selected, Some((Def::Datum, "Centre".to_string())));
assert_eq!(app.session.model.partitions()[0].datum, "Centre");
assert!(Def::Partition.rename_kind().is_none());
let _ = std::fs::remove_dir_all(dir);
}
/// A screen point on datum `name`'s drawn lines.
fn on_datum(app: &mut ModelApp, name: &str) -> egui::Pos2 {
let bounds = app.display().bounds;