diff --git a/frame/layershell/dlayershellwindow.cpp b/frame/layershell/dlayershellwindow.cpp index ba6833eb7..7283a5bed 100644 --- a/frame/layershell/dlayershellwindow.cpp +++ b/frame/layershell/dlayershellwindow.cpp @@ -31,6 +31,8 @@ class DLayerShellWindowPrivate } QWindow* parentWindow; + // 绑定(锁存)的 output。为空表示尚未显式绑定,此时沿用 QWindow::screen()。 + QPointer screen; QString scope; DLayerShellWindow::Anchors anchors = {DLayerShellWindow::AnchorNone}; int32_t exclusionZone = 0; @@ -159,6 +161,32 @@ void DLayerShellWindow::setScreenConfiguration(DLayerShellWindow::ScreenConfigur } } +void DLayerShellWindow::setScreen(QScreen *screen) +{ + if (d->screen == screen && (!d->parentWindow || d->parentWindow->screen() == screen)) { + return; + } + + d->screen = screen; + + // 先同步背景层的锁存屏幕,避免切屏时的几何回调仍按旧屏定位。 + Q_EMIT screenChanged(screen); + + // Wayland 下由 QWindow::screen() 决定 zwlr_layer_surface 绑定的 output, + // X11 背景层读取锁存输出,其他层继续跟随 QWindow::screen()。 + if (d->parentWindow && screen && d->parentWindow->screen() != screen) { + d->parentWindow->setScreen(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 12217adbd..72d151b45 100644 --- a/frame/layershell/dlayershellwindow.h +++ b/frame/layershell/dlayershellwindow.h @@ -115,6 +115,17 @@ class DS_SHARE DLayerShellWindow : public QObject void setScreenConfiguration(ScreenConfiguration screenConfiguration); ScreenConfiguration screenConfiguration() const; + /** + * Binds the layer surface to @p screen. + * + * On Wayland this forwards to QWindow::setScreen(). On X11 only background + * surfaces latch their output to survive screen rearrangements; use this + * method to explicitly move an existing background surface. Other layers + * continue to follow QWindow::screen(), including changes made from QML. + */ + Q_INVOKABLE void setScreen(QScreen *screen); + QScreen *screen() const; + void setPreferredWidth(int width); void resetPreferredWidth(); int preferredWidth() const; @@ -168,6 +179,7 @@ class DS_SHARE DLayerShellWindow : public QObject void scopeChanged(); void geometryHintsChanged(); void inputRegionChanged(); + void screenChanged(QScreen *screen); void resizingChanged(); // Final size in QWindow coordinates, delivered before resizing becomes false. void resizeFinished(const QSize &size); diff --git a/frame/layershell/x11dlayershellemulation.cpp b/frame/layershell/x11dlayershellemulation.cpp index 6b0a8f3d6..3b2ace487 100644 --- a/frame/layershell/x11dlayershellemulation.cpp +++ b/frame/layershell/x11dlayershellemulation.cpp @@ -76,7 +76,12 @@ LayerShellEmulation::LayerShellEmulation(QWindow* window, QObject *parent) : QObject(parent) , m_window(window) , m_dlayerShellWindow(DLayerShellWindow::get(m_window)) + , m_screen(m_dlayerShellWindow->screen()) { + // 背景窗口锁存创建时的 output,避免屏幕重排时随几何漂移。 + // 其他层仍使用 QWindow::screen(),保留现有客户端主动换屏的语义。 + 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); @@ -116,6 +121,34 @@ LayerShellEmulation::LayerShellEmulation(QWindow* window, QObject *parent) m_exclusionZoneChangedTimer.start(); }); + // 显式重绑允许背景窗口迁移到另一块输出,其他层仍跟随窗口屏幕。 + 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) { + // Qt 可能尚未迁走窗口,只从仍然存在的输出中选择回退屏幕。 + 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(); @@ -173,10 +206,21 @@ void LayerShellEmulation::onLayerChanged() } } +QScreen *LayerShellEmulation::targetScreen() const +{ + if (m_dlayerShellWindow->layer() == DLayerShellWindow::LayerBackground + && m_screen && qApp->screens().contains(m_screen.data())) { + return m_screen.data(); + } + 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(); + // 只有背景层固定归属输出;面板、通知、OSD 和描边窗口允许通过 Window.screen 换屏。 + auto screen = targetScreen(); if (!screen) { return; } @@ -260,7 +304,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..f24b86c96 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: + // 背景层使用锁存输出,其他层兼容 QWindow::screen() 的动态选屏。 + 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 a409a456f..949d84a58 100644 --- a/panels/dock/dockpanel.cpp +++ b/panels/dock/dockpanel.cpp @@ -6,6 +6,7 @@ #include "constants.h" #include "dockadaptor.h" #include "docksettings.h" +#include "layershell/dlayershellwindow.h" #include "panel.h" #include "pluginfactory.h" #include "waylanddockhelper.h" @@ -112,7 +113,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::DLayerShellWindow::get(window())->setScreen(m_dockScreen); onWindowGeometryChanged(); }); } else { @@ -570,7 +571,8 @@ void DockPanel::setDockScreen(QScreen *screen) if (m_dockScreen == screen) return; m_dockScreen = screen; - window()->setScreen(m_dockScreen); + if (window()) + ds::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 55e594835..5f2de9073 100644 --- a/panels/notification/center/package/main.qml +++ b/panels/notification/center/package/main.qml @@ -113,11 +113,9 @@ Window { DWindow.enableBlurWindow: true DWindow.borderColor: DTK.themeType === ApplicationHelper.DarkType ? Qt.rgba(0, 0, 0, 0.8) : Qt.rgba(0, 0, 0, 0.06) - // 修复:通知中心屏幕跟随任务栏屏幕,而不是硬编码为第一个屏幕 + // 通知中心跟随任务栏屏幕,非背景层由框架兼容 Window.screen 的动态选屏。 screen: getDockScreen() - // TODO `Qt.application.screens[0]` maybe invalid, why screen is changed. onScreenChanged: { - // 修复:屏幕变化时重新获取dock所在的屏幕 root.screen = Qt.binding(function () { return getDockScreen() }) } @@ -162,7 +160,6 @@ Window { function onRequestClosePopup() { Panel.close() } - // 修复:监听dock屏幕变化,同步更新通知中心屏幕 function onScreenNameChanged() { root.screen = getDockScreen() }