Skip to content

[lua] Added autofit and sizeHint properties to Dialog (fix #5176)#5321

Merged
dacap merged 1 commit into
aseprite:mainfrom
ckaiser:dialog-autofit
Aug 26, 2025
Merged

[lua] Added autofit and sizeHint properties to Dialog (fix #5176)#5321
dacap merged 1 commit into
aseprite:mainfrom
ckaiser:dialog-autofit

Conversation

@ckaiser

@ckaiser ckaiser commented Aug 3, 2025

Copy link
Copy Markdown
Member

Fixes #5176, makes automatic fitting the default, with autofit set to Align.LEFT | Align.TOP. (Note: Could've used ui::HORIZONTAL and ui::VERTICAL since those are more accurate for what we're doing, but they're not exposed to scripting at the moment - I could just add them, though).

This property can be changed via Lua, and I've added a new read-only sizeHint property to know the size the dialog wants to be to fit all the elements.

I've also while testing encountered a bug that caused old drawn pieces of dialogs linger behind them when the bounds are changed to something smaller, so I fixed that by invalidating the old rect when resizing.

Testing script, adapted from the one by @MapleGecko

local labelVisible = false
local dialog

local function generateDialog()
    if dialog ~= nil then dialog:close() end
    dialog = Dialog {
        id = "dialog",
        title = "Dialog",
        autofit = Align.LEFT | Align.TOP,
        onclose = function()
            dialog = nil
        end
    }

    dialog:label { id = "dialog_demolabel", visible = labelVisible, text = "Label is too long to be displayed!!!!!" }

    dialog:combobox {
        id = "dialog_autofit",
        option="Align.LEFT | Align.TOP",
        options={ "Align.LEFT | Align.TOP", "Align.BOTTOM | Align.RIGHT", "Align.BOTTOM | Align.LEFT", "Align.TOP", "Align.BOTTOM", "0" },
        onchange=function()
            local fit = dialog.data["dialog_autofit"]
            if (fit == "Align.LEFT | Align.TOP") then
                dialog.autofit = Align.LEFT | Align.TOP
            elseif (fit == "Align.LEFT") then
                dialog.autofit = Align.LEFT
            elseif (fit == "Align.BOTTOM | Align.RIGHT") then
                dialog.autofit = Align.BOTTOM | Align.RIGHT
            elseif (fit == "Align.BOTTOM | Align.LEFT") then
                dialog.autofit = Align.BOTTOM | Align.LEFT
            elseif (fit == "Align.TOP") then
                dialog.autofit = Align.TOP
            elseif (fit == "Align.BOTTOM") then
                dialog.autofit = Align.BOTTOM
            else
                dialog.autofit = 0
            end
        end
    }

    dialog:button { text = "Show label", onclick = function()
        labelVisible = not labelVisible
        dialog:modify { id = "dialog_demolabel", visible = labelVisible }
        -- Manual bounds with sizeHint
        --dialog.bounds = Rectangle(dialog.bounds.origin, dialog.sizeHint )
    end }

    dialog:show { wait = false }
end

generateDialog()

@ckaiser ckaiser requested a review from dacap as a code owner August 3, 2025 00:17
@aseprite-bot

Copy link
Copy Markdown
Collaborator

clang-tidy review says "All clean, LGTM! 👍"

@dacap

dacap commented Aug 4, 2025

Copy link
Copy Markdown
Member

Hi @ckaiser, from the conversation we had, the idea I was thinking about the autofit was to specify a point of anchor to resize, so TOP|LEFT is the default (as the origin of the window is kept), but we could specify CENTER|MIDDLE (we have to add Align.MIDDLE to Lua) to resize the window from the center, or RIGHT|BOTTOM, etc, or just 0 to avoid resizing.

@dacap dacap assigned ckaiser and unassigned dacap Aug 4, 2025
@aseprite-bot

Copy link
Copy Markdown
Collaborator

clang-tidy review says "All clean, LGTM! 👍"

@ckaiser

ckaiser commented Aug 7, 2025

Copy link
Copy Markdown
Member Author

Updated with the top/right/bottom/left functionality, also updated the script with a dropdown for easier testing.

@ckaiser ckaiser assigned dacap and unassigned ckaiser Aug 7, 2025
@dacap dacap added this to the v1.3.15 milestone Aug 26, 2025
@dacap

dacap commented Aug 26, 2025

Copy link
Copy Markdown
Member

It works great 👍 I think it needs a minor fix in the Dialog_new function (to avoid setting autofit to -1), going to fix it after merging this.

@dacap dacap merged commit 7495317 into aseprite:main Aug 26, 2025
12 checks passed
@ckaiser ckaiser deleted the dialog-autofit branch September 10, 2025 05:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add Dialog:autofitBounds() to automatically resize dialogs from their content

3 participants