Skip to content

AI-native design editor. Open-source Figma alternative.

fix(canvas): edit section labels inline (#608)

Danila Poyarkov35667d84b5a5
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 5cf688449..889a04c14 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -46,6 +46,7 @@
### Changed
+- Vertically center shaped section titles and allow renaming a section by double-clicking its canvas label.
- Load supported online fonts in the browser before revealing imported pages, preserve substituted text during editing, and shape canvas labels with bundled Inter typography.
- Upgrade CanvasKit to 0.41 and migrate renderer geometry to immutable paths built through `PathBuilder`.
- Upgrade direct model chat providers and transports to AI SDK 7 while retaining the local ACP execution path.
diff --git a/packages/core/src/canvas/index.ts b/packages/core/src/canvas/index.ts
index f6352947e..713ac8cc5 100644
--- a/packages/core/src/canvas/index.ts
+++ b/packages/core/src/canvas/index.ts
@@ -13,4 +13,5 @@ export {
export { computeGuideRedline } from './guides/redlines'
export { hitTestGuides, type GuideHit } from './guides/hit-test'
export type { GuideOverlayState, GuidePreview, GuideSelection } from './guides/types'
+export { canvasLabelForeground } from './labels/color'
export { SkiaRenderer, type RenderOverlays, type RulerTheme } from './renderer'
diff --git a/packages/core/src/canvas/labels/color.ts b/packages/core/src/canvas/labels/color.ts
new file mode 100644
index 000000000..344bd0273
--- /dev/null
+++ b/packages/core/src/canvas/labels/color.ts
@@ -0,0 +1,35 @@
+import { wcagContrast } from 'culori'
+
+import type { Color } from '@open-pencil/scene-graph'
+
+import { BLACK } from '#core/constants'
+
+const WHITE: Color = { r: 1, g: 1, b: 1, a: 1 }
+
+function compositeColor(foreground: Color, background: Color): Color {
+ const alpha = foreground.a
+ return {
+ r: foreground.r * alpha + background.r * (1 - alpha),
+ g: foreground.g * alpha + background.g * (1 - alpha),
+ b: foreground.b * alpha + background.b * (1 - alpha),
+ a: 1
+ }
+}
+
+export function canvasLabelForeground(background: Color, canvasBackground: Color = WHITE): Color {
+ const composited = compositeColor(background, canvasBackground)
+ const color = { mode: 'rgb' as const, r: composited.r, g: composited.g, b: composited.b }
+ const blackContrast = wcagContrast(color, {
+ mode: 'rgb',
+ r: BLACK.r,
+ g: BLACK.g,
+ b: BLACK.b
+ })
+ const whiteContrast = wcagContrast(color, {
+ mode: 'rgb',
+ r: WHITE.r,
+ g: WHITE.g,
+ b: WHITE.b
+ })
+ return blackContrast >= whiteContrast ? BLACK : WHITE
+}
diff --git a/packages/core/src/canvas/labels/draw.ts b/packages/core/src/canvas/labels/draw.ts
index 5673381c0..82474aee5 100644
--- a/packages/core/src/canvas/labels/draw.ts
+++ b/packages/core/src/canvas/labels/draw.ts
@@ -2,6 +2,7 @@ import type { Canvas } from 'canvaskit-wasm'
import type { SceneNode, SceneGraph } from '@open-pencil/scene-graph'
+import { canvasLabelForeground } from '#core/canvas/labels/color'
import type { SkiaRenderer } from '#core/canvas/renderer'
import {
SECTION_TITLE_HEIGHT,
@@ -47,11 +48,11 @@ function drawSectionTitle(
node.fills.length > 0 && node.fills[0].visible
? r.resolveFillColor(node.fills[0], 0, node, graph)
: { r: 0.37, g: 0.37, b: 0.37, a: 1 }
- const lum = 0.299 * pillColor.r + 0.587 * pillColor.g + 0.114 * pillColor.b
- const textColor = lum > 0.5 ? r.ck.BLACK : r.ck.WHITE
+ const foreground = canvasLabelForeground(pillColor, r.pageColor)
+ const textColor = r.ck.Color4f(foreground.r, foreground.g, foreground.b, foreground.a)
const maxTextW = Math.max(1, maxPillW - SECTION_TITLE_PADDING_X * 2)
- const textWidth = r.labelParagraphCache.measure(
+ const textMetrics = r.labelParagraphCache.measure(
r.ck,
provider,
node.name,
@@ -60,7 +61,7 @@ function drawSectionTitle(
textColor,
r.fontGeneration
)
- const pillW = Math.min(textWidth + SECTION_TITLE_PADDING_X * 2, maxPillW)
+ const pillW = Math.min(textMetrics.width + SECTION_TITLE_PADDING_X * 2, maxPillW)
const pillH = SECTION_TITLE_HEIGHT
const localPillX = 0
const localPillY = nested ? SECTION_TITLE_GAP : -pillH - SECTION_TITLE_GAP
@@ -86,7 +87,7 @@ function drawSectionTitle(
textColor,
r.fontGeneration,
localPillX + SECTION_TITLE_PADDING_X,
- localPillY
+ localPillY + (pillH - textMetrics.height) / 2
)
canvas.restore()
}
diff --git a/packages/core/src/canvas/labels/paragraph-cache.ts b/packages/core/src/canvas/labels/paragraph-cache.ts
index aaf94addb..fc60348df 100644
--- a/packages/core/src/canvas/labels/paragraph-cache.ts
+++ b/packages/core/src/canvas/labels/paragraph-cache.ts
@@ -3,6 +3,7 @@ import type { Canvas, CanvasKit, Paragraph, TypefaceFontProvider } from 'canvask
interface LabelParagraphEntry {
paragraph: Paragraph
width: number
+ height: number
}
const MAX_LABEL_PARAGRAPHS = 512
@@ -19,8 +20,8 @@ export class LabelParagraphCache {
maxWidth: number,
color: Float32Array,
generation: number
- ): number {
- return this.entry(ck, provider, text, fontSize, maxWidth, color, generation).width
+ ): Pick<LabelParagraphEntry, 'width' | 'height'> {
+ return this.entry(ck, provider, text, fontSize, maxWidth, color, generation)
}
draw(
@@ -76,7 +77,11 @@ export class LabelParagraphCache {
const paragraph = builder.build()
builder.delete()
paragraph.layout(boundedWidth)
- entry = { paragraph, width: Math.min(paragraph.getLongestLine(), boundedWidth) }
+ entry = {
+ paragraph,
+ width: Math.min(paragraph.getLongestLine(), boundedWidth),
+ height: paragraph.getHeight()
+ }
this.entries.set(key, entry)
this.evict()
} else {
diff --git a/packages/vue/src/canvas/labels/edit.ts b/packages/vue/src/canvas/labels/edit.ts
new file mode 100644
index 000000000..bb0821121
--- /dev/null
+++ b/packages/vue/src/canvas/labels/edit.ts
@@ -0,0 +1,49 @@
+import { ref, type Ref } from 'vue'
+
+import type { Editor } from '@open-pencil/core/editor'
+import type { SceneNode } from '@open-pencil/scene-graph'
+
+export type CanvasLabelKind = 'section-title' | 'frame-title' | 'component-label'
+
+export interface CanvasLabelEdit {
+ nodeId: string
+ kind: CanvasLabelKind
+ value: string
+}
+
+export interface CanvasLabelEditController {
+ edit: Ref<CanvasLabelEdit | null>
+ start: (kind: CanvasLabelKind, node: SceneNode) => void
+ update: (value: string) => void
+ commit: () => void
+ cancel: () => void
+}
+
+export function createCanvasLabelEdit(editor: Editor): CanvasLabelEditController {
+ const edit = ref<CanvasLabelEdit | null>(null)
+
+ function start(kind: CanvasLabelKind, node: SceneNode) {
+ edit.value = { nodeId: node.id, kind, value: node.name }
+ }
+
+ function update(value: string) {
+ if (edit.value) edit.value = { ...edit.value, value }
+ }
+
+ function commit() {
+ const current = edit.value
+ if (!current) return
+ const node = editor.graph.getNode(current.nodeId)
+ if (node) {
+ const value = current.value.trim() || node.name
+ if (value !== node.name) editor.updateNodeWithUndo(node.id, { name: value }, 'Rename layer')
+ }
+ edit.value = null
+ }
+
+ function cancel() {
+ edit.value = null
+ }
+
+ return { edit, start, update, commit, cancel }
+}
diff --git a/packages/vue/src/canvas/text-edit/input.ts b/packages/vue/src/canvas/text-edit/input.ts
index 919abfb99..31cd5b15f 100644
--- a/packages/vue/src/canvas/text-edit/input.ts
+++ b/packages/vue/src/canvas/text-edit/input.ts
@@ -1,6 +1,7 @@
import type { Editor } from '@open-pencil/core/editor'
import type { SceneNode } from '@open-pencil/scene-graph'
+import type { CanvasLabelKind } from '#vue/canvas/labels/edit'
import type { DragState } from '#vue/shared/input/types'
type NodeEditMethods = Partial<{ enterNodeEditMode: (nodeId: string) => void }>
@@ -16,6 +17,7 @@ type TextEditInputOptions = {
hitTestComponentLabel: (cx: number, cy: number) => SceneNode | null
getClickCount: () => number
wasSelectedBeforeClickSequence: (id: string) => boolean
+ onEditCanvasLabel?: (kind: CanvasLabelKind, node: SceneNode) => void
setDrag: SetDrag
}
@@ -28,6 +30,7 @@ export function createTextEditInput(options: TextEditInputOptions) {
hitTestComponentLabel,
getClickCount,
wasSelectedBeforeClickSequence,
+ onEditCanvasLabel,
setDrag
} = options
@@ -86,30 +89,46 @@ export function createTextEditInput(options: TextEditInputOptions) {
return null
}
+ function startSectionTitleRename(cx: number, cy: number): boolean {
+ const sectionTitle = hitTestSectionTitle(cx, cy)
+ if (sectionTitle?.type !== 'SECTION') return false
+ editor.select([sectionTitle.id])
+ onEditCanvasLabel?.('section-title', sectionTitle)
+ return true
+ }
+
+ function handleContainerDoubleClick(
+ selectedId: string | undefined,
+ cx: number,
+ cy: number
+ ): boolean {
+ const selectedNode = selectedId ? editor.graph.getNode(selectedId) : undefined
+ if (
+ !selectedNode ||
+ !selectedId ||
+ !editor.graph.isContainer(selectedId) ||
+ selectedNode.locked
+ ) {
+ return false
+ }
+ const hit = getContainerDescendantHit(selectedId, cx, cy)
+ editor.enterContainer(selectedId)
+ if (hit?.type === 'TEXT') startTextEditingAt(hit, cx, cy)
+ else if (hit) editor.select([hit.id])
+ else editor.clearSelection()
+ return true
+ }
+
function onDblClick(e: MouseEvent) {
const nodeEditEditor = editor as Editor & NodeEditMethods
if (editor.state.editingTextId) return
const { cx, cy } = getCoords(e)
+ if (startSectionTitleRename(cx, cy)) return
const selectedId =
editor.state.selectedIds.size === 1 ? [...editor.state.selectedIds][0] : undefined
- const selectedNode = selectedId ? editor.graph.getNode(selectedId) : undefined
- const canEnter =
- selectedNode && selectedId && editor.graph.isContainer(selectedId) && !selectedNode.locked
-
- if (canEnter) {
- const hit = getContainerDescendantHit(selectedId, cx, cy)
- editor.enterContainer(selectedId)
- if (hit?.type === 'TEXT') {
- startTextEditingAt(hit, cx, cy)
- } else if (hit) {
- editor.select([hit.id])
- } else {
- editor.clearSelection()
- }
- return
- }
+ if (handleContainerDoubleClick(selectedId, cx, cy)) return
const hit =
hitTestSectionTitle(cx, cy) ?? hitTestComponentLabel(cx, cy) ?? hitTestInScope(cx, cy, true)
diff --git a/packages/vue/src/canvas/useCanvasInput.ts b/packages/vue/src/canvas/useCanvasInput.ts
index b240c8a72..1fd455e8e 100644
--- a/packages/vue/src/canvas/useCanvasInput.ts
+++ b/packages/vue/src/canvas/useCanvasInput.ts
@@ -5,6 +5,7 @@ import type { Editor } from '@open-pencil/core/editor'
import type { SceneNode } from '@open-pencil/scene-graph'
import { createGuideInput, selectedTopLevelGuideFrameId } from '#vue/canvas/guides/input'
+import { createCanvasLabelEdit } from '#vue/canvas/labels/edit'
import { handlePenDragMove, updatePenHover } from '#vue/canvas/pen/input'
import { createCanvasPointer } from '#vue/canvas/pointer/use'
import { createTextEditInput } from '#vue/canvas/text-edit/input'
@@ -44,6 +45,7 @@ export function useCanvasInput(
isEnabled: () => boolean = () => true
) {
const drag = ref<DragState | null>(null)
+ const canvasLabelEdit = createCanvasLabelEdit(editor)
const cursorOverride = ref<string | null>(null)
const autoLayoutPaddingEdit = ref<{
nodeId: string
@@ -138,6 +140,7 @@ export function useCanvasInput(
hitTestComponentLabel,
getClickCount,
wasSelectedBeforeClickSequence: (id) => selectedIdsBeforeClickSequence.value.has(id),
+ onEditCanvasLabel: canvasLabelEdit.start,
setDrag
})
@@ -474,6 +477,10 @@ export function useCanvasInput(
return {
drag,
cursorOverride,
+ canvasLabelEdit: canvasLabelEdit.edit,
+ updateCanvasLabelEdit: canvasLabelEdit.update,
+ commitCanvasLabelEdit: canvasLabelEdit.commit,
+ cancelCanvasLabelEdit: canvasLabelEdit.cancel,
autoLayoutPaddingEdit,
updateAutoLayoutPaddingEdit,
commitAutoLayoutPaddingEdit,
diff --git a/packages/vue/src/index.ts b/packages/vue/src/index.ts
index 92deb3630..af7c340cf 100644
--- a/packages/vue/src/index.ts
+++ b/packages/vue/src/index.ts
@@ -21,6 +21,7 @@ export { provideEditor, useEditor, EDITOR_KEY } from '#vue/editor/context'
export { useCanvas } from '#vue/canvas/surface/use'
export type { UseCanvasOptions } from '#vue/canvas/surface/use'
export { useCanvasInput } from '#vue/canvas/useCanvasInput'
+export type { CanvasLabelEdit, CanvasLabelKind } from '#vue/canvas/labels/edit'
export { useCanvasVirtualReference } from '#vue/canvas/overlays/useCanvasVirtualReference'
export { useTextEdit } from '#vue/canvas/text-edit/use'
export { useCanvasDrop, extractImageFilesFromClipboard } from '#vue/canvas/drop/use'
diff --git a/src/components/EditorCanvas.vue b/src/components/EditorCanvas.vue
index dd661b786..2ac184b9b 100644
--- a/src/components/EditorCanvas.vue
+++ b/src/components/EditorCanvas.vue
@@ -29,6 +29,8 @@ import IconLucidePanelBottom from '~icons/lucide/panel-bottom'
import IconLucidePanelLeft from '~icons/lucide/panel-left'
import IconLucidePanelRight from '~icons/lucide/panel-right'
import IconLucidePanelTop from '~icons/lucide/panel-top'
+import CanvasLabelEditor from './canvas/labels/CanvasLabelEditor.vue'
+import { canvasLabelPresentation } from './canvas/labels/presentation'
import CanvasMenu from './canvas/CanvasMenu.vue'
import PreparationOverlay from '@/components/preparation/canvas/Overlay.vue'
import NumberField from './inputs/NumberField.vue'
@@ -86,6 +88,10 @@ const { hitTestSectionTitle, hitTestComponentLabel, hitTestFrameTitle } = useCan
)
const {
cursorOverride,
+ canvasLabelEdit,
+ updateCanvasLabelEdit,
+ commitCanvasLabelEdit,
+ cancelCanvasLabelEdit,
autoLayoutPaddingEdit,
updateAutoLayoutPaddingEdit,
commitAutoLayoutPaddingEdit,
@@ -117,6 +123,21 @@ const paddingSideIcons = {
left: IconLucidePanelLeft
} satisfies Record<'top' | 'right' | 'bottom' | 'left', Component>
+const canvasLabelEditNode = computed(() => {
+ const edit = canvasLabelEdit.value
+ return edit ? store.graph.getNode(edit.nodeId) : null
+})
+const canvasLabelEditAnchor = computed(() => {
+ const node = canvasLabelEditNode.value
+ if (!node) return null
+ const abs = store.graph.getAbsolutePosition(node.id)
+ return { x: abs.x, y: abs.y }
+})
+const canvasLabelEditReference = useCanvasVirtualReference(canvasRef, store, canvasLabelEditAnchor)
+const canvasLabelEditPresentation = computed(() =>
+ canvasLabelPresentation(store, canvasLabelEditNode.value ?? null)
+)
+
const paddingEditorAnchor = computed(() => {
const edit = autoLayoutPaddingEdit.value
if (!edit) return null
@@ -178,6 +199,14 @@ const cursor = computed(() => toolCursor(store.state.activeTool, cursorOverride.
class="pointer-events-none absolute inset-0 z-40 border-2 border-dashed border-accent/60 bg-accent/5"
/>
</Transition>
+ <CanvasLabelEditor
+ :edit="canvasLabelEdit"
+ :presentation="canvasLabelEditPresentation"
+ :reference="canvasLabelEditReference"
+ @update="updateCanvasLabelEdit"
+ @commit="commitCanvasLabelEdit"
+ @cancel="cancelCanvasLabelEdit"
+ />
<PopoverRoot :open="!!autoLayoutPaddingEdit">
<PopoverPortal>
<PopoverContent
diff --git a/src/components/canvas/labels/CanvasLabelEditor.vue b/src/components/canvas/labels/CanvasLabelEditor.vue
new file mode 100644
index 000000000..01649bac8
--- /dev/null
+++ b/src/components/canvas/labels/CanvasLabelEditor.vue
@@ -0,0 +1,52 @@
+<script setup lang="ts">
+import type { ReferenceElement } from 'reka-ui'
+import { PopoverContent, PopoverPortal, PopoverRoot } from 'reka-ui'
+
+import { colorToCSS } from '@open-pencil/core/color'
+import type { CanvasLabelEdit } from '@open-pencil/vue'
+
+import InlineLabelEditor from '@/components/ui/inline-label-editor/InlineLabelEditor.vue'
+import type { CanvasLabelPresentation } from '@/components/canvas/labels/presentation'
+
+const { edit, presentation, reference } = defineProps<{
+ edit: CanvasLabelEdit | null
+ presentation: CanvasLabelPresentation
+ reference: ReferenceElement | null
+}>()
+
+const emit = defineEmits<{
+ update: [value: string]
+ commit: []
+ cancel: []
+}>()
+</script>
+
+<template>
+ <PopoverRoot :open="!!edit">
+ <PopoverPortal>
+ <PopoverContent
+ v-if="edit && reference"
+ :reference="reference"
+ side="top"
+ align="start"
+ :side-offset="6"
+ :collision-padding="8"
+ :data-label-kind="edit.kind"
+ :data-foreground="presentation.foreground"
+ :style="{ backgroundColor: colorToCSS(presentation.background) }"
+ class="z-50 h-6 rounded-[5px] text-black shadow-sm ring-1 ring-accent data-[foreground=light]:text-white"
+ @open-auto-focus.prevent
+ @escape-key-down.prevent="emit('cancel')"
+ @pointer-down-outside="emit('commit')"
+ >
+ <InlineLabelEditor
+ :model-value="edit.value"
+ label="Layer name"
+ @update:model-value="emit('update', $event)"
+ @commit="emit('commit')"
+ @cancel="emit('cancel')"
+ />
+ </PopoverContent>
+ </PopoverPortal>
+ </PopoverRoot>
+</template>
diff --git a/src/components/canvas/labels/presentation.ts b/src/components/canvas/labels/presentation.ts
new file mode 100644
index 000000000..d8b43c7de
--- /dev/null
+++ b/src/components/canvas/labels/presentation.ts
@@ -0,0 +1,23 @@
+import { canvasLabelForeground } from '@open-pencil/core/canvas'
+import type { Editor } from '@open-pencil/core/editor'
+import type { Color, SceneNode } from '@open-pencil/scene-graph'
+
+export interface CanvasLabelPresentation {
+ background: Color
+ foreground: 'dark' | 'light'
+}
+
+const DEFAULT_LABEL_BACKGROUND: Color = { r: 0.37, g: 0.37, b: 0.37, a: 1 }
+
+export function canvasLabelPresentation(
+ editor: Editor,
+ node: SceneNode | null
+): CanvasLabelPresentation {
+ const fill = node?.fills[0]
+ const background =
+ node && fill?.visible
+ ? (editor.renderer?.resolveFillColor(fill, 0, node, editor.graph) ?? fill.color)
+ : DEFAULT_LABEL_BACKGROUND
+ const foreground = canvasLabelForeground(background, editor.state.pageColor)
+ return { background, foreground: foreground.r === 0 ? 'dark' : 'light' }
+}
diff --git a/src/components/ui/inline-label-editor/InlineLabelEditor.vue b/src/components/ui/inline-label-editor/InlineLabelEditor.vue
new file mode 100644
index 000000000..272c4d861
--- /dev/null
+++ b/src/components/ui/inline-label-editor/InlineLabelEditor.vue
@@ -0,0 +1,53 @@
+<script setup lang="ts">
+import { nextTick, ref, watch } from 'vue'
+
+const { modelValue, label } = defineProps<{
+ modelValue: string
+ label: string
+}>()
+
+const emit = defineEmits<{
+ 'update:modelValue': [value: string]
+ commit: []
+ cancel: []
+}>()
+
+const input = ref<HTMLInputElement | null>(null)
+
+function updateValue(event: Event) {
+ const target = event.target
+ if (target instanceof HTMLInputElement) emit('update:modelValue', target.value)
+}
+
+watch(
+ () => input.value,
+ (element) => {
+ if (!element) return
+ void nextTick(() => {
+ element.focus()
+ element.select()
+ })
+ },
+ { immediate: true }
+)
+</script>
+
+<template>
+ <div class="grid h-full">
+ <span
+ aria-hidden="true"
+ class="invisible col-start-1 row-start-1 h-full whitespace-pre px-2 text-xs leading-6"
+ >{{ modelValue || ' ' }}</span
+ >
+ <input
+ ref="input"
+ :value="modelValue"
+ :aria-label="label"
+ class="col-start-1 row-start-1 h-full w-0 min-w-full bg-transparent px-2 text-xs leading-6 outline-none"
+ @input="updateValue"
+ @keydown.enter.prevent="emit('commit')"
+ @keydown.escape.prevent="emit('cancel')"
+ @blur="emit('commit')"
+ />
+ </div>
+</template>
diff --git a/tests/e2e/canvas/labels/section-title-editing.spec.ts b/tests/e2e/canvas/labels/section-title-editing.spec.ts
new file mode 100644
index 000000000..92d30a2ab
--- /dev/null
+++ b/tests/e2e/canvas/labels/section-title-editing.spec.ts
@@ -0,0 +1,87 @@
+import { expect, test, useEditorSetupWithClear } from '#tests/e2e/fixtures'
+
+const editor = useEditorSetupWithClear('/?test&no-chrome&no-rulers')
+
+test('editing section labels preserve dark and light label presentation', async () => {
+ const sections = [
+ { id: 'dark-section', y: 160, color: { r: 0.37, g: 0.37, b: 0.37, a: 1 } },
+ { id: 'light-section', y: 400, color: { r: 0.92, g: 0.82, b: 0.38, a: 1 } }
+ ] as const
+ await editor.page.evaluate((items) => {
+ const store = window.openPencil?.getStore?.()
+ if (!store) throw new Error('OpenPencil store not initialized')
+ for (const item of items) {
+ store.graph.createNode('SECTION', store.state.currentPageId, {
+ id: item.id,
+ name: 'Components',
+ x: 120,
+ y: item.y,
+ width: 280,
+ height: 180,
+ fills: [{ type: 'SOLID', color: item.color, visible: true, opacity: 1 }]
+ })
+ }
+ store.requestRender()
+ }, sections)
+ await editor.canvas.waitForRender()
+
+ const canvas = editor.page.getByTestId('canvas-element')
+ const input = editor.page.getByRole('textbox', { name: 'Layer name' })
+ const expected = [
+ { y: 142, background: 'rgb(94, 94, 94)', foreground: 'rgb(255, 255, 255)' },
+ { y: 382, background: 'rgb(235, 209, 97)', foreground: 'rgb(0, 0, 0)' }
+ ]
+
+ for (const item of expected) {
+ await canvas.dblclick({ position: { x: 128, y: item.y } })
+ await expect(input).toBeVisible()
+ const editorPill = input.locator('xpath=../..')
+ await expect(editorPill).toHaveCSS('height', '24px')
+ await expect(editorPill).toHaveCSS('background-color', item.background)
+ await expect(input).toHaveCSS('color', item.foreground)
+ const width = await editorPill.evaluate((element) => element.getBoundingClientRect().width)
+ expect(width).toBeGreaterThan(80)
+ expect(width).toBeLessThan(100)
+ await input.press('Escape')
+ await expect(input).toBeHidden()
+ }
+})
+
+test('double-clicking a section title renames it inline', async () => {
+ await editor.page.evaluate(() => {
+ const store = window.openPencil?.getStore?.()
+ if (!store) throw new Error('OpenPencil store not initialized')
+ const pageId = store.state.currentPageId
+ store.graph.createNode('SECTION', pageId, {
+ id: 'section-inline-rename',
+ name: 'Components',
+ x: 120,
+ y: 160,
+ width: 280,
+ height: 180,
+ fills: [
+ { type: 'SOLID', color: { r: 0.37, g: 0.37, b: 0.37, a: 1 }, visible: true, opacity: 1 }
+ ]
+ })
+ store.requestRender()
+ })
+ await editor.canvas.waitForRender()
+
+ const canvas = editor.page.getByTestId('canvas-element')
+ await canvas.dblclick({ position: { x: 128, y: 142 } })
+ const input = editor.page.getByRole('textbox', { name: 'Layer name' })
+ await expect(input).toBeVisible()
+ await expect(input).toHaveValue('Components')
+
+ await input.fill('Primitives')
+ await input.press('Enter')
+
+ await expect(input).toBeHidden()
+ await expect
+ .poll(() =>
+ editor.page.evaluate(
+ () => window.openPencil?.getStore?.().graph.getNode('section-inline-rename')?.name
+ )
+ )
+ .toBe('Primitives')
+})
diff --git a/tests/engine/render/canvas/labels/color.test.ts b/tests/engine/render/canvas/labels/color.test.ts
new file mode 100644
index 000000000..837a0c06e
--- /dev/null
+++ b/tests/engine/render/canvas/labels/color.test.ts
@@ -0,0 +1,19 @@
+import { describe, expect, test } from 'bun:test'
+
+import { canvasLabelForeground } from '@open-pencil/core/canvas'
+
+const WHITE = { r: 1, g: 1, b: 1, a: 1 }
+const BLACK = { r: 0, g: 0, b: 0, a: 1 }
+
+describe('canvas label contrast', () => {
+ test('chooses the higher-contrast foreground for opaque midtones', () => {
+ expect(canvasLabelForeground({ r: 0.49, g: 0.49, b: 0.49, a: 1 })).toEqual(BLACK)
+ })
+
+ test('composites translucent fills over the canvas before choosing contrast', () => {
+ const translucentBlack = { r: 0, g: 0, b: 0, a: 0.2 }
+
+ expect(canvasLabelForeground(translucentBlack, WHITE)).toEqual(BLACK)
+ expect(canvasLabelForeground(translucentBlack, BLACK)).toEqual(WHITE)
+ })
+})
diff --git a/tests/engine/render/canvas/labels/paragraph-cache.test.ts b/tests/engine/render/canvas/labels/paragraph-cache.test.ts
index f84d85c7a..d508eea19 100644
--- a/tests/engine/render/canvas/labels/paragraph-cache.test.ts
+++ b/tests/engine/render/canvas/labels/paragraph-cache.test.ts
@@ -20,9 +20,10 @@ describe('label paragraph cache', () => {
provider.registerFont(data, 'Inter')
const cache = new LabelParagraphCache()
try {
- const width = cache.measure(ck, provider, 'Primitives', 12, 200, ck.BLACK, 1)
- expect(width).toBeGreaterThan(40)
- expect(width).toBeLessThan(80)
+ const metrics = cache.measure(ck, provider, 'Primitives', 12, 200, ck.BLACK, 1)
+ expect(metrics.width).toBeGreaterThan(40)
+ expect(metrics.width).toBeLessThan(80)
+ expect(metrics.height).toBeGreaterThan(0)
expect(cache.size()).toBe(1)
cache.measure(ck, provider, 'Primitives', 12, 200, ck.BLACK, 1)