Skip to content
Open
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
28 changes: 28 additions & 0 deletions frame/layershell/dlayershellwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ class DLayerShellWindowPrivate
}

QWindow* parentWindow;
// 绑定(锁存)的 output。为空表示尚未显式绑定,此时沿用 QWindow::screen()。
QPointer<QScreen> screen;
QString scope;
DLayerShellWindow::Anchors anchors = {DLayerShellWindow::AnchorNone};
int32_t exclusionZone = 0;
Expand Down Expand Up @@ -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) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟡 Medium · Non-background screen getter is stale

When a non-background layer has a saved screen from setScreen() and its QWindow screen later changes directly, DLayerShellWindow::screen() returns cached d->screen instead of the current parent/QWindow screen, so callers see the old output.

Make screen() follow the current QWindow screen for non-background layers instead of returning the saved d->screen.

Also at frame/layershell/dlayershellwindow.cpp:185-186.

Prompt for AI agents
In `frame/layershell/dlayershellwindow.cpp` at line 184:

**Non-background screen getter is stale**

When a non-background layer has a saved screen from `setScreen()` and its QWindow screen later changes directly, `DLayerShellWindow::screen()` returns cached `d->screen` instead of the current parent/QWindow screen, so callers see the old output.

Make `screen()` follow the current QWindow screen for non-background layers instead of returning the saved `d->screen`.

Also at `frame/layershell/dlayershellwindow.cpp:185-186`.

return d->screen.data();
}
return d->parentWindow ? d->parentWindow->screen() : nullptr;
}

void DLayerShellWindow::setPreferredWidth(int width)
{
if (width != d->preferredWidth) {
Expand Down
12 changes: 12 additions & 0 deletions frame/layershell/dlayershellwindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,17 @@
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;
Expand Down Expand Up @@ -168,6 +179,7 @@
void scopeChanged();
void geometryHintsChanged();
void inputRegionChanged();
void screenChanged(QScreen *screen);

Check warning on line 182 in frame/layershell/dlayershellwindow.h

View workflow job for this annotation

GitHub Actions / cppcheck

Local variable 'screenChanged' shadows outer function
void resizingChanged();
// Final size in QWindow coordinates, delivered before resizing becomes false.
void resizeFinished(const QSize &size);
Expand Down
51 changes: 49 additions & 2 deletions frame/layershell/x11dlayershellemulation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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("<null>"))
<< "winId:" << m_window->winId();
onLayerChanged();
connect(m_dlayerShellWindow, &DLayerShellWindow::layerChanged, this, &LayerShellEmulation::onLayerChanged);

Expand Down Expand Up @@ -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("<null>"));
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<void (QTimer::*)()>(&QTimer::start));

onScopeChanged();
Expand Down Expand Up @@ -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())) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟡 Medium · Background stays on old output

When a background window's QWindow screen changes directly, or a moved non-background window becomes a background layer, targetScreen() returns construction-time m_screen, so X11 geometry updates keep positioning the background on the old output, including through the bundled example's Window.screen path.

Use the current QWindow screen for background targeting when it has changed directly, rather than retaining the construction-time m_screen.

Prompt for AI agents
In `frame/layershell/x11dlayershellemulation.cpp` at line 212:

**Background stays on old output**

When a background window's QWindow screen changes directly, or a moved non-background window becomes a background layer, `targetScreen()` returns construction-time `m_screen`, so X11 geometry updates keep positioning the background on the old output, including through the bundled example's `Window.screen` path.

Use the current QWindow screen for background targeting when it has changed directly, rather than retaining the construction-time `m_screen`.

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;
}
Expand Down Expand Up @@ -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() 已经是设备无关像素,不需要缩放
Expand Down
6 changes: 6 additions & 0 deletions frame/layershell/x11dlayershellemulation.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@
#include "dsglobal.h"
#include "dlayershellwindow.h"

#include <QObject>

Check warning on line 10 in frame/layershell/x11dlayershellemulation.h

View workflow job for this annotation

GitHub Actions / cppcheck

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

Check warning on line 11 in frame/layershell/x11dlayershellemulation.h

View workflow job for this annotation

GitHub Actions / cppcheck

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

Check warning on line 12 in frame/layershell/x11dlayershellemulation.h

View workflow job for this annotation

GitHub Actions / cppcheck

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

Check warning on line 13 in frame/layershell/x11dlayershellemulation.h

View workflow job for this annotation

GitHub Actions / cppcheck

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

Check warning on line 14 in frame/layershell/x11dlayershellemulation.h

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 <xcb/xcb.h>
#include <xcb/xproto.h>
Expand All @@ -32,8 +34,12 @@
// void onKeyboardInteractivityChanged();

private:
// 背景层使用锁存输出,其他层兼容 QWindow::screen() 的动态选屏。
QScreen *targetScreen() const;

QWindow* m_window;
DLayerShellWindow* m_dlayerShellWindow;
QPointer<QScreen> m_screen;
QTimer m_exclusionZoneChangedTimer;
};
DS_END_NAMESPACE
6 changes: 4 additions & 2 deletions panels/dock/dockpanel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@

#include "dockpanel.h"
#include "constants.h"
#include "dockadaptor.h"

Check warning on line 7 in panels/dock/dockpanel.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: "dockadaptor.h" not found.
#include "docksettings.h"
#include "layershell/dlayershellwindow.h"

Check warning on line 9 in panels/dock/dockpanel.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: "layershell/dlayershellwindow.h" not found.
#include "panel.h"

Check warning on line 10 in panels/dock/dockpanel.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: "panel.h" not found.
#include "pluginfactory.h"

Check warning on line 11 in panels/dock/dockpanel.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: "pluginfactory.h" not found.
#include "waylanddockhelper.h"
#include "x11dockhelper.h"

Expand Down Expand Up @@ -112,7 +113,7 @@
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 {
Expand Down Expand Up @@ -570,7 +571,8 @@
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();
}
Expand Down
5 changes: 1 addition & 4 deletions panels/notification/center/package/main.qml
Original file line number Diff line number Diff line change
Expand Up @@ -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() })
}

Expand Down Expand Up @@ -162,7 +160,6 @@ Window {
function onRequestClosePopup() {
Panel.close()
}
// 修复:监听dock屏幕变化,同步更新通知中心屏幕
function onScreenNameChanged() {
root.screen = getDockScreen()
}
Expand Down
Loading