Merge pull request #10776 from dotCooCoo/fix/dynamic-text-gap-draft-target

Properties: an unfinished Word Gap or Line Gap stays with the type it was typed for
This commit is contained in:
.CooCoo authored and GitHub committed 2026-12-04 09:25:17 -08:00
commit f1cdd03d20
2 files changed
+174 -7

No files matched your search

@@ -10,7 +10,7 @@ use craft::ui::form::Field;
use craft::ui::text::{NumberField, StepArrows};
use egui::Ui;
use serde_json::{Value, json};
use vectorcraft_doc::{AreaOptions, NodeKind, TextKind};
use vectorcraft_doc::{AreaOptions, NodeId, NodeKind, TextKind};
use crate::VectorcraftApp;
@@ -25,12 +25,24 @@ pub(crate) struct Shown {
pub(crate) enabled: bool,
}
/// Dynamic Text of the selected area type (and area type in selected groups); `None` when the
/// selection holds no area type, or holds other type too (the command refuses it).
/// [`shown_with_target`] without the edit target.
#[cfg(test)]
pub(crate) fn shown(app: &VectorcraftApp) -> Option<Shown> {
shown_with_target(app).map(|(s, _)| s)
}
/// What the Word Gap and Line Gap fields edit, as their edit target: the document, how many times File › Revert
/// replaced its content, and the area type frames `type.dynamicText` changes, sorted by id. A field drops an
/// unfinished value when this changes, even where the newly selected frames have the same gap.
type EditTarget = (u64, u64, Vec<NodeId>);
/// Dynamic Text of the selected area type (and area type in selected groups), and the [`EditTarget`] of those
/// frames; `None` when the selection holds no area type, or holds other type too (the command refuses it).
fn shown_with_target(app: &VectorcraftApp) -> Option<(Shown, EditTarget)> {
let st = app.session.active()?;
let threaded = |id| st.doc.text_threads.iter().any(|t| t.len() > 1 && t.contains(&id));
let mut frames = vec![];
let mut ids = vec![];
let mut other = false;
for n in st.selection.objects.iter().filter_map(|id| st.doc.node(*id)) {
n.walk(&mut |c| {
@@ -38,18 +50,22 @@ pub(crate) fn shown(app: &VectorcraftApp) -> Option<Shown> {
if matches!(t.kind, TextKind::Area { .. }) {
let (word, line) = t.area.block_gaps();
frames.push((t.dynamic_text, word, line, threaded(c.id)));
ids.push(c.id);
} else {
other = true;
}
});
}
let &(on, word, line, _) = frames.first().filter(|_| !other)?;
Some(Shown {
ids.sort_unstable_by_key(|id| id.0);
ids.dedup();
let shown = Shown {
on: frames.iter().all(|f| f.0 == on).then_some(on),
word_gap: frames.iter().all(|f| f.1 == word).then_some(word),
line_gap: frames.iter().all(|f| f.2 == line).then_some(line),
enabled: frames.iter().all(|f| !f.3),
})
};
Some((shown, (st.uid, st.reverts, ids)))
}
fn run(app: &mut VectorcraftApp, p: Value) {
@@ -61,7 +77,7 @@ fn run(app: &mut VectorcraftApp, p: Value) {
/// The checkbox, and the gap rows while every selected frame has Block on. Nothing shows without
/// area type. Returns whether anything was drawn.
pub(crate) fn rows(app: &mut VectorcraftApp, ui: &mut Ui) -> bool {
let Some(s) = shown(app) else { return false };
let Some((s, target)) = shown_with_target(app) else { return false };
let on = s.on.map_or(Mixed::Mixed, Mixed::Same);
let out = Checkbox::new(Bind::given(on)).label(tl!("Dynamic Text")).enabled(s.enabled).show(ui);
if let Some(&on) = out.events.committed() {
@@ -78,6 +94,7 @@ pub(crate) fn rows(app: &mut VectorcraftApp, ui: &mut Ui) -> bool {
.range(lo..=hi)
.step(10.0)
.step_arrows(StepArrows::Leading)
.edit_target(&target)
.show(ui);
if let Some(Some(v)) = out.edit.events.committed() {
run(app, json!({ "wordGap": v.clamp(lo, hi) }));
@@ -86,7 +103,7 @@ pub(crate) fn rows(app: &mut VectorcraftApp, ui: &mut Ui) -> bool {
let (lo, hi) = AreaOptions::LINE_GAP;
let lengths = crate::canvas::general_lengths(app);
Field::new(tl!("Line Gap")).show(ui, |ui, _| {
let out = NumberField::length(tl!("Line Gap"), Bind::given(s.line_gap), lengths).range(lo..=hi).show(ui);
let out = NumberField::length(tl!("Line Gap"), Bind::given(s.line_gap), lengths).range(lo..=hi).edit_target(&target).show(ui);
if let Some(Some(v)) = out.edit.events.committed() {
run(app, json!({ "lineGap": v.clamp(lo, hi) }));
}
@@ -2113,6 +2113,156 @@ fn dynamic_text_rows_follow_the_selected_area_type() {
assert_eq!(dynamic_text::shown(&panel.app()).map(|s| (s.on, s.enabled)), Some((Some(false), false)));
}
/// Two area type frames at `x` 10 and 200 with Dynamic Text on, Word Gap 100% and Line Gap 2 pt, in a new document →
/// their ids.
fn dynamic_text_frames(app: &mut VectorcraftApp) -> (u64, u64) {
app.run("file.new", json!({"width": 500, "height": 400})).unwrap();
let mut make = |x: f64| {
let id = app.run("text.create", json!({"x": x, "y": 10, "text": "Panel\ntext", "area": {"width": 100, "height": 120}})).unwrap()["id"]
.as_u64()
.unwrap();
app.run("type.dynamicText", json!({"ids": [id], "on": true, "lineGap": 2})).unwrap();
id
};
(make(10.0), make(200.0))
}
/// The Word Gap and Line Gap of area type `id` in the active document.
fn block_gaps(app: &VectorcraftApp, id: u64) -> (f64, f64) {
match &app.session.active().unwrap().doc.node(vectorcraft_doc::NodeId(id)).unwrap().kind {
vectorcraft_doc::NodeKind::Text(t) => t.area.block_gaps(),
_ => panic!("object {id} is not type"),
}
}
/// Selects all of the field `name`'s value and types `3+`, leaving it unfinished.
fn type_unfinished(panel: &mut Panel, name: &str) {
use craft::ui::contract::Role;
panel.click_role(name, Role::SPIN_BUTTON);
panel.key(Key::A, Modifiers::COMMAND);
panel.type_text("3+");
}
/// A Word Gap or Line Gap typed for one frame and not yet entered is dropped when another frame with the same gap is
/// selected, so it does not reach that frame. With the selection unchanged, the value typed so far is kept.
#[test]
fn dynamic_text_gaps_drop_an_unfinished_value_when_the_selection_changes() {
use craft::ui::contract::Role;
let mut app = VectorcraftApp::new(Session::new(), Default::default());
let (a, b) = dynamic_text_frames(&mut app);
let mut panel = Panel::new(app, type_sections);
for (name, gap) in [("Word Gap", 0), ("Line Gap", 1)] {
let at = |panel: &Panel, id| {
let gaps = block_gaps(&panel.app(), id);
[gaps.0, gaps.1][gap]
};
let before = (at(&panel, a), at(&panel, b));
panel.app().run("select.set", json!({"ids": [a]})).unwrap();
panel.frame();
type_unfinished(&mut panel, name);
panel.app().run("select.set", json!({"ids": [b]})).unwrap();
panel.frame();
assert_ne!(panel.value_role(name, Role::SPIN_BUTTON).as_deref(), Some("3+"), "{name} drops the value typed for a");
panel.type_text("1");
panel.key(Key::Enter, Modifiers::NONE);
assert_eq!((at(&panel, a), at(&panel, b)), (before.0, 1.0), "{name}: a keeps its gap, b takes 1, not 3+1");
// With the selection unchanged, 3+ then 1 gives 4.
type_unfinished(&mut panel, name);
panel.frame();
panel.type_text("1");
panel.key(Key::Enter, Modifiers::NONE);
assert_eq!(at(&panel, b), 4.0, "{name}: an unchanged selection keeps the typed value");
panel.app().run("type.dynamicText", json!({"ids": [b], "wordGap": 100, "lineGap": 2})).unwrap();
}
}
/// The gap fields' unfinished value follows the area type frames Dynamic Text changes, not the selected objects: a
/// rectangle added to the selection keeps it, and a frame moved into a selected group drops it.
#[test]
fn dynamic_text_gaps_keep_an_unfinished_value_while_the_frames_stay_the_same() {
let mut app = VectorcraftApp::new(Session::new(), Default::default());
let (a, b) = dynamic_text_frames(&mut app);
let square = app.run("shape.rectangle", json!({"x": 10, "y": 300, "width": 20, "height": 20})).unwrap()["id"].as_u64().unwrap();
app.run("select.set", json!({"ids": [a]})).unwrap();
let mut panel = Panel::new(app, type_sections);
type_unfinished(&mut panel, "Word Gap");
panel.app().run("select.set", json!({"ids": [a, square]})).unwrap();
panel.frame();
panel.type_text("1");
panel.key(Key::Enter, Modifiers::NONE);
assert_eq!(block_gaps(&panel.app(), a).0, 4.0, "the same frame keeps the typed value: 3+1");
// b and a group holding a, selected; a third frame moved into the group while a value is typed.
let c = {
let mut app = panel.app();
app.run("type.dynamicText", json!({"ids": [a], "wordGap": 100})).unwrap();
let c = app.run("text.create", json!({"x": 10, "y": 150, "text": "Panel\ntext", "area": {"width": 100, "height": 120}})).unwrap()["id"]
.as_u64()
.unwrap();
app.run("type.dynamicText", json!({"ids": [c], "on": true, "lineGap": 2})).unwrap();
app.run("select.set", json!({"ids": [a]})).unwrap();
let group = app.run("object.group", json!({})).unwrap()["id"].as_u64().unwrap();
app.run("select.set", json!({"ids": [b, group]})).unwrap();
c
};
panel.frame();
type_unfinished(&mut panel, "Word Gap");
let selected = panel.app().session.active().unwrap().selection.objects.clone();
let group = selected[1].0;
panel.app().run("layer.move", json!({"id": c, "target": group, "place": "inside"})).unwrap();
assert_eq!(panel.app().session.active().unwrap().selection.objects, selected, "b and the group stay selected");
panel.frame();
panel.type_text("1");
panel.key(Key::Enter, Modifiers::NONE);
let gaps = [a, b, c].map(|id| block_gaps(&panel.app(), id).0);
assert_eq!(gaps, [1.0; 3], "all three frames take 1%, not 3+1");
}
/// A Word Gap typed in one document and not yet entered is dropped when another document whose frame has the same id
/// and the same gap becomes active, and when File › Revert restores the frame.
#[test]
fn dynamic_text_gaps_drop_an_unfinished_value_in_another_document_or_after_revert() {
let mut app = VectorcraftApp::new(Session::new(), Default::default());
let (a, _) = dynamic_text_frames(&mut app);
let (other, _) = dynamic_text_frames(&mut app);
assert_eq!(other, a, "both documents' first frames have the same id");
app.run("document.activate", json!({"index": 0})).unwrap();
app.run("select.set", json!({"ids": [a]})).unwrap();
let mut panel = Panel::new(app, type_sections);
type_unfinished(&mut panel, "Word Gap");
panel.app().run("document.activate", json!({"index": 1})).unwrap();
panel.app().run("select.set", json!({"ids": [a]})).unwrap();
panel.frame();
panel.type_text("1");
panel.key(Key::Enter, Modifiers::NONE);
let word_gap = |panel: &mut Panel, index: u64| {
panel.app().run("document.activate", json!({"index": index})).unwrap();
block_gaps(&panel.app(), a).0
};
assert_eq!((word_gap(&mut panel, 0), word_gap(&mut panel, 1)), (100.0, 1.0), "the other document's frame takes 1%, not 3+1");
// The second document, saved, then changed so that Revert has something to undo.
let dir = vectorcraft_testkit::TempDir::new("dynamic-text-revert");
{
let mut app = panel.app();
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("type.dynamicText", json!({"ids": [a], "wordGap": 100})).unwrap();
app.run("file.saveAs", json!({"path": dir.join("Dynamic Text.vectorcraft")})).unwrap();
app.run("shape.rectangle", json!({"x": 100, "y": 300, "width": 20, "height": 20})).unwrap();
app.run("select.set", json!({"ids": [a]})).unwrap();
}
panel.frame();
type_unfinished(&mut panel, "Word Gap");
panel.app().run("file.revert", json!({"confirmed": true})).unwrap();
panel.app().run("select.set", json!({"ids": [a]})).unwrap();
panel.frame();
panel.type_text("1");
panel.key(Key::Enter, Modifiers::NONE);
assert_eq!(block_gaps(&panel.app(), a).0, 1.0, "Revert drops the unfinished value");
}
/// A document with a graph of `kind` and three series, selected → the graph's id.
fn graph_doc(app: &mut VectorcraftApp, kind: &str, x: f64) -> u64 {
if app.session.active().is_none() {