AI-native design editor. Open-source Figma alternative.
import type { Canvas } from 'canvaskit-wasm'
import type { SceneGraph } from '@open-pencil/scene-graph'
import type { SkiaRenderer } from '#core/canvas/renderer'
import {
HANDLE_HALF_SIZE,
SIZE_FONT_SIZE,
SIZE_PILL_HEIGHT,
SIZE_PILL_PADDING_X,
SIZE_PILL_RADIUS
} from '#core/constants'
import type { ShapeHandleHover } from '#core/editor/types'
import {
SHAPE_HANDLE,
shapeHandleLayout,
shapeHandleValue,
type RotationPreview
} from '#core/geometry'
/**
* The selected rectangle's, polygon's, star's, or ellipse's handles while the pointer is over it:
* white circles the size of the resize handles with a border in the selection colour, dotted when
* a drag changes one corner and on an arc's start, and a label such as "Radius 12" by the pointer
* for the handle in use, as in Figma desktop.
*/
export function drawShapeHandles(
r: SkiaRenderer,
canvas: Canvas,
graph: SceneGraph,
selectedIds: ReadonlySet<string>,
hover: ShapeHandleHover | null | undefined,
preview?: RotationPreview | null
): void {
if (!hover || selectedIds.size !== 1 || !selectedIds.has(hover.nodeId)) return
const node = graph.getNode(hover.nodeId)
if (!node) return
const handles = shapeHandleLayout(node, r.sceneGeometry(graph, preview), r)
if (!handles) return
const color = r.outlineColor(node, graph)
const fill = r.auxFill
const border = r.auxStroke
fill.setMaskFilter(null)
border.setPathEffect(null)
border.setStrokeWidth(1)
border.setColor(color)
for (const { handle, point } of handles) {
fill.setColor(r.ck.WHITE)
canvas.drawCircle(point.x, point.y, HANDLE_HALF_SIZE, fill)
canvas.drawCircle(point.x, point.y, HANDLE_HALF_SIZE, border)
if (!hover.single && handle !== 'arcStart') continue
fill.setColor(color)
canvas.drawCircle(point.x, point.y, SHAPE_HANDLE.dotRadius, fill)
}
const provider = r.fontProvider
if (!hover.handle || !provider) return
// A paragraph rather than the size label's single font, so every script has a fallback face.
const text = `${hover.label} ${shapeHandleValue(node, hover.handle)}`
const white = r.ck.WHITE
const measured = r.labelParagraphCache.measure(
r.ck,
provider,
text,
SIZE_FONT_SIZE,
Number.POSITIVE_INFINITY,
white,
r.fontGeneration
)
const { labelOffset } = SHAPE_HANDLE
const left = hover.pointer.x + labelOffset.x
const top = hover.pointer.y + labelOffset.y - SIZE_PILL_HEIGHT / 2
fill.setColor(color)
canvas.drawRRect(
r.ck.RRectXY(
r.ck.LTRBRect(
left,
top,
left + measured.width + SIZE_PILL_PADDING_X * 2,
top + SIZE_PILL_HEIGHT
),
SIZE_PILL_RADIUS,
SIZE_PILL_RADIUS
),
fill
)
r.labelParagraphCache.draw(
r.ck,
canvas,
provider,
text,
SIZE_FONT_SIZE,
Number.POSITIVE_INFINITY,
white,
r.fontGeneration,
left + SIZE_PILL_PADDING_X,
top + (SIZE_PILL_HEIGHT - measured.height) / 2
)
}