Layer rename: focus the field until it has it, and test the editor in the UI

Request focus only while the field has none, so it can lose focus to Enter or a
click elsewhere; this replaces the per-layer temp flag and the second focus
request on double-click. UI tests drive the layer stack offscreen: Enter and a
click elsewhere rename, Escape cancels, and Ctrl+Z undoes the rename afterwards
(all three fail on main).
This commit is contained in:
bflatastic committed 2026-11-02 02:59:38 +07:00
1 parent 4a49019d8d
commit 94e288a944
2 files changed
+78 -25

No files matched your search

+10 -25
View File
@@ -382,35 +382,22 @@ fn layer_row(app: &mut TextureCraftApp, ui: &mut Ui, layer: &Layer, depth: usize
if renaming {
let mut child = ui.new_child(egui::UiBuilder::new().max_rect(name_rect).layout(egui::Layout::left_to_right(egui::Align::Center)));
if let Some((_, buf)) = app.ui.rename.as_mut() {
let edit_id = ui.scope_id().with(("layer_rename", layer.id.0));
let edit = egui::TextEdit::singleline(buf)
.id(edit_id)
.desired_width(name_rect.width())
.event_filter(egui::EventFilter {
horizontal_arrows: true,
vertical_arrows: true,
escape: true,
..Default::default()
});
let r = child.add(edit);
if ui.data(|d| d.get_temp::<bool>(edit_id)).unwrap_or(true) {
ui.data_mut(|d| d.insert_temp(edit_id, false));
if !r.has_focus() {
r.request_focus();
}
}
let enter = ui.input(|i| i.key_pressed(egui::Key::Enter));
let esc = ui.input(|i| i.key_pressed(egui::Key::Escape));
if esc {
ui.data_mut(|d| d.remove::<bool>(edit_id));
// Escape reaches us instead of just unfocusing the field, so it can cancel.
let filter = egui::EventFilter { horizontal_arrows: true, vertical_arrows: true, escape: true, ..Default::default() };
let r = child.add(egui::TextEdit::singleline(buf).desired_width(name_rect.width()).event_filter(filter));
if ui.input(|i| i.key_pressed(egui::Key::Escape)) {
app.ui.rename = None;
} else if enter || r.lost_focus() {
ui.data_mut(|d| d.remove::<bool>(edit_id));
} else if r.lost_focus() {
// Enter or a click elsewhere.
let name = buf.trim().to_string();
app.ui.rename = None;
if !name.is_empty() && name != layer.name {
app.run("layer.rename", json!({"layer": layer.id.0, "name": name}));
}
} else if !r.has_focus() {
// Only until the field has focus: requesting it every frame kept it from ever
// losing focus, which trapped the editor open and blocked every shortcut.
r.request_focus();
}
}
} else {
@@ -464,9 +451,7 @@ fn layer_row(app: &mut TextureCraftApp, ui: &mut Ui, layer: &Layer, depth: usize
app.run("layer.select", json!({"layer": layer.id.0}));
}
if resp.double_clicked() && !eye_resp.clicked() && !renaming {
let edit_id = ui.scope_id().with(("layer_rename", layer.id.0));
app.ui.rename = Some((layer.id.0, layer.name.clone()));
ui.ctx().memory_mut(|m| m.request_focus(edit_id));
}
if resp.drag_started() && !renaming {
resp.dnd_set_drag_payload(layer.id.0);
+68
View File
@@ -0,0 +1,68 @@
//! Renaming a layer in the stack: Enter, a click elsewhere or Escape end the editor, and the
//! shortcuts (Undo) work again afterwards.
use egui::accesskit::Role;
use egui::{Key, Modifiers};
use egui_kittest::Harness;
use egui_kittest::kittest::Queryable;
use texturecraft_ui_egui::TextureCraftApp;
fn harness() -> (Harness<'static, TextureCraftApp>, u64) {
let session = texturecraft_engine::sample_session(256, 64).unwrap();
let mut h = Harness::builder().with_size(egui::vec2(1600.0, 960.0)).build_eframe(|cc| TextureCraftApp::new(cc, session));
h.run_steps(4);
let layer = h.state().session.selected_layer().unwrap().0;
(h, layer)
}
fn name(h: &Harness<'_, TextureCraftApp>, layer: u64) -> String {
h.state().session.doc.project.layers.values().find(|l| l.id.0 == layer).unwrap().name.clone()
}
/// Open the editor on `layer`, select its text and type `text`.
fn start(h: &mut Harness<'_, TextureCraftApp>, layer: u64, text: &str) {
h.state_mut().ui.rename = Some((layer, name(h, layer)));
h.run_steps(3);
h.key_press_modifiers(Modifiers::COMMAND, Key::A);
h.event(egui::Event::Text(text.into()));
h.run_steps(3);
}
#[test]
fn enter_renames_and_undo_works_afterwards() {
let (mut h, layer) = harness();
let before = name(&h, layer);
start(&mut h, layer, "Rust streaks");
h.key_press(Key::Enter);
h.run_steps(3);
assert!(h.state().ui.rename.is_none(), "Enter closes the editor");
assert_eq!(name(&h, layer), "Rust streaks");
h.key_press_modifiers(Modifiers::COMMAND, Key::Z);
h.run_steps(3);
assert_eq!(name(&h, layer), before, "Ctrl+Z undoes the rename");
}
#[test]
fn a_click_elsewhere_renames() {
let (mut h, layer) = harness();
start(&mut h, layer, "Clicked away");
let edit = h.get_by(|n| n.role() == Role::TextInput && n.value().as_deref() == Some("Clicked away")).rect();
// Below the editor's row, in the layer stack.
let away = egui::pos2(edit.center().x, edit.bottom() + 120.0);
h.drag_at(away);
h.drop_at(away);
h.run_steps(3);
assert!(h.state().ui.rename.is_none(), "a click elsewhere closes the editor");
assert_eq!(name(&h, layer), "Clicked away");
}
#[test]
fn escape_cancels() {
let (mut h, layer) = harness();
let before = name(&h, layer);
start(&mut h, layer, "Never applied");
h.key_press(Key::Escape);
h.run_steps(3);
assert!(h.state().ui.rename.is_none(), "Escape closes the editor");
assert_eq!(name(&h, layer), before);
}