Merge pull request #10730 from dotCooCoo/fix/tabs-draft-edit-target

Tabs: an unentered X, Leader or Align On value stays with its stop
This commit is contained in:
.CooCoo authored and GitHub committed 2026-12-03 20:18:00 -08:00
commit 003489cb7c
2 files changed
+263 -3

No files matched your search

+23 -3
View File
@@ -344,6 +344,21 @@ fn shown_paragraph(app: &VectorcraftApp, t: &TextObject) -> usize {
super::character::text_editing(app).map_or(0, |(_, a, _)| t.paragraphs_in(a, a).start)
}
/// The edit target of the panel's fields for text `n`: the active document and its reverts, the
/// selected objects, the Type tool's range and whether Select All took the threaded story, and the
/// text and paragraph whose stops the panel shows.
/// A value typed in a field is dropped when this changes before it is entered, so it does not set
/// the stops of other text, even text whose stop holds the same values.
fn edited_paragraph(app: &VectorcraftApp, n: &Node) -> egui::IdSalt {
let para = match &n.kind {
NodeKind::Text(t) => shown_paragraph(app, t),
_ => 0,
};
let document = app.session.active().map(|st| (st.uid, st.reverts, &st.selection.objects));
let typed = (super::character::text_editing(app), super::character::text_story(app));
egui::IdSalt::new((document, typed, n.id, para))
}
/// [`above_text`], then note the active document and text `n`. A floating panel is
/// at least [`crate::panel_docking::PANEL_MIN_WIDTH`] wide, so for narrow text at a low zoom the
/// panel comes out wider than asked; the ruler then draws text `n` at [`screen_scale`] and runs
@@ -722,6 +737,7 @@ pub fn show(app: &mut VectorcraftApp, ui: &mut Ui) {
return;
};
let vertical = matches!(&node.kind, NodeKind::Text(text) if text.vertical);
let edited = edited_paragraph(app, &node);
let targets = indent_targets(app, &node);
let mixed_left = targets.iter().any(|p| p.left != para.left_indent);
let panel = &mut app.ui.tabs_panel;
@@ -763,7 +779,7 @@ pub fn show(app: &mut VectorcraftApp, ui: &mut Ui) {
let label = if vertical { tl!("Y:") } else { tl!("X:") };
let x = marker.map_or_else(|| stops.get(stop).map(|s| s.position), |item| picked_position(item, &para, span));
let typed = Field::new(label)
.show(ui, |ui, _| NumberField::length(label, Bind::given(x), lengths).range(0.0..=MAX_STOP).show(ui))
.show(ui, |ui, _| NumberField::length(label, Bind::given(x), lengths).range(0.0..=MAX_STOP).edit_target((edited, stop, marker)).show(ui))
.inner
.and_then(|out| out.edit.events.committed().copied().flatten());
if let Some(value) = typed {
@@ -773,7 +789,9 @@ pub fn show(app: &mut VectorcraftApp, ui: &mut Ui) {
let on_stop = marker.is_none() && stop < stops.len();
let leader = stops.get(stop).filter(|_| marker.is_none()).map(|s| s.leader.clone()).unwrap_or_default();
let typed = Field::new(tl!("Leader:"))
.show(ui, |ui, _| TextField::new(tl!("Leader:"), Bind::given(leader.clone())).limit(8_usize).enabled(on_stop).show(ui))
.show(ui, |ui, _| {
TextField::new(tl!("Leader:"), Bind::given(leader.clone())).limit(8_usize).enabled(on_stop).edit_target((edited, stop)).show(ui)
})
.inner
.and_then(|out| out.events.committed().cloned());
if let (Some(leader), Some(s)) = (typed, stops.get_mut(stop)) {
@@ -784,7 +802,9 @@ pub fn show(app: &mut VectorcraftApp, ui: &mut Ui) {
let decimal = marker.is_none() && stops.get(stop).is_some_and(|s| s.align == TabAlign::Decimal);
let on = stops.get(stop).map_or_else(|| ".".to_string(), |s| s.align_on.to_string());
let typed = Field::new(tl!("Align On:"))
.show(ui, |ui, _| TextField::new(tl!("Align On:"), Bind::given(on.clone())).limit(1_usize).enabled(decimal).show(ui))
.show(ui, |ui, _| {
TextField::new(tl!("Align On:"), Bind::given(on.clone())).limit(1_usize).enabled(decimal).edit_target((edited, stop)).show(ui)
})
.inner
.and_then(|out| out.events.committed().and_then(|text| text.chars().next()));
if let (Some(c), Some(s)) = (typed, stops.get_mut(stop)) {
+240
View File
@@ -1094,3 +1094,243 @@ fn above_text_puts_the_panel_at_the_top_when_below_the_text_does_not_fit() {
assert_eq!(top, 0.0, "view centered at y {center_y}: panel top {top}, canvas {canvas:?}");
}
}
/// An app with area texts that each have decimal stops, aligned on a period with no leader, at
/// `positions`, and the first text selected. → the app and the texts' ids.
fn decimal_stops_app(texts: usize, positions: &[f64]) -> (VectorcraftApp, Vec<u64>) {
let mut app = VectorcraftApp::new(Session::new(), Default::default());
app.run("file.new", json!({})).unwrap();
let stops: Vec<_> = positions.iter().map(|p| json!({"position": p, "align": "decimal"})).collect();
let ids: Vec<u64> = (0..texts)
.map(|i| {
let y = 20.0 + 80.0 * i as f64;
let id = app.run("text.create", json!({"x": 10, "y": y, "text": "a\t1.5", "area": {"width": 300, "height": 60}})).unwrap();
let id = id["id"].as_u64().unwrap();
app.run("select.set", json!({"ids": [id]})).unwrap();
app.run("text.tabs.set", json!({"stops": stops})).unwrap();
id
})
.collect();
app.run("select.set", json!({"ids": [ids[0]]})).unwrap();
(app, ids)
}
/// The stops of the first paragraph of text `id` as (position, leader, align on).
fn stops_of(app: &VectorcraftApp, id: u64) -> Vec<(f64, String, char)> {
text_of(app, id).para_at(0).tabs.iter().map(|s| (s.position, s.leader.clone(), s.align_on)).collect()
}
/// Selects all of the focused field's text, types `text` and leaves it unentered.
fn draft(panel: &mut crate::tests::panel_harness::Panel, label: &str, role: craft::ui::contract::Role, text: &str) {
panel.click_role(label, role);
panel.key(egui::Key::A, egui::Modifiers::COMMAND);
panel.type_text(text);
}
/// A value typed in the X, Leader or Align On field and not yet entered is dropped when the
/// selection moves to another text whose stop holds the same values: Enter then leaves both texts'
/// stops as they were. A value typed for the newly selected text still sets its stop.
#[test]
fn a_field_value_typed_before_the_selection_changes_does_not_reach_the_new_text() {
use craft::ui::contract::Role;
for (label, role, typed) in [("X:", Role::SPIN_BUTTON, "70"), ("Leader:", Role::TEXT_INPUT, "-"), ("Align On:", Role::TEXT_INPUT, ",")] {
let (app, ids) = decimal_stops_app(2, &[40.0]);
let (a, b) = (ids[0], ids[1]);
let before = stops_of(&app, a);
let mut panel = crate::tests::panel_harness::Panel::new(app, show);
draft(&mut panel, label, role, typed);
panel.app().run("select.set", json!({"ids": [b]})).unwrap();
panel.frame();
panel.key(egui::Key::Enter, egui::Modifiers::NONE);
for id in [a, b] {
assert_eq!(stops_of(&panel.app(), id), before, "{label} {typed:?} typed for text {a}, entered after selecting text {b}: text {id}");
}
draft(&mut panel, label, role, typed);
panel.key(egui::Key::Enter, egui::Modifiers::NONE);
assert_eq!(stops_of(&panel.app(), a), before, "{label} {typed:?} typed for text {b}: text {a}");
assert_ne!(stops_of(&panel.app(), b), before, "{label} {typed:?} typed for text {b} sets its stop");
}
}
/// A Leader value typed with two texts selected and not yet entered is dropped when the selection
/// changes to the first text and a third one: the panel still shows the first text, but Enter
/// would set every selected text's stops.
#[test]
fn a_field_value_typed_before_other_texts_join_the_selection_does_not_reach_them() {
let (app, ids) = decimal_stops_app(3, &[40.0]);
let before = stops_of(&app, ids[0]);
let mut panel = crate::tests::panel_harness::Panel::new(app, show);
panel.app().run("select.set", json!({"ids": [ids[0], ids[1]]})).unwrap();
draft(&mut panel, "Leader:", craft::ui::contract::Role::TEXT_INPUT, "-");
panel.app().run("select.set", json!({"ids": [ids[0], ids[2]]})).unwrap();
panel.frame();
panel.key(egui::Key::Enter, egui::Modifiers::NONE);
for &id in &ids {
assert_eq!(
stops_of(&panel.app(), id),
before,
"Leader typed for texts {} and {}, entered after selecting {} and {}: text {id}",
ids[0],
ids[1],
ids[0],
ids[2]
);
}
}
/// A Leader or Align On value typed for one stop and not yet entered is dropped when the ruler
/// picks another stop of the paragraph with the same leader and alignment character.
#[test]
fn a_field_value_typed_before_another_stop_is_picked_does_not_reach_that_stop() {
use craft::ui::contract::Role;
for (label, typed) in [("Leader:", "-"), ("Align On:", ",")] {
let (app, ids) = decimal_stops_app(1, &[30.0, 60.0]);
let before = stops_of(&app, ids[0]);
let mut panel = crate::tests::panel_harness::Panel::new(app, show);
panel.app().ui.tabs_panel.stop = 0;
draft(&mut panel, label, Role::TEXT_INPUT, typed);
panel.app().ui.tabs_panel.stop = 1;
panel.frame();
panel.key(egui::Key::Enter, egui::Modifiers::NONE);
assert_eq!(stops_of(&panel.app(), ids[0]), before, "{label} {typed:?} typed for the first stop, entered after picking the second");
}
}
/// An X value typed in one document and not yet entered is dropped when another document becomes
/// active, also when the text selected there has the same id and the same stops.
#[test]
fn an_x_value_typed_before_another_document_is_activated_does_not_reach_it() {
let mut app = VectorcraftApp::new(Session::new(), Default::default());
let ids: Vec<u64> = (0..2)
.map(|_| {
app.run("file.new", json!({})).unwrap();
let id = app.run("text.create", json!({"x": 10, "y": 20, "text": "a\t1.5", "area": {"width": 300, "height": 60}})).unwrap();
let id = id["id"].as_u64().unwrap();
app.run("select.set", json!({"ids": [id]})).unwrap();
app.run("text.tabs.set", json!({"stops": [{"position": 40, "align": "decimal"}]})).unwrap();
id
})
.collect();
assert_eq!(ids[0], ids[1], "the two documents' texts have the same id");
let id = ids[0];
let before = stops_of(&app, id);
let mut panel = crate::tests::panel_harness::Panel::new(app, show);
draft(&mut panel, "X:", craft::ui::contract::Role::SPIN_BUTTON, "70");
panel.app().run("document.activate", json!({"index": 0})).unwrap();
panel.frame();
panel.key(egui::Key::Enter, egui::Modifiers::NONE);
for index in [0, 1] {
panel.app().run("document.activate", json!({"index": index})).unwrap();
assert_eq!(stops_of(&panel.app(), id), before, "X typed in the second document, entered after activating the first: document {index}");
}
}
/// The stops of every paragraph of text `id` as (position, leader, align on).
fn paragraph_stops(app: &VectorcraftApp, id: u64) -> Vec<Vec<(f64, String, char)>> {
let t = text_of(app, id);
(0..t.paragraph_count()).map(|i| t.para_at(i).tabs.iter().map(|s| (s.position, s.leader.clone(), s.align_on)).collect()).collect()
}
/// An X value typed with the Type tool's insertion point in the first paragraph and not yet
/// entered is dropped when the insertion point moves to the second paragraph, or the selection
/// grows over both, though both paragraphs hold the same stops.
#[test]
fn an_x_value_typed_before_the_type_tool_selection_changes_does_not_reach_other_paragraphs() {
for (anchor, caret) in [(6, 6), (0, 9)] {
let mut app = VectorcraftApp::new(Session::new(), Default::default());
app.run("file.new", json!({})).unwrap();
let id = app.run("text.create", json!({"x": 10, "y": 20, "text": "a\t1.5\nb\t2.5", "area": {"width": 300, "height": 80}})).unwrap();
let id = id["id"].as_u64().unwrap();
app.run("select.set", json!({"ids": [id]})).unwrap();
app.run("text.tabs.set", json!({"stops": [{"position": 40, "align": "decimal"}]})).unwrap();
let before = paragraph_stops(&app, id);
assert_eq!(before.len(), 2);
assert_eq!(before[0], before[1], "both paragraphs hold the same stops");
app.session.select_tool("type", Default::default()).unwrap();
app.session.set_tool_option("textSelection", &json!({"editing": id, "anchor": 0, "caret": 0}));
let mut panel = crate::tests::panel_harness::Panel::new(app, show);
draft(&mut panel, "X:", craft::ui::contract::Role::SPIN_BUTTON, "70");
panel.app().session.set_tool_option("textSelection", &json!({"editing": id, "anchor": anchor, "caret": caret}));
panel.frame();
panel.key(egui::Key::Enter, egui::Modifiers::NONE);
assert_eq!(
paragraph_stops(&panel.app(), id),
before,
"X typed at the start of the text, entered with the Type tool's selection at {anchor}..{caret}"
);
}
}
/// An X value typed for the first-line marker and not yet entered is dropped when the ruler picks
/// the left indent's box at the same position.
#[test]
fn an_x_value_typed_before_another_indent_marker_is_picked_does_not_move_it() {
let mut app = indent_app(0.0, 0.0);
app.ui.tabs_panel.marker = Some(RulerItem::FirstLine);
let mut panel = crate::tests::panel_harness::Panel::new(app, show);
draft(&mut panel, "X:", craft::ui::contract::Role::SPIN_BUTTON, "20");
panel.app().ui.tabs_panel.marker = Some(RulerItem::LeftBox);
panel.frame();
panel.key(egui::Key::Enter, egui::Modifiers::NONE);
assert_eq!(indent_values(&panel.app(), 0), (0.0, 0.0), "X typed for the first-line marker, entered after picking the left indent's box");
}
/// An X value typed and not yet entered is dropped when File › Revert restores the document, also
/// when the restored text is selected again and holds the same stops.
#[test]
fn an_x_value_typed_before_file_revert_does_not_reach_the_restored_text() {
struct SavedFile(std::path::PathBuf);
impl Drop for SavedFile {
fn drop(&mut self) {
let _ = std::fs::remove_file(&self.0);
}
}
let (mut app, ids) = decimal_stops_app(1, &[40.0]);
let id = ids[0];
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())));
let path = SavedFile(std::env::temp_dir().join(format!("vc-ui-tabs-draft-revert-{}.vectorcraft", std::process::id())));
app.run("file.saveAs", json!({"path": path.0})).unwrap();
app.run("shape.rectangle", json!({"x": 10, "y": 200, "width": 40, "height": 40})).unwrap();
app.run("select.set", json!({"ids": [id]})).unwrap();
let before = stops_of(&app, id);
let mut panel = crate::tests::panel_harness::Panel::new(app, show);
draft(&mut panel, "X:", craft::ui::contract::Role::SPIN_BUTTON, "70");
panel.app().run("file.revert", json!({"confirmed": true})).unwrap();
panel.app().run("select.set", json!({"ids": [id]})).unwrap();
panel.frame();
panel.key(egui::Key::Enter, egui::Modifiers::NONE);
assert_eq!(stops_of(&panel.app(), id), before, "X typed before File › Revert, entered after selecting the restored text");
}
/// A Leader value typed with the Type tool's selection over all of a threaded frame's text and not
/// yet entered is dropped when Select All takes the whole story, though the frame's range stays
/// the same: Enter would set the stops of every frame of the story.
#[test]
fn a_field_value_typed_before_select_all_takes_the_story_does_not_reach_it() {
let mut app = VectorcraftApp::new(Session::new(), Default::default());
app.run("file.new", json!({})).unwrap();
let story = (1..=8).map(|i| format!("{i}\t1.5")).collect::<Vec<_>>().join("\n");
let a = app.run("text.create", json!({"x": 10, "y": 20, "text": story, "area": {"width": 300, "height": 50}})).unwrap()["id"].as_u64().unwrap();
app.run("select.set", json!({"ids": [a]})).unwrap();
app.run("text.tabs.set", json!({"stops": [{"position": 40, "align": "decimal"}]})).unwrap();
let b = app.run("text.thread.add", json!({"from": a, "rect": [10, 300, 300, 200]})).unwrap()["id"].as_u64().unwrap();
assert!(!text_of(&app, a).plain_text().is_empty() && !text_of(&app, b).plain_text().is_empty(), "the story runs through both frames");
let before = (paragraph_stops(&app, a), paragraph_stops(&app, b));
let len = text_of(&app, a).plain_text().len();
app.run("select.set", json!({"ids": [a]})).unwrap();
app.session.select_tool("type", Default::default()).unwrap();
app.session.set_tool_option("textSelection", &json!({"editing": a, "anchor": 0, "caret": len}));
let range = super::super::character::text_editing(&app);
let mut panel = crate::tests::panel_harness::Panel::new(app, show);
draft(&mut panel, "Leader:", craft::ui::contract::Role::TEXT_INPUT, "-");
panel.app().run("select.all", json!({})).unwrap();
assert_eq!(super::super::character::text_editing(&panel.app()), range, "Select All leaves the frame's range as it was");
assert!(super::super::character::text_story(&panel.app()).is_some(), "Select All takes the story");
panel.frame();
panel.key(egui::Key::Enter, egui::Modifiers::NONE);
let after_a = paragraph_stops(&panel.app(), a);
let after_b = paragraph_stops(&panel.app(), b);
assert_eq!((after_a, after_b), before, "Leader typed for frame {a}'s text, entered after Select All took the story");
}