Search in sources :

Example 1 with MouseChecker

use of com.intellij.openapi.ui.popup.MouseChecker in project android by JetBrains.

the class LintNotificationPanel method setupPopup.

/**
   * Sets-up the panel popup and calls the passed {@link Consumer<JBPopup>} with the new {@link JBPopup} instance.
   * This method will not display the popup.
   */
public JBPopup setupPopup(@Nullable Project project, @NotNull Consumer<JBPopup> onPopupBuilt) {
    JBPopup builder = JBPopupFactory.getInstance().createComponentPopupBuilder(myPanel, myPanel).setProject(project).setDimensionServiceKey(project, DIMENSION_KEY, false).setResizable(true).setMovable(true).setMinSize(MIN_POPUP_SIZE).setRequestFocus(true).setTitle("Lint Warnings in Layout").setCancelOnClickOutside(true).setLocateWithinScreenBounds(true).setShowShadow(true).setCancelOnWindowDeactivation(true).setCancelOnMouseOutCallback(new MouseChecker() {

        Rectangle myDismissRectangle;

        double myPreviousDistance = 0;

        @Override
        public boolean check(MouseEvent event) {
            if (myPopup == null) {
                return false;
            }
            Point mousePosition = event.getPoint();
            SwingUtilities.convertPointToScreen(mousePosition, event.getComponent());
            Point popupPosition = myPopup.getLocationOnScreen();
            Dimension popupDimension = myPopup.getSize();
            int centerX = popupPosition.x + popupDimension.width / 2;
            int centerY = popupPosition.y + popupDimension.height / 2;
            // Is it the mouse getting closer to the center of the popup or moving away? We only close the dialog if the mouse is
            // moving away.
            double currentDistance = mousePosition.distance(centerX, centerY);
            double previousDistance = myPreviousDistance;
            myPreviousDistance = currentDistance;
            boolean mouseMovingAway = previousDistance != 0 && currentDistance > previousDistance;
            if (!mouseMovingAway) {
                // We only dismiss the dialog if the mouse is moving away from the center
                return false;
            }
            int dismissRectX = popupPosition.x - DISMISS_MARGIN_PX;
            int dismissRectY = popupPosition.y - DISMISS_MARGIN_PX;
            int dismissRectW = popupDimension.width + 2 * DISMISS_MARGIN_PX;
            int dismissRectH = popupDimension.height + 2 * DISMISS_MARGIN_PX;
            if (myDismissRectangle == null) {
                myDismissRectangle = new Rectangle(dismissRectX, dismissRectY, dismissRectW, dismissRectH);
            } else {
                myDismissRectangle.setBounds(dismissRectX, dismissRectY, dismissRectW, dismissRectH);
            }
            return !myDismissRectangle.contains(mousePosition);
        }
    }).createPopup();
    myPopup = builder;
    Disposer.register(myScreenView.getSurface(), myPopup);
    onPopupBuilt.accept(builder);
    return builder;
}
Also used : MouseEvent(java.awt.event.MouseEvent) MouseChecker(com.intellij.openapi.ui.popup.MouseChecker) RelativePoint(com.intellij.ui.awt.RelativePoint) JBPopup(com.intellij.openapi.ui.popup.JBPopup) RelativePoint(com.intellij.ui.awt.RelativePoint)

Aggregations

JBPopup (com.intellij.openapi.ui.popup.JBPopup)1 MouseChecker (com.intellij.openapi.ui.popup.MouseChecker)1 RelativePoint (com.intellij.ui.awt.RelativePoint)1 MouseEvent (java.awt.event.MouseEvent)1