Fix preview window growing on mode switch

Don't close/reopen preview when cycling All/One/Static modes.
Just update button labels and repaint in place, preserving window
size and position. Remove unused pvWinW/H saved size system.
Fit still reopens with calculated content size (All/One only).
This commit is contained in:
Cidwel Highwind 2026-04-04 14:27:52 +02:00
parent 3517e65294
commit 6a97dbd527
1 changed files with 17 additions and 54 deletions

View File

@ -118,10 +118,6 @@ local S = {
previewLayoutValue = 2, -- number for fixed cols/rows
previewMode = "all", -- "all", "single", or "static"
previewSingleIdx = 1, -- which animation to show in single mode
-- Per-mode preview window size (0 = auto/not set yet)
pvWinWAll = 0, pvWinHAll = 0,
pvWinWOne = 0, pvWinHOne = 0,
pvWinWSta = 0, pvWinHSta = 0,
-- Static preview scroll/pan
previewStaScrollX = 0,
previewStaScrollY = 0,
@ -314,12 +310,6 @@ local function savePreset(presetName)
f:write("gbAlwaysOverwrite=" .. tostring(S.gbAlwaysOverwrite) .. "\n")
f:write("gbTileW=" .. S.gbTileW .. "\n")
f:write("gbTileH=" .. S.gbTileH .. "\n")
f:write("pvWinWAll=" .. S.pvWinWAll .. "\n")
f:write("pvWinHAll=" .. S.pvWinHAll .. "\n")
f:write("pvWinWOne=" .. S.pvWinWOne .. "\n")
f:write("pvWinHOne=" .. S.pvWinHOne .. "\n")
f:write("pvWinWSta=" .. S.pvWinWSta .. "\n")
f:write("pvWinHSta=" .. S.pvWinHSta .. "\n")
for i, name in ipairs(S.animNames) do
f:write("anim_" .. (i - 1) .. "=" .. serializeFrames(S.anims[name] or {}) .. "\n")
end
@ -388,12 +378,6 @@ local function loadPreset(presetName)
S.gbTileW = ts
S.gbTileH = ts
end
if k == "pvWinWAll" then S.pvWinWAll = tonumber(v) or 0 end
if k == "pvWinHAll" then S.pvWinHAll = tonumber(v) or 0 end
if k == "pvWinWOne" then S.pvWinWOne = tonumber(v) or 0 end
if k == "pvWinHOne" then S.pvWinHOne = tonumber(v) or 0 end
if k == "pvWinWSta" then S.pvWinWSta = tonumber(v) or 0 end
if k == "pvWinHSta" then S.pvWinHSta = tonumber(v) or 0 end
-- Dynamic anim frames: anim_0, anim_1, ...
local animIdx = k and string.match(k, "^anim_(%d+)$")
if animIdx then
@ -1346,41 +1330,14 @@ openPreviewWindow = function()
end
end
-- Save current window bounds into S for the given mode
local function saveBoundsForMode(mode)
if not previewDlg then return end
local ok, b = pcall(function() return previewDlg.bounds end)
if not ok or not b then return end
if mode == "all" then S.pvWinWAll = b.width; S.pvWinHAll = b.height
elseif mode == "single" then S.pvWinWOne = b.width; S.pvWinHOne = b.height
elseif mode == "static" then S.pvWinWSta = b.width; S.pvWinHSta = b.height
end
end
-- Get saved size for current mode, or fallback to calcPreviewSize
local function getWinSize()
local w, h = 0, 0
if S.previewMode == "all" then w = S.pvWinWAll; h = S.pvWinHAll
elseif S.previewMode == "single" then w = S.pvWinWOne; h = S.pvWinHOne
elseif S.previewMode == "static" then w = S.pvWinWSta; h = S.pvWinHSta
end
if w > 0 and h > 0 then return w, h end
-- No saved size: use content fit (Sta defaults to 200x200)
if S.previewMode == "static" then return 200, 200 end
local cw, ch = calcPreviewSize()
return math.max(cw, 120), math.max(ch, 40)
end
-- Calculate initial canvas size from saved window size
local pvWidth, pvHeight = getWinSize()
-- Canvas is smaller than window (buttons/decorations), but we approximate
-- Calculate initial canvas size from content fit
local pvWidth, pvHeight = calcPreviewSize()
pvWidth = math.max(pvWidth, 120)
pvHeight = math.max(pvHeight, 40)
previewDlg = Dialog{
title = "AniPhallow (" .. S.currentPreset .. ")",
onclose = function()
saveBoundsForMode(S.previewMode)
if previewTimer then pcall(function() previewTimer:stop() end) end
if pvRefreshTimer then pcall(function() pvRefreshTimer:stop() end) end
previewTimer = nil
@ -1404,8 +1361,6 @@ openPreviewWindow = function()
id = "pvToggleMode",
text = getModeLabel(),
onclick = function()
-- Save current bounds before switching
saveBoundsForMode(S.previewMode)
if S.previewMode == "all" then
S.previewMode = "single"
if #S.animNames > 0 then
@ -1418,9 +1373,21 @@ openPreviewWindow = function()
else
S.previewMode = "all"
end
-- Close and reopen with new mode's saved size
pcall(function() previewDlg:close() end)
openPreviewWindow()
-- Update button label, anim name visibility, Fit enabled state
pcall(function()
previewDlg:modify{ id = "pvToggleMode", text = getModeLabel() }
previewDlg:modify{ id = "pvAnimName", visible = (S.previewMode == "single") }
previewDlg:modify{ id = "pvFit", enabled = (S.previewMode ~= "static") }
if S.previewMode == "single" then
local name = ""
if S.previewSingleIdx >= 1 and S.previewSingleIdx <= #S.animNames then
name = S.animNames[S.previewSingleIdx]
end
previewDlg:modify{ id = "pvAnimName", text = name }
end
previewDlg:repaint()
end)
saveAll()
end
}
@ -1430,10 +1397,6 @@ openPreviewWindow = function()
enabled = (S.previewMode ~= "static"),
onclick = function()
if S.previewMode == "static" then return end
-- Clear saved size so it recalculates from content
if S.previewMode == "all" then S.pvWinWAll = 0; S.pvWinHAll = 0
elseif S.previewMode == "single" then S.pvWinWOne = 0; S.pvWinHOne = 0
end
pcall(function() previewDlg:close() end)
openPreviewWindow()
end