如何识别调用外部弹窗前的顶层IUP对话框?
Great question! Dealing with window focus after calling external APIs can be tricky, but we can streamline this by dynamically tracking the active IUP dialog before calling Poppy. Here's how to solve it cleanly:
Step 1: Dynamically Get the Active Top-Level Dialog
Instead of hardcoding a specific dlg variable, we can use IUP's built-in functions to grab the currently active dialog (the one the user was interacting with before Poppy pops up):
iup.GetFocus()returns the control that currently has input focus.iup.GetDialog(control)retrieves the parent dialog of that control.- As a fallback,
iup.GetTopWindow()will grab the overall top-most IUP window if no control has focus.
We can wrap this logic into a helper function to make it reusable:
local function get_active_dialog(default_dlg) -- Try to get the dialog from the focused control first local focused_ctrl = iup.GetFocus() local active_dlg = focused_ctrl and iup.GetDialog(focused_ctrl) -- If no focused control, fall back to the top-most window if not active_dlg then active_dlg = iup.GetTopWindow() end -- Ensure we're actually returning a dialog (validate type) if active_dlg and active_dlg.TYPE == "DIALOG" then return active_dlg end -- Final fallback: use your pre-defined default dialog if needed return default_dlg end
Step 2: Wrap Poppy Calls in a Reusable Function
Now we can create a single function that handles calling Poppy and restoring focus automatically. This eliminates repeating code every time you need to call Poppy:
-- Replace `your_default_dialog` with your actual default dialog variable if needed local function call_poppy_and_restore_focus(poppy_args, your_default_dialog) -- Grab the active dialog before invoking Poppy local active_dlg = get_active_dialog(your_default_dialog) -- Call the external Poppy API local res = Poppy(poppy_args) -- Bring the original active dialog back to the front if active_dlg then active_dlg.BRINGFRONT = "YES" end return res end
How to Use It
Instead of writing the focus-restoration code every time, just call this wrapper function:
-- Example call with your 'INDI' argument local res = call_poppy_and_restore_focus('INDI', dlg) return res
Why This Works
- It dynamically adapts to whichever dialog the user was interacting with, so you don't have to hardcode dialog references.
- The fallback logic ensures you always have a dialog to restore, even if no control had focus when Poppy was called.
- Wrapping everything into a helper function cuts down on repetitive code, which is perfect for your use case of multiple Poppy calls.
Just test this in your environment to confirm iup.GetFocus()/iup.GetTopWindow() behave as expected (IUP versions can have minor quirks, but this logic should hold for most setups).
内容的提问来源于stack exchange,提问作者ColeValleyGirl

