diff --git a/crates/ui-egui/src/property/graph.rs b/crates/ui-egui/src/property/graph.rs index 166b627b..8613cdb5 100644 --- a/crates/ui-egui/src/property/graph.rs +++ b/crates/ui-egui/src/property/graph.rs @@ -5,8 +5,26 @@ use egui_phosphor::regular as icon; use materialcraft_engine::core::{Graph, OutputFormat, Resource, Value}; use serde_json::{Value as JsonValue, json}; +/// A one-line field for a value the engine holds: what is typed stays between frames, and the text +/// comes back when the field loses focus with something new in it. A change to the value from +/// elsewhere (undo) drops the draft. +fn draft_field(ui: &mut Ui, salt: String, current: &str, label: &str) -> Option { + let id = ui.make_persistent_id(salt); + let draft: Option<(String, String)> = ui.data(|data| data.get_temp(id)); + let mut text = draft.filter(|(seen, _)| seen == current).map_or_else(|| current.to_string(), |(_, text)| text); + let response = ui.add(egui::TextEdit::singleline(&mut text).desired_width(ui.available_width())); + crate::widgets::name(&response, egui::Role::TextInput, label); + if response.has_focus() { + ui.data_mut(|data| data.insert_temp(id, (current.to_string(), text.clone()))); + } else { + ui.data_mut(|data| data.remove_temp::<(String, String)>(id)); + } + (response.lost_focus() && text != current).then_some(text) +} + pub(super) fn show(app: &mut MaterialCraftApp, ui: &mut Ui) { let tokens = Tokens::get(ui.ctx()); + let graph_index = app.session.graph; let Ok(graph) = app.session.graph() else { ui.label(tr("No graph is open")); return; @@ -17,10 +35,7 @@ pub(super) fn show(app: &mut MaterialCraftApp, ui: &mut Ui) { section(ui, "graph-base", tr("Base parameters"), |ui| { ui.horizontal(|ui| { ui.label(tr("Name")); - let mut name = graph.name.clone(); - let response = ui.add(egui::TextEdit::singleline(&mut name).desired_width(ui.available_width())); - crate::widgets::name(&response, egui::Role::TextInput, "Graph name"); - if response.lost_focus() && name != graph.name { + if let Some(name) = draft_field(ui, format!("graph-name-{graph_index}"), &graph.name, "Graph name") { emit("graph.rename", json!({"name": name})); } }); @@ -57,10 +72,7 @@ pub(super) fn show(app: &mut MaterialCraftApp, ui: &mut Ui) { ] { ui.horizontal(|ui| { ui.label(tr(label)); - let mut edited = current.clone(); - let response = ui.add(egui::TextEdit::singleline(&mut edited).desired_width(ui.available_width())); - crate::widgets::name(&response, egui::Role::TextInput, label); - if response.lost_focus() && edited != *current { + if let Some(edited) = draft_field(ui, format!("graph-attribute-{graph_index}-{key}"), current, label) { emit("graph.attributes", json!({key: edited})); } }); diff --git a/crates/ui-egui/tests/numeric_input.rs b/crates/ui-egui/tests/numeric_input.rs index 5679841d..df84d97e 100644 --- a/crates/ui-egui/tests/numeric_input.rs +++ b/crates/ui-egui/tests/numeric_input.rs @@ -47,6 +47,32 @@ fn pair(h: &Harness<'static, MaterialCraftApp>, id: NodeId, param: &str) -> [f32 } } #[test] +fn graph_name_typed_across_frames_commits_on_focus_loss() { + let mut app = MaterialCraftApp::headless(materialcraft_engine::sample_session(), UiState::default()); + app.run("file.new", json!({"template":"empty"})).unwrap(); + app.session.reset_history(); + let builder = Harness::builder().with_size(vec2(1600.0, 960.0)).with_step_dt(1.0 / 60.0); + let mut h = builder.build_ui_state( + |ui, app: &mut MaterialCraftApp| { + app.step(&ui.ctx().clone()); + app.draw(ui); + }, + app, + ); + h.run_steps(8); + h.state_mut().run("graph.select", json!({"ids": []})).unwrap(); + h.run_steps(3); + h.get_by_label("Graph name").click(); + h.step(); + h.key_press_modifiers(Modifiers::COMMAND, Key::A); + h.step(); + type_text(&mut h, "My_graph"); + h.run_steps(2); + h.key_press(Key::Enter); + h.run_steps(4); + assert_eq!(h.state().session.graph().unwrap().name, "My_graph", "graph name typed across frames was reset before focus was lost"); +} +#[test] fn offset_x_commits_on_enter_and_focus_loss_with_undo() { for enter in [true, false] { let (mut h, id) = harness(false);