fix: convert window px size to canvas-units (w/zoom) for dock + gap/center math

This commit is contained in:
Hermes Agent committed 2026-09-09 15:50:34 +02:00
1 parent 13f3129c78
commit 4228357a35
1 file changed
+19 -10
+19 -10
View File
@@ -15,17 +15,22 @@ export default function () {
let dockedPosition: 'left' | 'right' | 'center' | null = null let dockedPosition: 'left' | 'right' | 'center' | null = null
// Прижать окно к краю видимой области canvas (bounds) в canvas-space. // Прижать окно к краю видимой области canvas (bounds) в canvas-space.
// reposition() интерпретирует x/y как canvas-space, поэтому окно визуально // ВАЖНО: reposition()/getPosition() и bounds — в canvas-space (единицы документа),
// остаётся у края ЭКРАНА только пока мы пересчитываем его при изменении viewport. // а figma.ui.resize(width,height) — в ЭКРАННЫХ пикселях. Размер окна в canvas-units =
// width / zoom. Без этого деления при zoom≠100% окно прижимается с ошибкой
// (px-размер вычитается из canvas-ширины → уезжает пропорционально 1/zoom).
function applyDock (position: 'left' | 'right' | 'center', width: number, height: number) { function applyDock (position: 'left' | 'right' | 'center', width: number, height: number) {
const b = figma.viewport.bounds const b = figma.viewport.bounds
const zoom = figma.viewport.zoom || 1
const wCanvas = width / zoom
const hCanvas = height / zoom
let x = b.x let x = b.x
let y = b.y let y = b.y
if (position === 'right') { if (position === 'right') {
x = b.x + b.width - width x = b.x + b.width - wCanvas
} else if (position === 'center') { } else if (position === 'center') {
x = b.x + (b.width - width) / 2 x = b.x + (b.width - wCanvas) / 2
y = b.y + (b.height - height) / 2 y = b.y + (b.height - hCanvas) / 2
} }
figma.ui.reposition(x, y) figma.ui.reposition(x, y)
} }
@@ -86,19 +91,23 @@ export default function () {
// getPosition может быть недоступен в некоторых версиях — оставляем 0 // getPosition может быть недоступен в некоторых версиях — оставляем 0
} }
const zoom = figma.viewport.zoom const zoom = figma.viewport.zoom || 1
const b = figma.viewport.bounds const b = figma.viewport.bounds
const w = lastSize.width const w = lastSize.width
const h = lastSize.height const h = lastSize.height
// Размер окна в ЭКРАННЫХ пикселях (w/h) переводим в canvas-units через zoom,
// чтобы корректно вычитать из canvas-ширины bounds при любом зуме.
const wCanvas = w / zoom
const hCanvas = h / zoom
// Геометрия окна плагина относительно видимой области canvas (bounds). // Геометрия окна плагина относительно видимой области canvas (bounds).
// gapL/gapR/gapT/gapB — отступы окна от левого/правого/верхнего/нижнего края canvas. // gapL/gapR/gapT/gapB — отступы окна от левого/правого/верхнего/нижнего края canvas.
const gapL = Math.round(x - b.x) const gapL = Math.round(x - b.x)
const gapT = Math.round(y - b.y) const gapT = Math.round(y - b.y)
const gapR = Math.round(b.x + b.width - (x + w)) const gapR = Math.round(b.x + b.width - (x + wCanvas))
const gapB = Math.round(b.y + b.height - (y + h)) const gapB = Math.round(b.y + b.height - (y + hCanvas))
const cx = Math.round(x + w / 2) const cx = Math.round(x + wCanvas / 2)
const cy = Math.round(y + h / 2) const cy = Math.round(y + hCanvas / 2)
emit('WINDOW_INFO', { emit('WINDOW_INFO', {
x, x,