flamendless / Slab

An immediate mode GUI for the Love2D framework.
MIT License
286 stars 25 forks source link

How to use `FileDialog` in `MenuItem`? #129

Closed frissyn closed 2 years ago

frissyn commented 2 years ago

I'm using Menus and FileDialogs to create a File > Open functionality in the Main Menu (like Microsoft Word, Photoshop, etc.). A sample of the code looks this:

if slab.MenuItem("Open") then            
    if GUI.state.dialog.popOpen == true then
        res = slab.FileDialog({Type = "openfile"})

        if res.Button ~= "" then
            GUI.state.dialog.popOpen = false
        end

        print("res: ", res.Button, res.Files)
    end
end

Assuming that slab.FileDialog needs to be called every frame I need it open, GUI.state.dialog.popOpen is set to true in the love.load function, and is only set to false after the user has responded.

image

This is adapted from the example given in the wiki, and while the FileDialog does open, it only stays open for a frame or two before closing. Any reason why this isn't working?

frissyn commented 2 years ago

After reading through the docs more thoroughly and messing around bit more, it seems that FileDialog can't be called inside an if slab.MenuItem(...) then block because it will only be true for the duration of the click.

I've moved the FileDialog call to love.update, and the MenuItem("Open") will set GUI.state.dialog.popOpen to true on user click, which solves my issue.

Really impressive library!

flamendless commented 2 years ago

Glad you figured it out :) immediate mode UI can take a while to fully grasp how to do things around but once you get it, it'll be a breeze afterwards.

Thanks for the kind words :) credits to the original developer of this library