Browser tree: double-click a row to rename it (#4460)
This commit is contained in:
@@ -2010,6 +2010,42 @@ fn folder_rows_at(app: &mut SolveApp, ui: &mut egui::Ui, comp: u64, path: Option
|
||||
let _ = t;
|
||||
}
|
||||
|
||||
/// The body's name edited in its row (started by a double-click): Enter or leaving the box keeps
|
||||
/// the name, Escape cancels.
|
||||
fn inline_rename(app: &mut SolveApp, ui: &mut egui::Ui, row: Rect, e: &Entry, depth: usize) {
|
||||
let editing = app.menu.rename.as_ref().is_some_and(|r| r.inline && matches!(&r.what, crate::context_menu::RenameWhat::Body(b) if *b == e.key));
|
||||
if !editing {
|
||||
return;
|
||||
}
|
||||
let Some(mut rename) = app.menu.rename.take() else { return };
|
||||
// Where the label starts: the fold arrow, eye and icon come first.
|
||||
let left = row.left() + 2.0 + depth as f32 * INDENT + 14.0 + 18.0 + 21.0;
|
||||
let edit_rect = Rect::from_min_max(pos2(left, row.top() + 1.0), pos2(row.right() - 3.0, row.bottom() - 1.0));
|
||||
ui.painter().rect_filled(edit_rect, 3.0, Tokens::get().field);
|
||||
let id = ui.id().with(("browser_rename", &e.row_key));
|
||||
let response = crate::ui_kit::inline_name(ui, edit_rect, id, &mut rename.text);
|
||||
crate::scenario::publish_handle(&format!("rename:{}", e.key), edit_rect.center());
|
||||
let was_focused = rename.focused;
|
||||
if !rename.focused {
|
||||
if let Some(mut state) = egui::TextEdit::load_state(ui.ctx(), id) {
|
||||
let end = rename.text.chars().count();
|
||||
state.cursor.set_char_range(Some(egui::text::CCursorRange::two(egui::text::CCursor::new(0), egui::text::CCursor::new(end))));
|
||||
state.store(ui.ctx(), id);
|
||||
}
|
||||
response.request_focus();
|
||||
rename.focused = true;
|
||||
}
|
||||
let escape = ui.input(|i| i.key_pressed(egui::Key::Escape));
|
||||
let commit = ui.input(|i| i.key_pressed(egui::Key::Enter)) || (was_focused && response.lost_focus());
|
||||
if escape {
|
||||
// Dropping the state cancels the edit.
|
||||
} else if commit {
|
||||
crate::context_menu::commit_rename(app, &rename);
|
||||
} else {
|
||||
app.menu.rename = Some(rename);
|
||||
}
|
||||
}
|
||||
|
||||
/// One item row of a folder.
|
||||
#[allow(clippy::too_many_arguments)]
|
||||
fn entry_row(
|
||||
@@ -2079,6 +2115,22 @@ fn entry_row(
|
||||
} else {
|
||||
click_select(app, ui, e.row_key.clone(), e.sel.clone());
|
||||
}
|
||||
// A double-click renames the row (sketch rows open for editing instead), like F2. A body
|
||||
// is edited in its own row.
|
||||
if r.double {
|
||||
crate::context_menu::rename_selection(app, r.rect.map(|x| x.left_bottom()).unwrap_or_default());
|
||||
if folder == "bodies"
|
||||
&& let Some(rename) = app.menu.rename.as_mut()
|
||||
&& matches!(&rename.what, crate::context_menu::RenameWhat::Body(b) if *b == e.key)
|
||||
{
|
||||
rename.inline = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
if folder == "bodies"
|
||||
&& let Some(rect) = r.rect
|
||||
{
|
||||
inline_rename(app, ui, rect, e, depth);
|
||||
}
|
||||
if r.hovered && !e.bodies.is_empty() {
|
||||
acts.hover_bodies = e.bodies.clone();
|
||||
|
||||
@@ -247,6 +247,8 @@ pub struct Rename {
|
||||
pub text: String,
|
||||
pub at: Pos2,
|
||||
pub(crate) focused: bool,
|
||||
/// Edited in the browser row itself (a double-click on a body), not in the floating box.
|
||||
pub(crate) inline: bool,
|
||||
}
|
||||
|
||||
/// Radial slots, clockwise from the top.
|
||||
@@ -1497,7 +1499,7 @@ pub fn start_rename(app: &mut SolveApp, what: RenameWhat, at: Pos2) {
|
||||
RenameWhat::View(v) => v.clone(),
|
||||
RenameWhat::Joint(j) => doc.assembly.joints.iter().find(|x| x.id == *j).map(|x| x.name.clone()).unwrap_or_default(),
|
||||
};
|
||||
app.menu.rename = Some(Rename { what, text, at, focused: false });
|
||||
app.menu.rename = Some(Rename { what, text, at, focused: false, inline: false });
|
||||
}
|
||||
|
||||
pub(crate) fn commit_rename(app: &mut SolveApp, r: &Rename) {
|
||||
@@ -1969,6 +1971,9 @@ fn show_rename(app: &mut SolveApp, ctx: &egui::Context) {
|
||||
if app.preferences.timeline_left && matches!(app.menu.rename.as_ref().map(|rename| &rename.what), Some(RenameWhat::TimelineGroup(_))) {
|
||||
return;
|
||||
}
|
||||
if app.menu.rename.as_ref().is_some_and(|r| r.inline) {
|
||||
return;
|
||||
}
|
||||
let Some(mut r) = app.menu.rename.take() else { return };
|
||||
let mut done: Option<bool> = None;
|
||||
let resp =
|
||||
|
||||
@@ -773,6 +773,35 @@ fn f2_renames_the_selected_item() {
|
||||
assert!(!context_menu::rename_selection(&mut app, pos2(0.0, 0.0)), "nothing selected");
|
||||
}
|
||||
|
||||
/// A double-click on a body's browser row edits its name in the row, like F2 (no floating box);
|
||||
/// Escape cancels, and a second double-click then commits a new name.
|
||||
#[test]
|
||||
fn double_click_on_a_body_row_renames_it_in_the_row() {
|
||||
let mut h = crate::scenario::Harness::new();
|
||||
h.app.run("solid.box", json!({"length": 40, "width": 40, "height": 10, "body_name": "Block"})).unwrap();
|
||||
h.app.run("select.clear", json!({})).unwrap();
|
||||
h.frames(3);
|
||||
let at = crate::scenario::handle_at("row:Block").expect("the body has a browser row");
|
||||
h.call("ui.click", json!({"x": at.x, "y": at.y, "double": true}));
|
||||
h.frames(3);
|
||||
let rename = h.app.menu.rename.as_ref().expect("a double-click starts the rename");
|
||||
assert!(rename.inline, "the name is edited in its row");
|
||||
assert!(matches!(&rename.what, context_menu::RenameWhat::Body(b) if b == "Block"));
|
||||
assert!(crate::scenario::handle_at("rename:Block").is_some(), "the row shows its name editor");
|
||||
// Escape leaves the name alone.
|
||||
h.step(&json!({"key": "Escape"})).unwrap();
|
||||
h.frames(2);
|
||||
assert!(h.app.menu.rename.is_none());
|
||||
assert!(h.app.session.model.state().body("Block").is_some());
|
||||
// Past egui's double-click window, so this is a new double-click and not a triple.
|
||||
h.frames(40);
|
||||
let at = crate::scenario::handle_at("row:Block").expect("the row is still there");
|
||||
h.call("ui.click", json!({"x": at.x, "y": at.y, "double": true}));
|
||||
h.frames(3);
|
||||
assert!(context_menu::finish_rename(&mut h.app, Some("Base"), true));
|
||||
assert!(h.app.session.model.state().body("Base").is_some());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn occurrence_moves_turn_about_their_origin_then_move() {
|
||||
let mut app = sample_app();
|
||||
|
||||
Reference in New Issue
Block a user