Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,9 @@ option(KDDockWidgets_PYTHON_BINDINGS "Build python bindings" OFF)
option(KDDockWidgets_STATIC "Build statically" OFF)
option(KDDockWidgets_TESTS "Build the tests" OFF)
option(KDDockWidgets_WAYLAND_TESTS "Build the wayland tests" OFF)
option(KDDockWidgets_WAYLAND_TOPLEVEL_DRAG
"Support Config::Flag_WaylandToplevelDrag on Linux. Uses Qt's private Wayland client API" ON
)
option(KDDockWidgets_EXAMPLES "Build the examples" ON)
option(KDDockWidgets_DOCS "Build the API documentation" OFF)
option(KDDockWidgets_WERROR "Use -Werror (will be true for developer-mode unconditionally)"
Expand Down Expand Up @@ -274,6 +277,34 @@ if(KDDW_FRONTEND_QTWIDGETS OR KDDW_FRONTEND_QTQUICK)
set(KDDW_FRONTEND_QT ON)
endif()

# xdg-toplevel-drag, see Config::Flag_WaylandToplevelDrag. Qt's Wayland client supports it since 6.8, and
# its private API tells whether the compositor does.
set(KDDW_HAS_WAYLAND_TOPLEVEL_DRAG OFF)
if(KDDockWidgets_WAYLAND_TOPLEVEL_DRAG
AND KDDW_FRONTEND_QT
AND CMAKE_SYSTEM_NAME STREQUAL "Linux"
AND Qt6Core_VERSION VERSION_GREATER_EQUAL "6.8.0"
)
set(QT_NO_PRIVATE_MODULE_WARNING ON)
find_package(Qt6 ${QT_MIN_VERSION} NO_MODULE QUIET OPTIONAL_COMPONENTS WaylandClient)
if(Qt6Core_VERSION VERSION_GREATER_EQUAL "6.10.0")
find_package(Qt6 ${QT_MIN_VERSION} NO_MODULE QUIET OPTIONAL_COMPONENTS WaylandClientPrivate)
endif()

# Before Qt 6.10 the target exists even if the private headers are packaged separately and
# missing
set(_qwaylanddisplay_p_h
"${QT6_INSTALL_PREFIX}/${QT6_INSTALL_HEADERS}/QtWaylandClient/${Qt6WaylandClient_VERSION}/QtWaylandClient/private/qwaylanddisplay_p.h"
)
if(TARGET Qt6::WaylandClientPrivate AND EXISTS "${_qwaylanddisplay_p_h}")
set(KDDW_HAS_WAYLAND_TOPLEVEL_DRAG ON)
else()
message(
STATUS "Qt's private Wayland client headers not found, Config::Flag_WaylandToplevelDrag won't be supported"
)
endif()
endif()

if(KDDockWidgets_QML_MODULE AND Qt6Core_VERSION VERSION_LESS "6.8.0")
# We have workarounds for QTBUG-101146, but only since 6.8 the generated includes are guarded
# with _has_include
Expand Down
4 changes: 4 additions & 0 deletions Changelog
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
* v2.6.0
- Wayland: New experimental Config::Flag_WaylandToplevelDrag, which uses xdg-toplevel-drag to drag
the actual window instead of a pixmap. Needs Qt >= 6.8, Qt6WaylandClientPrivate at build time
and compositor support, it's ignored otherwise. See the Wayland chapter of the manual for its
limitations
- qtquick: New DockingArea.persistentCentralItemComponent, to declare the persistent central item
inline in QML instead of in a separate file
- qtquick: Fix leak when replacing DockingArea.persistentCentralItemFileName at runtime
Expand Down
28 changes: 28 additions & 0 deletions docs/book/src/qpa-wayland.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,33 @@ when running on Wayland:
- Kwin specific:
- The pixmap that's shown during a drag can't be bigger than 250x250. Might be a bug.

## Experimental: xdg-toplevel-drag

With `Config::Flag_WaylandToplevelDrag`, KDDockWidgets uses the
[xdg-toplevel-drag](https://wayland.app/protocols/xdg-toplevel-drag-v1) protocol. The window itself
moves with the pointer while dragging, instead of a pixmap, like on other platforms:

- Dragging a tab or a docked title bar detaches it into a floating window right away, which then
follows the pointer.
- Floating windows have only KDDockWidgets' title bar, which both moves them and docks them.

It needs:

- Qt >= 6.8, and Qt's private Wayland client headers when building KDDockWidgets, as Qt has no
public API to know whether the compositor supports the protocol. KDDockWidgets then links to
QtWaylandClient, even if only used on X11. Pass `-DKDDockWidgets_WAYLAND_TOPLEVEL_DRAG=OFF` to
CMake to avoid that.
- A compositor which supports xdg-toplevel-drag.

Otherwise the flag is ignored, with a warning, and the limitations above apply.

Known limitations:

- Resizing floating windows doesn't work properly. Without the native title bar they have no
native decorations, so KDDockWidgets resizes them itself, and it moves the window when resizing
from the left or top edges, which Wayland doesn't allow. The window then grows instead.
- A maximized floating window isn't restored to its normal size before being dragged, like it is
on other platforms.

All in all it's pretty decent and usable. Any further improvements should be done at the server or
protocol level now.
12 changes: 12 additions & 0 deletions examples/dockwidgets/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,13 @@ int main(int argc, char **argv)
"LayoutSaver::addWindowToSave()"));
parser.addOption(testPartialSaveRestore);

