# Bug FIx: Sticky mouseover highlight with juce::Button

**URL:** <https://forum.juce.com/t/bug-fix-sticky-mouseover-highlight-with-juce-button/46794>\
**Category:** General JUCE discussion\
**Created:** [July 2, 2021, 2:40pm UTC](https://forum.juce.com/t/bug-fix-sticky-mouseover-highlight-with-juce-button/46794 "2021-07-02T14:40:20Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![jimc](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/jimc/32/16067_2.png) [@jimc](https://forum.juce.com/u/jimc)\
**Post date:** [July 2, 2021, 2:40pm UTC](https://forum.juce.com/t/bug-fix-sticky-mouseover-highlight-with-juce-button/46794/1 "2021-07-02T14:40:20Z")

</div>

This code results in a stuck mouseover highlight on the button when right-clicking to see the menu.

Any suggestions as to how to avoid it?

The problem goes away if I choose setTriggeredOnMouseDown(true), but we don’t always want that!

```auto
    class B : public Button
    {
    public:
        B() : Button({})
        {
// setTriggeredOnMouseDown(true);
        }

        void paintButton(Graphics & g, bool shouldDrawButtonAsHighlighted, bool shouldDrawButtonAsDown) override
        {
            g.setColour(Colours::white);

            if (shouldDrawButtonAsHighlighted)
                g.fillAll(Colours::white.withAlpha(0.2f));

            g.drawText("Button", getLocalBounds(), Justification::centred);
            g.drawRect(getLocalBounds(), 1);

        }

        void clicked() override
        {
            PopupMenu m;
            m.addItem("Item", []() {});
            m.showMenuAsync({});
        }
        
    };

```

---

<div class="post-metadata">

**Author:** ![jimc](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/jimc/32/16067_2.png) [@jimc](https://forum.juce.com/u/jimc)\
**Post date:** [July 3, 2021, 4:57pm UTC](https://forum.juce.com/t/bug-fix-sticky-mouseover-highlight-with-juce-button/46794/2 "2021-07-03T16:57:32Z")

</div>

Some notes on this bug. It’s a problem with Component or Button. It’s not a repaint issue. The internal state of Button is stuck in mouseOver. After clicking on the button no mouseExit event is ever sent to the Button even once the mouse has moved away from the popup menu.

---

<div class="post-metadata">

**Author:** ![jimc](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/jimc/32/16067_2.png) [@jimc](https://forum.juce.com/u/jimc)\
**Post date:** [July 3, 2021, 5:08pm UTC](https://forum.juce.com/t/bug-fix-sticky-mouseover-highlight-with-juce-button/46794/3 "2021-07-03T17:08:54Z")

</div>

The problem is fixed by moving updateState to be after the click event is triggered. And copying the “deletionWatcher” pattern from elsewhere in Button.

Suggested replacement code is below and would fix another long standing niggle 🙂

```auto
void Button::mouseUp (const MouseEvent& e)
{
    WeakReference<Component> deletionWatcher (this);

    const bool wasDown = isDown();
    const bool wasOver = isOver();

    if (wasDown && wasOver && ! triggerOnMouseDown)
    {
        if (lastStatePainted != buttonDown)
            flashButtonState();

        internalClickCallback (e.mods);
    }

    if (deletionWatcher != nullptr)
        updateState (isMouseSourceOver (e), false);
}

```

This replaces the old code:

```auto
void Button::mouseUp (const MouseEvent& e)
{
    const bool wasDown = isDown();
    const bool wasOver = isOver();

    updateState (isMouseSourceOver (e), false);

    if (wasDown && wasOver && ! triggerOnMouseDown)
    {
        if (lastStatePainted != buttonDown)
            flashButtonState();

        internalClickCallback (e.mods);
    }  
}

```

Using setTriggeredOnMouseDown(…) doesn’t have the same problem with PopupMenu, the mouseOver state clears for different reasons. Although i wonder if it would be more consistent to make the same change to the mouseDown(…) call.

---

<div class="post-metadata">

**Author:** ![jimc](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/jimc/32/16067_2.png) [@jimc](https://forum.juce.com/u/jimc)\
**Post date:** [July 9, 2021, 11:54am UTC](https://forum.juce.com/t/bug-fix-sticky-mouseover-highlight-with-juce-button/46794/4 "2021-07-09T11:54:31Z")

</div>

Can we get this patched in develop pls 🙂

---

<div class="post-metadata">

**Author:** ![ed95](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/ed95/32/7635_2.png) [@ed95](https://forum.juce.com/u/ed95)\
**Post date:** [July 9, 2021, 4:39pm UTC](https://forum.juce.com/t/bug-fix-sticky-mouseover-highlight-with-juce-button/46794/5 "2021-07-09T16:39:11Z")

</div>

I think we’d still want to keep the first `updateState()` call as moving it might cause some subtle bugs due to the ordering of the state change message and internal click callback changing. `updateState()` will only send a change message if the state has actually changed so we don’t need to worry about duplicate messages, so something like this should work whilst maintaining the old order:

```auto
void Button::mouseUp (const MouseEvent& e)
{
    const auto wasDown = isDown();
    const auto wasOver = isOver();
    updateState (isMouseSourceOver (e), false);

    if (wasDown && wasOver && ! triggerOnMouseDown)
    {
        if (lastStatePainted != buttonDown)
            flashButtonState();

        WeakReference<Component> deletionWatcher (this);

        internalClickCallback (e.mods);

        if (deletionWatcher != nullptr)
            updateState (isMouseSourceOver (e), false);
    }
}

```

I don’t think a similar change is needed in `mouseDown()` since we always call `updateState (true, true);` so the second call after the click callback would be redundant.

Does that work for your use case?

---

<div class="post-metadata">

**Author:** ![jimc](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/jimc/32/16067_2.png) [@jimc](https://forum.juce.com/u/jimc)\
**Post date:** [July 9, 2021, 8:46pm UTC](https://forum.juce.com/t/bug-fix-sticky-mouseover-highlight-with-juce-button/46794/6 "2021-07-09T20:46:20Z")

</div>

I’m pretty confident that’ll fix it. As long as that second post-event updateState is triggered. This will be a great fix. Sticky highlights with PopupMenus have been a low level niggle of a problem for years with loads of JUCE projects 🙂

---

<div class="post-metadata">

**Author:** ![ed95](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/ed95/32/7635_2.png) [@ed95](https://forum.juce.com/u/ed95)\
**Post date:** [July 12, 2021, 9:49am UTC](https://forum.juce.com/t/bug-fix-sticky-mouseover-highlight-with-juce-button/46794/7 "2021-07-12T09:49:57Z")

</div>

Great, I’ve put this on `develop` now:

> <https://github.com/juce-framework/JUCE/commit/5a78b06f5c1e0c05a868a5803ad13549bdc43b5e>
>
> In cases where internalClickCallback() modified the state of the button this was… not being updated until the next call to updateState(). Calling it a second time after the click callback will make sure the state is up to date.
