mirror of
https://github.com/storytold/materialcraft.git
synced 2026-11-28 00:59:32 +01:00
Keep typed graph name and attribute text across frames (#124)
* Keep typed graph name and attribute text across frames Typing a graph name or attribute currently loses the text between frames, so the edit never reaches the engine command on focus loss. Persisting the draft across frames, as the slider and preset name already do, makes the edit commit correctly. Store the in-progress graph name and attribute text in temporary UI data while editing, like presets_bar does, and commit the stored text on lost_focus. Add a harness test that types a new graph name across several frames, moves focus away, and asserts the renamed graph. Test: graph_name_typed_across_frames_commits_on_focus_loss failed before the change and passes after. * Graph properties: a draft field's id salt is a string Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> --------- Co-authored-by: eliets2 <287273517+eliets2@users.noreply.github.com> Co-authored-by: FredApps <153173854+FredApps@users.noreply.github.com> Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2 files changed
+46
-8
No files matched your search
@@ -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<String> {
|
||||
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}));
|
||||
}
|
||||
});
|
||||
|
||||
@@ -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);
|
||||
|
||||
Reference in new issue
Block a user