AI-native design editor. Open-source Figma alternative.
fix(core): let scripts see and unbind a uniform radius a file binds (#1505)
Marcos Velásquezfb4d4607f755
diff --git a/changes/1505/fixed.md b/changes/1505/fixed.mdnew file mode 100644index 000000000..877be61e4--- /dev/null+++ b/changes/1505/fixed.md@@ -0,0 +1 @@+- Splitting a corner radius bound to a variable, in the panel, on the canvas or from a script, keeps every corner bound to it.diff --git a/packages/core/src/figma-api/accessors/visual.ts b/packages/core/src/figma-api/accessors/visual.tsindex 7c7842b01..d4dbd6bbe 100644--- a/packages/core/src/figma-api/accessors/visual.ts+++ b/packages/core/src/figma-api/accessors/visual.ts@@ -1,6 +1,6 @@import type { SceneGraph, SceneNode, Stroke } from '@open-pencil/scene-graph'import { newStrokeGeometry } from '@open-pencil/scene-graph/node-defaults'-import { resolvedPaintBindings } from '@open-pencil/scene-graph/variables'+import { independentCornerChanges, resolvedPaintBindings } from '@open-pencil/scene-graph/variables'import {graph,@@ -173,8 +173,13 @@ export function installVisualNodeProxyAccessors(cornerRadius: {get(this: ProxyThis): number | symbol {const node = raw(this, internals)- if (node.independentCorners) return mixed- return node.cornerRadius+ if (!node.independentCorners) return node.cornerRadius+ // Corners set or bound one by one read as one radius while they agree, as in Figma.+ const { topLeftRadius, topRightRadius, bottomLeftRadius, bottomRightRadius } = node+ const agree = [topRightRadius, bottomLeftRadius, bottomRightRadius].every(+ (radius) => radius === topLeftRadius+ )+ return agree ? topLeftRadius : mixed},set(this: ProxyThis, value: number | symbol) {if (value === mixed) return@@ -212,7 +217,10 @@ function cornerAccessor(return raw(this, internals)[field]},set(this: ProxyThis, value: number) {- updateNode(this, internals, { [field]: value, independentCorners: true })+ updateNode(this, internals, {+ ...independentCornerChanges(raw(this, internals)),+ [field]: value+ })}}}diff --git a/packages/core/src/figma-api/variables/bindings.ts b/packages/core/src/figma-api/variables/bindings.tsindex 71d119ba0..8fb301b31 100644--- a/packages/core/src/figma-api/variables/bindings.ts+++ b/packages/core/src/figma-api/variables/bindings.ts@@ -1,4 +1,5 @@import { uniq } from 'es-toolkit/array'+import { isEmptyObject } from 'es-toolkit/predicate'import {TEXT_BINDABLE_FIELDS,@@ -8,6 +9,8 @@ import {type Variable,type VariableType} from '@open-pencil/scene-graph'+import { recordInstanceOverride } from '@open-pencil/scene-graph/instances'+import { independentCornerChanges } from '@open-pencil/scene-graph/variables'import type { FigmaPaint } from '#core/figma-api/paints'import { reconcileVariableLayouts } from '#core/layout/variables'@@ -136,6 +139,17 @@ function documentFields(field: FigmaBindableNodeField): readonly string[] {return field === 'cornerRadius' ? CORNER_FIELDS : [NODE_FIELDS[field]]}+const isCornerField = (field: string): boolean =>+ field === 'cornerRadius' || (CORNER_FIELDS as readonly string[]).includes(field)++/** Draws the layer corner by corner before a script binds or unbinds one, as Figma does. */+function separateCorners(graph: SceneGraph, node: SceneNode): void {+ const changes = independentCornerChanges(node)+ if (isEmptyObject(changes)) return+ graph.updateNode(node.id, changes)+ recordInstanceOverride(graph, node.id, Object.keys(changes))+}+/*** Binds or unbinds the document fields behind a Figma node field, then shows the variable's value* on the layer and lays out what it moves.@@ -146,6 +160,9 @@ export function setLayerBinding(field: FigmaBindableNodeField,variable: Variable | null): void {+ // Figma keeps the corners' own bindings when a script unbinds the radius as a whole.+ if (field === 'cornerRadius' && !variable) return+ if (isCornerField(field)) separateCorners(graph, node)for (const name of documentFields(field)) {if (variable) graph.bindVariable(node.id, name, variable.id)else graph.unbindVariable(node.id, name)@@ -236,6 +253,9 @@ export function figmaBoundVariables(node: SceneNode): FigmaBoundVariables {const name = FIGMA_FIELD_BY_DOCUMENT_FIELD.get(field)if (name) result[name] = variableAlias(id)}+ // Figma lists a uniform radius by its corners; a file can bind it as one field.+ const radius = node.boundVariables.cornerRadius+ if (radius) for (const corner of CORNER_FIELDS) result[corner] ??= variableAlias(radius)if (node.type === 'TEXT') {for (const field of TEXT_BINDABLE_FIELDS) {const ids = uniq([diff --git a/packages/core/src/geometry/handles/shape.ts b/packages/core/src/geometry/handles/shape.tsindex 1adccc13b..39eb4611f 100644--- a/packages/core/src/geometry/handles/shape.ts+++ b/packages/core/src/geometry/handles/shape.ts@@ -3,6 +3,7 @@ import { clamp } from 'es-toolkit'import type { SceneNode } from '@open-pencil/scene-graph'import { POINT_COUNT_RANGE, polygonCorners } from '@open-pencil/scene-graph/polygon'import type { Vector } from '@open-pencil/scene-graph/primitives'+import { independentCornerChanges } from '@open-pencil/scene-graph/variables'import type { SceneGeometry } from '#core/geometry/scene'import type { ViewportTransform } from '#core/geometry/types'@@ -279,7 +280,7 @@ export function cornerRadiusChanges(}const radii = { ...cornerRadii(node), [corner]: radius }return {- independentCorners: true,+ ...independentCornerChanges(node),topLeftRadius: radii.topLeft,topRightRadius: radii.topRight,bottomRightRadius: radii.bottomRight,diff --git a/packages/core/tests/figma-api/accessors/visual/corner-radius.test.ts b/packages/core/tests/figma-api/accessors/visual/corner-radius.test.tsindex 60a03e62a..f63cce2de 100644--- a/packages/core/tests/figma-api/accessors/visual/corner-radius.test.ts+++ b/packages/core/tests/figma-api/accessors/visual/corner-radius.test.ts@@ -20,4 +20,16 @@ describe('corner radius', () => {// `api.mixed` widens to `symbol`, so the matcher is typed to accept either side.expect<number | symbol>(rect.cornerRadius).toBe(api.mixed)})++ // Figma desktop, 2026-10-26: `mixed` after the first corner, then 5 once all four are 5.+ test('corners set one by one read as one radius once they agree, as in Figma', () => {+ const api = createAPI()+ const rect = api.createRectangle()+ rect.topLeftRadius = 5+ expect<number | symbol>(rect.cornerRadius).toBe(api.mixed)+ rect.topRightRadius = 5+ rect.bottomLeftRadius = 5+ rect.bottomRightRadius = 5+ expect<number | symbol>(rect.cornerRadius).toBe(5)+ })})diff --git a/packages/core/tests/figma-api/variables/collections.test.ts b/packages/core/tests/figma-api/variables/collections.test.tsindex 74e283cba..4f2dd4c58 100644--- a/packages/core/tests/figma-api/variables/collections.test.ts+++ b/packages/core/tests/figma-api/variables/collections.test.ts@@ -188,6 +188,82 @@ describe('node variable bindings', () => {rect.setBoundVariable('minWidth', null)})+ test('shows and unbinds by corner a uniform radius a file binds as one field', () => {+ const { api, mode, size } = tokens()+ const rect = api.createRectangle()+ const corners = [+ 'topLeftRadius',+ 'topRightRadius',+ 'bottomLeftRadius',+ 'bottomRightRadius'+ ] as const+ // `api.mixed` widens to `symbol`, so the radii are typed to hold either.+ const radii = (): (number | symbol)[] => [+ rect.cornerRadius,+ ...corners.map((corner) => rect[corner])+ ]+ // A .pen or .fig file binds a uniform radius as one field.+ api.graph.bindVariable(rect.id, 'cornerRadius', size.id)+ size.setValueForMode(mode, 6)+ expect(Object.keys(rect.boundVariables)).toEqual([...corners])++ // An unbound corner keeps the radius it shows, and the others follow the variable.+ rect.setBoundVariable('topLeftRadius', null)+ expect(Object.keys(rect.boundVariables)).toEqual(corners.slice(1))+ expect(radii()).toEqual([6, 6, 6, 6, 6])+ size.setValueForMode(mode, 10)+ expect(radii()).toEqual([api.mixed, 6, 10, 10, 10])++ for (const corner of corners.slice(1)) rect.setBoundVariable(corner, null)+ rect.cornerRadius = 20+ expect(api.graph.getNode(rect.id)?.boundVariables).toEqual({})+ expect(radii()).toEqual([20, 20, 20, 20, 20])+ })++ test('setting one corner of a radius a file binds as one field keeps the others bound', () => {+ const { api, size } = tokens()+ const rect = api.createRectangle()+ api.graph.bindVariable(rect.id, 'cornerRadius', size.id)+ rect.topLeftRadius = 20+ const bound = api.graph.getNode(rect.id)?.boundVariables ?? {}+ expect(bound).not.toHaveProperty('cornerRadius')+ expect(bound).toMatchObject({+ topRightRadius: size.id,+ bottomLeftRadius: size.id,+ bottomRightRadius: size.id+ })+ })++ // Figma desktop, 2026-10-26: unbinding `cornerRadius` leaves every corner's binding in place.+ test('unbinding the radius as a whole keeps the corners bound', () => {+ const { api, size } = tokens()+ const rect = api.createRectangle()+ rect.setBoundVariable('cornerRadius', size)+ rect.setBoundVariable('cornerRadius', null)+ expect(Object.keys(rect.boundVariables)).toEqual([+ 'topLeftRadius',+ 'topRightRadius',+ 'bottomLeftRadius',+ 'bottomRightRadius'+ ])+ })++ test('a radius bound as a whole is drawn at the variable’s value', () => {+ const { api, mode, size } = tokens()+ const rect = api.createRectangle()+ rect.setBoundVariable('cornerRadius', size)+ size.setValueForMode(mode, 10)+ expect(rect.cornerRadius).toBe(10)+ // The layer draws its corners, which hold the bound value, not a radius left at 0.+ expect(api.graph.getNode(rect.id)).toMatchObject({+ independentCorners: true,+ topLeftRadius: 10,+ topRightRadius: 10,+ bottomLeftRadius: 10,+ bottomRightRadius: 10+ })+ })+test('setBoundVariable takes only the type Figma’s API allows for each field', () => {const { api, size, label, flag, color } = tokens()const rect = api.createRectangle()diff --git a/packages/core/tests/geometry/handles/shape.test.ts b/packages/core/tests/geometry/handles/shape.test.tsindex 4c26f3d57..7304f6619 100644--- a/packages/core/tests/geometry/handles/shape.test.ts+++ b/packages/core/tests/geometry/handles/shape.test.ts@@ -1,5 +1,7 @@import { describe, expect, test } from 'bun:test'+import { SceneGraph, type SceneNode } from '@open-pencil/scene-graph'+import {cornerRadiusAtPoint,cornerRadiusChanges,@@ -10,7 +12,6 @@ import {pointCountAtPoint,starRatioAtPoint} from '#core/geometry'-import { SceneGraph, type SceneNode } from '@open-pencil/scene-graph'// Values measured on a 160 × 232 rectangle in Figma desktop, 2026-10-10.@@ -156,9 +157,28 @@ describe('shape handles', () => {expect(snapped).toEqual([20, 30, 30, 40, 50])})+ test('move a binding of the whole radius to the corners when one is dragged alone', () => {+ const { graph, node } = rectangle({ cornerRadius: 20 })+ const radius = graph.createVariable('Radius', 'FLOAT', graph.createCollection('Tokens').id, 20)+ graph.bindVariable(node.id, 'cornerRadius', radius.id)+ const changes = cornerRadiusChanges(graph.getNode(node.id) ?? node, 'bottomRight', 30, true)++ expect(changes).toMatchObject({ independentCorners: true, bottomRightRadius: 30 })+ expect(Object.keys(changes.boundVariables ?? {}).toSorted()).toEqual([+ 'bottomLeftRadius',+ 'bottomRightRadius',+ 'topLeftRadius',+ 'topRightRadius'+ ])+ })+test('drag equal corners together and differing ones alone, Alt reversing either', () => {const uniform = rectangle({ cornerRadius: 20 }).node- const mixed = rectangle({ independentCorners: true, topLeftRadius: 10, topRightRadius: 40 }).node+ const mixed = rectangle({+ independentCorners: true,+ topLeftRadius: 10,+ topRightRadius: 40+ }).nodeexpect(dragsSingleCorner(uniform, false)).toBe(false)expect(dragsSingleCorner(uniform, true)).toBe(true)expect(dragsSingleCorner(mixed, false)).toBe(true)diff --git a/packages/scene-graph/src/variables/corners.ts b/packages/scene-graph/src/variables/corners.tsnew file mode 100644index 000000000..9ba0531fe--- /dev/null+++ b/packages/scene-graph/src/variables/corners.ts@@ -0,0 +1,39 @@+import { omit } from 'es-toolkit/object'++import type { SceneNode } from '../types'++const CORNERS = [+ 'topLeftRadius',+ 'topRightRadius',+ 'bottomLeftRadius',+ 'bottomRightRadius'+] as const++/**+ * The changes that draw a layer's radius corner by corner, as Figma keeps it once corners can+ * differ: each corner not bound on its own takes the radius the layer shows, and a binding of the+ * whole radius, which a .fig or .pen file holds as one field, moves to those corners, so each+ * corner can then change or be unbound alone.+ */+export function independentCornerChanges(node: SceneNode): Partial<SceneNode> {+ const changes: Partial<SceneNode> = {}+ if (!node.independentCorners) {+ changes.independentCorners = true+ // A corner bound on its own keeps the value its variable gave it.+ for (const corner of CORNERS)+ if (!node.boundVariables[corner]) changes[corner] = node.cornerRadius+ }+ const variableId = node.boundVariables.cornerRadius+ if (!variableId) return changes+ const unbound = CORNERS.filter((corner) => !node.boundVariables[corner])+ const scale = node.variableBindingScales.cornerRadius+ changes.boundVariables = {+ ...omit(node.boundVariables, ['cornerRadius']),+ ...Object.fromEntries(unbound.map((corner) => [corner, variableId]))+ }+ changes.variableBindingScales = {+ ...omit(node.variableBindingScales, ['cornerRadius']),+ ...(scale === undefined ? {} : Object.fromEntries(unbound.map((corner) => [corner, scale])))+ }+ return changes+}diff --git a/packages/scene-graph/src/variables/index.ts b/packages/scene-graph/src/variables/index.tsindex 478cce7c0..3fbb582f4 100644--- a/packages/scene-graph/src/variables/index.ts+++ b/packages/scene-graph/src/variables/index.ts@@ -337,6 +337,7 @@ function markBoundVariablesOverrideOnInstance(}export * from './bindings'+export * from './corners'export * from './formula'export * from './formula/schema'export * from './schema'diff --git a/packages/scene-graph/tests/variables/corners.test.ts b/packages/scene-graph/tests/variables/corners.test.tsnew file mode 100644index 000000000..084380e6e--- /dev/null+++ b/packages/scene-graph/tests/variables/corners.test.ts@@ -0,0 +1,62 @@+import { describe, expect, test } from 'bun:test'++import { SceneGraph } from '@open-pencil/scene-graph'+import { independentCornerChanges } from '@open-pencil/scene-graph/variables'++function rectangle() {+ const graph = new SceneGraph()+ const collection = graph.createCollection('Tokens')+ const radius = graph.createVariable('Radius', 'FLOAT', collection.id, 8)+ const corner = graph.createVariable('Corner', 'FLOAT', collection.id, 2)+ const node = graph.createNode('RECTANGLE', graph.getPages()[0].id, { cornerRadius: 8 })+ return { graph, node, radius, corner }+}++describe('drawing a radius corner by corner', () => {+ test('gives each corner the radius the layer shows', () => {+ const { node } = rectangle()+ expect(independentCornerChanges(node)).toEqual({+ independentCorners: true,+ topLeftRadius: 8,+ topRightRadius: 8,+ bottomRightRadius: 8,+ bottomLeftRadius: 8+ })+ })++ test('moves a binding of the whole radius to the corners not bound alone', () => {+ const { graph, node, radius, corner } = rectangle()+ graph.bindVariable(node.id, 'cornerRadius', radius.id)+ graph.bindVariable(node.id, 'topLeftRadius', corner.id)+ const changes = independentCornerChanges(graph.getNode(node.id) ?? node)++ expect(changes.boundVariables).toEqual({+ topLeftRadius: corner.id,+ topRightRadius: radius.id,+ bottomRightRadius: radius.id,+ bottomLeftRadius: radius.id+ })+ expect(Object.keys(changes.variableBindingScales ?? {}).toSorted()).toEqual([+ 'bottomLeftRadius',+ 'bottomRightRadius',+ 'topLeftRadius',+ 'topRightRadius'+ ])+ })++ test('leaves the radius of a corner bound on its own', () => {+ const { graph, node, radius, corner } = rectangle()+ graph.bindVariable(node.id, 'cornerRadius', radius.id)+ graph.bindVariable(node.id, 'topLeftRadius', corner.id)+ graph.updateNode(node.id, { topLeftRadius: 2 })+ const changes = independentCornerChanges(graph.getNode(node.id) ?? node)+ expect(changes).not.toHaveProperty('topLeftRadius')+ expect(changes).toMatchObject({ topRightRadius: 8, bottomLeftRadius: 8, bottomRightRadius: 8 })+ })++ test('changes nothing on corners already drawn alone and bound alone', () => {+ const { graph, node } = rectangle()+ graph.updateNode(node.id, { independentCorners: true })+ expect(independentCornerChanges(graph.getNode(node.id) ?? node)).toEqual({})+ })+})diff --git a/packages/vue/src/controls/appearance/helpers.ts b/packages/vue/src/controls/appearance/helpers.tsindex 4c7878f6e..42a83ede1 100644--- a/packages/vue/src/controls/appearance/helpers.ts+++ b/packages/vue/src/controls/appearance/helpers.ts@@ -4,6 +4,7 @@ import type { ComputedRef, Ref } from 'vue'import type { Editor } from '@open-pencil/core'import type { BlendMode, SceneNode } from '@open-pencil/scene-graph'+import { independentCornerChanges } from '@open-pencil/scene-graph/variables'import type { CornerGeometryKey, CornerRadiusKey } from '#vue/controls/appearance/types'import { MIXED, type MixedValue } from '#vue/controls/node-props/use'@@ -279,13 +280,7 @@ export function createAppearanceActions({if (target.independentCorners) continueeditor.updateNodeWithUndo(target.id,- {- independentCorners: true,- topLeftRadius: target.cornerRadius,- topRightRadius: target.cornerRadius,- bottomRightRadius: target.cornerRadius,- bottomLeftRadius: target.cornerRadius- } as Partial<SceneNode>,+ independentCornerChanges(target),'Independent corner radii')} else {diff --git a/tests/engine/vue/controls/appearance.test.ts b/tests/engine/vue/controls/appearance.test.tsindex b00bdc3a4..6f2b26b21 100644--- a/tests/engine/vue/controls/appearance.test.ts+++ b/tests/engine/vue/controls/appearance.test.ts@@ -109,6 +109,32 @@ describe('appearance control state', () => {expect(createAppearanceState(options).showIndependentCorners.value).toBe(false)})+ test('splitting a radius bound as a whole binds each corner, undoably', () => {+ const graph = makeSceneGraph()+ const rect = graph.createNode('RECTANGLE', firstPageId(graph), {+ cornerRadius: 8,+ boundVariables: { cornerRadius: 'radius' }+ })+ const editor = createEditor({ graph })+ const actions = createAppearanceActions({+ expandedCornerNodeId: ref<string | null>(null),+ editor,+ node: computed(() => graph.getNode(rect.id) ?? null),+ nodes: computed(() => []),+ isMulti: computed(() => false),+ merged: <K extends keyof SceneNode>(): MixedValue<SceneNode[K]> => MIXED+ })+ actions.toggleIndependentCorners()+ expect(graph.getNode(rect.id)?.boundVariables).toEqual({+ topLeftRadius: 'radius',+ topRightRadius: 'radius',+ bottomLeftRadius: 'radius',+ bottomRightRadius: 'radius'+ })+ editor.undo.undo()+ expect(graph.getNode(rect.id)?.boundVariables).toEqual({ cornerRadius: 'radius' })+ })+test('keeps equal independent corners expanded for distinct bindings', () => {const node = rectangle()node.independentCorners = true