Skip to content

The agent that grows with you

fix(tui): preserve raw layout geometry across rounding

fangliquana953eefe226a
diff --git a/ui-tui/packages/hermes-ink/src/native-ts/yoga-layout/index.ts b/ui-tui/packages/hermes-ink/src/native-ts/yoga-layout/index.ts
index 2333931237e..4b99ee2ec2d 100644
--- a/ui-tui/packages/hermes-ink/src/native-ts/yoga-layout/index.ts
+++ b/ui-tui/packages/hermes-ink/src/native-ts/yoga-layout/index.ts
@@ -436,7 +436,6 @@ export class Node {
_cGen = -1
_cN = 0
_cWr = 0
- _rLayoutGen = -1
_rSubtreeLayoutGen = -1
_rValid = false
_rLeft = NaN
@@ -549,26 +548,26 @@ export class Node {
this.markDirty()
}
getComputedLeft(): number {
- return this.layout.left
+ return this._rValid ? this._rRoundedLeft : this.layout.left
}
getComputedTop(): number {
- return this.layout.top
+ return this._rValid ? this._rRoundedTop : this.layout.top
}
getComputedWidth(): number {
- return this.layout.width
+ return this._rValid ? this._rRoundedWidth : this.layout.width
}
getComputedHeight(): number {
- return this.layout.height
+ return this._rValid ? this._rRoundedHeight : this.layout.height
}
getComputedRight(): number {
const p = this.parent
- return p ? p.layout.width - this.layout.left - this.layout.width : 0
+ return p ? p.getComputedWidth() - this.getComputedLeft() - this.getComputedWidth() : 0
}
getComputedBottom(): number {
const p = this.parent
- return p ? p.layout.height - this.layout.top - this.layout.height : 0
+ return p ? p.getComputedHeight() - this.getComputedTop() - this.getComputedHeight() : 0
}
getComputedLayout(): {
left: number
@@ -579,12 +578,12 @@ export class Node {
height: number
} {
return {
- left: this.layout.left,
- top: this.layout.top,
+ left: this.getComputedLeft(),
+ top: this.getComputedTop(),
right: this.getComputedRight(),
bottom: this.getComputedBottom(),
- width: this.layout.width,
- height: this.layout.height
+ width: this.getComputedWidth(),
+ height: this.getComputedHeight()
}
}
getComputedBorder(edge: Edge): number {
@@ -976,11 +975,6 @@ function layoutNode(
forceHeight = false
): void {
_yogaNodesVisited++
-
- if (performLayout) {
- node._rLayoutGen = _generation
- }
-
const style = node.style
const layout = node.layout
const sameGen = node._cGen === _generation && !performLayout
@@ -2231,7 +2225,6 @@ function collectLayoutChildren(node: Node, flow: Node[], abs: Node[]): void {
function roundLayout(node: Node, scale: number, absLeft: number, absTop: number): void {
const l = node.layout
const isText = node.measureFunc !== null
- const wasLaidOut = node._rLayoutGen === _generation
if (
node._rValid &&
@@ -2240,30 +2233,16 @@ function roundLayout(node: Node, scale: number, absLeft: number, absTop: number)
sameFloat(node._rParentAbsTop, absTop) &&
sameFloat(node._rScale, scale) &&
node._rIsText === isText &&
- sameFloat(node._rRoundedLeft, l.left) &&
- sameFloat(node._rRoundedTop, l.top) &&
- sameFloat(node._rRoundedWidth, l.width) &&
- sameFloat(node._rRoundedHeight, l.height)
+ sameFloat(node._rLeft, l.left) &&
+ sameFloat(node._rTop, l.top) &&
+ sameFloat(node._rWidth, l.width) &&
+ sameFloat(node._rHeight, l.height)
) {
_yogaRoundSkips++
return
}
- if (
- node._rValid &&
- !wasLaidOut &&
- sameFloat(l.left, node._rRoundedLeft) &&
- sameFloat(l.top, node._rRoundedTop) &&
- sameFloat(l.width, node._rRoundedWidth) &&
- sameFloat(l.height, node._rRoundedHeight)
- ) {
- l.left = node._rLeft
- l.top = node._rTop
- l.width = node._rWidth
- l.height = node._rHeight
- }
-
_yogaRoundedNodes++
const nodeLeft = l.left
@@ -2283,30 +2262,25 @@ function roundLayout(node: Node, scale: number, absLeft: number, absTop: number)
node._rIsText = isText
if (scale === 0) {
- l.left = nodeLeft
- l.top = nodeTop
- l.width = nodeWidth
- l.height = nodeHeight
+ node._rRoundedLeft = nodeLeft
+ node._rRoundedTop = nodeTop
+ node._rRoundedWidth = nodeWidth
+ node._rRoundedHeight = nodeHeight
} else {
- l.left = roundValue(nodeLeft, scale, false, isText)
- l.top = roundValue(nodeTop, scale, false, isText)
+ node._rRoundedLeft = roundValue(nodeLeft, scale, false, isText)
+ node._rRoundedTop = roundValue(nodeTop, scale, false, isText)
const absRight = absNodeLeft + nodeWidth
const absBottom = absNodeTop + nodeHeight
const hasFracW = !isWholeNumber(nodeWidth * scale)
const hasFracH = !isWholeNumber(nodeHeight * scale)
- l.width =
+ node._rRoundedWidth =
roundValue(absRight, scale, isText && hasFracW, isText && !hasFracW) -
roundValue(absNodeLeft, scale, false, isText)
- l.height =
+ node._rRoundedHeight =
roundValue(absBottom, scale, isText && hasFracH, isText && !hasFracH) -
roundValue(absNodeTop, scale, false, isText)
}
- node._rRoundedLeft = l.left
- node._rRoundedTop = l.top
- node._rRoundedWidth = l.width
- node._rRoundedHeight = l.height
-
for (const c of node.children) {
roundLayout(c, scale, absNodeLeft, absNodeTop)
}
diff --git a/ui-tui/packages/hermes-ink/src/native-ts/yoga-layout/round-layout.test.ts b/ui-tui/packages/hermes-ink/src/native-ts/yoga-layout/round-layout.test.ts
index 12f7b359270..7f709ea18d4 100644
--- a/ui-tui/packages/hermes-ink/src/native-ts/yoga-layout/round-layout.test.ts
+++ b/ui-tui/packages/hermes-ink/src/native-ts/yoga-layout/round-layout.test.ts
@@ -39,45 +39,83 @@ const buildTree = (rootWidth: number, widths: number[], scale: number) => {
return { config, leaves, root }
}
-describe('incremental layout rounding', () => {
- it('skips an unchanged transcript subtree when only the clock changes', () => {
- const config = Yoga.Config.create()
- config.setPointScaleFactor(2)
+interface NodeSpec {
+ w?: number
+ h?: number
+ pad?: number
+ mar?: number
+ row: boolean
+ grow?: number
+ kids: NodeSpec[]
+}
- const root = Yoga.Node.create(config)
- root.setWidth(80)
- root.setHeight(40)
+const buildMutationTree = (spec: NodeSpec) => {
+ const all: Node[] = []
+ const makeNode = (value: NodeSpec): Node => {
+ const node = Yoga.Node.create()
- const transcript = Yoga.Node.create(config)
- transcript.setHeight(39)
- root.insertChild(transcript, 0)
+ if (value.w !== undefined) node.setWidth(value.w)
+ if (value.h !== undefined) node.setHeight(value.h)
+ if (value.pad !== undefined) node.setPadding(1, value.pad)
+ if (value.mar !== undefined) node.setMargin(1, value.mar)
+ if (value.row) node.setFlexDirection(FlexDirection.Row)
+ if (value.grow !== undefined) node.setFlexGrow(value.grow)
- for (let index = 0; index < 500; index++) {
- const row = Yoga.Node.create(config)
- row.setWidth(20.25)
- row.setHeight(0.25)
- transcript.insertChild(row, index)
- }
+ all.push(node)
+ value.kids.forEach((child, index) => node.insertChild(makeNode(child), index))
- const clock = Yoga.Node.create(config)
- clock.setWidth(5.25)
- clock.setHeight(1)
- root.insertChild(clock, 1)
+ return node
+ }
- root.calculateLayout(80, 40)
- const transcriptWidth = transcript.getComputedWidth()
+ return { all, root: makeNode(spec) }
+}
- clock.setWidth(6.25)
- root.calculateLayout(80, 40)
+describe('incremental layout rounding', () => {
+ it('keeps rounding work flat when only the clock changes', () => {
+ const results = [50, 500, 5000].map(rowCount => {
+ const config = Yoga.Config.create()
+ config.setPointScaleFactor(2)
+
+ const root = Yoga.Node.create(config)
+ root.setWidth(80)
+ root.setHeight(40)
+
+ const transcript = Yoga.Node.create(config)
+ transcript.setHeight(39)
+ root.insertChild(transcript, 0)
+
+ for (let index = 0; index < rowCount; index++) {
+ const row = Yoga.Node.create(config)
+ row.setWidth(20.25)
+ row.setHeight(0.25)
+ transcript.insertChild(row, index)
+ }
- const counters = getYogaCounters()
- expect(clock.getComputedWidth()).toBe(6.5)
- expect(transcript.getComputedWidth()).toBe(transcriptWidth)
- expect(counters.rounded).toBeLessThanOrEqual(4)
- expect(counters.roundSkips).toBe(1)
+ const clock = Yoga.Node.create(config)
+ clock.setWidth(5.25)
+ clock.setHeight(1)
+ root.insertChild(clock, 1)
- root.freeRecursive()
- Yoga.Config.destroy(config)
+ root.calculateLayout(80, 40)
+ const transcriptWidth = transcript.getComputedWidth()
+
+ clock.setWidth(6.25)
+ root.calculateLayout(80, 40)
+
+ const counters = getYogaCounters()
+ expect(clock.getComputedWidth()).toBe(6.5)
+ expect(transcript.getComputedWidth()).toBe(transcriptWidth)
+ root.freeRecursive()
+ Yoga.Config.destroy(config)
+
+ return { rounded: counters.rounded, roundSkips: counters.roundSkips }
+ })
+
+ expect(results).toEqual([
+ { rounded: 2, roundSkips: 1 },
+ { rounded: 2, roundSkips: 1 },
+ { rounded: 2, roundSkips: 1 }
+ ])
})
it('re-rounds cached raw geometry when the point scale changes', () => {
@@ -143,4 +181,115 @@ describe('incremental layout rounding', () => {
incremental.root.freeRecursive()
Yoga.Config.destroy(incremental.config)
})
+
+ it('rounds a fractional child after its whole-pixel parent hits the layout cache', () => {
+ const root = Yoga.Node.create()
+ root.setWidth(120)
+ root.setHeight(40)
+ const row = Yoga.Node.create()
+ row.setWidth(120)
+ row.setHeight(2)
+ const leaf = Yoga.Node.create()
+ leaf.setWidth(10.4)
+ leaf.setHeight(1.6)
+ row.insertChild(leaf, 0)
+ root.insertChild(row, 0)
+ root.calculateLayout(120, 40)
+
+ leaf.setWidth(11.4)
+ leaf.setHeight(2.6)
+ root.calculateLayout(120, 40)
+
+ expect(leaf.getComputedLayout()).toMatchObject({ width: 11, height: 3 })
+ root.freeRecursive()
+ })
+
+ it('does not mix cached rounded coordinates with raw dimensions', () => {
+ const spec: NodeSpec = {
+ h: 4.917283,
+ pad: 1.934923,
+ row: false,
+ kids: [
+ {
+ w: 11.007846,
+ row: false,
+ kids: [
+ {
+ mar: 1.306334,
+ row: true,
+ kids: [
+ {
+ mar: 0.199071,
+ row: true,
+ grow: 1.522763,
+ kids: [{ w: 4.6117, h: 5.852709, row: false, kids: [] }]
+ }
+ ]
+ },
+ {
+ w: 40.14272,
+ pad: 1.043468,
+ mar: 1.495136,
+ row: false,
+ kids: [
+ {
+ w: 8.335441,
+ row: false,
+ kids: [
+ { h: 5.249866, mar: 0.997316, row: true, grow: 1.554555, kids: [] },
+ { h: 6.335704, mar: 0.817136, row: false, grow: 0.079169, kids: [] }
+ ]
+ },
+ {
+ w: 32.935694,
+ row: false,
+ grow: 1.781838,
+ kids: [
+ { w: 38.068879, pad: 0.696862, row: true, kids: [] },
+ { pad: 1.66287, row: false, kids: [] }
+ ]
+ }
+ ]
+ }
+ ]
+ }
+ ]
+ }
+ const mutations = [
+ { index: 10, kind: 'height', value: 2.823024 },
+ { index: 6, kind: 'margin', value: 0.858021 },
+ { index: 3, kind: 'grow', value: 1.321691 },
+ { index: 9, kind: 'grow', value: 0.022362 },
+ { index: 6, kind: 'grow', value: 0.812981 },
+ { index: 7, kind: 'width', value: 5.644585 },
+ { index: 2, kind: 'width', value: 20.498063 },
+ { index: 4, kind: 'grow', value: 1.653743 },
+ { index: 1, kind: 'height', value: 2.709275 },
+ { index: 1, kind: 'margin', value: 2.411063 }
+ ] as const
+ const applyMutation = (all: Node[], mutation: (typeof mutations)[number]) => {
+ const node = all[mutation.index]!
+
+ if (mutation.kind === 'width') node.setWidth(mutation.value)
+ else if (mutation.kind === 'height') node.setHeight(mutation.value)
+ else if (mutation.kind === 'margin') node.setMargin(1, mutation.value)
+ else node.setFlexGrow(mutation.value)
+ }
+ const incremental = buildMutationTree(spec)
+ incremental.root.calculateLayout(96, 5)
+
+ for (const mutation of mutations) {
+ applyMutation(incremental.all, mutation)
+ incremental.root.calculateLayout(96, 5)
+ }
+
+ const fresh = buildMutationTree(spec)
+ mutations.forEach(mutation => applyMutation(fresh.all, mutation))
+ fresh.root.calculateLayout(96, 5)
+
+ expect(incremental.all[11]!.getComputedHeight()).toBe(fresh.all[11]!.getComputedHeight())
+ expect(fresh.all[11]!.getComputedHeight()).toBe(2)
+ incremental.root.freeRecursive()
+ fresh.root.freeRecursive()
+ })
})