Skip to content
Merged
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
86 changes: 83 additions & 3 deletions src/tray-wayland-integration/pluginsurface.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// SPDX-FileCopyrightText: 2023 UnionTech Software Technology Co., Ltd.
// SPDX-FileCopyrightText: 2023 - 2026 UnionTech Software Technology Co., Ltd.
//
// SPDX-License-Identifier: GPL-3.0-or-later

Expand All @@ -6,12 +6,62 @@
#include "pluginsurface_p.h"
#include "pluginmanagerintegration_p.h"

#include "qwayland-plugin-manager-v1.h"

Check warning on line 9 in src/tray-wayland-integration/pluginsurface.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: "qwayland-plugin-manager-v1.h" not found.

#include <QTimer>

Check warning on line 11 in src/tray-wayland-integration/pluginsurface.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <QTimer> not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <QApplication>

Check warning on line 12 in src/tray-wayland-integration/pluginsurface.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <QApplication> not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <QWidget>

Check warning on line 13 in src/tray-wayland-integration/pluginsurface.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <QWidget> not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <QEvent>

Check warning on line 14 in src/tray-wayland-integration/pluginsurface.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <QEvent> not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <QtWaylandClient/private/qwaylandwindow_p.h>

Check warning on line 15 in src/tray-wayland-integration/pluginsurface.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <QtWaylandClient/private/qwaylandwindow_p.h> not found. Please note: Cppcheck does not need standard library headers to get proper results.

namespace Plugin {

class NullHandleGuard : public QObject {
Comment thread
yixinshark marked this conversation as resolved.
public:
static void install() {
static bool installed = false;
if (!installed && qApp) {
qApp->installEventFilter(new NullHandleGuard(qApp));
installed = true;
}
}

NullHandleGuard(QObject *parent = nullptr) : QObject(parent) {}

Check warning on line 29 in src/tray-wayland-integration/pluginsurface.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Class 'NullHandleGuard' has a constructor with 1 argument that is not explicit. Such, so called "Converting constructors", should in general be explicit for type safety reasons as that prevents unintended implicit conversions.

protected:
bool eventFilter(QObject *watched, QEvent *event) override {
if (event->type() == QEvent::UpdateRequest) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

每个widget的每一帧都要判断么?

QWindow *win = nullptr;
QWidget *w = qobject_cast<QWidget *>(watched);
QWindow *wnd = qobject_cast<QWindow *>(watched);

if (w) {
win = w->windowHandle();
if (!win && w->window()) {
win = w->window()->windowHandle();
}
} else if (wnd) {
win = wnd;
}

// Drop UpdateRequest to prevent QWaylandShmBackingStore from crashing
// in beginPaint/decoration() when trying to paint a destroyed window.
if (win) {
// Case 1: The QWindow exists, but its underlying Wayland surface is destroyed.
if (!win->handle()) {
qDebug() << "NullHandleGuard Dropped UpdateRequest for" << watched << "(null Wayland handle)";
return true;
}
} else if (w) {
// Case 2: The QWidget has lost its QWindow entirely.
// A widget without a QWindow cannot be painted to the screen.
qDebug() << "NullHandleGuard Dropped UpdateRequest for" << watched << "(missing QWindow)";
return true;
}
}
return QObject::eventFilter(watched, event);
}
};
PluginSurface::PluginSurface(PluginManagerIntegration *manager, QtWaylandClient::QWaylandWindow *window)
: QtWaylandClient::QWaylandShellSurface(window)
, QtWayland::plugin()
Expand Down Expand Up @@ -130,13 +180,43 @@
destroy();
}

void PluginPopupSurface::plugin_popup_close()

Check warning on line 183 in src/tray-wayland-integration/pluginsurface.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

The function 'plugin_popup_close' is never used.
{
// it would be delete this object directly.
m_window->close();
// Install the global safeguard just in case
NullHandleGuard::install();

// Use QPointer to ensure m_window is still valid when the queued lambda executes
QPointer<QWindow> safeWindow(m_window);

// DEFER the destruction!
// Why: QWaylandShmBackingStore::beginPaint() can spin the Wayland event loop
// (e.g., waiting for buffers) which synchronously dispatches this Wayland event.
// If we destroy m_window here, beginPaint() resumes with a dangling pointer
// and crashes immediately. Deferring to the next event loop iteration ensures
// that the current paint frame completes before we destroy the Wayland surface.
QMetaObject::invokeMethod(qApp, [safeWindow]() {
if (!safeWindow) {
return;
}

QWidget *popupWidget = nullptr;
for (QWidget *w : QApplication::topLevelWidgets()) {
if (w && w->windowHandle() == safeWindow.data()) {

Check warning on line 204 in src/tray-wayland-integration/pluginsurface.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Consider using std::find_if algorithm instead of a raw loop.
popupWidget = w;
break;
}
}

if (popupWidget) {
popupWidget->hide();
}

// Safely close the QWindow. This destroys the Wayland surface.
safeWindow->close();
}, Qt::QueuedConnection);
}

void PluginPopupSurface::plugin_popup_geometry(int32_t x, int32_t y, int32_t width, int32_t height)

Check warning on line 219 in src/tray-wayland-integration/pluginsurface.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

The function 'plugin_popup_geometry' is never used.
{
auto plugin = PluginPopup::get(m_window);
auto rect = QRect(x, y, width, height);
Expand Down
Loading