diff --git a/src/main.ts b/src/main.ts index c18e529..740bc5f 100644 --- a/src/main.ts +++ b/src/main.ts @@ -40,6 +40,9 @@ export default function () { // Stored on the Figma file via clientStorage (survives close/reopen). Saved after every // action and periodically (to catch the native Figma drag-by-titlebar the plugin can't see). const PLACEMENT_KEY = 'figma-ai-plugin:placement' + // Main-side mirror of the maximize toggle, so the periodic native-drag poller can + // re-persist it (otherwise it would overwrite the maximized flag with undefined). + let maximizedState = false type Placement = | { mode: 'dock'; position: DockPosition } | { mode: 'free'; x: number; y: number; w: number; h: number; maximized?: boolean } @@ -57,6 +60,7 @@ export default function () { // Used by maximize and corner docks, which must remember the EXACT size/position // rather than re-deriving it from a dock formula (which would reset the size). function saveFreeFromCurrent (maximized?: boolean) { + if (maximized !== undefined) maximizedState = maximized === true try { const pos = (figma.ui as any).getPosition() savePlacement({ @@ -65,7 +69,7 @@ export default function () { y: Math.round(pos.canvasSpace.y), w: lastSize.width, h: lastSize.height, - maximized: maximized === true + maximized: maximizedState }) } catch (e) { /* getPosition unavailable — skip */ } } @@ -182,7 +186,8 @@ export default function () { x: Math.round(pos.canvasSpace.x), y: Math.round(pos.canvasSpace.y), w: lastSize.width, - h: lastSize.height + h: lastSize.height, + maximized: maximizedState }) } } catch (e) { /* getPosition unavailable — skip */ } @@ -278,6 +283,8 @@ export default function () { applyDock(dockedPosition, width, height, false) savePlacement({ mode: 'dock', position: dockedPosition }) } else { + // Free (or center one-shot): manual resize clears the maximized flag. + maximizedState = false // Free (or center one-shot): save the actual canvas-space position + size. try { const pos = (figma.ui as any).getPosition() @@ -317,6 +324,8 @@ export default function () { lastBoundsKey = '' // форсируем пересчёт на ближайшем тике } else { dockedPosition = null + // Restore (center) or a corner teleport clears the maximized state. + maximizedState = false } // Попытка растяжения до канона (Figma клампит до лимита при видимом UI). const state = computeDock(position, payload.width, payload.height, true) @@ -361,6 +370,7 @@ export default function () { figma.ui.resize(size.width, size.height) // Выходим из дока: чистый клик = телепорт в угол без удержания. dockedPosition = null + maximizedState = false lastBoundsKey = '' lastSize = { width: size.width, height: size.height } // Прижимаем в угол от реального размера (computeDock для угла не меняет размер). @@ -383,7 +393,7 @@ export default function () { on('SAVE_FREE', function (payload?: { maximized?: boolean }) { dockedPosition = null // maximize is not held; clear any stale dock state lastBoundsKey = '' - saveFreeFromCurrent(payload != null ? payload.maximized === true : false) + saveFreeFromCurrent(payload != null ? payload.maximized === true : maximizedState) }) // Останавливаем поллинг при закрытии плагина, чтобы не висел в памяти. @@ -500,6 +510,7 @@ export default function () { } else { // Edge dock: stretch to its canonical size and pin (held across zoom/pan). dockedPosition = saved.position + maximizedState = false const state = computeDock(saved.position, 320, 200, true) figma.ui.resize(state.width, state.height) lastSize = { width: clampWindowSize(state).width, height: clampWindowSize(state).height } @@ -515,8 +526,10 @@ export default function () { figma.ui.resize(saved.w, saved.h) figma.ui.reposition(saved.x, saved.y) lastSize = { width: saved.w, height: saved.h } - // If it was maximized, tell UI to show the Restore button on next open. + // If it was maximized, remember it (so the poller re-persists the flag) and tell + // UI to show the Restore button on next open. if (saved.maximized === true) { + maximizedState = true emit('SET_MAXIMIZED', { maximized: true }) } }