sirk390 / wxasync

asyncio support for wxpython
MIT License
76 stars 9 forks source link

Add AsyncShowDialogWindowModal #19

Open jmkd3v opened 2 years ago

jmkd3v commented 2 years ago

On macOS, the behavior of .ShowWindowModal() is quite useful as it allows dialogs to be shown within their parent window rather than as a separate window

Screen Shot 2022-04-13 at 10 39 16

I am requesting a new function, AsyncShowDialogWindowModal, which behaves just like ShowWindowModal but yields instead of using another thread. The behavior of ShowWindowModal is quite silly - on Mac it doesn't yield and instead uses another thread(?) and then fires an event when it is closed, but on Windows and GTK it does yield and has the same behavior as ShowModal.

This is how I implement it in my code:

import platform
system = platform.system()

# ...

async def show_window_modal(dialog: wx.Dialog):
    if system == "Darwin":
        event = asyncio.Event()
        return_code = None

        def handle_event(dialog_event: wx.WindowModalDialogEvent):
            nonlocal return_code
            return_code = dialog_event.GetReturnCode()
            event.set()

        dialog.Bind(
            event=wx.EVT_WINDOW_MODAL_DIALOG_CLOSED,
            handler=handle_event
        )
        dialog.ShowWindowModal()

        await event.wait()
        return return_code
    else:
       return await wxasync.AsyncShowDialogModal(dialog)
sirk390 commented 2 years ago

Looks like a good addition, but I don't understand your code. Why test for 'os.name =="posix"' when you told me that there is a difference between Mac ("doesn't yield") and Windows and GTK ("does yield and has the same behavior as ShowModal") Shouldn't you test for MaOS instead? There might also be something missing in the else statement, as the doc says that ShowWindowModal "prevent the user from interacting with their parent frame only but not with the rest of the application"

jmkd3v commented 2 years ago

Why test for 'os.name =="posix"'

Fixed this.

There might also be something missing in the else statement, as the doc says that ShowWindowModal "prevent the user from interacting with their parent frame only but not with the rest of the application"

Yeah, that's a good point. In my app I sort of ignored this because I never have more than one frame except for dialogs - I guess we would need to add some extra logic to this.