Skip to content

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

feat(editor): add auto-layout hover controls

Danila Poyarkovc3fd32a51632
diff --git a/packages/core/src/canvas/overlays/auto-layout-hover.ts b/packages/core/src/canvas/overlays/auto-layout-hover.ts
new file mode 100644
index 000000000..04fda6f86
--- /dev/null
+++ b/packages/core/src/canvas/overlays/auto-layout-hover.ts
@@ -0,0 +1,293 @@
+import type { Canvas } from 'canvaskit-wasm'
+
+import type { RenderOverlays, SkiaRenderer } from '#core/canvas/renderer'
+import {
+ AUTO_LAYOUT_HOVER_BLUE,
+ AUTO_LAYOUT_HOVER_BLUE_FILL,
+ AUTO_LAYOUT_HOVER_CHILD_DASH,
+ AUTO_LAYOUT_HOVER_MAGENTA,
+ AUTO_LAYOUT_HOVER_MAGENTA_FILL,
+ AUTO_LAYOUT_HOVER_STRIPE_GAP,
+ AUTO_LAYOUT_HOVER_STRIPE_WIDTH,
+ AUTO_LAYOUT_HOVER_STROKE_WIDTH,
+ AUTO_LAYOUT_HOVER_TICK_LENGTH,
+ AUTO_LAYOUT_HOVER_VALUE_OFFSET,
+ AUTO_LAYOUT_HOVER_VALUE_PILL_HEIGHT,
+ AUTO_LAYOUT_HOVER_VALUE_PILL_PADDING_X,
+ AUTO_LAYOUT_HOVER_VALUE_PILL_RADIUS
+} from '#core/constants'
+import type { SceneGraph, SceneNode } from '#core/scene-graph'
+import type { Color } from '#core/types'
+
+type Hover = NonNullable<RenderOverlays['autoLayoutHover']>
+type RectTuple = [x: number, y: number, width: number, height: number]
+
+function visibleLayoutChildren(node: SceneNode, graph: SceneGraph) {
+ return node.childIds
+ .map((id) => graph.getNode(id))
+ .filter(
+ (child): child is SceneNode =>
+ !!child && child.visible && child.layoutPositioning !== 'ABSOLUTE'
+ )
+}
+
+function canvasColor(r: SkiaRenderer, color: Color) {
+ return r.ck.Color4f(color.r, color.g, color.b, color.a)
+}
+
+function setStroke(r: SkiaRenderer, color: Color) {
+ r.auxStroke.setStrokeWidth(AUTO_LAYOUT_HOVER_STROKE_WIDTH)
+ r.auxStroke.setColor(canvasColor(r, color))
+ r.auxStroke.setPathEffect(null)
+}
+
+function drawHorizontalTick(r: SkiaRenderer, canvas: Canvas, x: number, y: number) {
+ canvas.drawLine(
+ x - AUTO_LAYOUT_HOVER_TICK_LENGTH,
+ y,
+ x + AUTO_LAYOUT_HOVER_TICK_LENGTH,
+ y,
+ r.auxStroke
+ )
+}
+
+function drawVerticalTick(r: SkiaRenderer, canvas: Canvas, x: number, y: number) {
+ canvas.drawLine(
+ x,
+ y - AUTO_LAYOUT_HOVER_TICK_LENGTH,
+ x,
+ y + AUTO_LAYOUT_HOVER_TICK_LENGTH,
+ r.auxStroke
+ )
+}
+
+function toScreenRect(r: SkiaRenderer, [x, y, width, height]: RectTuple) {
+ return r.ck.LTRBRect(
+ x * r.zoom + r.panX,
+ y * r.zoom + r.panY,
+ (x + width) * r.zoom + r.panX,
+ (y + height) * r.zoom + r.panY
+ )
+}
+
+function drawStripedRect(r: SkiaRenderer, canvas: Canvas, rectTuple: RectTuple, color: Color, fill: Color) {
+ const [, , width, height] = rectTuple
+ if (width <= 0 || height <= 0) return
+ const rect = toScreenRect(r, rectTuple)
+ r.auxFill.setColor(canvasColor(r, fill))
+ canvas.drawRect(rect, r.auxFill)
+
+ const path = new r.ck.Path()
+ const left = rect[0]
+ const top = rect[1]
+ const right = rect[2]
+ const bottom = rect[3]
+ for (let sx = left - (bottom - top); sx < right; sx += AUTO_LAYOUT_HOVER_STRIPE_GAP) {
+ path.moveTo(sx, bottom)
+ path.lineTo(sx + (bottom - top), top)
+ }
+ r.auxStroke.setStrokeWidth(AUTO_LAYOUT_HOVER_STRIPE_WIDTH)
+ r.auxStroke.setColor(canvasColor(r, color))
+ r.auxStroke.setPathEffect(null)
+ canvas.save()
+ canvas.clipRect(rect, r.ck.ClipOp.Intersect, true)
+ canvas.drawPath(path, r.auxStroke)
+ canvas.restore()
+ path.delete()
+}
+
+function drawValuePill(r: SkiaRenderer, canvas: Canvas, text: string, x: number, y: number) {
+ if (!r.labelFont) return
+ const width = Math.max(24, text.length * 8 + AUTO_LAYOUT_HOVER_VALUE_PILL_PADDING_X * 2)
+ const height = AUTO_LAYOUT_HOVER_VALUE_PILL_HEIGHT
+ const rect = r.ck.RRectXY(
+ r.ck.LTRBRect(x - width / 2, y - height / 2, x + width / 2, y + height / 2),
+ AUTO_LAYOUT_HOVER_VALUE_PILL_RADIUS,
+ AUTO_LAYOUT_HOVER_VALUE_PILL_RADIUS
+ )
+ r.auxFill.setColor(r.selColor())
+ canvas.drawRRect(rect, r.auxFill)
+ r.auxFill.setColor(r.ck.Color4f(1, 1, 1, 1))
+ canvas.drawText(
+ text,
+ x - width / 2 + AUTO_LAYOUT_HOVER_VALUE_PILL_PADDING_X,
+ y + 5,
+ r.auxFill,
+ r.labelFont
+ )
+}
+
+function gapRects(node: SceneNode, graph: SceneGraph): RectTuple[] {
+ const children = visibleLayoutChildren(node, graph)
+ if (children.length < 2 || node.itemSpacing <= 0) return []
+ const abs = graph.getAbsolutePosition(node.id)
+ const isRow = node.layoutMode === 'HORIZONTAL'
+ const rects: RectTuple[] = []
+
+ for (let i = 0; i < children.length - 1; i++) {
+ const prev = children[i]
+ const next = children[i + 1]
+ const gapStart = isRow ? prev.x + prev.width : prev.y + prev.height
+ const gapEnd = isRow ? next.x : next.y
+ if (gapEnd <= gapStart) continue
+ rects.push(
+ isRow
+ ? [
+ abs.x + gapStart,
+ abs.y + node.paddingTop,
+ gapEnd - gapStart,
+ node.height - node.paddingTop - node.paddingBottom
+ ]
+ : [
+ abs.x + node.paddingLeft,
+ abs.y + gapStart,
+ node.width - node.paddingLeft - node.paddingRight,
+ gapEnd - gapStart
+ ]
+ )
+ }
+ return rects
+}
+
+function paddingRect(node: SceneNode, graph: SceneGraph, side: Hover['side']): RectTuple | null {
+ if (!side) return null
+ const abs = graph.getAbsolutePosition(node.id)
+ if (side === 'top') return [abs.x, abs.y, node.width, node.paddingTop]
+ if (side === 'bottom') {
+ return [abs.x, abs.y + node.height - node.paddingBottom, node.width, node.paddingBottom]
+ }
+ if (side === 'left') return [abs.x, abs.y, node.paddingLeft, node.height]
+ return [abs.x + node.width - node.paddingRight, abs.y, node.paddingRight, node.height]
+}
+
+function drawBaselineTicks(r: SkiaRenderer, canvas: Canvas, graph: SceneGraph, node: SceneNode) {
+ const abs = graph.getAbsolutePosition(node.id)
+ const xCenter = (abs.x + node.width / 2) * r.zoom + r.panX
+ const yCenter = (abs.y + node.height / 2) * r.zoom + r.panY
+
+ setStroke(r, AUTO_LAYOUT_HOVER_BLUE)
+ if (node.paddingTop > 0) {
+ drawHorizontalTick(r, canvas, xCenter, (abs.y + node.paddingTop / 2) * r.zoom + r.panY)
+ }
+ if (node.paddingBottom > 0) {
+ drawHorizontalTick(
+ r,
+ canvas,
+ xCenter,
+ (abs.y + node.height - node.paddingBottom / 2) * r.zoom + r.panY
+ )
+ }
+ if (node.paddingLeft > 0) {
+ drawVerticalTick(r, canvas, (abs.x + node.paddingLeft / 2) * r.zoom + r.panX, yCenter)
+ }
+ if (node.paddingRight > 0) {
+ drawVerticalTick(
+ r,
+ canvas,
+ (abs.x + node.width - node.paddingRight / 2) * r.zoom + r.panX,
+ yCenter
+ )
+ }
+
+ setStroke(r, AUTO_LAYOUT_HOVER_MAGENTA)
+ for (const rect of gapRects(node, graph)) {
+ const [x, y, width, height] = rect
+ if (node.layoutMode === 'HORIZONTAL') {
+ drawVerticalTick(r, canvas, (x + width / 2) * r.zoom + r.panX, yCenter)
+ } else {
+ drawHorizontalTick(r, canvas, xCenter, (y + height / 2) * r.zoom + r.panY)
+ }
+ }
+}
+
+function drawSpacingHover(
+ r: SkiaRenderer,
+ canvas: Canvas,
+ graph: SceneGraph,
+ node: SceneNode,
+ showValue: boolean
+) {
+ const rects = gapRects(node, graph)
+ for (const rect of rects) {
+ drawStripedRect(
+ r,
+ canvas,
+ rect,
+ AUTO_LAYOUT_HOVER_MAGENTA,
+ AUTO_LAYOUT_HOVER_MAGENTA_FILL
+ )
+ }
+ if (!showValue || rects.length === 0) return
+ const [x, y, width, height] = rects[0]
+ drawValuePill(
+ r,
+ canvas,
+ String(Math.round(node.itemSpacing)),
+ (x + width / 2) * r.zoom + r.panX + AUTO_LAYOUT_HOVER_VALUE_OFFSET,
+ (y + height / 2) * r.zoom + r.panY - AUTO_LAYOUT_HOVER_VALUE_OFFSET
+ )
+}
+
+function drawPaddingHover(
+ r: SkiaRenderer,
+ canvas: Canvas,
+ graph: SceneGraph,
+ node: SceneNode,
+ hover: Hover,
+ showValue: boolean
+) {
+ const rect = paddingRect(node, graph, hover.side)
+ if (!rect) return
+ drawStripedRect(r, canvas, rect, AUTO_LAYOUT_HOVER_BLUE, AUTO_LAYOUT_HOVER_BLUE_FILL)
+ if (!showValue) return
+ const [x, y, width, height] = rect
+ const value = hover.side === 'left' || hover.side === 'right' ? width : height
+ drawValuePill(
+ r,
+ canvas,
+ String(Math.round(value)),
+ (x + width / 2) * r.zoom + r.panX + AUTO_LAYOUT_HOVER_VALUE_OFFSET,
+ (y + height / 2) * r.zoom + r.panY - AUTO_LAYOUT_HOVER_VALUE_OFFSET
+ )
+}
+
+function drawChildrenHover(r: SkiaRenderer, canvas: Canvas, graph: SceneGraph, node: SceneNode) {
+ r.auxStroke.setStrokeWidth(1)
+ r.auxStroke.setColor(r.selColor())
+ r.auxStroke.setPathEffect(
+ r.ck.PathEffect.MakeDash([AUTO_LAYOUT_HOVER_CHILD_DASH, AUTO_LAYOUT_HOVER_CHILD_DASH], 0)
+ )
+ for (const child of visibleLayoutChildren(node, graph)) {
+ const abs = graph.getAbsolutePosition(child.id)
+ canvas.drawRect(
+ r.ck.LTRBRect(
+ abs.x * r.zoom + r.panX,
+ abs.y * r.zoom + r.panY,
+ (abs.x + child.width) * r.zoom + r.panX,
+ (abs.y + child.height) * r.zoom + r.panY
+ ),
+ r.auxStroke
+ )
+ }
+ r.auxStroke.setPathEffect(null)
+}
+
+export function drawAutoLayoutHover(
+ r: SkiaRenderer,
+ canvas: Canvas,
+ graph: SceneGraph,
+ hover?: RenderOverlays['autoLayoutHover']
+): void {
+ if (!hover) return
+ const node = graph.getNode(hover.nodeId)
+ if (!node || (node.layoutMode !== 'HORIZONTAL' && node.layoutMode !== 'VERTICAL')) return
+
+ if (hover.kind === 'children') drawChildrenHover(r, canvas, graph, node)
+ if (hover.kind === 'spacing' || hover.kind === 'spacing-value') {
+ drawSpacingHover(r, canvas, graph, node, hover.kind === 'spacing-value')
+ }
+ if (hover.kind === 'padding' || hover.kind === 'padding-value') {
+ drawPaddingHover(r, canvas, graph, node, hover, hover.kind === 'padding-value')
+ }
+ drawBaselineTicks(r, canvas, graph, node)
+}
diff --git a/packages/core/src/canvas/overlays/index.ts b/packages/core/src/canvas/overlays/index.ts
index 298e956b0..81dd383fa 100644
--- a/packages/core/src/canvas/overlays/index.ts
+++ b/packages/core/src/canvas/overlays/index.ts
@@ -9,6 +9,7 @@ export {
drawSelection,
getRotatedCorners
} from './selection'
+export { drawAutoLayoutHover } from './auto-layout-hover'
export { drawFlashes, drawLayoutInsertIndicator, drawMarquee, drawSnapGuides } from './feedback'
export { drawTextEditOverlay } from './text-edit'
export { drawSelectionLabels } from '#core/canvas/selection-labels'
diff --git a/packages/core/src/canvas/renderer.ts b/packages/core/src/canvas/renderer.ts
index 2244627eb..5bf6e88cc 100644
--- a/packages/core/src/canvas/renderer.ts
+++ b/packages/core/src/canvas/renderer.ts
@@ -169,6 +169,11 @@ export class SkiaRenderer {
canvas: Canvas,
indicator?: RenderOverlays['layoutInsertIndicator']
) => void
+ declare drawAutoLayoutHover: (
+ canvas: Canvas,
+ graph: SceneGraph,
+ hover?: RenderOverlays['autoLayoutHover']
+ ) => void
declare drawTextEditOverlay: (canvas: Canvas, node: SceneNode, editor: TextEditor) => void
declare drawNodeEditOverlay: (
canvas: Canvas,
diff --git a/packages/core/src/canvas/renderer/methods.ts b/packages/core/src/canvas/renderer/methods.ts
index ffa6eab1b..5c2d58469 100644
--- a/packages/core/src/canvas/renderer/methods.ts
+++ b/packages/core/src/canvas/renderer/methods.ts
@@ -95,6 +95,10 @@ const rendererMethods: ThisType<SkiaRenderer> = {
Overlays.drawLayoutInsertIndicator(this, canvas, indicator)
},
+ drawAutoLayoutHover(canvas: Canvas, graph: SceneGraph, hover?: RenderOverlays['autoLayoutHover']) {
+ Overlays.drawAutoLayoutHover(this, canvas, graph, hover)
+ },
+
drawTextEditOverlay(canvas: Canvas, node: SceneNode, editor: TextEditor): void {
Overlays.drawTextEditOverlay(this, canvas, node, editor)
},
diff --git a/packages/core/src/canvas/renderer/pipeline.ts b/packages/core/src/canvas/renderer/pipeline.ts
index 53af41a01..9381d548e 100644
--- a/packages/core/src/canvas/renderer/pipeline.ts
+++ b/packages/core/src/canvas/renderer/pipeline.ts
@@ -70,7 +70,8 @@ export function renderFromEditorState(
} as RenderOverlays['penState'])
: null,
nodeEditState: state.nodeEditState ?? null,
- remoteCursors: state.remoteCursors
+ remoteCursors: state.remoteCursors,
+ autoLayoutHover: state.autoLayoutHover
},
state.sceneVersion,
layer
@@ -197,6 +198,7 @@ export function render(
r.drawSnapGuides(canvas, overlays.snapGuides)
r.drawMarquee(canvas, overlays.marquee)
r.drawLayoutInsertIndicator(canvas, overlays.layoutInsertIndicator)
+ r.drawAutoLayoutHover(canvas, graph, overlays.autoLayoutHover)
r.drawNodeEditOverlay(canvas, graph, overlays.nodeEditState)
r.drawPenOverlay(canvas, overlays.penState)
r.drawRemoteCursors(canvas, graph, overlays.remoteCursors)
diff --git a/packages/core/src/canvas/renderer/types.ts b/packages/core/src/canvas/renderer/types.ts
index 1ec22d8a0..8f2cc9322 100644
--- a/packages/core/src/canvas/renderer/types.ts
+++ b/packages/core/src/canvas/renderer/types.ts
@@ -25,6 +25,12 @@ export interface RenderOverlays {
length: number
direction: 'HORIZONTAL' | 'VERTICAL'
} | null
+ autoLayoutHover?: {
+ nodeId: string
+ kind: 'frame' | 'children' | 'spacing' | 'spacing-value' | 'padding' | 'padding-value'
+ index?: number
+ side?: 'top' | 'right' | 'bottom' | 'left'
+ } | null
penState?: {
vertices: Vector[]
segments: Array<{
diff --git a/packages/core/src/constants.ts b/packages/core/src/constants.ts
index c86e2ab72..97b90bb72 100644
--- a/packages/core/src/constants.ts
+++ b/packages/core/src/constants.ts
@@ -83,6 +83,24 @@ export const DROP_HIGHLIGHT_ALPHA = 0.8
export const DROP_HIGHLIGHT_STROKE = 2
export const LAYOUT_INDICATOR_STROKE = 2
+export const AUTO_LAYOUT_HOVER_TICK_LENGTH = 12
+export const AUTO_LAYOUT_HOVER_STRIPE_GAP = 8
+export const AUTO_LAYOUT_HOVER_STRIPE_WIDTH = 1
+export const AUTO_LAYOUT_HOVER_STROKE_WIDTH = 1.5
+export const AUTO_LAYOUT_HOVER_CHILD_DASH = 4
+export const AUTO_LAYOUT_HOVER_VALUE_OFFSET = 18
+export const AUTO_LAYOUT_HOVER_VALUE_PILL_HEIGHT = 22
+export const AUTO_LAYOUT_HOVER_VALUE_PILL_RADIUS = 5
+export const AUTO_LAYOUT_HOVER_VALUE_PILL_PADDING_X = 5
+export const AUTO_LAYOUT_HOVER_GAP_REGION_TOLERANCE = 12
+export const AUTO_LAYOUT_HOVER_TICK_HIT_TOLERANCE = 8
+export const AUTO_LAYOUT_HOVER_PADDING_REGION_TOLERANCE = 20
+export const AUTO_LAYOUT_PADDING_EDITOR_OFFSET_X = 18
+export const AUTO_LAYOUT_PADDING_EDITOR_OFFSET_Y = 36
+export const AUTO_LAYOUT_HOVER_BLUE = { r: 0.28, g: 0.64, b: 1, a: 0.82 } satisfies Color
+export const AUTO_LAYOUT_HOVER_BLUE_FILL = { r: 0.28, g: 0.64, b: 1, a: 0.1 } satisfies Color
+export const AUTO_LAYOUT_HOVER_MAGENTA = { r: 1, g: 0.32, b: 0.68, a: 0.78 } satisfies Color
+export const AUTO_LAYOUT_HOVER_MAGENTA_FILL = { r: 1, g: 0.32, b: 0.68, a: 0.1 } satisfies Color
export const SECTION_CORNER_RADIUS = 5
export const SECTION_TITLE_HEIGHT = 24
diff --git a/packages/core/src/editor/selection/overlays.ts b/packages/core/src/editor/selection/overlays.ts
index 83baecbc0..09cbff454 100644
--- a/packages/core/src/editor/selection/overlays.ts
+++ b/packages/core/src/editor/selection/overlays.ts
@@ -36,12 +36,27 @@ export function createSelectionOverlayActions(ctx: EditorContext) {
ctx.requestRepaint()
}
+ function setAutoLayoutHover(hover: typeof ctx.state.autoLayoutHover) {
+ const current = ctx.state.autoLayoutHover
+ if (
+ current?.nodeId === hover?.nodeId &&
+ current?.kind === hover?.kind &&
+ current?.index === hover?.index &&
+ current?.side === hover?.side
+ ) {
+ return
+ }
+ ctx.state.autoLayoutHover = hover
+ ctx.requestRepaint()
+ }
+
return {
setMarquee,
setSnapGuides,
setRotationPreview,
setHoveredNode,
setDropTarget,
- setLayoutInsertIndicator
+ setLayoutInsertIndicator,
+ setAutoLayoutHover
}
}
diff --git a/packages/core/src/editor/state.ts b/packages/core/src/editor/state.ts
index 17c81060b..490377154 100644
--- a/packages/core/src/editor/state.ts
+++ b/packages/core/src/editor/state.ts
@@ -17,6 +17,7 @@ export function createDefaultEditorState(pageId: string): EditorState {
penCursorX: null,
penCursorY: null,
remoteCursors: [],
+ autoLayoutHover: null,
documentName: 'Untitled',
panX: 0,
pageColor: { ...getDefaultCanvasBgColor() },
diff --git a/packages/core/src/editor/types.ts b/packages/core/src/editor/types.ts
index 35285a7aa..da5d52b5c 100644
--- a/packages/core/src/editor/types.ts
+++ b/packages/core/src/editor/types.ts
@@ -59,6 +59,12 @@ export interface EditorState {
y: number
selection?: string[]
}>
+ autoLayoutHover: {
+ nodeId: string
+ kind: 'frame' | 'children' | 'spacing' | 'spacing-value' | 'padding' | 'padding-value'
+ index?: number
+ side?: 'top' | 'right' | 'bottom' | 'left'
+ } | null
documentName: string
panX: number
pageColor: Color
diff --git a/packages/vue/src/canvas/overlays/useCanvasVirtualReference.ts b/packages/vue/src/canvas/overlays/useCanvasVirtualReference.ts
new file mode 100644
index 000000000..89fc5a8f7
--- /dev/null
+++ b/packages/vue/src/canvas/overlays/useCanvasVirtualReference.ts
@@ -0,0 +1,29 @@
+import { computed, type ComputedRef, type Ref } from 'vue'
+
+import type { Editor } from '@open-pencil/core/editor'
+import type { Vector } from '@open-pencil/core/types'
+
+type CanvasVirtualReference = {
+ getBoundingClientRect: () => DOMRect
+}
+
+export function useCanvasVirtualReference(
+ canvasRef: Ref<HTMLElement | null>,
+ editor: Editor,
+ anchor: ComputedRef<Vector | null>
+) {
+ return computed<CanvasVirtualReference | null>(() => {
+ const point = anchor.value
+ const canvas = canvasRef.value
+ if (!point || !canvas) return null
+
+ return {
+ getBoundingClientRect() {
+ const rect = canvas.getBoundingClientRect()
+ const x = rect.left + point.x * editor.state.zoom + editor.state.panX
+ const y = rect.top + point.y * editor.state.zoom + editor.state.panY
+ return new DOMRect(x, y, 0, 0)
+ }
+ }
+ })
+}
diff --git a/packages/vue/src/canvas/useCanvasInput.ts b/packages/vue/src/canvas/useCanvasInput.ts
index f53193fbc..56d271748 100644
--- a/packages/vue/src/canvas/useCanvasInput.ts
+++ b/packages/vue/src/canvas/useCanvasInput.ts
@@ -15,6 +15,7 @@ import { createTextEditInput } from '#vue/canvas/text-edit/input'
import { handleToolMouseDown } from '#vue/canvas/tool-input/use'
import { createCanvasTransformInput } from '#vue/canvas/transform-input/use'
import { createClickCounter } from '#vue/shared/input/click-count'
+import { resolveAutoLayoutHover } from '#vue/shared/input/auto-layout-hover'
import { handleDrawMove, handleDrawUp } from '#vue/shared/input/draw'
import { handleMoveMove, handleMoveUp } from '#vue/shared/input/move'
import { handleNodeEditMove } from '#vue/shared/input/node-edit'
@@ -41,6 +42,12 @@ export function useCanvasInput(
) {
const drag = ref<DragState | null>(null)
const cursorOverride = ref<string | null>(null)
+ const autoLayoutPaddingEdit = ref<{
+ nodeId: string
+ side: 'top' | 'right' | 'bottom' | 'left'
+ value: number
+ previous: number
+ } | null>(null)
const selectedIdsBeforeClickSequence = ref<ReadonlySet<string>>(new Set())
const spaceHeld = useSpaceHeld()
const { recordClick, getClickCount } = createClickCounter()
@@ -57,7 +64,7 @@ export function useCanvasInput(
drag.value = d
}
- const { handleTextEditClick, onDblClick } = createTextEditInput({
+ const { handleTextEditClick, onDblClick: onTextDblClick } = createTextEditInput({
editor,
getCoords,
hitTestInScope,
@@ -76,6 +83,70 @@ export function useCanvasInput(
handleMarqueeMove
} = createCanvasTransformInput(editor, canvasToLocal, setDrag)
+ function paddingValue(node: SceneNode, side: 'top' | 'right' | 'bottom' | 'left') {
+ if (side === 'top') return node.paddingTop
+ if (side === 'right') return node.paddingRight
+ if (side === 'bottom') return node.paddingBottom
+ return node.paddingLeft
+ }
+
+ function paddingKey(side: 'top' | 'right' | 'bottom' | 'left') {
+ if (side === 'top') return 'paddingTop' as const
+ if (side === 'right') return 'paddingRight' as const
+ if (side === 'bottom') return 'paddingBottom' as const
+ return 'paddingLeft' as const
+ }
+
+ function startAutoLayoutPaddingEdit(e: MouseEvent): boolean {
+ const { cx, cy } = getCoords(e)
+ const hover = resolveAutoLayoutHover(cx, cy, editor)
+ if (hover?.kind !== 'padding' && hover?.kind !== 'padding-value') return false
+ if (!hover.side) return false
+ const node = editor.graph.getNode(hover.nodeId)
+ if (!node) return false
+ const value = paddingValue(node, hover.side)
+ autoLayoutPaddingEdit.value = {
+ nodeId: node.id,
+ side: hover.side,
+ value,
+ previous: value
+ }
+ e.preventDefault()
+ e.stopPropagation()
+ return true
+ }
+
+ function updateAutoLayoutPaddingEdit(value: number) {
+ const edit = autoLayoutPaddingEdit.value
+ if (!edit || !Number.isFinite(value)) return
+ const next = Math.max(0, value)
+ autoLayoutPaddingEdit.value = { ...edit, value: next }
+ editor.updateNode(edit.nodeId, { [paddingKey(edit.side)]: next })
+ }
+
+ function commitAutoLayoutPaddingEdit(value: number) {
+ const edit = autoLayoutPaddingEdit.value
+ if (!edit || !Number.isFinite(value)) {
+ autoLayoutPaddingEdit.value = null
+ return
+ }
+ const next = Math.max(0, value)
+ editor.updateNode(edit.nodeId, { [paddingKey(edit.side)]: edit.previous })
+ editor.updateNodeWithUndo(edit.nodeId, { [paddingKey(edit.side)]: next }, 'Update padding')
+ autoLayoutPaddingEdit.value = null
+ }
+
+ function cancelAutoLayoutPaddingEdit() {
+ const edit = autoLayoutPaddingEdit.value
+ if (edit) editor.updateNode(edit.nodeId, { [paddingKey(edit.side)]: edit.previous })
+ autoLayoutPaddingEdit.value = null
+ }
+
+ function onDblClick(e: MouseEvent) {
+ if (startAutoLayoutPaddingEdit(e)) return
+ onTextDblClick(e)
+ }
+
function onMouseDown(e: MouseEvent) {
if (!editor.state.editingTextId) canvasRef.value?.focus()
editor.setHoveredNode(null)
@@ -116,6 +187,7 @@ export function useCanvasInput(
if (!drag.value && editor.state.activeTool === 'SELECT') {
const { cx, cy } = getCoords(e)
cursorOverride.value = updateHoverCursor(cx, cy, editor, hitFns)
+ editor.setAutoLayoutHover(resolveAutoLayoutHover(cx, cy, editor))
}
if (!drag.value) return
@@ -220,6 +292,10 @@ export function useCanvasInput(
setupPanZoom(canvasRef, editor, drag, onMouseDown, onMouseMove, onMouseUp)
return {
drag,
- cursorOverride
+ cursorOverride,
+ autoLayoutPaddingEdit,
+ updateAutoLayoutPaddingEdit,
+ commitAutoLayoutPaddingEdit,
+ cancelAutoLayoutPaddingEdit
}
}
diff --git a/packages/vue/src/index.ts b/packages/vue/src/index.ts
index a09e0f627..56e71500b 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 { 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/packages/vue/src/shared/input/auto-layout-hover.ts b/packages/vue/src/shared/input/auto-layout-hover.ts
new file mode 100644
index 000000000..381196ddf
--- /dev/null
+++ b/packages/vue/src/shared/input/auto-layout-hover.ts
@@ -0,0 +1,145 @@
+import {
+ AUTO_LAYOUT_HOVER_GAP_REGION_TOLERANCE,
+ AUTO_LAYOUT_HOVER_PADDING_REGION_TOLERANCE,
+ AUTO_LAYOUT_HOVER_TICK_HIT_TOLERANCE
+} from '@open-pencil/core/constants'
+import type { Editor } from '@open-pencil/core/editor'
+import type { SceneNode } from '@open-pencil/core/scene-graph'
+
+type AutoLayoutHover = NonNullable<Editor['state']['autoLayoutHover']>
+
+function visibleLayoutChildren(node: SceneNode, editor: Editor) {
+ return node.childIds
+ .map((id) => editor.graph.getNode(id))
+ .filter(
+ (child): child is SceneNode =>
+ !!child && child.visible && child.layoutPositioning !== 'ABSOLUTE'
+ )
+}
+
+function isNear(value: number, target: number, tolerance = AUTO_LAYOUT_HOVER_TICK_HIT_TOLERANCE) {
+ return Math.abs(value - target) <= tolerance
+}
+
+function resolvePaddingHover(node: SceneNode, localX: number, localY: number): AutoLayoutHover | null {
+ const centerX = node.width / 2
+ const centerY = node.height / 2
+
+ if (node.paddingTop > 0) {
+ const tickY = node.paddingTop / 2
+ if (isNear(localX, centerX) && isNear(localY, tickY)) {
+ return { nodeId: node.id, kind: 'padding-value', side: 'top' }
+ }
+ if (localY <= Math.min(node.paddingTop, AUTO_LAYOUT_HOVER_PADDING_REGION_TOLERANCE)) {
+ return { nodeId: node.id, kind: 'padding', side: 'top' }
+ }
+ }
+
+ if (node.paddingBottom > 0) {
+ const tickY = node.height - node.paddingBottom / 2
+ if (isNear(localX, centerX) && isNear(localY, tickY)) {
+ return { nodeId: node.id, kind: 'padding-value', side: 'bottom' }
+ }
+ if (localY >= node.height - Math.min(node.paddingBottom, AUTO_LAYOUT_HOVER_PADDING_REGION_TOLERANCE)) {
+ return { nodeId: node.id, kind: 'padding', side: 'bottom' }
+ }
+ }
+
+ if (node.paddingLeft > 0) {
+ const tickX = node.paddingLeft / 2
+ if (isNear(localX, tickX) && isNear(localY, centerY)) {
+ return { nodeId: node.id, kind: 'padding-value', side: 'left' }
+ }
+ if (localX <= Math.min(node.paddingLeft, AUTO_LAYOUT_HOVER_PADDING_REGION_TOLERANCE)) {
+ return { nodeId: node.id, kind: 'padding', side: 'left' }
+ }
+ }
+
+ if (node.paddingRight > 0) {
+ const tickX = node.width - node.paddingRight / 2
+ if (isNear(localX, tickX) && isNear(localY, centerY)) {
+ return { nodeId: node.id, kind: 'padding-value', side: 'right' }
+ }
+ if (localX >= node.width - Math.min(node.paddingRight, AUTO_LAYOUT_HOVER_PADDING_REGION_TOLERANCE)) {
+ return { nodeId: node.id, kind: 'padding', side: 'right' }
+ }
+ }
+
+ return null
+}
+
+function resolveSpacingHover(
+ node: SceneNode,
+ children: SceneNode[],
+ localX: number,
+ localY: number
+): AutoLayoutHover | null {
+ if (children.length < 2 || node.itemSpacing <= 0) return null
+ const isRow = node.layoutMode === 'HORIZONTAL'
+ const contentCrossStart = isRow ? node.paddingTop : node.paddingLeft
+ const contentCrossEnd = isRow ? node.height - node.paddingBottom : node.width - node.paddingRight
+ const cross = isRow ? localY : localX
+ if (cross < contentCrossStart || cross > contentCrossEnd) return null
+
+ for (let i = 0; i < children.length - 1; i++) {
+ const prev = children[i]
+ const next = children[i + 1]
+ const gapStart = isRow ? prev.x + prev.width : prev.y + prev.height
+ const gapEnd = isRow ? next.x : next.y
+ if (gapEnd < gapStart) continue
+
+ const cursor = isRow ? localX : localY
+ const tickMain = (gapStart + gapEnd) / 2
+ const tickCross = (contentCrossStart + contentCrossEnd) / 2
+ if (isNear(cursor, tickMain) && isNear(cross, tickCross)) {
+ return { nodeId: node.id, kind: 'spacing-value', index: i }
+ }
+ if (
+ cursor >= gapStart - AUTO_LAYOUT_HOVER_GAP_REGION_TOLERANCE &&
+ cursor <= gapEnd + AUTO_LAYOUT_HOVER_GAP_REGION_TOLERANCE
+ ) {
+ return { nodeId: node.id, kind: 'spacing', index: i }
+ }
+ }
+ return null
+}
+
+function resolveChildrenHover(
+ node: SceneNode,
+ children: SceneNode[],
+ localX: number,
+ localY: number
+): AutoLayoutHover | null {
+ for (let i = 0; i < children.length; i++) {
+ const child = children[i]
+ if (
+ localX >= child.x &&
+ localX <= child.x + child.width &&
+ localY >= child.y &&
+ localY <= child.y + child.height
+ ) {
+ return { nodeId: node.id, kind: 'children', index: i }
+ }
+ }
+ return null
+}
+
+export function resolveAutoLayoutHover(cx: number, cy: number, editor: Editor): AutoLayoutHover | null {
+ if (editor.state.selectedIds.size !== 1) return null
+ const nodeId = [...editor.state.selectedIds][0]
+ const node = editor.graph.getNode(nodeId)
+ if (!node || (node.layoutMode !== 'HORIZONTAL' && node.layoutMode !== 'VERTICAL')) return null
+
+ const abs = editor.graph.getAbsolutePosition(node.id)
+ const localX = cx - abs.x
+ const localY = cy - abs.y
+ if (localX < 0 || localY < 0 || localX > node.width || localY > node.height) return null
+
+ const children = visibleLayoutChildren(node, editor)
+ return (
+ resolveSpacingHover(node, children, localX, localY) ??
+ resolvePaddingHover(node, localX, localY) ??
+ resolveChildrenHover(node, children, localX, localY) ??
+ { nodeId: node.id, kind: 'frame' }
+ )
+}
diff --git a/src/components/EditorCanvas.vue b/src/components/EditorCanvas.vue
index 5d942b154..d442964e1 100644
--- a/src/components/EditorCanvas.vue
+++ b/src/components/EditorCanvas.vue
@@ -1,14 +1,33 @@
<script setup lang="ts">
-import { ref, computed } from 'vue'
-import { ContextMenuRoot, ContextMenuTrigger, ContextMenuPortal } from 'reka-ui'
+import { computed, nextTick, ref, watch } from 'vue'
+import {
+ AUTO_LAYOUT_PADDING_EDITOR_OFFSET_X,
+ AUTO_LAYOUT_PADDING_EDITOR_OFFSET_Y
+} from '@open-pencil/core/constants'
+import {
+ ContextMenuPortal,
+ ContextMenuRoot,
+ ContextMenuTrigger,
+ PopoverContent,
+ PopoverPortal,
+ PopoverRoot
+} from 'reka-ui'
-import { toolCursor, useCanvas, useCanvasDrop, useCanvasInput, useTextEdit } from '@open-pencil/vue'
+import {
+ toolCursor,
+ useCanvas,
+ useCanvasDrop,
+ useCanvasInput,
+ useCanvasVirtualReference,
+ useTextEdit
+} from '@open-pencil/vue'
import { useCollabInjected } from '@/app/collab/use'
import { useEditorStore } from '@/app/editor/active-store'
import { useCanvasCollaborationAwareness } from '@/app/editor/canvas/collaboration-awareness'
import { createCanvasContextSelection } from '@/app/editor/canvas/context-selection'
import { fadeOutGlobalLoader } from '@/app/editor/canvas/loader-overlay'
import CanvasMenu from './CanvasMenu.vue'
+import ScrubInput from './ScrubInput.vue'
const store = useEditorStore()
const collab = useCollabInjected()
@@ -30,7 +49,13 @@ const { hitTestSectionTitle, hitTestComponentLabel, hitTestFrameTitle } = useCan
layer: 'overlays'
}
)
-const { cursorOverride } = useCanvasInput(
+const {
+ cursorOverride,
+ autoLayoutPaddingEdit,
+ updateAutoLayoutPaddingEdit,
+ commitAutoLayoutPaddingEdit,
+ cancelAutoLayoutPaddingEdit
+} = useCanvasInput(
canvasRef,
store,
hitTestSectionTitle,
@@ -42,6 +67,31 @@ const { cursorOverride } = useCanvasInput(
useTextEdit(canvasRef, store)
const { isDraggingOver } = useCanvasDrop(canvasRef, store)
+const paddingEditorRef = ref<HTMLElement | null>(null)
+
+const paddingEditorAnchor = computed(() => {
+ const edit = autoLayoutPaddingEdit.value
+ if (!edit) return null
+ const node = store.graph.getNode(edit.nodeId)
+ if (!node) return null
+ const abs = store.graph.getAbsolutePosition(node.id)
+ if (edit.side === 'top') return { x: abs.x + node.width / 2, y: abs.y + node.paddingTop / 2 }
+ if (edit.side === 'bottom') {
+ return { x: abs.x + node.width / 2, y: abs.y + node.height - node.paddingBottom / 2 }
+ }
+ if (edit.side === 'left') return { x: abs.x + node.paddingLeft / 2, y: abs.y + node.height / 2 }
+ return { x: abs.x + node.width - node.paddingRight / 2, y: abs.y + node.height / 2 }
+})
+const paddingEditorReference = useCanvasVirtualReference(canvasRef, store, paddingEditorAnchor)
+
+watch(autoLayoutPaddingEdit, async (edit) => {
+ if (!edit) return
+ await nextTick()
+ const input = paddingEditorRef.value?.querySelector<HTMLInputElement>('input')
+ input?.focus()
+ input?.select()
+})
+
const cursor = computed(() => toolCursor(store.state.activeTool, cursorOverride.value))
</script>
@@ -76,6 +126,34 @@ 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>
+ <PopoverRoot :open="!!autoLayoutPaddingEdit">
+ <PopoverPortal>
+ <PopoverContent
+ v-if="autoLayoutPaddingEdit && paddingEditorReference"
+ ref="paddingEditorRef"
+ :reference="paddingEditorReference"
+ side="top"
+ align="center"
+ :side-offset="AUTO_LAYOUT_PADDING_EDITOR_OFFSET_Y"
+ :align-offset="AUTO_LAYOUT_PADDING_EDITOR_OFFSET_X"
+ :collision-padding="8"
+ class="z-50 w-20 rounded-md bg-panel p-1 shadow-lg"
+ data-test-id="auto-layout-padding-editor"
+ @keydown.escape.prevent="cancelAutoLayoutPaddingEdit"
+ @open-auto-focus.prevent
+ >
+ <ScrubInput
+ :model-value="autoLayoutPaddingEdit.value"
+ :min="0"
+ :step="1"
+ icon="↔"
+ data-test-id="auto-layout-padding-input"
+ @update:model-value="updateAutoLayoutPaddingEdit"
+ @commit="(value: number) => commitAutoLayoutPaddingEdit(value)"
+ />
+ </PopoverContent>
+ </PopoverPortal>
+ </PopoverRoot>
<Transition leave-active-class="transition-opacity duration-300" leave-to-class="opacity-0">
<div
v-if="store.state.loading"