Skip to content

Commit 152e079

Browse files
tree: Fix Transformation & Support Node-Anchors (#904)
* tree: Fix transformation and support named nodes - Fixes transformations on the tree - Support named elements in tree nodes * tree: Fix transformation + node anchors * changes: Update changelog
1 parent 9a54b3b commit 152e079

File tree

3 files changed

+23
-29
lines changed

3 files changed

+23
-29
lines changed

CHANGES.md

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
# 0.4.1
2-
- Added a `n-star` shape for drawing n-pointed stars (#886)
3-
- Added `breakable: false` to the `block` element of the canvas (#880)
4-
- A new tree layout algorithm, implemented by @MichaelGoodale in Rust, can handle differently sized tree nodes (#889)
5-
- **BREAKING** The `tree` callback for drawing nodes does not pass the parent nodes name anymor (new signature is `(node) => elements`) (#889)
6-
- **BREAKING** The `parent-position` argument of `tree` got removed (#889, #891)
2+
- Added a `n-star` shape for drawing n-pointed stars
3+
- Added `breakable: false` to the `block` element of the canvas
4+
- A new tree layout algorithm, implemented by @MichaelGoodale in Rust, can handle differently sized tree nodes
5+
- **BREAKING** The `tree` callback for drawing nodes does not pass the parent nodes name anymore (new signature is `(node) => elements`)
6+
- **BREAKING** The `parent-position` argument of `tree` got removed
7+
- Tree nodes now support anchors: custom anchors from the `draw-node` callback
8+
are accessible.
79
- `styles.resolve` now supports nested roots by passing an array (#914)
810

911
# 0.4.0

src/lib/tree.typ

Lines changed: 11 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -129,17 +129,15 @@
129129
depth: depth,
130130
children: children,
131131
content: content,
132-
drawables: none,
133132
)
134133

135-
// Pre-Render the node
136-
let (ctx: _, drawables, bounds) = process.many(ctx, draw.scope({
137-
draw.set-origin((0, 0))
138-
(draw-node)(node)
139-
}))
140-
node.drawables = drawables
141-
134+
// Measure the node
142135
if measure-content {
136+
let (ctx: _, drawables: _, bounds) = process.many(ctx, {
137+
draw.set-origin((0, 0))
138+
(draw-node)(node)
139+
})
140+
143141
if bounds != none {
144142
(node.width, node.height, _) = aabb.size(bounds)
145143
}
@@ -190,21 +188,11 @@
190188
node.group-name = "g" + name
191189
node.element = {
192190
draw.anchor(node.name, node-position(node))
193-
draw.group(
194-
name: node.group-name,
195-
{
196-
draw.anchor("default", node-position(node))
197-
198-
(ctx => {
199-
let (x, y) = node-position(node)
200-
let translation = matrix.transform-translate(x, -y, 0)
201-
202-
return (
203-
ctx: ctx,
204-
drawables: drawable.apply-transform(
205-
translation, node.drawables),
206-
)
207-
},)
191+
draw.group(name: node.group-name, ctx => {
192+
let (x, y) = node-position(node)
193+
draw.translate((x, y, 0))
194+
draw.anchor("default", (0, 0))
195+
draw-node(node)
208196
},
209197
)
210198
}

src/process.typ

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
}
3636

3737
let name = element.at("name", default: none)
38+
element.name = name
3839
if name != none {
3940
assert.eq(type(name), str,
4041
message: "Element name must be a string")
@@ -64,6 +65,7 @@
6465
ctx: ctx,
6566
bounds: bounds,
6667
drawables: element.at("drawables", default: ()),
68+
element: element,
6769
)
6870
}
6971

@@ -74,6 +76,7 @@
7476
#let many(ctx, body) = {
7577
let drawables = ()
7678
let bounds = none
79+
let elements = ()
7780

7881
for el in body {
7982
let r = element(ctx, el)
@@ -85,6 +88,7 @@
8588
ctx = r.ctx
8689
drawables += r.drawables
8790
}
91+
elements.push(r.element)
8892
}
89-
return (ctx: ctx, bounds: bounds, drawables: drawables)
93+
return (ctx: ctx, bounds: bounds, drawables: drawables, elements: elements)
9094
}

0 commit comments

Comments
 (0)