Krita is a free and open source cross-platform application that offers an end-to-end solution for creating digital art files from scratch built on the KDE and Qt frameworks.
C++
Fix positioning of the Docker Box popup
Dmitry Kazakove244fed75f0e
diff --git a/libs/ui/kis_control_frame.cpp b/libs/ui/kis_control_frame.cppindex 783f5f1909..96978ea5c7 100644--- a/libs/ui/kis_control_frame.cpp+++ b/libs/ui/kis_control_frame.cpp@@ -44,6 +44,7 @@#include "widgets/kis_iconwidget.h"#include "widgets/gradient/KisGradientChooser.h"+#include "KisMainWindow.h"#include "KisViewManager.h"#include "kis_config.h"#include "kis_paintop_box.h"@@ -358,6 +359,14 @@ void KisControlFrame::createDockerBox(KisViewManager * view)m_dockerPopupButton->setArrowVisible(false);m_dockerPopup = new KisDockerHud(i18n("Toolbar Docker Box"), "toolbar");++ /**+ * Explicitly set the parent of the docker box popup to make sure it+ * can be positioned even when the button is hidden and the popup is+ * requested via a shortcut.+ */+ m_dockerPopup->setParent(m_viewManager->mainWindow());+// Set a reasonable minimum size so that dockers capable of being tiny are not,// but are not too big eitherm_dockerPopup->setMinimumHeight(300);diff --git a/libs/ui/kis_paintop_box.cc b/libs/ui/kis_paintop_box.ccindex a9148c4eab..386197e05c 100644--- a/libs/ui/kis_paintop_box.cc+++ b/libs/ui/kis_paintop_box.cc@@ -773,11 +773,7 @@ void KisPaintopBox::slotUpdateOptionsWidgetPopup()void KisPaintopBox::togglePresetEditor(){- if (m_brushEditorPopupButton->isPopupWidgetVisible()) {- m_brushEditorPopupButton->hidePopupWidget();- } else {- m_brushEditorPopupButton->showPopupWidget();- }+ m_brushEditorPopupButton->showPopupWidget();}KisPaintOpPresetSP KisPaintopBox::defaultPreset(const KoID& paintOp)diff --git a/libs/widgetutils/CMakeLists.txt b/libs/widgetutils/CMakeLists.txtindex 6d44ab7f69..4edcd490c4 100644--- a/libs/widgetutils/CMakeLists.txt+++ b/libs/widgetutils/CMakeLists.txt@@ -177,6 +177,7 @@ if (QT_MAJOR_VERSION STREQUAL "6")if (HAVE_WAYLAND)set_source_files_properties(KoItemToolTip.cpp+ KisPopupButton.cppPROPERTIESINCLUDE_DIRECTORIES"${Qt6WaylandGlobalPrivate_INCLUDE_DIRS};${Qt6WaylandClient_PRIVATE_INCLUDE_DIRS};${Qt6Gui_PRIVATE_INCLUDE_DIRS}"diff --git a/libs/widgetutils/KisPopupButton.cpp b/libs/widgetutils/KisPopupButton.cppindex f653607da7..36bb40378f 100644--- a/libs/widgetutils/KisPopupButton.cpp+++ b/libs/widgetutils/KisPopupButton.cpp@@ -21,6 +21,17 @@#include "kis_global.h"#include <kis_debug.h>+// for HAVE_WAYLAND+#include <KoConfig.h>++#if defined HAVE_WAYLAND && QT_VERSION >= QT_VERSION_CHECK(6, 11, 0)+#define HAVE_QT_PRIVATE_TOOLTIP_POSITION_API+#endif++#ifdef HAVE_QT_PRIVATE_TOOLTIP_POSITION_API+#include <QtWaylandClient/private/qwaylandwindow_p.h>+#endif+class KisPopupButtonFrame : public QFrame{@@ -126,10 +137,6 @@ void KisPopupButton::setPopupWidgetDetached(bool detach)if (wasVisible) {// Setting the window flags closes the widget, so make it visible again.setPopupWidgetVisible(true);- if (detach) {- m_d->isDetachedGeometrySet = true;- }- adjustPosition();}}}@@ -139,10 +146,12 @@ void KisPopupButton::setPopupWidget(QWidget* widget)if (widget) {delete m_d->frame;+ QWidget *effectiveParent = widget->parentWidget() ? widget->parentWidget() : this->window();+// Bit wonky to assign a popup menu to a popup widget, so they need// extra coddling to make them work properly.QMenu *menu = qobject_cast<QMenu *>(widget);- m_d->frame = new KisPopupButtonFrame(this->window(), m_d->isPopupDetached, menu);+ m_d->frame = new KisPopupButtonFrame(effectiveParent, m_d->isPopupDetached, menu);m_d->frame->setWindowTitle(widget->windowTitle());m_d->popupWidget = widget;@@ -160,21 +169,9 @@ void KisPopupButton::setPopupWidget(QWidget* widget)}}-void KisPopupButton::setPopupWidgetWidth(int w)-{- m_d->frame->resize(w, m_d->frame->height());-}-void KisPopupButton::showPopupWidget(){- if (m_d->popupWidget && !m_d->frame->isVisible()) {- setPopupWidgetVisible(true);- if (!m_d->isPopupDetached) {- adjustPosition();- }- } else {- hidePopupWidget();- }+ setPopupWidgetVisible(!m_d->frame->isVisible());}void KisPopupButton::hidePopupWidget()@@ -186,15 +183,7 @@ void KisPopupButton::setPopupWidgetVisible(bool visible){if (m_d->popupWidget) {if (visible) {- if (m_d->isPopupDetached) {- // Force layout sizing before positioning- m_d->popupWidget->adjustSize();- m_d->frame->adjustSize();- adjustPosition();- }- m_d->frame->raise();- m_d->frame->show();- m_d->frame->activateWindow();+ adjustPositionAndShow();Q_EMIT sigPopupWidgetShown();} else {m_d->frame->setVisible(false);@@ -228,55 +217,79 @@ void KisPopupButton::paintPopupArrow()p.drawPrimitive(QStyle::PE_IndicatorArrowDown, option);}-void KisPopupButton::adjustPosition()+void KisPopupButton::adjustPositionAndShow(){- QScreen *currentScreen = nullptr;- auto getCurrentScreen = [this, ¤tScreen] {- if (!currentScreen) {- QWindow *mainWinHandle = window()->windowHandle();- if (mainWinHandle) {- currentScreen = mainWinHandle->screen();+ const QSize popupSize = m_d->frame->sizeHint();+ QRect popupRect(QPoint(), popupSize);++ // resize **must** come before `setParentControlGeometry()`, otherwise+ // compositor will place the window incorrectly!+ m_d->frame->resize(popupSize);++ // make sure that all the platform-specific structures for the window+ // are created (i.e. QWaylandWindow) before trying to request them+ // via `windowHandle()->handle()`+ m_d->frame->winId();++#ifdef HAVE_QT_PRIVATE_TOOLTIP_POSITION_API+ if (auto waylandWindow = dynamic_cast<QNativeInterface::Private::QWaylandWindow *>(m_d->frame->windowHandle()->handle())) {+ if (m_d->isPopupDetached) {+ // one cannot control dialog position on Wayland, sorry :(+ } else {+ if (this->isVisible()) {+ QWidget *parentWidget = m_d->frame->parentWidget();++ const QRect gravityRect(parentWidget->window()->mapFromGlobal(mapToGlobal(QPoint())), size());++ waylandWindow->setParentControlGeometry(gravityRect);+ waylandWindow->setExtendedWindowType(QNativeInterface::Private::QWaylandWindow::ComboBox);} else {- currentScreen = QApplication::primaryScreen();+ QWidget *parentWidget = m_d->frame->parentWidget();+ const QPoint parentPos = parentWidget->window()->mapFromGlobal(QCursor::pos());+ const QRect gravityRect(parentPos.x() - popupSize.width() / 2, parentPos.y() - popupSize.height() / 2, popupSize.width(), popupSize.height());++ waylandWindow->setParentControlGeometry(gravityRect);+ waylandWindow->setExtendedWindowType(QNativeInterface::Private::QWaylandWindow::Default);}}- return currentScreen;- };-- // If popup is not detached, or if its detached geometry hasn't been set,- // we first move the popup to the "current" screen.- if (!m_d->isPopupDetached || !m_d->isDetachedGeometrySet) {- QWindow *winHandle = m_d->frame->windowHandle();- if (winHandle) {- winHandle->setScreen(getCurrentScreen());- }- }- // Attach to the button if it's visible, else attach to the cursor.- QPoint pos = this->isVisible() ? this->mapToGlobal(QPoint(0, this->size().height())) : QCursor().pos();- QSize popSize = m_d->popupWidget->size();- QRect popupRect(pos, popSize);+ m_d->frame->show();+ } else+#endif /* HAVE_QT_PRIVATE_TOOLTIP_POSITION_API */- // Get the available geometry of the screen which contains the popup.- QScreen *screen = [this, &getCurrentScreen]() {- QWindow *winHandle = m_d->frame->windowHandle();- if (winHandle && winHandle->screen()) {- return winHandle->screen();- }- return getCurrentScreen();- }();- QRect screenRect = screen->availableGeometry();- if (m_d->isPopupDetached) {- if (m_d->isDetachedGeometrySet) {- popupRect.moveTo(m_d->frame->geometry().topLeft());+ {+ // Get the available geometry of the screen which contains the popup.+ QScreen *screen = this->parentWidget() ? this->screen() : qApp->screenAt(QCursor::pos());+ const QRect screenRect = screen ? screen->availableGeometry() : QRect();++ if (m_d->isPopupDetached) {++ if (m_d->isDetachedGeometrySet) {+ popupRect.moveTo(m_d->frame->geometry().topLeft());+ } else {+ popupRect.moveTo(this->window()->geometry().center() - QRect(QPoint(0, 0), popupSize).center());+ m_d->isDetachedGeometrySet = true;+ }} else {- popupRect.moveTo(this->window()->geometry().center() - QRect(QPoint(0, 0), popSize).center());- m_d->isDetachedGeometrySet = true;+ // Attach to the button if it's visible, else attach to the cursor.+ if (this->isVisible()) {+ popupRect.moveTo(this->mapToGlobal(rect().bottomLeft()));+ } else {+ popupRect.moveCenter(QCursor::pos());+ }+ }++ if (!screenRect.isEmpty()) {+ popupRect = kisEnsureInRect(popupRect, screenRect);}- }- popupRect = kisEnsureInRect(popupRect, screenRect);- m_d->frame->setGeometry(popupRect);+ /**+ * On non-wayland platforms we first show the widget and only then+ * apply geometry, otherwise the settings are not applied properly+ */+ m_d->frame->show();+ m_d->frame->setGeometry(popupRect);+ }}void KisPopupButton::setArrowVisible (bool v)diff --git a/libs/widgetutils/KisPopupButton.h b/libs/widgetutils/KisPopupButton.hindex 8bd4860250..94b741f87f 100644--- a/libs/widgetutils/KisPopupButton.h+++ b/libs/widgetutils/KisPopupButton.h@@ -28,21 +28,21 @@ public:/*** Set the popup widget, the KisPopupButton becomes* the owner and parent of the widget.+ *+ * If a widget does already have a parent, then this parent+ * becomes a parent of the internal popup frame. That might+ * be useful for popup buttons attaches to actions, which may+ * be not visible or attached to anything, e.g the "Docker Box"+ * action.*/void setPopupWidget(QWidget* widget);- /**- * Set the width of the popup widget.- * @return new width of the popup widget- */- void setPopupWidgetWidth(int w);-/*** @brief adjustPosition* adjusts the position of the popup widget based on the position* of this button and the size of the widget*/- void adjustPosition();+ void adjustPositionAndShow();/*** This function allows to show or hide the arrow.