From e3415d38889b1469534e1ee16b72f8d72c9873e8 Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Thu, 3 Dec 2026 06:50:59 -0800 Subject: [PATCH 1/2] Variables: an unfinished Data Set name stays with its document and data set The Data Set box renamed the current data set while the name was still being typed: the panel ran dataset.rename for every value the field reported, not only a committed one. Typing a name and then switching to another document left the first document's data set renamed, without Enter. The box now renames only when the name is committed (Enter or leaving the box). Its edit target is the document's uid, how many times File > Revert replaced its content, and the current data set's name, so a name typed and not yet committed is dropped when another document, a reverted one or a data set with another name becomes current. --- crates/ui-egui/src/panels/variables.rs | 117 +++++++++++++++++++++++-- 1 file changed, 112 insertions(+), 5 deletions(-) diff --git a/crates/ui-egui/src/panels/variables.rs b/crates/ui-egui/src/panels/variables.rs index 30a840d7b9..5214799496 100644 --- a/crates/ui-egui/src/panels/variables.rs +++ b/crates/ui-egui/src/panels/variables.rs @@ -433,9 +433,10 @@ pub fn show(app: &mut VectorcraftApp, ui: &mut Ui) { let states = row_states(st); let sort = st.variables_sort; let doc_uid = st.uid; + let reverts = st.reverts; let (mut acts, mut pressed) = (Vec::new(), None); Stack::new().fill().footer(|ui, _| pressed = footer(ui, &states)).show(ui, |ui, _| { - data_set_bar(ui, &rows.sets, &mut acts); + data_set_bar(ui, &rows.sets, (doc_uid, reverts), &mut acts); variable_table(ui, &rows.vars, sort, &states, doc_uid, &mut acts); Text::new(tl!("Data Sets")).role(TypeRole::Heading).show(ui); data_set_list(ui, &rows.sets, &mut acts); @@ -463,9 +464,13 @@ pub fn show(app: &mut VectorcraftApp, ui: &mut Ui) { } } -/// The current data set across the top: its name in a box that renames it, the list that chooses -/// another, and the arrows that step through them. -fn data_set_bar(ui: &mut Ui, sets: &[SetRow], acts: &mut Vec) { +/// The current data set across the top: its name in a box that renames it when the typed name is +/// committed (Enter or leaving the box), the list that chooses another, and the arrows that step +/// through them. `doc` is the document's uid and how many times +/// File › Revert replaced its content: with the current data set's name, it is the box's edit +/// target, so a name typed and not yet committed is dropped when another document, a reverted one +/// or another data set becomes current, even one with the same name. +fn data_set_bar(ui: &mut Ui, sets: &[SetRow], doc: (u64, u64), acts: &mut Vec) { let current = sets.iter().find(|s| s.active).map(|s| s.name.as_str()); Row::new().show(ui, |ui, _| { Field::new(tl!("Data Set")).show(ui, |ui, _| { @@ -477,9 +482,10 @@ fn data_set_bar(ui: &mut Ui, sets: &[SetRow], acts: &mut Vec) { }), ) .enabled(current.is_some()) + .edit_target((doc, current)) .show(ui) .events - .landed() + .committed() .cloned(); if let (Some(from), Some(to)) = (current, typed) { acts.push(Act::Rename(from.to_owned(), to)); @@ -1211,6 +1217,107 @@ mod tests { assert_eq!(undo_steps(&app), before + 1); } + /// A document whose one data set, "Spring", is current. + fn add_spring(app: &mut VectorcraftApp) { + let t = app.run("text.create", json!({"x": 10, "y": 40, "text": "Alice"})).unwrap()["id"].as_u64().unwrap(); + app.run("variable.define", json!({"name": "Name", "kind": "text"})).unwrap(); + app.run("variable.bind", json!({"variable": "Name", "ids": [t]})).unwrap(); + app.run("dataset.capture", json!({})).unwrap(); + app.run("dataset.rename", json!({"name": "Data Set 1", "newName": "Spring"})).unwrap(); + } + + /// Clicks the Data Set box and types `text` over its name without pressing Enter. + fn start_typing(panel: &mut crate::tests::panel_harness::Panel, text: &str) { + panel.click_role(tl!("Data Set"), craft::ui::contract::Role::TEXT_INPUT); + panel.key(egui::Key::A, egui::Modifiers::COMMAND); + panel.type_text(text); + } + + /// A name typed into the Data Set box renames nothing until it is committed, and it is dropped + /// when another document becomes current, even when that document's current data set has the + /// same name: Enter there renames neither document's data set. + #[test] + fn an_unfinished_data_set_name_does_not_rename_another_documents_data_set() { + use crate::tests::panel_harness::Panel; + let mut app = VectorcraftApp::new(vectorcraft_engine::Session::new(), Default::default()); + for _ in 0..2 { + app.run("file.new", json!({"width": 300, "height": 200})).unwrap(); + add_spring(&mut app); + } + app.run("document.activate", json!({"index": 0})).unwrap(); + let steps = undo_steps(&app); + let mut panel = Panel::new(app, show); + let names = |panel: &Panel| -> Vec> { + panel.app().session.documents().iter().map(|st| st.doc.variables.datasets.iter().map(|d| d.name.clone()).collect()).collect() + }; + start_typing(&mut panel, "Summer"); + assert_eq!(names(&panel), [["Spring"], ["Spring"]], "typing renames nothing before Enter"); + assert_eq!(undo_steps(&panel.app()), steps, "typing adds no undo step"); + panel.app().run("document.activate", json!({"index": 1})).unwrap(); + panel.frame(); + panel.frame(); + let field = craft::ui::contract::Role::TEXT_INPUT; + assert!(panel.focused_nth(field, 0), "the box keeps the focus through the switch"); + assert_eq!(panel.value_role(tl!("Data Set"), field).as_deref(), Some("Spring"), "the box shows the other document's name"); + assert_eq!(names(&panel), [["Spring"], ["Spring"]], "switching documents renames nothing"); + panel.key(egui::Key::Enter, egui::Modifiers::NONE); + assert_eq!(names(&panel), [["Spring"], ["Spring"]], "Enter in the other document renames neither data set"); + } + + /// A name typed into the Data Set box and not yet committed is dropped by File › Revert, which + /// restores a data set of the same name: Enter afterwards does not rename the restored one. + #[test] + fn an_unfinished_data_set_name_does_not_rename_the_data_set_file_revert_restores() { + use crate::tests::panel_harness::Panel; + let mut app = VectorcraftApp::new(vectorcraft_engine::Session::new(), Default::default()); + app.services.read = Some(Box::new(|path| std::fs::read(path).map_err(|e| e.to_string()))); + app.services.write = Some(Box::new(|path, bytes| std::fs::write(path, bytes).map_err(|e| e.to_string()))); + app.run("file.new", json!({"width": 300, "height": 200})).unwrap(); + add_spring(&mut app); + let dir = vectorcraft_testkit::TempDir::new("variables-revert"); + app.run("file.saveAs", json!({"path": dir.join("Art.vectorcraft")})).unwrap(); + // An unsaved edit, so the revert has changes to throw away. + app.run("shape.rectangle", json!({"x": 0, "y": 0, "width": 20, "height": 20})).unwrap(); + let mut panel = Panel::new(app, show); + start_typing(&mut panel, "Summer"); + panel.app().run("file.revert", json!({"confirmed": true})).unwrap(); + assert_eq!(set_names(&mut panel.app()), ["Spring"], "the revert restores the data set"); + let steps = undo_steps(&panel.app()); + panel.frame(); + panel.frame(); + let field = craft::ui::contract::Role::TEXT_INPUT; + assert!(panel.focused_nth(field, 0), "the box keeps the focus through the revert"); + assert_eq!(panel.value_role(tl!("Data Set"), field).as_deref(), Some("Spring"), "the box shows the restored name"); + panel.key(egui::Key::Enter, egui::Modifiers::NONE); + assert_eq!(set_names(&mut panel.app()), ["Spring"], "the restored data set keeps its name"); + assert_eq!(undo_steps(&panel.app()), steps, "no rename runs"); + } + + /// A name typed into the Data Set box and not yet committed is dropped when another data set of + /// the same document becomes current: Enter afterwards renames neither data set. With the same + /// data set current, Enter renames it. + #[test] + fn an_unfinished_data_set_name_does_not_rename_the_next_data_set() { + use crate::tests::panel_harness::Panel; + let mut app = VectorcraftApp::new(vectorcraft_engine::Session::new(), Default::default()); + app.run("file.new", json!({"width": 300, "height": 200})).unwrap(); + add_spring(&mut app); + app.run("dataset.capture", json!({"name": "Autumn"})).unwrap(); + app.run("dataset.select", json!({"name": "Spring"})).unwrap(); + let mut panel = Panel::new(app, show); + start_typing(&mut panel, "Summer"); + panel.app().run("dataset.next", json!({})).unwrap(); + panel.frame(); + panel.frame(); + let field = craft::ui::contract::Role::TEXT_INPUT; + assert_eq!(panel.value_role(tl!("Data Set"), field).as_deref(), Some("Autumn"), "the box shows the next data set's name"); + panel.key(egui::Key::Enter, egui::Modifiers::NONE); + assert_eq!(set_names(&mut panel.app()), ["Spring", "Autumn"], "Enter renames neither data set"); + start_typing(&mut panel, "Winter"); + panel.key(egui::Key::Enter, egui::Modifiers::NONE); + assert_eq!(set_names(&mut panel.app()), ["Spring", "Winter"], "Enter renames the data set the name was typed for"); + } + /// A press on a column header sorts the list by that column, and a second press on it reverses /// the order, as `variable.list` reports it; sorting is not an edit. #[test] From 4c7aa2471c4b94dafce4c083831672d41b8ec94d Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Thu, 3 Dec 2026 08:46:41 -0800 Subject: [PATCH 2/2] Variables: a name being composed in the Data Set box stays with its data set A name being composed with an input method in the Data Set box carried over when another data set became current: a changed name clears the typed text but not the composition, so the input method's commit landed in the next data set's name and Enter renamed it. The current data set's name in the box's edit target drops the composition too, and a new test shows it. The box's doc comment now says that a data set replacing the current one under the same name in the same document still takes the unfinished name, since data sets have no id. New tests cover leaving the box with Tab (one undo step) and Escape (renames nothing). --- crates/ui-egui/src/panels/variables.rs | 61 ++++++++++++++++++++++++-- 1 file changed, 57 insertions(+), 4 deletions(-) diff --git a/crates/ui-egui/src/panels/variables.rs b/crates/ui-egui/src/panels/variables.rs index 5214799496..51d219feaf 100644 --- a/crates/ui-egui/src/panels/variables.rs +++ b/crates/ui-egui/src/panels/variables.rs @@ -466,10 +466,12 @@ pub fn show(app: &mut VectorcraftApp, ui: &mut Ui) { /// The current data set across the top: its name in a box that renames it when the typed name is /// committed (Enter or leaving the box), the list that chooses another, and the arrows that step -/// through them. `doc` is the document's uid and how many times -/// File › Revert replaced its content: with the current data set's name, it is the box's edit -/// target, so a name typed and not yet committed is dropped when another document, a reverted one -/// or another data set becomes current, even one with the same name. +/// through them. `doc` is the document's uid and how many times File › Revert replaced its +/// content. With the current data set's name, it is the box's edit target, so a name typed or +/// being composed and not yet committed is dropped when another document, a reverted one or a data +/// set with another name becomes current. Data sets have no identity besides their names, so a +/// data set that replaces the current one under the same name in the same document takes the +/// unfinished name. fn data_set_bar(ui: &mut Ui, sets: &[SetRow], doc: (u64, u64), acts: &mut Vec) { let current = sets.iter().find(|s| s.active).map(|s| s.name.as_str()); Row::new().show(ui, |ui, _| { @@ -1318,6 +1320,57 @@ mod tests { assert_eq!(set_names(&mut panel.app()), ["Spring", "Winter"], "Enter renames the data set the name was typed for"); } + /// A name being composed with an input method in the Data Set box is dropped when another data + /// set becomes current: the composition's commit and Enter rename neither data set. + #[test] + fn a_composed_data_set_name_does_not_rename_the_next_data_set() { + use crate::tests::panel_harness::Panel; + let mut app = VectorcraftApp::new(vectorcraft_engine::Session::new(), Default::default()); + app.run("file.new", json!({"width": 300, "height": 200})).unwrap(); + add_spring(&mut app); + app.run("dataset.capture", json!({"name": "Autumn"})).unwrap(); + app.run("dataset.select", json!({"name": "Spring"})).unwrap(); + let mut panel = Panel::new(app, show); + panel.click_role(tl!("Data Set"), craft::ui::contract::Role::TEXT_INPUT); + let preedit = |text: &str| egui::Event::Ime(egui::ImeEvent::Preedit { text: text.into(), active_range_chars: Some(0..text.chars().count()) }); + let field = craft::ui::contract::Role::TEXT_INPUT; + panel.frame_with(vec![preedit("Sum")]); + assert!(panel.value_role(tl!("Data Set"), field).is_some_and(|name| name.contains("Sum")), "the box shows the composition"); + panel.app().run("dataset.next", json!({})).unwrap(); + panel.frame(); + panel.frame(); + assert!(panel.focused_nth(field, 0), "the box keeps the focus through the switch"); + assert_eq!(panel.value_role(tl!("Data Set"), field).as_deref(), Some("Autumn"), "the box shows the next data set's name"); + panel.frame_with(vec![egui::Event::Ime(egui::ImeEvent::Commit("Summer".into()))]); + assert_eq!(panel.value_role(tl!("Data Set"), field).as_deref(), Some("Autumn"), "the commit does not reach the box"); + panel.key(egui::Key::Enter, egui::Modifiers::NONE); + assert_eq!(set_names(&mut panel.app()), ["Spring", "Autumn"], "the composition renames neither data set"); + } + + /// Leaving the Data Set box with Tab commits the typed name as one undo step, and Escape puts + /// the current name back without renaming anything. + #[test] + fn leaving_the_data_set_box_renames_once_and_escape_renames_nothing() { + use crate::tests::panel_harness::Panel; + let mut app = VectorcraftApp::new(vectorcraft_engine::Session::new(), Default::default()); + app.run("file.new", json!({"width": 300, "height": 200})).unwrap(); + add_spring(&mut app); + let steps = undo_steps(&app); + let mut panel = Panel::new(app, show); + let field = craft::ui::contract::Role::TEXT_INPUT; + start_typing(&mut panel, "Summer"); + panel.key(egui::Key::Escape, egui::Modifiers::NONE); + assert_eq!(panel.value_role(tl!("Data Set"), field).as_deref(), Some("Spring"), "Escape puts the name back"); + panel.key(egui::Key::Enter, egui::Modifiers::NONE); + assert_eq!(set_names(&mut panel.app()), ["Spring"], "Escape renames nothing"); + assert_eq!(undo_steps(&panel.app()), steps, "Escape adds no undo step"); + start_typing(&mut panel, "Summer"); + assert_eq!(set_names(&mut panel.app()), ["Spring"], "typing renames nothing"); + panel.key(egui::Key::Tab, egui::Modifiers::NONE); + assert_eq!(set_names(&mut panel.app()), ["Summer"], "Tab commits the typed name"); + assert_eq!(undo_steps(&panel.app()), steps + 1, "the rename is one undo step"); + } + /// A press on a column header sorts the list by that column, and a second press on it reverses /// the order, as `variable.list` reports it; sorting is not an edit. #[test]