#ifdef Q_OS_LINUX
QCommandLineOption waylandToplevelDrag(
"wayland-toplevel-drag",
QCoreApplication::translate("main", "Use xdg-toplevel-drag on Wayland so the actual window drags instead of a pixmap"));
parser.addOption(waylandToplevelDrag);
#endif

#if defined(DOCKS_DEVELOPER_MODE)
parser.addOption(centralFrame);

Expand Down Expand Up @@ -377,6 +384,11 @@ int main(int argc, char **argv)
if (parser.isSet(doubleClickMaximize))
flags |= KDDockWidgets::Config::Flag_DoubleClickMaximizes;

#ifdef Q_OS_LINUX
if (parser.isSet(waylandToplevelDrag))
flags |= KDDockWidgets::Config::Flag_WaylandToplevelDrag;
#endif

if (parser.isSet(incompatibleMainWindows) && !parser.isSet(multipleMainWindows)) {
qWarning() << "Error: Argument -i requires -m";
return 1;
Expand Down
5 changes: 5 additions & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,11 @@ if(KDDW_FRONTEND_QTQUICK)
target_compile_definitions(kddockwidgets PUBLIC KDDW_FRONTEND_QTQUICK KDDW_FRONTEND_QT)
endif()

if(KDDW_HAS_WAYLAND_TOPLEVEL_DRAG)
target_link_libraries(kddockwidgets PRIVATE Qt6::WaylandClientPrivate)
target_compile_definitions(kddockwidgets PRIVATE KDDW_HAS_WAYLAND_TOPLEVEL_DRAG)
endif()

if(NOT KDDW_FRONTEND_QT)
include(GenerateExportHeader)
generate_export_header(
Expand Down
9 changes: 8 additions & 1 deletion src/Config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#include "core/Platform.h"
#include "core/View.h"
#include "core/Logging_p.h"
#include "qtcommon/DragControllerWayland_p.h"

#include <iostream>
#include <limits>
Expand Down Expand Up @@ -361,7 +362,13 @@ void Config::Private::fixFlags()
}

#if defined(Q_OS_LINUX)
if (KDDockWidgets::isWayland()) {
if (KDDockWidgets::isWayland() && (m_flags & Config::Flag_WaylandToplevelDrag)
&& !Core::isWaylandToplevelDragSupported()) {
KDDW_WARN("Config: Ignoring Flag_WaylandToplevelDrag, it needs Qt >= 6.8 and a compositor which supports xdg-toplevel-drag");
m_flags = m_flags & ~Flag_WaylandToplevelDrag;
}

if (KDDockWidgets::isWayland() && !(m_flags & Config::Flag_WaylandToplevelDrag)) {
// Native title bar is forced on Wayland. Needed for moving the window.
// The inner KDDW title bar is used for DnD.
m_flags |= Flag_NativeTitleBar;
Expand Down
1 change: 1 addition & 0 deletions src/Config.h
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ class DOCKS_EXPORT Config
///< right clicking on the tab area
Flag_AutoHideAsTabGroups = 0x100000, ///< If tabbed dockwidgets are sent to/from sidebar, they're all sent and restored together
Flag_DisableDoubleClick = 0x200000, ///< Do not maximize of float if a title or tab is double-clicked.
Flag_WaylandToplevelDrag = 0x400000, ///< EXPERIMENTAL. Uses xdg-toplevel-drag on Wayland so the actual window drags instead of a pixmap. Ignored without Qt >= 6.8 and Qt6WaylandClientPrivate at build time, or without compositor support.
Flag_Default = Flag_AeroSnapWithClientDecos ///< The defaults
};
Q_DECLARE_FLAGS(Flags, Flag)
Expand Down
8 changes: 8 additions & 0 deletions src/KDDockWidgetsConfig.cmake.in
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ if (@KDDW_FRONTEND_QTQUICK@)
find_dependency(Qt@QT_VERSION_MAJOR@QuickControls2 REQUIRED)
endif()

# Static builds export Qt6::WaylandClientPrivate, which Qt < 6.10 provides in Qt6WaylandClient
if (@KDDockWidgets_STATIC@ AND @KDDW_HAS_WAYLAND_TOPLEVEL_DRAG@)
find_dependency(Qt6WaylandClient REQUIRED)
endif()

if (@KDDW_FIND_QT_PRIVATE_DEPS@)
set(QT_NO_PRIVATE_MODULE_WARNING ON)
find_dependency(Qt6GuiPrivate REQUIRED)
Expand All @@ -25,6 +30,9 @@ if (@KDDW_FIND_QT_PRIVATE_DEPS@)
if (@KDDW_FRONTEND_QTQUICK@)
find_dependency(Qt6QuickPrivate REQUIRED)
endif()
if (@KDDW_HAS_WAYLAND_TOPLEVEL_DRAG@)
find_dependency(Qt6WaylandClientPrivate REQUIRED)
endif()
endif()

if (@KDDockWidgets_DEVELOPER_MODE@)
Expand Down
2 changes: 1 addition & 1 deletion src/core/Utils_p.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ inline bool usesNativeTitleBar()
inline bool usesClientTitleBar()
{
if (isWayland()) {
// Wayland has both client and native title bars, due to limitations.
// Wayland always has the client title-bar, since the native one doesn't emit events when moved
return true;
}

Expand Down
114 changes: 111 additions & 3 deletions src/qtcommon/DragControllerWayland_p.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,20 @@
#include "core/ScopedValueRollback_p.h"
#include "kddockwidgets/core/Platform.h"
#include "kddockwidgets/core/DropArea.h"
#include "Config.h"

#ifdef KDDW_HAS_WAYLAND_TOPLEVEL_DRAG
#include "core/Utils_p.h"
#include "kddockwidgets/core/FloatingWindow.h"
#include "qtcommon/Window_p.h"

#include <QDataStream>
#include <QWindow>

#include <QtGui/private/qguiapplication_p.h>
#include <QtWaylandClient/private/qwaylanddisplay_p.h>
#include <QtWaylandClient/private/qwaylandintegration_p.h>
#endif

using namespace KDDockWidgets::Core;

Expand All @@ -36,14 +50,33 @@ void StateDraggingWayland::onEntry()
return;
}

if (!q->m_draggableGuard) {
KDDW_ERROR("Draggable was destroyed, canceling the drag");
q->dragCanceled.emit();
return;
}

ScopedValueRollback guard(DragController::instance()->m_inQDrag, true);
q->m_windowBeingDragged =
std::unique_ptr<WindowBeingDragged>(new WindowBeingDraggedWayland(q->m_draggable));

auto mimeData = new WaylandMimeData();
Drag drag(this);
drag.setMimeData(mimeData);
drag.setPixmap(q->m_windowBeingDragged->pixmap());

#ifdef KDDW_HAS_WAYLAND_TOPLEVEL_DRAG
// Checked here and not when creating the state, as the DragController can be created before the
// flags are set. QtQuick's Platform creates it, for example.
if (Config::self().flags() & Config::Flag_WaylandToplevelDrag) {
if (!setupToplevelDrag(mimeData)) {
q->dragCanceled.emit();
return;
}
} else
#endif
{
q->m_windowBeingDragged =
std::unique_ptr<WindowBeingDragged>(new WindowBeingDraggedWayland(q->m_draggable));
drag.setPixmap(q->m_windowBeingDragged->pixmap());
}

Platform::instance()->installGlobalEventFilter(q);
KDDW_DEBUG("Started QDrag");
Expand Down Expand Up @@ -130,3 +163,78 @@ bool StateDraggingWayland::handleDragMove(DragMoveEvent *ev, DropArea *dropArea,

return true;
}

#ifdef KDDW_HAS_WAYLAND_TOPLEVEL_DRAG

bool StateDraggingWayland::setupToplevelDrag(QMimeData *mimeData)
{
// Where the pointer will be in the dragged window. Like StateDragging, when detaching into a
// new window, that's where the title bar or tab bar was pressed, so it's near the new window's
// top-left. When dragging a floating window it's where it was pressed, so it doesn't jump. That
// comes from the global position, as Draggable::mapToWindow() doesn't account for margins,
// which QtQuick's floating windows have.
Point offset = q->m_offset;
if (q->m_draggable->isWindow()) {
if (auto window = q->m_draggable->asView()->window())
offset = window->mapFromGlobal(q->m_pressPos);
}

q->m_windowBeingDragged = q->m_draggable->makeWindow();
if (!q->m_windowBeingDragged) {
KDDW_ERROR("StateDraggingWayland: Failed to create window");
return false;
}

auto fw = q->m_windowBeingDragged->floatingWindow();
if (!fw) {
KDDW_ERROR("StateDraggingWayland: No FloatingWindow");
return false;
}

auto window = fw->view()->window();
if (!window) {
KDDW_ERROR("StateDraggingWayland: No window");
return false;
}

auto qtWindow = static_cast<QtCommon::Window *>(window.get())->qtWindow();
if (!qtWindow) {
KDDW_ERROR("StateDraggingWayland: No QWindow");
return false;
}

// The draggable can be bigger than the new window. Like StateDragging::onEntry(), keep the
// pointer over it.
if (offset.x() > fw->width())
offset.setX(fw->width() / 2);
if (offset.y() > fw->height())
offset.setY(fw->height() / 2);

auto serialize = [](const auto &object) {
QByteArray data;
QDataStream dataStream(&data, QIODevice::WriteOnly);
dataStream << object;
return data;
};

mimeData->setData(QStringLiteral("application/x-qt-mainwindowdrag-window"),
serialize(reinterpret_cast<qintptr>(qtWindow))); // NOLINT(cppcoreguidelines-pro-type-reinterpret-cast)
mimeData->setData(QStringLiteral("application/x-qt-mainwindowdrag-position"), serialize(offset));

return true;
}

#endif

bool KDDockWidgets::Core::isWaylandToplevelDragSupported()
{
#ifdef KDDW_HAS_WAYLAND_TOPLEVEL_DRAG
if (!isWayland())
return false;

auto integration = static_cast<QtWaylandClient::QWaylandIntegration *>(QGuiApplicationPrivate::platformIntegration());
return integration->display()->hasRegistryGlobal(u"xdg_toplevel_drag_manager_v1");
#else
return false;
#endif
}
10 changes: 10 additions & 0 deletions src/qtcommon/DragControllerWayland_p.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,18 @@ class StateDraggingWayland : public StateDragging
bool handleDragLeave(DropArea *) override;
bool handleDrop(DropEvent *, DropArea *, QPoint globalEventPos) override;
bool handleMouseMove(QPoint globalPos) override;

private:
#ifdef KDDW_HAS_WAYLAND_TOPLEVEL_DRAG
/// Sets up the drag to use xdg-toplevel-drag, so the window itself is dragged instead of a
/// pixmap. Returns false if the window couldn't be created.
bool setupToplevelDrag(QMimeData *mimeData);
#endif
};

/// Returns whether xdg-toplevel-drag can be used. Needs Qt >= 6.8 and compositor support.
bool isWaylandToplevelDragSupported();

// A sub-class just so we don't use QMimeData directly. We'll only accept drops if its mime data
// Can be qobject_casted to this class. For safety.
class WaylandMimeData : public QMimeData
Expand Down
12 changes: 12 additions & 0 deletions tests/wayland/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,15 @@ add_dependencies(tst_wayland kddw_test_compositor)
target_compile_definitions(tst_wayland PRIVATE KDDW_TEST_COMPOSITOR="$<TARGET_FILE:kddw_test_compositor>")
add_test(NAME tst_wayland COMMAND tst_wayland)
set_tests_properties(tst_wayland PROPERTIES LABELS wayland)

if(KDDW_HAS_WAYLAND_TOPLEVEL_DRAG AND KDDW_XDG_TOPLEVEL_DRAG_XML)
target_compile_definitions(tst_wayland PRIVATE KDDW_HAS_WAYLAND_TOPLEVEL_DRAG)

# Flag_WaylandToplevelDrag with a compositor that doesn't support it, which needs to be ignored
add_test(NAME tst_wayland_no_toplevel_drag COMMAND tst_wayland)
set_tests_properties(
tst_wayland_no_toplevel_drag
PROPERTIES LABELS wayland ENVIRONMENT
"KDDW_TEST_COMPOSITOR_ARGS=--no-toplevel-drag;KDDW_TEST_DRAG_MODE=toplevel"
)
endif()
Loading
Loading