2 ==============================================================================
4 This file is part of the JUCE library.
5 Copyright (c) 2022 - Raw Material Software Limited
7 JUCE is an open source library subject to commercial or open-source
10 By using JUCE, you agree to the terms of both the JUCE 7 End-User License
11 Agreement and JUCE Privacy Policy.
13 End User License Agreement: www.juce.com/juce-7-licence
14 Privacy Policy: www.juce.com/juce-privacy-policy
16 Or: You may also use this code under the terms of the GPL v3 (see
17 www.gnu.org/licenses).
19 JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
20 EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
23 ==============================================================================
29 ComponentDragger::ComponentDragger() {}
30 ComponentDragger::~ComponentDragger() {}
32 //==============================================================================
33 void ComponentDragger::startDraggingComponent (Component
* const componentToDrag
, const MouseEvent
& e
)
35 jassert (componentToDrag
!= nullptr);
36 jassert (e
.mods
.isAnyMouseButtonDown()); // The event has to be a drag event!
38 if (componentToDrag
!= nullptr)
39 mouseDownWithinTarget
= e
.getEventRelativeTo (componentToDrag
).getMouseDownPosition();
42 void ComponentDragger::dragComponent (Component
* const componentToDrag
, const MouseEvent
& e
,
43 ComponentBoundsConstrainer
* const constrainer
)
45 jassert (componentToDrag
!= nullptr);
46 jassert (e
.mods
.isAnyMouseButtonDown()); // The event has to be a drag event!
48 if (componentToDrag
!= nullptr)
50 auto bounds
= componentToDrag
->getBounds();
52 // If the component is a window, multiple mouse events can get queued while it's in the same position,
53 // so their coordinates become wrong after the first one moves the window, so in that case, we'll use
54 // the current mouse position instead of the one that the event contains...
55 if (componentToDrag
->isOnDesktop())
56 bounds
+= componentToDrag
->getLocalPoint (nullptr, e
.source
.getScreenPosition()).roundToInt() - mouseDownWithinTarget
;
58 bounds
+= e
.getEventRelativeTo (componentToDrag
).getPosition() - mouseDownWithinTarget
;
60 if (constrainer
!= nullptr)
61 constrainer
->setBoundsForComponent (componentToDrag
, bounds
, false, false, false, false);
63 componentToDrag
->setBounds (bounds
);