How to make tooltip transparent for mouse events?

A javafx.scene.Nodehas the ability to make it transparent to mouse events, so that it will not be selected as the target for such events:

Node.mouseTransparentProperty ()
If true, this node (along with all its children) is completely transparent to mouse events. When choosing a target for a mouse event, nodes with mouseTransparent set to true and their subtrees will not be taken into account.

Unfortunately, this feature is not yet implemented for javafx.scene.control.Tooltip.
There is an open request function for this, but there seems to be not much on this topic.

My question is: is there any workaround for this? How can I make the mouse tooltip transparent to route mouse events to the base control?

+4
source share
1 answer

If someone is still looking for a solution. I found a hacker path in javafx-8 (using the internal API!). It is convenient to patch it in javafx-8 +, so from the point of view of maintenance it is not a good option, but at least something:

    public static boolean correctNativeMouseEvent(MouseEvent event, Scene exclude)
    {
        Scene targetScene = getTargetScreen(event, exclude);
        if(targetScene != null)
        {
            PickResultChooser chooser = new PickResultChooser();

            targetScene.getRoot().impl_pickNode(new PickRay(event.getScreenX() - targetScene.getWindow().getX() - targetScene.getX(),
                    event.getScreenY() - targetScene.getWindow().getY() - targetScene.getY(),
                    1, Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY), chooser);

            PickResult res = chooser.toPickResult();
            if(res != null)
            {
                Point2D pos = res.getIntersectedNode().localToScene(res.getIntersectedPoint().getX(), res.getIntersectedPoint().getY());

                MouseEvent newEvent = new MouseEvent(null, null, event.getEventType(), pos.getX(), pos.getY(),
                        event.getScreenX(), event.getScreenY(),
                        event.getButton(), event.getClickCount(),
                        event.isShiftDown(), event.isControlDown(), event.isAltDown(), event.isMetaDown(),
                        event.isPrimaryButtonDown(), event.isMiddleButtonDown(), event.isSecondaryButtonDown(),
                        event.isSynthesized(), event.isPopupTrigger(), event.isStillSincePress(), res);

                targetScene.impl_processMouseEvent(newEvent);
                return true;
            }
        }
        return false;
    }

    static Scene getTargetScreen(MouseEvent event, Scene exclude)
    {
        double x = event.getScreenX();
        double y = event.getScreenY();

        double sx, sy, sw, sh;

        Iterator<Window> itr = Window.impl_getWindows();

        if(itr.hasNext())
        {
            for(Window w = itr.next(); itr.hasNext(); w = itr.next())
            {
                sx = w.getX();
                sy = w.getY();
                sw = w.getWidth();
                sh = w.getHeight();

                if(sx < x && x < sx + sw
                        && sy < y && y < sy + sh
                        && w.getScene() != exclude)
                    return w.getScene();
            }
        }
        return null;
    }

when creating a tooltip, you simply add the following:

Tooltip tp = new Tooltip();
// use filter to catch before anything can be consumed
tp.addEventFilter(MouseEvent.ANY, E -> {
    // now correct the event
    correctNativeMouseEvent(E, tp.getScene());
    // although it is optionally, I would recommend to just consume the event anyways
    E.consume();
};
+1
source

Source: https://habr.com/ru/post/1598092/


All Articles