Merge pull request #10787 from dotCooCoo/fix/variables-dataset-name-target

Variables: an unfinished Data Set name stays with its document and data set
This commit is contained in:
.CooCoo authored and GitHub committed 2026-12-04 10:15:36 -08:00
commit 2df909ac0d
1 file changed
+165 -5
+165 -5
View File
@@ -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,15 @@ 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<Act>) {
/// 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 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<Act>) {
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 +484,10 @@ fn data_set_bar(ui: &mut Ui, sets: &[SetRow], acts: &mut Vec<Act>) {
}),
)
.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));
@@ -1212,6 +1220,158 @@ 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<Vec<String>> {
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 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]