@Override
  public boolean dispatch(final @NotNull AWTEvent e) {
    LOG.assertTrue(isPopupActive());

    if (e.getID() == WindowEvent.WINDOW_LOST_FOCUS || e.getID() == WindowEvent.WINDOW_DEACTIVATED) {
      if (IdeEventQueueKt.getSkipWindowDeactivationEvents()) {
        LOG.warn("Skipped " + e);
        return false;
      }

      if (StartupUiUtil.isWaylandToolkit()) {
        // Reasons for skipping 'focus lost'-like events on Wayland:
        // - When a new popup window appears, the main frame looses focus, but the "opposite window"
        //   for that event is null (because Wayland); this can be solved by waiting a bit
        //   (several hundreds ms) for
        //   KeyboardFocusManager.getCurrentKeyboardFocusManager().getFocusedWindow()
        //   to become non-null.
        // - When a (popup) window is dragged, it also looses focus (because Wayland); this one is
        //   not solvable because there's no guarantee that the focus will get back, nor is there
        //   a notification that the drag is actually happening.
        return false;
      }

      if (!isPopupActive()) return false;

      Window sourceWindow = ((WindowEvent)e).getWindow();
      if (SystemInfo.isLinux && !sourceWindow.isShowing()) {
        // Ignore focusLost/deactivated events caused by some window closing on Linux.
        // Normally, in such cases another IDE window is focused, and 'opposite' event property should point to that window.
        // On Linux however, due to current JDK implementation, 'opposite' property is null in this case,
        // and the following code mistakenly assumes focus is transferred to another application.
        return false;
      }

      Window focusedWindow = ((WindowEvent)e).getOppositeWindow();
      maybeCloseAllPopups(focusedWindow, sourceWindow);
      return false;
    }
    else if (e instanceof KeyEvent keyEvent) {
      // the following is copied from IdeKeyEventDispatcher
      Object source = keyEvent.getSource();
      if (myIgnoreNextKeyTypedEvent) {
        if (KeyEvent.KEY_TYPED == e.getID()) return true;
        myIgnoreNextKeyTypedEvent = false;
      }
      else if (SystemInfo.isMac && InputEvent.ALT_DOWN_MASK == keyEvent.getModifiersEx() &&
               Registry.is("ide.mac.alt.mnemonic.without.ctrl") && source instanceof Component) {
        // the myIgnoreNextKeyTypedEvent changes event processing to support Alt-based mnemonics on Mac only
        if (KeyEvent.KEY_TYPED == e.getID() && !IdeEventQueue.getInstance().isInputMethodEnabled() ||
            IdeKeyEventDispatcher.hasMnemonicInWindow((Component)source, keyEvent)) {
          myIgnoreNextKeyTypedEvent = true;
          return false;
        }
      }
    }

    if (e instanceof KeyEvent || e instanceof MouseEvent || e instanceof InputMethodEvent) {
      for (int i = myDispatchStack.size() - 1; i >= 0 && i < myDispatchStack.size(); i--) {
        final boolean dispatched = myDispatchStack.get(i).dispatch(e);
        if (dispatched) return true;
      }
    }

    return false;
  }
@Override
  public boolean dispatch(final @NotNull AWTEvent e)