changeSizeCommand function
The Change size step (D7, decision 9): instance takes entry's
definition (reused or copied, definitionForEntry), its linear part
kept and its translation chosen so the new box's back-left corner
(left', back') lands where the old (left, back) was. Null when the
instance already draws entry's key (re-picking the checked size is
not a size change: it would copy a fresh definition over an older
version or an edited one), or a box is missing.
Implementation
CompoundCommand? changeSizeCommand(
DraftDocument doc, Handle instance, SymbolEntry entry) {
final node = doc.tree[instance];
if (node is! InstanceNode) return null;
if (doc.components.get<SymbolComponent>(node.definition)?.key == entry.key) {
return null;
}
final oldBox = boxOfDefinition(doc, node.definition);
final newBox = boxOfEntry(entry);
if (oldBox == null || newBox == null) return null;
final made = definitionForEntry(doc, entry);
if (made.definition == node.definition) return null;
final t = node.transform;
final corner = t.transformPoint(Vector2(oldBox.left, oldBox.back));
final next = Transform2(
t.a,
t.b,
t.c,
t.d,
_clean(corner.x - (t.a * newBox.left + t.c * newBox.back)),
_clean(corner.y - (t.b * newBox.left + t.d * newBox.back)),
);
return CompoundCommand([
...made.commands,
SetInstanceDefinitionCommand(instance, made.definition),
TransformNodeCommand(instance, next),
], label: 'Change size');
}