Modal dialog

Started by Kvaz1r, 28 September 2023, 12:21:55

Kvaz1r

I don't found a way create modal (blocking) dialog in TGUI now, but it's something that might be using often (e.g. input password or confirmation).

texus

You can use the technique described in https://tgui.eu/examples/1.0/blocking-events-outside-child-window/, but with a MessageBox instead of a ChildWindow

Kvaz1r

Quote from: texus on 28 September 2023, 17:19:31You can use the technique described in https://tgui.eu/examples/1.0/blocking-events-outside-child-window/, but with a MessageBox instead of a ChildWindow

Nice technique, but it's won't work here. For example I have some Yes/No confirmation dialog that showing on button click:
button->onPress([this](const tgui::String& text) {
            if (showModal(L"Confirm nick changing?"))
            {
            // do something with gui
            }
 });
without real blocking showModal must return something before user perform any action. But if I block thread then gui loop won't be updated.

texus

#3
I somehow missed your previous message before.

All you should need to do in your code is keep a flag that a message box is open, and not do anything in the main loop except for passing events to TGUI when the flag is set (until you get signal from the message box that it was closed).

I can try adding a function like "showModal" to the gui that takes away your control like "gui.mainLoop()", but I fear it might have problems that can't be solved. What happens when a user closes the SFML window while a modal message box is being shown? TGUI will read the close event, might return a "cancel" return value from showModal and your code will continue without knowing that the window was ever closed. This might not be so bad in SFML where you usually call "window.isOpen()" in your main loop, but this could prove problematic in code that relies on sf::Event::Closed being sent (which might e.g. happen in SDL backends as main loops there usually only end when SDL_EVENT_QUIT is received).
Resize events would also be discarded by TGUI, so your program would be unaware if the window size was changed while the message box was open.

The only case where showModal would work without issues is if you are already using "gui.mainLoop()". If you have your own main loop, which I imagine most non-trivial programs will have, then it simply can't work well if I take control of the main loop in TGUI while the message box is shown.

Kvaz1r

Quote from: texus on 30 September 2023, 09:23:00All you should need to do in your code is keep a flag that a message box is open, and not do anything in the main loop except for passing events to TGUI when the flag is set (until you get signal from the message box that it was closed).

Yeah, it should work. Not sure though how complex this approach will be if I'd have modal dialog inside modal child window, but for my current needs it suits.