1
0
Fork 0
mirror of https://github.com/juce-framework/JUCE.git synced 2026-01-10 23:44:24 +00:00

MacOS: Generate mouse move event for the peer when using setRawMousePosition()

Without this MouseInputSource::getComponentUnderMouse() will report a
stale value after moving the cursor within a single peer using
MouseInputSource::setRawMousePosition().
This commit is contained in:
attila 2022-12-07 16:22:25 +01:00
parent a309775160
commit b1ff2eda65

View file

@ -446,13 +446,43 @@ Point<float> MouseInputSource::getCurrentRawMousePosition()
}
}
static ComponentPeer* findPeerContainingPoint (Point<float> globalPos)
{
for (int i = 0; i < juce::ComponentPeer::getNumPeers(); ++i)
{
auto* peer = juce::ComponentPeer::getPeer (i);
if (peer->contains (peer->globalToLocal (globalPos).toInt(), false))
return peer;
}
return nullptr;
}
void MouseInputSource::setRawMousePosition (Point<float> newPosition)
{
const auto oldPosition = Desktop::getInstance().getMainMouseSource().getRawScreenPosition();
// this rubbish needs to be done around the warp call, to avoid causing a
// bizarre glitch..
CGAssociateMouseAndMouseCursorPosition (false);
CGWarpMouseCursorPosition (convertToCGPoint (newPosition));
CGAssociateMouseAndMouseCursorPosition (true);
// Mouse enter and exit events seem to be always generated as a consequence of programmatically
// moving the mouse. However, when the mouse stays within the same peer no mouse move event is
// generated, and we lose track of the correct Component under the mouse. Hence, we need to
// generate this missing event here.
if (auto* peer = findPeerContainingPoint (newPosition); peer != nullptr
&& peer == findPeerContainingPoint (oldPosition))
{
peer->handleMouseEvent (MouseInputSource::InputSourceType::mouse,
peer->globalToLocal (newPosition),
ModifierKeys::currentModifiers,
0.0f,
0.0f,
Time::currentTimeMillis());
}
}
double Desktop::getDefaultMasterScale()