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

Viewport: Stop ongoing physical drag upon user interaction

An animated drag operation will now stop if the user interacts with
the content area again before the animation is finished. It is also
stopped if the user interacts with the scrollbars.
This commit is contained in:
attila 2022-06-29 14:05:44 +02:00 committed by Attila Szarvas
parent c0350c54ab
commit 27924e4996
2 changed files with 24 additions and 5 deletions

View file

@ -61,6 +61,12 @@ struct Viewport::DragToScrollListener : private MouseListener,
Desktop::getInstance().removeGlobalMouseListener (this);
}
void stopOngoingAnimation()
{
offsetX.setPosition (offsetX.getPosition());
offsetY.setPosition (offsetY.getPosition());
}
void positionChanged (ViewportDragPosition&, double) override
{
viewport.setViewPosition (originalViewPos - Point<int> ((int) offsetX.getPosition(),
@ -119,9 +125,11 @@ struct Viewport::DragToScrollListener : private MouseListener,
void endDragAndClearGlobalMouseListener()
{
offsetX.endDrag();
offsetY.endDrag();
isDragging = false;
if (std::exchange (isDragging, false) == true)
{
offsetX.endDrag();
offsetY.endDrag();
}
viewport.contentHolder.addMouseListener (this, true);
Desktop::getInstance().removeGlobalMouseListener (this);
@ -229,6 +237,8 @@ void Viewport::recreateScrollbars()
getVerticalScrollBar().addListener (this);
getHorizontalScrollBar().addListener (this);
getVerticalScrollBar().addMouseListener (this, true);
getHorizontalScrollBar().addMouseListener (this, true);
resized();
}
@ -531,8 +541,15 @@ void Viewport::scrollBarMoved (ScrollBar* scrollBarThatHasMoved, double newRange
void Viewport::mouseWheelMove (const MouseEvent& e, const MouseWheelDetails& wheel)
{
if (! useMouseWheelMoveIfNeeded (e, wheel))
Component::mouseWheelMove (e, wheel);
if (e.eventComponent == this)
if (! useMouseWheelMoveIfNeeded (e, wheel))
Component::mouseWheelMove (e, wheel);
}
void Viewport::mouseDown (const MouseEvent& e)
{
if (e.eventComponent == horizontalScrollBar.get() || e.eventComponent == verticalScrollBar.get())
dragToScrollListener->stopOngoingAnimation();
}
static int rescaleMouseWheelDistance (float distance, int singleStepSize) noexcept

View file

@ -318,6 +318,8 @@ public:
/** @internal */
void mouseWheelMove (const MouseEvent&, const MouseWheelDetails&) override;
/** @internal */
void mouseDown (const MouseEvent& e) override;
/** @internal */
bool keyPressed (const KeyPress&) override;
/** @internal */
void componentMovedOrResized (Component&, bool wasMoved, bool wasResized) override;