Skip to content

Commit b801ada

Browse files
authored
Fix issue flameshot-org#564, that in wayland fractional scaling situtation, screenshot preview size is wrong. (flameshot-org#3869)
* fix screenshot's size not match with the screen when applying fractional scaling in KDE plasma desktop in wayland. * Fix: Really fix wayland dpr is not correct and that causes our screenshot preview is wrongly scaled issue. This fix supports that we will use dpr in xcb platform because it is correct in that case. * Chore: Use more concise variable names. Add comments to explain context. * Chore: Fix a typo(physcal -> physical). Change code style to meet the .clang-format requirement.
1 parent 6499b51 commit b801ada

File tree

2 files changed

+37
-2
lines changed

2 files changed

+37
-2
lines changed

src/utils/screengrabber.cpp

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,13 +80,36 @@ void ScreenGrabber::freeDesktopPortal(bool& ok, QPixmap& res)
8080
this);
8181

8282
QEventLoop loop;
83-
const auto gotSignal = [&res, &loop](uint status, const QVariantMap& map) {
83+
const auto gotSignal = [&res, &loop, this](uint status,
84+
const QVariantMap& map) {
8485
if (status == 0) {
8586
// Parse this as URI to handle unicode properly
8687
QUrl uri = map.value("uri").toString();
8788
QString uriString = uri.toLocalFile();
8889
res = QPixmap(uriString);
89-
res.setDevicePixelRatio(qApp->devicePixelRatio());
90+
91+
// we calculate an approximated physical desktop geometry based on
92+
// dpr(provided by qt), we calculate the logical desktop geometry
93+
// later, this is the accurate size, more info:
94+
// https://bugreports.qt.io/browse/QTBUG-135612
95+
QRect approxPhysGeo = desktopGeometry();
96+
QRect logicalGeo = logicalDesktopGeometry();
97+
if (res.size() ==
98+
approxPhysGeo.size()) // which means the res is physical size
99+
// and the dpr is correct.
100+
{
101+
res.setDevicePixelRatio(qApp->devicePixelRatio());
102+
} else if (res.size() ==
103+
logicalGeo.size()) // which means the res is logical size
104+
// and we need to do nothing.
105+
{
106+
// No action needed
107+
} else // which means the res is physical size and the dpr is not
108+
// correct.
109+
{
110+
res.setDevicePixelRatio(res.height() * 1.0f /
111+
logicalGeo.height());
112+
}
90113
QFile imgFile(uriString);
91114
imgFile.remove();
92115
}
@@ -248,3 +271,14 @@ QRect ScreenGrabber::desktopGeometry()
248271
}
249272
return geometry;
250273
}
274+
275+
QRect ScreenGrabber::logicalDesktopGeometry()
276+
{
277+
QRect geometry;
278+
for (QScreen* const screen : QGuiApplication::screens()) {
279+
QRect scrRect = screen->geometry();
280+
scrRect.moveTo(scrRect.x(), scrRect.y());
281+
geometry = geometry.united(scrRect);
282+
}
283+
return geometry;
284+
}

src/utils/screengrabber.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ class ScreenGrabber : public QObject
1818
void freeDesktopPortal(bool& ok, QPixmap& res);
1919
void generalGrimScreenshot(bool& ok, QPixmap& res);
2020
QRect desktopGeometry();
21+
QRect logicalDesktopGeometry();
2122

2223
private:
2324
DesktopInfo m_info;

0 commit comments

Comments
 (0)