The correct order for disabling and enabling windows

Raymond Chen

If you want to display modal UI, you need to disable the owner and enable the modal child, and then reverse the procedure when the modal child is finished. And if you do it wrong, focus will get all messed up. If you are finished with a modal dialog, your temptation would be to clean up in the following order:

  • Destroy the modal dialog.
  • Re-enable the owner.

But if you do that, you’ll find that foreground activation doesn’t go back to your owner. Instead, it goes to some random other window. Explicitly setting activation to the intended owner “fixes” the problem, but you still have all the flicker, and the Z-order of the interloper window gets all messed up. What’s going on? When you destroy the modal dialog, you are destroying the window with foreground activation. The window manager now needs to find somebody else to give activation to. It tries to give it to the dialog’s owner, but the owner is still disabled, so the window manager skips it and looks for some other window, somebody who is not disabled. That’s why you get the weird interloper window. The correct order for destroying a modal dialog is

  • Re-enable the owner.
  • Destroy the modal dialog.

This time, when the modal dialog is destroyed, the window manager looks to the owner and hey this time it’s enabled, so it inherits activation.

No flicker. No interloper.

0 comments

Discussion is closed.

Feedback usabilla icon