[SOLVED] How to stop tooltip from immediately reappearing after mouseDown?

I’ve been implementing tooltips on all my components. I notice that once the tooltip has appeared, if you click on the component to do something, the tooltip disappears; then immediately reappears.

What would be the best way (globally for all tooltips) to keep it from reappearing until you move to a new component? I don’t want to have to deal with this on an individual component level…

Thought I’d post my current solution, in case it helps anyone.

Subclass of TooltipWindow, and override getTipFor(). Have a flag in it that can be set to block the tooltip by something like a mouseDown() anywhere in your MainComponent. Store the component the tooltip was last shown for and if it’s the same, and the tooltip is blocked, don’t show it again.

This also allows you to do things like have any key press dismiss a tooltip and stop it from reappearing until the mouse is moved somewhere else, which I always liked. It also has the advantage of blocking it from showing on a component that you have clicked within the time window, before it pops up.

class MyTooltipWindow : public TooltipWindow
{
public:
    MyTooltipWindow(Component* parent, int ms)
    : TooltipWindow(parent, ms) {}
    
    virtual String getTipFor(Component& c) override
    {
        // if the component we're over is a TooltipClient,
        // see if it's the same as the previous component
        // if so and the tooltipBlocked flag was set, block the tooltip
        // this will keep happening until the component is changed
        
        if (dynamic_cast<TooltipClient*> (&c))
            if (&c == prevComponent.get() && tooltipBlocked)
                return {};
        
        // any other component resets
        prevComponent = &c;
        tooltipBlocked = false;

        // if we make it to here, call the base implementation
        return TooltipWindow::getTipFor(c);
    }
    
    // call by mouseDown(), keyPressed() etc.:
    void setTooltipBlocked(bool state)
    {
        tooltipBlocked = state;
        if (tooltipBlocked)
            hideTip();
    }
    
private:
    WeakReference<Component> prevComponent = nullptr;
    bool tooltipBlocked = false;
};


// in MainComponent:

void mouseDown(const MouseEvent& event)
{
    // mouseListener that listens to all child components
	// with wantsEventsForAllNestedChildComponents    
 
	tooltipWindow.setTooltipBlocked(true);
}
virtual bool keyPressed (const KeyPress& ) override
{
    // any key dismisses tooltips until it is changed

    tooltipWindow.setTooltipBlocked(true);
    return false;
}
1 Like