Skip to content

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

Section title hit test uses exact pill width from font metrics

Danila Poyarkov584661315f34
diff --git a/src/components/EditorCanvas.vue b/src/components/EditorCanvas.vue
index 16b995367..77aa58eed 100644
--- a/src/components/EditorCanvas.vue
+++ b/src/components/EditorCanvas.vue
@@ -8,8 +8,8 @@ import { useEditorStore } from '../stores/editor'
const store = useEditorStore()
const canvasRef = ref<HTMLCanvasElement | null>(null)
-useCanvas(canvasRef, store)
-const { cursorOverride } = useCanvasInput(canvasRef, store)
+const { hitTestSectionTitle } = useCanvas(canvasRef, store)
+const { cursorOverride } = useCanvasInput(canvasRef, store, hitTestSectionTitle)
const cursor = computed(() => {
if (cursorOverride.value) return cursorOverride.value
diff --git a/src/composables/use-canvas-input.ts b/src/composables/use-canvas-input.ts
index 863622f94..1585a4406 100644
--- a/src/composables/use-canvas-input.ts
+++ b/src/composables/use-canvas-input.ts
@@ -201,7 +201,11 @@ function hitTestRotationHandle(
return Math.abs(ur.sx - mx) < ROTATION_HIT_RADIUS && Math.abs(ur.sy - rotY) < ROTATION_HIT_RADIUS
}
-export function useCanvasInput(canvasRef: Ref<HTMLCanvasElement | null>, store: EditorStore) {
+export function useCanvasInput(
+ canvasRef: Ref<HTMLCanvasElement | null>,
+ store: EditorStore,
+ hitTestSectionTitle: (cx: number, cy: number) => import('../engine/scene-graph').SceneNode | null
+) {
const drag = ref<DragState | null>(null)
const cursorOverride = ref<string | null>(null)
@@ -309,8 +313,8 @@ export function useCanvasInput(canvasRef: Ref<HTMLCanvasElement | null>, store:
}
}
- // Hit test nodes
- const hit = store.graph.hitTest(cx, cy, store.state.currentPageId, store.state.zoom)
+ // Hit test nodes (section title pill first, then body)
+ const hit = hitTestSectionTitle(cx, cy) ?? store.graph.hitTest(cx, cy, store.state.currentPageId)
if (hit) {
if (!store.state.selectedIds.has(hit.id) && !e.shiftKey) {
store.select([hit.id])
@@ -480,7 +484,7 @@ export function useCanvasInput(canvasRef: Ref<HTMLCanvasElement | null>, store:
}
cursorOverride.value = cursor
- const hit = store.graph.hitTest(cx, cy, undefined, store.state.zoom)
+ const hit = hitTestSectionTitle(cx, cy) ?? store.graph.hitTest(cx, cy)
store.setHoveredNode(hit && !store.state.selectedIds.has(hit.id) ? hit.id : null)
}
@@ -820,7 +824,7 @@ export function useCanvasInput(canvasRef: Ref<HTMLCanvasElement | null>, store:
function onDblClick(e: MouseEvent) {
const { cx, cy } = getCoords(e)
- const hit = store.graph.hitTest(cx, cy, store.state.currentPageId, store.state.zoom)
+ const hit = hitTestSectionTitle(cx, cy) ?? store.graph.hitTest(cx, cy, store.state.currentPageId)
if (hit && hit.type === 'TEXT') {
store.select([hit.id])
store.startTextEditing(hit.id)
diff --git a/src/composables/use-canvas.ts b/src/composables/use-canvas.ts
index d96e759c0..a11880be1 100644
--- a/src/composables/use-canvas.ts
+++ b/src/composables/use-canvas.ts
@@ -99,5 +99,9 @@ export function useCanvas(canvasRef: Ref<HTMLCanvasElement | null>, store: Edito
() => render()
)
- return { render }
+ function hitTestSectionTitle(canvasX: number, canvasY: number) {
+ return renderer?.hitTestSectionTitle(store.graph, canvasX, canvasY) ?? null
+ }
+
+ return { render, hitTestSectionTitle }
}
diff --git a/src/engine/renderer.ts b/src/engine/renderer.ts
index 8c83abc7d..ae712ef56 100644
--- a/src/engine/renderer.ts
+++ b/src/engine/renderer.ts
@@ -141,6 +141,60 @@ export class SkiaRenderer {
this.fontsLoaded = true
}
+ hitTestSectionTitle(graph: SceneGraph, canvasX: number, canvasY: number): SceneNode | null {
+ if (!this.sectionTitleFont) return null
+
+ const pageNode = graph.getNode(this.pageId ?? graph.rootId)
+ if (!pageNode) return null
+
+ const font = this.sectionTitleFont
+ let result: SceneNode | null = null
+
+ const check = (parentId: string, ox: number, oy: number, insideSection: boolean) => {
+ const parent = graph.getNode(parentId)
+ if (!parent) return
+ for (let i = parent.childIds.length - 1; i >= 0; i--) {
+ if (result) return
+ const childId = parent.childIds[i]
+ const child = graph.getNode(childId)
+ if (!child || !child.visible) continue
+ const ax = ox + child.x
+ const ay = oy + child.y
+ if (child.type === 'SECTION') {
+ // Compute pill width from font metrics
+ const glyphIds = font.getGlyphIDs(child.name)
+ const widths = font.getGlyphWidths(glyphIds)
+ let textW = 0
+ for (const w of widths) textW += w
+ const pillW = Math.min(textW + SECTION_TITLE_PADDING_X * 2, child.width * this.zoom) / this.zoom
+ const pillH = SECTION_TITLE_HEIGHT / this.zoom
+ const gap = SECTION_TITLE_GAP / this.zoom
+
+ let pillX = ax
+ let pillY: number
+ if (insideSection) {
+ pillY = ay + gap
+ } else {
+ pillY = ay - pillH - gap
+ }
+
+ if (canvasX >= pillX && canvasX <= pillX + pillW &&
+ canvasY >= pillY && canvasY <= pillY + pillH) {
+ result = child
+ return
+ }
+
+ check(childId, ax, ay, true)
+ } else if (child.childIds.length > 0) {
+ check(childId, ax, ay, insideSection)
+ }
+ }
+ }
+
+ check(pageNode.id, 0, 0, false)
+ return result
+ }
+
render(graph: SceneGraph, selectedIds: Set<string>, overlays: RenderOverlays = {}): void {
const canvas = this.surface.getCanvas()
canvas.clear(this.ck.Color4f(this.pageColor.r, this.pageColor.g, this.pageColor.b, 1))
diff --git a/src/engine/scene-graph.ts b/src/engine/scene-graph.ts
index f43876185..5ad9ca294 100644
--- a/src/engine/scene-graph.ts
+++ b/src/engine/scene-graph.ts
@@ -1,5 +1,3 @@
-import { SECTION_TITLE_HEIGHT, SECTION_TITLE_GAP } from '../constants'
-
export type { GUID, Color } from '../types'
export type HandleMirroring = 'NONE' | 'ANGLE' | 'ANGLE_AND_LENGTH'
@@ -362,9 +360,9 @@ export class SceneGraph {
this.nodes.delete(id)
}
- hitTest(px: number, py: number, scopeId?: string, zoom = 1): SceneNode | null {
+ hitTest(px: number, py: number, scopeId?: string): SceneNode | null {
const scope = scopeId ?? this.rootId
- return this.hitTestChildren(px, py, scope, 0, 0, zoom, false)
+ return this.hitTestChildren(px, py, scope, 0, 0)
}
private hitTestChildren(
@@ -372,9 +370,7 @@ export class SceneGraph {
py: number,
parentId: string,
offsetX: number,
- offsetY: number,
- zoom: number,
- insideSection: boolean
+ offsetY: number
): SceneNode | null {
const parent = this.nodes.get(parentId)
if (!parent) return null
@@ -390,26 +386,10 @@ export class SceneGraph {
// Check children first (deeper hit)
if (CONTAINER_TYPES.has(child.type)) {
- const nested = insideSection || child.type === 'SECTION'
- const deepHit = this.hitTestChildren(px, py, childId, ax, ay, zoom, nested)
+ const deepHit = this.hitTestChildren(px, py, childId, ax, ay)
if (deepHit) return deepHit
}
- // Section title pill extends above (top-level) or inside (nested)
- if (child.type === 'SECTION') {
- const titleH = (SECTION_TITLE_HEIGHT + SECTION_TITLE_GAP) / zoom
- let titleTop: number
- if (insideSection) {
- titleTop = ay + SECTION_TITLE_GAP / zoom
- } else {
- titleTop = ay - titleH
- }
- const titleBottom = insideSection ? titleTop + SECTION_TITLE_HEIGHT / zoom : ay
- if (px >= ax && px <= ax + child.width && py >= titleTop && py <= titleBottom) {
- return child
- }
- }
-
if (px >= ax && px <= ax + child.width && py >= ay && py <= ay + child.height) {
return child
}