diff options
author | Andreas Kling <awesomekling@gmail.com> | 2019-12-08 16:50:23 +0100 |
---|---|---|
committer | Andreas Kling <awesomekling@gmail.com> | 2019-12-08 16:50:23 +0100 |
commit | a7f414bba7f4fa306af30cdf52df6df85278ed62 (patch) | |
tree | 0a7e483b275e01421ca2945a6276c5f95ea5ba12 /Libraries/LibGUI/GDragOperation.cpp | |
parent | e09a02ad3f95c9622be803d4074b85a55b91c2fa (diff) | |
download | serenity-a7f414bba7f4fa306af30cdf52df6df85278ed62.zip |
LibGUI+WindowServer: Start fleshing out drag&drop functionality
This patch enables basic drag&drop between applications.
You initiate a drag by creating a GDragOperation object and calling
exec() on it. This creates a nested event loop in the calling program
that only returns once the drag operation has ended.
On the receiving side, you get a call to GWidget::drop_event() with
a GDropEvent containing information about the dropped data.
The only data passed right now is a piece of text that's also used
to visually indicate that a drag is happening (by showing the text in
a little box that follows the mouse cursor around.)
There are things to fix here, but we're off to a nice start. :^)
Diffstat (limited to 'Libraries/LibGUI/GDragOperation.cpp')
-rw-r--r-- | Libraries/LibGUI/GDragOperation.cpp | 53 |
1 files changed, 53 insertions, 0 deletions
diff --git a/Libraries/LibGUI/GDragOperation.cpp b/Libraries/LibGUI/GDragOperation.cpp new file mode 100644 index 0000000000..b88b863f84 --- /dev/null +++ b/Libraries/LibGUI/GDragOperation.cpp @@ -0,0 +1,53 @@ +#include <LibGUI/GDragOperation.h> +#include <LibGUI/GWindowServerConnection.h> + +static GDragOperation* s_current_drag_operation; + +GDragOperation::GDragOperation(CObject* parent) + : CObject(parent) +{ +} + +GDragOperation::~GDragOperation() +{ +} + +GDragOperation::Outcome GDragOperation::exec() +{ + ASSERT(!s_current_drag_operation); + ASSERT(!m_event_loop); + + auto response = GWindowServerConnection::the().send_sync<WindowServer::StartDrag>(m_text, -1, Size()); + if (!response->started()) { + m_outcome = Outcome::Cancelled; + return m_outcome; + } + + s_current_drag_operation = this; + m_event_loop = make<CEventLoop>(); + auto result = m_event_loop->exec(); + m_event_loop = nullptr; + dbgprintf("%s: event loop returned with result %d\n", class_name(), result); + remove_from_parent(); + s_current_drag_operation = nullptr; + return m_outcome; +} + +void GDragOperation::done(Outcome outcome) +{ + ASSERT(m_outcome == Outcome::None); + m_outcome = outcome; + m_event_loop->quit(0); +} + +void GDragOperation::notify_accepted(Badge<GWindowServerConnection>) +{ + ASSERT(s_current_drag_operation); + s_current_drag_operation->done(Outcome::Accepted); +} + +void GDragOperation::notify_cancelled(Badge<GWindowServerConnection>) +{ + ASSERT(s_current_drag_operation); + s_current_drag_operation->done(Outcome::Cancelled); +} |