diff --git a/frame/layershell/dlayershellwindow.cpp b/frame/layershell/dlayershellwindow.cpp index 934e91133..d1c5a9b49 100644 --- a/frame/layershell/dlayershellwindow.cpp +++ b/frame/layershell/dlayershellwindow.cpp @@ -30,6 +30,8 @@ class DLayerShellWindowPrivate } QWindow* parentWindow; + // 绑定(锁存)的 output。为空表示尚未显式绑定,此时沿用 QWindow::screen()。 + QPointer screen; QString scope; DLayerShellWindow::Anchors anchors = {DLayerShellWindow::AnchorNone}; int32_t exclusionZone = 0; @@ -157,6 +159,31 @@ void DLayerShellWindow::setScreenConfiguration(DLayerShellWindow::ScreenConfigur } } +void DLayerShellWindow::setScreen(QScreen *screen) +{ + if (d->screen == screen && (!d->parentWindow || d->parentWindow->screen() == screen)) { + return; + } + + d->screen = screen; + + // Wayland 下由 QWindow::screen() 决定 zwlr_layer_surface 绑定的 output, + // X11 下模拟层会读取 d->screen 锁存,两边都同步一次。 + if (d->parentWindow && screen && d->parentWindow->screen() != screen) { + d->parentWindow->setScreen(screen); + } + + Q_EMIT screenChanged(screen); +} + +QScreen *DLayerShellWindow::screen() const +{ + if (d->screen) { + return d->screen.data(); + } + return d->parentWindow ? d->parentWindow->screen() : nullptr; +} + void DLayerShellWindow::setPreferredWidth(int width) { if (width != d->preferredWidth) { diff --git a/frame/layershell/dlayershellwindow.h b/frame/layershell/dlayershellwindow.h index 578e94ed2..6ffc37145 100644 --- a/frame/layershell/dlayershellwindow.h +++ b/frame/layershell/dlayershellwindow.h @@ -114,6 +114,20 @@ class DS_SHARE DLayerShellWindow : public QObject void setScreenConfiguration(ScreenConfiguration screenConfiguration); ScreenConfiguration screenConfiguration() const; + /** + * Binds the layer surface to @p screen. + * + * On Wayland this simply forwards to QWindow::setScreen() before the + * zwlr_layer_surface is created. On X11 the emulation *latches* the screen: + * the surface keeps belonging to it and its geometry is always computed + * from that screen, no matter where the window is currently placed. + * X11 clients that need to move an existing surface to another screen must + * call this, a plain QWindow::setScreen() is no longer enough once the + * emulation has been created. + */ + Q_INVOKABLE void setScreen(QScreen *screen); + QScreen *screen() const; + void setPreferredWidth(int width); void resetPreferredWidth(); int preferredWidth() const; @@ -164,6 +178,7 @@ class DS_SHARE DLayerShellWindow : public QObject void scopeChanged(); void geometryHintsChanged(); void inputRegionChanged(); + void screenChanged(QScreen *screen); private: DLayerShellWindow(QWindow* window); diff --git a/frame/layershell/x11dlayershellemulation.cpp b/frame/layershell/x11dlayershellemulation.cpp index 08d6f76ba..dbe8a61cd 100644 --- a/frame/layershell/x11dlayershellemulation.cpp +++ b/frame/layershell/x11dlayershellemulation.cpp @@ -27,7 +27,14 @@ LayerShellEmulation::LayerShellEmulation(QWindow* window, QObject *parent) : QObject(parent) , m_window(window) , m_dlayerShellWindow(DLayerShellWindow::get(m_window)) + , m_screen(m_dlayerShellWindow->screen()) { + // 创建时锁存 output。客户端(如桌面)在创建 layer surface 之前已经 + // QWindow::setScreen(),此处取到的就是它声明的屏。此后 QWindow::screen() + // 会随窗口几何被 Qt 自动推导,导致"窗口压在哪块屏上就属于哪块屏"的 + // 正反馈,所以不能再拿它当归属依据。 + qCInfo(layershell) << "layer surface bound to screen:" << (m_screen ? m_screen->name() : QStringLiteral("")) + << "winId:" << m_window->winId(); onLayerChanged(); connect(m_dlayerShellWindow, &DLayerShellWindow::layerChanged, this, &LayerShellEmulation::onLayerChanged); @@ -67,6 +74,36 @@ LayerShellEmulation::LayerShellEmulation(QWindow* window, QObject *parent) m_exclusionZoneChangedTimer.start(); }); + // 只有显式重绑(DLayerShellWindow::setScreen)才允许改变归属屏, + // 客户端靠它把已有 surface 搬到另一块屏(如 dock 切换主屏)。 + connect(m_dlayerShellWindow, &DLayerShellWindow::screenChanged, this, [this](QScreen *newScreen) { + m_screen = newScreen ? newScreen : m_window->screen(); + qCInfo(layershell) << "layer surface rebound to screen:" << (m_screen ? m_screen->name() : QStringLiteral("")); + onPositionChanged(); + m_exclusionZoneChangedTimer.start(); + }); + + // 锁存的 output 被拔掉时退回窗口当前所在屏,避免用空指针算几何。 + connect(qApp, &QGuiApplication::screenRemoved, this, [this](QScreen *removed) { + if (!m_screen || m_screen == removed) { + // screenRemoved can be emitted before Qt has moved the window off + // the removed screen. Select only from the remaining outputs. + const auto screens = qApp->screens(); + QScreen *fallback = nullptr; + const auto primaryScreen = qApp->primaryScreen(); + for (auto *screen : screens) { + if (screen == removed) + continue; + if (!fallback || screen == primaryScreen) + fallback = screen; + if (screen == primaryScreen) + break; + } + m_screen = fallback; + onPositionChanged(); + } + }); + connect(qApp, &QGuiApplication::screenRemoved, &m_exclusionZoneChangedTimer, static_cast(&QTimer::start)); onScopeChanged(); @@ -118,10 +155,24 @@ void LayerShellEmulation::onLayerChanged() } } +QScreen *LayerShellEmulation::targetScreen() const +{ + if (m_screen && qApp->screens().contains(m_screen.data())) { + return m_screen.data(); + } + // 尚未锁存或锁存的 output 已消失:仅使用仍然有效的窗口屏幕。 + auto *windowScreen = m_window ? m_window->screen() : nullptr; + return windowScreen && qApp->screens().contains(windowScreen) ? windowScreen : nullptr; +} + void LayerShellEmulation::onPositionChanged() { auto anchors = m_dlayerShellWindow->anchors(); - auto screen = m_window->screen(); + // 注意:这里必须用锁存的 output,不能用 m_window->screen()。 + // QWindow::screen() 是 Qt 依据窗口几何推导出来的,排列变化后窗口一旦 + // 落进相邻屏就会被判成"属于那块屏",再把几何铺满那块屏,形成 + // "错位 → 判成邻屏 → 更错位" 的正反馈,副屏桌面会永久跑到主屏上。 + auto screen = targetScreen(); if (!screen) { return; } @@ -189,7 +240,10 @@ void LayerShellEmulation::onExclusionZoneChanged() xcb_ewmh_wm_strut_partial_t strut_partial; memset(&strut_partial, 0, sizeof(xcb_ewmh_wm_strut_partial_t)); auto anchors = m_dlayerShellWindow->anchors(); - QScreen *currentScreen = m_window->screen(); + QScreen *currentScreen = targetScreen(); + if (!currentScreen) { + return; + } if ((anchors == DLayerShellWindow::AnchorLeft) || (anchors ^ DLayerShellWindow::AnchorLeft) == (DLayerShellWindow::AnchorTop | DLayerShellWindow::AnchorBottom)) { // 计算独占区域:屏幕X坐标 + 任务栏物理宽度 // 注意:QScreen::geometry().x() 已经是设备无关像素,不需要缩放 diff --git a/frame/layershell/x11dlayershellemulation.h b/frame/layershell/x11dlayershellemulation.h index 5814f7b8b..f0b392de5 100644 --- a/frame/layershell/x11dlayershellemulation.h +++ b/frame/layershell/x11dlayershellemulation.h @@ -8,6 +8,8 @@ #include "dlayershellwindow.h" #include +#include +#include #include #include @@ -32,8 +34,12 @@ private slots: // void onKeyboardInteractivityChanged(); private: + // 该 surface 所属的 output。创建时锁存,之后不再随窗口当前所在屏漂移。 + QScreen *targetScreen() const; + QWindow* m_window; DLayerShellWindow* m_dlayerShellWindow; + QPointer m_screen; QTimer m_exclusionZoneChangedTimer; }; DS_END_NAMESPACE diff --git a/panels/dock/dockpanel.cpp b/panels/dock/dockpanel.cpp index b790011c9..a5fc4f965 100644 --- a/panels/dock/dockpanel.cpp +++ b/panels/dock/dockpanel.cpp @@ -24,6 +24,8 @@ #include #include +#include "layershell/dlayershellwindow.h" + #include #include @@ -109,7 +111,7 @@ bool DockPanel::init() if (m_dockScreen != window()->screen() && qApp->screens().contains( m_dockScreen)) { qWarning() << "m_dockScreen" << m_dockScreen << m_dockScreen->name() << "window()->screen()" << window()->screen() << window()->screen()->name(); QTimer::singleShot(10, this, [this](){ - window()->setScreen(m_dockScreen); + DS_NAMESPACE::DLayerShellWindow::get(window())->setScreen(m_dockScreen); onWindowGeometryChanged(); }); } else { @@ -531,7 +533,8 @@ void DockPanel::setDockScreen(QScreen *screen) if (m_dockScreen == screen) return; m_dockScreen = screen; - window()->setScreen(m_dockScreen); + if (window()) + DS_NAMESPACE::DLayerShellWindow::get(window())->setScreen(m_dockScreen); Q_EMIT dockScreenChanged(m_dockScreen); Q_EMIT screenNameChanged(); } diff --git a/panels/notification/center/package/main.qml b/panels/notification/center/package/main.qml index 0ee598a90..939ef6de6 100644 --- a/panels/notification/center/package/main.qml +++ b/panels/notification/center/package/main.qml @@ -119,6 +119,7 @@ Window { onScreenChanged: { // 修复:屏幕变化时重新获取dock所在的屏幕 root.screen = Qt.binding(function () { return getDockScreen() }) + root.DLayerShellWindow.setScreen(root.screen) } onVisibleChanged: function (v) { @@ -165,6 +166,7 @@ Window { // 修复:监听dock屏幕变化,同步更新通知中心屏幕 function onScreenNameChanged() { root.screen = getDockScreen() + root.DLayerShellWindow.setScreen(root.screen) } }