Skip to content
Merged
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
27 changes: 27 additions & 0 deletions frame/layershell/dlayershellwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,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 @@ -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) {
Expand Down
15 changes: 15 additions & 0 deletions frame/layershell/dlayershellwindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,20 @@
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;
Expand Down Expand Up @@ -164,9 +178,10 @@
void scopeChanged();
void geometryHintsChanged();
void inputRegionChanged();
void screenChanged(QScreen *screen);

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

View workflow job for this annotation

GitHub Actions / cppcheck

Local variable 'screenChanged' shadows outer function

private:
DLayerShellWindow(QWindow* window);

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

View workflow job for this annotation

GitHub Actions / cppcheck

Class 'DLayerShellWindow' 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.
QScopedPointer<DLayerShellWindowPrivate> d;
};

Expand Down
58 changes: 56 additions & 2 deletions frame/layershell/x11dlayershellemulation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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("<null>"))
<< "winId:" << m_window->winId();
onLayerChanged();
connect(m_dlayerShellWindow, &DLayerShellWindow::layerChanged, this, &LayerShellEmulation::onLayerChanged);

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

onScopeChanged();
Expand Down Expand Up @@ -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();
Comment thread
sourcery-ai[bot] marked this conversation as resolved.
Resolved
if (!screen) {
return;
}
Expand Down Expand Up @@ -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() 已经是设备无关像素,不需要缩放
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:
// 该 surface 所属的 output。创建时锁存,之后不再随窗口当前所在屏漂移。
QScreen *targetScreen() const;

QWindow* m_window;
DLayerShellWindow* m_dlayerShellWindow;
QPointer<QScreen> m_screen;
QTimer m_exclusionZoneChangedTimer;
};
DS_END_NAMESPACE
7 changes: 5 additions & 2 deletions panels/dock/dockpanel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,11 @@
#include <QGuiApplication>
#include <QLoggingCategory>
#include <QProcess>
#include <QQuickItem>

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

View workflow job for this annotation

GitHub Actions / cppcheck

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

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

View workflow job for this annotation

GitHub Actions / cppcheck

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

#include "layershell/dlayershellwindow.h"

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

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: "layershell/dlayershellwindow.h" not found.

#include <wayland/xdgactivation.h>

#include <QtWaylandClient/private/qwaylanddisplay_p.h>
Expand Down Expand Up @@ -109,7 +111,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_NAMESPACE::DLayerShellWindow::get(window())->setScreen(m_dockScreen);
onWindowGeometryChanged();
});
} else {
Expand Down Expand Up @@ -531,7 +533,8 @@
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();
}
Expand Down
2 changes: 2 additions & 0 deletions panels/notification/center/package/main.qml
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ Window {
onScreenChanged: {
// 修复:屏幕变化时重新获取dock所在的屏幕
root.screen = Qt.binding(function () { return getDockScreen() })
root.DLayerShellWindow.setScreen(root.screen)
}

onVisibleChanged: function (v) {
Expand Down Expand Up @@ -165,6 +166,7 @@ Window {
// 修复:监听dock屏幕变化,同步更新通知中心屏幕
function onScreenNameChanged() {
root.screen = getDockScreen()
root.DLayerShellWindow.setScreen(root.screen)
}
}

Expand Down
Loading